diff --git a/course/externallib.php b/course/externallib.php index 42a2369e245..2ed81936116 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -388,14 +388,8 @@ class core_course_external extends external_api { // We didn't this before to be able to retrieve stealth activities. foreach ($coursecontents as $sectionnumber => $sectioncontents) { $section = $sections[$sectionnumber]; - // Show the section if the user is permitted to access it OR - // if it's not available but there is some available info text which explains the reason & should display OR - // the course is configured to show hidden sections name. - $showsection = $section->uservisible || - ($section->visible && !$section->available && !empty($section->availableinfo)) || - (!$section->visible && empty($courseformat->get_course()->hiddensections)); - if (!$showsection) { + if (!$courseformat->is_section_visible($section)) { unset($coursecontents[$sectionnumber]); continue; } diff --git a/course/format/classes/base.php b/course/format/classes/base.php index 079cc4acf2c..36452825365 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -1210,6 +1210,28 @@ abstract class base { return ($sectionnum && ($course = $this->get_course()) && $course->marker == $sectionnum); } + /** + * Returns if an specific section is visible to the current user. + * + * Formats can overrride this method to implement any special section logic. + * + * @param section_info $section the section modinfo + * @return bool; + */ + public function is_section_visible(section_info $section): bool { + // Previous to Moodle 4.0 thas logic was hardcoded. To prevent errors in the contrib plugins + // the default logic is the same required for topics and weeks format and still uses + // a "hiddensections" format setting. + $course = $this->get_course(); + $hidesections = $course->hiddensections ?? true; + // Show the section if the user is permitted to access it, OR if it's not available + // but there is some available info text which explains the reason & should display, + // OR it is hidden but the course has a setting to display hidden sections as unavailable. + return $section->uservisible || + ($section->visible && !$section->available && !empty($section->availableinfo)) || + (!$section->visible && !$hidesections); + } + /** * return true if the course editor must be displayed. * diff --git a/course/format/classes/external/get_state.php b/course/format/classes/external/get_state.php index f718ac0d626..99c354f21bd 100644 --- a/course/format/classes/external/get_state.php +++ b/course/format/classes/external/get_state.php @@ -102,7 +102,7 @@ class get_state extends external_api { // Sections and course modules state. $sections = $modinfo->get_section_info_all(); foreach ($sections as $section) { - if (!empty($section->uservisible)) { + if ($courseformat->is_section_visible($section)) { // Only return this section data if it's visible by current user on the course page. $sectionstate = new $sectionclass($courseformat, $section); $result->section[] = $sectionstate->export_for_template($renderer); diff --git a/course/format/classes/output/local/content.php b/course/format/classes/output/local/content.php index 9c864fb7b4c..434cbdb8f88 100644 --- a/course/format/classes/output/local/content.php +++ b/course/format/classes/output/local/content.php @@ -153,13 +153,7 @@ class content implements renderable, templatable { continue; } - // Show the section if the user is permitted to access it, OR if it's not available - // but there is some available info text which explains the reason & should display, - // OR it is hidden but the course has a setting to display hidden sections as unavilable. - $showsection = $thissection->uservisible || - ($thissection->visible && !$thissection->available && !empty($thissection->availableinfo)) || - (!$thissection->visible && !$course->hiddensections); - if (!$showsection) { + if (!$format->is_section_visible($thissection)) { continue; } diff --git a/course/format/classes/output/local/state/course.php b/course/format/classes/output/local/state/course.php index e6f33e50401..475ff63c9bb 100644 --- a/course/format/classes/output/local/state/course.php +++ b/course/format/classes/output/local/state/course.php @@ -69,7 +69,7 @@ class course implements renderable { $sections = $modinfo->get_section_info_all(); foreach ($sections as $section) { - if (!empty($section->uservisible)) { + if ($format->is_section_visible($section)) { $data->sectionlist[] = $section->id; } } diff --git a/course/format/classes/output/local/state/section.php b/course/format/classes/output/local/state/section.php index 0b3fba0054e..f2294a26637 100644 --- a/course/format/classes/output/local/state/section.php +++ b/course/format/classes/output/local/state/section.php @@ -96,7 +96,7 @@ class section implements renderable { foreach ($modinfo->sections[$section->section] as $modnumber) { $mod = $modinfo->cms[$modnumber]; - if ($mod->is_visible_on_course_page()) { + if ($section->uservisible && $mod->is_visible_on_course_page()) { $data->cmlist[] = $mod->id; } } diff --git a/course/format/classes/output/section_renderer.php b/course/format/classes/output/section_renderer.php index f84ff3cf1d2..b0493ce94d3 100644 --- a/course/format/classes/output/section_renderer.php +++ b/course/format/classes/output/section_renderer.php @@ -1033,13 +1033,8 @@ abstract class section_renderer extends core_course_renderer { // Activities inside this section are 'orphaned', this section will be printed as 'stealth' below. continue; } - // Show the section if the user is permitted to access it, OR if it's not available - // but there is some available info text which explains the reason & should display, - // OR it is hidden but the course has a setting to display hidden sections as unavilable. - $showsection = $thissection->uservisible || - ($thissection->visible && !$thissection->available && !empty($thissection->availableinfo)) || - (!$thissection->visible && !$course->hiddensections); - if (!$showsection) { + + if (!$format->is_section_visible($thissection)) { continue; } diff --git a/course/format/classes/stateupdates.php b/course/format/classes/stateupdates.php index e4922e449f6..a3921da0082 100644 --- a/course/format/classes/stateupdates.php +++ b/course/format/classes/stateupdates.php @@ -111,14 +111,15 @@ class stateupdates implements JsonSerializable { } $course = $this->format->get_course(); $modinfo = course_modinfo::instance($course); + $format = $this->format; $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST); - if (!$section->uservisible) { + if (!$format->is_section_visible($section)) { return; } - $sectionclass = $this->format->get_output_classname('state\\section'); + $sectionclass = $format->get_output_classname('state\\section'); $currentstate = new $sectionclass($this->format, $section); $this->add_update('section', $action, $currentstate->export_for_template($this->output)); @@ -162,12 +163,13 @@ class stateupdates implements JsonSerializable { $cm = $modinfo->get_cm($cmid); $section = $modinfo->get_section_info_by_id($cm->section); + $format = $this->format; if (!$section->uservisible || !$cm->is_visible_on_course_page()) { return; } - $cmclass = $this->format->get_output_classname('state\\cm'); + $cmclass = $format->get_output_classname('state\\cm'); $currentstate = new $cmclass($this->format, $section, $cm); $this->add_update('cm', $action, $currentstate->export_for_template($this->output));