From f2ab1fd1d075ff6558bcb67125b5a73e899a0178 Mon Sep 17 00:00:00 2001 From: james-cnz <5689414+james-cnz@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:19:41 +1300 Subject: [PATCH] MDL-84045 core_courseformat: Restore section return functionality --- course/format/classes/base.php | 38 +++++++++---------- .../local/content/cm/delegatedcontrolmenu.php | 7 ++-- .../local/content/section/controlmenu.php | 7 ++-- course/format/topics/lib.php | 17 +-------- course/format/weeks/lib.php | 19 ++-------- 5 files changed, 30 insertions(+), 58 deletions(-) diff --git a/course/format/classes/base.php b/course/format/classes/base.php index bd1e343dea7..4ac75427695 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -903,32 +903,32 @@ abstract class base { * 'expanded' (bool) if true the section will be shown expanded, true by default * @return null|moodle_url */ - public function get_view_url($section, $options = array()) { + public function get_view_url($section, $options = []) { $course = $this->get_course(); - $url = new moodle_url('/course/view.php', ['id' => $course->id]); + $section = (is_object($section) || is_null($section)) ? $section : $this->get_section($section, IGNORE_MISSING); + // Determine page. if (array_key_exists('sr', $options)) { - $sectionno = $options['sr']; - } else if (is_object($section)) { - $sectionno = $section->section; + $pagesection = !is_null($options['sr']) ? $this->get_section($options['sr'], IGNORE_MISSING) : null; + } else if ($options['navigation'] ?? false) { + $pagesection = $section; } else { - $sectionno = $section; + $pagesection = null; } - if ((!empty($options['navigation']) || array_key_exists('sr', $options)) && $sectionno !== null) { - // Display section on separate page. - $sectioninfo = $this->get_section($sectionno); - return new moodle_url('/course/section.php', ['id' => $sectioninfo->id]); + + // Base URL. + if (is_null($pagesection)) { + $url = new moodle_url('/course/view.php', ['id' => $course->id]); + } else { + $url = new moodle_url('/course/section.php', ['id' => $pagesection->id]); } - if ($this->uses_sections() && $sectionno !== null) { - // The url includes the parameter to expand the section by default. - if (!array_key_exists('expanded', $options)) { - $options['expanded'] = true; + + // Add details. + if ($this->uses_sections() && $section && ($section->id != $pagesection?->id)) { + if ($options['expanded'] ?? true) { + $url->param('expandsection', $section->section); } - if ($options['expanded']) { - // This parameter is being set by default. - $url->param('expandsection', $sectionno); - } - $url->set_anchor('section-'.$sectionno); + $url->set_anchor('section-' . $section->section); } return $url; diff --git a/course/format/classes/output/local/content/cm/delegatedcontrolmenu.php b/course/format/classes/output/local/content/cm/delegatedcontrolmenu.php index ab11f47d13f..8d253a3c383 100644 --- a/course/format/classes/output/local/content/cm/delegatedcontrolmenu.php +++ b/course/format/classes/output/local/content/cm/delegatedcontrolmenu.php @@ -120,12 +120,11 @@ class delegatedcontrolmenu extends basecontrolmenu { return null; } + $sectionreturn = $this->format->get_sectionnum(); + $returnparams = !is_null($sectionreturn) ? ['sr' => $sectionreturn] : []; $url = new url( '/course/editsection.php', - [ - 'id' => $this->section->id, - 'sr' => $this->section->sectionnum, - ] + array_merge(['id' => $this->section->id], $returnparams) ); return new link_secondary( diff --git a/course/format/classes/output/local/content/section/controlmenu.php b/course/format/classes/output/local/content/section/controlmenu.php index 9c6f44581a4..815306190bf 100644 --- a/course/format/classes/output/local/content/section/controlmenu.php +++ b/course/format/classes/output/local/content/section/controlmenu.php @@ -136,12 +136,11 @@ class controlmenu extends basecontrolmenu { return null; } + $sectionreturn = $this->format->get_sectionnum(); + $returnparams = !is_null($sectionreturn) ? ['sr' => $sectionreturn] : []; $url = new url( '/course/editsection.php', - [ - 'id' => $this->section->id, - 'sr' => $this->section->sectionnum, - ] + array_merge(['id' => $this->section->id], $returnparams) ); return new link_secondary( diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index 0b1ddefd9dd..4d5dd05d9a1 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -110,21 +110,8 @@ class format_topics extends core_courseformat\base { * @return moodle_url */ public function get_view_url($section, $options = []) { - $course = $this->get_course(); - if (array_key_exists('sr', $options) && !is_null($options['sr'])) { - $sectionno = $options['sr']; - } else if (is_object($section)) { - $sectionno = $section->section; - } else { - $sectionno = $section; - } - if ((!empty($options['navigation']) || array_key_exists('sr', $options)) && $sectionno !== null) { - // Display section on separate page. - $sectioninfo = $this->get_section($sectionno); - return new moodle_url('/course/section.php', ['id' => $sectioninfo->id]); - } - - return new moodle_url('/course/view.php', ['id' => $course->id]); + $options['expanded'] = false; + return parent::get_view_url($section, $options); } /** diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 4a3e47433a0..35d3ba94c1d 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -126,22 +126,9 @@ class format_weeks extends core_courseformat\base { * 'sr' (int) used by course formats to specify to which section to return * @return moodle_url */ - public function get_view_url($section, $options = array()) { - $course = $this->get_course(); - if (array_key_exists('sr', $options) && !is_null($options['sr'])) { - $sectionno = $options['sr']; - } else if (is_object($section)) { - $sectionno = $section->section; - } else { - $sectionno = $section; - } - if ((!empty($options['navigation']) || array_key_exists('sr', $options)) && $sectionno !== null) { - // Display section on separate page. - $sectioninfo = $this->get_section($sectionno); - return new moodle_url('/course/section.php', ['id' => $sectioninfo->id]); - } - - return new moodle_url('/course/view.php', ['id' => $course->id]); + public function get_view_url($section, $options = []) { + $options['expanded'] = false; + return parent::get_view_url($section, $options); } /**