Skip to content

Commit

Permalink
MDL-74901 core_navigation: support callbacks in frontpage secondary nav
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
snake committed Jul 25, 2022
1 parent 2ccd074 commit ed09a5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
7 changes: 1 addition & 6 deletions index.php
Expand Up @@ -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');

Expand Down
44 changes: 22 additions & 22 deletions lib/classes/navigation/views/secondary.php
Expand Up @@ -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);
Expand All @@ -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)
);
}
}

/**
Expand Down

0 comments on commit ed09a5a

Please sign in to comment.