From ed09a5ace4a87a20d95da4136659af1f8ec03b73 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 21 Jul 2022 14:22:26 +0800 Subject: [PATCH] MDL-74901 core_navigation: support callbacks in frontpage secondary nav Fixes the site home secondary nav, so that nodes added by plugins implementing the PLUGIN_extend_navigation_frontpage callback can be displayed for users who don't have the 'course:update' capability. This change comprises: - Removed course:update capability checks from site home (index.php) and from the secondary nav view. This isn't needed since the nav is capability aware. - Fixed the initialisation of the secondary nav for the frontpage course, removing erroneous duplicate 'home' nodes. The nav is now only shown if there are nodes to display. --- index.php | 7 +--- lib/classes/navigation/views/secondary.php | 44 +++++++++++----------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/index.php b/index.php index 82ae5cd6ba4c..b0ceb50e1afb 100644 --- a/index.php +++ b/index.php @@ -103,12 +103,7 @@ $editing = $PAGE->user_is_editing(); $PAGE->set_title($SITE->fullname); $PAGE->set_heading($SITE->fullname); -if (has_capability('moodle/course:update', context_system::instance())) { - $PAGE->set_secondary_navigation(true); - $PAGE->set_secondary_active_tab('coursehome'); -} else { - $PAGE->set_secondary_navigation(false); -} +$PAGE->set_secondary_active_tab('coursehome'); $courserenderer = $PAGE->get_renderer('core', 'course'); diff --git a/lib/classes/navigation/views/secondary.php b/lib/classes/navigation/views/secondary.php index 13ecdce8aecc..90089eb37134 100644 --- a/lib/classes/navigation/views/secondary.php +++ b/lib/classes/navigation/views/secondary.php @@ -392,30 +392,25 @@ protected function load_course_navigation(?navigation_node $rootnode = null): vo $settingsnav = $this->page->settingsnav; $navigation = $this->page->navigation; - $url = new \moodle_url('/course/view.php', ['id' => $course->id]); - $firstnodeidentifier = get_string('course'); - $issitecourse = $course->id == $SITE->id; - if ($issitecourse) { - $firstnodeidentifier = get_string('home'); - if ($frontpage = $settingsnav->get('frontpage')) { - $settingsnav = $frontpage; - } + if ($course->id == $SITE->id) { + $firstnodeidentifier = get_string('home'); // The first node in the site course nav is called 'Home'. + $frontpage = $settingsnav->get('frontpage'); // The site course nodes are children of a dedicated 'frontpage' node. + $settingsnav = $frontpage ?: $settingsnav; + $courseadminnode = $frontpage ?: null; // Custom nodes for the site course are also children of the 'frontpage' node. + } else { + $firstnodeidentifier = get_string('course'); // Regular courses have a first node called 'Course'. + $courseadminnode = $settingsnav->get('courseadmin'); // Custom nodes for regular courses live under 'courseadmin'. } - $rootnode->add($firstnodeidentifier, $url, self::TYPE_COURSE, null, 'coursehome'); + // Add the known nodes from settings and navigation. $nodes = $this->get_default_course_mapping(); $nodesordered = $this->get_leaf_nodes($settingsnav, $nodes['settings'] ?? []); $nodesordered += $this->get_leaf_nodes($navigation, $nodes['navigation'] ?? []); $this->add_ordered_nodes($nodesordered, $rootnode); - // Try to get any custom nodes defined by a user which may include containers. - $expectedcourseadmin = $this->get_expected_course_admin_nodes(); - $courseadminnode = $settingsnav; - if (!$issitecourse) { - $courseadminnode = $settingsnav->get('courseadmin'); - } - + // Try to get any custom nodes defined by plugins, which may include containers. if ($courseadminnode) { + $expectedcourseadmin = $this->get_expected_course_admin_nodes(); foreach ($courseadminnode->children as $other) { if (array_search($other->key, $expectedcourseadmin, true) === false) { $othernode = $this->get_first_action_for_node($other); @@ -427,17 +422,22 @@ protected function load_course_navigation(?navigation_node $rootnode = null): vo } } - $coursecontext = \context_course::instance($course->id); - if (has_capability('moodle/course:update', $coursecontext)) { - $overflownode = $this->get_course_overflow_nodes($rootnode); - if (is_null($overflownode)) { - return; - } + // Move some nodes into a 'course reuse' node. + $overflownode = $this->get_course_overflow_nodes($rootnode); + if (!is_null($overflownode)) { $actionnode = $this->get_first_action_for_node($overflownode); // All additional nodes will be available under the 'Course reuse' page. $text = get_string('coursereuse'); $rootnode->add($text, $actionnode->action, navigation_node::TYPE_COURSE, null, 'coursereuse', new \pix_icon('t/edit', $text)); } + + // Add the respective first node, provided there are other nodes included. + if (!empty($nodekeys = $rootnode->children->get_key_list())) { + $rootnode->add_node( + navigation_node::create($firstnodeidentifier, new \moodle_url('/course/view.php', ['id' => $course->id]), + self::TYPE_COURSE, null, 'coursehome'), reset($nodekeys) + ); + } } /**