From f8f83eae0eedbd6e7aa9dacb60e3ba8a5381d1e2 Mon Sep 17 00:00:00 2001 From: Peter Dias Date: Tue, 16 Feb 2021 13:28:43 +0800 Subject: [PATCH 1/2] MDL-70207 core_navigation: Initial backend for primary navigation --- lib/classes/navigation/views/primary.php | 67 ++++++++++++++++++++++++ lib/pagelib.php | 20 +++++++ version.php | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 lib/classes/navigation/views/primary.php diff --git a/lib/classes/navigation/views/primary.php b/lib/classes/navigation/views/primary.php new file mode 100644 index 00000000000..67f45c7ebe6 --- /dev/null +++ b/lib/classes/navigation/views/primary.php @@ -0,0 +1,67 @@ +. + +namespace core\navigation\views; + +/** + * Class primary. + * + * The primary navigation view is a combination of few components - navigation, output->navbar, + * + * @package core + * @category navigation + * @copyright 2021 onwards Peter Dias + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class primary extends view { + /** + * Initialise the primary navigation node + */ + public function initialise(): void { + if (during_initial_install() || $this->initialised) { + return; + } + $this->id = 'primary_navigation'; + $this->add(get_string('home'), new \moodle_url('/'), self::TYPE_SYSTEM, + null, 'home', new \pix_icon('i/home', '')); + + // Add the dashboard link. + if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in. + $this->rootnodes['home'] = $this->add(get_string('myhome'), new \moodle_url('/my/'), + self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', '')); + } + + // Add a dummy mycourse link to a mycourses page. + $this->add(get_string('mycourses'), new \moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); + + // Add the site admin node. We are using the settingsnav so as to avoid rechecking permissions again. + $settingsnav = $this->page->settingsnav; + $node = $settingsnav->find('siteadministration', self::TYPE_SITE_ADMIN); + if (!$node) { + // Try again. This node can exist with 2 different keys. + $node = $settingsnav->find('root', self::TYPE_SITE_ADMIN); + } + + if ($node) { + // We don't need everything from the node just the initial link. + $this->add($node->text, $node->action(), self::TYPE_SITE_ADMIN, null, 'siteadminnode', $node->icon); + } + + // Search and set the active node. + $this->search_for_active_node(); + $this->initialised = true; + } +} diff --git a/lib/pagelib.php b/lib/pagelib.php index 481eb2e5814..2f08c914f6d 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -26,6 +26,7 @@ */ defined('MOODLE_INTERNAL') || die(); +use core\navigation\views\primary; use core\navigation\views\secondary; /** @@ -82,6 +83,7 @@ use core\navigation\views\secondary; * @property-read navbar $navbar The navbar object used to display the navbar * @property-read secondary $secondarynav The secondary navigation object * used to display the secondarynav in boost + * @property-read primary $primarynav The primary navigation object used to display the primary nav in boost * @property-read global_navigation $navigation The navigation structure for this page. * @property-read xhtml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed. * mainly for internal use by the rendering code. @@ -303,6 +305,12 @@ class moodle_page { */ protected $_secondarynav = null; + /** + * @var primary Contains the nav nodes that will appear + * in the primary navigation. + */ + protected $_primarynav = null; + /** * @var navbar Contains the navbar structure. */ @@ -804,6 +812,18 @@ class moodle_page { return $this->_secondarynav; } + /** + * Returns the primary navigation object + * @return primary + */ + protected function magic_get_primarynav() { + if ($this->_primarynav === null) { + $this->_primarynav = new primary($this); + $this->_primarynav->initialise(); + } + return $this->_primarynav; + } + /** * Returns request IP address. * diff --git a/version.php b/version.php index 6797a5a73f8..2d0aa19bea6 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.73; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.74; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20210330)'; // Human-friendly version name From c8c39e403bba943ec4b24174aad7736c1c9da136 Mon Sep 17 00:00:00 2001 From: Peter Dias Date: Wed, 17 Feb 2021 14:05:07 +0800 Subject: [PATCH 2/2] MDL-70207 core_navigation: Primary nav unit tests --- lib/tests/navigation/views/primary_test.php | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/tests/navigation/views/primary_test.php diff --git a/lib/tests/navigation/views/primary_test.php b/lib/tests/navigation/views/primary_test.php new file mode 100644 index 00000000000..41f8e9efa9d --- /dev/null +++ b/lib/tests/navigation/views/primary_test.php @@ -0,0 +1,66 @@ +. + +namespace core\navigation\views; + +/** + * Class core_primary_testcase + * + * Unit test for the primary nav view. + * + * @package core + * @category navigation + * @copyright 2021 onwards Peter Dias + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class primary_test extends \advanced_testcase { + /** + * Test the initialise in different contexts + * + * @param string $usertype The user to setup for - admin, guest, regular user + * @param string $expected The expected nodes + * @dataProvider test_setting_initialise_provider + */ + public function test_setting_initialise($usertype, $expected) { + global $PAGE; + $PAGE->set_url("/"); + $this->resetAfterTest(); + if ($usertype == 'admin') { + $this->setAdminUser(); + } else if ($usertype == 'guest') { + $this->setGuestUser(); + } else { + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + } + + $node = new primary($PAGE); + $node->initialise(); + $children = $node->get_children_key_list(); + $this->assertEquals($expected, $children); + } + + /** + * Data provider for the test_setting_initialise function + */ + public function test_setting_initialise_provider() { + return [ + 'Testing as a guest user' => ['guest', ['home', 'courses']], + 'Testing as an admin' => ['admin', ['home', 'myhome', 'courses', 'siteadminnode']], + 'Testing as a regular user' => ['user', ['home', 'myhome', 'courses']] + ]; + } +}