See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This file contains classes used to manage the navigation structures within Moodle. 19 * 20 * @since Moodle 2.0 21 * @package core 22 * @copyright 2009 Sam Hemelryk 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * The name that will be used to separate the navigation cache within SESSION 30 */ 31 define('NAVIGATION_CACHE_NAME', 'navigation'); 32 define('NAVIGATION_SITE_ADMIN_CACHE_NAME', 'navigationsiteadmin'); 33 34 /** 35 * This class is used to represent a node in a navigation tree 36 * 37 * This class is used to represent a node in a navigation tree within Moodle, 38 * the tree could be one of global navigation, settings navigation, or the navbar. 39 * Each node can be one of two types either a Leaf (default) or a branch. 40 * When a node is first created it is created as a leaf, when/if children are added 41 * the node then becomes a branch. 42 * 43 * @package core 44 * @category navigation 45 * @copyright 2009 Sam Hemelryk 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 47 */ 48 class navigation_node implements renderable { 49 /** @var int Used to identify this node a leaf (default) 0 */ 50 const NODETYPE_LEAF = 0; 51 /** @var int Used to identify this node a branch, happens with children 1 */ 52 const NODETYPE_BRANCH = 1; 53 /** @var null Unknown node type null */ 54 const TYPE_UNKNOWN = null; 55 /** @var int System node type 0 */ 56 const TYPE_ROOTNODE = 0; 57 /** @var int System node type 1 */ 58 const TYPE_SYSTEM = 1; 59 /** @var int Category node type 10 */ 60 const TYPE_CATEGORY = 10; 61 /** var int Category displayed in MyHome navigation node */ 62 const TYPE_MY_CATEGORY = 11; 63 /** @var int Course node type 20 */ 64 const TYPE_COURSE = 20; 65 /** @var int Course Structure node type 30 */ 66 const TYPE_SECTION = 30; 67 /** @var int Activity node type, e.g. Forum, Quiz 40 */ 68 const TYPE_ACTIVITY = 40; 69 /** @var int Resource node type, e.g. Link to a file, or label 50 */ 70 const TYPE_RESOURCE = 50; 71 /** @var int A custom node type, default when adding without specifing type 60 */ 72 const TYPE_CUSTOM = 60; 73 /** @var int Setting node type, used only within settings nav 70 */ 74 const TYPE_SETTING = 70; 75 /** @var int site admin branch node type, used only within settings nav 71 */ 76 const TYPE_SITE_ADMIN = 71; 77 /** @var int Setting node type, used only within settings nav 80 */ 78 const TYPE_USER = 80; 79 /** @var int Setting node type, used for containers of no importance 90 */ 80 const TYPE_CONTAINER = 90; 81 /** var int Course the current user is not enrolled in */ 82 const COURSE_OTHER = 0; 83 /** var int Course the current user is enrolled in but not viewing */ 84 const COURSE_MY = 1; 85 /** var int Course the current user is currently viewing */ 86 const COURSE_CURRENT = 2; 87 /** var string The course index page navigation node */ 88 const COURSE_INDEX_PAGE = 'courseindexpage'; 89 90 /** @var int Parameter to aid the coder in tracking [optional] */ 91 public $id = null; 92 /** @var string|int The identifier for the node, used to retrieve the node */ 93 public $key = null; 94 /** @var string The text to use for the node */ 95 public $text = null; 96 /** @var string Short text to use if requested [optional] */ 97 public $shorttext = null; 98 /** @var string The title attribute for an action if one is defined */ 99 public $title = null; 100 /** @var string A string that can be used to build a help button */ 101 public $helpbutton = null; 102 /** @var moodle_url|action_link|null An action for the node (link) */ 103 public $action = null; 104 /** @var pix_icon The path to an icon to use for this node */ 105 public $icon = null; 106 /** @var int See TYPE_* constants defined for this class */ 107 public $type = self::TYPE_UNKNOWN; 108 /** @var int See NODETYPE_* constants defined for this class */ 109 public $nodetype = self::NODETYPE_LEAF; 110 /** @var bool If set to true the node will be collapsed by default */ 111 public $collapse = false; 112 /** @var bool If set to true the node will be expanded by default */ 113 public $forceopen = false; 114 /** @var array An array of CSS classes for the node */ 115 public $classes = array(); 116 /** @var navigation_node_collection An array of child nodes */ 117 public $children = array(); 118 /** @var bool If set to true the node will be recognised as active */ 119 public $isactive = false; 120 /** @var bool If set to true the node will be dimmed */ 121 public $hidden = false; 122 /** @var bool If set to false the node will not be displayed */ 123 public $display = true; 124 /** @var bool If set to true then an HR will be printed before the node */ 125 public $preceedwithhr = false; 126 /** @var bool If set to true the the navigation bar should ignore this node */ 127 public $mainnavonly = false; 128 /** @var bool If set to true a title will be added to the action no matter what */ 129 public $forcetitle = false; 130 /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */ 131 public $parent = null; 132 /** @var bool Override to not display the icon even if one is provided **/ 133 public $hideicon = false; 134 /** @var bool Set to true if we KNOW that this node can be expanded. */ 135 public $isexpandable = false; 136 /** @var array */ 137 protected $namedtypes = array(0 => 'system', 10 => 'category', 20 => 'course', 30 => 'structure', 40 => 'activity', 138 50 => 'resource', 60 => 'custom', 70 => 'setting', 71 => 'siteadmin', 80 => 'user', 139 90 => 'container'); 140 /** @var moodle_url */ 141 protected static $fullmeurl = null; 142 /** @var bool toogles auto matching of active node */ 143 public static $autofindactive = true; 144 /** @var bool should we load full admin tree or rely on AJAX for performance reasons */ 145 protected static $loadadmintree = false; 146 /** @var mixed If set to an int, that section will be included even if it has no activities */ 147 public $includesectionnum = false; 148 /** @var bool does the node need to be loaded via ajax */ 149 public $requiresajaxloading = false; 150 /** @var bool If set to true this node will be added to the "flat" navigation */ 151 public $showinflatnavigation = false; 152 153 /** 154 * Constructs a new navigation_node 155 * 156 * @param array|string $properties Either an array of properties or a string to use 157 * as the text for the node 158 */ 159 public function __construct($properties) { 160 if (is_array($properties)) { 161 // Check the array for each property that we allow to set at construction. 162 // text - The main content for the node 163 // shorttext - A short text if required for the node 164 // icon - The icon to display for the node 165 // type - The type of the node 166 // key - The key to use to identify the node 167 // parent - A reference to the nodes parent 168 // action - The action to attribute to this node, usually a URL to link to 169 if (array_key_exists('text', $properties)) { 170 $this->text = $properties['text']; 171 } 172 if (array_key_exists('shorttext', $properties)) { 173 $this->shorttext = $properties['shorttext']; 174 } 175 if (!array_key_exists('icon', $properties)) { 176 $properties['icon'] = new pix_icon('i/navigationitem', ''); 177 } 178 $this->icon = $properties['icon']; 179 if ($this->icon instanceof pix_icon) { 180 if (empty($this->icon->attributes['class'])) { 181 $this->icon->attributes['class'] = 'navicon'; 182 } else { 183 $this->icon->attributes['class'] .= ' navicon'; 184 } 185 } 186 if (array_key_exists('type', $properties)) { 187 $this->type = $properties['type']; 188 } else { 189 $this->type = self::TYPE_CUSTOM; 190 } 191 if (array_key_exists('key', $properties)) { 192 $this->key = $properties['key']; 193 } 194 // This needs to happen last because of the check_if_active call that occurs 195 if (array_key_exists('action', $properties)) { 196 $this->action = $properties['action']; 197 if (is_string($this->action)) { 198 $this->action = new moodle_url($this->action); 199 } 200 if (self::$autofindactive) { 201 $this->check_if_active(); 202 } 203 } 204 if (array_key_exists('parent', $properties)) { 205 $this->set_parent($properties['parent']); 206 } 207 } else if (is_string($properties)) { 208 $this->text = $properties; 209 } 210 if ($this->text === null) { 211 throw new coding_exception('You must set the text for the node when you create it.'); 212 } 213 // Instantiate a new navigation node collection for this nodes children 214 $this->children = new navigation_node_collection(); 215 } 216 217 /** 218 * Checks if this node is the active node. 219 * 220 * This is determined by comparing the action for the node against the 221 * defined URL for the page. A match will see this node marked as active. 222 * 223 * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE 224 * @return bool 225 */ 226 public function check_if_active($strength=URL_MATCH_EXACT) { 227 global $FULLME, $PAGE; 228 // Set fullmeurl if it hasn't already been set 229 if (self::$fullmeurl == null) { 230 if ($PAGE->has_set_url()) { 231 self::override_active_url(new moodle_url($PAGE->url)); 232 } else { 233 self::override_active_url(new moodle_url($FULLME)); 234 } 235 } 236 237 // Compare the action of this node against the fullmeurl 238 if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) { 239 $this->make_active(); 240 return true; 241 } 242 return false; 243 } 244 245 /** 246 * True if this nav node has siblings in the tree. 247 * 248 * @return bool 249 */ 250 public function has_siblings() { 251 if (empty($this->parent) || empty($this->parent->children)) { 252 return false; 253 } 254 if ($this->parent->children instanceof navigation_node_collection) { 255 $count = $this->parent->children->count(); 256 } else { 257 $count = count($this->parent->children); 258 } 259 return ($count > 1); 260 } 261 262 /** 263 * Get a list of sibling navigation nodes at the same level as this one. 264 * 265 * @return bool|array of navigation_node 266 */ 267 public function get_siblings() { 268 // Returns a list of the siblings of the current node for display in a flat navigation element. Either 269 // the in-page links or the breadcrumb links. 270 $siblings = false; 271 272 if ($this->has_siblings()) { 273 $siblings = []; 274 foreach ($this->parent->children as $child) { 275 if ($child->display) { 276 $siblings[] = $child; 277 } 278 } 279 } 280 return $siblings; 281 } 282 283 /** 284 * This sets the URL that the URL of new nodes get compared to when locating 285 * the active node. 286 * 287 * The active node is the node that matches the URL set here. By default this 288 * is either $PAGE->url or if that hasn't been set $FULLME. 289 * 290 * @param moodle_url $url The url to use for the fullmeurl. 291 * @param bool $loadadmintree use true if the URL point to administration tree 292 */ 293 public static function override_active_url(moodle_url $url, $loadadmintree = false) { 294 // Clone the URL, in case the calling script changes their URL later. 295 self::$fullmeurl = new moodle_url($url); 296 // True means we do not want AJAX loaded admin tree, required for all admin pages. 297 if ($loadadmintree) { 298 // Do not change back to false if already set. 299 self::$loadadmintree = true; 300 } 301 } 302 303 /** 304 * Use when page is linked from the admin tree, 305 * if not used navigation could not find the page using current URL 306 * because the tree is not fully loaded. 307 */ 308 public static function require_admin_tree() { 309 self::$loadadmintree = true; 310 } 311 312 /** 313 * Creates a navigation node, ready to add it as a child using add_node 314 * function. (The created node needs to be added before you can use it.) 315 * @param string $text 316 * @param moodle_url|action_link $action 317 * @param int $type 318 * @param string $shorttext 319 * @param string|int $key 320 * @param pix_icon $icon 321 * @return navigation_node 322 */ 323 public static function create($text, $action=null, $type=self::TYPE_CUSTOM, 324 $shorttext=null, $key=null, pix_icon $icon=null) { 325 // Properties array used when creating the new navigation node 326 $itemarray = array( 327 'text' => $text, 328 'type' => $type 329 ); 330 // Set the action if one was provided 331 if ($action!==null) { 332 $itemarray['action'] = $action; 333 } 334 // Set the shorttext if one was provided 335 if ($shorttext!==null) { 336 $itemarray['shorttext'] = $shorttext; 337 } 338 // Set the icon if one was provided 339 if ($icon!==null) { 340 $itemarray['icon'] = $icon; 341 } 342 // Set the key 343 $itemarray['key'] = $key; 344 // Construct and return 345 return new navigation_node($itemarray); 346 } 347 348 /** 349 * Adds a navigation node as a child of this node. 350 * 351 * @param string $text 352 * @param moodle_url|action_link $action 353 * @param int $type 354 * @param string $shorttext 355 * @param string|int $key 356 * @param pix_icon $icon 357 * @return navigation_node 358 */ 359 public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { 360 // Create child node 361 $childnode = self::create($text, $action, $type, $shorttext, $key, $icon); 362 363 // Add the child to end and return 364 return $this->add_node($childnode); 365 } 366 367 /** 368 * Adds a navigation node as a child of this one, given a $node object 369 * created using the create function. 370 * @param navigation_node $childnode Node to add 371 * @param string $beforekey 372 * @return navigation_node The added node 373 */ 374 public function add_node(navigation_node $childnode, $beforekey=null) { 375 // First convert the nodetype for this node to a branch as it will now have children 376 if ($this->nodetype !== self::NODETYPE_BRANCH) { 377 $this->nodetype = self::NODETYPE_BRANCH; 378 } 379 // Set the parent to this node 380 $childnode->set_parent($this); 381 382 // Default the key to the number of children if not provided 383 if ($childnode->key === null) { 384 $childnode->key = $this->children->count(); 385 } 386 387 // Add the child using the navigation_node_collections add method 388 $node = $this->children->add($childnode, $beforekey); 389 390 // If added node is a category node or the user is logged in and it's a course 391 // then mark added node as a branch (makes it expandable by AJAX) 392 $type = $childnode->type; 393 if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY) || 394 ($type === self::TYPE_SITE_ADMIN)) { 395 $node->nodetype = self::NODETYPE_BRANCH; 396 } 397 // If this node is hidden mark it's children as hidden also 398 if ($this->hidden) { 399 $node->hidden = true; 400 } 401 // Return added node (reference returned by $this->children->add() 402 return $node; 403 } 404 405 /** 406 * Return a list of all the keys of all the child nodes. 407 * @return array the keys. 408 */ 409 public function get_children_key_list() { 410 return $this->children->get_key_list(); 411 } 412 413 /** 414 * Searches for a node of the given type with the given key. 415 * 416 * This searches this node plus all of its children, and their children.... 417 * If you know the node you are looking for is a child of this node then please 418 * use the get method instead. 419 * 420 * @param int|string $key The key of the node we are looking for 421 * @param int $type One of navigation_node::TYPE_* 422 * @return navigation_node|false 423 */ 424 public function find($key, $type) { 425 return $this->children->find($key, $type); 426 } 427 428 /** 429 * Walk the tree building up a list of all the flat navigation nodes. 430 * 431 * @param flat_navigation $nodes List of the found flat navigation nodes. 432 * @param boolean $showdivider Show a divider before the first node. 433 * @param string $label A label for the collection of navigation links. 434 */ 435 public function build_flat_navigation_list(flat_navigation $nodes, $showdivider = false, $label = '') { 436 if ($this->showinflatnavigation) { 437 $indent = 0; 438 if ($this->type == self::TYPE_COURSE || $this->key === self::COURSE_INDEX_PAGE) { 439 $indent = 1; 440 } 441 $flat = new flat_navigation_node($this, $indent); 442 $flat->set_showdivider($showdivider, $label); 443 $nodes->add($flat); 444 } 445 foreach ($this->children as $child) { 446 $child->build_flat_navigation_list($nodes, false); 447 } 448 } 449 450 /** 451 * Get the child of this node that has the given key + (optional) type. 452 * 453 * If you are looking for a node and want to search all children + their children 454 * then please use the find method instead. 455 * 456 * @param int|string $key The key of the node we are looking for 457 * @param int $type One of navigation_node::TYPE_* 458 * @return navigation_node|false 459 */ 460 public function get($key, $type=null) { 461 return $this->children->get($key, $type); 462 } 463 464 /** 465 * Removes this node. 466 * 467 * @return bool 468 */ 469 public function remove() { 470 return $this->parent->children->remove($this->key, $this->type); 471 } 472 473 /** 474 * Checks if this node has or could have any children 475 * 476 * @return bool Returns true if it has children or could have (by AJAX expansion) 477 */ 478 public function has_children() { 479 return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0 || $this->isexpandable); 480 } 481 482 /** 483 * Marks this node as active and forces it open. 484 * 485 * Important: If you are here because you need to mark a node active to get 486 * the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}? 487 * You can use it to specify a different URL to match the active navigation node on 488 * rather than having to locate and manually mark a node active. 489 */ 490 public function make_active() { 491 $this->isactive = true; 492 $this->add_class('active_tree_node'); 493 $this->force_open(); 494 if ($this->parent !== null) { 495 $this->parent->make_inactive(); 496 } 497 } 498 499 /** 500 * Marks a node as inactive and recusised back to the base of the tree 501 * doing the same to all parents. 502 */ 503 public function make_inactive() { 504 $this->isactive = false; 505 $this->remove_class('active_tree_node'); 506 if ($this->parent !== null) { 507 $this->parent->make_inactive(); 508 } 509 } 510 511 /** 512 * Forces this node to be open and at the same time forces open all 513 * parents until the root node. 514 * 515 * Recursive. 516 */ 517 public function force_open() { 518 $this->forceopen = true; 519 if ($this->parent !== null) { 520 $this->parent->force_open(); 521 } 522 } 523 524 /** 525 * Adds a CSS class to this node. 526 * 527 * @param string $class 528 * @return bool 529 */ 530 public function add_class($class) { 531 if (!in_array($class, $this->classes)) { 532 $this->classes[] = $class; 533 } 534 return true; 535 } 536 537 /** 538 * Removes a CSS class from this node. 539 * 540 * @param string $class 541 * @return bool True if the class was successfully removed. 542 */ 543 public function remove_class($class) { 544 if (in_array($class, $this->classes)) { 545 $key = array_search($class,$this->classes); 546 if ($key!==false) { 547 // Remove the class' array element. 548 unset($this->classes[$key]); 549 // Reindex the array to avoid failures when the classes array is iterated later in mustache templates. 550 $this->classes = array_values($this->classes); 551 552 return true; 553 } 554 } 555 return false; 556 } 557 558 /** 559 * Sets the title for this node and forces Moodle to utilise it. 560 * @param string $title 561 */ 562 public function title($title) { 563 $this->title = $title; 564 $this->forcetitle = true; 565 } 566 567 /** 568 * Resets the page specific information on this node if it is being unserialised. 569 */ 570 public function __wakeup(){ 571 $this->forceopen = false; 572 $this->isactive = false; 573 $this->remove_class('active_tree_node'); 574 } 575 576 /** 577 * Checks if this node or any of its children contain the active node. 578 * 579 * Recursive. 580 * 581 * @return bool 582 */ 583 public function contains_active_node() { 584 if ($this->isactive) { 585 return true; 586 } else { 587 foreach ($this->children as $child) { 588 if ($child->isactive || $child->contains_active_node()) { 589 return true; 590 } 591 } 592 } 593 return false; 594 } 595 596 /** 597 * To better balance the admin tree, we want to group all the short top branches together. 598 * 599 * This means < 8 nodes and no subtrees. 600 * 601 * @return bool 602 */ 603 public function is_short_branch() { 604 $limit = 8; 605 if ($this->children->count() >= $limit) { 606 return false; 607 } 608 foreach ($this->children as $child) { 609 if ($child->has_children()) { 610 return false; 611 } 612 } 613 return true; 614 } 615 616 /** 617 * Finds the active node. 618 * 619 * Searches this nodes children plus all of the children for the active node 620 * and returns it if found. 621 * 622 * Recursive. 623 * 624 * @return navigation_node|false 625 */ 626 public function find_active_node() { 627 if ($this->isactive) { 628 return $this; 629 } else { 630 foreach ($this->children as &$child) { 631 $outcome = $child->find_active_node(); 632 if ($outcome !== false) { 633 return $outcome; 634 } 635 } 636 } 637 return false; 638 } 639 640 /** 641 * Searches all children for the best matching active node 642 * @return navigation_node|false 643 */ 644 public function search_for_active_node() { 645 if ($this->check_if_active(URL_MATCH_BASE)) { 646 return $this; 647 } else { 648 foreach ($this->children as &$child) { 649 $outcome = $child->search_for_active_node(); 650 if ($outcome !== false) { 651 return $outcome; 652 } 653 } 654 } 655 return false; 656 } 657 658 /** 659 * Gets the content for this node. 660 * 661 * @param bool $shorttext If true shorttext is used rather than the normal text 662 * @return string 663 */ 664 public function get_content($shorttext=false) { 665 if ($shorttext && $this->shorttext!==null) { 666 return format_string($this->shorttext); 667 } else { 668 return format_string($this->text); 669 } 670 } 671 672 /** 673 * Gets the title to use for this node. 674 * 675 * @return string 676 */ 677 public function get_title() { 678 if ($this->forcetitle || $this->action != null){ 679 return $this->title; 680 } else { 681 return ''; 682 } 683 } 684 685 /** 686 * Used to easily determine if this link in the breadcrumbs has a valid action/url. 687 * 688 * @return boolean 689 */ 690 public function has_action() { 691 return !empty($this->action); 692 } 693 694 /** 695 * Used to easily determine if this link in the breadcrumbs is hidden. 696 * 697 * @return boolean 698 */ 699 public function is_hidden() { 700 return $this->hidden; 701 } 702 703 /** 704 * Gets the CSS class to add to this node to describe its type 705 * 706 * @return string 707 */ 708 public function get_css_type() { 709 if (array_key_exists($this->type, $this->namedtypes)) { 710 return 'type_'.$this->namedtypes[$this->type]; 711 } 712 return 'type_unknown'; 713 } 714 715 /** 716 * Finds all nodes that are expandable by AJAX 717 * 718 * @param array $expandable An array by reference to populate with expandable nodes. 719 */ 720 public function find_expandable(array &$expandable) { 721 foreach ($this->children as &$child) { 722 if ($child->display && $child->has_children() && $child->children->count() == 0) { 723 $child->id = 'expandable_branch_'.$child->type.'_'.clean_param($child->key, PARAM_ALPHANUMEXT); 724 $this->add_class('canexpand'); 725 $child->requiresajaxloading = true; 726 $expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type); 727 } 728 $child->find_expandable($expandable); 729 } 730 } 731 732 /** 733 * Finds all nodes of a given type (recursive) 734 * 735 * @param int $type One of navigation_node::TYPE_* 736 * @return array 737 */ 738 public function find_all_of_type($type) { 739 $nodes = $this->children->type($type); 740 foreach ($this->children as &$node) { 741 $childnodes = $node->find_all_of_type($type); 742 $nodes = array_merge($nodes, $childnodes); 743 } 744 return $nodes; 745 } 746 747 /** 748 * Removes this node if it is empty 749 */ 750 public function trim_if_empty() { 751 if ($this->children->count() == 0) { 752 $this->remove(); 753 } 754 } 755 756 /** 757 * Creates a tab representation of this nodes children that can be used 758 * with print_tabs to produce the tabs on a page. 759 * 760 * call_user_func_array('print_tabs', $node->get_tabs_array()); 761 * 762 * @param array $inactive 763 * @param bool $return 764 * @return array Array (tabs, selected, inactive, activated, return) 765 */ 766 public function get_tabs_array(array $inactive=array(), $return=false) { 767 $tabs = array(); 768 $rows = array(); 769 $selected = null; 770 $activated = array(); 771 foreach ($this->children as $node) { 772 $tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title()); 773 if ($node->contains_active_node()) { 774 if ($node->children->count() > 0) { 775 $activated[] = $node->key; 776 foreach ($node->children as $child) { 777 if ($child->contains_active_node()) { 778 $selected = $child->key; 779 } 780 $rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title()); 781 } 782 } else { 783 $selected = $node->key; 784 } 785 } 786 } 787 return array(array($tabs, $rows), $selected, $inactive, $activated, $return); 788 } 789 790 /** 791 * Sets the parent for this node and if this node is active ensures that the tree is properly 792 * adjusted as well. 793 * 794 * @param navigation_node $parent 795 */ 796 public function set_parent(navigation_node $parent) { 797 // Set the parent (thats the easy part) 798 $this->parent = $parent; 799 // Check if this node is active (this is checked during construction) 800 if ($this->isactive) { 801 // Force all of the parent nodes open so you can see this node 802 $this->parent->force_open(); 803 // Make all parents inactive so that its clear where we are. 804 $this->parent->make_inactive(); 805 } 806 } 807 808 /** 809 * Hides the node and any children it has. 810 * 811 * @since Moodle 2.5 812 * @param array $typestohide Optional. An array of node types that should be hidden. 813 * If null all nodes will be hidden. 814 * If an array is given then nodes will only be hidden if their type mtatches an element in the array. 815 * e.g. array(navigation_node::TYPE_COURSE) would hide only course nodes. 816 */ 817 public function hide(array $typestohide = null) { 818 if ($typestohide === null || in_array($this->type, $typestohide)) { 819 $this->display = false; 820 if ($this->has_children()) { 821 foreach ($this->children as $child) { 822 $child->hide($typestohide); 823 } 824 } 825 } 826 } 827 828 /** 829 * Get the action url for this navigation node. 830 * Called from templates. 831 * 832 * @since Moodle 3.2 833 */ 834 public function action() { 835 if ($this->action instanceof moodle_url) { 836 return $this->action; 837 } else if ($this->action instanceof action_link) { 838 return $this->action->url; 839 } 840 return $this->action; 841 } 842 843 /** 844 * Add the menu item to handle locking and unlocking of a conext. 845 * 846 * @param \navigation_node $node Node to add 847 * @param \context $context The context to be locked 848 */ 849 protected function add_context_locking_node(\navigation_node $node, \context $context) { 850 global $CFG; 851 // Manage context locking. 852 if (!empty($CFG->contextlocking) && has_capability('moodle/site:managecontextlocks', $context)) { 853 $parentcontext = $context->get_parent_context(); 854 if (empty($parentcontext) || !$parentcontext->locked) { 855 if ($context->locked) { 856 $lockicon = 'i/unlock'; 857 $lockstring = get_string('managecontextunlock', 'admin'); 858 } else { 859 $lockicon = 'i/lock'; 860 $lockstring = get_string('managecontextlock', 'admin'); 861 } 862 $node->add( 863 $lockstring, 864 new moodle_url( 865 '/admin/lock.php', 866 [ 867 'id' => $context->id, 868 ] 869 ), 870 self::TYPE_SETTING, 871 null, 872 'contextlocking', 873 new pix_icon($lockicon, '') 874 ); 875 } 876 } 877 878 } 879 } 880 881 /** 882 * Navigation node collection 883 * 884 * This class is responsible for managing a collection of navigation nodes. 885 * It is required because a node's unique identifier is a combination of both its 886 * key and its type. 887 * 888 * Originally an array was used with a string key that was a combination of the two 889 * however it was decided that a better solution would be to use a class that 890 * implements the standard IteratorAggregate interface. 891 * 892 * @package core 893 * @category navigation 894 * @copyright 2010 Sam Hemelryk 895 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 896 */ 897 class navigation_node_collection implements IteratorAggregate, Countable { 898 /** 899 * A multidimensional array to where the first key is the type and the second 900 * key is the nodes key. 901 * @var array 902 */ 903 protected $collection = array(); 904 /** 905 * An array that contains references to nodes in the same order they were added. 906 * This is maintained as a progressive array. 907 * @var array 908 */ 909 protected $orderedcollection = array(); 910 /** 911 * A reference to the last node that was added to the collection 912 * @var navigation_node 913 */ 914 protected $last = null; 915 /** 916 * The total number of items added to this array. 917 * @var int 918 */ 919 protected $count = 0; 920 921 /** 922 * Label for collection of nodes. 923 * @var string 924 */ 925 protected $collectionlabel = ''; 926 927 /** 928 * Adds a navigation node to the collection 929 * 930 * @param navigation_node $node Node to add 931 * @param string $beforekey If specified, adds before a node with this key, 932 * otherwise adds at end 933 * @return navigation_node Added node 934 */ 935 public function add(navigation_node $node, $beforekey=null) { 936 global $CFG; 937 $key = $node->key; 938 $type = $node->type; 939 940 // First check we have a 2nd dimension for this type 941 if (!array_key_exists($type, $this->orderedcollection)) { 942 $this->orderedcollection[$type] = array(); 943 } 944 // Check for a collision and report if debugging is turned on 945 if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) { 946 debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER); 947 } 948 949 // Find the key to add before 950 $newindex = $this->count; 951 $last = true; 952 if ($beforekey !== null) { 953 foreach ($this->collection as $index => $othernode) { 954 if ($othernode->key === $beforekey) { 955 $newindex = $index; 956 $last = false; 957 break; 958 } 959 } 960 if ($newindex === $this->count) { 961 debugging('Navigation node add_before: Reference node not found ' . $beforekey . 962 ', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER); 963 } 964 } 965 966 // Add the node to the appropriate place in the by-type structure (which 967 // is not ordered, despite the variable name) 968 $this->orderedcollection[$type][$key] = $node; 969 if (!$last) { 970 // Update existing references in the ordered collection (which is the 971 // one that isn't called 'ordered') to shuffle them along if required 972 for ($oldindex = $this->count; $oldindex > $newindex; $oldindex--) { 973 $this->collection[$oldindex] = $this->collection[$oldindex - 1]; 974 } 975 } 976 // Add a reference to the node to the progressive collection. 977 $this->collection[$newindex] = $this->orderedcollection[$type][$key]; 978 // Update the last property to a reference to this new node. 979 $this->last = $this->orderedcollection[$type][$key]; 980 981 // Reorder the array by index if needed 982 if (!$last) { 983 ksort($this->collection); 984 } 985 $this->count++; 986 // Return the reference to the now added node 987 return $node; 988 } 989 990 /** 991 * Return a list of all the keys of all the nodes. 992 * @return array the keys. 993 */ 994 public function get_key_list() { 995 $keys = array(); 996 foreach ($this->collection as $node) { 997 $keys[] = $node->key; 998 } 999 return $keys; 1000 } 1001 1002 /** 1003 * Set a label for this collection. 1004 * 1005 * @param string $label 1006 */ 1007 public function set_collectionlabel($label) { 1008 $this->collectionlabel = $label; 1009 } 1010 1011 /** 1012 * Return a label for this collection. 1013 * 1014 * @return string 1015 */ 1016 public function get_collectionlabel() { 1017 return $this->collectionlabel; 1018 } 1019 1020 /** 1021 * Fetches a node from this collection. 1022 * 1023 * @param string|int $key The key of the node we want to find. 1024 * @param int $type One of navigation_node::TYPE_*. 1025 * @return navigation_node|null 1026 */ 1027 public function get($key, $type=null) { 1028 if ($type !== null) { 1029 // If the type is known then we can simply check and fetch 1030 if (!empty($this->orderedcollection[$type][$key])) { 1031 return $this->orderedcollection[$type][$key]; 1032 } 1033 } else { 1034 // Because we don't know the type we look in the progressive array 1035 foreach ($this->collection as $node) { 1036 if ($node->key === $key) { 1037 return $node; 1038 } 1039 } 1040 } 1041 return false; 1042 } 1043 1044 /** 1045 * Searches for a node with matching key and type. 1046 * 1047 * This function searches both the nodes in this collection and all of 1048 * the nodes in each collection belonging to the nodes in this collection. 1049 * 1050 * Recursive. 1051 * 1052 * @param string|int $key The key of the node we want to find. 1053 * @param int $type One of navigation_node::TYPE_*. 1054 * @return navigation_node|null 1055 */ 1056 public function find($key, $type=null) { 1057 if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) { 1058 return $this->orderedcollection[$type][$key]; 1059 } else { 1060 $nodes = $this->getIterator(); 1061 // Search immediate children first 1062 foreach ($nodes as &$node) { 1063 if ($node->key === $key && ($type === null || $type === $node->type)) { 1064 return $node; 1065 } 1066 } 1067 // Now search each childs children 1068 foreach ($nodes as &$node) { 1069 $result = $node->children->find($key, $type); 1070 if ($result !== false) { 1071 return $result; 1072 } 1073 } 1074 } 1075 return false; 1076 } 1077 1078 /** 1079 * Fetches the last node that was added to this collection 1080 * 1081 * @return navigation_node 1082 */ 1083 public function last() { 1084 return $this->last; 1085 } 1086 1087 /** 1088 * Fetches all nodes of a given type from this collection 1089 * 1090 * @param string|int $type node type being searched for. 1091 * @return array ordered collection 1092 */ 1093 public function type($type) { 1094 if (!array_key_exists($type, $this->orderedcollection)) { 1095 $this->orderedcollection[$type] = array(); 1096 } 1097 return $this->orderedcollection[$type]; 1098 } 1099 /** 1100 * Removes the node with the given key and type from the collection 1101 * 1102 * @param string|int $key The key of the node we want to find. 1103 * @param int $type 1104 * @return bool 1105 */ 1106 public function remove($key, $type=null) { 1107 $child = $this->get($key, $type); 1108 if ($child !== false) { 1109 foreach ($this->collection as $colkey => $node) { 1110 if ($node->key === $key && (is_null($type) || $node->type == $type)) { 1111 unset($this->collection[$colkey]); 1112 $this->collection = array_values($this->collection); 1113 break; 1114 } 1115 } 1116 unset($this->orderedcollection[$child->type][$child->key]); 1117 $this->count--; 1118 return true; 1119 } 1120 return false; 1121 } 1122 1123 /** 1124 * Gets the number of nodes in this collection 1125 * 1126 * This option uses an internal count rather than counting the actual options to avoid 1127 * a performance hit through the count function. 1128 * 1129 * @return int 1130 */ 1131 public function count() { 1132 return $this->count; 1133 } 1134 /** 1135 * Gets an array iterator for the collection. 1136 * 1137 * This is required by the IteratorAggregator interface and is used by routines 1138 * such as the foreach loop. 1139 * 1140 * @return ArrayIterator 1141 */ 1142 public function getIterator() { 1143 return new ArrayIterator($this->collection); 1144 } 1145 } 1146 1147 /** 1148 * The global navigation class used for... the global navigation 1149 * 1150 * This class is used by PAGE to store the global navigation for the site 1151 * and is then used by the settings nav and navbar to save on processing and DB calls 1152 * 1153 * See 1154 * {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()} 1155 * {@link lib/ajax/getnavbranch.php} Called by ajax 1156 * 1157 * @package core 1158 * @category navigation 1159 * @copyright 2009 Sam Hemelryk 1160 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1161 */ 1162 class global_navigation extends navigation_node { 1163 /** @var moodle_page The Moodle page this navigation object belongs to. */ 1164 protected $page; 1165 /** @var bool switch to let us know if the navigation object is initialised*/ 1166 protected $initialised = false; 1167 /** @var array An array of course information */ 1168 protected $mycourses = array(); 1169 /** @var navigation_node[] An array for containing root navigation nodes */ 1170 protected $rootnodes = array(); 1171 /** @var bool A switch for whether to show empty sections in the navigation */ 1172 protected $showemptysections = true; 1173 /** @var bool A switch for whether courses should be shown within categories on the navigation. */ 1174 protected $showcategories = null; 1175 /** @var null@var bool A switch for whether or not to show categories in the my courses branch. */ 1176 protected $showmycategories = null; 1177 /** @var array An array of stdClasses for users that the navigation is extended for */ 1178 protected $extendforuser = array(); 1179 /** @var navigation_cache */ 1180 protected $cache; 1181 /** @var array An array of course ids that are present in the navigation */ 1182 protected $addedcourses = array(); 1183 /** @var bool */ 1184 protected $allcategoriesloaded = false; 1185 /** @var array An array of category ids that are included in the navigation */ 1186 protected $addedcategories = array(); 1187 /** @var int expansion limit */ 1188 protected $expansionlimit = 0; 1189 /** @var int userid to allow parent to see child's profile page navigation */ 1190 protected $useridtouseforparentchecks = 0; 1191 /** @var cache_session A cache that stores information on expanded courses */ 1192 protected $cacheexpandcourse = null; 1193 1194 /** Used when loading categories to load all top level categories [parent = 0] **/ 1195 const LOAD_ROOT_CATEGORIES = 0; 1196 /** Used when loading categories to load all categories **/ 1197 const LOAD_ALL_CATEGORIES = -1; 1198 1199 /** 1200 * Constructs a new global navigation 1201 * 1202 * @param moodle_page $page The page this navigation object belongs to 1203 */ 1204 public function __construct(moodle_page $page) { 1205 global $CFG, $SITE, $USER; 1206 1207 if (during_initial_install()) { 1208 return; 1209 } 1210 1211 if (get_home_page() == HOMEPAGE_SITE) { 1212 // We are using the site home for the root element 1213 $properties = array( 1214 'key' => 'home', 1215 'type' => navigation_node::TYPE_SYSTEM, 1216 'text' => get_string('home'), 1217 'action' => new moodle_url('/'), 1218 'icon' => new pix_icon('i/home', '') 1219 ); 1220 } else { 1221 // We are using the users my moodle for the root element 1222 $properties = array( 1223 'key' => 'myhome', 1224 'type' => navigation_node::TYPE_SYSTEM, 1225 'text' => get_string('myhome'), 1226 'action' => new moodle_url('/my/'), 1227 'icon' => new pix_icon('i/dashboard', '') 1228 ); 1229 } 1230 1231 // Use the parents constructor.... good good reuse 1232 parent::__construct($properties); 1233 $this->showinflatnavigation = true; 1234 1235 // Initalise and set defaults 1236 $this->page = $page; 1237 $this->forceopen = true; 1238 $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); 1239 } 1240 1241 /** 1242 * Mutator to set userid to allow parent to see child's profile 1243 * page navigation. See MDL-25805 for initial issue. Linked to it 1244 * is an issue explaining why this is a REALLY UGLY HACK thats not 1245 * for you to use! 1246 * 1247 * @param int $userid userid of profile page that parent wants to navigate around. 1248 */ 1249 public function set_userid_for_parent_checks($userid) { 1250 $this->useridtouseforparentchecks = $userid; 1251 } 1252 1253 1254 /** 1255 * Initialises the navigation object. 1256 * 1257 * This causes the navigation object to look at the current state of the page 1258 * that it is associated with and then load the appropriate content. 1259 * 1260 * This should only occur the first time that the navigation structure is utilised 1261 * which will normally be either when the navbar is called to be displayed or 1262 * when a block makes use of it. 1263 * 1264 * @return bool 1265 */ 1266 public function initialise() { 1267 global $CFG, $SITE, $USER; 1268 // Check if it has already been initialised 1269 if ($this->initialised || during_initial_install()) { 1270 return true; 1271 } 1272 $this->initialised = true; 1273 1274 // Set up the five base root nodes. These are nodes where we will put our 1275 // content and are as follows: 1276 // site: Navigation for the front page. 1277 // myprofile: User profile information goes here. 1278 // currentcourse: The course being currently viewed. 1279 // mycourses: The users courses get added here. 1280 // courses: Additional courses are added here. 1281 // users: Other users information loaded here. 1282 $this->rootnodes = array(); 1283 if (get_home_page() == HOMEPAGE_SITE) { 1284 // The home element should be my moodle because the root element is the site 1285 if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in 1286 $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), 1287 self::TYPE_SETTING, null, 'myhome', new pix_icon('i/dashboard', '')); 1288 $this->rootnodes['home']->showinflatnavigation = true; 1289 } 1290 } else { 1291 // The home element should be the site because the root node is my moodle 1292 $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), 1293 self::TYPE_SETTING, null, 'home', new pix_icon('i/home', '')); 1294 $this->rootnodes['home']->showinflatnavigation = true; 1295 if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) { 1296 // We need to stop automatic redirection 1297 $this->rootnodes['home']->action->param('redirect', '0'); 1298 } 1299 } 1300 $this->rootnodes['site'] = $this->add_course($SITE); 1301 $this->rootnodes['myprofile'] = $this->add(get_string('profile'), null, self::TYPE_USER, null, 'myprofile'); 1302 $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); 1303 $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses', new pix_icon('i/course', '')); 1304 $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); 1305 if (!core_course_category::user_top()) { 1306 $this->rootnodes['courses']->hide(); 1307 } 1308 $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); 1309 1310 // We always load the frontpage course to ensure it is available without 1311 // JavaScript enabled. 1312 $this->add_front_page_course_essentials($this->rootnodes['site'], $SITE); 1313 $this->load_course_sections($SITE, $this->rootnodes['site']); 1314 1315 $course = $this->page->course; 1316 $this->load_courses_enrolled(); 1317 1318 // $issite gets set to true if the current pages course is the sites frontpage course 1319 $issite = ($this->page->course->id == $SITE->id); 1320 1321 // Determine if the user is enrolled in any course. 1322 $enrolledinanycourse = enrol_user_sees_own_courses(); 1323 1324 $this->rootnodes['currentcourse']->mainnavonly = true; 1325 if ($enrolledinanycourse) { 1326 $this->rootnodes['mycourses']->isexpandable = true; 1327 $this->rootnodes['mycourses']->showinflatnavigation = true; 1328 if ($CFG->navshowallcourses) { 1329 // When we show all courses we need to show both the my courses and the regular courses branch. 1330 $this->rootnodes['courses']->isexpandable = true; 1331 } 1332 } else { 1333 $this->rootnodes['courses']->isexpandable = true; 1334 } 1335 $this->rootnodes['mycourses']->forceopen = true; 1336 1337 $canviewcourseprofile = true; 1338 1339 // Next load context specific content into the navigation 1340 switch ($this->page->context->contextlevel) { 1341 case CONTEXT_SYSTEM : 1342 // Nothing left to do here I feel. 1343 break; 1344 case CONTEXT_COURSECAT : 1345 // This is essential, we must load categories. 1346 $this->load_all_categories($this->page->context->instanceid, true); 1347 break; 1348 case CONTEXT_BLOCK : 1349 case CONTEXT_COURSE : 1350 if ($issite) { 1351 // Nothing left to do here. 1352 break; 1353 } 1354 1355 // Load the course associated with the current page into the navigation. 1356 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 1357 // If the course wasn't added then don't try going any further. 1358 if (!$coursenode) { 1359 $canviewcourseprofile = false; 1360 break; 1361 } 1362 1363 // If the user is not enrolled then we only want to show the 1364 // course node and not populate it. 1365 1366 // Not enrolled, can't view, and hasn't switched roles 1367 if (!can_access_course($course, null, '', true)) { 1368 if ($coursenode->isexpandable === true) { 1369 // Obviously the situation has changed, update the cache and adjust the node. 1370 // This occurs if the user access to a course has been revoked (one way or another) after 1371 // initially logging in for this session. 1372 $this->get_expand_course_cache()->set($course->id, 1); 1373 $coursenode->isexpandable = true; 1374 $coursenode->nodetype = self::NODETYPE_BRANCH; 1375 } 1376 // Very ugly hack - do not force "parents" to enrol into course their child is enrolled in, 1377 // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) 1378 if (!$this->current_user_is_parent_role()) { 1379 $coursenode->make_active(); 1380 $canviewcourseprofile = false; 1381 break; 1382 } 1383 } else if ($coursenode->isexpandable === false) { 1384 // Obviously the situation has changed, update the cache and adjust the node. 1385 // This occurs if the user has been granted access to a course (one way or another) after initially 1386 // logging in for this session. 1387 $this->get_expand_course_cache()->set($course->id, 1); 1388 $coursenode->isexpandable = true; 1389 $coursenode->nodetype = self::NODETYPE_BRANCH; 1390 } 1391 1392 // Add the essentials such as reports etc... 1393 $this->add_course_essentials($coursenode, $course); 1394 // Extend course navigation with it's sections/activities 1395 $this->load_course_sections($course, $coursenode); 1396 if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { 1397 $coursenode->make_active(); 1398 } 1399 1400 break; 1401 case CONTEXT_MODULE : 1402 if ($issite) { 1403 // If this is the site course then most information will have 1404 // already been loaded. 1405 // However we need to check if there is more content that can 1406 // yet be loaded for the specific module instance. 1407 $activitynode = $this->rootnodes['site']->find($this->page->cm->id, navigation_node::TYPE_ACTIVITY); 1408 if ($activitynode) { 1409 $this->load_activity($this->page->cm, $this->page->course, $activitynode); 1410 } 1411 break; 1412 } 1413 1414 $course = $this->page->course; 1415 $cm = $this->page->cm; 1416 1417 // Load the course associated with the page into the navigation 1418 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 1419 1420 // If the course wasn't added then don't try going any further. 1421 if (!$coursenode) { 1422 $canviewcourseprofile = false; 1423 break; 1424 } 1425 1426 // If the user is not enrolled then we only want to show the 1427 // course node and not populate it. 1428 if (!can_access_course($course, null, '', true)) { 1429 $coursenode->make_active(); 1430 $canviewcourseprofile = false; 1431 break; 1432 } 1433 1434 $this->add_course_essentials($coursenode, $course); 1435 1436 // Load the course sections into the page 1437 $this->load_course_sections($course, $coursenode, null, $cm); 1438 $activity = $coursenode->find($cm->id, navigation_node::TYPE_ACTIVITY); 1439 if (!empty($activity)) { 1440 // Finally load the cm specific navigaton information 1441 $this->load_activity($cm, $course, $activity); 1442 // Check if we have an active ndoe 1443 if (!$activity->contains_active_node() && !$activity->search_for_active_node()) { 1444 // And make the activity node active. 1445 $activity->make_active(); 1446 } 1447 } 1448 break; 1449 case CONTEXT_USER : 1450 if ($issite) { 1451 // The users profile information etc is already loaded 1452 // for the front page. 1453 break; 1454 } 1455 $course = $this->page->course; 1456 // Load the course associated with the user into the navigation 1457 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 1458 1459 // If the course wasn't added then don't try going any further. 1460 if (!$coursenode) { 1461 $canviewcourseprofile = false; 1462 break; 1463 } 1464 1465 // If the user is not enrolled then we only want to show the 1466 // course node and not populate it. 1467 if (!can_access_course($course, null, '', true)) { 1468 $coursenode->make_active(); 1469 $canviewcourseprofile = false; 1470 break; 1471 } 1472 $this->add_course_essentials($coursenode, $course); 1473 $this->load_course_sections($course, $coursenode); 1474 break; 1475 } 1476 1477 // Load for the current user 1478 $this->load_for_user(); 1479 if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id && $canviewcourseprofile) { 1480 $this->load_for_user(null, true); 1481 } 1482 // Load each extending user into the navigation. 1483 foreach ($this->extendforuser as $user) { 1484 if ($user->id != $USER->id) { 1485 $this->load_for_user($user); 1486 } 1487 } 1488 1489 // Give the local plugins a chance to include some navigation if they want. 1490 $this->load_local_plugin_navigation(); 1491 1492 // Remove any empty root nodes 1493 foreach ($this->rootnodes as $node) { 1494 // Dont remove the home node 1495 /** @var navigation_node $node */ 1496 if (!in_array($node->key, ['home', 'myhome']) && !$node->has_children() && !$node->isactive) { 1497 $node->remove(); 1498 } 1499 } 1500 1501 if (!$this->contains_active_node()) { 1502 $this->search_for_active_node(); 1503 } 1504 1505 // If the user is not logged in modify the navigation structure as detailed 1506 // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} 1507 if (!isloggedin()) { 1508 $activities = clone($this->rootnodes['site']->children); 1509 $this->rootnodes['site']->remove(); 1510 $children = clone($this->children); 1511 $this->children = new navigation_node_collection(); 1512 foreach ($activities as $child) { 1513 $this->children->add($child); 1514 } 1515 foreach ($children as $child) { 1516 $this->children->add($child); 1517 } 1518 } 1519 return true; 1520 } 1521 1522 /** 1523 * This function gives local plugins an opportunity to modify navigation. 1524 */ 1525 protected function load_local_plugin_navigation() { 1526 foreach (get_plugin_list_with_function('local', 'extend_navigation') as $function) { 1527 $function($this); 1528 } 1529 } 1530 1531 /** 1532 * Returns true if the current user is a parent of the user being currently viewed. 1533 * 1534 * If the current user is not viewing another user, or if the current user does not hold any parent roles over the 1535 * other user being viewed this function returns false. 1536 * In order to set the user for whom we are checking against you must call {@link set_userid_for_parent_checks()} 1537 * 1538 * @since Moodle 2.4 1539 * @return bool 1540 */ 1541 protected function current_user_is_parent_role() { 1542 global $USER, $DB; 1543 if ($this->useridtouseforparentchecks && $this->useridtouseforparentchecks != $USER->id) { 1544 $usercontext = context_user::instance($this->useridtouseforparentchecks, MUST_EXIST); 1545 if (!has_capability('moodle/user:viewdetails', $usercontext)) { 1546 return false; 1547 } 1548 if ($DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))) { 1549 return true; 1550 } 1551 } 1552 return false; 1553 } 1554 1555 /** 1556 * Returns true if courses should be shown within categories on the navigation. 1557 * 1558 * @param bool $ismycourse Set to true if you are calculating this for a course. 1559 * @return bool 1560 */ 1561 protected function show_categories($ismycourse = false) { 1562 global $CFG, $DB; 1563 if ($ismycourse) { 1564 return $this->show_my_categories(); 1565 } 1566 if ($this->showcategories === null) { 1567 $show = false; 1568 if ($this->page->context->contextlevel == CONTEXT_COURSECAT) { 1569 $show = true; 1570 } else if (!empty($CFG->navshowcategories) && $DB->count_records('course_categories') > 1) { 1571 $show = true; 1572 } 1573 $this->showcategories = $show; 1574 } 1575 return $this->showcategories; 1576 } 1577 1578 /** 1579 * Returns true if we should show categories in the My Courses branch. 1580 * @return bool 1581 */ 1582 protected function show_my_categories() { 1583 global $CFG; 1584 if ($this->showmycategories === null) { 1585 $this->showmycategories = !empty($CFG->navshowmycoursecategories) && !core_course_category::is_simple_site(); 1586 } 1587 return $this->showmycategories; 1588 } 1589 1590 /** 1591 * Loads the courses in Moodle into the navigation. 1592 * 1593 * @global moodle_database $DB 1594 * @param string|array $categoryids An array containing categories to load courses 1595 * for, OR null to load courses for all categories. 1596 * @return array An array of navigation_nodes one for each course 1597 */ 1598 protected function load_all_courses($categoryids = null) { 1599 global $CFG, $DB, $SITE; 1600 1601 // Work out the limit of courses. 1602 $limit = 20; 1603 if (!empty($CFG->navcourselimit)) { 1604 $limit = $CFG->navcourselimit; 1605 } 1606 1607 $toload = (empty($CFG->navshowallcourses))?self::LOAD_ROOT_CATEGORIES:self::LOAD_ALL_CATEGORIES; 1608 1609 // If we are going to show all courses AND we are showing categories then 1610 // to save us repeated DB calls load all of the categories now 1611 if ($this->show_categories()) { 1612 $this->load_all_categories($toload); 1613 } 1614 1615 // Will be the return of our efforts 1616 $coursenodes = array(); 1617 1618 // Check if we need to show categories. 1619 if ($this->show_categories()) { 1620 // Hmmm we need to show categories... this is going to be painful. 1621 // We now need to fetch up to $limit courses for each category to 1622 // be displayed. 1623 if ($categoryids !== null) { 1624 if (!is_array($categoryids)) { 1625 $categoryids = array($categoryids); 1626 } 1627 list($categorywhere, $categoryparams) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cc'); 1628 $categorywhere = 'WHERE cc.id '.$categorywhere; 1629 } else if ($toload == self::LOAD_ROOT_CATEGORIES) { 1630 $categorywhere = 'WHERE cc.depth = 1 OR cc.depth = 2'; 1631 $categoryparams = array(); 1632 } else { 1633 $categorywhere = ''; 1634 $categoryparams = array(); 1635 } 1636 1637 // First up we are going to get the categories that we are going to 1638 // need so that we can determine how best to load the courses from them. 1639 $sql = "SELECT cc.id, COUNT(c.id) AS coursecount 1640 FROM {course_categories} cc 1641 LEFT JOIN {course} c ON c.category = cc.id 1642 {$categorywhere} 1643 GROUP BY cc.id"; 1644 $categories = $DB->get_recordset_sql($sql, $categoryparams); 1645 $fullfetch = array(); 1646 $partfetch = array(); 1647 foreach ($categories as $category) { 1648 if (!$this->can_add_more_courses_to_category($category->id)) { 1649 continue; 1650 } 1651 if ($category->coursecount > $limit * 5) { 1652 $partfetch[] = $category->id; 1653 } else if ($category->coursecount > 0) { 1654 $fullfetch[] = $category->id; 1655 } 1656 } 1657 $categories->close(); 1658 1659 if (count($fullfetch)) { 1660 // First up fetch all of the courses in categories where we know that we are going to 1661 // need the majority of courses. 1662 list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory'); 1663 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 1664 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; 1665 $categoryparams['contextlevel'] = CONTEXT_COURSE; 1666 $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect 1667 FROM {course} c 1668 $ccjoin 1669 WHERE c.category {$categoryids} 1670 ORDER BY c.sortorder ASC"; 1671 $coursesrs = $DB->get_recordset_sql($sql, $categoryparams); 1672 foreach ($coursesrs as $course) { 1673 if ($course->id == $SITE->id) { 1674 // This should not be necessary, frontpage is not in any category. 1675 continue; 1676 } 1677 if (array_key_exists($course->id, $this->addedcourses)) { 1678 // It is probably better to not include the already loaded courses 1679 // directly in SQL because inequalities may confuse query optimisers 1680 // and may interfere with query caching. 1681 continue; 1682 } 1683 if (!$this->can_add_more_courses_to_category($course->category)) { 1684 continue; 1685 } 1686 context_helper::preload_from_record($course); 1687 if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { 1688 continue; 1689 } 1690 $coursenodes[$course->id] = $this->add_course($course); 1691 } 1692 $coursesrs->close(); 1693 } 1694 1695 if (count($partfetch)) { 1696 // Next we will work our way through the categories where we will likely only need a small 1697 // proportion of the courses. 1698 foreach ($partfetch as $categoryid) { 1699 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 1700 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; 1701 $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect 1702 FROM {course} c 1703 $ccjoin 1704 WHERE c.category = :categoryid 1705 ORDER BY c.sortorder ASC"; 1706 $courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE); 1707 $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5); 1708 foreach ($coursesrs as $course) { 1709 if ($course->id == $SITE->id) { 1710 // This should not be necessary, frontpage is not in any category. 1711 continue; 1712 } 1713 if (array_key_exists($course->id, $this->addedcourses)) { 1714 // It is probably better to not include the already loaded courses 1715 // directly in SQL because inequalities may confuse query optimisers 1716 // and may interfere with query caching. 1717 // This also helps to respect expected $limit on repeated executions. 1718 continue; 1719 } 1720 if (!$this->can_add_more_courses_to_category($course->category)) { 1721 break; 1722 } 1723 context_helper::preload_from_record($course); 1724 if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { 1725 continue; 1726 } 1727 $coursenodes[$course->id] = $this->add_course($course); 1728 } 1729 $coursesrs->close(); 1730 } 1731 } 1732 } else { 1733 // Prepare the SQL to load the courses and their contexts 1734 list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false); 1735 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 1736 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; 1737 $courseparams['contextlevel'] = CONTEXT_COURSE; 1738 $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect 1739 FROM {course} c 1740 $ccjoin 1741 WHERE c.id {$courseids} 1742 ORDER BY c.sortorder ASC"; 1743 $coursesrs = $DB->get_recordset_sql($sql, $courseparams); 1744 foreach ($coursesrs as $course) { 1745 if ($course->id == $SITE->id) { 1746 // frotpage is not wanted here 1747 continue; 1748 } 1749 if ($this->page->course && ($this->page->course->id == $course->id)) { 1750 // Don't include the currentcourse in this nodelist - it's displayed in the Current course node 1751 continue; 1752 } 1753 context_helper::preload_from_record($course); 1754 if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { 1755 continue; 1756 } 1757 $coursenodes[$course->id] = $this->add_course($course); 1758 if (count($coursenodes) >= $limit) { 1759 break; 1760 } 1761 } 1762 $coursesrs->close(); 1763 } 1764 1765 return $coursenodes; 1766 } 1767 1768 /** 1769 * Returns true if more courses can be added to the provided category. 1770 * 1771 * @param int|navigation_node|stdClass $category 1772 * @return bool 1773 */ 1774 protected function can_add_more_courses_to_category($category) { 1775 global $CFG; 1776 $limit = 20; 1777 if (!empty($CFG->navcourselimit)) { 1778 $limit = (int)$CFG->navcourselimit; 1779 } 1780 if (is_numeric($category)) { 1781 if (!array_key_exists($category, $this->addedcategories)) { 1782 return true; 1783 } 1784 $coursecount = count($this->addedcategories[$category]->children->type(self::TYPE_COURSE)); 1785 } else if ($category instanceof navigation_node) { 1786 if (($category->type != self::TYPE_CATEGORY) || ($category->type != self::TYPE_MY_CATEGORY)) { 1787 return false; 1788 } 1789 $coursecount = count($category->children->type(self::TYPE_COURSE)); 1790 } else if (is_object($category) && property_exists($category,'id')) { 1791 $coursecount = count($this->addedcategories[$category->id]->children->type(self::TYPE_COURSE)); 1792 } 1793 return ($coursecount <= $limit); 1794 } 1795 1796 /** 1797 * Loads all categories (top level or if an id is specified for that category) 1798 * 1799 * @param int $categoryid The category id to load or null/0 to load all base level categories 1800 * @param bool $showbasecategories If set to true all base level categories will be loaded as well 1801 * as the requested category and any parent categories. 1802 * @return navigation_node|void returns a navigation node if a category has been loaded. 1803 */ 1804 protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) { 1805 global $CFG, $DB; 1806 1807 // Check if this category has already been loaded 1808 if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) { 1809 return true; 1810 } 1811 1812 $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); 1813 $sqlselect = "SELECT cc.*, $catcontextsql 1814 FROM {course_categories} cc 1815 JOIN {context} ctx ON cc.id = ctx.instanceid"; 1816 $sqlwhere = "WHERE ctx.contextlevel = ".CONTEXT_COURSECAT; 1817 $sqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; 1818 $params = array(); 1819 1820 $categoriestoload = array(); 1821 if ($categoryid == self::LOAD_ALL_CATEGORIES) { 1822 // We are going to load all categories regardless... prepare to fire 1823 // on the database server! 1824 } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0 1825 // We are going to load all of the first level categories (categories without parents) 1826 $sqlwhere .= " AND cc.parent = 0"; 1827 } else if (array_key_exists($categoryid, $this->addedcategories)) { 1828 // The category itself has been loaded already so we just need to ensure its subcategories 1829 // have been loaded 1830 $addedcategories = $this->addedcategories; 1831 unset($addedcategories[$categoryid]); 1832 if (count($addedcategories) > 0) { 1833 list($sql, $params) = $DB->get_in_or_equal(array_keys($addedcategories), SQL_PARAMS_NAMED, 'parent', false); 1834 if ($showbasecategories) { 1835 // We need to include categories with parent = 0 as well 1836 $sqlwhere .= " AND (cc.parent = :categoryid OR cc.parent = 0) AND cc.parent {$sql}"; 1837 } else { 1838 // All we need is categories that match the parent 1839 $sqlwhere .= " AND cc.parent = :categoryid AND cc.parent {$sql}"; 1840 } 1841 } 1842 $params['categoryid'] = $categoryid; 1843 } else { 1844 // This category hasn't been loaded yet so we need to fetch it, work out its category path 1845 // and load this category plus all its parents and subcategories 1846 $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST); 1847 $categoriestoload = explode('/', trim($category->path, '/')); 1848 list($select, $params) = $DB->get_in_or_equal($categoriestoload); 1849 // We are going to use select twice so double the params 1850 $params = array_merge($params, $params); 1851 $basecategorysql = ($showbasecategories)?' OR cc.depth = 1':''; 1852 $sqlwhere .= " AND (cc.id {$select} OR cc.parent {$select}{$basecategorysql})"; 1853 } 1854 1855 $categoriesrs = $DB->get_recordset_sql("$sqlselect $sqlwhere $sqlorder", $params); 1856 $categories = array(); 1857 foreach ($categoriesrs as $category) { 1858 // Preload the context.. we'll need it when adding the category in order 1859 // to format the category name. 1860 context_helper::preload_from_record($category); 1861 if (array_key_exists($category->id, $this->addedcategories)) { 1862 // Do nothing, its already been added. 1863 } else if ($category->parent == '0') { 1864 // This is a root category lets add it immediately 1865 $this->add_category($category, $this->rootnodes['courses']); 1866 } else if (array_key_exists($category->parent, $this->addedcategories)) { 1867 // This categories parent has already been added we can add this immediately 1868 $this->add_category($category, $this->addedcategories[$category->parent]); 1869 } else { 1870 $categories[] = $category; 1871 } 1872 } 1873 $categoriesrs->close(); 1874 1875 // Now we have an array of categories we need to add them to the navigation. 1876 while (!empty($categories)) { 1877 $category = reset($categories); 1878 if (array_key_exists($category->id, $this->addedcategories)) { 1879 // Do nothing 1880 } else if ($category->parent == '0') { 1881 $this->add_category($category, $this->rootnodes['courses']); 1882 } else if (array_key_exists($category->parent, $this->addedcategories)) { 1883 $this->add_category($category, $this->addedcategories[$category->parent]); 1884 } else { 1885 // This category isn't in the navigation and niether is it's parent (yet). 1886 // We need to go through the category path and add all of its components in order. 1887 $path = explode('/', trim($category->path, '/')); 1888 foreach ($path as $catid) { 1889 if (!array_key_exists($catid, $this->addedcategories)) { 1890 // This category isn't in the navigation yet so add it. 1891 $subcategory = $categories[$catid]; 1892 if ($subcategory->parent == '0') { 1893 // Yay we have a root category - this likely means we will now be able 1894 // to add categories without problems. 1895 $this->add_category($subcategory, $this->rootnodes['courses']); 1896 } else if (array_key_exists($subcategory->parent, $this->addedcategories)) { 1897 // The parent is in the category (as we'd expect) so add it now. 1898 $this->add_category($subcategory, $this->addedcategories[$subcategory->parent]); 1899 // Remove the category from the categories array. 1900 unset($categories[$catid]); 1901 } else { 1902 // We should never ever arrive here - if we have then there is a bigger 1903 // problem at hand. 1904 throw new coding_exception('Category path order is incorrect and/or there are missing categories'); 1905 } 1906 } 1907 } 1908 } 1909 // Remove the category from the categories array now that we know it has been added. 1910 unset($categories[$category->id]); 1911 } 1912 if ($categoryid === self::LOAD_ALL_CATEGORIES) { 1913 $this->allcategoriesloaded = true; 1914 } 1915 // Check if there are any categories to load. 1916 if (count($categoriestoload) > 0) { 1917 $readytoloadcourses = array(); 1918 foreach ($categoriestoload as $category) { 1919 if ($this->can_add_more_courses_to_category($category)) { 1920 $readytoloadcourses[] = $category; 1921 } 1922 } 1923 if (count($readytoloadcourses)) { 1924 $this->load_all_courses($readytoloadcourses); 1925 } 1926 } 1927 1928 // Look for all categories which have been loaded 1929 if (!empty($this->addedcategories)) { 1930 $categoryids = array(); 1931 foreach ($this->addedcategories as $category) { 1932 if ($this->can_add_more_courses_to_category($category)) { 1933 $categoryids[] = $category->key; 1934 } 1935 } 1936 if ($categoryids) { 1937 list($categoriessql, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED); 1938 $params['limit'] = (!empty($CFG->navcourselimit))?$CFG->navcourselimit:20; 1939 $sql = "SELECT cc.id, COUNT(c.id) AS coursecount 1940 FROM {course_categories} cc 1941 JOIN {course} c ON c.category = cc.id 1942 WHERE cc.id {$categoriessql} 1943 GROUP BY cc.id 1944 HAVING COUNT(c.id) > :limit"; 1945 $excessivecategories = $DB->get_records_sql($sql, $params); 1946 foreach ($categories as &$category) { 1947 if (array_key_exists($category->key, $excessivecategories) && !$this->can_add_more_courses_to_category($category)) { 1948 $url = new moodle_url('/course/index.php', array('categoryid' => $category->key)); 1949 $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); 1950 } 1951 } 1952 } 1953 } 1954 } 1955 1956 /** 1957 * Adds a structured category to the navigation in the correct order/place 1958 * 1959 * @param stdClass $category category to be added in navigation. 1960 * @param navigation_node $parent parent navigation node 1961 * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY 1962 * @return void. 1963 */ 1964 protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) { 1965 if (array_key_exists($category->id, $this->addedcategories)) { 1966 return; 1967 } 1968 $canview = core_course_category::can_view_category($category); 1969 $url = $canview ? new moodle_url('/course/index.php', array('categoryid' => $category->id)) : null; 1970 $context = context_coursecat::instance($category->id); 1971 $categoryname = $canview ? format_string($category->name, true, array('context' => $context)) : 1972 get_string('categoryhidden'); 1973 $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id); 1974 if (!$canview) { 1975 // User does not have required capabilities to view category. 1976 $categorynode->display = false; 1977 } else if (!$category->visible) { 1978 // Category is hidden but user has capability to view hidden categories. 1979 $categorynode->hidden = true; 1980 } 1981 $this->addedcategories[$category->id] = $categorynode; 1982 } 1983 1984 /** 1985 * Loads the given course into the navigation 1986 * 1987 * @param stdClass $course 1988 * @return navigation_node 1989 */ 1990 protected function load_course(stdClass $course) { 1991 global $SITE; 1992 if ($course->id == $SITE->id) { 1993 // This is always loaded during initialisation 1994 return $this->rootnodes['site']; 1995 } else if (array_key_exists($course->id, $this->addedcourses)) { 1996 // The course has already been loaded so return a reference 1997 return $this->addedcourses[$course->id]; 1998 } else { 1999 // Add the course 2000 return $this->add_course($course); 2001 } 2002 } 2003 2004 /** 2005 * Loads all of the courses section into the navigation. 2006 * 2007 * This function calls method from current course format, see 2008 * {@link format_base::extend_course_navigation()} 2009 * If course module ($cm) is specified but course format failed to create the node, 2010 * the activity node is created anyway. 2011 * 2012 * By default course formats call the method {@link global_navigation::load_generic_course_sections()} 2013 * 2014 * @param stdClass $course Database record for the course 2015 * @param navigation_node $coursenode The course node within the navigation 2016 * @param null|int $sectionnum If specified load the contents of section with this relative number 2017 * @param null|cm_info $cm If specified make sure that activity node is created (either 2018 * in containg section or by calling load_stealth_activity() ) 2019 */ 2020 protected function load_course_sections(stdClass $course, navigation_node $coursenode, $sectionnum = null, $cm = null) { 2021 global $CFG, $SITE; 2022 require_once($CFG->dirroot.'/course/lib.php'); 2023 if (isset($cm->sectionnum)) { 2024 $sectionnum = $cm->sectionnum; 2025 } 2026 if ($sectionnum !== null) { 2027 $this->includesectionnum = $sectionnum; 2028 } 2029 course_get_format($course)->extend_course_navigation($this, $coursenode, $sectionnum, $cm); 2030 if (isset($cm->id)) { 2031 $activity = $coursenode->find($cm->id, self::TYPE_ACTIVITY); 2032 if (empty($activity)) { 2033 $activity = $this->load_stealth_activity($coursenode, get_fast_modinfo($course)); 2034 } 2035 } 2036 } 2037 2038 /** 2039 * Generates an array of sections and an array of activities for the given course. 2040 * 2041 * This method uses the cache to improve performance and avoid the get_fast_modinfo call 2042 * 2043 * @param stdClass $course 2044 * @return array Array($sections, $activities) 2045 */ 2046 protected function generate_sections_and_activities(stdClass $course) { 2047 global $CFG; 2048 require_once($CFG->dirroot.'/course/lib.php'); 2049 2050 $modinfo = get_fast_modinfo($course); 2051 $sections = $modinfo->get_section_info_all(); 2052 2053 // For course formats using 'numsections' trim the sections list 2054 $courseformatoptions = course_get_format($course)->get_format_options(); 2055 if (isset($courseformatoptions['numsections'])) { 2056 $sections = array_slice($sections, 0, $courseformatoptions['numsections']+1, true); 2057 } 2058 2059 $activities = array(); 2060 2061 foreach ($sections as $key => $section) { 2062 // Clone and unset summary to prevent $SESSION bloat (MDL-31802). 2063 $sections[$key] = clone($section); 2064 unset($sections[$key]->summary); 2065 $sections[$key]->hasactivites = false; 2066 if (!array_key_exists($section->section, $modinfo->sections)) { 2067 continue; 2068 } 2069 foreach ($modinfo->sections[$section->section] as $cmid) { 2070 $cm = $modinfo->cms[$cmid]; 2071 $activity = new stdClass; 2072 $activity->id = $cm->id; 2073 $activity->course = $course->id; 2074 $activity->section = $section->section; 2075 $activity->name = $cm->name; 2076 $activity->icon = $cm->icon; 2077 $activity->iconcomponent = $cm->iconcomponent; 2078 $activity->hidden = (!$cm->visible); 2079 $activity->modname = $cm->modname; 2080 $activity->nodetype = navigation_node::NODETYPE_LEAF; 2081 $activity->onclick = $cm->onclick; 2082 $url = $cm->url; 2083 if (!$url) { 2084 $activity->url = null; 2085 $activity->display = false; 2086 } else { 2087 $activity->url = $url->out(); 2088 $activity->display = $cm->is_visible_on_course_page() ? true : false; 2089 if (self::module_extends_navigation($cm->modname)) { 2090 $activity->nodetype = navigation_node::NODETYPE_BRANCH; 2091 } 2092 } 2093 $activities[$cmid] = $activity; 2094 if ($activity->display) { 2095 $sections[$key]->hasactivites = true; 2096 } 2097 } 2098 } 2099 2100 return array($sections, $activities); 2101 } 2102 2103 /** 2104 * Generically loads the course sections into the course's navigation. 2105 * 2106 * @param stdClass $course 2107 * @param navigation_node $coursenode 2108 * @return array An array of course section nodes 2109 */ 2110 public function load_generic_course_sections(stdClass $course, navigation_node $coursenode) { 2111 global $CFG, $DB, $USER, $SITE; 2112 require_once($CFG->dirroot.'/course/lib.php'); 2113 2114 list($sections, $activities) = $this->generate_sections_and_activities($course); 2115 2116 $navigationsections = array(); 2117 foreach ($sections as $sectionid => $section) { 2118 $section = clone($section); 2119 if ($course->id == $SITE->id) { 2120 $this->load_section_activities($coursenode, $section->section, $activities); 2121 } else { 2122 if (!$section->uservisible || (!$this->showemptysections && 2123 !$section->hasactivites && $this->includesectionnum !== $section->section)) { 2124 continue; 2125 } 2126 2127 $sectionname = get_section_name($course, $section); 2128 $url = course_get_url($course, $section->section, array('navigation' => true)); 2129 2130 $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, 2131 null, $section->id, new pix_icon('i/section', '')); 2132 $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; 2133 $sectionnode->hidden = (!$section->visible || !$section->available); 2134 if ($this->includesectionnum !== false && $this->includesectionnum == $section->section) { 2135 $this->load_section_activities($sectionnode, $section->section, $activities); 2136 } 2137 $section->sectionnode = $sectionnode; 2138 $navigationsections[$sectionid] = $section; 2139 } 2140 } 2141 return $navigationsections; 2142 } 2143 2144 /** 2145 * Loads all of the activities for a section into the navigation structure. 2146 * 2147 * @param navigation_node $sectionnode 2148 * @param int $sectionnumber 2149 * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()} 2150 * @param stdClass $course The course object the section and activities relate to. 2151 * @return array Array of activity nodes 2152 */ 2153 protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) { 2154 global $CFG, $SITE; 2155 // A static counter for JS function naming 2156 static $legacyonclickcounter = 0; 2157 2158 $activitynodes = array(); 2159 if (empty($activities)) { 2160 return $activitynodes; 2161 } 2162 2163 if (!is_object($course)) { 2164 $activity = reset($activities); 2165 $courseid = $activity->course; 2166 } else { 2167 $courseid = $course->id; 2168 } 2169 $showactivities = ($courseid != $SITE->id || !empty($CFG->navshowfrontpagemods)); 2170 2171 foreach ($activities as $activity) { 2172 if ($activity->section != $sectionnumber) { 2173 continue; 2174 } 2175 if ($activity->icon) { 2176 $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent); 2177 } else { 2178 $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname); 2179 } 2180 2181 // Prepare the default name and url for the node 2182 $activityname = format_string($activity->name, true, array('context' => context_module::instance($activity->id))); 2183 $action = new moodle_url($activity->url); 2184 2185 // Check if the onclick property is set (puke!) 2186 if (!empty($activity->onclick)) { 2187 // Increment the counter so that we have a unique number. 2188 $legacyonclickcounter++; 2189 // Generate the function name we will use 2190 $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter; 2191 $propogrationhandler = ''; 2192 // Check if we need to cancel propogation. Remember inline onclick 2193 // events would return false if they wanted to prevent propogation and the 2194 // default action. 2195 if (strpos($activity->onclick, 'return false')) { 2196 $propogrationhandler = 'e.halt();'; 2197 } 2198 // Decode the onclick - it has already been encoded for display (puke) 2199 $onclick = htmlspecialchars_decode($activity->onclick, ENT_QUOTES); 2200 // Build the JS function the click event will call 2201 $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }"; 2202 $this->page->requires->js_amd_inline($jscode); 2203 // Override the default url with the new action link 2204 $action = new action_link($action, $activityname, new component_action('click', $functionname)); 2205 } 2206 2207 $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon); 2208 $activitynode->title(get_string('modulename', $activity->modname)); 2209 $activitynode->hidden = $activity->hidden; 2210 $activitynode->display = $showactivities && $activity->display; 2211 $activitynode->nodetype = $activity->nodetype; 2212 $activitynodes[$activity->id] = $activitynode; 2213 } 2214 2215 return $activitynodes; 2216 } 2217 /** 2218 * Loads a stealth module from unavailable section 2219 * @param navigation_node $coursenode 2220 * @param stdClass $modinfo 2221 * @return navigation_node or null if not accessible 2222 */ 2223 protected function load_stealth_activity(navigation_node $coursenode, $modinfo) { 2224 if (empty($modinfo->cms[$this->page->cm->id])) { 2225 return null; 2226 } 2227 $cm = $modinfo->cms[$this->page->cm->id]; 2228 if ($cm->icon) { 2229 $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent); 2230 } else { 2231 $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname); 2232 } 2233 $url = $cm->url; 2234 $activitynode = $coursenode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon); 2235 $activitynode->title(get_string('modulename', $cm->modname)); 2236 $activitynode->hidden = (!$cm->visible); 2237 if (!$cm->is_visible_on_course_page()) { 2238 // Do not show any error here, let the page handle exception that activity is not visible for the current user. 2239 // Also there may be no exception at all in case when teacher is logged in as student. 2240 $activitynode->display = false; 2241 } else if (!$url) { 2242 // Don't show activities that don't have links! 2243 $activitynode->display = false; 2244 } else if (self::module_extends_navigation($cm->modname)) { 2245 $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; 2246 } 2247 return $activitynode; 2248 } 2249 /** 2250 * Loads the navigation structure for the given activity into the activities node. 2251 * 2252 * This method utilises a callback within the modules lib.php file to load the 2253 * content specific to activity given. 2254 * 2255 * The callback is a method: {modulename}_extend_navigation() 2256 * Examples: 2257 * * {@link forum_extend_navigation()} 2258 * * {@link workshop_extend_navigation()} 2259 * 2260 * @param cm_info|stdClass $cm 2261 * @param stdClass $course 2262 * @param navigation_node $activity 2263 * @return bool 2264 */ 2265 protected function load_activity($cm, stdClass $course, navigation_node $activity) { 2266 global $CFG, $DB; 2267 2268 // make sure we have a $cm from get_fast_modinfo as this contains activity access details 2269 if (!($cm instanceof cm_info)) { 2270 $modinfo = get_fast_modinfo($course); 2271 $cm = $modinfo->get_cm($cm->id); 2272 } 2273 $activity->nodetype = navigation_node::NODETYPE_LEAF; 2274 $activity->make_active(); 2275 $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'; 2276 $function = $cm->modname.'_extend_navigation'; 2277 2278 if (file_exists($file)) { 2279 require_once($file); 2280 if (function_exists($function)) { 2281 $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); 2282 $function($activity, $course, $activtyrecord, $cm); 2283 } 2284 } 2285 2286 // Allow the active advanced grading method plugin to append module navigation 2287 $featuresfunc = $cm->modname.'_supports'; 2288 if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING)) { 2289 require_once($CFG->dirroot.'/grade/grading/lib.php'); 2290 $gradingman = get_grading_manager($cm->context, 'mod_'.$cm->modname); 2291 $gradingman->extend_navigation($this, $activity); 2292 } 2293 2294 return $activity->has_children(); 2295 } 2296 /** 2297 * Loads user specific information into the navigation in the appropriate place. 2298 * 2299 * If no user is provided the current user is assumed. 2300 * 2301 * @param stdClass $user 2302 * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) 2303 * @return bool 2304 */ 2305 protected function load_for_user($user=null, $forceforcontext=false) { 2306 global $DB, $CFG, $USER, $SITE; 2307 2308 require_once($CFG->dirroot . '/course/lib.php'); 2309 2310 if ($user === null) { 2311 // We can't require login here but if the user isn't logged in we don't 2312 // want to show anything 2313 if (!isloggedin() || isguestuser()) { 2314 return false; 2315 } 2316 $user = $USER; 2317 } else if (!is_object($user)) { 2318 // If the user is not an object then get them from the database 2319 $select = context_helper::get_preload_record_columns_sql('ctx'); 2320 $sql = "SELECT u.*, $select 2321 FROM {user} u 2322 JOIN {context} ctx ON u.id = ctx.instanceid 2323 WHERE u.id = :userid AND 2324 ctx.contextlevel = :contextlevel"; 2325 $user = $DB->get_record_sql($sql, array('userid' => (int)$user, 'contextlevel' => CONTEXT_USER), MUST_EXIST); 2326 context_helper::preload_from_record($user); 2327 } 2328 2329 $iscurrentuser = ($user->id == $USER->id); 2330 2331 $usercontext = context_user::instance($user->id); 2332 2333 // Get the course set against the page, by default this will be the site 2334 $course = $this->page->course; 2335 $baseargs = array('id'=>$user->id); 2336 if ($course->id != $SITE->id && (!$iscurrentuser || $forceforcontext)) { 2337 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 2338 $baseargs['course'] = $course->id; 2339 $coursecontext = context_course::instance($course->id); 2340 $issitecourse = false; 2341 } else { 2342 // Load all categories and get the context for the system 2343 $coursecontext = context_system::instance(); 2344 $issitecourse = true; 2345 } 2346 2347 // Create a node to add user information under. 2348 $usersnode = null; 2349 if (!$issitecourse) { 2350 // Not the current user so add it to the participants node for the current course. 2351 $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); 2352 $userviewurl = new moodle_url('/user/view.php', $baseargs); 2353 } else if ($USER->id != $user->id) { 2354 // This is the site so add a users node to the root branch. 2355 $usersnode = $this->rootnodes['users']; 2356 if (course_can_view_participants($coursecontext)) { 2357 $usersnode->action = new moodle_url('/user/index.php', array('id' => $course->id)); 2358 } 2359 $userviewurl = new moodle_url('/user/profile.php', $baseargs); 2360 } 2361 if (!$usersnode) { 2362 // We should NEVER get here, if the course hasn't been populated 2363 // with a participants node then the navigaiton either wasn't generated 2364 // for it (you are missing a require_login or set_context call) or 2365 // you don't have access.... in the interests of no leaking informatin 2366 // we simply quit... 2367 return false; 2368 } 2369 // Add a branch for the current user. 2370 // Only reveal user details if $user is the current user, or a user to which the current user has access. 2371 $viewprofile = true; 2372 if (!$iscurrentuser) { 2373 require_once($CFG->dirroot . '/user/lib.php'); 2374 if ($this->page->context->contextlevel == CONTEXT_USER && !has_capability('moodle/user:viewdetails', $usercontext) ) { 2375 $viewprofile = false; 2376 } else if ($this->page->context->contextlevel != CONTEXT_USER && !user_can_view_profile($user, $course, $usercontext)) { 2377 $viewprofile = false; 2378 } 2379 if (!$viewprofile) { 2380 $viewprofile = user_can_view_profile($user, null, $usercontext); 2381 } 2382 } 2383 2384 // Now, conditionally add the user node. 2385 if ($viewprofile) { 2386 $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); 2387 $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, 'user' . $user->id); 2388 } else { 2389 $usernode = $usersnode->add(get_string('user')); 2390 } 2391 2392 if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { 2393 $usernode->make_active(); 2394 } 2395 2396 // Add user information to the participants or user node. 2397 if ($issitecourse) { 2398 2399 // If the user is the current user or has permission to view the details of the requested 2400 // user than add a view profile link. 2401 if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || 2402 has_capability('moodle/user:viewdetails', $usercontext)) { 2403 if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { 2404 $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php', $baseargs)); 2405 } else { 2406 $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php', $baseargs)); 2407 } 2408 } 2409 2410 if (!empty($CFG->navadduserpostslinks)) { 2411 // Add nodes for forum posts and discussions if the user can view either or both 2412 // There are no capability checks here as the content of the page is based 2413 // purely on the forums the current user has access too. 2414 $forumtab = $usernode->add(get_string('forumposts', 'forum')); 2415 $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); 2416 $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', 2417 array_merge($baseargs, array('mode' => 'discussions')))); 2418 } 2419 2420 // Add blog nodes. 2421 if (!empty($CFG->enableblogs)) { 2422 if (!$this->cache->cached('userblogoptions'.$user->id)) { 2423 require_once($CFG->dirroot.'/blog/lib.php'); 2424 // Get all options for the user. 2425 $options = blog_get_options_for_user($user); 2426 $this->cache->set('userblogoptions'.$user->id, $options); 2427 } else { 2428 $options = $this->cache->{'userblogoptions'.$user->id}; 2429 } 2430 2431 if (count($options) > 0) { 2432 $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); 2433 foreach ($options as $type => $option) { 2434 if ($type == "rss") { 2435 $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null, 2436 new pix_icon('i/rss', '')); 2437 } else { 2438 $blogs->add($option['string'], $option['link']); 2439 } 2440 } 2441 } 2442 } 2443 2444 // Add the messages link. 2445 // It is context based so can appear in the user's profile and in course participants information. 2446 if (!empty($CFG->messaging)) { 2447 $messageargs = array('user1' => $USER->id); 2448 if ($USER->id != $user->id) { 2449 $messageargs['user2'] = $user->id; 2450 } 2451 $url = new moodle_url('/message/index.php', $messageargs); 2452 $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); 2453 } 2454 2455 // Add the "My private files" link. 2456 // This link doesn't have a unique display for course context so only display it under the user's profile. 2457 if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { 2458 $url = new moodle_url('/user/files.php'); 2459 $usernode->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles'); 2460 } 2461 2462 // Add a node to view the users notes if permitted. 2463 if (!empty($CFG->enablenotes) && 2464 has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { 2465 $url = new moodle_url('/notes/index.php', array('user' => $user->id)); 2466 if ($coursecontext->instanceid != SITEID) { 2467 $url->param('course', $coursecontext->instanceid); 2468 } 2469 $usernode->add(get_string('notes', 'notes'), $url); 2470 } 2471 2472 // Show the grades node. 2473 if (($issitecourse && $iscurrentuser) || has_capability('moodle/user:viewdetails', $usercontext)) { 2474 require_once($CFG->dirroot . '/user/lib.php'); 2475 // Set the grades node to link to the "Grades" page. 2476 if ($course->id == SITEID) { 2477 $url = user_mygrades_url($user->id, $course->id); 2478 } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version). 2479 $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id)); 2480 } 2481 if ($USER->id != $user->id) { 2482 $usernode->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'usergrades'); 2483 } else { 2484 $usernode->add(get_string('grades', 'grades'), $url); 2485 } 2486 } 2487 2488 // If the user is the current user add the repositories for the current user. 2489 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); 2490 if (!$iscurrentuser && 2491 $course->id == $SITE->id && 2492 has_capability('moodle/user:viewdetails', $usercontext) && 2493 (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { 2494 2495 // Add view grade report is permitted. 2496 $reports = core_component::get_plugin_list('gradereport'); 2497 arsort($reports); // User is last, we want to test it first. 2498 2499 $userscourses = enrol_get_users_courses($user->id, false, '*'); 2500 $userscoursesnode = $usernode->add(get_string('courses')); 2501 2502 $count = 0; 2503 foreach ($userscourses as $usercourse) { 2504 if ($count === (int)$CFG->navcourselimit) { 2505 $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1)); 2506 $userscoursesnode->add(get_string('showallcourses'), $url); 2507 break; 2508 } 2509 $count++; 2510 $usercoursecontext = context_course::instance($usercourse->id); 2511 $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext)); 2512 $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', 2513 array('id' => $user->id, 'course' => $usercourse->id)), self::TYPE_CONTAINER); 2514 2515 $gradeavailable = has_capability('moodle/grade:view', $usercoursecontext); 2516 if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { 2517 foreach ($reports as $plugin => $plugindir) { 2518 if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) { 2519 // Stop when the first visible plugin is found. 2520 $gradeavailable = true; 2521 break; 2522 } 2523 } 2524 } 2525 2526 if ($gradeavailable) { 2527 $url = new moodle_url('/grade/report/index.php', array('id' => $usercourse->id)); 2528 $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, 2529 new pix_icon('i/grades', '')); 2530 } 2531 2532 // Add a node to view the users notes if permitted. 2533 if (!empty($CFG->enablenotes) && 2534 has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { 2535 $url = new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $usercourse->id)); 2536 $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); 2537 } 2538 2539 if (can_access_course($usercourse, $user->id, '', true)) { 2540 $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', 2541 array('id' => $usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); 2542 } 2543 2544 $reporttab = $usercoursenode->add(get_string('activityreports')); 2545 2546 $reportfunctions = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); 2547 foreach ($reportfunctions as $reportfunction) { 2548 $reportfunction($reporttab, $user, $usercourse); 2549 } 2550 2551 $reporttab->trim_if_empty(); 2552 } 2553 } 2554 2555 // Let plugins hook into user navigation. 2556 $pluginsfunction = get_plugins_with_function('extend_navigation_user', 'lib.php'); 2557 foreach ($pluginsfunction as $plugintype => $plugins) { 2558 if ($plugintype != 'report') { 2559 foreach ($plugins as $pluginfunction) { 2560 $pluginfunction($usernode, $user, $usercontext, $course, $coursecontext); 2561 } 2562 } 2563 } 2564 } 2565 return true; 2566 } 2567 2568 /** 2569 * This method simply checks to see if a given module can extend the navigation. 2570 * 2571 * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation. 2572 * 2573 * @param string $modname 2574 * @return bool 2575 */ 2576 public static function module_extends_navigation($modname) { 2577 global $CFG; 2578 static $extendingmodules = array(); 2579 if (!array_key_exists($modname, $extendingmodules)) { 2580 $extendingmodules[$modname] = false; 2581 $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php'; 2582 if (file_exists($file)) { 2583 $function = $modname.'_extend_navigation'; 2584 require_once($file); 2585 $extendingmodules[$modname] = (function_exists($function)); 2586 } 2587 } 2588 return $extendingmodules[$modname]; 2589 } 2590 /** 2591 * Extends the navigation for the given user. 2592 * 2593 * @param stdClass $user A user from the database 2594 */ 2595 public function extend_for_user($user) { 2596 $this->extendforuser[] = $user; 2597 } 2598 2599 /** 2600 * Returns all of the users the navigation is being extended for 2601 * 2602 * @return array An array of extending users. 2603 */ 2604 public function get_extending_users() { 2605 return $this->extendforuser; 2606 } 2607 /** 2608 * Adds the given course to the navigation structure. 2609 * 2610 * @param stdClass $course 2611 * @param bool $forcegeneric 2612 * @param bool $ismycourse 2613 * @return navigation_node 2614 */ 2615 public function add_course(stdClass $course, $forcegeneric = false, $coursetype = self::COURSE_OTHER) { 2616 global $CFG, $SITE; 2617 2618 // We found the course... we can return it now :) 2619 if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { 2620 return $this->addedcourses[$course->id]; 2621 } 2622 2623 $coursecontext = context_course::instance($course->id); 2624 2625 if ($coursetype != self::COURSE_MY && $coursetype != self::COURSE_CURRENT && $course->id != $SITE->id) { 2626 if (is_role_switched($course->id)) { 2627 // user has to be able to access course in order to switch, let's skip the visibility test here 2628 } else if (!core_course_category::can_view_course_info($course)) { 2629 return false; 2630 } 2631 } 2632 2633 $issite = ($course->id == $SITE->id); 2634 $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); 2635 $fullname = format_string($course->fullname, true, array('context' => $coursecontext)); 2636 // This is the name that will be shown for the course. 2637 $coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname; 2638 2639 if ($coursetype == self::COURSE_CURRENT) { 2640 if ($coursenode = $this->rootnodes['mycourses']->find($course->id, self::TYPE_COURSE)) { 2641 return $coursenode; 2642 } else { 2643 $coursetype = self::COURSE_OTHER; 2644 } 2645 } 2646 2647 // Can the user expand the course to see its content. 2648 $canexpandcourse = true; 2649 if ($issite) { 2650 $parent = $this; 2651 $url = null; 2652 if (empty($CFG->usesitenameforsitepages)) { 2653 $coursename = get_string('sitepages'); 2654 } 2655 } else if ($coursetype == self::COURSE_CURRENT) { 2656 $parent = $this->rootnodes['currentcourse']; 2657 $url = new moodle_url('/course/view.php', array('id'=>$course->id)); 2658 $canexpandcourse = $this->can_expand_course($course); 2659 } else if ($coursetype == self::COURSE_MY && !$forcegeneric) { 2660 if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) { 2661 // Nothing to do here the above statement set $parent to the category within mycourses. 2662 } else { 2663 $parent = $this->rootnodes['mycourses']; 2664 } 2665 $url = new moodle_url('/course/view.php', array('id'=>$course->id)); 2666 } else { 2667 $parent = $this->rootnodes['courses']; 2668 $url = new moodle_url('/course/view.php', array('id'=>$course->id)); 2669 // They can only expand the course if they can access it. 2670 $canexpandcourse = $this->can_expand_course($course); 2671 if (!empty($course->category) && $this->show_categories($coursetype == self::COURSE_MY)) { 2672 if (!$this->is_category_fully_loaded($course->category)) { 2673 // We need to load the category structure for this course 2674 $this->load_all_categories($course->category, false); 2675 } 2676 if (array_key_exists($course->category, $this->addedcategories)) { 2677 $parent = $this->addedcategories[$course->category]; 2678 // This could lead to the course being created so we should check whether it is the case again 2679 if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { 2680 return $this->addedcourses[$course->id]; 2681 } 2682 } 2683 } 2684 } 2685 2686 $coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id, new pix_icon('i/course', '')); 2687 $coursenode->showinflatnavigation = $coursetype == self::COURSE_MY; 2688 2689 $coursenode->hidden = (!$course->visible); 2690 $coursenode->title(format_string($course->fullname, true, array('context' => $coursecontext, 'escape' => false))); 2691 if ($canexpandcourse) { 2692 // This course can be expanded by the user, make it a branch to make the system aware that its expandable by ajax. 2693 $coursenode->nodetype = self::NODETYPE_BRANCH; 2694 $coursenode->isexpandable = true; 2695 } else { 2696 $coursenode->nodetype = self::NODETYPE_LEAF; 2697 $coursenode->isexpandable = false; 2698 } 2699 if (!$forcegeneric) { 2700 $this->addedcourses[$course->id] = $coursenode; 2701 } 2702 2703 return $coursenode; 2704 } 2705 2706 /** 2707 * Returns a cache instance to use for the expand course cache. 2708 * @return cache_session 2709 */ 2710 protected function get_expand_course_cache() { 2711 if ($this->cacheexpandcourse === null) { 2712 $this->cacheexpandcourse = cache::make('core', 'navigation_expandcourse'); 2713 } 2714 return $this->cacheexpandcourse; 2715 } 2716 2717 /** 2718 * Checks if a user can expand a course in the navigation. 2719 * 2720 * We use a cache here because in order to be accurate we need to call can_access_course which is a costly function. 2721 * Because this functionality is basic + non-essential and because we lack good event triggering this cache 2722 * permits stale data. 2723 * In the situation the user is granted access to a course after we've initialised this session cache the cache 2724 * will be stale. 2725 * It is brought up to date in only one of two ways. 2726 * 1. The user logs out and in again. 2727 * 2. The user browses to the course they've just being given access to. 2728 * 2729 * Really all this controls is whether the node is shown as expandable or not. It is uber un-important. 2730 * 2731 * @param stdClass $course 2732 * @return bool 2733 */ 2734 protected function can_expand_course($course) { 2735 $cache = $this->get_expand_course_cache(); 2736 $canexpand = $cache->get($course->id); 2737 if ($canexpand === false) { 2738 $canexpand = isloggedin() && can_access_course($course, null, '', true); 2739 $canexpand = (int)$canexpand; 2740 $cache->set($course->id, $canexpand); 2741 } 2742 return ($canexpand === 1); 2743 } 2744 2745 /** 2746 * Returns true if the category has already been loaded as have any child categories 2747 * 2748 * @param int $categoryid 2749 * @return bool 2750 */ 2751 protected function is_category_fully_loaded($categoryid) { 2752 return (array_key_exists($categoryid, $this->addedcategories) && ($this->allcategoriesloaded || $this->addedcategories[$categoryid]->children->count() > 0)); 2753 } 2754 2755 /** 2756 * Adds essential course nodes to the navigation for the given course. 2757 * 2758 * This method adds nodes such as reports, blogs and participants 2759 * 2760 * @param navigation_node $coursenode 2761 * @param stdClass $course 2762 * @return bool returns true on successful addition of a node. 2763 */ 2764 public function add_course_essentials($coursenode, stdClass $course) { 2765 global $CFG, $SITE; 2766 require_once($CFG->dirroot . '/course/lib.php'); 2767 2768 if ($course->id == $SITE->id) { 2769 return $this->add_front_page_course_essentials($coursenode, $course); 2770 } 2771 2772 if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { 2773 return true; 2774 } 2775 2776 $navoptions = course_get_user_navigation_options($this->page->context, $course); 2777 2778 //Participants 2779 if ($navoptions->participants) { 2780 $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), 2781 self::TYPE_CONTAINER, get_string('participants'), 'participants', new pix_icon('i/users', '')); 2782 2783 if ($navoptions->blogs) { 2784 $blogsurls = new moodle_url('/blog/index.php'); 2785 if ($currentgroup = groups_get_course_group($course, true)) { 2786 $blogsurls->param('groupid', $currentgroup); 2787 } else { 2788 $blogsurls->param('courseid', $course->id); 2789 } 2790 $participants->add(get_string('blogscourse', 'blog'), $blogsurls->out(), self::TYPE_SETTING, null, 'courseblogs'); 2791 } 2792 2793 if ($navoptions->notes) { 2794 $participants->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('filtertype' => 'course', 'filterselect' => $course->id)), self::TYPE_SETTING, null, 'currentcoursenotes'); 2795 } 2796 } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { 2797 $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); 2798 } 2799 2800 // Badges. 2801 if ($navoptions->badges) { 2802 $url = new moodle_url('/badges/view.php', array('type' => 2, 'id' => $course->id)); 2803 2804 $coursenode->add(get_string('coursebadges', 'badges'), $url, 2805 navigation_node::TYPE_SETTING, null, 'badgesview', 2806 new pix_icon('i/badge', get_string('coursebadges', 'badges'))); 2807 } 2808 2809 // Check access to the course and competencies page. 2810 if ($navoptions->competencies) { 2811 // Just a link to course competency. 2812 $title = get_string('competencies', 'core_competency'); 2813 $path = new moodle_url("/admin/tool/lp/coursecompetencies.php", array('courseid' => $course->id)); 2814 $coursenode->add($title, $path, navigation_node::TYPE_SETTING, null, 'competencies', 2815 new pix_icon('i/competencies', '')); 2816 } 2817 if ($navoptions->grades) { 2818 $url = new moodle_url('/grade/report/index.php', array('id'=>$course->id)); 2819 $gradenode = $coursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, 2820 'grades', new pix_icon('i/grades', '')); 2821 // If the page type matches the grade part, then make the nav drawer grade node (incl. all sub pages) active. 2822 if ($this->page->context->contextlevel < CONTEXT_MODULE && strpos($this->page->pagetype, 'grade-') === 0) { 2823 $gradenode->make_active(); 2824 } 2825 } 2826 2827 return true; 2828 } 2829 /** 2830 * This generates the structure of the course that won't be generated when 2831 * the modules and sections are added. 2832 * 2833 * Things such as the reports branch, the participants branch, blogs... get 2834 * added to the course node by this method. 2835 * 2836 * @param navigation_node $coursenode 2837 * @param stdClass $course 2838 * @return bool True for successfull generation 2839 */ 2840 public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { 2841 global $CFG, $USER, $COURSE, $SITE; 2842 require_once($CFG->dirroot . '/course/lib.php'); 2843 2844 if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { 2845 return true; 2846 } 2847 2848 $systemcontext = context_system::instance(); 2849 $navoptions = course_get_user_navigation_options($systemcontext, $course); 2850 2851 // Hidden node that we use to determine if the front page navigation is loaded. 2852 // This required as there are not other guaranteed nodes that may be loaded. 2853 $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false; 2854 2855 // Participants. 2856 if ($navoptions->participants) { 2857 $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); 2858 } 2859 2860 // Blogs. 2861 if ($navoptions->blogs) { 2862 $blogsurls = new moodle_url('/blog/index.php'); 2863 $coursenode->add(get_string('blogssite', 'blog'), $blogsurls->out(), self::TYPE_SYSTEM, null, 'siteblog'); 2864 } 2865 2866 $filterselect = 0; 2867 2868 // Badges. 2869 if ($navoptions->badges) { 2870 $url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1)); 2871 $coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM); 2872 } 2873 2874 // Notes. 2875 if ($navoptions->notes) { 2876 $coursenode->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', 2877 array('filtertype' => 'course', 'filterselect' => $filterselect)), self::TYPE_SETTING, null, 'notes'); 2878 } 2879 2880 // Tags 2881 if ($navoptions->tags) { 2882 $node = $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'), 2883 self::TYPE_SETTING, null, 'tags'); 2884 } 2885 2886 // Search. 2887 if ($navoptions->search) { 2888 $node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'), 2889 self::TYPE_SETTING, null, 'search'); 2890 } 2891 2892 if ($navoptions->calendar) { 2893 $courseid = $COURSE->id; 2894 $params = array('view' => 'month'); 2895 if ($courseid != $SITE->id) { 2896 $params['course'] = $courseid; 2897 } 2898 2899 // Calendar 2900 $calendarurl = new moodle_url('/calendar/view.php', $params); 2901 $node = $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, 2902 self::TYPE_CUSTOM, null, 'calendar', new pix_icon('i/calendar', '')); 2903 $node->showinflatnavigation = true; 2904 } 2905 2906 if (isloggedin()) { 2907 $usercontext = context_user::instance($USER->id); 2908 if (has_capability('moodle/user:manageownfiles', $usercontext)) { 2909 $url = new moodle_url('/user/files.php'); 2910 $node = $coursenode->add(get_string('privatefiles'), $url, 2911 self::TYPE_SETTING, null, 'privatefiles', new pix_icon('i/privatefiles', '')); 2912 $node->display = false; 2913 $node->showinflatnavigation = true; 2914 } 2915 } 2916 2917 if (isloggedin()) { 2918 $context = $this->page->context; 2919 switch ($context->contextlevel) { 2920 case CONTEXT_COURSECAT: 2921 // OK, expected context level. 2922 break; 2923 case CONTEXT_COURSE: 2924 // OK, expected context level if not on frontpage. 2925 if ($COURSE->id != $SITE->id) { 2926 break; 2927 } 2928 default: 2929 // If this context is part of a course (excluding frontpage), use the course context. 2930 // Otherwise, use the system context. 2931 $coursecontext = $context->get_course_context(false); 2932 if ($coursecontext && $coursecontext->instanceid !== $SITE->id) { 2933 $context = $coursecontext; 2934 } else { 2935 $context = $systemcontext; 2936 } 2937 } 2938 2939 $params = ['contextid' => $context->id]; 2940 if (has_capability('moodle/contentbank:access', $context)) { 2941 $url = new moodle_url('/contentbank/index.php', $params); 2942 $node = $coursenode->add(get_string('contentbank'), $url, 2943 self::TYPE_CUSTOM, null, 'contentbank', new pix_icon('i/contentbank', '')); 2944 $node->showinflatnavigation = true; 2945 } 2946 } 2947 2948 return true; 2949 } 2950 2951 /** 2952 * Clears the navigation cache 2953 */ 2954 public function clear_cache() { 2955 $this->cache->clear(); 2956 } 2957 2958 /** 2959 * Sets an expansion limit for the navigation 2960 * 2961 * The expansion limit is used to prevent the display of content that has a type 2962 * greater than the provided $type. 2963 * 2964 * Can be used to ensure things such as activities or activity content don't get 2965 * shown on the navigation. 2966 * They are still generated in order to ensure the navbar still makes sense. 2967 * 2968 * @param int $type One of navigation_node::TYPE_* 2969 * @return bool true when complete. 2970 */ 2971 public function set_expansion_limit($type) { 2972 global $SITE; 2973 $nodes = $this->find_all_of_type($type); 2974 2975 // We only want to hide specific types of nodes. 2976 // Only nodes that represent "structure" in the navigation tree should be hidden. 2977 // If we hide all nodes then we risk hiding vital information. 2978 $typestohide = array( 2979 self::TYPE_CATEGORY, 2980 self::TYPE_COURSE, 2981 self::TYPE_SECTION, 2982 self::TYPE_ACTIVITY 2983 ); 2984 2985 foreach ($nodes as $node) { 2986 // We need to generate the full site node 2987 if ($type == self::TYPE_COURSE && $node->key == $SITE->id) { 2988 continue; 2989 } 2990 foreach ($node->children as $child) { 2991 $child->hide($typestohide); 2992 } 2993 } 2994 return true; 2995 } 2996 /** 2997 * Attempts to get the navigation with the given key from this nodes children. 2998 * 2999 * This function only looks at this nodes children, it does NOT look recursivily. 3000 * If the node can't be found then false is returned. 3001 * 3002 * If you need to search recursivily then use the {@link global_navigation::find()} method. 3003 * 3004 * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} 3005 * may be of more use to you. 3006 * 3007 * @param string|int $key The key of the node you wish to receive. 3008 * @param int $type One of navigation_node::TYPE_* 3009 * @return navigation_node|false 3010 */ 3011 public function get($key, $type = null) { 3012 if (!$this->initialised) { 3013 $this->initialise(); 3014 } 3015 return parent::get($key, $type); 3016 } 3017 3018 /** 3019 * Searches this nodes children and their children to find a navigation node 3020 * with the matching key and type. 3021 * 3022 * This method is recursive and searches children so until either a node is 3023 * found or there are no more nodes to search. 3024 * 3025 * If you know that the node being searched for is a child of this node 3026 * then use the {@link global_navigation::get()} method instead. 3027 * 3028 * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} 3029 * may be of more use to you. 3030 * 3031 * @param string|int $key The key of the node you wish to receive. 3032 * @param int $type One of navigation_node::TYPE_* 3033 * @return navigation_node|false 3034 */ 3035 public function find($key, $type) { 3036 if (!$this->initialised) { 3037 $this->initialise(); 3038 } 3039 if ($type == self::TYPE_ROOTNODE && array_key_exists($key, $this->rootnodes)) { 3040 return $this->rootnodes[$key]; 3041 } 3042 return parent::find($key, $type); 3043 } 3044 3045 /** 3046 * They've expanded the 'my courses' branch. 3047 */ 3048 protected function load_courses_enrolled() { 3049 global $CFG; 3050 3051 $limit = (int) $CFG->navcourselimit; 3052 3053 $courses = enrol_get_my_courses('*'); 3054 $flatnavcourses = []; 3055 3056 // Go through the courses and see which ones we want to display in the flatnav. 3057 foreach ($courses as $course) { 3058 $classify = course_classify_for_timeline($course); 3059 3060 if ($classify == COURSE_TIMELINE_INPROGRESS) { 3061 $flatnavcourses[$course->id] = $course; 3062 } 3063 } 3064 3065 // Get the number of courses that can be displayed in the nav block and in the flatnav. 3066 $numtotalcourses = count($courses); 3067 $numtotalflatnavcourses = count($flatnavcourses); 3068 3069 // Reduce the size of the arrays to abide by the 'navcourselimit' setting. 3070 $courses = array_slice($courses, 0, $limit, true); 3071 $flatnavcourses = array_slice($flatnavcourses, 0, $limit, true); 3072 3073 // Get the number of courses we are going to show for each. 3074 $numshowncourses = count($courses); 3075 $numshownflatnavcourses = count($flatnavcourses); 3076 if ($numshowncourses && $this->show_my_categories()) { 3077 // Generate an array containing unique values of all the courses' categories. 3078 $categoryids = array(); 3079 foreach ($courses as $course) { 3080 if (in_array($course->category, $categoryids)) { 3081 continue; 3082 } 3083 $categoryids[] = $course->category; 3084 } 3085 3086 // Array of category IDs that include the categories of the user's courses and the related course categories. 3087 $fullpathcategoryids = []; 3088 // Get the course categories for the enrolled courses' category IDs. 3089 $mycoursecategories = core_course_category::get_many($categoryids); 3090 // Loop over each of these categories and build the category tree using each category's path. 3091 foreach ($mycoursecategories as $mycoursecat) { 3092 $pathcategoryids = explode('/', $mycoursecat->path); 3093 // First element of the exploded path is empty since paths begin with '/'. 3094 array_shift($pathcategoryids); 3095 // Merge the exploded category IDs into the full list of category IDs that we will fetch. 3096 $fullpathcategoryids = array_merge($fullpathcategoryids, $pathcategoryids); 3097 } 3098 3099 // Fetch all of the categories related to the user's courses. 3100 $pathcategories = core_course_category::get_many($fullpathcategoryids); 3101 // Loop over each of these categories and build the category tree. 3102 foreach ($pathcategories as $coursecat) { 3103 // No need to process categories that have already been added. 3104 if (isset($this->addedcategories[$coursecat->id])) { 3105 continue; 3106 } 3107 // Skip categories that are not visible. 3108 if (!$coursecat->is_uservisible()) { 3109 continue; 3110 } 3111 3112 // Get this course category's parent node. 3113 $parent = null; 3114 if ($coursecat->parent && isset($this->addedcategories[$coursecat->parent])) { 3115 $parent = $this->addedcategories[$coursecat->parent]; 3116 } 3117 if (!$parent) { 3118 // If it has no parent, then it should be right under the My courses node. 3119 $parent = $this->rootnodes['mycourses']; 3120 } 3121 3122 // Build the category object based from the coursecat object. 3123 $mycategory = new stdClass(); 3124 $mycategory->id = $coursecat->id; 3125 $mycategory->name = $coursecat->name; 3126 $mycategory->visible = $coursecat->visible; 3127 3128 // Add this category to the nav tree. 3129 $this->add_category($mycategory, $parent, self::TYPE_MY_CATEGORY); 3130 } 3131 } 3132 3133 // Go through each course now and add it to the nav block, and the flatnav if applicable. 3134 foreach ($courses as $course) { 3135 $node = $this->add_course($course, false, self::COURSE_MY); 3136 if ($node) { 3137 $node->showinflatnavigation = false; 3138 // Check if we should also add this to the flat nav as well. 3139 if (isset($flatnavcourses[$course->id])) { 3140 $node->showinflatnavigation = true; 3141 } 3142 } 3143 } 3144 3145 // Go through each course in the flatnav now. 3146 foreach ($flatnavcourses as $course) { 3147 // Check if we haven't already added it. 3148 if (!isset($courses[$course->id])) { 3149 // Ok, add it to the flatnav only. 3150 $node = $this->add_course($course, false, self::COURSE_MY); 3151 $node->display = false; 3152 $node->showinflatnavigation = true; 3153 } 3154 } 3155 3156 $showmorelinkinnav = $numtotalcourses > $numshowncourses; 3157 $showmorelinkinflatnav = $numtotalflatnavcourses > $numshownflatnavcourses; 3158 // Show a link to the course page if there are more courses the user is enrolled in. 3159 if ($showmorelinkinnav || $showmorelinkinflatnav) { 3160 // Adding hash to URL so the link is not highlighted in the navigation when clicked. 3161 $url = new moodle_url('/my/'); 3162 $parent = $this->rootnodes['mycourses']; 3163 $coursenode = $parent->add(get_string('morenavigationlinks'), $url, self::TYPE_CUSTOM, null, self::COURSE_INDEX_PAGE); 3164 3165 if ($showmorelinkinnav) { 3166 $coursenode->display = true; 3167 } 3168 3169 if ($showmorelinkinflatnav) { 3170 $coursenode->showinflatnavigation = true; 3171 } 3172 } 3173 } 3174 } 3175 3176 /** 3177 * The global navigation class used especially for AJAX requests. 3178 * 3179 * The primary methods that are used in the global navigation class have been overriden 3180 * to ensure that only the relevant branch is generated at the root of the tree. 3181 * This can be done because AJAX is only used when the backwards structure for the 3182 * requested branch exists. 3183 * This has been done only because it shortens the amounts of information that is generated 3184 * which of course will speed up the response time.. because no one likes laggy AJAX. 3185 * 3186 * @package core 3187 * @category navigation 3188 * @copyright 2009 Sam Hemelryk 3189 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 3190 */ 3191 class global_navigation_for_ajax extends global_navigation { 3192 3193 /** @var int used for determining what type of navigation_node::TYPE_* is being used */ 3194 protected $branchtype; 3195 3196 /** @var int the instance id */ 3197 protected $instanceid; 3198 3199 /** @var array Holds an array of expandable nodes */ 3200 protected $expandable = array(); 3201 3202 /** 3203 * Constructs the navigation for use in an AJAX request 3204 * 3205 * @param moodle_page $page moodle_page object 3206 * @param int $branchtype 3207 * @param int $id 3208 */ 3209 public function __construct($page, $branchtype, $id) { 3210 $this->page = $page; 3211 $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); 3212 $this->children = new navigation_node_collection(); 3213 $this->branchtype = $branchtype; 3214 $this->instanceid = $id; 3215 $this->initialise(); 3216 } 3217 /** 3218 * Initialise the navigation given the type and id for the branch to expand. 3219 * 3220 * @return array An array of the expandable nodes 3221 */ 3222 public function initialise() { 3223 global $DB, $SITE; 3224 3225 if ($this->initialised || during_initial_install()) { 3226 return $this->expandable; 3227 } 3228 $this->initialised = true; 3229 3230 $this->rootnodes = array(); 3231 $this->rootnodes['site'] = $this->add_course($SITE); 3232 $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); 3233 $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); 3234 // The courses branch is always displayed, and is always expandable (although may be empty). 3235 // This mimicks what is done during {@link global_navigation::initialise()}. 3236 $this->rootnodes['courses']->isexpandable = true; 3237 3238 // Branchtype will be one of navigation_node::TYPE_* 3239 switch ($this->branchtype) { 3240 case 0: 3241 if ($this->instanceid === 'mycourses') { 3242 $this->load_courses_enrolled(); 3243 } else if ($this->instanceid === 'courses') { 3244 $this->load_courses_other(); 3245 } 3246 break; 3247 case self::TYPE_CATEGORY : 3248 $this->load_category($this->instanceid); 3249 break; 3250 case self::TYPE_MY_CATEGORY : 3251 $this->load_category($this->instanceid, self::TYPE_MY_CATEGORY); 3252 break; 3253 case self::TYPE_COURSE : 3254 $course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST); 3255 if (!can_access_course($course, null, '', true)) { 3256 // Thats OK all courses are expandable by default. We don't need to actually expand it we can just 3257 // add the course node and break. This leads to an empty node. 3258 $this->add_course($course); 3259 break; 3260 } 3261 require_course_login($course, true, null, false, true); 3262 $this->page->set_context(context_course::instance($course->id)); 3263 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 3264 $this->add_course_essentials($coursenode, $course); 3265 $this->load_course_sections($course, $coursenode); 3266 break; 3267 case self::TYPE_SECTION : 3268 $sql = 'SELECT c.*, cs.section AS sectionnumber 3269 FROM {course} c 3270 LEFT JOIN {course_sections} cs ON cs.course = c.id 3271 WHERE cs.id = ?'; 3272 $course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST); 3273 require_course_login($course, true, null, false, true); 3274 $this->page->set_context(context_course::instance($course->id)); 3275 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 3276 $this->add_course_essentials($coursenode, $course); 3277 $this->load_course_sections($course, $coursenode, $course->sectionnumber); 3278 break; 3279 case self::TYPE_ACTIVITY : 3280 $sql = "SELECT c.* 3281 FROM {course} c 3282 JOIN {course_modules} cm ON cm.course = c.id 3283 WHERE cm.id = :cmid"; 3284 $params = array('cmid' => $this->instanceid); 3285 $course = $DB->get_record_sql($sql, $params, MUST_EXIST); 3286 $modinfo = get_fast_modinfo($course); 3287 $cm = $modinfo->get_cm($this->instanceid); 3288 require_course_login($course, true, $cm, false, true); 3289 $this->page->set_context(context_module::instance($cm->id)); 3290 $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); 3291 $this->load_course_sections($course, $coursenode, null, $cm); 3292 $activitynode = $coursenode->find($cm->id, self::TYPE_ACTIVITY); 3293 if ($activitynode) { 3294 $modulenode = $this->load_activity($cm, $course, $activitynode); 3295 } 3296 break; 3297 default: 3298 throw new Exception('Unknown type'); 3299 return $this->expandable; 3300 } 3301 3302 if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id) { 3303 $this->load_for_user(null, true); 3304 } 3305 3306 // Give the local plugins a chance to include some navigation if they want. 3307 $this->load_local_plugin_navigation(); 3308 3309 $this->find_expandable($this->expandable); 3310 return $this->expandable; 3311 } 3312 3313 /** 3314 * They've expanded the general 'courses' branch. 3315 */ 3316 protected function load_courses_other() { 3317 $this->load_all_courses(); 3318 } 3319 3320 /** 3321 * Loads a single category into the AJAX navigation. 3322 * 3323 * This function is special in that it doesn't concern itself with the parent of 3324 * the requested category or its siblings. 3325 * This is because with the AJAX navigation we know exactly what is wanted and only need to 3326 * request that. 3327 * 3328 * @global moodle_database $DB 3329 * @param int $categoryid id of category to load in navigation. 3330 * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY 3331 * @return void. 3332 */ 3333 protected function load_category($categoryid, $nodetype = self::TYPE_CATEGORY) { 3334 global $CFG, $DB; 3335 3336 $limit = 20; 3337 if (!empty($CFG->navcourselimit)) { 3338 $limit = (int)$CFG->navcourselimit; 3339 } 3340 3341 $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); 3342 $sql = "SELECT cc.*, $catcontextsql 3343 FROM {course_categories} cc 3344 JOIN {context} ctx ON cc.id = ctx.instanceid 3345 WHERE ctx.contextlevel = ".CONTEXT_COURSECAT." AND 3346 (cc.id = :categoryid1 OR cc.parent = :categoryid2) 3347 ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; 3348 $params = array('categoryid1' => $categoryid, 'categoryid2' => $categoryid); 3349 $categories = $DB->get_recordset_sql($sql, $params, 0, $limit); 3350 $categorylist = array(); 3351 $subcategories = array(); 3352 $basecategory = null; 3353 foreach ($categories as $category) { 3354 $categorylist[] = $category->id; 3355 context_helper::preload_from_record($category); 3356 if ($category->id == $categoryid) { 3357 $this->add_category($category, $this, $nodetype); 3358 $basecategory = $this->addedcategories[$category->id]; 3359 } else { 3360 $subcategories[$category->id] = $category; 3361 } 3362 } 3363 $categories->close(); 3364 3365 3366 // If category is shown in MyHome then only show enrolled courses and hide empty subcategories, 3367 // else show all courses. 3368 if ($nodetype === self::TYPE_MY_CATEGORY) { 3369 $courses = enrol_get_my_courses('*'); 3370 $categoryids = array(); 3371 3372 // Only search for categories if basecategory was found. 3373 if (!is_null($basecategory)) { 3374 // Get course parent category ids. 3375 foreach ($courses as $course) { 3376 $categoryids[] = $course->category; 3377 } 3378 3379 // Get a unique list of category ids which a part of the path 3380 // to user's courses. 3381 $coursesubcategories = array(); 3382 $addedsubcategories = array(); 3383 3384 list($sql, $params) = $DB->get_in_or_equal($categoryids); 3385 $categories = $DB->get_recordset_select('course_categories', 'id '.$sql, $params, 'sortorder, id', 'id, path'); 3386 3387 foreach ($categories as $category){ 3388 $coursesubcategories = array_merge($coursesubcategories, explode('/', trim($category->path, "/"))); 3389 } 3390 $categories->close(); 3391 $coursesubcategories = array_unique($coursesubcategories); 3392 3393 // Only add a subcategory if it is part of the path to user's course and 3394 // wasn't already added. 3395 foreach ($subcategories as $subid => $subcategory) { 3396 if (in_array($subid, $coursesubcategories) && 3397 !in_array($subid, $addedsubcategories)) { 3398 $this->add_category($subcategory, $basecategory, $nodetype); 3399 $addedsubcategories[] = $subid; 3400 } 3401 } 3402 } 3403 3404 foreach ($courses as $course) { 3405 // Add course if it's in category. 3406 if (in_array($course->category, $categorylist)) { 3407 $this->add_course($course, true, self::COURSE_MY); 3408 } 3409 } 3410 } else { 3411 if (!is_null($basecategory)) { 3412 foreach ($subcategories as $key=>$category) { 3413 $this->add_category($category, $basecategory, $nodetype); 3414 } 3415 } 3416 $courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit); 3417 foreach ($courses as $course) { 3418 $this->add_course($course); 3419 } 3420 $courses->close(); 3421 } 3422 } 3423 3424 /** 3425 * Returns an array of expandable nodes 3426 * @return array 3427 */ 3428 public function get_expandable() { 3429 return $this->expandable; 3430 } 3431 } 3432 3433 /** 3434 * Navbar class 3435 * 3436 * This class is used to manage the navbar, which is initialised from the navigation 3437 * object held by PAGE 3438 * 3439 * @package core 3440 * @category navigation 3441 * @copyright 2009 Sam Hemelryk 3442 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 3443 */ 3444 class navbar extends navigation_node { 3445 /** @var bool A switch for whether the navbar is initialised or not */ 3446 protected $initialised = false; 3447 /** @var mixed keys used to reference the nodes on the navbar */ 3448 protected $keys = array(); 3449 /** @var null|string content of the navbar */ 3450 protected $content = null; 3451 /** @var moodle_page object the moodle page that this navbar belongs to */ 3452 protected $page; 3453 /** @var bool A switch for whether to ignore the active navigation information */ 3454 protected $ignoreactive = false; 3455 /** @var bool A switch to let us know if we are in the middle of an install */ 3456 protected $duringinstall = false; 3457 /** @var bool A switch for whether the navbar has items */ 3458 protected $hasitems = false; 3459 /** @var array An array of navigation nodes for the navbar */ 3460 protected $items; 3461 /** @var array An array of child node objects */ 3462 public $children = array(); 3463 /** @var bool A switch for whether we want to include the root node in the navbar */ 3464 public $includesettingsbase = false; 3465 /** @var breadcrumb_navigation_node[] $prependchildren */ 3466 protected $prependchildren = array(); 3467 3468 /** 3469 * The almighty constructor 3470 * 3471 * @param moodle_page $page 3472 */ 3473 public function __construct(moodle_page $page) { 3474 global $CFG; 3475 if (during_initial_install()) { 3476 $this->duringinstall = true; 3477 return false; 3478 } 3479 $this->page = $page; 3480 $this->text = get_string('home'); 3481 $this->shorttext = get_string('home'); 3482 $this->action = new moodle_url($CFG->wwwroot); 3483 $this->nodetype = self::NODETYPE_BRANCH; 3484 $this->type = self::TYPE_SYSTEM; 3485 } 3486 3487 /** 3488 * Quick check to see if the navbar will have items in. 3489 * 3490 * @return bool Returns true if the navbar will have items, false otherwise 3491 */ 3492 public function has_items() { 3493 if ($this->duringinstall) { 3494 return false; 3495 } else if ($this->hasitems !== false) { 3496 return true; 3497 } 3498 if (count($this->children) > 0 || count($this->prependchildren) > 0) { 3499 // There have been manually added items - there are definitely items. 3500 $outcome = true; 3501 } else if (!$this->ignoreactive) { 3502 // We will need to initialise the navigation structure to check if there are active items. 3503 $this->page->navigation->initialise($this->page); 3504 $outcome = ($this->page->navigation->contains_active_node() || $this->page->settingsnav->contains_active_node()); 3505 } 3506 $this->hasitems = $outcome; 3507 return $outcome; 3508 } 3509 3510 /** 3511 * Turn on/off ignore active 3512 * 3513 * @param bool $setting 3514 */ 3515 public function ignore_active($setting=true) { 3516 $this->ignoreactive = ($setting); 3517 } 3518 3519 /** 3520 * Gets a navigation node 3521 * 3522 * @param string|int $key for referencing the navbar nodes 3523 * @param int $type breadcrumb_navigation_node::TYPE_* 3524 * @return breadcrumb_navigation_node|bool 3525 */ 3526 public function get($key, $type = null) { 3527 foreach ($this->children as &$child) { 3528 if ($child->key === $key && ($type == null || $type == $child->type)) { 3529 return $child; 3530 } 3531 } 3532 foreach ($this->prependchildren as &$child) { 3533 if ($child->key === $key && ($type == null || $type == $child->type)) { 3534 return $child; 3535 } 3536 } 3537 return false; 3538 } 3539 /** 3540 * Returns an array of breadcrumb_navigation_nodes that make up the navbar. 3541 * 3542 * @return array 3543 */ 3544 public function get_items() { 3545 global $CFG; 3546 $items = array(); 3547 // Make sure that navigation is initialised 3548 if (!$this->has_items()) { 3549 return $items; 3550 } 3551 if ($this->items !== null) { 3552 return $this->items; 3553 } 3554 3555 if (count($this->children) > 0) { 3556 // Add the custom children. 3557 $items = array_reverse($this->children); 3558 } 3559 3560 // Check if navigation contains the active node 3561 if (!$this->ignoreactive) { 3562 // We will need to ensure the navigation has been initialised. 3563 $this->page->navigation->initialise($this->page); 3564 // Now find the active nodes on both the navigation and settings. 3565 $navigationactivenode = $this->page->navigation->find_active_node(); 3566 $settingsactivenode = $this->page->settingsnav->find_active_node(); 3567 3568 if ($navigationactivenode && $settingsactivenode) { 3569 // Parse a combined navigation tree 3570 while ($settingsactivenode && $settingsactivenode->parent !== null) { 3571 if (!$settingsactivenode->mainnavonly) { 3572 $items[] = new breadcrumb_navigation_node($settingsactivenode); 3573 } 3574 $settingsactivenode = $settingsactivenode->parent; 3575 } 3576 if (!$this->includesettingsbase) { 3577 // Removes the first node from the settings (root node) from the list 3578 array_pop($items); 3579 } 3580 while ($navigationactivenode && $navigationactivenode->parent !== null) { 3581 if (!$navigationactivenode->mainnavonly) { 3582 $items[] = new breadcrumb_navigation_node($navigationactivenode); 3583 } 3584 if (!empty($CFG->navshowcategories) && 3585 $navigationactivenode->type === self::TYPE_COURSE && 3586 $navigationactivenode->parent->key === 'currentcourse') { 3587 foreach ($this->get_course_categories() as $item) { 3588 $items[] = new breadcrumb_navigation_node($item); 3589 } 3590 } 3591 $navigationactivenode = $navigationactivenode->parent; 3592 } 3593 } else if ($navigationactivenode) { 3594 // Parse the navigation tree to get the active node 3595 while ($navigationactivenode && $navigationactivenode->parent !== null) { 3596 if (!$navigationactivenode->mainnavonly) { 3597 $items[] = new breadcrumb_navigation_node($navigationactivenode); 3598 } 3599 if (!empty($CFG->navshowcategories) && 3600 $navigationactivenode->type === self::TYPE_COURSE && 3601 $navigationactivenode->parent->key === 'currentcourse') { 3602 foreach ($this->get_course_categories() as $item) { 3603 $items[] = new breadcrumb_navigation_node($item); 3604 } 3605 } 3606 $navigationactivenode = $navigationactivenode->parent; 3607 } 3608 } else if ($settingsactivenode) { 3609 // Parse the settings navigation to get the active node 3610 while ($settingsactivenode && $settingsactivenode->parent !== null) { 3611 if (!$settingsactivenode->mainnavonly) { 3612 $items[] = new breadcrumb_navigation_node($settingsactivenode); 3613 } 3614 $settingsactivenode = $settingsactivenode->parent; 3615 } 3616 } 3617 } 3618 3619 $items[] = new breadcrumb_navigation_node(array( 3620 'text' => $this->page->navigation->text, 3621 'shorttext' => $this->page->navigation->shorttext, 3622 'key' => $this->page->navigation->key, 3623 'action' => $this->page->navigation->action 3624 )); 3625 3626 if (count($this->prependchildren) > 0) { 3627 // Add the custom children 3628 $items = array_merge($items, array_reverse($this->prependchildren)); 3629 } 3630 3631 $last = reset($items); 3632 if ($last) { 3633 $last->set_last(true); 3634 } 3635 $this->items = array_reverse($items); 3636 return $this->items; 3637 } 3638 3639 /** 3640 * Get the list of categories leading to this course. 3641 * 3642 * This function is used by {@link navbar::get_items()} to add back the "courses" 3643 * node and category chain leading to the current course. Note that this is only ever 3644 * called for the current course, so we don't need to bother taking in any parameters. 3645 * 3646 * @return array 3647 */ 3648 private function get_course_categories() { 3649 global $CFG; 3650 require_once($CFG->dirroot.'/course/lib.php'); 3651 3652 $categories = array(); 3653 $cap = 'moodle/category:viewhiddencategories'; 3654 $showcategories = !core_course_category::is_simple_site(); 3655 3656 if ($showcategories) { 3657 foreach ($this->page->categories as $category) { 3658 $context = context_coursecat::instance($category->id); 3659 if (!core_course_category::can_view_category($category)) { 3660 continue; 3661 } 3662 $url = new moodle_url('/course/index.php', array('categoryid' => $category->id)); 3663 $name = format_string($category->name, true, array('context' => $context)); 3664 $categorynode = breadcrumb_navigation_node::create($name, $url, self::TYPE_CATEGORY, null, $category->id); 3665 if (!$category->visible) { 3666 $categorynode->hidden = true; 3667 } 3668 $categories[] = $categorynode; 3669 } 3670 } 3671 3672 // Don't show the 'course' node if enrolled in this course. 3673 $coursecontext = context_course::instance($this->page->course->id); 3674 if (!is_enrolled($coursecontext, null, '', true)) { 3675 $courses = $this->page->navigation->get('courses'); 3676 if (!$courses) { 3677 // Courses node may not be present. 3678 $courses = breadcrumb_navigation_node::create( 3679 get_string('courses'), 3680 new moodle_url('/course/index.php'), 3681 self::TYPE_CONTAINER 3682 ); 3683 } 3684 $categories[] = $courses; 3685 } 3686 3687 return $categories; 3688 } 3689 3690 /** 3691 * Add a new breadcrumb_navigation_node to the navbar, overrides parent::add 3692 * 3693 * This function overrides {@link breadcrumb_navigation_node::add()} so that we can change 3694 * the way nodes get added to allow us to simply call add and have the node added to the 3695 * end of the navbar 3696 * 3697 * @param string $text 3698 * @param string|moodle_url|action_link $action An action to associate with this node. 3699 * @param int $type One of navigation_node::TYPE_* 3700 * @param string $shorttext 3701 * @param string|int $key A key to identify this node with. Key + type is unique to a parent. 3702 * @param pix_icon $icon An optional icon to use for this node. 3703 * @return navigation_node 3704 */ 3705 public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { 3706 if ($this->content !== null) { 3707 debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); 3708 } 3709 3710 // Properties array used when creating the new navigation node 3711 $itemarray = array( 3712 'text' => $text, 3713 'type' => $type 3714 ); 3715 // Set the action if one was provided 3716 if ($action!==null) { 3717 $itemarray['action'] = $action; 3718 } 3719 // Set the shorttext if one was provided 3720 if ($shorttext!==null) { 3721 $itemarray['shorttext'] = $shorttext; 3722 } 3723 // Set the icon if one was provided 3724 if ($icon!==null) { 3725 $itemarray['icon'] = $icon; 3726 } 3727 // Default the key to the number of children if not provided 3728 if ($key === null) { 3729 $key = count($this->children); 3730 } 3731 // Set the key 3732 $itemarray['key'] = $key; 3733 // Set the parent to this node 3734 $itemarray['parent'] = $this; 3735 // Add the child using the navigation_node_collections add method 3736 $this->children[] = new breadcrumb_navigation_node($itemarray); 3737 return $this; 3738 } 3739 3740 /** 3741 * Prepends a new navigation_node to the start of the navbar 3742 * 3743 * @param string $text 3744 * @param string|moodle_url|action_link $action An action to associate with this node. 3745 * @param int $type One of navigation_node::TYPE_* 3746 * @param string $shorttext 3747 * @param string|int $key A key to identify this node with. Key + type is unique to a parent. 3748 * @param pix_icon $icon An optional icon to use for this node. 3749 * @return navigation_node 3750 */ 3751 public function prepend($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { 3752 if ($this->content !== null) { 3753 debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); 3754 } 3755 // Properties array used when creating the new navigation node. 3756 $itemarray = array( 3757 'text' => $text, 3758 'type' => $type 3759 ); 3760 // Set the action if one was provided. 3761 if ($action!==null) { 3762 $itemarray['action'] = $action; 3763 } 3764 // Set the shorttext if one was provided. 3765 if ($shorttext!==null) { 3766 $itemarray['shorttext'] = $shorttext; 3767 } 3768 // Set the icon if one was provided. 3769 if ($icon!==null) { 3770 $itemarray['icon'] = $icon; 3771 } 3772 // Default the key to the number of children if not provided. 3773 if ($key === null) { 3774 $key = count($this->children); 3775 } 3776 // Set the key. 3777 $itemarray['key'] = $key; 3778 // Set the parent to this node. 3779 $itemarray['parent'] = $this; 3780 // Add the child node to the prepend list. 3781 $this->prependchildren[] = new breadcrumb_navigation_node($itemarray); 3782 return $this; 3783 } 3784 } 3785 3786 /** 3787 * Subclass of navigation_node allowing different rendering for the breadcrumbs 3788 * in particular adding extra metadata for search engine robots to leverage. 3789 * 3790 * @package core 3791 * @category navigation 3792 * @copyright 2015 Brendan Heywood 3793 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 3794 */ 3795 class breadcrumb_navigation_node extends navigation_node { 3796 3797 /** @var $last boolean A flag indicating this is the last item in the list of breadcrumbs. */ 3798 private $last = false; 3799 3800 /** 3801 * A proxy constructor 3802 * 3803 * @param mixed $navnode A navigation_node or an array 3804 */ 3805 public function __construct($navnode) { 3806 if (is_array($navnode)) { 3807 parent::__construct($navnode); 3808 } else if ($navnode instanceof navigation_node) { 3809 3810 // Just clone everything. 3811 $objvalues = get_object_vars($navnode); 3812 foreach ($objvalues as $key => $value) { 3813 $this->$key = $value; 3814 } 3815 } else { 3816 throw new coding_exception('Not a valid breadcrumb_navigation_node'); 3817 } 3818 } 3819 3820 /** 3821 * Getter for "last" 3822 * @return boolean 3823 */ 3824 public function is_last() { 3825 return $this->last; 3826 } 3827 3828 /** 3829 * Setter for "last" 3830 * @param $val boolean 3831 */ 3832 public function set_last($val) { 3833 $this->last = $val; 3834 } 3835 } 3836 3837 /** 3838 * Subclass of navigation_node allowing different rendering for the flat navigation 3839 * in particular allowing dividers and indents. 3840 * 3841 * @package core 3842 * @category navigation 3843 * @copyright 2016 Damyon Wiese 3844 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 3845 */ 3846 class flat_navigation_node extends navigation_node { 3847 3848 /** @var $indent integer The indent level */ 3849 private $indent = 0; 3850 3851 /** @var $showdivider bool Show a divider before this element */ 3852 private $showdivider = false; 3853 3854 /** @var $collectionlabel string Label for a group of nodes */ 3855 private $collectionlabel = ''; 3856 3857 /** 3858 * A proxy constructor 3859 * 3860 * @param mixed $navnode A navigation_node or an array 3861 */ 3862 public function __construct($navnode, $indent) { 3863 if (is_array($navnode)) { 3864 parent::__construct($navnode); 3865 } else if ($navnode instanceof navigation_node) { 3866 3867 // Just clone everything. 3868 $objvalues = get_object_vars($navnode); 3869 foreach ($objvalues as $key => $value) { 3870 $this->$key = $value; 3871 } 3872 } else { 3873 throw new coding_exception('Not a valid flat_navigation_node'); 3874 } 3875 $this->indent = $indent; 3876 } 3877 3878 /** 3879 * Setter, a label is required for a flat navigation node that shows a divider. 3880 * 3881 * @param string $label 3882 */ 3883 public function set_collectionlabel($label) { 3884 $this->collectionlabel = $label; 3885 } 3886 3887 /** 3888 * Getter, get the label for this flat_navigation node, or it's parent if it doesn't have one. 3889 * 3890 * @return string 3891 */ 3892 public function get_collectionlabel() { 3893 if (!empty($this->collectionlabel)) { 3894 return $this->collectionlabel; 3895 } 3896 if ($this->parent && ($this->parent instanceof flat_navigation_node || $this->parent instanceof flat_navigation)) { 3897 return $this->parent->get_collectionlabel(); 3898 } 3899 debugging('Navigation region requires a label', DEBUG_DEVELOPER); 3900 return ''; 3901 } 3902 3903 /** 3904 * Does this node represent a course section link. 3905 * @return boolean 3906 */ 3907 public function is_section() { 3908 return $this->type == navigation_node::TYPE_SECTION; 3909 } 3910 3911 /** 3912 * In flat navigation - sections are active if we are looking at activities in the section. 3913 * @return boolean 3914 */ 3915 public function isactive() { 3916 global $PAGE; 3917 3918 if ($this->is_section()) { 3919 $active = $PAGE->navigation->find_active_node(); 3920 while ($active = $active->parent) { 3921 if ($active->key == $this->key && $active->type == $this->type) { 3922 return true; 3923 } 3924 } 3925 } 3926 return $this->isactive; 3927 } 3928 3929 /** 3930 * Getter for "showdivider" 3931 * @return boolean 3932 */ 3933 public function showdivider() { 3934 return $this->showdivider; 3935 } 3936 3937 /** 3938 * Setter for "showdivider" 3939 * @param $val boolean 3940 * @param $label string Label for the group of nodes 3941 */ 3942 public function set_showdivider($val, $label = '') { 3943 $this->showdivider = $val; 3944 if ($this->showdivider && empty($label)) { 3945 debugging('Navigation region requires a label', DEBUG_DEVELOPER); 3946 } else { 3947 $this->set_collectionlabel($label); 3948 } 3949 } 3950 3951 /** 3952 * Getter for "indent" 3953 * @return boolean 3954 */ 3955 public function get_indent() { 3956 return $this->indent; 3957 } 3958 3959 /** 3960 * Setter for "indent" 3961 * @param $val boolean 3962 */ 3963 public function set_indent($val) { 3964 $this->indent = $val; 3965 } 3966 } 3967 3968 /** 3969 * Class used to generate a collection of navigation nodes most closely related 3970 * to the current page. 3971 * 3972 * @package core 3973 * @copyright 2016 Damyon Wiese 3974 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 3975 */ 3976 class flat_navigation extends navigation_node_collection { 3977 /** @var moodle_page the moodle page that the navigation belongs to */ 3978 protected $page; 3979 3980 /** 3981 * Constructor. 3982 * 3983 * @param moodle_page $page 3984 */ 3985 public function __construct(moodle_page &$page) { 3986 if (during_initial_install()) { 3987 return false; 3988 } 3989 $this->page = $page; 3990 } 3991 3992 /** 3993 * Build the list of navigation nodes based on the current navigation and settings trees. 3994 * 3995 */ 3996 public function initialise() { 3997 global $PAGE, $USER, $OUTPUT, $CFG; 3998 if (during_initial_install()) { 3999 return; 4000 } 4001 4002 $current = false; 4003 4004 $course = $PAGE->course; 4005 4006 $this->page->navigation->initialise(); 4007 4008 // First walk the nav tree looking for "flat_navigation" nodes. 4009 if ($course->id > 1) { 4010 // It's a real course. 4011 $url = new moodle_url('/course/view.php', array('id' => $course->id)); 4012 4013 $coursecontext = context_course::instance($course->id, MUST_EXIST); 4014 // This is the name that will be shown for the course. 4015 $coursename = empty($CFG->navshowfullcoursenames) ? 4016 format_string($course->shortname, true, array('context' => $coursecontext)) : 4017 format_string($course->fullname, true, array('context' => $coursecontext)); 4018 4019 $flat = new flat_navigation_node(navigation_node::create($coursename, $url), 0); 4020 $flat->set_collectionlabel($coursename); 4021 $flat->key = 'coursehome'; 4022 $flat->icon = new pix_icon('i/course', ''); 4023 4024 $courseformat = course_get_format($course); 4025 $coursenode = $PAGE->navigation->find_active_node(); 4026 $targettype = navigation_node::TYPE_COURSE; 4027 4028 // Single activity format has no course node - the course node is swapped for the activity node. 4029 if (!$courseformat->has_view_page()) { 4030 $targettype = navigation_node::TYPE_ACTIVITY; 4031 } 4032 4033 while (!empty($coursenode) && ($coursenode->type != $targettype)) { 4034 $coursenode = $coursenode->parent; 4035 } 4036 // There is one very strange page in mod/feedback/view.php which thinks it is both site and course 4037 // context at the same time. That page is broken but we need to handle it (hence the SITEID). 4038 if ($coursenode && $coursenode->key != SITEID) { 4039 $this->add($flat); 4040 foreach ($coursenode->children as $child) { 4041 if ($child->action) { 4042 $flat = new flat_navigation_node($child, 0); 4043 $this->add($flat); 4044 } 4045 } 4046 } 4047 4048 $this->page->navigation->build_flat_navigation_list($this, true, get_string('site')); 4049 } else { 4050 $this->page->navigation->build_flat_navigation_list($this, false, get_string('site')); 4051 } 4052 4053 $admin = $PAGE->settingsnav->find('siteadministration', navigation_node::TYPE_SITE_ADMIN); 4054 if (!$admin) { 4055 // Try again - crazy nav tree! 4056 $admin = $PAGE->settingsnav->find('root', navigation_node::TYPE_SITE_ADMIN); 4057 } 4058 if ($admin) { 4059 $flat = new flat_navigation_node($admin, 0); 4060 $flat->set_showdivider(true, get_string('sitesettings')); 4061 $flat->key = 'sitesettings'; 4062 $flat->icon = new pix_icon('t/preferences', ''); 4063 $this->add($flat); 4064 } 4065 4066 // Add-a-block in editing mode. 4067 if (isset($this->page->theme->addblockposition) && 4068 $this->page->theme->addblockposition == BLOCK_ADDBLOCK_POSITION_FLATNAV && 4069 $PAGE->user_is_editing() && $PAGE->user_can_edit_blocks() && 4070 ($addable = $PAGE->blocks->get_addable_blocks())) { 4071 $url = new moodle_url($PAGE->url, ['bui_addblock' => '', 'sesskey' => sesskey()]); 4072 $addablock = navigation_node::create(get_string('addblock'), $url); 4073 $flat = new flat_navigation_node($addablock, 0); 4074 $flat->set_showdivider(true, get_string('blocksaddedit')); 4075 $flat->key = 'addblock'; 4076 $flat->icon = new pix_icon('i/addblock', ''); 4077 $this->add($flat); 4078 $blocks = []; 4079 foreach ($addable as $block) { 4080 $blocks[] = $block->name; 4081 } 4082 $params = array('blocks' => $blocks, 'url' => '?' . $url->get_query_string(false)); 4083 $PAGE->requires->js_call_amd('core/addblockmodal', 'init', array($params)); 4084 } 4085 } 4086 4087 4088 /** 4089 * Override the parent so we can set a label for this collection if it has not been set yet. 4090 * 4091 * @param navigation_node $node Node to add 4092 * @param string $beforekey If specified, adds before a node with this key, 4093 * otherwise adds at end 4094 * @return navigation_node Added node 4095 */ 4096 public function add(navigation_node $node, $beforekey=null) { 4097 $result = parent::add($node, $beforekey); 4098 // Extend the parent to get a name for the collection of nodes if required. 4099 if (empty($this->collectionlabel)) { 4100 if ($node instanceof flat_navigation_node) { 4101 $this->set_collectionlabel($node->get_collectionlabel()); 4102 } 4103 } 4104 4105 return $result; 4106 } 4107 } 4108 4109 /** 4110 * Class used to manage the settings option for the current page 4111 * 4112 * This class is used to manage the settings options in a tree format (recursively) 4113 * and was created initially for use with the settings blocks. 4114 * 4115 * @package core 4116 * @category navigation 4117 * @copyright 2009 Sam Hemelryk 4118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 4119 */ 4120 class settings_navigation extends navigation_node { 4121 /** @var stdClass the current context */ 4122 protected $context; 4123 /** @var moodle_page the moodle page that the navigation belongs to */ 4124 protected $page; 4125 /** @var string contains administration section navigation_nodes */ 4126 protected $adminsection; 4127 /** @var bool A switch to see if the navigation node is initialised */ 4128 protected $initialised = false; 4129 /** @var array An array of users that the nodes can extend for. */ 4130 protected $userstoextendfor = array(); 4131 /** @var navigation_cache **/ 4132 protected $cache; 4133 4134 /** 4135 * Sets up the object with basic settings and preparse it for use 4136 * 4137 * @param moodle_page $page 4138 */ 4139 public function __construct(moodle_page &$page) { 4140 if (during_initial_install()) { 4141 return false; 4142 } 4143 $this->page = $page; 4144 // Initialise the main navigation. It is most important that this is done 4145 // before we try anything 4146 $this->page->navigation->initialise(); 4147 // Initialise the navigation cache 4148 $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); 4149 $this->children = new navigation_node_collection(); 4150 } 4151 4152 /** 4153 * Initialise the settings navigation based on the current context 4154 * 4155 * This function initialises the settings navigation tree for a given context 4156 * by calling supporting functions to generate major parts of the tree. 4157 * 4158 */ 4159 public function initialise() { 4160 global $DB, $SESSION, $SITE; 4161 4162 if (during_initial_install()) { 4163 return false; 4164 } else if ($this->initialised) { 4165 return true; 4166 } 4167 $this->id = 'settingsnav'; 4168 $this->context = $this->page->context; 4169 4170 $context = $this->context; 4171 if ($context->contextlevel == CONTEXT_BLOCK) { 4172 $this->load_block_settings(); 4173 $context = $context->get_parent_context(); 4174 $this->context = $context; 4175 } 4176 switch ($context->contextlevel) { 4177 case CONTEXT_SYSTEM: 4178 if ($this->page->url->compare(new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings')))) { 4179 $this->load_front_page_settings(($context->id == $this->context->id)); 4180 } 4181 break; 4182 case CONTEXT_COURSECAT: 4183 $this->load_category_settings(); 4184 break; 4185 case CONTEXT_COURSE: 4186 if ($this->page->course->id != $SITE->id) { 4187 $this->load_course_settings(($context->id == $this->context->id)); 4188 } else { 4189 $this->load_front_page_settings(($context->id == $this->context->id)); 4190 } 4191 break; 4192 case CONTEXT_MODULE: 4193 $this->load_module_settings(); 4194 $this->load_course_settings(); 4195 break; 4196 case CONTEXT_USER: 4197 if ($this->page->course->id != $SITE->id) { 4198 $this->load_course_settings(); 4199 } 4200 break; 4201 } 4202 4203 $usersettings = $this->load_user_settings($this->page->course->id); 4204 4205 $adminsettings = false; 4206 if (isloggedin() && !isguestuser() && (!isset($SESSION->load_navigation_admin) || $SESSION->load_navigation_admin)) { 4207 $isadminpage = $this->is_admin_tree_needed(); 4208 4209 if (has_capability('moodle/site:configview', context_system::instance())) { 4210 if (has_capability('moodle/site:config', context_system::instance())) { 4211 // Make sure this works even if config capability changes on the fly 4212 // and also make it fast for admin right after login. 4213 $SESSION->load_navigation_admin = 1; 4214 if ($isadminpage) { 4215 $adminsettings = $this->load_administration_settings(); 4216 } 4217 4218 } else if (!isset($SESSION->load_navigation_admin)) { 4219 $adminsettings = $this->load_administration_settings(); 4220 $SESSION->load_navigation_admin = (int)($adminsettings->children->count() > 0); 4221 4222 } else if ($SESSION->load_navigation_admin) { 4223 if ($isadminpage) { 4224 $adminsettings = $this->load_administration_settings(); 4225 } 4226 } 4227 4228 // Print empty navigation node, if needed. 4229 if ($SESSION->load_navigation_admin && !$isadminpage) { 4230 if ($adminsettings) { 4231 // Do not print settings tree on pages that do not need it, this helps with performance. 4232 $adminsettings->remove(); 4233 $adminsettings = false; 4234 } 4235 $siteadminnode = $this->add(get_string('administrationsite'), new moodle_url('/admin/search.php'), 4236 self::TYPE_SITE_ADMIN, null, 'siteadministration'); 4237 $siteadminnode->id = 'expandable_branch_' . $siteadminnode->type . '_' . 4238 clean_param($siteadminnode->key, PARAM_ALPHANUMEXT); 4239 $siteadminnode->requiresajaxloading = 'true'; 4240 } 4241 } 4242 } 4243 4244 if ($context->contextlevel == CONTEXT_SYSTEM && $adminsettings) { 4245 $adminsettings->force_open(); 4246 } else if ($context->contextlevel == CONTEXT_USER && $usersettings) { 4247 $usersettings->force_open(); 4248 } 4249 4250 // At this point we give any local plugins the ability to extend/tinker with the navigation settings. 4251 $this->load_local_plugin_settings(); 4252 4253 foreach ($this->children as $key=>$node) { 4254 if ($node->nodetype == self::NODETYPE_BRANCH && $node->children->count() == 0) { 4255 // Site administration is shown as link. 4256 if (!empty($SESSION->load_navigation_admin) && ($node->type === self::TYPE_SITE_ADMIN)) { 4257 continue; 4258 } 4259 $node->remove(); 4260 } 4261 } 4262 $this->initialised = true; 4263 } 4264 /** 4265 * Override the parent function so that we can add preceeding hr's and set a 4266 * root node class against all first level element 4267 * 4268 * It does this by first calling the parent's add method {@link navigation_node::add()} 4269 * and then proceeds to use the key to set class and hr 4270 * 4271 * @param string $text text to be used for the link. 4272 * @param string|moodle_url $url url for the new node 4273 * @param int $type the type of node navigation_node::TYPE_* 4274 * @param string $shorttext 4275 * @param string|int $key a key to access the node by. 4276 * @param pix_icon $icon An icon that appears next to the node. 4277 * @return navigation_node with the new node added to it. 4278 */ 4279 public function add($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { 4280 $node = parent::add($text, $url, $type, $shorttext, $key, $icon); 4281 $node->add_class('root_node'); 4282 return $node; 4283 } 4284 4285 /** 4286 * This function allows the user to add something to the start of the settings 4287 * navigation, which means it will be at the top of the settings navigation block 4288 * 4289 * @param string $text text to be used for the link. 4290 * @param string|moodle_url $url url for the new node 4291 * @param int $type the type of node navigation_node::TYPE_* 4292 * @param string $shorttext 4293 * @param string|int $key a key to access the node by. 4294 * @param pix_icon $icon An icon that appears next to the node. 4295 * @return navigation_node $node with the new node added to it. 4296 */ 4297 public function prepend($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { 4298 $children = $this->children; 4299 $childrenclass = get_class($children); 4300 $this->children = new $childrenclass; 4301 $node = $this->add($text, $url, $type, $shorttext, $key, $icon); 4302 foreach ($children as $child) { 4303 $this->children->add($child); 4304 } 4305 return $node; 4306 } 4307 4308 /** 4309 * Does this page require loading of full admin tree or is 4310 * it enough rely on AJAX? 4311 * 4312 * @return bool 4313 */ 4314 protected function is_admin_tree_needed() { 4315 if (self::$loadadmintree) { 4316 // Usually external admin page or settings page. 4317 return true; 4318 } 4319 4320 if ($this->page->pagelayout === 'admin' or strpos($this->page->pagetype, 'admin-') === 0) { 4321 // Admin settings tree is intended for system level settings and management only, use navigation for the rest! 4322 if ($this->page->context->contextlevel != CONTEXT_SYSTEM) { 4323 return false; 4324 } 4325 return true; 4326 } 4327 4328 return false; 4329 } 4330 4331 /** 4332 * Load the site administration tree 4333 * 4334 * This function loads the site administration tree by using the lib/adminlib library functions 4335 * 4336 * @param navigation_node $referencebranch A reference to a branch in the settings 4337 * navigation tree 4338 * @param part_of_admin_tree $adminbranch The branch to add, if null generate the admin 4339 * tree and start at the beginning 4340 * @return mixed A key to access the admin tree by 4341 */ 4342 protected function load_administration_settings(navigation_node $referencebranch=null, part_of_admin_tree $adminbranch=null) { 4343 global $CFG; 4344 4345 // Check if we are just starting to generate this navigation. 4346 if ($referencebranch === null) { 4347 4348 // Require the admin lib then get an admin structure 4349 if (!function_exists('admin_get_root')) { 4350 require_once($CFG->dirroot.'/lib/adminlib.php'); 4351 } 4352 $adminroot = admin_get_root(false, false); 4353 // This is the active section identifier 4354 $this->adminsection = $this->page->url->param('section'); 4355 4356 // Disable the navigation from automatically finding the active node 4357 navigation_node::$autofindactive = false; 4358 $referencebranch = $this->add(get_string('administrationsite'), '/admin/search.php', self::TYPE_SITE_ADMIN, null, 'root'); 4359 foreach ($adminroot->children as $adminbranch) { 4360 $this->load_administration_settings($referencebranch, $adminbranch); 4361 } 4362 navigation_node::$autofindactive = true; 4363 4364 // Use the admin structure to locate the active page 4365 if (!$this->contains_active_node() && $current = $adminroot->locate($this->adminsection, true)) { 4366 $currentnode = $this; 4367 while (($pathkey = array_pop($current->path))!==null && $currentnode) { 4368 $currentnode = $currentnode->get($pathkey); 4369 } 4370 if ($currentnode) { 4371 $currentnode->make_active(); 4372 } 4373 } else { 4374 $this->scan_for_active_node($referencebranch); 4375 } 4376 return $referencebranch; 4377 } else if ($adminbranch->check_access()) { 4378 // We have a reference branch that we can access and is not hidden `hurrah` 4379 // Now we need to display it and any children it may have 4380 $url = null; 4381 $icon = null; 4382 if ($adminbranch instanceof admin_settingpage) { 4383 $url = new moodle_url('/'.$CFG->admin.'/settings.php', array('section'=>$adminbranch->name)); 4384 } else if ($adminbranch instanceof admin_externalpage) { 4385 $url = $adminbranch->url; 4386 } else if (!empty($CFG->linkadmincategories) && $adminbranch instanceof admin_category) { 4387 $url = new moodle_url('/'.$CFG->admin.'/category.php', array('category' => $adminbranch->name)); 4388 } 4389 4390 // Add the branch 4391 $reference = $referencebranch->add($adminbranch->visiblename, $url, self::TYPE_SETTING, null, $adminbranch->name, $icon); 4392 4393 if ($adminbranch->is_hidden()) { 4394 if (($adminbranch instanceof admin_externalpage || $adminbranch instanceof admin_settingpage) && $adminbranch->name == $this->adminsection) { 4395 $reference->add_class('hidden'); 4396 } else { 4397 $reference->display = false; 4398 } 4399 } 4400 4401 // Check if we are generating the admin notifications and whether notificiations exist 4402 if ($adminbranch->name === 'adminnotifications' && admin_critical_warnings_present()) { 4403 $reference->add_class('criticalnotification'); 4404 } 4405 // Check if this branch has children 4406 if ($reference && isset($adminbranch->children) && is_array($adminbranch->children) && count($adminbranch->children)>0) { 4407 foreach ($adminbranch->children as $branch) { 4408 // Generate the child branches as well now using this branch as the reference 4409 $this->load_administration_settings($reference, $branch); 4410 } 4411 } else { 4412 $reference->icon = new pix_icon('i/settings', ''); 4413 } 4414 } 4415 } 4416 4417 /** 4418 * This function recursivily scans nodes until it finds the active node or there 4419 * are no more nodes. 4420 * @param navigation_node $node 4421 */ 4422 protected function scan_for_active_node(navigation_node $node) { 4423 if (!$node->check_if_active() && $node->children->count()>0) { 4424 foreach ($node->children as &$child) { 4425 $this->scan_for_active_node($child); 4426 } 4427 } 4428 } 4429 4430 /** 4431 * Gets a navigation node given an array of keys that represent the path to 4432 * the desired node. 4433 * 4434 * @param array $path 4435 * @return navigation_node|false 4436 */ 4437 protected function get_by_path(array $path) { 4438 $node = $this->get(array_shift($path)); 4439 foreach ($path as $key) { 4440 $node->get($key); 4441 } 4442 return $node; 4443 } 4444 4445 /** 4446 * This function loads the course settings that are available for the user 4447 * 4448 * @param bool $forceopen If set to true the course node will be forced open 4449 * @return navigation_node|false 4450 */ 4451 protected function load_course_settings($forceopen = false) { 4452 global $CFG; 4453 require_once($CFG->dirroot . '/course/lib.php'); 4454 4455 $course = $this->page->course; 4456 $coursecontext = context_course::instance($course->id); 4457 $adminoptions = course_get_user_administration_options($course, $coursecontext); 4458 4459 // note: do not test if enrolled or viewing here because we need the enrol link in Course administration section 4460 4461 $coursenode = $this->add(get_string('courseadministration'), null, self::TYPE_COURSE, null, 'courseadmin'); 4462 if ($forceopen) { 4463 $coursenode->force_open(); 4464 } 4465 4466 4467 if ($adminoptions->update) { 4468 // Add the course settings link 4469 $url = new moodle_url('/course/edit.php', array('id'=>$course->id)); 4470 $coursenode->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, 'editsettings', new pix_icon('i/settings', '')); 4471 } 4472 4473 if ($adminoptions->editcompletion) { 4474 // Add the course completion settings link 4475 $url = new moodle_url('/course/completion.php', array('id' => $course->id)); 4476 $coursenode->add(get_string('coursecompletion', 'completion'), $url, self::TYPE_SETTING, null, null, 4477 new pix_icon('i/settings', '')); 4478 } 4479 4480 if (!$adminoptions->update && $adminoptions->tags) { 4481 $url = new moodle_url('/course/tags.php', array('id' => $course->id)); 4482 $coursenode->add(get_string('coursetags', 'tag'), $url, self::TYPE_SETTING, null, 'coursetags', new pix_icon('i/settings', '')); 4483 } 4484 4485 // add enrol nodes 4486 enrol_add_course_navigation($coursenode, $course); 4487 4488 // Manage filters 4489 if ($adminoptions->filters) { 4490 $url = new moodle_url('/filter/manage.php', array('contextid'=>$coursecontext->id)); 4491 $coursenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/filter', '')); 4492 } 4493 4494 // View course reports. 4495 if ($adminoptions->reports) { 4496 $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, 'coursereports', 4497 new pix_icon('i/stats', '')); 4498 $coursereports = core_component::get_plugin_list('coursereport'); 4499 foreach ($coursereports as $report => $dir) { 4500 $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; 4501 if (file_exists($libfile)) { 4502 require_once($libfile); 4503 $reportfunction = $report.'_report_extend_navigation'; 4504 if (function_exists($report.'_report_extend_navigation')) { 4505 $reportfunction($reportnav, $course, $coursecontext); 4506 } 4507 } 4508 } 4509 4510 $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); 4511 foreach ($reports as $reportfunction) { 4512 $reportfunction($reportnav, $course, $coursecontext); 4513 } 4514 } 4515 4516 // Check if we can view the gradebook's setup page. 4517 if ($adminoptions->gradebook) { 4518 $url = new moodle_url('/grade/edit/tree/index.php', array('id' => $course->id)); 4519 $coursenode->add(get_string('gradebooksetup', 'grades'), $url, self::TYPE_SETTING, 4520 null, 'gradebooksetup', new pix_icon('i/settings', '')); 4521 } 4522 4523 // Add the context locking node. 4524 $this->add_context_locking_node($coursenode, $coursecontext); 4525 4526 // Add outcome if permitted 4527 if ($adminoptions->outcomes) { 4528 $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$course->id)); 4529 $coursenode->add(get_string('outcomes', 'grades'), $url, self::TYPE_SETTING, null, 'outcomes', new pix_icon('i/outcomes', '')); 4530 } 4531 4532 //Add badges navigation 4533 if ($adminoptions->badges) { 4534 require_once($CFG->libdir .'/badgeslib.php'); 4535 badges_add_course_navigation($coursenode, $course); 4536 } 4537 4538 // Backup this course 4539 if ($adminoptions->backup) { 4540 $url = new moodle_url('/backup/backup.php', array('id'=>$course->id)); 4541 $coursenode->add(get_string('backup'), $url, self::TYPE_SETTING, null, 'backup', new pix_icon('i/backup', '')); 4542 } 4543 4544 // Restore to this course 4545 if ($adminoptions->restore) { 4546 $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id)); 4547 $coursenode->add(get_string('restore'), $url, self::TYPE_SETTING, null, 'restore', new pix_icon('i/restore', '')); 4548 } 4549 4550 // Import data from other courses 4551 if ($adminoptions->import) { 4552 $url = new moodle_url('/backup/import.php', array('id'=>$course->id)); 4553 $coursenode->add(get_string('import'), $url, self::TYPE_SETTING, null, 'import', new pix_icon('i/import', '')); 4554 } 4555 4556 // Copy this course. 4557 if ($adminoptions->copy) { 4558 $url = new moodle_url('/backup/copy.php', array('id' => $course->id)); 4559 $coursenode->add(get_string('copycourse'), $url, self::TYPE_SETTING, null, 'copy', new pix_icon('t/copy', '')); 4560 } 4561 4562 // Reset this course 4563 if ($adminoptions->reset) { 4564 $url = new moodle_url('/course/reset.php', array('id'=>$course->id)); 4565 $coursenode->add(get_string('reset'), $url, self::TYPE_SETTING, null, 'reset', new pix_icon('i/return', '')); 4566 } 4567 4568 // Questions 4569 require_once($CFG->libdir . '/questionlib.php'); 4570 question_extend_settings_navigation($coursenode, $coursecontext)->trim_if_empty(); 4571 4572 if ($adminoptions->update) { 4573 // Repository Instances 4574 if (!$this->cache->cached('contexthasrepos'.$coursecontext->id)) { 4575 require_once($CFG->dirroot . '/repository/lib.php'); 4576 $editabletypes = repository::get_editable_types($coursecontext); 4577 $haseditabletypes = !empty($editabletypes); 4578 unset($editabletypes); 4579 $this->cache->set('contexthasrepos'.$coursecontext->id, $haseditabletypes); 4580 } else { 4581 $haseditabletypes = $this->cache->{'contexthasrepos'.$coursecontext->id}; 4582 } 4583 if ($haseditabletypes) { 4584 $url = new moodle_url('/repository/manage_instances.php', array('contextid' => $coursecontext->id)); 4585 $coursenode->add(get_string('repositories'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/repository', '')); 4586 } 4587 } 4588 4589 // Manage files 4590 if ($adminoptions->files) { 4591 // hidden in new courses and courses where legacy files were turned off 4592 $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id)); 4593 $coursenode->add(get_string('courselegacyfiles'), $url, self::TYPE_SETTING, null, 'coursefiles', new pix_icon('i/folder', '')); 4594 4595 } 4596 4597 // Let plugins hook into course navigation. 4598 $pluginsfunction = get_plugins_with_function('extend_navigation_course', 'lib.php'); 4599 foreach ($pluginsfunction as $plugintype => $plugins) { 4600 // Ignore the report plugin as it was already loaded above. 4601 if ($plugintype == 'report') { 4602 continue; 4603 } 4604 foreach ($plugins as $pluginfunction) { 4605 $pluginfunction($coursenode, $course, $coursecontext); 4606 } 4607 } 4608 4609 // Return we are done 4610 return $coursenode; 4611 } 4612 4613 /** 4614 * This function calls the module function to inject module settings into the 4615 * settings navigation tree. 4616 * 4617 * This only gets called if there is a corrosponding function in the modules 4618 * lib file. 4619 * 4620 * For examples mod/forum/lib.php {@link forum_extend_settings_navigation()} 4621 * 4622 * @return navigation_node|false 4623 */ 4624 protected function load_module_settings() { 4625 global $CFG; 4626 4627 if (!$this->page->cm && $this->context->contextlevel == CONTEXT_MODULE && $this->context->instanceid) { 4628 $cm = get_coursemodule_from_id(false, $this->context->instanceid, 0, false, MUST_EXIST); 4629 $this->page->set_cm($cm, $this->page->course); 4630 } 4631 4632 $file = $CFG->dirroot.'/mod/'.$this->page->activityname.'/lib.php'; 4633 if (file_exists($file)) { 4634 require_once($file); 4635 } 4636 4637 $modulenode = $this->add(get_string('pluginadministration', $this->page->activityname), null, self::TYPE_SETTING, null, 'modulesettings'); 4638 $modulenode->nodetype = navigation_node::NODETYPE_BRANCH; 4639 $modulenode->force_open(); 4640 4641 // Settings for the module 4642 if (has_capability('moodle/course:manageactivities', $this->page->cm->context)) { 4643 $url = new moodle_url('/course/modedit.php', array('update' => $this->page->cm->id, 'return' => 1)); 4644 $modulenode->add(get_string('editsettings'), $url, navigation_node::TYPE_SETTING, null, 'modedit'); 4645 } 4646 // Assign local roles 4647 if (count(get_assignable_roles($this->page->cm->context))>0) { 4648 $url = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid'=>$this->page->cm->context->id)); 4649 $modulenode->add(get_string('localroles', 'role'), $url, self::TYPE_SETTING, null, 'roleassign'); 4650 } 4651 // Override roles 4652 if (has_capability('moodle/role:review', $this->page->cm->context) or count(get_overridable_roles($this->page->cm->context))>0) { 4653 $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid'=>$this->page->cm->context->id)); 4654 $modulenode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING, null, 'roleoverride'); 4655 } 4656 // Check role permissions 4657 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $this->page->cm->context)) { 4658 $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid'=>$this->page->cm->context->id)); 4659 $modulenode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING, null, 'rolecheck'); 4660 } 4661 4662 // Add the context locking node. 4663 $this->add_context_locking_node($modulenode, $this->page->cm->context); 4664 4665 // Manage filters 4666 if (has_capability('moodle/filter:manage', $this->page->cm->context) && count(filter_get_available_in_context($this->page->cm->context))>0) { 4667 $url = new moodle_url('/filter/manage.php', array('contextid'=>$this->page->cm->context->id)); 4668 $modulenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, 'filtermanage'); 4669 } 4670 // Add reports 4671 $reports = get_plugin_list_with_function('report', 'extend_navigation_module', 'lib.php'); 4672 foreach ($reports as $reportfunction) { 4673 $reportfunction($modulenode, $this->page->cm); 4674 } 4675 // Add a backup link 4676 $featuresfunc = $this->page->activityname.'_supports'; 4677 if (function_exists($featuresfunc) && $featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/backup:backupactivity', $this->page->cm->context)) { 4678 $url = new moodle_url('/backup/backup.php', array('id'=>$this->page->cm->course, 'cm'=>$this->page->cm->id)); 4679 $modulenode->add(get_string('backup'), $url, self::TYPE_SETTING, null, 'backup'); 4680 } 4681 4682 // Restore this activity 4683 $featuresfunc = $this->page->activityname.'_supports'; 4684 if (function_exists($featuresfunc) && $featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/restore:restoreactivity', $this->page->cm->context)) { 4685 $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$this->page->cm->context->id)); 4686 $modulenode->add(get_string('restore'), $url, self::TYPE_SETTING, null, 'restore'); 4687 } 4688 4689 // Allow the active advanced grading method plugin to append its settings 4690 $featuresfunc = $this->page->activityname.'_supports'; 4691 if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING) && has_capability('moodle/grade:managegradingforms', $this->page->cm->context)) { 4692 require_once($CFG->dirroot.'/grade/grading/lib.php'); 4693 $gradingman = get_grading_manager($this->page->cm->context, 'mod_'.$this->page->activityname); 4694 $gradingman->extend_settings_navigation($this, $modulenode); 4695 } 4696 4697 $function = $this->page->activityname.'_extend_settings_navigation'; 4698 if (function_exists($function)) { 4699 $function($this, $modulenode); 4700 } 4701 4702 // Remove the module node if there are no children. 4703 if ($modulenode->children->count() <= 0) { 4704 $modulenode->remove(); 4705 } 4706 4707 return $modulenode; 4708 } 4709 4710 /** 4711 * Loads the user settings block of the settings nav 4712 * 4713 * This function is simply works out the userid and whether we need to load 4714 * just the current users profile settings, or the current user and the user the 4715 * current user is viewing. 4716 * 4717 * This function has some very ugly code to work out the user, if anyone has 4718 * any bright ideas please feel free to intervene. 4719 * 4720 * @param int $courseid The course id of the current course 4721 * @return navigation_node|false 4722 */ 4723 protected function load_user_settings($courseid = SITEID) { 4724 global $USER, $CFG; 4725 4726 if (isguestuser() || !isloggedin()) { 4727 return false; 4728 } 4729 4730 $navusers = $this->page->navigation->get_extending_users(); 4731 4732 if (count($this->userstoextendfor) > 0 || count($navusers) > 0) { 4733 $usernode = null; 4734 foreach ($this->userstoextendfor as $userid) { 4735 if ($userid == $USER->id) { 4736 continue; 4737 } 4738 $node = $this->generate_user_settings($courseid, $userid, 'userviewingsettings'); 4739 if (is_null($usernode)) { 4740 $usernode = $node; 4741 } 4742 } 4743 foreach ($navusers as $user) { 4744 if ($user->id == $USER->id) { 4745 continue; 4746 } 4747 $node = $this->generate_user_settings($courseid, $user->id, 'userviewingsettings'); 4748 if (is_null($usernode)) { 4749 $usernode = $node; 4750 } 4751 } 4752 $this->generate_user_settings($courseid, $USER->id); 4753 } else { 4754 $usernode = $this->generate_user_settings($courseid, $USER->id); 4755 } 4756 return $usernode; 4757 } 4758 4759 /** 4760 * Extends the settings navigation for the given user. 4761 * 4762 * Note: This method gets called automatically if you call 4763 * $PAGE->navigation->extend_for_user($userid) 4764 * 4765 * @param int $userid 4766 */ 4767 public function extend_for_user($userid) { 4768 global $CFG; 4769 4770 if (!in_array($userid, $this->userstoextendfor)) { 4771 $this->userstoextendfor[] = $userid; 4772 if ($this->initialised) { 4773 $this->generate_user_settings($this->page->course->id, $userid, 'userviewingsettings'); 4774 $children = array(); 4775 foreach ($this->children as $child) { 4776 $children[] = $child; 4777 } 4778 array_unshift($children, array_pop($children)); 4779 $this->children = new navigation_node_collection(); 4780 foreach ($children as $child) { 4781 $this->children->add($child); 4782 } 4783 } 4784 } 4785 } 4786 4787 /** 4788 * This function gets called by {@link settings_navigation::load_user_settings()} and actually works out 4789 * what can be shown/done 4790 * 4791 * @param int $courseid The current course' id 4792 * @param int $userid The user id to load for 4793 * @param string $gstitle The string to pass to get_string for the branch title 4794 * @return navigation_node|false 4795 */ 4796 protected function generate_user_settings($courseid, $userid, $gstitle='usercurrentsettings') { 4797 global $DB, $CFG, $USER, $SITE; 4798 4799 if ($courseid != $SITE->id) { 4800 if (!empty($this->page->course->id) && $this->page->course->id == $courseid) { 4801 $course = $this->page->course; 4802 } else { 4803 $select = context_helper::get_preload_record_columns_sql('ctx'); 4804 $sql = "SELECT c.*, $select 4805 FROM {course} c 4806 JOIN {context} ctx ON c.id = ctx.instanceid 4807 WHERE c.id = :courseid AND ctx.contextlevel = :contextlevel"; 4808 $params = array('courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE); 4809 $course = $DB->get_record_sql($sql, $params, MUST_EXIST); 4810 context_helper::preload_from_record($course); 4811 } 4812 } else { 4813 $course = $SITE; 4814 } 4815 4816 $coursecontext = context_course::instance($course->id); // Course context 4817 $systemcontext = context_system::instance(); 4818 $currentuser = ($USER->id == $userid); 4819 4820 if ($currentuser) { 4821 $user = $USER; 4822 $usercontext = context_user::instance($user->id); // User context 4823 } else { 4824 $select = context_helper::get_preload_record_columns_sql('ctx'); 4825 $sql = "SELECT u.*, $select 4826 FROM {user} u 4827 JOIN {context} ctx ON u.id = ctx.instanceid 4828 WHERE u.id = :userid AND ctx.contextlevel = :contextlevel"; 4829 $params = array('userid' => $userid, 'contextlevel' => CONTEXT_USER); 4830 $user = $DB->get_record_sql($sql, $params, IGNORE_MISSING); 4831 if (!$user) { 4832 return false; 4833 } 4834 context_helper::preload_from_record($user); 4835 4836 // Check that the user can view the profile 4837 $usercontext = context_user::instance($user->id); // User context 4838 $canviewuser = has_capability('moodle/user:viewdetails', $usercontext); 4839 4840 if ($course->id == $SITE->id) { 4841 if ($CFG->forceloginforprofiles && !has_coursecontact_role($user->id) && !$canviewuser) { // Reduce possibility of "browsing" userbase at site level 4842 // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366) 4843 return false; 4844 } 4845 } else { 4846 $canviewusercourse = has_capability('moodle/user:viewdetails', $coursecontext); 4847 $userisenrolled = is_enrolled($coursecontext, $user->id, '', true); 4848 if ((!$canviewusercourse && !$canviewuser) || !$userisenrolled) { 4849 return false; 4850 } 4851 $canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); 4852 if (!$canaccessallgroups && groups_get_course_groupmode($course) == SEPARATEGROUPS && !$canviewuser) { 4853 // If groups are in use, make sure we can see that group (MDL-45874). That does not apply to parents. 4854 if ($courseid == $this->page->course->id) { 4855 $mygroups = get_fast_modinfo($this->page->course)->groups; 4856 } else { 4857 $mygroups = groups_get_user_groups($courseid); 4858 } 4859 $usergroups = groups_get_user_groups($courseid, $userid); 4860 if (!array_intersect_key($mygroups[0], $usergroups[0])) { 4861 return false; 4862 } 4863 } 4864 } 4865 } 4866 4867 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->page->context)); 4868 4869 $key = $gstitle; 4870 $prefurl = new moodle_url('/user/preferences.php'); 4871 if ($gstitle != 'usercurrentsettings') { 4872 $key .= $userid; 4873 $prefurl->param('userid', $userid); 4874 } 4875 4876 // Add a user setting branch. 4877 if ($gstitle == 'usercurrentsettings') { 4878 $dashboard = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_CONTAINER, null, 'dashboard'); 4879 // This should be set to false as we don't want to show this to the user. It's only for generating the correct 4880 // breadcrumb. 4881 $dashboard->display = false; 4882 if (get_home_page() == HOMEPAGE_MY) { 4883 $dashboard->mainnavonly = true; 4884 } 4885 4886 $iscurrentuser = ($user->id == $USER->id); 4887 4888 $baseargs = array('id' => $user->id); 4889 if ($course->id != $SITE->id && !$iscurrentuser) { 4890 $baseargs['course'] = $course->id; 4891 $issitecourse = false; 4892 } else { 4893 // Load all categories and get the context for the system. 4894 $issitecourse = true; 4895 } 4896 4897 // Add the user profile to the dashboard. 4898 $profilenode = $dashboard->add(get_string('profile'), new moodle_url('/user/profile.php', 4899 array('id' => $user->id)), self::TYPE_SETTING, null, 'myprofile'); 4900 4901 if (!empty($CFG->navadduserpostslinks)) { 4902 // Add nodes for forum posts and discussions if the user can view either or both 4903 // There are no capability checks here as the content of the page is based 4904 // purely on the forums the current user has access too. 4905 $forumtab = $profilenode->add(get_string('forumposts', 'forum')); 4906 $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs), null, 'myposts'); 4907 $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', 4908 array_merge($baseargs, array('mode' => 'discussions'))), null, 'mydiscussions'); 4909 } 4910 4911 // Add blog nodes. 4912 if (!empty($CFG->enableblogs)) { 4913 if (!$this->cache->cached('userblogoptions'.$user->id)) { 4914 require_once($CFG->dirroot.'/blog/lib.php'); 4915 // Get all options for the user. 4916 $options = blog_get_options_for_user($user); 4917 $this->cache->set('userblogoptions'.$user->id, $options); 4918 } else { 4919 $options = $this->cache->{'userblogoptions'.$user->id}; 4920 } 4921 4922 if (count($options) > 0) { 4923 $blogs = $profilenode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); 4924 foreach ($options as $type => $option) { 4925 if ($type == "rss") { 4926 $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, null, 4927 new pix_icon('i/rss', '')); 4928 } else { 4929 $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, 'blog' . $type); 4930 } 4931 } 4932 } 4933 } 4934 4935 // Add the messages link. 4936 // It is context based so can appear in the user's profile and in course participants information. 4937 if (!empty($CFG->messaging)) { 4938 $messageargs = array('user1' => $USER->id); 4939 if ($USER->id != $user->id) { 4940 $messageargs['user2'] = $user->id; 4941 } 4942 $url = new moodle_url('/message/index.php', $messageargs); 4943 $dashboard->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); 4944 } 4945 4946 // Add the "My private files" link. 4947 // This link doesn't have a unique display for course context so only display it under the user's profile. 4948 if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { 4949 $url = new moodle_url('/user/files.php'); 4950 $dashboard->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles'); 4951 } 4952 4953 // Add a node to view the users notes if permitted. 4954 if (!empty($CFG->enablenotes) && 4955 has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { 4956 $url = new moodle_url('/notes/index.php', array('user' => $user->id)); 4957 if ($coursecontext->instanceid != SITEID) { 4958 $url->param('course', $coursecontext->instanceid); 4959 } 4960 $profilenode->add(get_string('notes', 'notes'), $url); 4961 } 4962 4963 // Show the grades node. 4964 if (($issitecourse && $iscurrentuser) || has_capability('moodle/user:viewdetails', $usercontext)) { 4965 require_once($CFG->dirroot . '/user/lib.php'); 4966 // Set the grades node to link to the "Grades" page. 4967 if ($course->id == SITEID) { 4968 $url = user_mygrades_url($user->id, $course->id); 4969 } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version). 4970 $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id)); 4971 } 4972 $dashboard->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'mygrades'); 4973 } 4974 4975 // Let plugins hook into user navigation. 4976 $pluginsfunction = get_plugins_with_function('extend_navigation_user', 'lib.php'); 4977 foreach ($pluginsfunction as $plugintype => $plugins) { 4978 if ($plugintype != 'report') { 4979 foreach ($plugins as $pluginfunction) { 4980 $pluginfunction($profilenode, $user, $usercontext, $course, $coursecontext); 4981 } 4982 } 4983 } 4984 4985 $usersetting = navigation_node::create(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); 4986 $dashboard->add_node($usersetting); 4987 } else { 4988 $usersetting = $this->add(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); 4989 $usersetting->display = false; 4990 } 4991 $usersetting->id = 'usersettings'; 4992 4993 // Check if the user has been deleted. 4994 if ($user->deleted) { 4995 if (!has_capability('moodle/user:update', $coursecontext)) { 4996 // We can't edit the user so just show the user deleted message. 4997 $usersetting->add(get_string('userdeleted'), null, self::TYPE_SETTING); 4998 } else { 4999 // We can edit the user so show the user deleted message and link it to the profile. 5000 if ($course->id == $SITE->id) { 5001 $profileurl = new moodle_url('/user/profile.php', array('id'=>$user->id)); 5002 } else { 5003 $profileurl = new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$course->id)); 5004 } 5005 $usersetting->add(get_string('userdeleted'), $profileurl, self::TYPE_SETTING); 5006 } 5007 return true; 5008 } 5009 5010 $userauthplugin = false; 5011 if (!empty($user->auth)) { 5012 $userauthplugin = get_auth_plugin($user->auth); 5013 } 5014 5015 $useraccount = $usersetting->add(get_string('useraccount'), null, self::TYPE_CONTAINER, null, 'useraccount'); 5016 5017 // Add the profile edit link. 5018 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { 5019 if (($currentuser || is_siteadmin($USER) || !is_siteadmin($user)) && 5020 has_capability('moodle/user:update', $systemcontext)) { 5021 $url = new moodle_url('/user/editadvanced.php', array('id'=>$user->id, 'course'=>$course->id)); 5022 $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING, null, 'editprofile'); 5023 } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user)) || 5024 ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext))) { 5025 if ($userauthplugin && $userauthplugin->can_edit_profile()) { 5026 $url = $userauthplugin->edit_profile_url(); 5027 if (empty($url)) { 5028 $url = new moodle_url('/user/edit.php', array('id'=>$user->id, 'course'=>$course->id)); 5029 } 5030 $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING, null, 'editprofile'); 5031 } 5032 } 5033 } 5034 5035 // Change password link. 5036 if ($userauthplugin && $currentuser && !\core\session\manager::is_loggedinas() && !isguestuser() && 5037 has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) { 5038 $passwordchangeurl = $userauthplugin->change_password_url(); 5039 if (empty($passwordchangeurl)) { 5040 $passwordchangeurl = new moodle_url('/login/change_password.php', array('id'=>$course->id)); 5041 } 5042 $useraccount->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING, null, 'changepassword'); 5043 } 5044 5045 // Default homepage. 5046 $defaulthomepageuser = (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)); 5047 if (isloggedin() && !isguestuser($user) && $defaulthomepageuser) { 5048 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5049 has_capability('moodle/user:editprofile', $usercontext)) { 5050 $url = new moodle_url('/user/defaulthomepage.php', ['id' => $user->id]); 5051 $useraccount->add(get_string('defaulthomepageuser'), $url, self::TYPE_SETTING, null, 'defaulthomepageuser'); 5052 } 5053 } 5054 5055 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { 5056 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5057 has_capability('moodle/user:editprofile', $usercontext)) { 5058 $url = new moodle_url('/user/language.php', array('id' => $user->id, 'course' => $course->id)); 5059 $useraccount->add(get_string('preferredlanguage'), $url, self::TYPE_SETTING, null, 'preferredlanguage'); 5060 } 5061 } 5062 $pluginmanager = core_plugin_manager::instance(); 5063 $enabled = $pluginmanager->get_enabled_plugins('mod'); 5064 if (isset($enabled['forum']) && isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { 5065 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5066 has_capability('moodle/user:editprofile', $usercontext)) { 5067 $url = new moodle_url('/user/forum.php', array('id' => $user->id, 'course' => $course->id)); 5068 $useraccount->add(get_string('forumpreferences'), $url, self::TYPE_SETTING); 5069 } 5070 } 5071 $editors = editors_get_enabled(); 5072 if (count($editors) > 1) { 5073 if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { 5074 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5075 has_capability('moodle/user:editprofile', $usercontext)) { 5076 $url = new moodle_url('/user/editor.php', array('id' => $user->id, 'course' => $course->id)); 5077 $useraccount->add(get_string('editorpreferences'), $url, self::TYPE_SETTING); 5078 } 5079 } 5080 } 5081 5082 // Add "Course preferences" link. 5083 if (isloggedin() && !isguestuser($user)) { 5084 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5085 has_capability('moodle/user:editprofile', $usercontext)) { 5086 $url = new moodle_url('/user/course.php', array('id' => $user->id, 'course' => $course->id)); 5087 $useraccount->add(get_string('coursepreferences'), $url, self::TYPE_SETTING, null, 'coursepreferences'); 5088 } 5089 } 5090 5091 // Add "Calendar preferences" link. 5092 if (isloggedin() && !isguestuser($user)) { 5093 if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || 5094 has_capability('moodle/user:editprofile', $usercontext)) { 5095 $url = new moodle_url('/user/calendar.php', array('id' => $user->id)); 5096 $useraccount->add(get_string('calendarpreferences', 'calendar'), $url, self::TYPE_SETTING, null, 'preferredcalendar'); 5097 } 5098 } 5099 5100 // View the roles settings. 5101 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 5102 'moodle/role:manage'), $usercontext)) { 5103 $roles = $usersetting->add(get_string('roles'), null, self::TYPE_SETTING); 5104 5105 $url = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$user->id, 'courseid'=>$course->id)); 5106 $roles->add(get_string('thisusersroles', 'role'), $url, self::TYPE_SETTING); 5107 5108 $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH); 5109 5110 if (!empty($assignableroles)) { 5111 $url = new moodle_url('/admin/roles/assign.php', 5112 array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); 5113 $roles->add(get_string('assignrolesrelativetothisuser', 'role'), $url, self::TYPE_SETTING); 5114 } 5115 5116 if (has_capability('moodle/role:review', $usercontext) || count(get_overridable_roles($usercontext, ROLENAME_BOTH))>0) { 5117 $url = new moodle_url('/admin/roles/permissions.php', 5118 array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); 5119 $roles->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); 5120 } 5121 5122 $url = new moodle_url('/admin/roles/check.php', 5123 array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); 5124 $roles->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); 5125 } 5126 5127 // Repositories. 5128 if (!$this->cache->cached('contexthasrepos'.$usercontext->id)) { 5129 require_once($CFG->dirroot . '/repository/lib.php'); 5130 $editabletypes = repository::get_editable_types($usercontext); 5131 $haseditabletypes = !empty($editabletypes); 5132 unset($editabletypes); 5133 $this->cache->set('contexthasrepos'.$usercontext->id, $haseditabletypes); 5134 } else { 5135 $haseditabletypes = $this->cache->{'contexthasrepos'.$usercontext->id}; 5136 } 5137 if ($haseditabletypes) { 5138 $repositories = $usersetting->add(get_string('repositories', 'repository'), null, self::TYPE_SETTING); 5139 $repositories->add(get_string('manageinstances', 'repository'), new moodle_url('/repository/manage_instances.php', 5140 array('contextid' => $usercontext->id))); 5141 } 5142 5143 // Portfolio. 5144 if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) { 5145 require_once($CFG->libdir . '/portfoliolib.php'); 5146 if (portfolio_has_visible_instances()) { 5147 $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING); 5148 5149 $url = new moodle_url('/user/portfolio.php', array('courseid'=>$course->id)); 5150 $portfolio->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING); 5151 5152 $url = new moodle_url('/user/portfoliologs.php', array('courseid'=>$course->id)); 5153 $portfolio->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING); 5154 } 5155 } 5156 5157 $enablemanagetokens = false; 5158 if (!empty($CFG->enablerssfeeds)) { 5159 $enablemanagetokens = true; 5160 } else if (!is_siteadmin($USER->id) 5161 && !empty($CFG->enablewebservices) 5162 && has_capability('moodle/webservice:createtoken', context_system::instance()) ) { 5163 $enablemanagetokens = true; 5164 } 5165 // Security keys. 5166 if ($currentuser && $enablemanagetokens) { 5167 $url = new moodle_url('/user/managetoken.php', array('sesskey'=>sesskey())); 5168 $useraccount->add(get_string('securitykeys', 'webservice'), $url, self::TYPE_SETTING); 5169 } 5170 5171 // Messaging. 5172 if (($currentuser && has_capability('moodle/user:editownmessageprofile', $systemcontext)) || (!isguestuser($user) && 5173 has_capability('moodle/user:editmessageprofile', $usercontext) && !is_primary_admin($user->id))) { 5174 $messagingurl = new moodle_url('/message/edit.php', array('id' => $user->id)); 5175 $notificationsurl = new moodle_url('/message/notificationpreferences.php', array('userid' => $user->id)); 5176 $useraccount->add(get_string('messagepreferences', 'message'), $messagingurl, self::TYPE_SETTING); 5177 $useraccount->add(get_string('notificationpreferences', 'message'), $notificationsurl, self::TYPE_SETTING); 5178 } 5179 5180 // Blogs. 5181 if ($currentuser && !empty($CFG->enableblogs)) { 5182 $blog = $usersetting->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER, null, 'blogs'); 5183 if (has_capability('moodle/blog:view', $systemcontext)) { 5184 $blog->add(get_string('preferences', 'blog'), new moodle_url('/blog/preferences.php'), 5185 navigation_node::TYPE_SETTING); 5186 } 5187 if (!empty($CFG->useexternalblogs) && $CFG->maxexternalblogsperuser > 0 && 5188 has_capability('moodle/blog:manageexternal', $systemcontext)) { 5189 $blog->add(get_string('externalblogs', 'blog'), new moodle_url('/blog/external_blogs.php'), 5190 navigation_node::TYPE_SETTING); 5191 $blog->add(get_string('addnewexternalblog', 'blog'), new moodle_url('/blog/external_blog_edit.php'), 5192 navigation_node::TYPE_SETTING); 5193 } 5194 // Remove the blog node if empty. 5195 $blog->trim_if_empty(); 5196 } 5197 5198 // Badges. 5199 if ($currentuser && !empty($CFG->enablebadges)) { 5200 $badges = $usersetting->add(get_string('badges'), null, navigation_node::TYPE_CONTAINER, null, 'badges'); 5201 if (has_capability('moodle/badges:manageownbadges', $usercontext)) { 5202 $url = new moodle_url('/badges/mybadges.php'); 5203 $badges->add(get_string('managebadges', 'badges'), $url, self::TYPE_SETTING); 5204 } 5205 $badges->add(get_string('preferences', 'badges'), new moodle_url('/badges/preferences.php'), 5206 navigation_node::TYPE_SETTING); 5207 if (!empty($CFG->badges_allowexternalbackpack)) { 5208 $badges->add(get_string('backpackdetails', 'badges'), new moodle_url('/badges/mybackpack.php'), 5209 navigation_node::TYPE_SETTING); 5210 } 5211 } 5212 5213 // Let plugins hook into user settings navigation. 5214 $pluginsfunction = get_plugins_with_function('extend_navigation_user_settings', 'lib.php'); 5215 foreach ($pluginsfunction as $plugintype => $plugins) { 5216 foreach ($plugins as $pluginfunction) { 5217 $pluginfunction($usersetting, $user, $usercontext, $course, $coursecontext); 5218 } 5219 } 5220 5221 return $usersetting; 5222 } 5223 5224 /** 5225 * Loads block specific settings in the navigation 5226 * 5227 * @return navigation_node 5228 */ 5229 protected function load_block_settings() { 5230 global $CFG; 5231 5232 $blocknode = $this->add($this->context->get_context_name(), null, self::TYPE_SETTING, null, 'blocksettings'); 5233 $blocknode->force_open(); 5234 5235 // Assign local roles 5236 if (get_assignable_roles($this->context, ROLENAME_ORIGINAL)) { 5237 $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid' => $this->context->id)); 5238 $blocknode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING, null, 5239 'roles', new pix_icon('i/assignroles', '')); 5240 } 5241 5242 // Override roles 5243 if (has_capability('moodle/role:review', $this->context) or count(get_overridable_roles($this->context))>0) { 5244 $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid'=>$this->context->id)); 5245 $blocknode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING, null, 5246 'permissions', new pix_icon('i/permissions', '')); 5247 } 5248 // Check role permissions 5249 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $this->context)) { 5250 $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid'=>$this->context->id)); 5251 $blocknode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING, null, 5252 'checkpermissions', new pix_icon('i/checkpermissions', '')); 5253 } 5254 5255 // Add the context locking node. 5256 $this->add_context_locking_node($blocknode, $this->context); 5257 5258 return $blocknode; 5259 } 5260 5261 /** 5262 * Loads category specific settings in the navigation 5263 * 5264 * @return navigation_node 5265 */ 5266 protected function load_category_settings() { 5267 global $CFG; 5268 5269 // We can land here while being in the context of a block, in which case we 5270 // should get the parent context which should be the category one. See self::initialise(). 5271 if ($this->context->contextlevel == CONTEXT_BLOCK) { 5272 $catcontext = $this->context->get_parent_context(); 5273 } else { 5274 $catcontext = $this->context; 5275 } 5276 5277 // Let's make sure that we always have the right context when getting here. 5278 if ($catcontext->contextlevel != CONTEXT_COURSECAT) { 5279 throw new coding_exception('Unexpected context while loading category settings.'); 5280 } 5281 5282 $categorynodetype = navigation_node::TYPE_CONTAINER; 5283 $categorynode = $this->add($catcontext->get_context_name(), null, $categorynodetype, null, 'categorysettings'); 5284 $categorynode->nodetype = navigation_node::NODETYPE_BRANCH; 5285 $categorynode->force_open(); 5286 5287 if (can_edit_in_category($catcontext->instanceid)) { 5288 $url = new moodle_url('/course/management.php', array('categoryid' => $catcontext->instanceid)); 5289 $editstring = get_string('managecategorythis'); 5290 $categorynode->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', '')); 5291 } 5292 5293 if (has_capability('moodle/category:manage', $catcontext)) { 5294 $editurl = new moodle_url('/course/editcategory.php', array('id' => $catcontext->instanceid)); 5295 $categorynode->add(get_string('editcategorythis'), $editurl, self::TYPE_SETTING, null, 'edit', new pix_icon('i/edit', '')); 5296 5297 $addsubcaturl = new moodle_url('/course/editcategory.php', array('parent' => $catcontext->instanceid)); 5298 $categorynode->add(get_string('addsubcategory'), $addsubcaturl, self::TYPE_SETTING, null, 'addsubcat', new pix_icon('i/withsubcat', '')); 5299 } 5300 5301 // Assign local roles 5302 $assignableroles = get_assignable_roles($catcontext); 5303 if (!empty($assignableroles)) { 5304 $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid' => $catcontext->id)); 5305 $categorynode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING, null, 'roles', new pix_icon('i/assignroles', '')); 5306 } 5307 5308 // Override roles 5309 if (has_capability('moodle/role:review', $catcontext) or count(get_overridable_roles($catcontext)) > 0) { 5310 $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid' => $catcontext->id)); 5311 $categorynode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING, null, 'permissions', new pix_icon('i/permissions', '')); 5312 } 5313 // Check role permissions 5314 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 5315 'moodle/role:override', 'moodle/role:assign'), $catcontext)) { 5316 $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid' => $catcontext->id)); 5317 $categorynode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING, null, 'checkpermissions', new pix_icon('i/checkpermissions', '')); 5318 } 5319 5320 // Add the context locking node. 5321 $this->add_context_locking_node($categorynode, $catcontext); 5322 5323 // Cohorts 5324 if (has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $catcontext)) { 5325 $categorynode->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/index.php', 5326 array('contextid' => $catcontext->id)), self::TYPE_SETTING, null, 'cohort', new pix_icon('i/cohort', '')); 5327 } 5328 5329 // Manage filters 5330 if (has_capability('moodle/filter:manage', $catcontext) && count(filter_get_available_in_context($catcontext)) > 0) { 5331 $url = new moodle_url('/filter/manage.php', array('contextid' => $catcontext->id)); 5332 $categorynode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, 'filters', new pix_icon('i/filter', '')); 5333 } 5334 5335 // Restore. 5336 if (has_capability('moodle/restore:restorecourse', $catcontext)) { 5337 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $catcontext->id)); 5338 $categorynode->add(get_string('restorecourse', 'admin'), $url, self::TYPE_SETTING, null, 'restorecourse', new pix_icon('i/restore', '')); 5339 } 5340 5341 // Let plugins hook into category settings navigation. 5342 $pluginsfunction = get_plugins_with_function('extend_navigation_category_settings', 'lib.php'); 5343 foreach ($pluginsfunction as $plugintype => $plugins) { 5344 foreach ($plugins as $pluginfunction) { 5345 $pluginfunction($categorynode, $catcontext); 5346 } 5347 } 5348 5349 return $categorynode; 5350 } 5351 5352 /** 5353 * Determine whether the user is assuming another role 5354 * 5355 * This function checks to see if the user is assuming another role by means of 5356 * role switching. In doing this we compare each RSW key (context path) against 5357 * the current context path. This ensures that we can provide the switching 5358 * options against both the course and any page shown under the course. 5359 * 5360 * @return bool|int The role(int) if the user is in another role, false otherwise 5361 */ 5362 protected function in_alternative_role() { 5363 global $USER; 5364 if (!empty($USER->access['rsw']) && is_array($USER->access['rsw'])) { 5365 if (!empty($this->page->context) && !empty($USER->access['rsw'][$this->page->context->path])) { 5366 return $USER->access['rsw'][$this->page->context->path]; 5367 } 5368 foreach ($USER->access['rsw'] as $key=>$role) { 5369 if (strpos($this->context->path,$key)===0) { 5370 return $role; 5371 } 5372 } 5373 } 5374 return false; 5375 } 5376 5377 /** 5378 * This function loads all of the front page settings into the settings navigation. 5379 * This function is called when the user is on the front page, or $COURSE==$SITE 5380 * @param bool $forceopen (optional) 5381 * @return navigation_node 5382 */ 5383 protected function load_front_page_settings($forceopen = false) { 5384 global $SITE, $CFG; 5385 require_once($CFG->dirroot . '/course/lib.php'); 5386 5387 $course = clone($SITE); 5388 $coursecontext = context_course::instance($course->id); // Course context 5389 $adminoptions = course_get_user_administration_options($course, $coursecontext); 5390 5391 $frontpage = $this->add(get_string('frontpagesettings'), null, self::TYPE_SETTING, null, 'frontpage'); 5392 if ($forceopen) { 5393 $frontpage->force_open(); 5394 } 5395 $frontpage->id = 'frontpagesettings'; 5396 5397 if ($this->page->user_allowed_editing()) { 5398 5399 // Add the turn on/off settings 5400 $url = new moodle_url('/course/view.php', array('id'=>$course->id, 'sesskey'=>sesskey())); 5401 if ($this->page->user_is_editing()) { 5402 $url->param('edit', 'off'); 5403 $editstring = get_string('turneditingoff'); 5404 } else { 5405 $url->param('edit', 'on'); 5406 $editstring = get_string('turneditingon'); 5407 } 5408 $frontpage->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', '')); 5409 } 5410 5411 if ($adminoptions->update) { 5412 // Add the course settings link 5413 $url = new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings')); 5414 $frontpage->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/settings', '')); 5415 } 5416 5417 // add enrol nodes 5418 enrol_add_course_navigation($frontpage, $course); 5419 5420 // Manage filters 5421 if ($adminoptions->filters) { 5422 $url = new moodle_url('/filter/manage.php', array('contextid'=>$coursecontext->id)); 5423 $frontpage->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/filter', '')); 5424 } 5425 5426 // View course reports. 5427 if ($adminoptions->reports) { 5428 $frontpagenav = $frontpage->add(get_string('reports'), null, self::TYPE_CONTAINER, null, 'frontpagereports', 5429 new pix_icon('i/stats', '')); 5430 $coursereports = core_component::get_plugin_list('coursereport'); 5431 foreach ($coursereports as $report=>$dir) { 5432 $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; 5433 if (file_exists($libfile)) { 5434 require_once($libfile); 5435 $reportfunction = $report.'_report_extend_navigation'; 5436 if (function_exists($report.'_report_extend_navigation')) { 5437 $reportfunction($frontpagenav, $course, $coursecontext); 5438 } 5439 } 5440 } 5441 5442 $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); 5443 foreach ($reports as $reportfunction) { 5444 $reportfunction($frontpagenav, $course, $coursecontext); 5445 } 5446 } 5447 5448 // Backup this course 5449 if ($adminoptions->backup) { 5450 $url = new moodle_url('/backup/backup.php', array('id'=>$course->id)); 5451 $frontpage->add(get_string('backup'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/backup', '')); 5452 } 5453 5454 // Restore to this course 5455 if ($adminoptions->restore) { 5456 $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id)); 5457 $frontpage->add(get_string('restore'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/restore', '')); 5458 } 5459 5460 // Questions 5461 require_once($CFG->libdir . '/questionlib.php'); 5462 question_extend_settings_navigation($frontpage, $coursecontext)->trim_if_empty(); 5463 5464 // Manage files 5465 if ($adminoptions->files) { 5466 //hiden in new installs 5467 $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id)); 5468 $frontpage->add(get_string('sitelegacyfiles'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/folder', '')); 5469 } 5470 5471 // Let plugins hook into frontpage navigation. 5472 $pluginsfunction = get_plugins_with_function('extend_navigation_frontpage', 'lib.php'); 5473 foreach ($pluginsfunction as $plugintype => $plugins) { 5474 foreach ($plugins as $pluginfunction) { 5475 $pluginfunction($frontpage, $course, $coursecontext); 5476 } 5477 } 5478 5479 return $frontpage; 5480 } 5481 5482 /** 5483 * This function gives local plugins an opportunity to modify the settings navigation. 5484 */ 5485 protected function load_local_plugin_settings() { 5486 5487 foreach (get_plugin_list_with_function('local', 'extend_settings_navigation') as $function) { 5488 $function($this, $this->context); 5489 } 5490 } 5491 5492 /** 5493 * This function marks the cache as volatile so it is cleared during shutdown 5494 */ 5495 public function clear_cache() { 5496 $this->cache->volatile(); 5497 } 5498 5499 /** 5500 * Checks to see if there are child nodes available in the specific user's preference node. 5501 * If so, then they have the appropriate permissions view this user's preferences. 5502 * 5503 * @since Moodle 2.9.3 5504 * @param int $userid The user's ID. 5505 * @return bool True if child nodes exist to view, otherwise false. 5506 */ 5507 public function can_view_user_preferences($userid) { 5508 if (is_siteadmin()) { 5509 return true; 5510 } 5511 // See if any nodes are present in the preferences section for this user. 5512 $preferencenode = $this->find('userviewingsettings' . $userid, null); 5513 if ($preferencenode && $preferencenode->has_children()) { 5514 // Run through each child node. 5515 foreach ($preferencenode->children as $childnode) { 5516 // If the child node has children then this user has access to a link in the preferences page. 5517 if ($childnode->has_children()) { 5518 return true; 5519 } 5520 } 5521 } 5522 // No links found for the user to access on the preferences page. 5523 return false; 5524 } 5525 } 5526 5527 /** 5528 * Class used to populate site admin navigation for ajax. 5529 * 5530 * @package core 5531 * @category navigation 5532 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com> 5533 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 5534 */ 5535 class settings_navigation_ajax extends settings_navigation { 5536 /** 5537 * Constructs the navigation for use in an AJAX request 5538 * 5539 * @param moodle_page $page 5540 */ 5541 public function __construct(moodle_page &$page) { 5542 $this->page = $page; 5543 $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); 5544 $this->children = new navigation_node_collection(); 5545 $this->initialise(); 5546 } 5547 5548 /** 5549 * Initialise the site admin navigation. 5550 * 5551 * @return array An array of the expandable nodes 5552 */ 5553 public function initialise() { 5554 if ($this->initialised || during_initial_install()) { 5555 return false; 5556 } 5557 $this->context = $this->page->context; 5558 $this->load_administration_settings(); 5559 5560 // Check if local plugins is adding node to site admin. 5561 $this->load_local_plugin_settings(); 5562 5563 $this->initialised = true; 5564 } 5565 } 5566 5567 /** 5568 * Simple class used to output a navigation branch in XML 5569 * 5570 * @package core 5571 * @category navigation 5572 * @copyright 2009 Sam Hemelryk 5573 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 5574 */ 5575 class navigation_json { 5576 /** @var array An array of different node types */ 5577 protected $nodetype = array('node','branch'); 5578 /** @var array An array of node keys and types */ 5579 protected $expandable = array(); 5580 /** 5581 * Turns a branch and all of its children into XML 5582 * 5583 * @param navigation_node $branch 5584 * @return string XML string 5585 */ 5586 public function convert($branch) { 5587 $xml = $this->convert_child($branch); 5588 return $xml; 5589 } 5590 /** 5591 * Set the expandable items in the array so that we have enough information 5592 * to attach AJAX events 5593 * @param array $expandable 5594 */ 5595 public function set_expandable($expandable) { 5596 foreach ($expandable as $node) { 5597 $this->expandable[$node['key'].':'.$node['type']] = $node; 5598 } 5599 } 5600 /** 5601 * Recusively converts a child node and its children to XML for output 5602 * 5603 * @param navigation_node $child The child to convert 5604 * @param int $depth Pointlessly used to track the depth of the XML structure 5605 * @return string JSON 5606 */ 5607 protected function convert_child($child, $depth=1) { 5608 if (!$child->display) { 5609 return ''; 5610 } 5611 $attributes = array(); 5612 $attributes['id'] = $child->id; 5613 $attributes['name'] = (string)$child->text; // This can be lang_string object so typecast it. 5614 $attributes['type'] = $child->type; 5615 $attributes['key'] = $child->key; 5616 $attributes['class'] = $child->get_css_type(); 5617 $attributes['requiresajaxloading'] = $child->requiresajaxloading; 5618 5619 if ($child->icon instanceof pix_icon) { 5620 $attributes['icon'] = array( 5621 'component' => $child->icon->component, 5622 'pix' => $child->icon->pix, 5623 ); 5624 foreach ($child->icon->attributes as $key=>$value) { 5625 if ($key == 'class') { 5626 $attributes['icon']['classes'] = explode(' ', $value); 5627 } else if (!array_key_exists($key, $attributes['icon'])) { 5628 $attributes['icon'][$key] = $value; 5629 } 5630 5631 } 5632 } else if (!empty($child->icon)) { 5633 $attributes['icon'] = (string)$child->icon; 5634 } 5635 5636 if ($child->forcetitle || $child->title !== $child->text) { 5637 $attributes['title'] = htmlentities($child->title, ENT_QUOTES, 'UTF-8'); 5638 } 5639 if (array_key_exists($child->key.':'.$child->type, $this->expandable)) { 5640 $attributes['expandable'] = $child->key; 5641 $child->add_class($this->expandable[$child->key.':'.$child->type]['id']); 5642 } 5643 5644 if (count($child->classes)>0) { 5645 $attributes['class'] .= ' '.join(' ',$child->classes); 5646 } 5647 if (is_string($child->action)) { 5648 $attributes['link'] = $child->action; 5649 } else if ($child->action instanceof moodle_url) { 5650 $attributes['link'] = $child->action->out(); 5651 } else if ($child->action instanceof action_link) { 5652 $attributes['link'] = $child->action->url->out(); 5653 } 5654 $attributes['hidden'] = ($child->hidden); 5655 $attributes['haschildren'] = ($child->children->count()>0 || $child->type == navigation_node::TYPE_CATEGORY); 5656 $attributes['haschildren'] = $attributes['haschildren'] || $child->type == navigation_node::TYPE_MY_CATEGORY; 5657 5658 if ($child->children->count() > 0) { 5659 $attributes['children'] = array(); 5660 foreach ($child->children as $subchild) { 5661 $attributes['children'][] = $this->convert_child($subchild, $depth+1); 5662 } 5663 } 5664 5665 if ($depth > 1) { 5666 return $attributes; 5667 } else { 5668 return json_encode($attributes); 5669 } 5670 } 5671 } 5672 5673 /** 5674 * The cache class used by global navigation and settings navigation. 5675 * 5676 * It is basically an easy access point to session with a bit of smarts to make 5677 * sure that the information that is cached is valid still. 5678 * 5679 * Example use: 5680 * <code php> 5681 * if (!$cache->viewdiscussion()) { 5682 * // Code to do stuff and produce cachable content 5683 * $cache->viewdiscussion = has_capability('mod/forum:viewdiscussion', $coursecontext); 5684 * } 5685 * $content = $cache->viewdiscussion; 5686 * </code> 5687 * 5688 * @package core 5689 * @category navigation 5690 * @copyright 2009 Sam Hemelryk 5691 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 5692 */ 5693 class navigation_cache { 5694 /** @var int represents the time created */ 5695 protected $creation; 5696 /** @var array An array of session keys */ 5697 protected $session; 5698 /** 5699 * The string to use to segregate this particular cache. It can either be 5700 * unique to start a fresh cache or if you want to share a cache then make 5701 * it the string used in the original cache. 5702 * @var string 5703 */ 5704 protected $area; 5705 /** @var int a time that the information will time out */ 5706 protected $timeout; 5707 /** @var stdClass The current context */ 5708 protected $currentcontext; 5709 /** @var int cache time information */ 5710 const CACHETIME = 0; 5711 /** @var int cache user id */ 5712 const CACHEUSERID = 1; 5713 /** @var int cache value */ 5714 const CACHEVALUE = 2; 5715 /** @var null|array An array of navigation cache areas to expire on shutdown */ 5716 public static $volatilecaches; 5717 5718 /** 5719 * Contructor for the cache. Requires two arguments 5720 * 5721 * @param string $area The string to use to segregate this particular cache 5722 * it can either be unique to start a fresh cache or if you want 5723 * to share a cache then make it the string used in the original 5724 * cache 5725 * @param int $timeout The number of seconds to time the information out after 5726 */ 5727 public function __construct($area, $timeout=1800) { 5728 $this->creation = time(); 5729 $this->area = $area; 5730 $this->timeout = time() - $timeout; 5731 if (rand(0,100) === 0) { 5732 $this->garbage_collection(); 5733 } 5734 } 5735 5736 /** 5737 * Used to set up the cache within the SESSION. 5738 * 5739 * This is called for each access and ensure that we don't put anything into the session before 5740 * it is required. 5741 */ 5742 protected function ensure_session_cache_initialised() { 5743 global $SESSION; 5744 if (empty($this->session)) { 5745 if (!isset($SESSION->navcache)) { 5746 $SESSION->navcache = new stdClass; 5747 } 5748 if (!isset($SESSION->navcache->{$this->area})) { 5749 $SESSION->navcache->{$this->area} = array(); 5750 } 5751 $this->session = &$SESSION->navcache->{$this->area}; // pointer to array, =& is correct here 5752 } 5753 } 5754 5755 /** 5756 * Magic Method to retrieve something by simply calling using = cache->key 5757 * 5758 * @param mixed $key The identifier for the information you want out again 5759 * @return void|mixed Either void or what ever was put in 5760 */ 5761 public function __get($key) { 5762 if (!$this->cached($key)) { 5763 return; 5764 } 5765 $information = $this->session[$key][self::CACHEVALUE]; 5766 return unserialize($information); 5767 } 5768 5769 /** 5770 * Magic method that simply uses {@link set();} to store something in the cache 5771 * 5772 * @param string|int $key 5773 * @param mixed $information 5774 */ 5775 public function __set($key, $information) { 5776 $this->set($key, $information); 5777 } 5778 5779 /** 5780 * Sets some information against the cache (session) for later retrieval 5781 * 5782 * @param string|int $key 5783 * @param mixed $information 5784 */ 5785 public function set($key, $information) { 5786 global $USER; 5787 $this->ensure_session_cache_initialised(); 5788 $information = serialize($information); 5789 $this->session[$key]= array(self::CACHETIME=>time(), self::CACHEUSERID=>$USER->id, self::CACHEVALUE=>$information); 5790 } 5791 /** 5792 * Check the existence of the identifier in the cache 5793 * 5794 * @param string|int $key 5795 * @return bool 5796 */ 5797 public function cached($key) { 5798 global $USER; 5799 $this->ensure_session_cache_initialised(); 5800 if (!array_key_exists($key, $this->session) || !is_array($this->session[$key]) || $this->session[$key][self::CACHEUSERID]!=$USER->id || $this->session[$key][self::CACHETIME] < $this->timeout) { 5801 return false; 5802 } 5803 return true; 5804 } 5805 /** 5806 * Compare something to it's equivilant in the cache 5807 * 5808 * @param string $key 5809 * @param mixed $value 5810 * @param bool $serialise Whether to serialise the value before comparison 5811 * this should only be set to false if the value is already 5812 * serialised 5813 * @return bool If the value is the same false if it is not set or doesn't match 5814 */ 5815 public function compare($key, $value, $serialise = true) { 5816 if ($this->cached($key)) { 5817 if ($serialise) { 5818 $value = serialize($value); 5819 } 5820 if ($this->session[$key][self::CACHEVALUE] === $value) { 5821 return true; 5822 } 5823 } 5824 return false; 5825 } 5826 /** 5827 * Wipes the entire cache, good to force regeneration 5828 */ 5829 public function clear() { 5830 global $SESSION; 5831 unset($SESSION->navcache); 5832 $this->session = null; 5833 } 5834 /** 5835 * Checks all cache entries and removes any that have expired, good ole cleanup 5836 */ 5837 protected function garbage_collection() { 5838 if (empty($this->session)) { 5839 return true; 5840 } 5841 foreach ($this->session as $key=>$cachedinfo) { 5842 if (is_array($cachedinfo) && $cachedinfo[self::CACHETIME]<$this->timeout) { 5843 unset($this->session[$key]); 5844 } 5845 } 5846 } 5847 5848 /** 5849 * Marks the cache as being volatile (likely to change) 5850 * 5851 * Any caches marked as volatile will be destroyed at the on shutdown by 5852 * {@link navigation_node::destroy_volatile_caches()} which is registered 5853 * as a shutdown function if any caches are marked as volatile. 5854 * 5855 * @param bool $setting True to destroy the cache false not too 5856 */ 5857 public function volatile($setting = true) { 5858 if (self::$volatilecaches===null) { 5859 self::$volatilecaches = array(); 5860 core_shutdown_manager::register_function(array('navigation_cache','destroy_volatile_caches')); 5861 } 5862 5863 if ($setting) { 5864 self::$volatilecaches[$this->area] = $this->area; 5865 } else if (array_key_exists($this->area, self::$volatilecaches)) { 5866 unset(self::$volatilecaches[$this->area]); 5867 } 5868 } 5869 5870 /** 5871 * Destroys all caches marked as volatile 5872 * 5873 * This function is static and works in conjunction with the static volatilecaches 5874 * property of navigation cache. 5875 * Because this function is static it manually resets the cached areas back to an 5876 * empty array. 5877 */ 5878 public static function destroy_volatile_caches() { 5879 global $SESSION; 5880 if (is_array(self::$volatilecaches) && count(self::$volatilecaches)>0) { 5881 foreach (self::$volatilecaches as $area) { 5882 $SESSION->navcache->{$area} = array(); 5883 } 5884 } else { 5885 $SESSION->navcache = new stdClass; 5886 } 5887 } 5888 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body