Commit | Line | Data |
---|---|---|
7d2a0492 | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * This file contains classes used to manage the navigation structures in Moodle | |
20 | * and was introduced as part of the changes occuring in Moodle 2.0 | |
21 | * | |
78bfb562 PS |
22 | * @since 2.0 |
23 | * @package core | |
78bfb562 PS |
24 | * @copyright 2009 Sam Hemelryk |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7d2a0492 | 26 | */ |
27 | ||
78bfb562 PS |
28 | defined('MOODLE_INTERNAL') || die(); |
29 | ||
f5b5a822 | 30 | /** |
2561c4e9 | 31 | * The name that will be used to separate the navigation cache within SESSION |
f5b5a822 | 32 | */ |
33 | define('NAVIGATION_CACHE_NAME', 'navigation'); | |
34 | ||
7d2a0492 | 35 | /** |
36 | * This class is used to represent a node in a navigation tree | |
37 | * | |
38 | * This class is used to represent a node in a navigation tree within Moodle, | |
39 | * the tree could be one of global navigation, settings navigation, or the navbar. | |
40 | * Each node can be one of two types either a Leaf (default) or a branch. | |
41 | * When a node is first created it is created as a leaf, when/if children are added | |
42 | * the node then becomes a branch. | |
43 | * | |
31fde54f AG |
44 | * @package core |
45 | * @category navigation | |
7d2a0492 | 46 | * @copyright 2009 Sam Hemelryk |
47 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
48 | */ | |
3406acde SH |
49 | class navigation_node implements renderable { |
50 | /** @var int Used to identify this node a leaf (default) 0 */ | |
507a7a9a | 51 | const NODETYPE_LEAF = 0; |
3406acde | 52 | /** @var int Used to identify this node a branch, happens with children 1 */ |
7d2a0492 | 53 | const NODETYPE_BRANCH = 1; |
3406acde | 54 | /** @var null Unknown node type null */ |
7d2a0492 | 55 | const TYPE_UNKNOWN = null; |
3406acde SH |
56 | /** @var int System node type 0 */ |
57 | const TYPE_ROOTNODE = 0; | |
58 | /** @var int System node type 1 */ | |
59 | const TYPE_SYSTEM = 1; | |
60 | /** @var int Category node type 10 */ | |
7d2a0492 | 61 | const TYPE_CATEGORY = 10; |
3406acde | 62 | /** @var int Course node type 20 */ |
7d2a0492 | 63 | const TYPE_COURSE = 20; |
3406acde | 64 | /** @var int Course Structure node type 30 */ |
507a7a9a | 65 | const TYPE_SECTION = 30; |
3406acde | 66 | /** @var int Activity node type, e.g. Forum, Quiz 40 */ |
7d2a0492 | 67 | const TYPE_ACTIVITY = 40; |
3406acde | 68 | /** @var int Resource node type, e.g. Link to a file, or label 50 */ |
7d2a0492 | 69 | const TYPE_RESOURCE = 50; |
3406acde | 70 | /** @var int A custom node type, default when adding without specifing type 60 */ |
7d2a0492 | 71 | const TYPE_CUSTOM = 60; |
3406acde | 72 | /** @var int Setting node type, used only within settings nav 70 */ |
7d2a0492 | 73 | const TYPE_SETTING = 70; |
3406acde | 74 | /** @var int Setting node type, used only within settings nav 80 */ |
507a7a9a | 75 | const TYPE_USER = 80; |
3406acde SH |
76 | /** @var int Setting node type, used for containers of no importance 90 */ |
77 | const TYPE_CONTAINER = 90; | |
7d2a0492 | 78 | |
79 | /** @var int Parameter to aid the coder in tracking [optional] */ | |
80 | public $id = null; | |
81 | /** @var string|int The identifier for the node, used to retrieve the node */ | |
82 | public $key = null; | |
83 | /** @var string The text to use for the node */ | |
84 | public $text = null; | |
85 | /** @var string Short text to use if requested [optional] */ | |
86 | public $shorttext = null; | |
87 | /** @var string The title attribute for an action if one is defined */ | |
88 | public $title = null; | |
89 | /** @var string A string that can be used to build a help button */ | |
90 | public $helpbutton = null; | |
3406acde | 91 | /** @var moodle_url|action_link|null An action for the node (link) */ |
7d2a0492 | 92 | public $action = null; |
f9fc1461 | 93 | /** @var pix_icon The path to an icon to use for this node */ |
7d2a0492 | 94 | public $icon = null; |
95 | /** @var int See TYPE_* constants defined for this class */ | |
96 | public $type = self::TYPE_UNKNOWN; | |
97 | /** @var int See NODETYPE_* constants defined for this class */ | |
98 | public $nodetype = self::NODETYPE_LEAF; | |
99 | /** @var bool If set to true the node will be collapsed by default */ | |
100 | public $collapse = false; | |
101 | /** @var bool If set to true the node will be expanded by default */ | |
102 | public $forceopen = false; | |
3406acde | 103 | /** @var array An array of CSS classes for the node */ |
7d2a0492 | 104 | public $classes = array(); |
3406acde | 105 | /** @var navigation_node_collection An array of child nodes */ |
7d2a0492 | 106 | public $children = array(); |
107 | /** @var bool If set to true the node will be recognised as active */ | |
108 | public $isactive = false; | |
3406acde | 109 | /** @var bool If set to true the node will be dimmed */ |
7d2a0492 | 110 | public $hidden = false; |
111 | /** @var bool If set to false the node will not be displayed */ | |
112 | public $display = true; | |
113 | /** @var bool If set to true then an HR will be printed before the node */ | |
114 | public $preceedwithhr = false; | |
115 | /** @var bool If set to true the the navigation bar should ignore this node */ | |
116 | public $mainnavonly = false; | |
117 | /** @var bool If set to true a title will be added to the action no matter what */ | |
118 | public $forcetitle = false; | |
d38a4849 | 119 | /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */ |
3406acde | 120 | public $parent = null; |
493a48f3 SH |
121 | /** @var bool Override to not display the icon even if one is provided **/ |
122 | public $hideicon = false; | |
7d2a0492 | 123 | /** @var array */ |
b14ae498 | 124 | protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user'); |
7d2a0492 | 125 | /** @var moodle_url */ |
126 | protected static $fullmeurl = null; | |
d9d2817a SH |
127 | /** @var bool toogles auto matching of active node */ |
128 | public static $autofindactive = true; | |
d67e4821 | 129 | /** @var mixed If set to an int, that section will be included even if it has no activities */ |
130 | public $includesectionnum = false; | |
7d2a0492 | 131 | |
132 | /** | |
3406acde | 133 | * Constructs a new navigation_node |
7d2a0492 | 134 | * |
3406acde SH |
135 | * @param array|string $properties Either an array of properties or a string to use |
136 | * as the text for the node | |
7d2a0492 | 137 | */ |
138 | public function __construct($properties) { | |
7d2a0492 | 139 | if (is_array($properties)) { |
3406acde SH |
140 | // Check the array for each property that we allow to set at construction. |
141 | // text - The main content for the node | |
142 | // shorttext - A short text if required for the node | |
143 | // icon - The icon to display for the node | |
144 | // type - The type of the node | |
145 | // key - The key to use to identify the node | |
146 | // parent - A reference to the nodes parent | |
147 | // action - The action to attribute to this node, usually a URL to link to | |
7d2a0492 | 148 | if (array_key_exists('text', $properties)) { |
149 | $this->text = $properties['text']; | |
150 | } | |
151 | if (array_key_exists('shorttext', $properties)) { | |
152 | $this->shorttext = $properties['shorttext']; | |
153 | } | |
7081714d SH |
154 | if (!array_key_exists('icon', $properties)) { |
155 | $properties['icon'] = new pix_icon('i/navigationitem', 'moodle'); | |
156 | } | |
157 | $this->icon = $properties['icon']; | |
158 | if ($this->icon instanceof pix_icon) { | |
159 | if (empty($this->icon->attributes['class'])) { | |
160 | $this->icon->attributes['class'] = 'navicon'; | |
161 | } else { | |
162 | $this->icon->attributes['class'] .= ' navicon'; | |
7da3a799 | 163 | } |
7d2a0492 | 164 | } |
165 | if (array_key_exists('type', $properties)) { | |
166 | $this->type = $properties['type']; | |
167 | } else { | |
168 | $this->type = self::TYPE_CUSTOM; | |
169 | } | |
170 | if (array_key_exists('key', $properties)) { | |
171 | $this->key = $properties['key']; | |
172 | } | |
3406acde SH |
173 | // This needs to happen last because of the check_if_active call that occurs |
174 | if (array_key_exists('action', $properties)) { | |
175 | $this->action = $properties['action']; | |
176 | if (is_string($this->action)) { | |
177 | $this->action = new moodle_url($this->action); | |
178 | } | |
179 | if (self::$autofindactive) { | |
180 | $this->check_if_active(); | |
181 | } | |
182 | } | |
d38a4849 SH |
183 | if (array_key_exists('parent', $properties)) { |
184 | $this->set_parent($properties['parent']); | |
185 | } | |
7d2a0492 | 186 | } else if (is_string($properties)) { |
187 | $this->text = $properties; | |
188 | } | |
189 | if ($this->text === null) { | |
190 | throw new coding_exception('You must set the text for the node when you create it.'); | |
191 | } | |
3406acde | 192 | // Default the title to the text |
7d2a0492 | 193 | $this->title = $this->text; |
3406acde SH |
194 | // Instantiate a new navigation node collection for this nodes children |
195 | $this->children = new navigation_node_collection(); | |
7d2a0492 | 196 | } |
197 | ||
95b97515 | 198 | /** |
3406acde | 199 | * Checks if this node is the active node. |
0baa5d46 | 200 | * |
3406acde SH |
201 | * This is determined by comparing the action for the node against the |
202 | * defined URL for the page. A match will see this node marked as active. | |
0baa5d46 | 203 | * |
3406acde SH |
204 | * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE |
205 | * @return bool | |
7d2a0492 | 206 | */ |
bf6c37c7 | 207 | public function check_if_active($strength=URL_MATCH_EXACT) { |
208 | global $FULLME, $PAGE; | |
3406acde | 209 | // Set fullmeurl if it hasn't already been set |
7d2a0492 | 210 | if (self::$fullmeurl == null) { |
bf6c37c7 | 211 | if ($PAGE->has_set_url()) { |
3406acde | 212 | self::override_active_url(new moodle_url($PAGE->url)); |
bf6c37c7 | 213 | } else { |
3406acde | 214 | self::override_active_url(new moodle_url($FULLME)); |
7d2a0492 | 215 | } |
216 | } | |
bf6c37c7 | 217 | |
3406acde | 218 | // Compare the action of this node against the fullmeurl |
bf6c37c7 | 219 | if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) { |
7d2a0492 | 220 | $this->make_active(); |
221 | return true; | |
7d2a0492 | 222 | } |
223 | return false; | |
224 | } | |
3406acde | 225 | |
7d2a0492 | 226 | /** |
d0cfbab3 SH |
227 | * This sets the URL that the URL of new nodes get compared to when locating |
228 | * the active node. | |
229 | * | |
230 | * The active node is the node that matches the URL set here. By default this | |
231 | * is either $PAGE->url or if that hasn't been set $FULLME. | |
7d2a0492 | 232 | * |
3406acde SH |
233 | * @param moodle_url $url The url to use for the fullmeurl. |
234 | */ | |
235 | public static function override_active_url(moodle_url $url) { | |
633c0843 TH |
236 | // Clone the URL, in case the calling script changes their URL later. |
237 | self::$fullmeurl = new moodle_url($url); | |
3406acde SH |
238 | } |
239 | ||
240 | /** | |
188a8127 | 241 | * Creates a navigation node, ready to add it as a child using add_node |
242 | * function. (The created node needs to be added before you can use it.) | |
3406acde SH |
243 | * @param string $text |
244 | * @param moodle_url|action_link $action | |
245 | * @param int $type | |
246 | * @param string $shorttext | |
247 | * @param string|int $key | |
248 | * @param pix_icon $icon | |
249 | * @return navigation_node | |
7d2a0492 | 250 | */ |
188a8127 | 251 | public static function create($text, $action=null, $type=self::TYPE_CUSTOM, |
252 | $shorttext=null, $key=null, pix_icon $icon=null) { | |
3406acde SH |
253 | // Properties array used when creating the new navigation node |
254 | $itemarray = array( | |
255 | 'text' => $text, | |
256 | 'type' => $type | |
257 | ); | |
258 | // Set the action if one was provided | |
7d2a0492 | 259 | if ($action!==null) { |
260 | $itemarray['action'] = $action; | |
261 | } | |
3406acde | 262 | // Set the shorttext if one was provided |
7d2a0492 | 263 | if ($shorttext!==null) { |
264 | $itemarray['shorttext'] = $shorttext; | |
265 | } | |
3406acde | 266 | // Set the icon if one was provided |
7d2a0492 | 267 | if ($icon!==null) { |
268 | $itemarray['icon'] = $icon; | |
269 | } | |
3406acde | 270 | // Set the key |
7d2a0492 | 271 | $itemarray['key'] = $key; |
188a8127 | 272 | // Construct and return |
273 | return new navigation_node($itemarray); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Adds a navigation node as a child of this node. | |
278 | * | |
279 | * @param string $text | |
280 | * @param moodle_url|action_link $action | |
281 | * @param int $type | |
282 | * @param string $shorttext | |
283 | * @param string|int $key | |
284 | * @param pix_icon $icon | |
285 | * @return navigation_node | |
286 | */ | |
287 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { | |
288 | // Create child node | |
289 | $childnode = self::create($text, $action, $type, $shorttext, $key, $icon); | |
290 | ||
291 | // Add the child to end and return | |
292 | return $this->add_node($childnode); | |
293 | } | |
294 | ||
295 | /** | |
296 | * Adds a navigation node as a child of this one, given a $node object | |
297 | * created using the create function. | |
298 | * @param navigation_node $childnode Node to add | |
31fde54f | 299 | * @param string $beforekey |
188a8127 | 300 | * @return navigation_node The added node |
301 | */ | |
302 | public function add_node(navigation_node $childnode, $beforekey=null) { | |
303 | // First convert the nodetype for this node to a branch as it will now have children | |
304 | if ($this->nodetype !== self::NODETYPE_BRANCH) { | |
305 | $this->nodetype = self::NODETYPE_BRANCH; | |
306 | } | |
3406acde | 307 | // Set the parent to this node |
d38a4849 | 308 | $childnode->set_parent($this); |
188a8127 | 309 | |
310 | // Default the key to the number of children if not provided | |
311 | if ($childnode->key === null) { | |
312 | $childnode->key = $this->children->count(); | |
313 | } | |
314 | ||
3406acde | 315 | // Add the child using the navigation_node_collections add method |
188a8127 | 316 | $node = $this->children->add($childnode, $beforekey); |
317 | ||
318 | // If added node is a category node or the user is logged in and it's a course | |
319 | // then mark added node as a branch (makes it expandable by AJAX) | |
320 | $type = $childnode->type; | |
c73e37e0 | 321 | if (($type==self::TYPE_CATEGORY) || (isloggedin() && $type==self::TYPE_COURSE)) { |
3406acde | 322 | $node->nodetype = self::NODETYPE_BRANCH; |
7d2a0492 | 323 | } |
3406acde | 324 | // If this node is hidden mark it's children as hidden also |
7d2a0492 | 325 | if ($this->hidden) { |
3406acde | 326 | $node->hidden = true; |
7d2a0492 | 327 | } |
188a8127 | 328 | // Return added node (reference returned by $this->children->add() |
3406acde | 329 | return $node; |
7d2a0492 | 330 | } |
331 | ||
0c2f94e0 TH |
332 | /** |
333 | * Return a list of all the keys of all the child nodes. | |
334 | * @return array the keys. | |
335 | */ | |
336 | public function get_children_key_list() { | |
337 | return $this->children->get_key_list(); | |
338 | } | |
339 | ||
7d2a0492 | 340 | /** |
3406acde | 341 | * Searches for a node of the given type with the given key. |
7d2a0492 | 342 | * |
3406acde SH |
343 | * This searches this node plus all of its children, and their children.... |
344 | * If you know the node you are looking for is a child of this node then please | |
345 | * use the get method instead. | |
346 | * | |
347 | * @param int|string $key The key of the node we are looking for | |
348 | * @param int $type One of navigation_node::TYPE_* | |
349 | * @return navigation_node|false | |
7d2a0492 | 350 | */ |
3406acde SH |
351 | public function find($key, $type) { |
352 | return $this->children->find($key, $type); | |
353 | } | |
354 | ||
355 | /** | |
31fde54f | 356 | * Get the child of this node that has the given key + (optional) type. |
3406acde SH |
357 | * |
358 | * If you are looking for a node and want to search all children + thier children | |
359 | * then please use the find method instead. | |
360 | * | |
361 | * @param int|string $key The key of the node we are looking for | |
362 | * @param int $type One of navigation_node::TYPE_* | |
363 | * @return navigation_node|false | |
364 | */ | |
365 | public function get($key, $type=null) { | |
366 | return $this->children->get($key, $type); | |
367 | } | |
368 | ||
369 | /** | |
370 | * Removes this node. | |
371 | * | |
372 | * @return bool | |
373 | */ | |
374 | public function remove() { | |
375 | return $this->parent->children->remove($this->key, $this->type); | |
376 | } | |
377 | ||
378 | /** | |
379 | * Checks if this node has or could have any children | |
380 | * | |
381 | * @return bool Returns true if it has children or could have (by AJAX expansion) | |
382 | */ | |
383 | public function has_children() { | |
384 | return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0); | |
385 | } | |
386 | ||
387 | /** | |
388 | * Marks this node as active and forces it open. | |
d0cfbab3 SH |
389 | * |
390 | * Important: If you are here because you need to mark a node active to get | |
93123b17 | 391 | * the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}? |
d0cfbab3 SH |
392 | * You can use it to specify a different URL to match the active navigation node on |
393 | * rather than having to locate and manually mark a node active. | |
3406acde SH |
394 | */ |
395 | public function make_active() { | |
396 | $this->isactive = true; | |
397 | $this->add_class('active_tree_node'); | |
398 | $this->force_open(); | |
399 | if ($this->parent !== null) { | |
400 | $this->parent->make_inactive(); | |
401 | } | |
402 | } | |
403 | ||
404 | /** | |
405 | * Marks a node as inactive and recusised back to the base of the tree | |
406 | * doing the same to all parents. | |
407 | */ | |
408 | public function make_inactive() { | |
409 | $this->isactive = false; | |
410 | $this->remove_class('active_tree_node'); | |
411 | if ($this->parent !== null) { | |
412 | $this->parent->make_inactive(); | |
7d2a0492 | 413 | } |
414 | } | |
415 | ||
416 | /** | |
3406acde SH |
417 | * Forces this node to be open and at the same time forces open all |
418 | * parents until the root node. | |
117bd748 | 419 | * |
3406acde SH |
420 | * Recursive. |
421 | */ | |
422 | public function force_open() { | |
423 | $this->forceopen = true; | |
424 | if ($this->parent !== null) { | |
425 | $this->parent->force_open(); | |
426 | } | |
427 | } | |
428 | ||
429 | /** | |
430 | * Adds a CSS class to this node. | |
431 | * | |
432 | * @param string $class | |
433 | * @return bool | |
7d2a0492 | 434 | */ |
435 | public function add_class($class) { | |
436 | if (!in_array($class, $this->classes)) { | |
437 | $this->classes[] = $class; | |
438 | } | |
439 | return true; | |
440 | } | |
441 | ||
442 | /** | |
3406acde | 443 | * Removes a CSS class from this node. |
7d2a0492 | 444 | * |
445 | * @param string $class | |
3406acde | 446 | * @return bool True if the class was successfully removed. |
7d2a0492 | 447 | */ |
448 | public function remove_class($class) { | |
449 | if (in_array($class, $this->classes)) { | |
450 | $key = array_search($class,$this->classes); | |
451 | if ($key!==false) { | |
452 | unset($this->classes[$key]); | |
453 | return true; | |
454 | } | |
455 | } | |
456 | return false; | |
457 | } | |
458 | ||
459 | /** | |
3406acde SH |
460 | * Sets the title for this node and forces Moodle to utilise it. |
461 | * @param string $title | |
462 | */ | |
463 | public function title($title) { | |
464 | $this->title = $title; | |
465 | $this->forcetitle = true; | |
466 | } | |
467 | ||
468 | /** | |
469 | * Resets the page specific information on this node if it is being unserialised. | |
470 | */ | |
471 | public function __wakeup(){ | |
472 | $this->forceopen = false; | |
473 | $this->isactive = false; | |
474 | $this->remove_class('active_tree_node'); | |
475 | } | |
476 | ||
477 | /** | |
478 | * Checks if this node or any of its children contain the active node. | |
435a512e | 479 | * |
3406acde | 480 | * Recursive. |
7d2a0492 | 481 | * |
3406acde | 482 | * @return bool |
7d2a0492 | 483 | */ |
3406acde SH |
484 | public function contains_active_node() { |
485 | if ($this->isactive) { | |
7d2a0492 | 486 | return true; |
487 | } else { | |
3406acde SH |
488 | foreach ($this->children as $child) { |
489 | if ($child->isactive || $child->contains_active_node()) { | |
490 | return true; | |
491 | } | |
492 | } | |
7d2a0492 | 493 | } |
3406acde | 494 | return false; |
7d2a0492 | 495 | } |
496 | ||
497 | /** | |
3406acde SH |
498 | * Finds the active node. |
499 | * | |
500 | * Searches this nodes children plus all of the children for the active node | |
501 | * and returns it if found. | |
7d2a0492 | 502 | * |
3406acde SH |
503 | * Recursive. |
504 | * | |
505 | * @return navigation_node|false | |
7d2a0492 | 506 | */ |
3406acde SH |
507 | public function find_active_node() { |
508 | if ($this->isactive) { | |
509 | return $this; | |
510 | } else { | |
7d2a0492 | 511 | foreach ($this->children as &$child) { |
3406acde SH |
512 | $outcome = $child->find_active_node(); |
513 | if ($outcome !== false) { | |
514 | return $outcome; | |
7d2a0492 | 515 | } |
516 | } | |
7d2a0492 | 517 | } |
3406acde | 518 | return false; |
7d2a0492 | 519 | } |
520 | ||
7c4efe3b SH |
521 | /** |
522 | * Searches all children for the best matching active node | |
523 | * @return navigation_node|false | |
524 | */ | |
525 | public function search_for_active_node() { | |
526 | if ($this->check_if_active(URL_MATCH_BASE)) { | |
527 | return $this; | |
528 | } else { | |
529 | foreach ($this->children as &$child) { | |
530 | $outcome = $child->search_for_active_node(); | |
531 | if ($outcome !== false) { | |
532 | return $outcome; | |
533 | } | |
534 | } | |
535 | } | |
536 | return false; | |
537 | } | |
538 | ||
7d2a0492 | 539 | /** |
3406acde | 540 | * Gets the content for this node. |
7d2a0492 | 541 | * |
3406acde SH |
542 | * @param bool $shorttext If true shorttext is used rather than the normal text |
543 | * @return string | |
7d2a0492 | 544 | */ |
3406acde | 545 | public function get_content($shorttext=false) { |
7d2a0492 | 546 | if ($shorttext && $this->shorttext!==null) { |
3406acde | 547 | return format_string($this->shorttext); |
7d2a0492 | 548 | } else { |
3406acde | 549 | return format_string($this->text); |
1c4eef57 | 550 | } |
3406acde | 551 | } |
1c4eef57 | 552 | |
3406acde SH |
553 | /** |
554 | * Gets the title to use for this node. | |
435a512e | 555 | * |
3406acde SH |
556 | * @return string |
557 | */ | |
558 | public function get_title() { | |
bbfa9be0 | 559 | if ($this->forcetitle || $this->action != null){ |
3406acde SH |
560 | return $this->title; |
561 | } else { | |
9bf16314 PS |
562 | return ''; |
563 | } | |
7d2a0492 | 564 | } |
117bd748 | 565 | |
7d2a0492 | 566 | /** |
3406acde | 567 | * Gets the CSS class to add to this node to describe its type |
435a512e | 568 | * |
7d2a0492 | 569 | * @return string |
570 | */ | |
571 | public function get_css_type() { | |
572 | if (array_key_exists($this->type, $this->namedtypes)) { | |
573 | return 'type_'.$this->namedtypes[$this->type]; | |
574 | } | |
575 | return 'type_unknown'; | |
576 | } | |
577 | ||
578 | /** | |
3406acde | 579 | * Finds all nodes that are expandable by AJAX |
7d2a0492 | 580 | * |
3406acde | 581 | * @param array $expandable An array by reference to populate with expandable nodes. |
7d2a0492 | 582 | */ |
3406acde | 583 | public function find_expandable(array &$expandable) { |
3406acde | 584 | foreach ($this->children as &$child) { |
8ad24c1a | 585 | if ($child->nodetype == self::NODETYPE_BRANCH && $child->children->count() == 0 && $child->display) { |
3406acde SH |
586 | $child->id = 'expandable_branch_'.(count($expandable)+1); |
587 | $this->add_class('canexpand'); | |
8ad24c1a | 588 | $expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type); |
7d2a0492 | 589 | } |
3406acde | 590 | $child->find_expandable($expandable); |
7d2a0492 | 591 | } |
7d2a0492 | 592 | } |
88f77c3c | 593 | |
4766a50c SH |
594 | /** |
595 | * Finds all nodes of a given type (recursive) | |
596 | * | |
31fde54f | 597 | * @param int $type One of navigation_node::TYPE_* |
4766a50c SH |
598 | * @return array |
599 | */ | |
88f77c3c SH |
600 | public function find_all_of_type($type) { |
601 | $nodes = $this->children->type($type); | |
602 | foreach ($this->children as &$node) { | |
603 | $childnodes = $node->find_all_of_type($type); | |
604 | $nodes = array_merge($nodes, $childnodes); | |
605 | } | |
606 | return $nodes; | |
607 | } | |
56ed242b SH |
608 | |
609 | /** | |
610 | * Removes this node if it is empty | |
611 | */ | |
612 | public function trim_if_empty() { | |
613 | if ($this->children->count() == 0) { | |
614 | $this->remove(); | |
615 | } | |
616 | } | |
617 | ||
618 | /** | |
619 | * Creates a tab representation of this nodes children that can be used | |
620 | * with print_tabs to produce the tabs on a page. | |
621 | * | |
622 | * call_user_func_array('print_tabs', $node->get_tabs_array()); | |
623 | * | |
624 | * @param array $inactive | |
625 | * @param bool $return | |
626 | * @return array Array (tabs, selected, inactive, activated, return) | |
627 | */ | |
628 | public function get_tabs_array(array $inactive=array(), $return=false) { | |
629 | $tabs = array(); | |
630 | $rows = array(); | |
631 | $selected = null; | |
632 | $activated = array(); | |
633 | foreach ($this->children as $node) { | |
634 | $tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title()); | |
635 | if ($node->contains_active_node()) { | |
636 | if ($node->children->count() > 0) { | |
637 | $activated[] = $node->key; | |
638 | foreach ($node->children as $child) { | |
639 | if ($child->contains_active_node()) { | |
640 | $selected = $child->key; | |
641 | } | |
642 | $rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title()); | |
643 | } | |
644 | } else { | |
645 | $selected = $node->key; | |
646 | } | |
647 | } | |
648 | } | |
649 | return array(array($tabs, $rows), $selected, $inactive, $activated, $return); | |
650 | } | |
d38a4849 SH |
651 | |
652 | /** | |
653 | * Sets the parent for this node and if this node is active ensures that the tree is properly | |
654 | * adjusted as well. | |
655 | * | |
656 | * @param navigation_node $parent | |
657 | */ | |
658 | public function set_parent(navigation_node $parent) { | |
659 | // Set the parent (thats the easy part) | |
660 | $this->parent = $parent; | |
661 | // Check if this node is active (this is checked during construction) | |
662 | if ($this->isactive) { | |
663 | // Force all of the parent nodes open so you can see this node | |
664 | $this->parent->force_open(); | |
665 | // Make all parents inactive so that its clear where we are. | |
666 | $this->parent->make_inactive(); | |
667 | } | |
668 | } | |
3406acde | 669 | } |
7d2a0492 | 670 | |
3406acde SH |
671 | /** |
672 | * Navigation node collection | |
673 | * | |
674 | * This class is responsible for managing a collection of navigation nodes. | |
675 | * It is required because a node's unique identifier is a combination of both its | |
676 | * key and its type. | |
677 | * | |
678 | * Originally an array was used with a string key that was a combination of the two | |
679 | * however it was decided that a better solution would be to use a class that | |
680 | * implements the standard IteratorAggregate interface. | |
681 | * | |
31fde54f AG |
682 | * @package core |
683 | * @category navigation | |
3406acde SH |
684 | * @copyright 2010 Sam Hemelryk |
685 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
686 | */ | |
687 | class navigation_node_collection implements IteratorAggregate { | |
7d2a0492 | 688 | /** |
3406acde SH |
689 | * A multidimensional array to where the first key is the type and the second |
690 | * key is the nodes key. | |
691 | * @var array | |
7d2a0492 | 692 | */ |
3406acde | 693 | protected $collection = array(); |
7d2a0492 | 694 | /** |
3406acde SH |
695 | * An array that contains references to nodes in the same order they were added. |
696 | * This is maintained as a progressive array. | |
697 | * @var array | |
7d2a0492 | 698 | */ |
3406acde | 699 | protected $orderedcollection = array(); |
da3ab9c4 | 700 | /** |
3406acde SH |
701 | * A reference to the last node that was added to the collection |
702 | * @var navigation_node | |
da3ab9c4 | 703 | */ |
3406acde | 704 | protected $last = null; |
6644741d | 705 | /** |
3406acde SH |
706 | * The total number of items added to this array. |
707 | * @var int | |
6644741d | 708 | */ |
3406acde | 709 | protected $count = 0; |
0c2f94e0 | 710 | |
7d2a0492 | 711 | /** |
3406acde | 712 | * Adds a navigation node to the collection |
7d2a0492 | 713 | * |
188a8127 | 714 | * @param navigation_node $node Node to add |
715 | * @param string $beforekey If specified, adds before a node with this key, | |
716 | * otherwise adds at end | |
717 | * @return navigation_node Added node | |
7d2a0492 | 718 | */ |
188a8127 | 719 | public function add(navigation_node $node, $beforekey=null) { |
3406acde SH |
720 | global $CFG; |
721 | $key = $node->key; | |
722 | $type = $node->type; | |
188a8127 | 723 | |
3406acde SH |
724 | // First check we have a 2nd dimension for this type |
725 | if (!array_key_exists($type, $this->orderedcollection)) { | |
726 | $this->orderedcollection[$type] = array(); | |
7d2a0492 | 727 | } |
3406acde SH |
728 | // Check for a collision and report if debugging is turned on |
729 | if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) { | |
730 | debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER); | |
7d2a0492 | 731 | } |
188a8127 | 732 | |
733 | // Find the key to add before | |
734 | $newindex = $this->count; | |
735 | $last = true; | |
736 | if ($beforekey !== null) { | |
737 | foreach ($this->collection as $index => $othernode) { | |
738 | if ($othernode->key === $beforekey) { | |
739 | $newindex = $index; | |
740 | $last = false; | |
741 | break; | |
742 | } | |
743 | } | |
744 | if ($newindex === $this->count) { | |
188a8127 | 745 | debugging('Navigation node add_before: Reference node not found ' . $beforekey . |
0c2f94e0 | 746 | ', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER); |
188a8127 | 747 | } |
748 | } | |
749 | ||
750 | // Add the node to the appropriate place in the by-type structure (which | |
751 | // is not ordered, despite the variable name) | |
3406acde | 752 | $this->orderedcollection[$type][$key] = $node; |
188a8127 | 753 | if (!$last) { |
754 | // Update existing references in the ordered collection (which is the | |
755 | // one that isn't called 'ordered') to shuffle them along if required | |
756 | for ($oldindex = $this->count; $oldindex > $newindex; $oldindex--) { | |
757 | $this->collection[$oldindex] = $this->collection[$oldindex - 1]; | |
758 | } | |
759 | } | |
3406acde | 760 | // Add a reference to the node to the progressive collection. |
188a8127 | 761 | $this->collection[$newindex] = &$this->orderedcollection[$type][$key]; |
3406acde SH |
762 | // Update the last property to a reference to this new node. |
763 | $this->last = &$this->orderedcollection[$type][$key]; | |
5436561c | 764 | |
188a8127 | 765 | // Reorder the array by index if needed |
766 | if (!$last) { | |
767 | ksort($this->collection); | |
188a8127 | 768 | } |
3406acde SH |
769 | $this->count++; |
770 | // Return the reference to the now added node | |
188a8127 | 771 | return $node; |
7d2a0492 | 772 | } |
773 | ||
0c2f94e0 TH |
774 | /** |
775 | * Return a list of all the keys of all the nodes. | |
776 | * @return array the keys. | |
777 | */ | |
778 | public function get_key_list() { | |
779 | $keys = array(); | |
780 | foreach ($this->collection as $node) { | |
781 | $keys[] = $node->key; | |
782 | } | |
783 | return $keys; | |
784 | } | |
785 | ||
7d2a0492 | 786 | /** |
3406acde | 787 | * Fetches a node from this collection. |
7d2a0492 | 788 | * |
3406acde SH |
789 | * @param string|int $key The key of the node we want to find. |
790 | * @param int $type One of navigation_node::TYPE_*. | |
791 | * @return navigation_node|null | |
7d2a0492 | 792 | */ |
3406acde SH |
793 | public function get($key, $type=null) { |
794 | if ($type !== null) { | |
795 | // If the type is known then we can simply check and fetch | |
796 | if (!empty($this->orderedcollection[$type][$key])) { | |
797 | return $this->orderedcollection[$type][$key]; | |
798 | } | |
799 | } else { | |
800 | // Because we don't know the type we look in the progressive array | |
801 | foreach ($this->collection as $node) { | |
802 | if ($node->key === $key) { | |
803 | return $node; | |
7d2a0492 | 804 | } |
805 | } | |
806 | } | |
807 | return false; | |
808 | } | |
0c2f94e0 | 809 | |
7d2a0492 | 810 | /** |
3406acde | 811 | * Searches for a node with matching key and type. |
7d2a0492 | 812 | * |
3406acde SH |
813 | * This function searches both the nodes in this collection and all of |
814 | * the nodes in each collection belonging to the nodes in this collection. | |
7d2a0492 | 815 | * |
3406acde | 816 | * Recursive. |
7d2a0492 | 817 | * |
3406acde SH |
818 | * @param string|int $key The key of the node we want to find. |
819 | * @param int $type One of navigation_node::TYPE_*. | |
820 | * @return navigation_node|null | |
7d2a0492 | 821 | */ |
3406acde SH |
822 | public function find($key, $type=null) { |
823 | if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) { | |
824 | return $this->orderedcollection[$type][$key]; | |
825 | } else { | |
826 | $nodes = $this->getIterator(); | |
827 | // Search immediate children first | |
828 | foreach ($nodes as &$node) { | |
d9219fc9 | 829 | if ($node->key === $key && ($type === null || $type === $node->type)) { |
3406acde | 830 | return $node; |
d926f4b1 | 831 | } |
3406acde SH |
832 | } |
833 | // Now search each childs children | |
834 | foreach ($nodes as &$node) { | |
835 | $result = $node->children->find($key, $type); | |
836 | if ($result !== false) { | |
837 | return $result; | |
d926f4b1 | 838 | } |
7d2a0492 | 839 | } |
840 | } | |
841 | return false; | |
842 | } | |
843 | ||
d926f4b1 | 844 | /** |
3406acde | 845 | * Fetches the last node that was added to this collection |
435a512e | 846 | * |
3406acde | 847 | * @return navigation_node |
d926f4b1 | 848 | */ |
3406acde SH |
849 | public function last() { |
850 | return $this->last; | |
851 | } | |
0c2f94e0 | 852 | |
3406acde SH |
853 | /** |
854 | * Fetches all nodes of a given type from this collection | |
31fde54f AG |
855 | * |
856 | * @param string|int $type node type being searched for. | |
857 | * @return array ordered collection | |
3406acde SH |
858 | */ |
859 | public function type($type) { | |
860 | if (!array_key_exists($type, $this->orderedcollection)) { | |
861 | $this->orderedcollection[$type] = array(); | |
d926f4b1 | 862 | } |
3406acde | 863 | return $this->orderedcollection[$type]; |
d926f4b1 | 864 | } |
7d2a0492 | 865 | /** |
3406acde | 866 | * Removes the node with the given key and type from the collection |
7d2a0492 | 867 | * |
31fde54f | 868 | * @param string|int $key The key of the node we want to find. |
3406acde SH |
869 | * @param int $type |
870 | * @return bool | |
7d2a0492 | 871 | */ |
3406acde SH |
872 | public function remove($key, $type=null) { |
873 | $child = $this->get($key, $type); | |
874 | if ($child !== false) { | |
875 | foreach ($this->collection as $colkey => $node) { | |
876 | if ($node->key == $key && $node->type == $type) { | |
877 | unset($this->collection[$colkey]); | |
878 | break; | |
879 | } | |
7d2a0492 | 880 | } |
3406acde SH |
881 | unset($this->orderedcollection[$child->type][$child->key]); |
882 | $this->count--; | |
883 | return true; | |
7d2a0492 | 884 | } |
3406acde | 885 | return false; |
7d2a0492 | 886 | } |
887 | ||
9da1ec27 | 888 | /** |
3406acde | 889 | * Gets the number of nodes in this collection |
e26507b3 SH |
890 | * |
891 | * This option uses an internal count rather than counting the actual options to avoid | |
892 | * a performance hit through the count function. | |
893 | * | |
3406acde | 894 | * @return int |
7d2a0492 | 895 | */ |
3406acde | 896 | public function count() { |
e26507b3 | 897 | return $this->count; |
7d2a0492 | 898 | } |
7d2a0492 | 899 | /** |
3406acde | 900 | * Gets an array iterator for the collection. |
7d2a0492 | 901 | * |
3406acde SH |
902 | * This is required by the IteratorAggregator interface and is used by routines |
903 | * such as the foreach loop. | |
7d2a0492 | 904 | * |
3406acde | 905 | * @return ArrayIterator |
7d2a0492 | 906 | */ |
3406acde SH |
907 | public function getIterator() { |
908 | return new ArrayIterator($this->collection); | |
7d2a0492 | 909 | } |
910 | } | |
911 | ||
912 | /** | |
913 | * The global navigation class used for... the global navigation | |
914 | * | |
915 | * This class is used by PAGE to store the global navigation for the site | |
916 | * and is then used by the settings nav and navbar to save on processing and DB calls | |
917 | * | |
918 | * See | |
93123b17 | 919 | * {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()} |
31fde54f | 920 | * {@link lib/ajax/getnavbranch.php} Called by ajax |
7d2a0492 | 921 | * |
31fde54f AG |
922 | * @package core |
923 | * @category navigation | |
7d2a0492 | 924 | * @copyright 2009 Sam Hemelryk |
925 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
926 | */ | |
927 | class global_navigation extends navigation_node { | |
31fde54f | 928 | /** @var moodle_page The Moodle page this navigation object belongs to. */ |
3406acde | 929 | protected $page; |
31fde54f | 930 | /** @var bool switch to let us know if the navigation object is initialised*/ |
7d2a0492 | 931 | protected $initialised = false; |
31fde54f | 932 | /** @var array An array of course information */ |
3406acde | 933 | protected $mycourses = array(); |
31fde54f | 934 | /** @var array An array for containing root navigation nodes */ |
3406acde | 935 | protected $rootnodes = array(); |
31fde54f | 936 | /** @var bool A switch for whether to show empty sections in the navigation */ |
3406acde | 937 | protected $showemptysections = false; |
31fde54f | 938 | /** @var bool A switch for whether courses should be shown within categories on the navigation. */ |
9bf5af21 | 939 | protected $showcategories = null; |
31fde54f | 940 | /** @var array An array of stdClasses for users that the navigation is extended for */ |
7a7e209d | 941 | protected $extendforuser = array(); |
3406acde SH |
942 | /** @var navigation_cache */ |
943 | protected $cache; | |
31fde54f | 944 | /** @var array An array of course ids that are present in the navigation */ |
3406acde | 945 | protected $addedcourses = array(); |
31fde54f | 946 | /** @var array An array of category ids that are included in the navigation */ |
e26507b3 | 947 | protected $addedcategories = array(); |
31fde54f | 948 | /** @var int expansion limit */ |
88f77c3c | 949 | protected $expansionlimit = 0; |
31fde54f | 950 | /** @var int userid to allow parent to see child's profile page navigation */ |
870815fa | 951 | protected $useridtouseforparentchecks = 0; |
88f77c3c | 952 | |
7d2a0492 | 953 | /** |
3406acde SH |
954 | * Constructs a new global navigation |
955 | * | |
3406acde | 956 | * @param moodle_page $page The page this navigation object belongs to |
7d2a0492 | 957 | */ |
3406acde | 958 | public function __construct(moodle_page $page) { |
4766a50c | 959 | global $CFG, $SITE, $USER; |
3406acde | 960 | |
7d2a0492 | 961 | if (during_initial_install()) { |
3406acde | 962 | return; |
7d2a0492 | 963 | } |
3406acde | 964 | |
4766a50c SH |
965 | if (get_home_page() == HOMEPAGE_SITE) { |
966 | // We are using the site home for the root element | |
967 | $properties = array( | |
968 | 'key' => 'home', | |
969 | 'type' => navigation_node::TYPE_SYSTEM, | |
970 | 'text' => get_string('home'), | |
971 | 'action' => new moodle_url('/') | |
972 | ); | |
973 | } else { | |
974 | // We are using the users my moodle for the root element | |
975 | $properties = array( | |
976 | 'key' => 'myhome', | |
977 | 'type' => navigation_node::TYPE_SYSTEM, | |
978 | 'text' => get_string('myhome'), | |
979 | 'action' => new moodle_url('/my/') | |
980 | ); | |
dd8e5011 | 981 | } |
4766a50c | 982 | |
31fde54f | 983 | // Use the parents constructor.... good good reuse |
3406acde SH |
984 | parent::__construct($properties); |
985 | ||
986 | // Initalise and set defaults | |
987 | $this->page = $page; | |
7d2a0492 | 988 | $this->forceopen = true; |
f5b5a822 | 989 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
7d2a0492 | 990 | } |
991 | ||
b9bcad24 AB |
992 | /** |
993 | * Mutator to set userid to allow parent to see child's profile | |
994 | * page navigation. See MDL-25805 for initial issue. Linked to it | |
995 | * is an issue explaining why this is a REALLY UGLY HACK thats not | |
996 | * for you to use! | |
997 | * | |
5607036a | 998 | * @param int $userid userid of profile page that parent wants to navigate around. |
b9bcad24 | 999 | */ |
870815fa SH |
1000 | public function set_userid_for_parent_checks($userid) { |
1001 | $this->useridtouseforparentchecks = $userid; | |
b9bcad24 AB |
1002 | } |
1003 | ||
1004 | ||
7d2a0492 | 1005 | /** |
3406acde | 1006 | * Initialises the navigation object. |
7d2a0492 | 1007 | * |
3406acde SH |
1008 | * This causes the navigation object to look at the current state of the page |
1009 | * that it is associated with and then load the appropriate content. | |
7d2a0492 | 1010 | * |
3406acde SH |
1011 | * This should only occur the first time that the navigation structure is utilised |
1012 | * which will normally be either when the navbar is called to be displayed or | |
1013 | * when a block makes use of it. | |
7d2a0492 | 1014 | * |
3406acde | 1015 | * @return bool |
7d2a0492 | 1016 | */ |
3406acde | 1017 | public function initialise() { |
4766a50c | 1018 | global $CFG, $SITE, $USER, $DB; |
3406acde | 1019 | // Check if it has alread been initialised |
7d2a0492 | 1020 | if ($this->initialised || during_initial_install()) { |
1021 | return true; | |
1022 | } | |
e2b436d0 | 1023 | $this->initialised = true; |
3406acde SH |
1024 | |
1025 | // Set up the five base root nodes. These are nodes where we will put our | |
1026 | // content and are as follows: | |
1027 | // site: Navigation for the front page. | |
1028 | // myprofile: User profile information goes here. | |
1029 | // mycourses: The users courses get added here. | |
1030 | // courses: Additional courses are added here. | |
1031 | // users: Other users information loaded here. | |
1032 | $this->rootnodes = array(); | |
4766a50c | 1033 | if (get_home_page() == HOMEPAGE_SITE) { |
3f9ccf85 | 1034 | // The home element should be my moodle because the root element is the site |
b9d4c7d3 | 1035 | if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in |
e26507b3 | 1036 | $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_SETTING, null, 'home'); |
3f9ccf85 | 1037 | } |
4766a50c SH |
1038 | } else { |
1039 | // The home element should be the site because the root node is my moodle | |
e26507b3 | 1040 | $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), self::TYPE_SETTING, null, 'home'); |
4766a50c SH |
1041 | if ($CFG->defaulthomepage == HOMEPAGE_MY) { |
1042 | // We need to stop automatic redirection | |
1043 | $this->rootnodes['home']->action->param('redirect', '0'); | |
1044 | } | |
1045 | } | |
3406acde | 1046 | $this->rootnodes['site'] = $this->add_course($SITE); |
e26507b3 | 1047 | $this->rootnodes['myprofile'] = $this->add(get_string('myprofile'), null, self::TYPE_USER, null, 'myprofile'); |
3406acde | 1048 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses'); |
83f78f8d | 1049 | $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); |
3406acde SH |
1050 | $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); |
1051 | ||
1052 | // Fetch all of the users courses. | |
4766a50c SH |
1053 | $limit = 20; |
1054 | if (!empty($CFG->navcourselimit)) { | |
1055 | $limit = $CFG->navcourselimit; | |
1056 | } | |
1057 | ||
e26507b3 SH |
1058 | $mycourses = enrol_get_my_courses(NULL, 'visible DESC,sortorder ASC', $limit); |
1059 | $showallcourses = (count($mycourses) == 0 || !empty($CFG->navshowallcourses)); | |
9bf5af21 | 1060 | $showcategories = ($showallcourses && $this->show_categories()); |
56c062a4 | 1061 | $issite = ($this->page->course->id == SITEID); |
e26507b3 | 1062 | $ismycourse = (array_key_exists($this->page->course->id, $mycourses)); |
ba2789c1 | 1063 | |
3406acde | 1064 | // Check if any courses were returned. |
e26507b3 | 1065 | if (count($mycourses) > 0) { |
3406acde | 1066 | // Add all of the users courses to the navigation |
e26507b3 SH |
1067 | foreach ($mycourses as $course) { |
1068 | $course->coursenode = $this->add_course($course, false, true); | |
3406acde | 1069 | } |
4766a50c SH |
1070 | } |
1071 | ||
e26507b3 | 1072 | if ($showallcourses) { |
ba2789c1 SH |
1073 | // Load all courses |
1074 | $this->load_all_courses(); | |
3406acde SH |
1075 | } |
1076 | ||
14d35a26 SH |
1077 | // We always load the frontpage course to ensure it is available without |
1078 | // JavaScript enabled. | |
1079 | $frontpagecourse = $this->load_course($SITE); | |
1080 | $this->add_front_page_course_essentials($frontpagecourse, $SITE); | |
56c062a4 | 1081 | $this->load_course_sections($SITE, $frontpagecourse); |
14d35a26 | 1082 | |
cede87e0 SH |
1083 | $canviewcourseprofile = true; |
1084 | ||
56c062a4 SH |
1085 | if (!$issite) { |
1086 | // Next load context specific content into the navigation | |
1087 | switch ($this->page->context->contextlevel) { | |
1088 | case CONTEXT_SYSTEM : | |
1089 | // This has already been loaded we just need to map the variable | |
1090 | $coursenode = $frontpagecourse; | |
1091 | $this->load_all_categories(null, $showcategories); | |
fab0a39f | 1092 | break; |
56c062a4 SH |
1093 | case CONTEXT_COURSECAT : |
1094 | // This has already been loaded we just need to map the variable | |
1095 | $coursenode = $frontpagecourse; | |
1096 | $this->load_all_categories($this->page->context->instanceid, $showcategories); | |
1097 | break; | |
1098 | case CONTEXT_BLOCK : | |
1099 | case CONTEXT_COURSE : | |
1100 | // Load the course associated with the page into the navigation | |
1101 | $course = $this->page->course; | |
1102 | if ($showcategories && !$ismycourse) { | |
1103 | $this->load_all_categories($course->category, $showcategories); | |
b9bcad24 | 1104 | } |
56c062a4 | 1105 | $coursenode = $this->load_course($course); |
b9bcad24 | 1106 | |
56c062a4 SH |
1107 | // If the course wasn't added then don't try going any further. |
1108 | if (!$coursenode) { | |
b9bcad24 AB |
1109 | $canviewcourseprofile = false; |
1110 | break; | |
1111 | } | |
e26507b3 | 1112 | |
56c062a4 SH |
1113 | // If the user is not enrolled then we only want to show the |
1114 | // course node and not populate it. | |
e26507b3 | 1115 | |
56c062a4 SH |
1116 | // Not enrolled, can't view, and hasn't switched roles |
1117 | if (!can_access_course($course)) { | |
1118 | // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in, | |
1119 | // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) | |
1120 | $isparent = false; | |
1121 | if ($this->useridtouseforparentchecks) { | |
1122 | if ($this->useridtouseforparentchecks != $USER->id) { | |
1123 | $usercontext = get_context_instance(CONTEXT_USER, $this->useridtouseforparentchecks, MUST_EXIST); | |
1124 | if ($DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id)) | |
1125 | and has_capability('moodle/user:viewdetails', $usercontext)) { | |
1126 | $isparent = true; | |
1127 | } | |
1128 | } | |
1129 | } | |
cede87e0 | 1130 | |
56c062a4 SH |
1131 | if (!$isparent) { |
1132 | $coursenode->make_active(); | |
1133 | $canviewcourseprofile = false; | |
1134 | break; | |
1135 | } | |
1136 | } | |
1137 | // Add the essentials such as reports etc... | |
1138 | $this->add_course_essentials($coursenode, $course); | |
1139 | if ($this->format_display_course_content($course->format)) { | |
1140 | // Load the course sections | |
1141 | $sections = $this->load_course_sections($course, $coursenode); | |
1142 | } | |
1143 | if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { | |
1144 | $coursenode->make_active(); | |
1145 | } | |
2027c10e | 1146 | break; |
56c062a4 SH |
1147 | case CONTEXT_MODULE : |
1148 | $course = $this->page->course; | |
1149 | $cm = $this->page->cm; | |
2027c10e | 1150 | |
56c062a4 SH |
1151 | if ($showcategories && !$ismycourse) { |
1152 | $this->load_all_categories($course->category, $showcategories); | |
1153 | } | |
cede87e0 | 1154 | |
56c062a4 SH |
1155 | // Load the course associated with the page into the navigation |
1156 | $coursenode = $this->load_course($course); | |
1157 | ||
1158 | // If the course wasn't added then don't try going any further. | |
1159 | if (!$coursenode) { | |
1160 | $canviewcourseprofile = false; | |
1161 | break; | |
1162 | } | |
1163 | ||
1164 | // If the user is not enrolled then we only want to show the | |
1165 | // course node and not populate it. | |
1166 | if (!can_access_course($course)) { | |
1167 | $coursenode->make_active(); | |
1168 | $canviewcourseprofile = false; | |
1169 | break; | |
1170 | } | |
1171 | ||
1172 | $this->add_course_essentials($coursenode, $course); | |
d67e4821 | 1173 | |
1174 | // Get section number from $cm (if provided) - we need this | |
1175 | // before loading sections in order to tell it to load this section | |
1176 | // even if it would not normally display (=> it contains only | |
1177 | // a label, which we are now editing) | |
1178 | $sectionnum = isset($cm->sectionnum) ? $cm->sectionnum : 0; | |
1179 | if ($sectionnum) { | |
1180 | // This value has to be stored in a member variable because | |
1181 | // otherwise we would have to pass it through a public API | |
1182 | // to course formats and they would need to change their | |
1183 | // functions to pass it along again... | |
1184 | $this->includesectionnum = $sectionnum; | |
1185 | } else { | |
1186 | $this->includesectionnum = false; | |
1187 | } | |
1188 | ||
56c062a4 SH |
1189 | // Load the course sections into the page |
1190 | $sections = $this->load_course_sections($course, $coursenode); | |
1191 | if ($course->id != SITEID) { | |
1192 | // Find the section for the $CM associated with the page and collect | |
1193 | // its section number. | |
d67e4821 | 1194 | if ($sectionnum) { |
1195 | $cm->sectionnumber = $sectionnum; | |
56c062a4 SH |
1196 | } else { |
1197 | foreach ($sections as $section) { | |
1198 | if ($section->id == $cm->section) { | |
1199 | $cm->sectionnumber = $section->section; | |
1200 | break; | |
1201 | } | |
0d8b6a69 | 1202 | } |
3406acde | 1203 | } |
3406acde | 1204 | |
56c062a4 SH |
1205 | // Load all of the section activities for the section the cm belongs to. |
1206 | if (isset($cm->sectionnumber) and !empty($sections[$cm->sectionnumber])) { | |
1207 | list($sectionarray, $activityarray) = $this->generate_sections_and_activities($course); | |
1208 | $activities = $this->load_section_activities($sections[$cm->sectionnumber]->sectionnode, $cm->sectionnumber, $activityarray); | |
1209 | } else { | |
1210 | $activities = array(); | |
1211 | if ($activity = $this->load_stealth_activity($coursenode, get_fast_modinfo($course))) { | |
1212 | // "stealth" activity from unavailable section | |
1213 | $activities[$cm->id] = $activity; | |
1214 | } | |
1215 | } | |
2a62743c PS |
1216 | } else { |
1217 | $activities = array(); | |
56c062a4 | 1218 | $activities[$cm->id] = $coursenode->get($cm->id, navigation_node::TYPE_ACTIVITY); |
2a62743c | 1219 | } |
56c062a4 SH |
1220 | if (!empty($activities[$cm->id])) { |
1221 | // Finally load the cm specific navigaton information | |
1222 | $this->load_activity($cm, $course, $activities[$cm->id]); | |
1223 | // Check if we have an active ndoe | |
1224 | if (!$activities[$cm->id]->contains_active_node() && !$activities[$cm->id]->search_for_active_node()) { | |
1225 | // And make the activity node active. | |
1226 | $activities[$cm->id]->make_active(); | |
1227 | } | |
1228 | } else { | |
1229 | //TODO: something is wrong, what to do? (Skodak) | |
2a62743c | 1230 | } |
56c062a4 SH |
1231 | break; |
1232 | case CONTEXT_USER : | |
50d15ca4 | 1233 | $course = $this->page->course; |
56c062a4 | 1234 | if ($showcategories && !$ismycourse) { |
e26507b3 SH |
1235 | $this->load_all_categories($course->category, $showcategories); |
1236 | } | |
3406acde SH |
1237 | // Load the course associated with the user into the navigation |
1238 | $coursenode = $this->load_course($course); | |
2027c10e SH |
1239 | |
1240 | // If the course wasn't added then don't try going any further. | |
1241 | if (!$coursenode) { | |
1242 | $canviewcourseprofile = false; | |
1243 | break; | |
1244 | } | |
1245 | ||
cede87e0 SH |
1246 | // If the user is not enrolled then we only want to show the |
1247 | // course node and not populate it. | |
1344b0ca | 1248 | if (!can_access_course($course)) { |
cede87e0 SH |
1249 | $coursenode->make_active(); |
1250 | $canviewcourseprofile = false; | |
1251 | break; | |
1252 | } | |
3406acde SH |
1253 | $this->add_course_essentials($coursenode, $course); |
1254 | $sections = $this->load_course_sections($course, $coursenode); | |
56c062a4 SH |
1255 | break; |
1256 | } | |
e52a5d3a SH |
1257 | } else { |
1258 | // We need to check if the user is viewing a front page module. | |
1259 | // If so then there is potentially more content to load yet for that | |
1260 | // module. | |
1261 | if ($this->page->context->contextlevel == CONTEXT_MODULE) { | |
1262 | $activitynode = $this->rootnodes['site']->get($this->page->cm->id, navigation_node::TYPE_ACTIVITY); | |
1263 | if ($activitynode) { | |
1264 | $this->load_activity($this->page->cm, $this->page->course, $activitynode); | |
1265 | } | |
1266 | } | |
7d2a0492 | 1267 | } |
7a7e209d | 1268 | |
ba2789c1 SH |
1269 | $limit = 20; |
1270 | if (!empty($CFG->navcourselimit)) { | |
1271 | $limit = $CFG->navcourselimit; | |
1272 | } | |
1273 | if ($showcategories) { | |
1274 | $categories = $this->find_all_of_type(self::TYPE_CATEGORY); | |
1275 | foreach ($categories as &$category) { | |
1276 | if ($category->children->count() >= $limit) { | |
1277 | $url = new moodle_url('/course/category.php', array('id'=>$category->key)); | |
1278 | $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); | |
1279 | } | |
1280 | } | |
1281 | } else if ($this->rootnodes['courses']->children->count() >= $limit) { | |
1282 | $this->rootnodes['courses']->add(get_string('viewallcoursescategories'), new moodle_url('/course/index.php'), self::TYPE_SETTING); | |
1283 | } | |
1284 | ||
3406acde SH |
1285 | // Load for the current user |
1286 | $this->load_for_user(); | |
cede87e0 | 1287 | if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != SITEID && $canviewcourseprofile) { |
87c215de SH |
1288 | $this->load_for_user(null, true); |
1289 | } | |
7a7e209d SH |
1290 | // Load each extending user into the navigation. |
1291 | foreach ($this->extendforuser as $user) { | |
44303ca6 | 1292 | if ($user->id != $USER->id) { |
7a7e209d SH |
1293 | $this->load_for_user($user); |
1294 | } | |
1295 | } | |
7a7e209d | 1296 | |
a683da3c SH |
1297 | // Give the local plugins a chance to include some navigation if they want. |
1298 | foreach (get_list_of_plugins('local') as $plugin) { | |
1299 | if (!file_exists($CFG->dirroot.'/local/'.$plugin.'/lib.php')) { | |
1300 | continue; | |
1301 | } | |
1302 | require_once($CFG->dirroot.'/local/'.$plugin.'/lib.php'); | |
1303 | $function = $plugin.'_extends_navigation'; | |
1304 | if (function_exists($function)) { | |
1305 | $function($this); | |
1306 | } | |
1307 | } | |
1308 | ||
3406acde SH |
1309 | // Remove any empty root nodes |
1310 | foreach ($this->rootnodes as $node) { | |
4766a50c SH |
1311 | // Dont remove the home node |
1312 | if ($node->key !== 'home' && !$node->has_children()) { | |
3406acde SH |
1313 | $node->remove(); |
1314 | } | |
1315 | } | |
1316 | ||
7c4efe3b SH |
1317 | if (!$this->contains_active_node()) { |
1318 | $this->search_for_active_node(); | |
1319 | } | |
1320 | ||
3406acde | 1321 | // If the user is not logged in modify the navigation structure as detailed |
728ebac7 | 1322 | // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} |
3406acde SH |
1323 | if (!isloggedin()) { |
1324 | $activities = clone($this->rootnodes['site']->children); | |
1325 | $this->rootnodes['site']->remove(); | |
1326 | $children = clone($this->children); | |
1327 | $this->children = new navigation_node_collection(); | |
1328 | foreach ($activities as $child) { | |
1329 | $this->children->add($child); | |
1330 | } | |
1331 | foreach ($children as $child) { | |
1332 | $this->children->add($child); | |
1333 | } | |
3406acde | 1334 | } |
7d2a0492 | 1335 | return true; |
1336 | } | |
9bf5af21 SH |
1337 | |
1338 | /** | |
31fde54f | 1339 | * Returns true if courses should be shown within categories on the navigation. |
9bf5af21 SH |
1340 | * |
1341 | * @return bool | |
1342 | */ | |
1343 | protected function show_categories() { | |
1344 | global $CFG, $DB; | |
1345 | if ($this->showcategories === null) { | |
1346 | $this->showcategories = !empty($CFG->navshowcategories) && $DB->count_records('course_categories') > 1; | |
1347 | } | |
1348 | return $this->showcategories; | |
1349 | } | |
1350 | ||
7d2a0492 | 1351 | /** |
3406acde SH |
1352 | * Checks the course format to see whether it wants the navigation to load |
1353 | * additional information for the course. | |
1354 | * | |
1355 | * This function utilises a callback that can exist within the course format lib.php file | |
1356 | * The callback should be a function called: | |
1357 | * callback_{formatname}_display_content() | |
1358 | * It doesn't get any arguments and should return true if additional content is | |
1359 | * desired. If the callback doesn't exist we assume additional content is wanted. | |
1360 | * | |
3406acde SH |
1361 | * @param string $format The course format |
1362 | * @return bool | |
1363 | */ | |
1364 | protected function format_display_course_content($format) { | |
1365 | global $CFG; | |
1366 | $formatlib = $CFG->dirroot.'/course/format/'.$format.'/lib.php'; | |
1367 | if (file_exists($formatlib)) { | |
1368 | require_once($formatlib); | |
1369 | $displayfunc = 'callback_'.$format.'_display_content'; | |
1370 | if (function_exists($displayfunc) && !$displayfunc()) { | |
1371 | return $displayfunc(); | |
1372 | } | |
1373 | } | |
1374 | return true; | |
1375 | } | |
1376 | ||
1377 | /** | |
31fde54f | 1378 | * Loads the courses in Moodle into the navigation. |
3406acde | 1379 | * |
31fde54f | 1380 | * @param mixed $categoryids Either a string or array of category ids to load courses for |
3406acde SH |
1381 | * @return array An array of navigation_node |
1382 | */ | |
4766a50c SH |
1383 | protected function load_all_courses($categoryids=null) { |
1384 | global $CFG, $DB, $USER; | |
1385 | ||
1386 | if ($categoryids !== null) { | |
1387 | if (is_array($categoryids)) { | |
e26507b3 | 1388 | list ($categoryselect, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'catid'); |
4766a50c | 1389 | } else { |
e26507b3 SH |
1390 | $categoryselect = '= :categoryid'; |
1391 | $params = array('categoryid', $categoryids); | |
4766a50c | 1392 | } |
e26507b3 SH |
1393 | $params['siteid'] = SITEID; |
1394 | $categoryselect = ' AND c.category '.$categoryselect; | |
4766a50c | 1395 | } else { |
e26507b3 SH |
1396 | $params = array('siteid' => SITEID); |
1397 | $categoryselect = ''; | |
1398 | } | |
1399 | ||
3406acde | 1400 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); |
e26507b3 SH |
1401 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array(SITEID), SQL_PARAMS_NAMED, 'lcourse', false); |
1402 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category, cat.path AS categorypath $ccselect | |
1403 | FROM {course} c | |
1404 | $ccjoin | |
1405 | LEFT JOIN {course_categories} cat ON cat.id=c.category | |
1406 | WHERE c.id {$courseids} {$categoryselect} | |
1407 | ORDER BY c.sortorder ASC"; | |
4766a50c SH |
1408 | $limit = 20; |
1409 | if (!empty($CFG->navcourselimit)) { | |
1410 | $limit = $CFG->navcourselimit; | |
1411 | } | |
e26507b3 | 1412 | $courses = $DB->get_records_sql($sql, $params + $courseparams, 0, $limit); |
4766a50c | 1413 | |
3406acde SH |
1414 | $coursenodes = array(); |
1415 | foreach ($courses as $course) { | |
1416 | context_instance_preload($course); | |
1417 | $coursenodes[$course->id] = $this->add_course($course); | |
1418 | } | |
1419 | return $coursenodes; | |
1420 | } | |
1421 | ||
4766a50c SH |
1422 | /** |
1423 | * Loads all categories (top level or if an id is specified for that category) | |
1424 | * | |
e26507b3 SH |
1425 | * @param int $categoryid The category id to load or null/0 to load all base level categories |
1426 | * @param bool $showbasecategories If set to true all base level categories will be loaded as well | |
1427 | * as the requested category and any parent categories. | |
31fde54f | 1428 | * @return navigation_node|void returns a navigation node if a category has been loaded. |
4766a50c | 1429 | */ |
e26507b3 | 1430 | protected function load_all_categories($categoryid = null, $showbasecategories = false) { |
4766a50c | 1431 | global $DB; |
e26507b3 SH |
1432 | |
1433 | // Check if this category has already been loaded | |
1434 | if ($categoryid !== null && array_key_exists($categoryid, $this->addedcategories) && $this->addedcategories[$categoryid]->children->count() > 0) { | |
1435 | return $this->addedcategories[$categoryid]; | |
1436 | } | |
1437 | ||
1438 | $coursestoload = array(); | |
1439 | if (empty($categoryid)) { // can be 0 | |
1440 | // We are going to load all of the first level categories (categories without parents) | |
c4afcf84 | 1441 | $categories = $DB->get_records('course_categories', array('parent'=>'0'), 'sortorder ASC, id ASC'); |
e26507b3 SH |
1442 | } else if (array_key_exists($categoryid, $this->addedcategories)) { |
1443 | // The category itself has been loaded already so we just need to ensure its subcategories | |
1444 | // have been loaded | |
1445 | list($sql, $params) = $DB->get_in_or_equal(array_keys($this->addedcategories), SQL_PARAMS_NAMED, 'parent', false); | |
1446 | if ($showbasecategories) { | |
1447 | // We need to include categories with parent = 0 as well | |
1448 | $sql = "SELECT * | |
1449 | FROM {course_categories} cc | |
1450 | WHERE (parent = :categoryid OR parent = 0) AND | |
1451 | parent {$sql} | |
c4afcf84 | 1452 | ORDER BY depth DESC, sortorder ASC, id ASC"; |
e26507b3 SH |
1453 | } else { |
1454 | $sql = "SELECT * | |
1455 | FROM {course_categories} cc | |
1456 | WHERE parent = :categoryid AND | |
1457 | parent {$sql} | |
c4afcf84 | 1458 | ORDER BY depth DESC, sortorder ASC, id ASC"; |
e26507b3 SH |
1459 | } |
1460 | $params['categoryid'] = $categoryid; | |
1461 | $categories = $DB->get_records_sql($sql, $params); | |
1462 | if (count($categories) == 0) { | |
1463 | // There are no further categories that require loading. | |
1464 | return; | |
1465 | } | |
4766a50c | 1466 | } else { |
e26507b3 SH |
1467 | // This category hasn't been loaded yet so we need to fetch it, work out its category path |
1468 | // and load this category plus all its parents and subcategories | |
3992a46e | 1469 | $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST); |
e26507b3 SH |
1470 | $coursestoload = explode('/', trim($category->path, '/')); |
1471 | list($select, $params) = $DB->get_in_or_equal($coursestoload); | |
4766a50c | 1472 | $select = 'id '.$select.' OR parent '.$select; |
e26507b3 SH |
1473 | if ($showbasecategories) { |
1474 | $select .= ' OR parent = 0'; | |
1475 | } | |
4766a50c SH |
1476 | $params = array_merge($params, $params); |
1477 | $categories = $DB->get_records_select('course_categories', $select, $params, 'sortorder'); | |
1478 | } | |
e26507b3 SH |
1479 | |
1480 | // Now we have an array of categories we need to add them to the navigation. | |
1481 | while (!empty($categories)) { | |
1482 | $category = reset($categories); | |
1483 | if (array_key_exists($category->id, $this->addedcategories)) { | |
1484 | // Do nothing | |
1485 | } else if ($category->parent == '0') { | |
1486 | $this->add_category($category, $this->rootnodes['courses']); | |
1487 | } else if (array_key_exists($category->parent, $this->addedcategories)) { | |
1488 | $this->add_category($category, $this->addedcategories[$category->parent]); | |
4766a50c | 1489 | } else { |
e26507b3 SH |
1490 | // This category isn't in the navigation and niether is it's parent (yet). |
1491 | // We need to go through the category path and add all of its components in order. | |
1492 | $path = explode('/', trim($category->path, '/')); | |
1493 | foreach ($path as $catid) { | |
1494 | if (!array_key_exists($catid, $this->addedcategories)) { | |
1495 | // This category isn't in the navigation yet so add it. | |
1496 | $subcategory = $categories[$catid]; | |
c4afcf84 SH |
1497 | if ($subcategory->parent == '0') { |
1498 | // Yay we have a root category - this likely means we will now be able | |
1499 | // to add categories without problems. | |
1500 | $this->add_category($subcategory, $this->rootnodes['courses']); | |
1501 | } else if (array_key_exists($subcategory->parent, $this->addedcategories)) { | |
e26507b3 SH |
1502 | // The parent is in the category (as we'd expect) so add it now. |
1503 | $this->add_category($subcategory, $this->addedcategories[$subcategory->parent]); | |
1504 | // Remove the category from the categories array. | |
1505 | unset($categories[$catid]); | |
1506 | } else { | |
1507 | // We should never ever arrive here - if we have then there is a bigger | |
1508 | // problem at hand. | |
5607036a | 1509 | throw new coding_exception('Category path order is incorrect and/or there are missing categories'); |
e26507b3 | 1510 | } |
4766a50c | 1511 | } |
4766a50c SH |
1512 | } |
1513 | } | |
e26507b3 SH |
1514 | // Remove the category from the categories array now that we know it has been added. |
1515 | unset($categories[$category->id]); | |
4766a50c | 1516 | } |
e26507b3 SH |
1517 | // Check if there are any categories to load. |
1518 | if (count($coursestoload) > 0) { | |
1519 | $this->load_all_courses($coursestoload); | |
4766a50c SH |
1520 | } |
1521 | } | |
1522 | ||
1523 | /** | |
1524 | * Adds a structured category to the navigation in the correct order/place | |
1525 | * | |
e26507b3 | 1526 | * @param stdClass $category |
435a512e | 1527 | * @param navigation_node $parent |
4766a50c | 1528 | */ |
e26507b3 SH |
1529 | protected function add_category(stdClass $category, navigation_node $parent) { |
1530 | if (array_key_exists($category->id, $this->addedcategories)) { | |
563329e8 | 1531 | return; |
e26507b3 SH |
1532 | } |
1533 | $url = new moodle_url('/course/category.php', array('id' => $category->id)); | |
63390481 SH |
1534 | $context = get_context_instance(CONTEXT_COURSECAT, $category->id); |
1535 | $categoryname = format_string($category->name, true, array('context' => $context)); | |
1536 | $categorynode = $parent->add($categoryname, $url, self::TYPE_CATEGORY, $categoryname, $category->id); | |
e26507b3 SH |
1537 | if (empty($category->visible)) { |
1538 | if (has_capability('moodle/category:viewhiddencategories', get_system_context())) { | |
1539 | $categorynode->hidden = true; | |
1540 | } else { | |
1541 | $categorynode->display = false; | |
14337688 | 1542 | } |
4766a50c | 1543 | } |
e26507b3 | 1544 | $this->addedcategories[$category->id] = &$categorynode; |
4766a50c SH |
1545 | } |
1546 | ||
3406acde SH |
1547 | /** |
1548 | * Loads the given course into the navigation | |
7d2a0492 | 1549 | * |
3406acde SH |
1550 | * @param stdClass $course |
1551 | * @return navigation_node | |
1552 | */ | |
1553 | protected function load_course(stdClass $course) { | |
1554 | if ($course->id == SITEID) { | |
e26507b3 SH |
1555 | return $this->rootnodes['site']; |
1556 | } else if (array_key_exists($course->id, $this->addedcourses)) { | |
1557 | return $this->addedcourses[$course->id]; | |
3406acde | 1558 | } else { |
e26507b3 | 1559 | return $this->add_course($course); |
3406acde | 1560 | } |
3406acde SH |
1561 | } |
1562 | ||
1563 | /** | |
1564 | * Loads all of the courses section into the navigation. | |
1565 | * | |
1566 | * This function utilisies a callback that can be implemented within the course | |
1567 | * formats lib.php file to customise the navigation that is generated at this | |
1568 | * point for the course. | |
1569 | * | |
93123b17 | 1570 | * By default (if not defined) the method {@link global_navigation::load_generic_course_sections()} is |
3406acde SH |
1571 | * called instead. |
1572 | * | |
3406acde SH |
1573 | * @param stdClass $course Database record for the course |
1574 | * @param navigation_node $coursenode The course node within the navigation | |
1575 | * @return array Array of navigation nodes for the section with key = section id | |
1576 | */ | |
1577 | protected function load_course_sections(stdClass $course, navigation_node $coursenode) { | |
1578 | global $CFG; | |
1579 | $structurefile = $CFG->dirroot.'/course/format/'.$course->format.'/lib.php'; | |
1580 | $structurefunc = 'callback_'.$course->format.'_load_content'; | |
1581 | if (function_exists($structurefunc)) { | |
1582 | return $structurefunc($this, $course, $coursenode); | |
1583 | } else if (file_exists($structurefile)) { | |
1584 | require_once $structurefile; | |
1585 | if (function_exists($structurefunc)) { | |
1586 | return $structurefunc($this, $course, $coursenode); | |
1587 | } else { | |
0f4ab67d | 1588 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1589 | } |
1590 | } else { | |
0f4ab67d | 1591 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1592 | } |
1593 | } | |
1594 | ||
e26507b3 SH |
1595 | /** |
1596 | * Generates an array of sections and an array of activities for the given course. | |
1597 | * | |
1598 | * This method uses the cache to improve performance and avoid the get_fast_modinfo call | |
1599 | * | |
1600 | * @param stdClass $course | |
1601 | * @return array Array($sections, $activities) | |
1602 | */ | |
1603 | protected function generate_sections_and_activities(stdClass $course) { | |
1604 | global $CFG; | |
1605 | require_once($CFG->dirroot.'/course/lib.php'); | |
1606 | ||
1607 | if (!$this->cache->cached('course_sections_'.$course->id) || !$this->cache->cached('course_activites_'.$course->id)) { | |
1608 | $modinfo = get_fast_modinfo($course); | |
1609 | $sections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true); | |
1610 | ||
1611 | $activities = array(); | |
1612 | ||
1613 | foreach ($sections as $key => $section) { | |
1614 | $sections[$key]->hasactivites = false; | |
1615 | if (!array_key_exists($section->section, $modinfo->sections)) { | |
1616 | continue; | |
1617 | } | |
1618 | foreach ($modinfo->sections[$section->section] as $cmid) { | |
1619 | $cm = $modinfo->cms[$cmid]; | |
1620 | if (!$cm->uservisible) { | |
1621 | continue; | |
1622 | } | |
1623 | $activity = new stdClass; | |
4037098e | 1624 | $activity->course = $course->id; |
e26507b3 SH |
1625 | $activity->section = $section->section; |
1626 | $activity->name = $cm->name; | |
1627 | $activity->icon = $cm->icon; | |
1628 | $activity->iconcomponent = $cm->iconcomponent; | |
1629 | $activity->id = $cm->id; | |
1630 | $activity->hidden = (!$cm->visible); | |
1631 | $activity->modname = $cm->modname; | |
1632 | $activity->nodetype = navigation_node::NODETYPE_LEAF; | |
dee1a0fd | 1633 | $activity->onclick = $cm->get_on_click(); |
e26507b3 SH |
1634 | $url = $cm->get_url(); |
1635 | if (!$url) { | |
1636 | $activity->url = null; | |
1637 | $activity->display = false; | |
1638 | } else { | |
1639 | $activity->url = $cm->get_url()->out(); | |
1640 | $activity->display = true; | |
1641 | if (self::module_extends_navigation($cm->modname)) { | |
1642 | $activity->nodetype = navigation_node::NODETYPE_BRANCH; | |
1643 | } | |
1644 | } | |
1645 | $activities[$cmid] = $activity; | |
d67e4821 | 1646 | if ($activity->display) { |
1647 | $sections[$key]->hasactivites = true; | |
1648 | } | |
e26507b3 SH |
1649 | } |
1650 | } | |
1651 | $this->cache->set('course_sections_'.$course->id, $sections); | |
1652 | $this->cache->set('course_activites_'.$course->id, $activities); | |
1653 | } else { | |
1654 | $sections = $this->cache->{'course_sections_'.$course->id}; | |
1655 | $activities = $this->cache->{'course_activites_'.$course->id}; | |
1656 | } | |
1657 | return array($sections, $activities); | |
1658 | } | |
1659 | ||
3406acde SH |
1660 | /** |
1661 | * Generically loads the course sections into the course's navigation. | |
1662 | * | |
1663 | * @param stdClass $course | |
1664 | * @param navigation_node $coursenode | |
e26507b3 | 1665 | * @param string $courseformat The course format |
3406acde SH |
1666 | * @return array An array of course section nodes |
1667 | */ | |
0f4ab67d | 1668 | public function load_generic_course_sections(stdClass $course, navigation_node $coursenode, $courseformat='unknown') { |
df997f84 | 1669 | global $CFG, $DB, $USER; |
df997f84 | 1670 | require_once($CFG->dirroot.'/course/lib.php'); |
435a512e | 1671 | |
e26507b3 | 1672 | list($sections, $activities) = $this->generate_sections_and_activities($course); |
0f4ab67d SH |
1673 | |
1674 | $namingfunction = 'callback_'.$courseformat.'_get_section_name'; | |
1675 | $namingfunctionexists = (function_exists($namingfunction)); | |
ad470097 | 1676 | |
e26507b3 | 1677 | $viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context); |
435a512e | 1678 | |
ad470097 SH |
1679 | $urlfunction = 'callback_'.$courseformat.'_get_section_url'; |
1680 | if (empty($CFG->navlinkcoursesections) || !function_exists($urlfunction)) { | |
1681 | $urlfunction = null; | |
1682 | } | |
1683 | ||
1684 | $keyfunction = 'callback_'.$courseformat.'_request_key'; | |
1685 | $key = course_get_display($course->id); | |
1686 | if (defined('AJAX_SCRIPT') && AJAX_SCRIPT == '0' && function_exists($keyfunction) && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { | |
1687 | $key = optional_param($keyfunction(), $key, PARAM_INT); | |
1688 | } | |
1689 | ||
7487c856 | 1690 | $navigationsections = array(); |
e26507b3 | 1691 | foreach ($sections as $sectionid => $section) { |
7487c856 | 1692 | $section = clone($section); |
3406acde | 1693 | if ($course->id == SITEID) { |
e26507b3 | 1694 | $this->load_section_activities($coursenode, $section->section, $activities); |
3406acde | 1695 | } else { |
d67e4821 | 1696 | if ((!$viewhiddensections && !$section->visible) || (!$this->showemptysections && |
1697 | !$section->hasactivites && $this->includesectionnum !== $section->section)) { | |
3406acde SH |
1698 | continue; |
1699 | } | |
0f4ab67d SH |
1700 | if ($namingfunctionexists) { |
1701 | $sectionname = $namingfunction($course, $section, $sections); | |
3406acde | 1702 | } else { |
0f4ab67d | 1703 | $sectionname = get_string('section').' '.$section->section; |
3406acde | 1704 | } |
ad470097 | 1705 | |
dbe5050d | 1706 | $url = null; |
ad470097 SH |
1707 | if (!empty($urlfunction)) { |
1708 | $url = $urlfunction($course->id, $section->section); | |
1709 | } | |
3406acde SH |
1710 | $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id); |
1711 | $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; | |
1712 | $sectionnode->hidden = (!$section->visible); | |
ad470097 SH |
1713 | if ($key != '0' && $section->section != '0' && $section->section == $key && $this->page->context->contextlevel != CONTEXT_MODULE && $section->hasactivites) { |
1714 | $sectionnode->make_active(); | |
e26507b3 | 1715 | $this->load_section_activities($sectionnode, $section->section, $activities); |
3406acde SH |
1716 | } |
1717 | $section->sectionnode = $sectionnode; | |
7487c856 | 1718 | $navigationsections[$sectionid] = $section; |
3406acde SH |
1719 | } |
1720 | } | |
7487c856 | 1721 | return $navigationsections; |
3406acde SH |
1722 | } |
1723 | /** | |
1724 | * Loads all of the activities for a section into the navigation structure. | |
1725 | * | |
1726 | * @param navigation_node $sectionnode | |
1727 | * @param int $sectionnumber | |
93123b17 | 1728 | * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()} |
1580cfdb | 1729 | * @param stdClass $course The course object the section and activities relate to. |
3406acde SH |
1730 | * @return array Array of activity nodes |
1731 | */ | |
1580cfdb | 1732 | protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) { |
4037098e | 1733 | global $CFG; |
dee1a0fd SH |
1734 | // A static counter for JS function naming |
1735 | static $legacyonclickcounter = 0; | |
3406acde | 1736 | |
e26507b3 | 1737 | $activitynodes = array(); |
4037098e SH |
1738 | if (empty($activities)) { |
1739 | return $activitynodes; | |
1740 | } | |
1741 | ||
1742 | if (!is_object($course)) { | |
1743 | $activity = reset($activities); | |
1744 | $courseid = $activity->course; | |
1745 | } else { | |
1746 | $courseid = $course->id; | |
1747 | } | |
1748 | $showactivities = ($courseid != SITEID || !empty($CFG->navshowfrontpagemods)); | |
1749 | ||
e26507b3 SH |
1750 | foreach ($activities as $activity) { |
1751 | if ($activity->section != $sectionnumber) { | |
3406acde SH |
1752 | continue; |
1753 | } | |
e26507b3 SH |
1754 | if ($activity->icon) { |
1755 | $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent); | |
3406acde | 1756 | } else { |
e26507b3 | 1757 | $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname); |
3406acde | 1758 | } |
dee1a0fd SH |
1759 | |
1760 | // Prepare the default name and url for the node | |
1761 | $activityname = format_string($activity->name, true, array('context' => get_context_instance(CONTEXT_MODULE, $activity->id))); | |
1762 | $action = new moodle_url($activity->url); | |
1763 | ||
1764 | // Check if the onclick property is set (puke!) | |
1765 | if (!empty($activity->onclick)) { | |
1766 | // Increment the counter so that we have a unique number. | |
1767 | $legacyonclickcounter++; | |
1768 | // Generate the function name we will use | |
1769 | $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter; | |
1770 | $propogrationhandler = ''; | |
1771 | // Check if we need to cancel propogation. Remember inline onclick | |
1772 | // events would return false if they wanted to prevent propogation and the | |
1773 | // default action. | |
1774 | if (strpos($activity->onclick, 'return false')) { | |
1775 | $propogrationhandler = 'e.halt();'; | |
1776 | } | |
1777 | // Decode the onclick - it has already been encoded for display (puke) | |
1778 | $onclick = htmlspecialchars_decode($activity->onclick); | |
1779 | // Build the JS function the click event will call | |
1780 | $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }"; | |
1781 | $this->page->requires->js_init_code($jscode); | |
1782 | // Override the default url with the new action link | |
1783 | $action = new action_link($action, $activityname, new component_action('click', $functionname)); | |
1784 | } | |
1785 | ||
1786 | $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon); | |
e26507b3 SH |
1787 | $activitynode->title(get_string('modulename', $activity->modname)); |
1788 | $activitynode->hidden = $activity->hidden; | |
4037098e | 1789 | $activitynode->display = $showactivities && $activity->display; |
e26507b3 SH |
1790 | $activitynode->nodetype = $activity->nodetype; |
1791 | $activitynodes[$activity->id] = $activitynode; | |
3406acde SH |
1792 | } |
1793 | ||
e26507b3 | 1794 | return $activitynodes; |
3406acde | 1795 | } |
2a62743c PS |
1796 | /** |
1797 | * Loads a stealth module from unavailable section | |
1798 | * @param navigation_node $coursenode | |
1799 | * @param stdClass $modinfo | |
1800 | * @return navigation_node or null if not accessible | |
1801 | */ | |
1802 | protected function load_stealth_activity(navigation_node $coursenode, $modinfo) { | |
1803 | if (empty($modinfo->cms[$this->page->cm->id])) { | |
1804 | return null; | |
1805 | } | |
1806 | $cm = $modinfo->cms[$this->page->cm->id]; | |
1807 | if (!$cm->uservisible) { | |
1808 | return null; | |
1809 | } | |
1810 | if ($cm->icon) { | |
1811 | $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent); | |
1812 | } else { | |
1813 | $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname); | |
1814 | } | |
0d8b6a69 | 1815 | $url = $cm->get_url(); |
2a62743c PS |
1816 | $activitynode = $coursenode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon); |
1817 | $activitynode->title(get_string('modulename', $cm->modname)); | |
1818 | $activitynode->hidden = (!$cm->visible); | |
0d8b6a69 | 1819 | if (!$url) { |
1820 | // Don't show activities that don't have links! | |
2a62743c | 1821 | $activitynode->display = false; |
e26507b3 | 1822 | } else if (self::module_extends_navigation($cm->modname)) { |
2a62743c PS |
1823 | $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; |
1824 | } | |
1825 | return $activitynode; | |
1826 | } | |
3406acde SH |
1827 | /** |
1828 | * Loads the navigation structure for the given activity into the activities node. | |
1829 | * | |
1830 | * This method utilises a callback within the modules lib.php file to load the | |
1831 | * content specific to activity given. | |
1832 | * | |
1833 | * The callback is a method: {modulename}_extend_navigation() | |
1834 | * Examples: | |
93123b17 EL |
1835 | * * {@link forum_extend_navigation()} |
1836 | * * {@link workshop_extend_navigation()} | |
3406acde | 1837 | * |
f0dcc212 | 1838 | * @param cm_info|stdClass $cm |
3406acde SH |
1839 | * @param stdClass $course |
1840 | * @param navigation_node $activity | |
1841 | * @return bool | |
1842 | */ | |
0d8b6a69 | 1843 | protected function load_activity($cm, stdClass $course, navigation_node $activity) { |
3406acde | 1844 | global $CFG, $DB; |
44303ca6 | 1845 | |
f0dcc212 SH |
1846 | // make sure we have a $cm from get_fast_modinfo as this contains activity access details |
1847 | if (!($cm instanceof cm_info)) { | |
1848 | $modinfo = get_fast_modinfo($course); | |
1849 | $cm = $modinfo->get_cm($cm->id); | |
1850 | } | |
3406acde | 1851 | |
577c8964 | 1852 | $activity->nodetype = navigation_node::NODETYPE_LEAF; |
3406acde SH |
1853 | $activity->make_active(); |
1854 | $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'; | |
1855 | $function = $cm->modname.'_extend_navigation'; | |
1856 | ||
1857 | if (file_exists($file)) { | |
1858 | require_once($file); | |
1859 | if (function_exists($function)) { | |
1860 | $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); | |
1861 | $function($activity, $course, $activtyrecord, $cm); | |
3406acde SH |
1862 | } |
1863 | } | |
577c8964 MG |
1864 | |
1865 | // Allow the active advanced grading method plugin to append module navigation | |
1866 | $featuresfunc = $cm->modname.'_supports'; | |
1867 | if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING)) { | |
1868 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
1869 | $gradingman = get_grading_manager($cm->context, $cm->modname); | |
1870 | $gradingman->extend_navigation($this, $activity); | |
1871 | } | |
1872 | ||
1873 | return $activity->has_children(); | |
3406acde SH |
1874 | } |
1875 | /** | |
4d00fded | 1876 | * Loads user specific information into the navigation in the appropriate place. |
3406acde SH |
1877 | * |
1878 | * If no user is provided the current user is assumed. | |
1879 | * | |
3406acde | 1880 | * @param stdClass $user |
4d00fded | 1881 | * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) |
3406acde | 1882 | * @return bool |
7a7e209d | 1883 | */ |
87c215de | 1884 | protected function load_for_user($user=null, $forceforcontext=false) { |
3406acde | 1885 | global $DB, $CFG, $USER; |
4f0c2d00 | 1886 | |
7a7e209d SH |
1887 | if ($user === null) { |
1888 | // We can't require login here but if the user isn't logged in we don't | |
1889 | // want to show anything | |
b9d4c7d3 | 1890 | if (!isloggedin() || isguestuser()) { |
7a7e209d SH |
1891 | return false; |
1892 | } | |
1893 | $user = $USER; | |
7a7e209d SH |
1894 | } else if (!is_object($user)) { |
1895 | // If the user is not an object then get them from the database | |
e26507b3 SH |
1896 | list($select, $join) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx'); |
1897 | $sql = "SELECT u.* $select FROM {user} u $join WHERE u.id = :userid"; | |
1898 | $user = $DB->get_record_sql($sql, array('userid' => (int)$user), MUST_EXIST); | |
1899 | context_instance_preload($user); | |
7a7e209d | 1900 | } |
136ca7c8 SH |
1901 | |
1902 | $iscurrentuser = ($user->id == $USER->id); | |
1903 | ||
507a7a9a | 1904 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); |
7a7e209d SH |
1905 | |
1906 | // Get the course set against the page, by default this will be the site | |
3406acde SH |
1907 | $course = $this->page->course; |
1908 | $baseargs = array('id'=>$user->id); | |
44303ca6 | 1909 | if ($course->id != SITEID && (!$iscurrentuser || $forceforcontext)) { |
e26507b3 | 1910 | $coursenode = $this->load_course($course); |
7a7e209d | 1911 | $baseargs['course'] = $course->id; |
3406acde | 1912 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
7a7e209d | 1913 | $issitecourse = false; |
7d2a0492 | 1914 | } else { |
7a7e209d | 1915 | // Load all categories and get the context for the system |
507a7a9a | 1916 | $coursecontext = get_context_instance(CONTEXT_SYSTEM); |
7a7e209d SH |
1917 | $issitecourse = true; |
1918 | } | |
1919 | ||
1920 | // Create a node to add user information under. | |
87c215de | 1921 | if ($iscurrentuser && !$forceforcontext) { |
3406acde SH |
1922 | // If it's the current user the information will go under the profile root node |
1923 | $usernode = $this->rootnodes['myprofile']; | |
e96bc3f9 SH |
1924 | $course = get_site(); |
1925 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); | |
1926 | $issitecourse = true; | |
7a7e209d SH |
1927 | } else { |
1928 | if (!$issitecourse) { | |
1929 | // Not the current user so add it to the participants node for the current course | |
3406acde | 1930 | $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); |
ad93ddd5 | 1931 | $userviewurl = new moodle_url('/user/view.php', $baseargs); |
7a7e209d SH |
1932 | } else { |
1933 | // This is the site so add a users node to the root branch | |
3406acde | 1934 | $usersnode = $this->rootnodes['users']; |
8b0614d9 DP |
1935 | if (has_capability('moodle/course:viewparticipants', $coursecontext)) { |
1936 | $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); | |
1937 | } | |
ad93ddd5 | 1938 | $userviewurl = new moodle_url('/user/profile.php', $baseargs); |
7a7e209d | 1939 | } |
f5c1e621 SH |
1940 | if (!$usersnode) { |
1941 | // We should NEVER get here, if the course hasn't been populated | |
1942 | // with a participants node then the navigaiton either wasn't generated | |
1943 | // for it (you are missing a require_login or set_context call) or | |
1944 | // you don't have access.... in the interests of no leaking informatin | |
1945 | // we simply quit... | |
1946 | return false; | |
1947 | } | |
7a7e209d | 1948 | // Add a branch for the current user |
76565fa1 AG |
1949 | $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); |
1950 | $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, $user->id); | |
3406acde | 1951 | |
5ac851fb SH |
1952 | if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { |
1953 | $usernode->make_active(); | |
1954 | } | |
7a7e209d SH |
1955 | } |
1956 | ||
1957 | // If the user is the current user or has permission to view the details of the requested | |
1958 | // user than add a view profile link. | |
507a7a9a | 1959 | if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { |
87c215de | 1960 | if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { |
03d9401e MD |
1961 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php',$baseargs)); |
1962 | } else { | |
1963 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php',$baseargs)); | |
1964 | } | |
7a7e209d SH |
1965 | } |
1966 | ||
356a6de3 SH |
1967 | if (!empty($CFG->navadduserpostslinks)) { |
1968 | // Add nodes for forum posts and discussions if the user can view either or both | |
1969 | // There are no capability checks here as the content of the page is based | |
1970 | // purely on the forums the current user has access too. | |
1971 | $forumtab = $usernode->add(get_string('forumposts', 'forum')); | |
1972 | $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); | |
1973 | $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode'=>'discussions')))); | |
1974 | } | |
7a7e209d | 1975 | |
27bad0a6 SH |
1976 | // Add blog nodes |
1977 | if (!empty($CFG->bloglevel)) { | |
e26507b3 SH |
1978 | if (!$this->cache->cached('userblogoptions'.$user->id)) { |
1979 | require_once($CFG->dirroot.'/blog/lib.php'); | |
1980 | // Get all options for the user | |
1981 | $options = blog_get_options_for_user($user); | |
1982 | $this->cache->set('userblogoptions'.$user->id, $options); | |
1983 | } else { | |
1984 | $options = $this->cache->{'userblogoptions'.$user->id}; | |
1985 | } | |
1986 | ||
27bad0a6 SH |
1987 | if (count($options) > 0) { |
1988 | $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); | |
c000545d JF |
1989 | foreach ($options as $type => $option) { |
1990 | if ($type == "rss") { | |
1991 | $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); | |
1992 | } else { | |
1993 | $blogs->add($option['string'], $option['link']); | |
1994 | } | |
27bad0a6 SH |
1995 | } |
1996 | } | |
1997 | } | |
1998 | ||
5ac851fb SH |
1999 | if (!empty($CFG->messaging)) { |
2000 | $messageargs = null; | |
2001 | if ($USER->id!=$user->id) { | |
2002 | $messageargs = array('id'=>$user->id); | |
2003 | } | |
2004 | $url = new moodle_url('/message/index.php',$messageargs); | |
2005 | $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); | |
c81b9f69 | 2006 | } |
c81b9f69 | 2007 | |
52d1a804 JG |
2008 | $context = get_context_instance(CONTEXT_USER, $USER->id); |
2009 | if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) { | |
82af55d7 MD |
2010 | $url = new moodle_url('/user/files.php'); |
2011 | $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); | |
78765507 DC |
2012 | } |
2013 | ||
7a7e209d | 2014 | // Add a node to view the users notes if permitted |
507a7a9a | 2015 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { |
3406acde SH |
2016 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id)); |
2017 | if ($coursecontext->instanceid) { | |
2018 | $url->param('course', $coursecontext->instanceid); | |
2019 | } | |
2020 | $usernode->add(get_string('notes', 'notes'), $url); | |
7a7e209d SH |
2021 | } |
2022 | ||
beda8fa8 | 2023 | // Add reports node |
4d00fded | 2024 | $reporttab = $usernode->add(get_string('activityreports')); |
4d00fded PS |
2025 | $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); |
2026 | foreach ($reports as $reportfunction) { | |
2027 | $reportfunction($reporttab, $user, $course); | |
2028 | } | |
beda8fa8 PS |
2029 | $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); |
2030 | if ($anyreport || ($course->showreports && $iscurrentuser && $forceforcontext)) { | |
2031 | // Add grade hardcoded grade report if necessary | |
03d9401e MD |
2032 | $gradeaccess = false; |
2033 | if (has_capability('moodle/grade:viewall', $coursecontext)) { | |
2034 | //ok - can view all course grades | |
7a7e209d | 2035 | $gradeaccess = true; |
03d9401e MD |
2036 | } else if ($course->showgrades) { |
2037 | if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) { | |
2038 | //ok - can view own grades | |
2039 | $gradeaccess = true; | |
2040 | } else if (has_capability('moodle/grade:viewall', $usercontext)) { | |
2041 | // ok - can view grades of this user - parent most probably | |
2042 | $gradeaccess = true; | |
2043 | } else if ($anyreport) { | |
2044 | // ok - can view grades of this user - parent most probably | |
2045 | $gradeaccess = true; | |
2046 | } | |
2047 | } | |
2048 | if ($gradeaccess) { | |
a0b15989 | 2049 | $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array('mode'=>'grade', 'id'=>$course->id, 'user'=>$usercontext->instanceid))); |
7a7e209d | 2050 | } |
7a7e209d | 2051 | } |
beda8fa8 | 2052 | // Check the number of nodes in the report node... if there are none remove the node |
4d00fded PS |
2053 | $reporttab->trim_if_empty(); |
2054 | ||
7a7e209d | 2055 | // If the user is the current user add the repositories for the current user |
9acb8241 | 2056 | $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); |
7a7e209d | 2057 | if ($iscurrentuser) { |
e26507b3 SH |
2058 | if (!$this->cache->cached('contexthasrepos'.$usercontext->id)) { |
2059 | require_once($CFG->dirroot . '/repository/lib.php'); | |
2060 | $editabletypes = repository::get_editable_types($usercontext); | |
2061 | $haseditabletypes = !empty($editabletypes); | |
2062 | unset($editabletypes); | |
2063 | $this->cache->set('contexthasrepos'.$usercontext->id, $haseditabletypes); | |
2064 | } else { | |
2065 | $haseditabletypes = $this->cache->{'contexthasrepos'.$usercontext->id}; | |
2066 | } | |
2067 | if ($haseditabletypes) { | |
ad70376c | 2068 | $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); |
7a7e209d | 2069 | } |
9acb8241 SH |
2070 | } else if ($course->id == SITEID && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { |
2071 | ||
2072 | // Add view grade report is permitted | |
2073 | $reports = get_plugin_list('gradereport'); | |
2074 | arsort($reports); // user is last, we want to test it first | |
2075 | ||
2076 | $userscourses = enrol_get_users_courses($user->id); | |
2077 | $userscoursesnode = $usernode->add(get_string('courses')); | |
69816a5c | 2078 | |
9acb8241 SH |
2079 | foreach ($userscourses as $usercourse) { |
2080 | $usercoursecontext = get_context_instance(CONTEXT_COURSE, $usercourse->id); | |
8ebbb06a SH |
2081 | $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext)); |
2082 | $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$usercourse->id)), self::TYPE_CONTAINER); | |
9acb8241 SH |
2083 | |
2084 | $gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext); | |
2085 | if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { | |
2086 | foreach ($reports as $plugin => $plugindir) { | |
2087 | if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) { | |
2088 | //stop when the first visible plugin is found | |
2089 | $gradeavailable = true; | |
2090 | break; | |
deaf04c7 | 2091 | } |
9acb8241 SH |
2092 | } |
2093 | } | |
2094 | ||
2095 | if ($gradeavailable) { | |
2096 | $url = new moodle_url('/grade/report/index.php', array('id'=>$usercourse->id)); | |
2097 | $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', '')); | |
2098 | } | |
2099 | ||
2100 | // Add a node to view the users notes if permitted | |
2101 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { | |
2102 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id, 'course'=>$usercourse->id)); | |
2103 | $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); | |
2104 | } | |
2105 | ||
1344b0ca | 2106 | if (can_access_course($usercourse, $user->id)) { |
9acb8241 SH |
2107 | $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', array('id'=>$usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); |
2108 | } | |
2109 | ||
4d00fded PS |
2110 | $reporttab = $usercoursenode->add(get_string('activityreports')); |
2111 | ||
2112 | $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); | |
2113 | foreach ($reports as $reportfunction) { | |
2114 | $reportfunction($reporttab, $user, $usercourse); | |
2115 | } | |
2116 | ||
4d00fded | 2117 | $reporttab->trim_if_empty(); |
9acb8241 | 2118 | } |
7a7e209d SH |
2119 | } |
2120 | return true; | |
2121 | } | |
2122 | ||
2123 | /** | |
3406acde | 2124 | * This method simply checks to see if a given module can extend the navigation. |
7d2a0492 | 2125 | * |
31fde54f | 2126 | * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation. |
e26507b3 | 2127 | * |
7d2a0492 | 2128 | * @param string $modname |
2129 | * @return bool | |
2130 | */ | |
e26507b3 | 2131 | protected static function module_extends_navigation($modname) { |
7d2a0492 | 2132 | global $CFG; |
e26507b3 SH |
2133 | static $extendingmodules = array(); |
2134 | if (!array_key_exists($modname, $extendingmodules)) { | |
2135 | $extendingmodules[$modname] = false; | |
2136 | $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php'; | |
2137 | if (file_exists($file)) { | |
2138 | $function = $modname.'_extend_navigation'; | |
2139 | require_once($file); | |
2140 | $extendingmodules[$modname] = (function_exists($function)); | |
7d2a0492 | 2141 | } |
2142 | } | |
e26507b3 | 2143 | return $extendingmodules[$modname]; |
7d2a0492 | 2144 | } |
2145 | /** | |
3406acde | 2146 | * Extends the navigation for the given user. |
435a512e | 2147 | * |
3406acde | 2148 | * @param stdClass $user A user from the database |
7d2a0492 | 2149 | */ |
3406acde SH |
2150 | public function extend_for_user($user) { |
2151 | $this->extendforuser[] = $user; | |
5d07e957 SH |
2152 | } |
2153 | ||
2154 | /** | |
2155 | * Returns all of the users the navigation is being extended for | |
2156 | * | |
31fde54f | 2157 | * @return array An array of extending users. |
5d07e957 SH |
2158 | */ |
2159 | public function get_extending_users() { | |
2160 | return $this->extendforuser; | |
7d2a0492 | 2161 | } |
7d2a0492 | 2162 | /** |
3406acde | 2163 | * Adds the given course to the navigation structure. |
7d2a0492 | 2164 | * |
3406acde | 2165 | * @param stdClass $course |
31fde54f AG |
2166 | * @param bool $forcegeneric (optional) |
2167 | * @param bool $ismycourse (optional) | |
3406acde | 2168 | * @return navigation_node |
7d2a0492 | 2169 | */ |
e26507b3 | 2170 | public function add_course(stdClass $course, $forcegeneric = false, $ismycourse = false) { |
4766a50c | 2171 | global $CFG; |
44303ca6 | 2172 | |
e26507b3 SH |
2173 | // We found the course... we can return it now :) |
2174 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2175 | return $this->addedcourses[$course->id]; | |
2176 | } | |
2177 | ||
8ebbb06a SH |
2178 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
2179 | ||
e26507b3 SH |
2180 | if ($course->id != SITEID && !$course->visible) { |
2181 | if (is_role_switched($course->id)) { | |
2182 | // user has to be able to access course in order to switch, let's skip the visibility test here | |
8ebbb06a | 2183 | } else if (!has_capability('moodle/course:viewhiddencourses', $coursecontext)) { |
e26507b3 | 2184 | return false; |
44303ca6 | 2185 | } |
7d2a0492 | 2186 | } |
7d2a0492 | 2187 | |
4766a50c | 2188 | $issite = ($course->id == SITEID); |
e26507b3 | 2189 | $ismycourse = ($ismycourse && !$forcegeneric); |
8ebbb06a | 2190 | $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); |
4766a50c SH |
2191 | |
2192 | if ($issite) { | |
3406acde | 2193 | $parent = $this; |
4766a50c | 2194 | $url = null; |
016ba638 SH |
2195 | if (empty($CFG->usesitenameforsitepages)) { |
2196 | $shortname = get_string('sitepages'); | |
2197 | } | |
4766a50c | 2198 | } else if ($ismycourse) { |
3406acde SH |
2199 | $parent = $this->rootnodes['mycourses']; |
2200 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); | |
7a7e209d | 2201 | } else { |
3406acde | 2202 | $parent = $this->rootnodes['courses']; |
a6855934 | 2203 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
7d2a0492 | 2204 | } |
4766a50c | 2205 | |
3992a46e | 2206 | if (!$ismycourse && !$issite && !empty($course->category)) { |
9bf5af21 | 2207 | if ($this->show_categories()) { |
3992a46e SH |
2208 | // We need to load the category structure for this course |
2209 | $this->load_all_categories($course->category); | |
2210 | } | |
2211 | if (array_key_exists($course->category, $this->addedcategories)) { | |
2212 | $parent = $this->addedcategories[$course->category]; | |
2213 | // This could lead to the course being created so we should check whether it is the case again | |
2214 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2215 | return $this->addedcourses[$course->id]; | |
2216 | } | |
4766a50c SH |
2217 | } |
2218 | } | |
2219 | ||
95892197 | 2220 | $coursenode = $parent->add($shortname, $url, self::TYPE_COURSE, $shortname, $course->id); |
3406acde SH |
2221 | $coursenode->nodetype = self::NODETYPE_BRANCH; |
2222 | $coursenode->hidden = (!$course->visible); | |
91d284c1 | 2223 | $coursenode->title(format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))); |
e26507b3 SH |
2224 | if (!$forcegeneric) { |
2225 | $this->addedcourses[$course->id] = &$coursenode; | |
2226 | } | |
4766a50c SH |
2227 | if ($ismycourse && !empty($CFG->navshowallcourses)) { |
2228 | // We need to add this course to the general courses node as well as the | |
2229 | // my courses node, rerun the function with the kill param | |
2230 | $genericcourse = $this->add_course($course, true); | |
2231 | if ($genericcourse->isactive) { | |
2232 | $genericcourse->make_inactive(); | |
2233 | $genericcourse->collapse = true; | |
2234 | if ($genericcourse->parent && $genericcourse->parent->type == self::TYPE_CATEGORY) { | |
2235 | $parent = $genericcourse->parent; | |
2236 | while ($parent && $parent->type == self::TYPE_CATEGORY) { | |
2237 | $parent->collapse = true; | |
2238 | $parent = $parent->parent; | |
2239 | } | |
2240 | } | |
2241 | } | |
2242 | } | |
e26507b3 | 2243 | |
3406acde | 2244 | return $coursenode; |
7d2a0492 | 2245 | } |
2246 | /** | |
3406acde | 2247 | * Adds essential course nodes to the navigation for the given course. |
7d2a0492 | 2248 | * |
3406acde | 2249 | * This method adds nodes such as reports, blogs and participants |
7d2a0492 | 2250 | * |
3406acde SH |
2251 | * @param navigation_node $coursenode |
2252 | * @param stdClass $course | |
31fde54f | 2253 | * @return bool returns true on successful addition of a node. |
7d2a0492 | 2254 | */ |
2027c10e | 2255 | public function add_course_essentials($coursenode, stdClass $course) { |
3406acde | 2256 | global $CFG; |
7d2a0492 | 2257 | |
4766a50c | 2258 | if ($course->id == SITEID) { |
3406acde | 2259 | return $this->add_front_page_course_essentials($coursenode, $course); |
7d2a0492 | 2260 | } |
7d2a0492 | 2261 | |
2027c10e | 2262 | if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { |
3406acde | 2263 | return true; |
7d2a0492 | 2264 | } |
7d2a0492 | 2265 | |
3406acde SH |
2266 | //Participants |
2267 | if (has_capability('moodle/course:viewparticipants', $this->page->context)) { | |
3406acde SH |
2268 | $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2269 | $currentgroup = groups_get_course_group($course, true); | |
2270 | if ($course->id == SITEID) { | |
2271 | $filterselect = ''; | |
2272 | } else if ($course->id && !$currentgroup) { | |
2273 | $filterselect = $course->id; | |
2274 | } else { | |
2275 | $filterselect = $currentgroup; | |
2276 | } | |
2277 | $filterselect = clean_param($filterselect, PARAM_INT); | |
8f6c1f34 PS |
2278 | if (($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) |
2279 | and has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { | |
3406acde SH |
2280 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); |
2281 | $participants->add(get_string('blogs','blog'), $blogsurls->out()); | |
2282 | } | |
2283 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { | |
41eae0d9 | 2284 | $participants->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$course->id))); |
3406acde | 2285 | } |
533299cb | 2286 | } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { |
3406acde SH |
2287 | $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2288 | } | |
2289 | ||
2290 | // View course reports | |
2291 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
275cbac7 PS |
2292 | $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); |
2293 | $coursereports = get_plugin_list('coursereport'); // deprecated | |
3406acde SH |
2294 | foreach ($coursereports as $report=>$dir) { |
2295 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
2296 | if (file_exists($libfile)) { | |
2297 | require_once($libfile); | |
2298 | $reportfunction = $report.'_report_extend_navigation'; | |
2299 | if (function_exists($report.'_report_extend_navigation')) { | |
2300 | $reportfunction($reportnav, $course, $this->page->context); | |
7d2a0492 | 2301 | } |
2302 | } | |
2303 | } | |
275cbac7 PS |
2304 | |
2305 | $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); | |
2306 | foreach ($reports as $reportfunction) { | |
2307 | $reportfunction($reportnav, $course, $this->page->context); | |
2308 | } | |
7d2a0492 | 2309 | } |
2310 | return true; | |
2311 | } | |
deaf04c7 | 2312 | /** |
31fde54f | 2313 | * This generates the structure of the course that won't be generated when |
deaf04c7 SH |
2314 | * the modules and sections are added. |
2315 | * | |
2316 | * Things such as the reports branch, the participants branch, blogs... get | |
2317 | * added to the course node by this method. | |
2318 | * | |
2319 | * @param navigation_node $coursenode | |
2320 | * @param stdClass $course | |
2321 | * @return bool True for successfull generation | |
2322 | */ | |
3406acde SH |
2323 | public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { |
2324 | global $CFG; | |
7d2a0492 | 2325 | |
1fa692ed | 2326 | if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { |
3406acde | 2327 | return true; |
7a7e209d SH |
2328 | } |
2329 | ||
1fa692ed SH |
2330 | // Hidden node that we use to determine if the front page navigation is loaded. |
2331 | // This required as there are not other guaranteed nodes that may be loaded. | |
2332 | $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false; | |
2333 | ||
3406acde | 2334 | //Participants |
b475cf4c | 2335 | if (has_capability('moodle/course:viewparticipants', get_system_context())) { |
3406acde SH |
2336 | $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); |
2337 | } | |
435a512e | 2338 | |
83a5e4fc | 2339 | $filterselect = 0; |
593270c6 MD |
2340 | |
2341 | // Blogs | |
8f6c1f34 PS |
2342 | if (!empty($CFG->bloglevel) |
2343 | and ($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) | |
2344 | and has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { | |
2345 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); | |
2346 | $coursenode->add(get_string('blogs','blog'), $blogsurls->out()); | |
7d2a0492 | 2347 | } |
593270c6 MD |
2348 | |
2349 | // Notes | |
3406acde SH |
2350 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { |
2351 | $coursenode->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect))); | |
2352 | } | |
593270c6 MD |
2353 | |
2354 | // Tags | |
2355 | if (!empty($CFG->usetags) && isloggedin()) { | |
3406acde | 2356 | $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php')); |
7d2a0492 | 2357 | } |
6644741d | 2358 | |
e7f9b7ab SH |
2359 | if (isloggedin()) { |
2360 | // Calendar | |
2361 | $calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month')); | |
2362 | $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar'); | |
2363 | } | |
6644741d | 2364 | |
3406acde SH |
2365 | // View course reports |
2366 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
275cbac7 PS |
2367 | $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); |
2368 | $coursereports = get_plugin_list('coursereport'); // deprecated | |
3406acde SH |
2369 | foreach ($coursereports as $report=>$dir) { |
2370 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
2371 | if (file_exists($libfile)) { | |
2372 | require_once($libfile); | |
2373 | $reportfunction = $report.'_report_extend_navigation'; | |
2374 | if (function_exists($report.'_report_extend_navigation')) { | |
2375 | $reportfunction($reportnav, $course, $this->page->context); | |
2376 | } | |
6644741d | 2377 | } |
6644741d | 2378 | } |
275cbac7 PS |
2379 | |
2380 | $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); | |
2381 | foreach ($reports as $reportfunction) { | |
2382 | $reportfunction($reportnav, $course, $this->page->context); | |
2383 | } | |
6644741d | 2384 | } |
3406acde | 2385 | return true; |
6644741d | 2386 | } |
da3ab9c4 | 2387 | |
3406acde SH |
2388 | /** |
2389 | * Clears the navigation cache | |
2390 | */ | |
2391 | public function clear_cache() { | |
2392 | $this->cache->clear(); | |
da3ab9c4 | 2393 | } |
88f77c3c | 2394 | |
deaf04c7 SH |
2395 | /** |
2396 | * Sets an expansion limit for the navigation | |
2397 | * | |
2398 | * The expansion limit is used to prevent the display of content that has a type | |
2399 | * greater than the provided $type. | |
2400 | * | |
2401 | * Can be used to ensure things such as activities or activity content don't get | |
2402 | * shown on the navigation. | |
2403 | * They are still generated in order to ensure the navbar still makes sense. | |
2404 | * | |
2405 | * @param int $type One of navigation_node::TYPE_* | |
31fde54f | 2406 | * @return bool true when complete. |
deaf04c7 | 2407 | */ |
88f77c3c SH |
2408 | public function set_expansion_limit($type) { |
2409 | $nodes = $this->find_all_of_type($type); | |
2410 | foreach ($nodes as &$node) { | |
1af67ecb SH |
2411 | // We need to generate the full site node |
2412 | if ($type == self::TYPE_COURSE && $node->key == SITEID) { | |
2413 | continue; | |
2414 | } | |
88f77c3c | 2415 | foreach ($node->children as &$child) { |
1af67ecb SH |
2416 | // We still want to show course reports and participants containers |
2417 | // or there will be navigation missing. | |
2418 | if ($type == self::TYPE_COURSE && $child->type === self::TYPE_CONTAINER) { | |
2419 | continue; | |
2420 | } | |
88f77c3c SH |
2421 | $child->display = false; |
2422 | } | |
2423 | } | |
2424 | return true; | |
2425 | } | |
deaf04c7 SH |
2426 | /** |
2427 | * Attempts to get the navigation with the given key from this nodes children. | |
2428 | * | |
2429 | * This function only looks at this nodes children, it does NOT look recursivily. | |
2430 | * If the node can't be found then false is returned. | |
2431 | * | |
93123b17 | 2432 | * If you need to search recursivily then use the {@link global_navigation::find()} method. |
deaf04c7 | 2433 | * |
93123b17 | 2434 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2435 | * may be of more use to you. |
2436 | * | |
2437 | * @param string|int $key The key of the node you wish to receive. | |
2438 | * @param int $type One of navigation_node::TYPE_* | |
2439 | * @return navigation_node|false | |
2440 | */ | |
e2b436d0 | 2441 | public function get($key, $type = null) { |
246a9b05 SH |
2442 | if (!$this->initialised) { |
2443 | $this->initialise(); | |
2444 | } | |
54dc15ab | 2445 | return parent::get($key, $type); |
e2b436d0 SH |
2446 | } |
2447 | ||
deaf04c7 | 2448 | /** |
31fde54f | 2449 | * Searches this nodes children and their children to find a navigation node |
deaf04c7 SH |
2450 | * with the matching key and type. |
2451 | * | |
2452 | * This method is recursive and searches children so until either a node is | |
31fde54f | 2453 | * found or there are no more nodes to search. |
deaf04c7 SH |
2454 | * |
2455 | * If you know that the node being searched for is a child of this node | |
93123b17 | 2456 | * then use the {@link global_navigation::get()} method instead. |
deaf04c7 | 2457 | * |
93123b17 | 2458 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2459 | * may be of more use to you. |
2460 | * | |
2461 | * @param string|int $key The key of the node you wish to receive. | |
2462 | * @param int $type One of navigation_node::TYPE_* | |
2463 | * @return navigation_node|false | |
2464 | */ | |
e2b436d0 | 2465 | public function find($key, $type) { |
246a9b05 SH |
2466 | if (!$this->initialised) { |
2467 | $this->initialise(); | |
2468 | } | |
54dc15ab | 2469 | return parent::find($key, $type); |
e2b436d0 | 2470 | } |
7d2a0492 | 2471 | } |
2472 | ||
2473 | /** | |
2474 | * The limited global navigation class used for the AJAX extension of the global | |
2475 | * navigation class. | |
2476 | * | |
2477 | * The primary methods that are used in the global navigation class have been overriden | |
2478 | * to ensure that only the relevant branch is generated at the root of the tree. | |
2479 | * This can be done because AJAX is only used when the backwards structure for the | |
2480 | * requested branch exists. | |
2481 | * This has been done only because it shortens the amounts of information that is generated | |
2482 | * which of course will speed up the response time.. because no one likes laggy AJAX. | |
2483 | * | |
31fde54f AG |
2484 | * @package core |
2485 | * @category navigation | |
7d2a0492 | 2486 | * @copyright 2009 Sam Hemelryk |
2487 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2488 | */ | |
507a7a9a | 2489 | class global_navigation_for_ajax extends global_navigation { |
3406acde | 2490 | |
31fde54f | 2491 | /** @var int used for determining what type of navigation_node::TYPE_* is being used */ |
39ae5e54 | 2492 | protected $branchtype; |
31fde54f AG |
2493 | |
2494 | /** @var int the instance id */ | |
39ae5e54 SH |
2495 | protected $instanceid; |
2496 | ||
31fde54f | 2497 | /** @var array Holds an array of expandable nodes */ |
3406acde SH |
2498 | protected $expandable = array(); |
2499 | ||
7d2a0492 | 2500 | /** |
31fde54f AG |
2501 | * Constructs the navigation for use in an AJAX request |
2502 | * | |
2503 | * @param moodle_page $page moodle_page object | |
2504 | * @param int $branchtype | |
2505 | * @param int $id | |
3406acde | 2506 | */ |
246a9b05 | 2507 | public function __construct($page, $branchtype, $id) { |
4766a50c | 2508 | $this->page = $page; |
3406acde SH |
2509 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
2510 | $this->children = new navigation_node_collection(); | |
39ae5e54 SH |
2511 | $this->branchtype = $branchtype; |
2512 | $this->instanceid = $id; | |
2513 | $this->initialise(); | |
3406acde SH |
2514 | } |
2515 | /** | |
2516 | * Initialise the navigation given the type and id for the branch to expand. | |
7d2a0492 | 2517 | * |
31fde54f | 2518 | * @return array An array of the expandable nodes |
7d2a0492 | 2519 | */ |
39ae5e54 SH |
2520 | public function initialise() { |
2521 | global $CFG, $DB, $SITE; | |
507a7a9a | 2522 | |
7d2a0492 | 2523 | if ($this->initialised || during_initial_install()) { |
95b97515 | 2524 | return $this->expandable; |
7d2a0492 | 2525 | } |
246a9b05 SH |
2526 | $this->initialised = true; |
2527 | ||
2528 | $this->rootnodes = array(); | |
e26507b3 | 2529 | $this->rootnodes['site'] = $this->add_course($SITE); |
246a9b05 | 2530 | $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); |
507a7a9a SH |
2531 | |
2532 | // Branchtype will be one of navigation_node::TYPE_* | |
39ae5e54 | 2533 | switch ($this->branchtype) { |
4766a50c | 2534 | case self::TYPE_CATEGORY : |
39ae5e54 | 2535 | $this->load_all_categories($this->instanceid); |
4766a50c SH |
2536 | $limit = 20; |
2537 | if (!empty($CFG->navcourselimit)) { | |
2538 | $limit = (int)$CFG->navcourselimit; | |
2539 | } | |
39ae5e54 | 2540 | $courses = $DB->get_records('course', array('category' => $this->instanceid), 'sortorder','*', 0, $limit); |
4766a50c SH |
2541 | foreach ($courses as $course) { |
2542 | $this->add_course($course); | |
2543 | } | |
2544 | break; | |
507a7a9a | 2545 | case self::TYPE_COURSE : |
39ae5e54 | 2546 | $course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST); |
507a7a9a | 2547 | require_course_login($course); |
87c215de | 2548 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
2549 | $coursenode = $this->add_course($course); |
2550 | $this->add_course_essentials($coursenode, $course); | |
2551 | if ($this->format_display_course_content($course->format)) { | |
2552 | $this->load_course_sections($course, $coursenode); | |
2553 | } | |
7d2a0492 | 2554 | break; |
507a7a9a | 2555 | case self::TYPE_SECTION : |
3406acde | 2556 | $sql = 'SELECT c.*, cs.section AS sectionnumber |
507a7a9a SH |
2557 | FROM {course} c |
2558 | LEFT JOIN {course_sections} cs ON cs.course = c.id | |
2559 | WHERE cs.id = ?'; | |
39ae5e54 | 2560 | $course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST); |
507a7a9a | 2561 | require_course_login($course); |
87c215de | 2562 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
2563 | $coursenode = $this->add_course($course); |
2564 | $this->add_course_essentials($coursenode, $course); | |
2565 | $sections = $this->load_course_sections($course, $coursenode); | |
e26507b3 SH |
2566 | list($sectionarray, $activities) = $this->generate_sections_and_activities($course); |
2567 | $this->load_section_activities($sections[$course->sectionnumber]->sectionnode, $course->sectionnumber, $activities); | |
7d2a0492 | 2568 | break; |
507a7a9a | 2569 | case self::TYPE_ACTIVITY : |
c78262b5 SH |
2570 | $sql = "SELECT c.* |
2571 | FROM {course} c | |
2572 | JOIN {course_modules} cm ON cm.course = c.id | |
2573 | WHERE cm.id = :cmid"; | |
2574 | $params = array('cmid' => $this->instanceid); | |
2575 | $course = $DB->get_record_sql($sql, $params, MUST_EXIST); | |
f0dcc212 SH |
2576 | $modinfo = get_fast_modinfo($course); |
2577 | $cm = $modinfo->get_cm($this->instanceid); | |
507a7a9a | 2578 | require_course_login($course, true, $cm); |
87c215de | 2579 | $this->page->set_context(get_context_instance(CONTEXT_MODULE, $cm->id)); |
3406acde | 2580 | $coursenode = $this->load_course($course); |
1aa1e9b5 SH |
2581 | if ($course->id == SITEID) { |
2582 | $modulenode = $this->load_activity($cm, $course, $coursenode->find($cm->id, self::TYPE_ACTIVITY)); | |
2583 | } else { | |
c78262b5 | 2584 | $sections = $this->load_course_sections($course, $coursenode); |
e26507b3 SH |
2585 | list($sectionarray, $activities) = $this->generate_sections_and_activities($course); |
2586 | $activities = $this->load_section_activities($sections[$cm->sectionnum]->sectionnode, $cm->sectionnum, $activities); | |
1aa1e9b5 SH |
2587 | $modulenode = $this->load_activity($cm, $course, $activities[$cm->id]); |
2588 | } | |
7d2a0492 | 2589 | break; |
507a7a9a | 2590 | default: |
3406acde | 2591 | throw new Exception('Unknown type'); |
507a7a9a | 2592 | return $this->expandable; |
7d2a0492 | 2593 | } |
588a3953 SH |
2594 | |
2595 | if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != SITEID) { | |
2596 | $this->load_for_user(null, true); | |
2597 | } | |
2598 | ||
507a7a9a | 2599 | $this->find_expandable($this->expandable); |
507a7a9a | 2600 | return $this->expandable; |
246a9b05 SH |
2601 | } |
2602 | ||
deaf04c7 SH |
2603 | /** |
2604 | * Returns an array of expandable nodes | |
2605 | * @return array | |
2606 | */ | |
246a9b05 SH |
2607 | public function get_expandable() { |
2608 | return $this->expandable; | |
7d2a0492 | 2609 | } |
7d2a0492 | 2610 | } |
2611 | ||
2612 | /** | |
2613 | * Navbar class | |
2614 | * | |
2615 | * This class is used to manage the navbar, which is initialised from the navigation | |
2616 | * object held by PAGE | |
2617 | * | |
31fde54f AG |
2618 | * @package core |
2619 | * @category navigation | |
7d2a0492 | 2620 | * @copyright 2009 Sam Hemelryk |
2621 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2622 | */ | |
2623 | class navbar extends navigation_node { | |
31fde54f | 2624 | /** @var bool A switch for whether the navbar is initialised or not */ |
7d2a0492 | 2625 | protected $initialised = false; |
31fde54f | 2626 | /** @var mixed keys used to reference the nodes on the navbar */ |
7d2a0492 | 2627 | protected $keys = array(); |
31fde54f | 2628 | /** @var null|string content of the navbar */ |
7d2a0492 | 2629 | protected $content = null; |
31fde54f | 2630 | /** @var moodle_page object the moodle page that this navbar belongs to */ |
7d2a0492 | 2631 | protected $page; |
31fde54f | 2632 | /** @var bool A switch for whether to ignore the active navigation information */ |
31759089 | 2633 | protected $ignoreactive = false; |
31fde54f | 2634 | /** @var bool A switch to let us know if we are in the middle of an install */ |
7d2a0492 | 2635 | protected $duringinstall = false; |
31fde54f | 2636 | /** @var bool A switch for whether the navbar has items */ |
7a7e209d | 2637 | protected $hasitems = false; |
31fde54f | 2638 | /** @var array An array of navigation nodes for the navbar */ |
3406acde | 2639 | protected $items; |
31fde54f | 2640 | /** @var array An array of child node objects */ |
3406acde | 2641 | public $children = array(); |
31fde54f | 2642 | /** @var bool A switch for whether we want to include the root node in the navbar */ |
4d5059d4 | 2643 | public $includesettingsbase = false; |
7d2a0492 | 2644 | /** |
2645 | * The almighty constructor | |
3406acde SH |
2646 | * |
2647 | * @param moodle_page $page | |
7d2a0492 | 2648 | */ |
3406acde | 2649 | public function __construct(moodle_page $page) { |
507a7a9a | 2650 | global $CFG; |
7d2a0492 | 2651 | if (during_initial_install()) { |
2652 | $this->duringinstall = true; | |
2653 | return false; | |
2654 | } | |
2655 | $this->page = $page; | |
2656 | $this->text = get_string('home'); | |
2657 | $this->shorttext = get_string('home'); | |
2658 | $this->action = new moodle_url($CFG->wwwroot); | |
2659 | $this->nodetype = self::NODETYPE_BRANCH; | |
2660 | $this->type = self::TYPE_SYSTEM; | |
2661 | } | |
2662 | ||
2663 | /** | |
2664 | * Quick check to see if the navbar will have items in. | |
2665 | * | |
2666 | * @return bool Returns true if the navbar will have items, false otherwise | |
2667 | */ | |
2668 | public function has_items() { | |
2669 | if ($this->duringinstall) { | |
2670 | return false; | |
7a7e209d SH |
2671 | } else if ($this->hasitems !== false) { |
2672 | return true; | |
7d2a0492 | 2673 | } |
3406acde | 2674 | $this->page->navigation->initialise($this->page); |
bf6c37c7 | 2675 | |
7a7e209d | 2676 | $activenodefound = ($this->page->navigation->contains_active_node() || |
3406acde | 2677 | $this->page->settingsnav->contains_active_node()); |
bf6c37c7 | 2678 | |
3406acde | 2679 | $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound)); |
7a7e209d | 2680 | $this->hasitems = $outcome; |
bf6c37c7 | 2681 | return $outcome; |
31759089 | 2682 | } |
2683 | ||
3406acde SH |
2684 | /** |
2685 | * Turn on/off ignore active | |
2686 | * | |
2687 | * @param bool $setting | |
2688 | */ | |
31759089 | 2689 | public function ignore_active($setting=true) { |
2690 | $this->ignoreactive = ($setting); | |
7d2a0492 | 2691 | } |
31fde54f AG |
2692 | |
2693 | /** | |
2694 | * Gets a navigation node | |
2695 | * | |
2696 | * @param string|int $key for referencing the navbar nodes | |
2697 | * @param int $type navigation_node::TYPE_* | |
2698 | * @return navigation_node|bool | |
2699 | */ | |
3406acde SH |
2700 | public function get($key, $type = null) { |
2701 | foreach ($this->children as &$child) { | |
2702 | if ($child->key === $key && ($type == null || $type == $child->type)) { | |
2703 | return $child; | |
2704 | } | |
2705 | } | |
2706 | return false; | |
2707 | } | |
7d2a0492 | 2708 | /** |
3406acde | 2709 | * Returns an array of navigation_node's that make up the navbar. |
435a512e | 2710 | * |
3406acde | 2711 | * @return array |
7d2a0492 | 2712 | */ |
3406acde SH |
2713 | public function get_items() { |
2714 | $items = array(); | |
7d2a0492 | 2715 | // Make sure that navigation is initialised |
7a7e209d | 2716 | if (!$this->has_items()) { |
3406acde | 2717 | return $items; |
7a7e209d | 2718 | } |
3406acde SH |
2719 | if ($this->items !== null) { |
2720 | return $this->items; | |
7d2a0492 | 2721 | } |
2722 | ||
3406acde SH |
2723 | if (count($this->children) > 0) { |
2724 | // Add the custom children | |
2725 | $items = array_reverse($this->children); | |
2726 | } | |
117bd748 | 2727 | |
3406acde SH |
2728 | $navigationactivenode = $this->page->navigation->find_active_node(); |
2729 | $settingsactivenode = $this->page->settingsnav->find_active_node(); | |
0b29477b | 2730 | |
7d2a0492 | 2731 | // Check if navigation contains the active node |
0b29477b | 2732 | if (!$this->ignoreactive) { |
435a512e | 2733 | |
3406acde | 2734 | if ($navigationactivenode && $settingsactivenode) { |
0b29477b | 2735 | // Parse a combined navigation tree |
3406acde SH |
2736 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
2737 | if (!$settingsactivenode->mainnavonly) { | |
2738 | $items[] = $settingsactivenode; | |
2739 | } | |
2740 | $settingsactivenode = $settingsactivenode->parent; | |
2741 | } | |
4d5059d4 SH |
2742 | if (!$this->includesettingsbase) { |
2743 | // Removes the first node from the settings (root node) from the list | |
2744 | array_pop($items); | |
2745 | } | |
3406acde SH |
2746 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
2747 | if (!$navigationactivenode->mainnavonly) { | |
2748 | $items[] = $navigationactivenode; | |
2749 | } | |
2750 | $navigationactivenode = $navigationactivenode->parent; | |
0b29477b | 2751 | } |
3406acde | 2752 | } else if ($navigationactivenode) { |
0b29477b | 2753 | // Parse the navigation tree to get the active node |
3406acde SH |
2754 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
2755 | if (!$navigationactivenode->mainnavonly) { | |
2756 | $items[] = $navigationactivenode; | |
2757 | } | |
2758 | $navigationactivenode = $navigationactivenode->parent; | |
2759 | } | |
2760 | } else if ($settingsactivenode) { | |
0b29477b | 2761 | // Parse the settings navigation to get the active node |
3406acde SH |
2762 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
2763 | if (!$settingsactivenode->mainnavonly) { | |
2764 | $items[] = $settingsactivenode; | |
2765 | } | |
2766 | $settingsactivenode = $settingsactivenode->parent; | |
2767 | } | |
0b29477b | 2768 | } |
7d2a0492 | 2769 | } |
a3bbac8b | 2770 | |
3406acde SH |
2771 | $items[] = new navigation_node(array( |
2772 | 'text'=>$this->page->navigation->text, | |
2773 | 'shorttext'=>$this->page->navigation->shorttext, | |
2774 | 'key'=>$this->page->navigation->key, | |
2775 | 'action'=>$this->page->navigation->action | |
2776 | )); | |
a3bbac8b | 2777 | |
3406acde SH |
2778 | $this->items = array_reverse($items); |
2779 | return $this->items; | |
7d2a0492 | 2780 | } |
507a7a9a | 2781 | |
7d2a0492 | 2782 | /** |
3406acde | 2783 | * Add a new navigation_node to the navbar, overrides parent::add |
7d2a0492 | 2784 | * |
2785 | * This function overrides {@link navigation_node::add()} so that we can change | |
2786 | * the way nodes get added to allow us to simply call add and have the node added to the | |
2787 | * end of the navbar | |
2788 | * | |
2789 | * @param string $text | |
7d2a0492 | 2790 | * @param string|moodle_url $action |
d972bad1 | 2791 | * @param int $type |
2792 | * @param string|int $key | |
2793 | * @param string $shorttext | |
7d2a0492 | 2794 | * @param string $icon |
3406acde | 2795 | * @return navigation_node |
7d2a0492 | 2796 | */ |
f9fc1461 | 2797 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { |
3406acde SH |
2798 | if ($this->content !== null) { |
2799 | debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); | |
2800 | } | |
435a512e | 2801 | |
3406acde SH |
2802 | // Properties array used when creating the new navigation node |
2803 | $itemarray = array( | |
2804 | 'text' => $text, | |
2805 | 'type' => $type | |
2806 | ); | |
2807 | // Set the action if one was provided | |
2808 | if ($action!==null) { | |
2809 | $itemarray['action'] = $action; | |
2810 | } | |
2811 | // Set the shorttext if one was provided | |
2812 | if ($shorttext!==null) { | |
2813 | $itemarray['shorttext'] = $shorttext; | |
2814 | } | |
2815 | // Set the icon if one was provided | |
2816 | if ($icon!==null) { | |
2817 | $itemarray['icon'] = $icon; | |
7d2a0492 | 2818 | } |
3406acde SH |
2819 | // Default the key to the number of children if not provided |
2820 | if ($key === null) { | |
2821 | $key = count($this->children); | |
7d2a0492 | 2822 | } |
3406acde SH |
2823 | // Set the key |
2824 | $itemarray['key'] = $key; | |
2825 | // Set the parent to this node | |
2826 | $itemarray['parent'] = $this; | |
2827 | // Add the child using the navigation_node_collections add method | |
2828 | $this->children[] = new navigation_node($itemarray); | |
2829 | return $this; | |
7d2a0492 | 2830 | } |
2831 | } | |
2832 | ||
2833 | /** | |
2834 | * Class used to manage the settings option for the current page | |
2835 | * | |
2836 | * This class is used to manage the settings options in a tree format (recursively) | |
2837 | * and was created initially for use with the settings blocks. | |
2838 | * | |
31fde54f AG |
2839 | * @package core |
2840 | * @category navigation | |
7d2a0492 | 2841 | * @copyright 2009 Sam Hemelryk |
2842 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2843 | */ | |
2844 | class settings_navigation extends navigation_node { | |
31fde54f | 2845 | /** @var stdClass the current context */ |
7d2a0492 | 2846 | protected $context; |
31fde54f | 2847 | /** @var moodle_page the moodle page that the navigation belongs to */ |
7d2a0492 | 2848 | protected $page; |
31fde54f | 2849 | /** @var string contains administration section navigation_nodes */ |
d9d2817a | 2850 | protected $adminsection; |
31fde54f | 2851 | /** @var bool A switch to see if the navigation node is initialised */ |
4766a50c | 2852 | protected $initialised = false; |
31fde54f | 2853 | /** @var array An array of users that the nodes can extend for. */ |
87c215de | 2854 | protected $userstoextendfor = array(); |
e26507b3 SH |
2855 | /** @var navigation_cache **/ |
2856 | protected $cache; | |
4766a50c | 2857 | |
7d2a0492 | 2858 | /** |
2859 | * Sets up the object with basic settings and preparse it for use | |
435a512e | 2860 | * |
3406acde | 2861 | * @param moodle_page $page |
7d2a0492 | 2862 | */ |
507a7a9a | 2863 | public function __construct(moodle_page &$page) { |
7d2a0492 | 2864 | if (during_initial_install()) { |
2865 | return false; | |
2866 | } | |
7d2a0492 | 2867 | $this->page = $page; |
2868 | // Initialise the main navigation. It is most important that this is done | |
2869 | // before we try anything | |
2870 | $this->page->navigation->initialise(); | |
2871 | // Initialise the navigation cache | |
f5b5a822 | 2872 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
3406acde | 2873 | $this->children = new navigation_node_collection(); |
7d2a0492 | 2874 | } |
2875 | /** | |
2876 | * Initialise the settings navigation based on the current context | |
2877 | * | |
2878 | * This function initialises the settings navigation tree for a given context | |
2879 | * by calling supporting functions to generate major parts of the tree. | |
3406acde | 2880 | * |
7d2a0492 | 2881 | */ |
2882 | public function initialise() { | |
7e90d3a4 | 2883 | global $DB, $SESSION; |
4f0c2d00 | 2884 | |
7d2a0492 | 2885 | if (during_initial_install()) { |
2886 | return false; | |
4766a50c SH |
2887 | } else if ($this->initialised) { |
2888 | return true; | |
7d2a0492 | 2889 | } |
2890 | $this->id = 'settingsnav'; | |
2891 | $this->context = $this->page->context; | |
0b29477b SH |
2892 | |
2893 | $context = $this->context; | |
2894 | if ($context->contextlevel == CONTEXT_BLOCK) { | |
2895 | $this->load_block_settings(); | |
e922fe23 | 2896 | $context = $context->get_parent_context(); |
0b29477b SH |
2897 | } |
2898 | ||
2899 | switch ($context->contextlevel) { | |
7d2a0492 | 2900 | case CONTEXT_SYSTEM: |
0b29477b SH |
2901 | if ($this->page->url->compare(new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings')))) { |
2902 | $this->load_front_page_settings(($context->id == $this->context->id)); | |
2903 | } | |
7d2a0492 | 2904 | break; |
2905 | case CONTEXT_COURSECAT: | |
0b29477b | 2906 | $this->load_category_settings(); |
7d2a0492 | 2907 | break; |
2908 | case CONTEXT_COURSE: | |
0b29477b SH |
2909 | if ($this->page->course->id != SITEID) { |
2910 | $this->load_course_settings(($context->id == $this->context->id)); | |
7d2a0492 | 2911 | } else { |
0b29477b | 2912 | $this->load_front_page_settings(($context->id == $this->context->id)); |
7d2a0492 | 2913 | } |
2914 | break; | |
2915 | case CONTEXT_MODULE: | |
0b29477b SH |
2916 | $this->load_module_settings(); |
2917 | $this->load_course_settings(); | |
7d2a0492 | 2918 | break; |
2919 | case CONTEXT_USER: | |
0b29477b SH |
2920 | if ($this->page->course->id != SITEID) { |
2921 | $this->load_course_settings(); | |
7d2a0492 | 2922 | } |
7d2a0492 | 2923 | break; |
2924 | } | |
2925 | ||
3406acde | 2926 | $settings = $this->load_user_settings($this->page->course->id); |
7e90d3a4 SH |
2927 | |
2928 | if (isloggedin() && !isguestuser() && (!property_exists($SESSION, 'load_navigation_admin') || $SESSION->load_navigation_admin)) { | |
2929 | $admin = $this->load_administration_settings(); | |
2930 | $SESSION->load_navigation_admin = ($admin->has_children()); | |
2931 | } else { | |
2932 | $admin = false; | |
2933 | } | |
0b29477b | 2934 | |
3406acde SH |
2935 | if ($context->contextlevel == CONTEXT_SYSTEM && $admin) { |
2936 | $admin->force_open(); | |
2937 | } else if ($context->contextlevel == CONTEXT_USER && $settings) { | |
2938 | $settings->force_open(); | |
0b29477b SH |
2939 | } |
2940 | ||
7d2a0492 | 2941 | // Check if the user is currently logged in as another user |
2942 | if (session_is_loggedinas()) { | |
2943 | // Get the actual user, we need this so we can display an informative return link | |
2944 | $realuser = session_get_realuser(); | |
2945 | // Add the informative return to original user link | |
a6855934 | 2946 | $url = new moodle_url('/course/loginas.php',array('id'=>$this->page->course->id, 'return'=>1,'sesskey'=>sesskey())); |
f9fc1461 | 2947 | $this->add(get_string('returntooriginaluser', 'moodle', fullname($realuser, true)), $url, self::TYPE_SETTING, null, null, new pix_icon('t/left', '')); |
7d2a0492 | 2948 | } |
2949 | ||
3406acde SH |
2950 | foreach ($this->children as $key=>$node) { |
2951 | if ($node->nodetype != self::NODETYPE_BRANCH || $node->children->count()===0) { | |
2952 | $node->remove(); | |
2953 | } | |
2954 | } | |
4766a50c | 2955 | $this->initialised = true; |
7d2a0492 | 2956 | } |
2957 | /** | |
2958 | * Override the parent function so that we can add preceeding hr's and set a | |
2959 | * root node class against all first level element | |
2960 | * | |
2961 | * It does this by first calling the parent's add method {@link navigation_node::add()} | |
117bd748 | 2962 | * and then proceeds to use the key to set class and hr |
7d2a0492 | 2963 | * |
31fde54f AG |
2964 | * @param string $text text to be used for the link. |
2965 | * @param string|moodle_url $url url for the new node | |
2966 | * @param int $type the type of node navigation_node::TYPE_* | |
7d2a0492 | 2967 | * @param string $shorttext |
31fde54f AG |
2968 | * @param string|int $key a key to access the node by. |
2969 | * @param pix_icon $icon An icon that appears next to the node. | |
2970 | * @return navigation_node with the new node added to it. | |
7d2a0492 | 2971 | */ |
f9fc1461 | 2972 | public function add($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { |
3406acde SH |
2973 | $node = parent::add($text, $url, $type, $shorttext, $key, $icon); |
2974 | $node->add_class('root_node'); | |
2975 | return $node; | |
7d2a0492 | 2976 | } |
a6e34701 | 2977 | |
2978 | /** | |
2979 | * This function allows the user to add something to the start of the settings | |
2980 | * navigation, which means it will be at the top of the settings navigation block | |
2981 | * | |
31fde54f AG |
2982 | * @param string $text text to be used for the link. |
2983 | * @param string|moodle_url $url url for the new node | |
2984 | * @param int $type the type of node navigation_node::TYPE_* | |
a6e34701 | 2985 | * @param string $shorttext |
31fde54f AG |
2986 | * @param string|int $key a key to access the node by. |
2987 | * @param pix_icon $icon An icon that appears next to the node. | |
2988 | * @return navigation_node $node with the new node added to it. | |
a6e34701 | 2989 | */ |
f9fc1461 | 2990 | public function prepend($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { |
a6e34701 | 2991 | $children = $this->children; |
b499db57 SH |
2992 | $childrenclass = get_class($children); |
2993 | $this->children = new $childrenclass; | |
3406acde SH |
2994 | $node = $this->add($text, $url, $type, $shorttext, $key, $icon); |
2995 | foreach ($children as $child) { | |
2996 | $this->children->add($child); | |
a6e34701 | 2997 | } |
3406acde | 2998 | return $node; |
a6e34701 | 2999 | } |
7d2a0492 | 3000 | /** |
3001 | * Load the site administration tree | |
3002 | * | |
3003 | * This function loads the site administration tree by using the lib/adminlib library functions | |
3004 | * | |
3406acde | 3005 | * @param navigation_node $referencebranch A reference to a branch in the settings |
7d2a0492 | 3006 | * navigation tree |
3406acde | 3007 | * @param part_of_admin_tree $adminbranch The branch to add, if null generate the admin |
7d2a0492 | 3008 | * tree and start at the beginning |
3009 | * @return mixed A key to access the admin tree by | |
3010 | */ | |
0e3eee62 | 3011 | protected function load_administration_settings(navigation_node $referencebranch=null, part_of_admin_tree $adminbranch=null) { |
507a7a9a | 3012 | global $CFG; |
0e3eee62 | 3013 | |
7d2a0492 | 3014 | // Check if we are just starting to generate this navigation. |
3015 | if ($referencebranch === null) { | |
0e3eee62 | 3016 | |
d9d2817a | 3017 | // Require the admin lib then get an admin structure |
0e3eee62 SH |
3018 | if (!function_exists('admin_get_root')) { |
3019 | require_once($CFG->dirroot.'/lib/adminlib.php'); | |
3020 | } | |
3021 | $adminroot = admin_get_root(false, false); | |
d9d2817a SH |
3022 | // This is the active section identifier |
3023 | $this->adminsection = $this->page->url->param('section'); | |
4f0c2d00 | 3024 | |
d9d2817a SH |
3025 | // Disable the navigation from automatically finding the active node |
3026 | navigation_node::$autofindactive = false; | |
3406acde | 3027 | $referencebranch = $this->add(get_string('administrationsite'), null, self::TYPE_SETTING, null, 'root'); |
0e3eee62 SH |
3028 | foreach ($adminroot->children as $adminbranch) { |
3029 | $this->load_administration_settings($referencebranch, $adminbranch); | |
3030 | } | |
d9d2817a | 3031 | navigation_node::$autofindactive = true; |
0e3eee62 | 3032 | |
d9d2817a | 3033 | // Use the admin structure to locate the active page |
3406acde SH |
3034 | if (!$this->contains_active_node() && $current = $adminroot->locate($this->adminsection, true)) { |
3035 | $currentnode = $this; | |
3036 | while (($pathkey = array_pop($current->path))!==null && $currentnode) { | |
3037 | $currentnode = $currentnode->get($pathkey); | |
3038 | } | |
3039 | if ($currentnode) { | |
3040 | $currentnode->make_active(); | |
7d2a0492 | 3041 | } |
25b550d2 SH |
3042 | } else { |
3043 | $this->scan_for_active_node($referencebranch); | |
0e3eee62 | 3044 | } |
3406acde | 3045 | return $referencebranch; |
8140c440 | 3046 | } else if ($adminbranch->check_access()) { |
7d2a0492 | 3047 | // We have a reference branch that we can access and is not hidden `hurrah` |
3048 | // Now we need to display it and any children it may have | |
3049 | $url = null; | |
3050 | $icon = null; | |
3051 | if ($adminbranch instanceof admin_settingpage) { | |
83f78f8d | 3052 | $url = new moodle_url('/'.$CFG->admin.'/settings.php', array('section'=>$adminbranch->name)); |
7d2a0492 | 3053 | } else if ($adminbranch instanceof admin_externalpage) { |
3054 | $url = $adminbranch->url; | |
83f78f8d SH |
3055 | } else if (!empty($CFG->linkadmincategories) && $adminbranch instanceof admin_category) { |
3056 | $url = new moodle_url('/'.$CFG->admin.'/category.php', array('category' => $adminbranch->name)); | |
7d2a0492 | 3057 | } |
3058 | ||
3059 | // Add the branch | |
3406acde | 3060 | $reference = $referencebranch->add($adminbranch->visiblename, $url, self::TYPE_SETTING, null, $adminbranch->name, $icon); |
8140c440 | 3061 | |
3062 | if ($adminbranch->is_hidden()) { | |
d9d2817a SH |
3063 | if (($adminbranch instanceof admin_externalpage || $adminbranch instanceof admin_settingpage) && $adminbranch->name == $this->adminsection) { |
3064 | $reference->add_class('hidden'); | |
3065 | } else { | |
3066 | $reference->display = false; | |
3067 | } | |
8140c440 | 3068 | } |
3069 | ||
7d2a0492 | 3070 | // Check if we are generating the admin notifications and whether notificiations exist |
3071 | if ($adminbranch->name === 'adminnotifications' && admin_critical_warnings_present()) { | |
3072 | $reference->add_class('criticalnotification'); | |
3073 | } | |
3074 | // Check if this branch has children | |
3075 | if ($reference && isset($adminbranch->children) && is_array($adminbranch->children) && count($adminbranch->children)>0) { | |
3076 | foreach ($adminbranch->children as $branch) { | |
3077 | // Generate the child branches as well now using this branch as the reference | |
459e384e | 3078 | $this->load_administration_settings($reference, $branch); |
7d2a0492 | 3079 | } |
3080 | } else { | |
f9fc1461 | 3081 | $reference->icon = new pix_icon('i/settings', ''); |
7d2a0492 | 3082 | } |
3083 | } | |
3084 | } | |
3085 | ||
25b550d2 SH |
3086 | /** |
3087 | * This function recursivily scans nodes until it finds the active node or there | |
3088 | * are no more nodes. | |
3089 | * @param navigation_node $node | |
3090 | */ | |
3091 | protected function scan_for_active_node(navigation_node $node) { | |
3092 | if (!$node->check_if_active() && $node->children->count()>0) { | |
3093 | foreach ($node->children as &$child) { | |
3094 | $this->scan_for_active_node($child); | |
3095 | } | |
3096 | } | |
3097 | } | |
3098 | ||
3406acde SH |
3099 | /** |
3100 | * Gets a navigation node given an array of keys that represent the path to | |
3101 | * the desired node. | |
3102 | * | |
3103 | * @param array $path | |
3104 | * @return navigation_node|false | |
3105 | */ | |
3106 | protected function get_by_path(array $path) { | |
3107 | $node = $this->get(array_shift($path)); | |
3108 | foreach ($path as $key) { | |
3109 | $node->get($key); | |
3110 | } | |
3111 | return $node; | |
3112 | } | |
3113 |