first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,92 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\navigation\output;
use renderable;
use renderer_base;
use templatable;
use custom_menu;
/**
* more menu navigation renderable
*
* @package core
* @category navigation
* @copyright 2021 onwards Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class more_menu implements renderable, templatable {
protected $content;
protected $navbarstyle;
protected $haschildren;
protected $istablist;
/**
* Constructor for this class.
*
* @param object $content Navigation objects.
* @param string $navbarstyle class name.
* @param bool $haschildren The content has children.
* @param bool $istablist When true, the more menu should be rendered and behave with a tablist ARIA role.
* If false, it's rendered with a menubar ARIA role. Defaults to false.
*/
public function __construct(object $content, string $navbarstyle, bool $haschildren = true, bool $istablist = false) {
$this->content = $content;
$this->navbarstyle = $navbarstyle;
$this->haschildren = $haschildren;
$this->istablist = $istablist;
}
/**
* Return data for rendering a template.
*
* @param renderer_base $output The output
* @return array Data for rendering a template
*/
public function export_for_template(renderer_base $output): array {
$data = [
'navbarstyle' => $this->navbarstyle,
'istablist' => $this->istablist,
];
if ($this->haschildren) {
// The node collection doesn't have anything to render so exit now.
if (!isset($this->content->children) || count($this->content->children) == 0) {
return [];
}
// Find all nodes that have children and are defined to show the children in a submenu.
// For each of these nodes we would like to display a dropdown menu and in order to achieve that
// (as required by the template) we need to set the node's property 'moremenuid' to a new unique value and
// 'haschildren' to true.
foreach ($this->content->children as &$item) {
if ($item->showchildreninsubmenu && isset($this->content->children) &&
count($this->content->children) > 0) {
$item->moremenuid = uniqid();
$item->haschildren = true;
}
}
$data['nodecollection'] = $this->content;
} else {
$data['nodearray'] = (array) $this->content;
}
$data['moremenuid'] = uniqid();
return $data;
}
}
+357
View File
@@ -0,0 +1,357 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\navigation\output;
use renderable;
use renderer_base;
use templatable;
use custom_menu;
/**
* Primary navigation renderable
*
* This file combines primary nav, custom menu, lang menu and
* usermenu into a standardized format for the frontend
*
* @package core
* @category navigation
* @copyright 2021 onwards Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class primary implements renderable, templatable {
/** @var \moodle_page $page the moodle page that the navigation belongs to */
private $page = null;
/**
* primary constructor.
* @param \moodle_page $page
*/
public function __construct($page) {
$this->page = $page;
}
/**
* Combine the various menus into a standardized output.
*
* @param renderer_base|null $output
* @return array
*/
public function export_for_template(?renderer_base $output = null): array {
if (!$output) {
$output = $this->page->get_renderer('core');
}
$menudata = (object) $this->merge_primary_and_custom($this->get_primary_nav(), $this->get_custom_menu($output));
$moremenu = new \core\navigation\output\more_menu($menudata, 'navbar-nav', false);
$mobileprimarynav = $this->merge_primary_and_custom($this->get_primary_nav(), $this->get_custom_menu($output), true);
$languagemenu = new \core\output\language_menu($this->page);
return [
'mobileprimarynav' => $mobileprimarynav,
'moremenu' => $moremenu->export_for_template($output),
'lang' => !isloggedin() || isguestuser() ? $languagemenu->export_for_template($output) : [],
'user' => $this->get_user_menu($output),
];
}
/**
* Get the primary nav object and standardize the output
*
* @param \navigation_node|null $parent used for nested nodes, by default the primarynav node
* @return array
*/
protected function get_primary_nav($parent = null): array {
if ($parent === null) {
$parent = $this->page->primarynav;
}
$nodes = [];
foreach ($parent->children as $node) {
$children = $this->get_primary_nav($node);
$activechildren = array_filter($children, function($child) {
return !empty($child['isactive']);
});
if ($node->preceedwithhr && count($nodes) && empty($nodes[count($nodes) - 1]['divider'])) {
$nodes[] = ['divider' => true];
}
$nodes[] = [
'title' => $node->get_title(),
'url' => $node->action(),
'text' => $node->text,
'icon' => $node->icon,
'isactive' => $node->isactive || !empty($activechildren),
'key' => $node->key,
'children' => $children,
'haschildren' => !empty($children) ? 1 : 0,
];
}
return $nodes;
}
/**
* Custom menu items reside on the same level as the original nodes.
* Fetch and convert the nodes to a standardised array.
*
* @param renderer_base $output
* @return array
*/
protected function get_custom_menu(renderer_base $output): array {
global $CFG;
// Early return if a custom menu does not exists.
if (empty($CFG->custommenuitems)) {
return [];
}
$custommenuitems = $CFG->custommenuitems;
$currentlang = current_language();
$custommenunodes = custom_menu::convert_text_to_menu_nodes($custommenuitems, $currentlang);
$nodes = [];
foreach ($custommenunodes as $node) {
$nodes[] = $node->export_for_template($output);
}
return $nodes;
}
/**
* When defining custom menu items, the active flag is not obvserved correctly. Therefore, the merge of the primary
* and custom navigation must be handled a bit smarter. Change the "isactive" flag of the nodes (this may set by
* default in the primary nav nodes but is entirely missing in the custom nav nodes).
* Set the $expandedmenu argument to true when the menu for the mobile template is build.
*
* @param array $primary
* @param array $custom
* @param bool $expandedmenu
* @return array
*/
protected function merge_primary_and_custom(array $primary, array $custom, bool $expandedmenu = false): array {
if (empty($custom)) {
return $primary; // No custom nav, nothing to merge.
}
// Remember the amount of primary nodes and whether we changed the active flag in the custom menu nodes.
$primarylen = count($primary);
$changed = false;
foreach (array_keys($custom) as $i) {
if (!$changed) {
if ($this->flag_active_nodes($custom[$i], $expandedmenu)) {
$changed = true;
}
}
$primary[] = $custom[$i];
}
// In case some custom node is active, mark all primary nav elements as inactive.
if ($changed) {
for ($i = 0; $i < $primarylen; $i++) {
$primary[$i]['isactive'] = false;
}
}
return $primary;
}
/**
* Recursive checks if any of the children is active. If that's the case this node (the parent) is active as
* well. If the node has no children, check if the node itself is active. Use pass by reference for the node
* object because we actively change/set the "isactive" flag inside the method and this needs to be kept at the
* callers side.
* Set $expandedmenu to true, if the mobile menu is done, in this case the active flag gets the node that is
* actually active, while the parent hierarchy of the active node gets the flag isopen.
*
* @param object $node
* @param bool $expandedmenu
* @return bool
*/
protected function flag_active_nodes(object $node, bool $expandedmenu = false): bool {
global $FULLME;
$active = false;
foreach (array_keys($node->children ?? []) as $c) {
if ($this->flag_active_nodes($node->children[$c], $expandedmenu)) {
$active = true;
}
}
// One of the children is active, so this node (the parent) is active as well.
if ($active) {
if ($expandedmenu) {
$node->isopen = true;
} else {
$node->isactive = true;
}
return true;
}
// By default, the menu item node to check is not active.
$node->isactive = false;
// Check if the node url matches the called url. The node url may omit the trailing index.php, therefore check
// this as well.
if (empty($node->url)) {
// Current menu node has no url set, so it can't be active.
return false;
}
$nodeurl = parse_url($node->url);
$current = parse_url($FULLME ?? '');
$pathmatches = false;
// Exact match of the path of node and current url.
$nodepath = $nodeurl['path'] ?? '/';
$currentpath = $current['path'] ?? '/';
if ($nodepath === $currentpath) {
$pathmatches = true;
}
// The current url may be trailed by a index.php, otherwise it's the same as the node path.
if (!$pathmatches && $nodepath . 'index.php' === $currentpath) {
$pathmatches = true;
}
// No path did match, so the node can't be active.
if (!$pathmatches) {
return false;
}
// We are here because the path matches, so now look at the query string.
$nodequery = $nodeurl['query'] ?? '';
$currentquery = $current['query'] ?? '';
// If the node has no query string defined, then the patch match is sufficient.
if (empty($nodeurl['query'])) {
$node->isactive = true;
return true;
}
// If the node contains a query string then also the current url must match this query.
if ($nodequery === $currentquery) {
$node->isactive = true;
}
return $node->isactive;
}
/**
* Get/Generate the user menu.
*
* This is leveraging the data from user_get_user_navigation_info and the logic in $OUTPUT->user_menu()
*
* @param renderer_base $output
* @return array
*/
public function get_user_menu(renderer_base $output): array {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/user/lib.php');
$usermenudata = [];
$submenusdata = [];
$info = user_get_user_navigation_info($USER, $PAGE);
if (isset($info->unauthenticateduser)) {
$info->unauthenticateduser['content'] = get_string($info->unauthenticateduser['content']);
$info->unauthenticateduser['url'] = get_login_url();
return (array) $info;
}
// Gather all the avatar data to be displayed in the user menu.
$usermenudata['avatardata'][] = [
'content' => $info->metadata['useravatar'],
'classes' => 'current'
];
$usermenudata['userfullname'] = $info->metadata['realuserfullname'] ?? $info->metadata['userfullname'];
// Logged in as someone else.
if ($info->metadata['asotheruser']) {
$usermenudata['avatardata'][] = [
'content' => $info->metadata['realuseravatar'],
'classes' => 'realuser'
];
$usermenudata['metadata'][] = [
'content' => get_string('loggedinas', 'moodle', $info->metadata['userfullname']),
'classes' => 'viewingas'
];
}
// Gather all the meta data to be displayed in the user menu.
$metadata = [
'asotherrole' => [
'value' => 'rolename',
'class' => 'role role-##GENERATEDCLASS##',
],
'userloginfail' => [
'value' => 'userloginfail',
'class' => 'loginfailures',
],
'asmnetuser' => [
'value' => 'mnetidprovidername',
'class' => 'mnet mnet-##GENERATEDCLASS##',
],
];
foreach ($metadata as $key => $value) {
if (!empty($info->metadata[$key])) {
$content = $info->metadata[$value['value']] ?? '';
$generatedclass = strtolower(preg_replace('#[ ]+#', '-', trim($content)));
$customclass = str_replace('##GENERATEDCLASS##', $generatedclass, ($value['class'] ?? ''));
$usermenudata['metadata'][] = [
'content' => $content,
'classes' => $customclass
];
}
}
$modifiedarray = array_map(function($value) {
$value->divider = $value->itemtype == 'divider';
$value->link = $value->itemtype == 'link';
if (isset($value->pix) && !empty($value->pix)) {
$value->pixicon = $value->pix;
unset($value->pix);
}
return $value;
}, $info->navitems);
// Include the language menu as a submenu within the user menu.
$languagemenu = new \core\output\language_menu($this->page);
$langmenu = $languagemenu->export_for_template($output);
if (!empty($langmenu)) {
$languageitems = $langmenu['items'];
// If there are available languages, generate the data for the the language selector submenu.
if (!empty($languageitems)) {
$langsubmenuid = uniqid();
// Generate the data for the link to language selector submenu.
$language = (object) [
'itemtype' => 'submenu-link',
'submenuid' => $langsubmenuid,
'title' => get_string('language'),
'divider' => false,
'submenulink' => true,
];
// Place the link before the 'Log out' menu item which is either the last item in the menu or
// second to last when 'Switch roles' is available.
$menuposition = count($modifiedarray) - 1;
if (has_capability('moodle/role:switchroles', $PAGE->context)) {
$menuposition = count($modifiedarray) - 2;
}
array_splice($modifiedarray, $menuposition, 0, [$language]);
// Generate the data for the language selector submenu.
$submenusdata[] = (object)[
'id' => $langsubmenuid,
'title' => get_string('languageselector'),
'items' => $languageitems,
];
}
}
// Add divider before the last item.
$modifiedarray[count($modifiedarray) - 2]->divider = true;
$usermenudata['items'] = $modifiedarray;
$usermenudata['submenus'] = array_values($submenusdata);
return $usermenudata;
}
}
+197
View File
@@ -0,0 +1,197 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\navigation\views;
use navigation_node;
/**
* 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 {
global $CFG;
if (during_initial_install() || $this->initialised) {
return;
}
$this->id = 'primary_navigation';
$showhomenode = empty($this->page->theme->removedprimarynavitems) ||
!in_array('home', $this->page->theme->removedprimarynavitems);
// We do not need to change the text for the home/dashboard depending on the set homepage.
if ($showhomenode) {
$sitehome = $this->add(get_string('home'), new \moodle_url('/'), self::TYPE_SYSTEM,
null, 'home', new \pix_icon('i/home', ''));
}
if (isloggedin() && !isguestuser()) {
$homepage = get_home_page();
if ($homepage == HOMEPAGE_MY || $homepage == HOMEPAGE_MYCOURSES) {
// We need to stop automatic redirection.
if ($showhomenode) {
$sitehome->action->param('redirect', '0');
}
}
// Add the dashboard link.
$showmyhomenode = !empty($CFG->enabledashboard) && (empty($this->page->theme->removedprimarynavitems) ||
!in_array('myhome', $this->page->theme->removedprimarynavitems));
if ($showmyhomenode) {
$this->add(get_string('myhome'), new \moodle_url('/my/'),
self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', ''));
}
// Add the mycourses link.
$showcoursesnode = empty($this->page->theme->removedprimarynavitems) ||
!in_array('courses', $this->page->theme->removedprimarynavitems);
if ($showcoursesnode) {
$this->add(get_string('mycourses'), new \moodle_url('/my/courses.php'), self::TYPE_ROOTNODE, null, 'mycourses');
}
}
$showsiteadminnode = empty($this->page->theme->removedprimarynavitems) ||
!in_array('siteadminnode', $this->page->theme->removedprimarynavitems);
if ($showsiteadminnode && $node = $this->get_site_admin_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);
}
// Allow plugins to add nodes to the primary navigation.
$hook = new \core\hook\navigation\primary_extend($this);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
// Search and set the active node.
$this->set_active_node();
$this->initialised = true;
}
/**
* Get the site admin node if available.
*
* @return navigation_node|null
*/
private function get_site_admin_node(): ?navigation_node {
// 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);
}
return $node ?: null;
}
/**
* Find and set the active node. Initially searches based on URL/explicitly set active node.
* If nothing is found, it checks the following:
* - If the node is a site page, set 'Home' as active
* - If within a course context, set 'My courses' as active
* - If within a course category context, set 'Site Admin' (if available) else set 'Home'
* - Else if available set site admin as active
* - Fallback, set 'Home' as active
*/
private function set_active_node(): void {
global $SITE;
$activenode = $this->search_and_set_active_node($this);
// If we haven't found an active node based on the standard search. Follow the criteria above.
if (!$activenode) {
$children = $this->get_children_key_list();
$navactivenode = $this->page->navigation->find_active_node();
$activekey = 'home';
if (isset($navactivenode->parent) && $navactivenode->parent->text == get_string('sitepages')) {
$activekey = 'home';
} else if (in_array($this->context->contextlevel, [CONTEXT_COURSE, CONTEXT_MODULE])) {
if ($this->page->course->id != $SITE->id) {
$activekey = 'courses';
}
} else if (in_array('siteadminnode', $children) && $node = $this->get_site_admin_node()) {
if ($this->context->contextlevel == CONTEXT_COURSECAT || $node->search_for_active_node(URL_MATCH_EXACT)) {
$activekey = 'siteadminnode';
}
}
if ($activekey && $activenode = $this->find($activekey, null)) {
$activenode->make_active();
}
}
}
/**
* 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()) {
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);
}
}
File diff suppressed because it is too large Load Diff
+153
View File
@@ -0,0 +1,153 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\navigation\views;
use navigation_node;
use navigation_node_collection;
/**
* Class view.
*
* The base view class which expands on the navigation_node,
*
* @package core
* @category navigation
* @copyright 2021 onwards Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class view extends navigation_node {
/** @var \stdClass $context the current context */
protected $context;
/** @var \moodle_page $page the moodle page that the navigation belongs to */
protected $page;
/** @var bool $initialised A switch to see if the navigation node is initialised */
protected $initialised = false;
/** @var navigation_node $activenode A string identifier for the active node*/
public $activenode;
/**
* Function to initialise the respective view
* @return void
*/
abstract public function initialise(): void;
/**
* navigation constructor.
* @param \moodle_page $page
*/
public function __construct(\moodle_page $page) {
global $FULLME;
if (during_initial_install()) {
return false;
}
$this->page = $page;
$this->context = $this->page->context;
$this->children = new navigation_node_collection();
}
/**
* Get the leaf nodes for the nav view
*
* @param navigation_node $source The settingsnav OR navigation object
* @param array $nodes An array of nodes to fetch from the source which specifies the node type and final order
* @return array $nodesordered The fetched nodes ordered based on final specification.
*/
protected function get_leaf_nodes(navigation_node $source, array $nodes): array {
$nodesordered = [];
foreach ($nodes as $type => $leaves) {
foreach ($leaves as $leaf => $location) {
if ($node = $source->find($leaf, $type)) {
$nodesordered["$location"] = $nodesordered["$location"] ?? $node;
}
}
}
return $nodesordered;
}
/**
* Scan the given node for the active node. It starts first with a strict search and then switches to a base search if
* required.
*
* @param navigation_node $node The node to scan.
* @return navigation_node|null The active node or null.
*/
protected function scan_for_active_node(navigation_node $node): ?navigation_node {
$result = $this->active_node_scan($node);
if (!is_null($result)) {
return $result;
} else {
return $this->active_node_scan($node, URL_MATCH_BASE);
}
}
/**
* This function recursively scans nodes until it finds the active node or there
* are no more nodes. We are using a custom implementation here to adjust the strictness
* and also because we need the parent node and not the specific child node in the new views.
* e.g. Structure for site admin,
* SecondaryNav
* - Site Admin
* - Users
* - User policies
* - Courses
* In the above example, if we are on the 'User Policies' page, the active node should be 'Users'
*
* @param navigation_node $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 {
$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.
}
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();
$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 $result;
}
}