Skip to content

Commit

Permalink
Merge branch 'MDL-72396-master' of https://github.com/sharidas/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 30, 2021
2 parents 2384dc7 + 3d18e00 commit a665ad2
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 20 deletions.
1 change: 1 addition & 0 deletions admin/search.php
Expand Up @@ -47,6 +47,7 @@
}

$PAGE->has_secondary_navigation_setter(false);
$PAGE->set_primary_active_tab('siteadminnode');

// and finally, if we get here, then there are matching settings and we have to print a form
// to modify them
Expand Down
3 changes: 3 additions & 0 deletions course/index.php
Expand Up @@ -65,6 +65,9 @@
$PAGE->set_heading($heading);
$content = $courserenderer->course_category($categoryid);

$PAGE->set_primary_active_tab('courses');
$PAGE->set_secondary_active_tab('categorymain');

echo $OUTPUT->header();
echo $OUTPUT->skip_link_target();
echo $content;
Expand Down
1 change: 1 addition & 0 deletions course/management.php
Expand Up @@ -108,6 +108,7 @@
$PAGE->set_title($strmanagement);
$PAGE->set_heading($pageheading);
$PAGE->requires->js_call_amd('core_course/copy_modal', 'init', array($context->id));
$PAGE->set_secondary_active_tab('managecategory');

// This is a system level page that operates on other contexts.
require_login();
Expand Down
58 changes: 57 additions & 1 deletion lib/classes/navigation/views/primary.php
Expand Up @@ -16,6 +16,8 @@

namespace core\navigation\views;

use navigation_node;

/**
* Class primary.
*
Expand Down Expand Up @@ -67,7 +69,61 @@ public function initialise(): void {
}

// Search and set the active node.
$this->search_for_active_node();
$this->search_and_set_active_node($this);
$this->initialised = true;
}

/**
* Searches all children for the matching active node
*
* This method recursively traverse through the node tree to
* find the node to activate/highlight:
* 1. If the user had set primary node key to highlight, it
* tries to match this key with the node(s). Hence it would
* travel all the nodes.
* 2. If no primary key is provided by the dev, then it would
* check for the active node set in the tree.
*
* @param navigation_node $node
* @param array $actionnodes navigation nodes array to set active and inactive.
* @return navigation_node|null
*/
private function search_and_set_active_node(navigation_node $node,
array &$actionnodes = []): ?navigation_node {
global $PAGE;

$activekey = $PAGE->get_primary_activate_tab();
if ($activekey) {
if ($node->key && ($activekey === $node->key)) {
return $node;
}
} else if ($node->check_if_active(URL_MATCH_BASE)) {
return $node;
}

foreach ($node->children as $child) {
$outcome = $this->search_and_set_active_node($child, $actionnodes);
if ($outcome !== null) {
$outcome->make_active();
$actionnodes['active'] = $outcome;
if ($activekey === null) {
return $actionnodes['active'];
}
} else {
// If the child is active then make it inactive.
if ($child->isactive) {
$actionnodes['set_inactive'][] = $child;
}
}
}

// If we have successfully found an active node then reset any other nodes to inactive.
if (isset($actionnodes['set_inactive']) && isset($actionnodes['active'])) {
foreach ($actionnodes['set_inactive'] as $inactivenode) {
$inactivenode->make_inactive();
}
$actionnodes['set_inactive'] = [];
}
return ($actionnodes['active'] ?? null);
}
}
47 changes: 29 additions & 18 deletions lib/classes/navigation/views/view.php
Expand Up @@ -113,31 +113,42 @@ protected function scan_for_active_node(navigation_node $node): ?navigation_node
* @param int $strictness How stict to be with the scan for the active node.
* @return navigation_node|null
*/
protected function active_node_scan(navigation_node $node, int $strictness = URL_MATCH_EXACT): ?navigation_node {
protected function active_node_scan(navigation_node $node,
int $strictness = URL_MATCH_EXACT): ?navigation_node {

if ($node->check_if_active($strictness)) {
$result = null;
$activekey = $this->page->get_secondary_active_tab();
if ($activekey) {
if ($node->key && $activekey === $node->key) {
return $node;
}
} else if ($node->check_if_active($strictness)) {
return $node; // No need to continue, exit function.
}

if ($node->children->count() > 0) {
foreach ($node->children as $child) {
if ($this->active_node_scan($child, $strictness)) {
// If node is one of the new views then set the active node to the child.
if (!$node instanceof view) {
$node->make_active();
$child->make_inactive();
} else {
$child->make_active();
$this->activenode = $child;
}

return $node; // We have found the active node, set the parent status, no need to continue.
} else {
// Make sure to reset the active state.
foreach ($node->children as $child) {
if ($this->active_node_scan($child, $strictness)) {
// If node is one of the new views then set the active node to the child.
if (!$node instanceof view) {
$node->make_active();
$child->make_inactive();
$result = $node;
} else {
$child->make_active();
$this->activenode = $child;
$result = $child;
}

// If the secondary active tab not set then just return the result (fallback).
if ($activekey === null) {
return $result;
}
} else {
// Make sure to reset the active state.
$child->make_inactive();
}
}
return null;

return $result;
}
}
46 changes: 46 additions & 0 deletions lib/pagelib.php
Expand Up @@ -393,6 +393,16 @@ class moodle_page {
*/
protected $_hassecondarynavigation = true;

/**
* @var string the key of the secondary node to be activated.
*/
protected $_activekeysecondary = null;

/**
* @var string the key of the primary node to be activated.
*/
protected $_activenodeprimary = null;

/**
* Force the settings menu to be displayed on this page. This will only force the
* settings menu on an activity / resource page that is being displayed on a theme that
Expand Down Expand Up @@ -2189,4 +2199,40 @@ public function has_secondary_navigation_setter(bool $value) : void {
public function has_secondary_navigation() : bool {
return $this->_hassecondarynavigation;
}

/**
* Set the key of the secondary nav node to be activated.
*
* @param string $navkey the key of the secondary nav node to be activated.
*/
public function set_secondary_active_tab(string $navkey) : void {
$this->_activekeysecondary = $navkey;
}

/**
* The key of secondary nav node to activate.
*
* @return string|null get the key of the secondary node to activate.
*/
public function get_secondary_active_tab(): ?string {
return $this->_activekeysecondary;
}

/**
* Set the key of the primary nav node to be activated.
*
* @param string $navkey
*/
public function set_primary_active_tab(string $navkey): void {
$this->_activenodeprimary = $navkey;
}

/**
* The key of the primary nav node to activate.
*
* @return string|null get the key of the primary nav node to activate.
*/
public function get_primary_activate_tab(): ?string {
return $this->_activenodeprimary;
}
}
106 changes: 106 additions & 0 deletions lib/tests/navigation/views/primary_test.php
Expand Up @@ -16,6 +16,9 @@

namespace core\navigation\views;

use navigation_node;
use ReflectionMethod;

/**
* Class core_primary_testcase
*
Expand Down Expand Up @@ -63,4 +66,107 @@ public function test_setting_initialise_provider() {
'Testing as a regular user' => ['user', ['home', 'myhome', 'courses']]
];
}

/**
* Get the nav tree initialised to test search_and_set_active_node.
*
* @param string|null $seturl The url set for $PAGE.
* @return navigation_node The initialised nav tree.
*/
private function get_tree_initilised_to_set_activate(?string $seturl = null): navigation_node {
$node = new navigation_node('My test node');
$node->type = navigation_node::TYPE_SYSTEM;
$node->add('first child', null, navigation_node::TYPE_CUSTOM, 'firstchld', 'firstchild');
$child2 = $node->add('second child', null, navigation_node::TYPE_COURSE, 'secondchld', 'secondchild');
$child3 = $node->add('third child', null, navigation_node::TYPE_CONTAINER, 'thirdchld', 'thirdchild');
$node->add('fourth child', null, navigation_node::TYPE_ACTIVITY, 'fourthchld', 'fourthchld');
$node->add('fifth child', '/my', navigation_node::TYPE_CATEGORY, 'fifthchld', 'fifthchild');

// If seturl is null then set actionurl of child6 to '/'.
if ($seturl === null) {
$child6actionurl = new \moodle_url('/');
} else {
// If seturl is provided then set actionurl of child6 to '/foo'.
$child6actionurl = new \moodle_url('/foo');
}
$child6 = $child2->add('sixth child', $child6actionurl, navigation_node::TYPE_COURSE, 'sixthchld', 'sixthchild');
// Activate the sixthchild node.
$child6->make_active();
$child2->add('seventh child', null, navigation_node::TYPE_COURSE, 'seventhchld', 'seventhchild');
$child8 = $child2->add('eighth child', null, navigation_node::TYPE_CUSTOM, 'eighthchld', 'eighthchild');
$child8->add('nineth child', null, navigation_node::TYPE_CUSTOM, 'ninethchld', 'ninethchild');
$child3->add('tenth child', null, navigation_node::TYPE_CUSTOM, 'tenthchld', 'tenthchild');

return $node;
}

/**
* Testing search_and_set_active_node.
*
* @param string $expectedkey Expected key of the node, if set.
* @param string|null $key The key of the node to activate.
* @param string|null $seturl Set the url for $PAGE.
* @return void
* @dataProvider test_search_and_set_active_node_provider
*/
public function test_search_and_set_active_node(string $expectedkey, ?string $key = null, ?string $seturl = null): void {
global $PAGE;

if ($seturl !== null) {
navigation_node::override_active_url(new \moodle_url($seturl));
} else {
$PAGE->set_url('/');
navigation_node::override_active_url(new \moodle_url('/'));
}
if ($key !== null) {
$PAGE->set_primary_active_tab($key);
}

$node = $this->get_tree_initilised_to_set_activate($seturl);

$primary = new primary($PAGE);
$method = new ReflectionMethod('core\navigation\views\primary', 'search_and_set_active_node');
$method->setAccessible(true);

$result = $method->invoke($primary, $node);

$sixthchildnode = $node->find('sixthchild', navigation_node::TYPE_COURSE);
if ($expectedkey !== '') {
$this->assertInstanceOf('navigation_node', $result);
$this->assertEquals($result->isactive, true);
$this->assertEquals($result->key, $expectedkey);

// Test the state of sixthchild, based on $expectedkey.
if ($expectedkey !== 'sixthchild') {
$this->assertFalse($sixthchildnode->isactive);
} else {
$this->assertTrue($sixthchildnode->isactive);
}
} else {
$this->assertNull($result);
$this->assertTrue($sixthchildnode->isactive);
}
}

/**
* Data provider for test_search_and_set_active_node
*
* @return array
*/
public function test_search_and_set_active_node_provider(): array {
return [
'Test by activating node which is part of the tree'
=> ['tenthchild', 'tenthchild'],
'Do not change the state of any nodes of the tree'
=> ['sixthchild'],
'Test by setting an empty string as node key to activate' => ['sixthchild', ''],
'Activate a node which does not exist in the tree'
=> ['', 'foobar'],
'Activate the leaf node of the tree' => ['ninethchild', 'ninethchild'],
'Test by changing the $PAGE url which is different from action url of child6'
=> ['', null, '/foobar'],
'Test by having $PAGE url and child6 action url same'
=> ['sixthchild', null, '/foo'],
];
}
}

0 comments on commit a665ad2

Please sign in to comment.