Skip to content

Commit

Permalink
Merge branch 'MDL-76099-400' of https://github.com/stevandoMoodle/moodle
Browse files Browse the repository at this point in the history
 into MOODLE_400_STABLE
  • Loading branch information
abgreeve committed Nov 11, 2022
2 parents 6981b82 + 921e34a commit 56a9861
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions backup/copyprogress.php
Expand Up @@ -48,6 +48,7 @@
$PAGE->set_heading($title);
$PAGE->set_secondary_active_tab('coursereuse');
$PAGE->requires->js_call_amd('core_backup/async_backup', 'asyncCopyAllStatus');
$PAGE->secondarynav->set_overflow_selected_node('copy');

// Build the page output.
echo $OUTPUT->header();
Expand Down
2 changes: 2 additions & 0 deletions backup/restore.php
Expand Up @@ -53,6 +53,8 @@
require_login($course, null, $cm);
require_capability('moodle/restore:restorecourse', $context);

$PAGE->secondarynav->set_overflow_selected_node('restore');

if (is_null($course)) {
$coursefullname = $SITE->fullname;
$courseshortname = $SITE->shortname;
Expand Down
60 changes: 59 additions & 1 deletion lib/classes/navigation/views/secondary.php
Expand Up @@ -41,6 +41,9 @@ class secondary extends view {
/** @var navigation_node The course overflow node. */
protected $courseoverflownode = null;

/** @var string The key of the node to set as selected in the course overflow menu, if explicitly set by a page. */
protected $overflowselected = null;

/**
* Defines the default structure for the secondary nav in a course context.
*
Expand Down Expand Up @@ -522,6 +525,55 @@ protected function nodes_match_current_url(navigation_node $node): ?navigation_n
return null;
}

/**
* Recursively search a node and its children for a node matching the key string $key.
*
* @param navigation_node $node the navigation node to check.
* @param string $key the key of the node to match.
* @return navigation_node|null node if found, otherwise null.
*/
protected function node_matches_key_string(navigation_node $node, string $key): ?navigation_node {
if ($node->has_action()) {
// Check this node first.
if ($node->key == $key) {
return $node;
}
}
if ($node->has_children()) {
foreach ($node->children as $child) {
$result = $this->node_matches_key_string($child, $key);
if ($result) {
return $result;
}
}
}
return null;
}

/**
* Force a specific node in the 'coursereuse' course overflow to be selected, based on the provided node key.
*
* Normally, the selected node is determined by matching the page URL to the node URL. E.g. The page 'backup/restorefile.php'
* will match the "Restore" node which has a registered URL of 'backup/restorefile.php' because the URLs match.
*
* This method allows a page to choose a specific node to match, which is useful in cases where the page knows its URL won't
* match the node it needs to reside under. I.e. this permits several pages to 'share' the same overflow node. When the page
* knows the PAGE->url won't match the node URL, the page can simply say "I want to match the 'XXX' node".
*
* E.g.
* - The $PAGE->url is 'backup/restore.php' (this page is used during restores but isn't the main landing page for a restore)
* - The 'Restore' node in the overflow has a key of 'restore' and will only match 'backup/restorefile.php' by default (the
* main restore landing page).
* - The backup/restore.php page calls:
* $PAGE->secondarynav->set_overflow_selected_node(new moodle_url('restore');
* and when the page is loaded, the 'Restore' node be presented as the selected node.
*
* @param string $nodekey The string key of the overflow node to match.
*/
public function set_overflow_selected_node(string $nodekey): void {
$this->overflowselected = $nodekey;
}

/**
* Returns a url_select object with overflow navigation nodes.
* This looks to see if the current page is within the course administration, or some other page that requires an overflow
Expand Down Expand Up @@ -578,7 +630,13 @@ public function get_overflow_menu_data(): ?url_select {
return null;
}
}
$menuselect = new url_select($menuarray, $this->page->url, null);
// If the page has explicitly set the overflow node it would like selected, find and use that node.
if ($this->overflowselected) {
$selectedoverflownode = $this->node_matches_key_string($courseoverflownode, $this->overflowselected);
$selectedoverflownodeurl = $selectedoverflownode ? $selectedoverflownode->action->out(false) : null;
}

$menuselect = new url_select($menuarray, $selectedoverflownodeurl ?? $this->page->url, null);
$menuselect->set_label(get_string('browsecourseadminindex', 'course'), ['class' => 'sr-only']);
return $menuselect;
} else {
Expand Down

0 comments on commit 56a9861

Please sign in to comment.