From 276123c343ac2661f86c911d0ea5d823b2591c9a Mon Sep 17 00:00:00 2001 From: David Herney Date: Wed, 15 May 2024 15:00:58 -0500 Subject: [PATCH] Fixed: Duplication of activitiy in section 0 #154 --- classes/header.php | 8 +++----- classes/output/courseformat/content.php | 18 ++++++++++++++--- .../output/courseformat/content/section.php | 13 ++++++++++-- lib.php | 20 +++++++++---------- styles.php | 2 ++ version.php | 4 ++-- 6 files changed, 43 insertions(+), 22 deletions(-) diff --git a/classes/header.php b/classes/header.php index f6c13c8..a5ec765 100644 --- a/classes/header.php +++ b/classes/header.php @@ -56,11 +56,11 @@ public function __construct(\format_onetopic $format) { /** * Export this data so it can be used as the context for a mustache template (core/inplace_editable). * - * @param renderer_base $output typically, the renderer that's calling this function + * @param \renderer_base $output typically, the renderer that's calling this function * @return stdClass data context for a mustache template */ public function export_for_template(\renderer_base $output) { - global $COURSE, $PAGE, $CFG, $OUTPUT; + global $PAGE, $CFG, $OUTPUT; $format = $this->format; $course = $this->format->get_course(); @@ -170,7 +170,6 @@ public function export_for_template(\renderer_base $output) { * @return \format_onetopic\tabs an object with tabs information */ private function get_tabs(course_modinfo $modinfo, \renderer_base $output): \format_onetopic\tabs { - global $PAGE; $course = $this->format->get_course(); $sections = $modinfo->get_section_info_all(); @@ -240,7 +239,7 @@ private function get_tabs(course_modinfo $modinfo, \renderer_base $output): \for } if ($section == 0) { - $url = new \moodle_url('/course/view.php', array('id' => $course->id, 'section' => 0)); + $url = new \moodle_url('/course/view.php', ['id' => $course->id, 'section' => 0]); } else { $url = course_get_url($course, $section); } @@ -261,7 +260,6 @@ private function get_tabs(course_modinfo $modinfo, \renderer_base $output): \for // Check if display available message is required. $availablemessage = null; if ($course->hiddensections == 2) { - $sectiontpl = new content_base\section($this->format, $thissection); $availabilityclass = $this->format->get_output_classname('content\\section\\availability'); $availability = new $availabilityclass($this->format, $thissection); $availabledata = $availability->export_for_template($output); diff --git a/classes/output/courseformat/content.php b/classes/output/courseformat/content.php index 8ed744c..2762c33 100644 --- a/classes/output/courseformat/content.php +++ b/classes/output/courseformat/content.php @@ -61,7 +61,7 @@ public function get_template_name(\renderer_base $renderer): string { /** * Export this data so it can be used as the context for a mustache template (core/inplace_editable). * - * @param renderer_base $output typically, the renderer that's calling this function + * @param \renderer_base $output typically, the renderer that's calling this function * @return stdClass data context for a mustache template */ public function export_for_template(\renderer_base $output) { @@ -123,7 +123,7 @@ public function export_for_template(\renderer_base $output) { /** * Export sections array data. * - * @param renderer_base $output typically, the renderer that's calling this function + * @param \renderer_base $output typically, the renderer that's calling this function * @return array data context for a mustache template */ protected function export_sections(\renderer_base $output): array { @@ -132,11 +132,16 @@ protected function export_sections(\renderer_base $output): array { $course = $format->get_course(); $modinfo = $this->format->get_modinfo(); + $realcoursedisplay = property_exists($course, 'realcoursedisplay') ? $course->realcoursedisplay : false; + $firstsectionastab = ($realcoursedisplay == COURSE_DISPLAY_MULTIPAGE) ? 1 : 0; + // Generate section list. $sections = []; $stealthsections = []; $numsections = $format->get_last_section_number(); - foreach ($this->get_sections_to_display($modinfo) as $sectionnum => $thissection) { + + foreach ($this->get_sections_to_display($modinfo) as $thissection) { + // The course/view.php check the section existence but the output can be called // from other parts so we need to check it. if (!$thissection) { @@ -144,6 +149,11 @@ protected function export_sections(\renderer_base $output): array { } $section = new $this->sectionclass($format, $thissection); + $sectionnum = $section->get_section_number(); + + if ($sectionnum === 0 && $firstsectionastab) { + continue; + } if ($sectionnum > $numsections) { // Activities inside this section are 'orphaned', this section will be printed as 'stealth' below. @@ -159,9 +169,11 @@ protected function export_sections(\renderer_base $output): array { $sections[] = $section->export_for_template($output); } + if (!empty($stealthsections)) { $sections = array_merge($sections, $stealthsections); } + return $sections; } diff --git a/classes/output/courseformat/content/section.php b/classes/output/courseformat/content/section.php index 12ff6c7..169e682 100644 --- a/classes/output/courseformat/content/section.php +++ b/classes/output/courseformat/content/section.php @@ -43,11 +43,11 @@ class section extends section_base { /** * Export this data so it can be used as the context for a mustache template. * - * @param renderer_base $output typically, the renderer that's calling this function + * @param \renderer_base $output typically, the renderer that's calling this function * @return array data context for a mustache template */ public function export_for_template(\renderer_base $output): stdClass { - global $USER, $PAGE; + global $PAGE; $format = $this->format; $course = $format->get_course(); @@ -76,4 +76,13 @@ public function export_for_template(\renderer_base $output): stdClass { return $data; } + + /** + * Get the section number. + * + * @return int + */ + public function get_section_number() { + return $this->section->section; + } } diff --git a/lib.php b/lib.php index 73215c1..094a13b 100644 --- a/lib.php +++ b/lib.php @@ -186,28 +186,25 @@ protected function __construct($format, $courseid) { } if ($course->realcoursedisplay == COURSE_DISPLAY_MULTIPAGE && $realsection === 0 && $numsections >= 1) { - $realsection = 1; + $realsection = null; } - // Can view the hidden sections in current course? - $canviewhidden = has_capability('moodle/course:viewhiddensections', $context); - $modinfo = get_fast_modinfo($course); $sections = $modinfo->get_section_info_all(); // Check if the display section is available. - if ((!$canviewhidden && (!$sections[$realsection]->uservisible || !$sections[$realsection]->available))) { + if ($realsection === null || !$sections[$realsection]->uservisible) { - self::$formatmsgs[] = get_string('hidden_message', 'format_onetopic', $this->get_section_name($realsection)); + if ($realsection) { + self::$formatmsgs[] = get_string('hidden_message', 'format_onetopic', $this->get_section_name($realsection)); + } $valid = false; $k = $course->realcoursedisplay ? 1 : 0; do { $formatoptions = $this->get_format_options($k); - if ($formatoptions['level'] == 0 - && ($sections[$k]->available && $sections[$k]->uservisible) - || $canviewhidden) { + if ($formatoptions['level'] == 0 && $sections[$k]->uservisible) { $valid = true; break; } @@ -219,7 +216,10 @@ protected function __construct($format, $courseid) { $realsection = $valid ? $k : 0; } + $realsection = $realsection ?? 0; + // The $section var is a global var, we need to set it to the real section. $section = $realsection; + $this->set_section_number($section); $USER->display[$course->id] = $realsection; $urlparams['section'] = $realsection; $PAGE->set_url('/course/view.php', $urlparams); @@ -989,7 +989,7 @@ public function fot_get_sections_extra() { } $course = $this->get_course(); - $realcoursedisplay = property_exists($course, 'coursedisplay') ? $course->coursedisplay : false; + $realcoursedisplay = property_exists($course, 'realcoursedisplay') ? $course->realcoursedisplay : false; $firstsection = ($realcoursedisplay == COURSE_DISPLAY_MULTIPAGE) ? 1 : 0; $sections = $this->get_sections(); $parentsections = []; diff --git a/styles.php b/styles.php index bd763cd..38bade1 100644 --- a/styles.php +++ b/styles.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ + define('NO_MOODLE_COOKIES', true); + // Require_login is not needed here. // phpcs:disable moodle.Files.RequireLogin.Missing require_once('../../../config.php'); diff --git a/version.php b/version.php index 6927a25..50d3a6e 100644 --- a/version.php +++ b/version.php @@ -24,9 +24,9 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022081611; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2022081612; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2022041902; // Requires this Moodle version. $plugin->component = 'format_onetopic'; // Full name of the plugin (used for diagnostics). $plugin->maturity = MATURITY_STABLE; -$plugin->release = '4.1.11(PiedrasTeherán)'; +$plugin->release = '4.1.12(PiedrasTeherán)'; $plugin->dependencies = ['format_topics' => 2022041900];