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 */ |
fe221dd5 | 937 | protected $showemptysections = true; |
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(); |
8e8de15f SH |
946 | /** @var bool */ |
947 | protected $allcategoriesloaded = false; | |
31fde54f | 948 | /** @var array An array of category ids that are included in the navigation */ |
e26507b3 | 949 | protected $addedcategories = array(); |
31fde54f | 950 | /** @var int expansion limit */ |
88f77c3c | 951 | protected $expansionlimit = 0; |
31fde54f | 952 | /** @var int userid to allow parent to see child's profile page navigation */ |
870815fa | 953 | protected $useridtouseforparentchecks = 0; |
88f77c3c | 954 | |
8e8de15f SH |
955 | /** Used when loading categories to load all top level categories [parent = 0] **/ |
956 | const LOAD_ROOT_CATEGORIES = 0; | |
957 | /** Used when loading categories to load all categories **/ | |
958 | const LOAD_ALL_CATEGORIES = -1; | |
959 | ||
7d2a0492 | 960 | /** |
3406acde SH |
961 | * Constructs a new global navigation |
962 | * | |
3406acde | 963 | * @param moodle_page $page The page this navigation object belongs to |
7d2a0492 | 964 | */ |
3406acde | 965 | public function __construct(moodle_page $page) { |
4766a50c | 966 | global $CFG, $SITE, $USER; |
3406acde | 967 | |
7d2a0492 | 968 | if (during_initial_install()) { |
3406acde | 969 | return; |
7d2a0492 | 970 | } |
3406acde | 971 | |
4766a50c SH |
972 | if (get_home_page() == HOMEPAGE_SITE) { |
973 | // We are using the site home for the root element | |
974 | $properties = array( | |
975 | 'key' => 'home', | |
976 | 'type' => navigation_node::TYPE_SYSTEM, | |
977 | 'text' => get_string('home'), | |
978 | 'action' => new moodle_url('/') | |
979 | ); | |
980 | } else { | |
981 | // We are using the users my moodle for the root element | |
982 | $properties = array( | |
983 | 'key' => 'myhome', | |
984 | 'type' => navigation_node::TYPE_SYSTEM, | |
985 | 'text' => get_string('myhome'), | |
986 | 'action' => new moodle_url('/my/') | |
987 | ); | |
dd8e5011 | 988 | } |
4766a50c | 989 | |
31fde54f | 990 | // Use the parents constructor.... good good reuse |
3406acde SH |
991 | parent::__construct($properties); |
992 | ||
993 | // Initalise and set defaults | |
994 | $this->page = $page; | |
7d2a0492 | 995 | $this->forceopen = true; |
f5b5a822 | 996 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
7d2a0492 | 997 | } |
998 | ||
b9bcad24 AB |
999 | /** |
1000 | * Mutator to set userid to allow parent to see child's profile | |
1001 | * page navigation. See MDL-25805 for initial issue. Linked to it | |
1002 | * is an issue explaining why this is a REALLY UGLY HACK thats not | |
1003 | * for you to use! | |
1004 | * | |
5607036a | 1005 | * @param int $userid userid of profile page that parent wants to navigate around. |
b9bcad24 | 1006 | */ |
870815fa SH |
1007 | public function set_userid_for_parent_checks($userid) { |
1008 | $this->useridtouseforparentchecks = $userid; | |
b9bcad24 AB |
1009 | } |
1010 | ||
1011 | ||
7d2a0492 | 1012 | /** |
3406acde | 1013 | * Initialises the navigation object. |
7d2a0492 | 1014 | * |
3406acde SH |
1015 | * This causes the navigation object to look at the current state of the page |
1016 | * that it is associated with and then load the appropriate content. | |
7d2a0492 | 1017 | * |
3406acde SH |
1018 | * This should only occur the first time that the navigation structure is utilised |
1019 | * which will normally be either when the navbar is called to be displayed or | |
1020 | * when a block makes use of it. | |
7d2a0492 | 1021 | * |
3406acde | 1022 | * @return bool |
7d2a0492 | 1023 | */ |
3406acde | 1024 | public function initialise() { |
4766a50c | 1025 | global $CFG, $SITE, $USER, $DB; |
3406acde | 1026 | // Check if it has alread been initialised |
7d2a0492 | 1027 | if ($this->initialised || during_initial_install()) { |
1028 | return true; | |
1029 | } | |
e2b436d0 | 1030 | $this->initialised = true; |
3406acde SH |
1031 | |
1032 | // Set up the five base root nodes. These are nodes where we will put our | |
1033 | // content and are as follows: | |
1034 | // site: Navigation for the front page. | |
1035 | // myprofile: User profile information goes here. | |
1036 | // mycourses: The users courses get added here. | |
1037 | // courses: Additional courses are added here. | |
1038 | // users: Other users information loaded here. | |
1039 | $this->rootnodes = array(); | |
4766a50c | 1040 | if (get_home_page() == HOMEPAGE_SITE) { |
3f9ccf85 | 1041 | // The home element should be my moodle because the root element is the site |
b9d4c7d3 | 1042 | if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in |
e26507b3 | 1043 | $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_SETTING, null, 'home'); |
3f9ccf85 | 1044 | } |
4766a50c SH |
1045 | } else { |
1046 | // The home element should be the site because the root node is my moodle | |
e26507b3 | 1047 | $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), self::TYPE_SETTING, null, 'home'); |
ab6ec58a | 1048 | if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) { |
4766a50c SH |
1049 | // We need to stop automatic redirection |
1050 | $this->rootnodes['home']->action->param('redirect', '0'); | |
1051 | } | |
1052 | } | |
3406acde | 1053 | $this->rootnodes['site'] = $this->add_course($SITE); |
e26507b3 | 1054 | $this->rootnodes['myprofile'] = $this->add(get_string('myprofile'), null, self::TYPE_USER, null, 'myprofile'); |
3406acde | 1055 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses'); |
83f78f8d | 1056 | $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); |
3406acde SH |
1057 | $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); |
1058 | ||
1059 | // Fetch all of the users courses. | |
8e8de15f | 1060 | $mycourses = enrol_get_my_courses(); |
8e8de15f SH |
1061 | // We need to show categories if we can show categories and the user isn't enrolled in any courses or we're not showing all courses |
1062 | $showcategories = ($this->show_categories() && (count($mycourses) == 0 || !empty($CFG->navshowallcourses))); | |
1063 | // $issite gets set to true if the current pages course is the sites frontpage course | |
56c062a4 | 1064 | $issite = ($this->page->course->id == SITEID); |
8e8de15f | 1065 | // $ismycourse gets set to true if the user is enrolled in the current pages course. |
e26507b3 | 1066 | $ismycourse = (array_key_exists($this->page->course->id, $mycourses)); |
ba2789c1 | 1067 | |
3406acde | 1068 | // Check if any courses were returned. |
e26507b3 | 1069 | if (count($mycourses) > 0) { |
b0712163 SH |
1070 | |
1071 | // Check if categories should be displayed within the my courses branch | |
1072 | if (!empty($CFG->navshowmycoursecategories)) { | |
1073 | ||
1074 | // Find the category of each mycourse | |
1075 | $categories = array(); | |
1076 | foreach ($mycourses as $course) { | |
1077 | $categories[] = $course->category; | |
1078 | } | |
1079 | ||
1080 | // Do a single DB query to get the categories immediately associated with | |
1081 | // courses the user is enrolled in. | |
1082 | $categories = $DB->get_records_list('course_categories', 'id', array_unique($categories), 'depth ASC, sortorder ASC'); | |
1083 | // Work out the parent categories that we need to load that we havn't | |
1084 | // already got. | |
1085 | $categoryids = array(); | |
1086 | foreach ($categories as $category) { | |
1087 | $categoryids = array_merge($categoryids, explode('/', trim($category->path, '/'))); | |
1088 | } | |
1089 | $categoryids = array_unique($categoryids); | |
1090 | $categoryids = array_diff($categoryids, array_keys($categories)); | |
1091 | ||
1092 | if (count($categoryids)) { | |
1093 | // Fetch any other categories we need. | |
aad30b6a SH |
1094 | $allcategories = $DB->get_records_list('course_categories', 'id', $categoryids, 'depth ASC, sortorder ASC'); |
1095 | if (is_array($allcategories) && count($allcategories) > 0) { | |
bc40124b | 1096 | $categories = array_merge($categories, $allcategories); |
aad30b6a | 1097 | } |
b0712163 SH |
1098 | } |
1099 | ||
1100 | // We ONLY want the categories, we need to get rid of the keys | |
1101 | $categories = array_values($categories); | |
1102 | $addedcategories = array(); | |
1103 | while (($category = array_shift($categories)) !== null) { | |
1104 | if ($category->parent == '0') { | |
1105 | $categoryparent = $this->rootnodes['mycourses']; | |
1106 | } else if (array_key_exists($category->parent, $addedcategories)) { | |
1107 | $categoryparent = $addedcategories[$category->parent]; | |
1108 | } else { | |
1109 | // Prepare to count iterations. We don't want to loop forever | |
1110 | // accidentally if for some reason a category can't be placed. | |
1111 | if (!isset($category->loopcount)) { | |
1112 | $category->loopcount = 0; | |
1113 | } | |
1114 | $category->loopcount++; | |
1115 | if ($category->loopcount > 5) { | |
1116 | // This is a pretty serious problem and this should never happen. | |
1117 | // If it does then for some reason a category has been loaded but | |
1118 | // its parents have now. It could be data corruption. | |
1119 | debugging('Category '.$category->id.' could not be placed within the navigation', DEBUG_DEVELOPER); | |
1120 | } else { | |
1121 | // Add it back to the end of the categories array | |
1122 | array_push($categories, $category); | |
1123 | } | |
1124 | continue; | |
1125 | } | |
1126 | ||
1127 | $url = new moodle_url('/course/category.php', array('id' => $category->id)); | |
1128 | $addedcategories[$category->id] = $categoryparent->add($category->name, $url, self::TYPE_CATEGORY, $category->name, $category->id); | |
1129 | ||
1130 | if (!$category->visible) { | |
1131 | if (!has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_COURSECAT, $category->parent))) { | |
1132 | $addedcategories[$category->id]->display = false; | |
1133 | } else { | |
1134 | $addedcategories[$category->id]->hidden = true; | |
1135 | } | |
1136 | } | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | // Add all of the users courses to the navigation. | |
8e8de15f | 1141 | // First up we need to add to the mycourses section. |
e26507b3 SH |
1142 | foreach ($mycourses as $course) { |
1143 | $course->coursenode = $this->add_course($course, false, true); | |
3406acde | 1144 | } |
4766a50c | 1145 | |
ee03fe79 | 1146 | if (!empty($CFG->navshowallcourses)) { |
8e8de15f SH |
1147 | // Load all courses |
1148 | $this->load_all_courses(); | |
1149 | } | |
1150 | ||
1151 | // Next if nasvshowallcourses is enabled then we need to add courses | |
1152 | // to the courses branch as well. | |
1153 | if (!empty($CFG->navshowallcourses)) { | |
1154 | foreach ($mycourses as $course) { | |
1155 | if (!empty($course->category) && !$this->can_add_more_courses_to_category($course->category)) { | |
1156 | continue; | |
1157 | } | |
1158 | $genericcoursenode = $this->add_course($course, true); | |
1159 | if ($genericcoursenode->isactive) { | |
1160 | // We don't want this node to be active because we want the | |
1161 | // node in the mycourses branch to be active. | |
1162 | $genericcoursenode->make_inactive(); | |
1163 | $genericcoursenode->collapse = true; | |
1164 | if ($genericcoursenode->parent && $genericcoursenode->parent->type == self::TYPE_CATEGORY) { | |
1165 | $parent = $genericcoursenode->parent; | |
1166 | while ($parent && $parent->type == self::TYPE_CATEGORY) { | |
1167 | $parent->collapse = true; | |
1168 | $parent = $parent->parent; | |
1169 | } | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | } | |
ee03fe79 | 1174 | } else if (!empty($CFG->navshowallcourses) || !$this->show_categories()) { |
ba2789c1 SH |
1175 | // Load all courses |
1176 | $this->load_all_courses(); | |
3406acde SH |
1177 | } |
1178 | ||
14d35a26 SH |
1179 | // We always load the frontpage course to ensure it is available without |
1180 | // JavaScript enabled. | |
1181 | $frontpagecourse = $this->load_course($SITE); | |
1182 | $this->add_front_page_course_essentials($frontpagecourse, $SITE); | |
56c062a4 | 1183 | $this->load_course_sections($SITE, $frontpagecourse); |
14d35a26 | 1184 | |
cede87e0 SH |
1185 | $canviewcourseprofile = true; |
1186 | ||
6a16e136 SH |
1187 | // Next load context specific content into the navigation |
1188 | switch ($this->page->context->contextlevel) { | |
1189 | case CONTEXT_SYSTEM : | |
1190 | // This has already been loaded we just need to map the variable | |
8e8de15f SH |
1191 | if ($this->show_categories()) { |
1192 | $this->load_all_categories(self::LOAD_ROOT_CATEGORIES, $showcategories); | |
1193 | } | |
6a16e136 SH |
1194 | break; |
1195 | case CONTEXT_COURSECAT : | |
1196 | // This has already been loaded we just need to map the variable | |
8e8de15f SH |
1197 | if ($this->show_categories()) { |
1198 | $this->load_all_categories($this->page->context->instanceid, $showcategories); | |
6a16e136 SH |
1199 | } |
1200 | break; | |
1201 | case CONTEXT_BLOCK : | |
1202 | case CONTEXT_COURSE : | |
1203 | if ($issite) { | |
1204 | // If it is the front page course, or a block on it then | |
1205 | // everything has already been loaded. | |
56c062a4 | 1206 | break; |
6a16e136 SH |
1207 | } |
1208 | // Load the course associated with the page into the navigation | |
1209 | $course = $this->page->course; | |
8e8de15f | 1210 | if ($this->show_categories() && !$ismycourse) { |
6a16e136 SH |
1211 | $this->load_all_categories($course->category, $showcategories); |
1212 | } | |
1213 | $coursenode = $this->load_course($course); | |
b9bcad24 | 1214 | |
6a16e136 SH |
1215 | // If the course wasn't added then don't try going any further. |
1216 | if (!$coursenode) { | |
1217 | $canviewcourseprofile = false; | |
1218 | break; | |
1219 | } | |
e26507b3 | 1220 | |
6a16e136 SH |
1221 | // If the user is not enrolled then we only want to show the |
1222 | // course node and not populate it. | |
1223 | ||
1224 | // Not enrolled, can't view, and hasn't switched roles | |
1225 | if (!can_access_course($course)) { | |
1226 | // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in, | |
1227 | // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) | |
1228 | $isparent = false; | |
1229 | if ($this->useridtouseforparentchecks) { | |
1230 | if ($this->useridtouseforparentchecks != $USER->id) { | |
1231 | $usercontext = get_context_instance(CONTEXT_USER, $this->useridtouseforparentchecks, MUST_EXIST); | |
1232 | if ($DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id)) | |
1233 | and has_capability('moodle/user:viewdetails', $usercontext)) { | |
1234 | $isparent = true; | |
56c062a4 SH |
1235 | } |
1236 | } | |
56c062a4 | 1237 | } |
6a16e136 SH |
1238 | |
1239 | if (!$isparent) { | |
56c062a4 | 1240 | $coursenode->make_active(); |
6a16e136 SH |
1241 | $canviewcourseprofile = false; |
1242 | break; | |
1243 | } | |
1244 | } | |
1245 | // Add the essentials such as reports etc... | |
1246 | $this->add_course_essentials($coursenode, $course); | |
1247 | if ($this->format_display_course_content($course->format)) { | |
1248 | // Load the course sections | |
1249 | $sections = $this->load_course_sections($course, $coursenode); | |
1250 | } | |
1251 | if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { | |
1252 | $coursenode->make_active(); | |
1253 | } | |
1254 | break; | |
1255 | case CONTEXT_MODULE : | |
1256 | if ($issite) { | |
1257 | // If this is the site course then most information will have | |
1258 | // already been loaded. | |
1259 | // However we need to check if there is more content that can | |
1260 | // yet be loaded for the specific module instance. | |
1261 | $activitynode = $this->rootnodes['site']->get($this->page->cm->id, navigation_node::TYPE_ACTIVITY); | |
1262 | if ($activitynode) { | |
1263 | $this->load_activity($this->page->cm, $this->page->course, $activitynode); | |
56c062a4 | 1264 | } |
2027c10e | 1265 | break; |
6a16e136 | 1266 | } |
2027c10e | 1267 | |
6a16e136 SH |
1268 | $course = $this->page->course; |
1269 | $cm = $this->page->cm; | |
cede87e0 | 1270 | |
8e8de15f | 1271 | if ($this->show_categories() && !$ismycourse) { |
6a16e136 SH |
1272 | $this->load_all_categories($course->category, $showcategories); |
1273 | } | |
56c062a4 | 1274 | |
6a16e136 SH |
1275 | // Load the course associated with the page into the navigation |
1276 | $coursenode = $this->load_course($course); | |
56c062a4 | 1277 | |
6a16e136 SH |
1278 | // If the course wasn't added then don't try going any further. |
1279 | if (!$coursenode) { | |
1280 | $canviewcourseprofile = false; | |
1281 | break; | |
1282 | } | |
1283 | ||
1284 | // If the user is not enrolled then we only want to show the | |
1285 | // course node and not populate it. | |
1286 | if (!can_access_course($course)) { | |
1287 | $coursenode->make_active(); | |
1288 | $canviewcourseprofile = false; | |
1289 | break; | |
1290 | } | |
56c062a4 | 1291 | |
6a16e136 | 1292 | $this->add_course_essentials($coursenode, $course); |
56c062a4 | 1293 | |
6a16e136 SH |
1294 | // Get section number from $cm (if provided) - we need this |
1295 | // before loading sections in order to tell it to load this section | |
1296 | // even if it would not normally display (=> it contains only | |
1297 | // a label, which we are now editing) | |
1298 | $sectionnum = isset($cm->sectionnum) ? $cm->sectionnum : 0; | |
1299 | if ($sectionnum) { | |
1300 | // This value has to be stored in a member variable because | |
1301 | // otherwise we would have to pass it through a public API | |
1302 | // to course formats and they would need to change their | |
1303 | // functions to pass it along again... | |
1304 | $this->includesectionnum = $sectionnum; | |
1305 | } else { | |
1306 | $this->includesectionnum = false; | |
1307 | } | |
d67e4821 | 1308 | |
6a16e136 SH |
1309 | // Load the course sections into the page |
1310 | $sections = $this->load_course_sections($course, $coursenode); | |
1311 | if ($course->id != SITEID) { | |
1312 | // Find the section for the $CM associated with the page and collect | |
1313 | // its section number. | |
d67e4821 | 1314 | if ($sectionnum) { |
6a16e136 | 1315 | $cm->sectionnumber = $sectionnum; |
d67e4821 | 1316 | } else { |
6a16e136 SH |
1317 | foreach ($sections as $section) { |
1318 | if ($section->id == $cm->section) { | |
1319 | $cm->sectionnumber = $section->section; | |
1320 | break; | |
0d8b6a69 | 1321 | } |
3406acde | 1322 | } |
6a16e136 | 1323 | } |
3406acde | 1324 | |
6a16e136 SH |
1325 | // Load all of the section activities for the section the cm belongs to. |
1326 | if (isset($cm->sectionnumber) and !empty($sections[$cm->sectionnumber])) { | |
1327 | list($sectionarray, $activityarray) = $this->generate_sections_and_activities($course); | |
1328 | $activities = $this->load_section_activities($sections[$cm->sectionnumber]->sectionnode, $cm->sectionnumber, $activityarray); | |
2a62743c PS |
1329 | } else { |
1330 | $activities = array(); | |
6a16e136 SH |
1331 | if ($activity = $this->load_stealth_activity($coursenode, get_fast_modinfo($course))) { |
1332 | // "stealth" activity from unavailable section | |
1333 | $activities[$cm->id] = $activity; | |
56c062a4 | 1334 | } |
2a62743c | 1335 | } |
6a16e136 SH |
1336 | } else { |
1337 | $activities = array(); | |
1338 | $activities[$cm->id] = $coursenode->get($cm->id, navigation_node::TYPE_ACTIVITY); | |
1339 | } | |
1340 | if (!empty($activities[$cm->id])) { | |
1341 | // Finally load the cm specific navigaton information | |
1342 | $this->load_activity($cm, $course, $activities[$cm->id]); | |
1343 | // Check if we have an active ndoe | |
1344 | if (!$activities[$cm->id]->contains_active_node() && !$activities[$cm->id]->search_for_active_node()) { | |
1345 | // And make the activity node active. | |
1346 | $activities[$cm->id]->make_active(); | |
e26507b3 | 1347 | } |
6a16e136 SH |
1348 | } else { |
1349 | //TODO: something is wrong, what to do? (Skodak) | |
1350 | } | |
1351 | break; | |
1352 | case CONTEXT_USER : | |
1353 | if ($issite) { | |
1354 | // The users profile information etc is already loaded | |
1355 | // for the front page. | |
96e78552 | 1356 | break; |
6a16e136 SH |
1357 | } |
1358 | $course = $this->page->course; | |
8e8de15f | 1359 | if ($this->show_categories() && !$ismycourse) { |
6a16e136 SH |
1360 | $this->load_all_categories($course->category, $showcategories); |
1361 | } | |
1362 | // Load the course associated with the user into the navigation | |
1363 | $coursenode = $this->load_course($course); | |
2027c10e | 1364 | |
6a16e136 SH |
1365 | // If the course wasn't added then don't try going any further. |
1366 | if (!$coursenode) { | |
1367 | $canviewcourseprofile = false; | |
1368 | break; | |
1369 | } | |
2027c10e | 1370 | |
6a16e136 SH |
1371 | // If the user is not enrolled then we only want to show the |
1372 | // course node and not populate it. | |
1373 | if (!can_access_course($course)) { | |
1374 | $coursenode->make_active(); | |
1375 | $canviewcourseprofile = false; | |
56c062a4 | 1376 | break; |
e52a5d3a | 1377 | } |
6a16e136 SH |
1378 | $this->add_course_essentials($coursenode, $course); |
1379 | $sections = $this->load_course_sections($course, $coursenode); | |
1380 | break; | |
7d2a0492 | 1381 | } |
7a7e209d | 1382 | |
ba2789c1 SH |
1383 | if ($showcategories) { |
1384 | $categories = $this->find_all_of_type(self::TYPE_CATEGORY); | |
8e8de15f SH |
1385 | if (count($categories) !== 0) { |
1386 | $categoryids = array(); | |
1387 | foreach ($categories as $category) { | |
1388 | $categoryids[] = $category->key; | |
1389 | } | |
1390 | list($categoriessql, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED); | |
1391 | $params['limit'] = (!empty($CFG->navcourselimit))?$CFG->navcourselimit:20; | |
1392 | $sql = "SELECT cc.id, COUNT(c.id) AS coursecount | |
1393 | FROM {course_categories} cc | |
1394 | JOIN {course} c ON c.category = cc.id | |
1395 | WHERE cc.id {$categoriessql} | |
1396 | GROUP BY cc.id | |
1397 | HAVING COUNT(c.id) > :limit"; | |
1398 | $excessivecategories = $DB->get_records_sql($sql, $params); | |
1399 | foreach ($categories as &$category) { | |
1400 | if (array_key_exists($category->key, $excessivecategories) && !$this->can_add_more_courses_to_category($category)) { | |
1401 | $url = new moodle_url('/course/category.php', array('id'=>$category->key)); | |
1402 | $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); | |
1403 | } | |
ba2789c1 SH |
1404 | } |
1405 | } | |
8e8de15f | 1406 | } else if ((!empty($CFG->navshowallcourses) || empty($mycourses)) && !$this->can_add_more_courses_to_category($this->rootnodes['courses'])) { |
ba2789c1 SH |
1407 | $this->rootnodes['courses']->add(get_string('viewallcoursescategories'), new moodle_url('/course/index.php'), self::TYPE_SETTING); |
1408 | } | |
1409 | ||
3406acde SH |
1410 | // Load for the current user |
1411 | $this->load_for_user(); | |
cede87e0 | 1412 | if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != SITEID && $canviewcourseprofile) { |
87c215de SH |
1413 | $this->load_for_user(null, true); |
1414 | } | |
7a7e209d SH |
1415 | // Load each extending user into the navigation. |
1416 | foreach ($this->extendforuser as $user) { | |
44303ca6 | 1417 | if ($user->id != $USER->id) { |
7a7e209d SH |
1418 | $this->load_for_user($user); |
1419 | } | |
1420 | } | |
7a7e209d | 1421 | |
a683da3c SH |
1422 | // Give the local plugins a chance to include some navigation if they want. |
1423 | foreach (get_list_of_plugins('local') as $plugin) { | |
1424 | if (!file_exists($CFG->dirroot.'/local/'.$plugin.'/lib.php')) { | |
1425 | continue; | |
1426 | } | |
1427 | require_once($CFG->dirroot.'/local/'.$plugin.'/lib.php'); | |
1428 | $function = $plugin.'_extends_navigation'; | |
1429 | if (function_exists($function)) { | |
1430 | $function($this); | |
1431 | } | |
1432 | } | |
1433 | ||
3406acde SH |
1434 | // Remove any empty root nodes |
1435 | foreach ($this->rootnodes as $node) { | |
4766a50c SH |
1436 | // Dont remove the home node |
1437 | if ($node->key !== 'home' && !$node->has_children()) { | |
3406acde SH |
1438 | $node->remove(); |
1439 | } | |
1440 | } | |
1441 | ||
7c4efe3b SH |
1442 | if (!$this->contains_active_node()) { |
1443 | $this->search_for_active_node(); | |
1444 | } | |
1445 | ||
3406acde | 1446 | // If the user is not logged in modify the navigation structure as detailed |
728ebac7 | 1447 | // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} |
3406acde SH |
1448 | if (!isloggedin()) { |
1449 | $activities = clone($this->rootnodes['site']->children); | |
1450 | $this->rootnodes['site']->remove(); | |
1451 | $children = clone($this->children); | |
1452 | $this->children = new navigation_node_collection(); | |
1453 | foreach ($activities as $child) { | |
1454 | $this->children->add($child); | |
1455 | } | |
1456 | foreach ($children as $child) { | |
1457 | $this->children->add($child); | |
1458 | } | |
3406acde | 1459 | } |
7d2a0492 | 1460 | return true; |
1461 | } | |
9bf5af21 SH |
1462 | |
1463 | /** | |
31fde54f | 1464 | * Returns true if courses should be shown within categories on the navigation. |
9bf5af21 SH |
1465 | * |
1466 | * @return bool | |
1467 | */ | |
1468 | protected function show_categories() { | |
1469 | global $CFG, $DB; | |
1470 | if ($this->showcategories === null) { | |
6a16e136 SH |
1471 | $show = $this->page->context->contextlevel == CONTEXT_COURSECAT; |
1472 | $show = $show || (!empty($CFG->navshowcategories) && $DB->count_records('course_categories') > 1); | |
1473 | $this->showcategories = $show; | |
9bf5af21 SH |
1474 | } |
1475 | return $this->showcategories; | |
1476 | } | |
1477 | ||
7d2a0492 | 1478 | /** |
3406acde SH |
1479 | * Checks the course format to see whether it wants the navigation to load |
1480 | * additional information for the course. | |
1481 | * | |
1482 | * This function utilises a callback that can exist within the course format lib.php file | |
1483 | * The callback should be a function called: | |
1484 | * callback_{formatname}_display_content() | |
1485 | * It doesn't get any arguments and should return true if additional content is | |
1486 | * desired. If the callback doesn't exist we assume additional content is wanted. | |
1487 | * | |
3406acde SH |
1488 | * @param string $format The course format |
1489 | * @return bool | |
1490 | */ | |
1491 | protected function format_display_course_content($format) { | |
1492 | global $CFG; | |
1493 | $formatlib = $CFG->dirroot.'/course/format/'.$format.'/lib.php'; | |
1494 | if (file_exists($formatlib)) { | |
1495 | require_once($formatlib); | |
1496 | $displayfunc = 'callback_'.$format.'_display_content'; | |
1497 | if (function_exists($displayfunc) && !$displayfunc()) { | |
1498 | return $displayfunc(); | |
1499 | } | |
1500 | } | |
1501 | return true; | |
1502 | } | |
1503 | ||
1504 | /** | |
31fde54f | 1505 | * Loads the courses in Moodle into the navigation. |
3406acde | 1506 | * |
8e8de15f SH |
1507 | * @global moodle_database $DB |
1508 | * @param string|array $categoryids An array containing categories to load courses | |
1509 | * for, OR null to load courses for all categories. | |
1510 | * @return array An array of navigation_nodes one for each course | |
3406acde | 1511 | */ |
8e8de15f SH |
1512 | protected function load_all_courses($categoryids = null) { |
1513 | global $CFG, $DB; | |
4766a50c | 1514 | |
8e8de15f | 1515 | // Work out the limit of courses. |
4766a50c SH |
1516 | $limit = 20; |
1517 | if (!empty($CFG->navcourselimit)) { | |
1518 | $limit = $CFG->navcourselimit; | |
1519 | } | |
4766a50c | 1520 | |
8e8de15f SH |
1521 | // Work out the key to use for caching. |
1522 | if (is_array($categoryids)) { | |
1523 | $cachekey = sprintf('load_all_courses_%d_%s', $limit, md5(join('_', $categoryids))); | |
1524 | } else if (is_string($categoryids) || is_int($categoryids)) { | |
1525 | $cachekey = sprintf('load_all_courses_%d_%s', $limit, md5($categoryids)); | |
1526 | } else { | |
1527 | $cachekey = sprintf('load_all_courses_%d', $limit); | |
1528 | } | |
1529 | ||
1530 | $toload = (empty($CFG->navshowallcourses))?self::LOAD_ROOT_CATEGORIES:self::LOAD_ALL_CATEGORIES; | |
1531 | ||
1532 | // If we are going to show all courses AND we are showing categories then | |
1533 | // to save us repeated DB calls load all of the categories now | |
1534 | if ($this->show_categories()) { | |
1535 | $this->load_all_categories($toload); | |
1536 | } | |
1537 | ||
1538 | // Will be the return of our efforts | |
3406acde | 1539 | $coursenodes = array(); |
8e8de15f SH |
1540 | |
1541 | // Here we have a very important cache check. | |
1542 | // Loading all courses is a VERY costly buisness for two reasons. | |
1543 | // 1. We still have a limit per category of courses, however there is no | |
1544 | // great way to select a maximum per category in a single query. | |
1545 | // 2. Each course loaded is costly as we have permission checks, ajax tie | |
1546 | // in's and a great deal of code that will be executed. | |
1547 | if (!$this->cache->cached($cachekey)) { | |
1548 | // Check if we need to show categories. | |
1549 | if ($this->show_categories()) { | |
1550 | // Hmmm we need to show categories... this is going to be painful. | |
1551 | // We now need to fetch up to $limit courses for each category to | |
1552 | // be displayed. | |
1553 | if ($categoryids !== null) { | |
1554 | if (!is_array($categoryids)) { | |
1555 | $categoryids = array($categoryids); | |
1556 | } | |
1557 | list($categorywhere, $categoryparams) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cc'); | |
1558 | $categorywhere = 'WHERE cc.id '.$categorywhere; | |
1559 | } else if ($toload == self::LOAD_ROOT_CATEGORIES) { | |
1560 | $categorywhere = 'WHERE cc.depth = 1 OR cc.depth = 2'; | |
1561 | $categoryparams = array(); | |
1562 | } else { | |
1563 | $categorywhere = ''; | |
1564 | $categoryparams = array(); | |
1565 | } | |
1566 | ||
1567 | // First up we are going to get the categories that we are going to | |
1568 | // need so that we can determine how best to load the courses from them. | |
1569 | $sql = "SELECT cc.id, COUNT(c.id) AS coursecount | |
1570 | FROM {course_categories} cc | |
1571 | LEFT JOIN {course} c ON c.category = cc.id | |
1572 | {$categorywhere} | |
1573 | GROUP BY cc.id"; | |
1574 | $categories = $DB->get_recordset_sql($sql, $categoryparams); | |
1575 | $fullfetch = array(); | |
1576 | $partfetch = array(); | |
1577 | foreach ($categories as $category) { | |
1578 | if (!$this->can_add_more_courses_to_category($category->id)) { | |
1579 | continue; | |
1580 | } | |
1581 | if ($category->coursecount > $limit * 5) { | |
1582 | $partfetch[] = $category->id; | |
1583 | } else if ($category->coursecount > 0) { | |
1584 | $fullfetch[] = $category->id; | |
1585 | } | |
1586 | } | |
1587 | $categories->close(); | |
1588 | ||
1589 | if (count($fullfetch)) { | |
1590 | // First up fetch all of the courses in categories where we know that we are going to | |
1591 | // need the majority of courses. | |
1592 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
1593 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array(SITEID), SQL_PARAMS_NAMED, 'lcourse', false); | |
1594 | list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory'); | |
1595 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect | |
1596 | FROM {course} c | |
1597 | $ccjoin | |
1598 | WHERE c.category {$categoryids} AND | |
1599 | c.id {$courseids} | |
1600 | ORDER BY c.sortorder ASC"; | |
1601 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams + $categoryparams); | |
1602 | foreach ($coursesrs as $course) { | |
1603 | if (!$this->can_add_more_courses_to_category($course->category)) { | |
1604 | continue; | |
1605 | } | |
1606 | context_instance_preload($course); | |
1607 | if ($course->id != SITEID && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { | |
1608 | continue; | |
1609 | } | |
1610 | $coursenodes[$course->id] = $this->add_course($course); | |
1611 | } | |
1612 | $coursesrs->close(); | |
1613 | } | |
1614 | ||
1615 | if (count($partfetch)) { | |
1616 | // Next we will work our way through the categories where we will likely only need a small | |
1617 | // proportion of the courses. | |
1618 | foreach ($partfetch as $categoryid) { | |
1619 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
1620 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array(SITEID), SQL_PARAMS_NAMED, 'lcourse', false); | |
1621 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect | |
1622 | FROM {course} c | |
1623 | $ccjoin | |
1624 | WHERE c.category = :categoryid AND | |
1625 | c.id {$courseids} | |
1626 | ORDER BY c.sortorder ASC"; | |
1627 | $courseparams['categoryid'] = $categoryid; | |
1628 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5); | |
1629 | foreach ($coursesrs as $course) { | |
1630 | if (!$this->can_add_more_courses_to_category($course->category)) { | |
1631 | break; | |
1632 | } | |
1633 | context_instance_preload($course); | |
1634 | if ($course->id != SITEID && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { | |
1635 | continue; | |
1636 | } | |
1637 | $coursenodes[$course->id] = $this->add_course($course); | |
1638 | } | |
1639 | $coursesrs->close(); | |
1640 | } | |
1641 | } | |
1642 | } else { | |
1643 | // Prepare the SQL to load the courses and their contexts | |
1644 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
1645 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false); | |
1646 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect | |
1647 | FROM {course} c | |
1648 | $ccjoin | |
1649 | WHERE c.id {$courseids} | |
1650 | ORDER BY c.sortorder ASC"; | |
1651 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams); | |
1652 | foreach ($coursesrs as $course) { | |
1653 | context_instance_preload($course); | |
1654 | if ($course->id != SITEID && !$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { | |
1655 | continue; | |
1656 | } | |
1657 | $coursenodes[$course->id] = $this->add_course($course); | |
1658 | if (count($coursenodes) >= $limit) { | |
1659 | break; | |
1660 | } | |
1661 | } | |
1662 | $coursesrs->close(); | |
1663 | } | |
1664 | ||
1665 | // Cache the course id's that we've had to load to save us running these queries again. | |
1666 | $this->cache->set($cachekey, array_keys($coursenodes)); | |
1667 | } else { | |
1668 | // YAY we've already cached this information | |
1669 | // First get the courses that we need to load from the navigation cache | |
1670 | $courseids = $this->cache->$cachekey; | |
1671 | ||
1672 | // Check to make sure we have some course nodes. | |
1673 | if (!count($courseids)) { | |
1674 | return $coursenodes; | |
1675 | } | |
1676 | ||
1677 | // Next check for courses that have already been loaded and remove them | |
1678 | // from the array we are about to load. | |
1679 | foreach (array_intersect($courseids, array_keys($this->addedcourses)) as $id) { | |
1680 | $key = array_search($id, $courseids); | |
1681 | unset($courseids[$key]); | |
1682 | } | |
1683 | ||
1684 | // Check that we still have course nodes. Any that have already been loaded | |
1685 | // will now have been removed. | |
1686 | if (!count($courseids)) { | |
1687 | return $coursenodes; | |
1688 | } | |
1689 | ||
1690 | // Prepare the SQL to load the courses and their contexts | |
1691 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
1692 | list($courseids, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'lc'); | |
1693 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect | |
1694 | FROM {course} c | |
1695 | $ccjoin | |
1696 | WHERE c.id {$courseids} | |
1697 | ORDER BY c.sortorder ASC"; | |
1698 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams); | |
1699 | foreach ($coursesrs as $course) { | |
1700 | context_instance_preload($course); | |
1701 | $coursenodes[$course->id] = $this->add_course($course); | |
1702 | } | |
1703 | $coursesrs->close(); | |
3406acde SH |
1704 | } |
1705 | return $coursenodes; | |
1706 | } | |
1707 | ||
8e8de15f SH |
1708 | /** |
1709 | * Returns true if more courses can be added to the provided category. | |
1710 | * | |
1711 | * @global type $CFG | |
1712 | * @param type $categoryid | |
1713 | * @return type | |
1714 | */ | |
1715 | protected function can_add_more_courses_to_category($category) { | |
1716 | global $CFG; | |
1717 | $limit = 20; | |
1718 | if (!empty($CFG->navcourselimit)) { | |
1719 | $limit = (int)$CFG->navcourselimit; | |
1720 | } | |
1721 | if (is_numeric($category)) { | |
1722 | if (!array_key_exists($category, $this->addedcategories)) { | |
1723 | return true; | |
1724 | } | |
1725 | $coursecount = count($this->addedcategories[$category]->children->type(self::TYPE_COURSE)); | |
1726 | } else if ($category instanceof navigation_node) { | |
ee03fe79 | 1727 | if ($category->type != self::TYPE_CATEGORY) { |
8e8de15f SH |
1728 | return false; |
1729 | } | |
1730 | $coursecount = count($category->children->type(self::TYPE_COURSE)); | |
1731 | } else if (is_object($category) && property_exists($category,'id')) { | |
1732 | $coursecount = count($this->addedcategories[$category->id]->children->type(self::TYPE_COURSE)); | |
1733 | } | |
1734 | return ($coursecount < $limit); | |
1735 | } | |
1736 | ||
4766a50c SH |
1737 | /** |
1738 | * Loads all categories (top level or if an id is specified for that category) | |
1739 | * | |
e26507b3 SH |
1740 | * @param int $categoryid The category id to load or null/0 to load all base level categories |
1741 | * @param bool $showbasecategories If set to true all base level categories will be loaded as well | |
1742 | * as the requested category and any parent categories. | |
31fde54f | 1743 | * @return navigation_node|void returns a navigation node if a category has been loaded. |
4766a50c | 1744 | */ |
8e8de15f | 1745 | protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) { |
4766a50c | 1746 | global $DB; |
e26507b3 SH |
1747 | |
1748 | // Check if this category has already been loaded | |
8e8de15f SH |
1749 | if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) { |
1750 | return true; | |
e26507b3 SH |
1751 | } |
1752 | ||
176b75b5 SH |
1753 | $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); |
1754 | $sqlselect = "SELECT cc.*, $catcontextsql | |
1755 | FROM {course_categories} cc | |
1756 | JOIN {context} ctx ON cc.id = ctx.instanceid"; | |
1757 | $sqlwhere = "WHERE ctx.contextlevel = ".CONTEXT_COURSECAT; | |
1758 | $sqlorder = "ORDER BY depth ASC, sortorder ASC, id ASC"; | |
1759 | $params = array(); | |
1760 | ||
8e8de15f SH |
1761 | $categoriestoload = array(); |
1762 | if ($categoryid == self::LOAD_ALL_CATEGORIES) { | |
176b75b5 SH |
1763 | // We are going to load all categories regardless... prepare to fire |
1764 | // on the database server! | |
8e8de15f | 1765 | } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0 |
e26507b3 | 1766 | // We are going to load all of the first level categories (categories without parents) |
176b75b5 | 1767 | $sqlwhere .= " AND cc.parent = 0"; |
e26507b3 SH |
1768 | } else if (array_key_exists($categoryid, $this->addedcategories)) { |
1769 | // The category itself has been loaded already so we just need to ensure its subcategories | |
1770 | // have been loaded | |
1771 | list($sql, $params) = $DB->get_in_or_equal(array_keys($this->addedcategories), SQL_PARAMS_NAMED, 'parent', false); | |
1772 | if ($showbasecategories) { | |
1773 | // We need to include categories with parent = 0 as well | |
176b75b5 | 1774 | $sqlwhere .= " AND (parent = :categoryid OR parent = 0) AND parent {$sql}"; |
e26507b3 | 1775 | } else { |
176b75b5 SH |
1776 | // All we need is categories that match the parent |
1777 | $sqlwhere .= " AND parent = :categoryid AND parent {$sql}"; | |
e26507b3 SH |
1778 | } |
1779 | $params['categoryid'] = $categoryid; | |
4766a50c | 1780 | } else { |
e26507b3 SH |
1781 | // This category hasn't been loaded yet so we need to fetch it, work out its category path |
1782 | // and load this category plus all its parents and subcategories | |
3992a46e | 1783 | $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST); |
8e8de15f SH |
1784 | $categoriestoload = explode('/', trim($category->path, '/')); |
1785 | list($select, $params) = $DB->get_in_or_equal($categoriestoload); | |
176b75b5 | 1786 | // We are going to use select twice so double the params |
4766a50c | 1787 | $params = array_merge($params, $params); |
176b75b5 SH |
1788 | $basecategorysql = ($showbasecategories)?' OR depth = 1':''; |
1789 | $sqlwhere .= " AND (id {$select} OR parent {$select}{$basecategorysql})"; | |
1790 | } | |
1791 | ||
1792 | $categoriesrs = $DB->get_recordset_sql("$sqlselect $sqlwhere $sqlorder", $params); | |
1793 | $categories = array(); | |
1794 | foreach ($categoriesrs as $category) { | |
1795 | // Preload the context.. we'll need it when adding the category in order | |
1796 | // to format the category name. | |
1797 | context_helper::preload_from_record($category); | |
1798 | if (array_key_exists($category->id, $this->addedcategories)) { | |
1799 | // Do nothing, its already been added. | |
1800 | } else if ($category->parent == '0') { | |
1801 | // This is a root category lets add it immediately | |
1802 | $this->add_category($category, $this->rootnodes['courses']); | |
1803 | } else if (array_key_exists($category->parent, $this->addedcategories)) { | |
1804 | // This categories parent has already been added we can add this immediately | |
1805 | $this->add_category($category, $this->addedcategories[$category->parent]); | |
1806 | } else { | |
1807 | $categories[] = $category; | |
1808 | } | |
4766a50c | 1809 | } |
176b75b5 | 1810 | $categoriesrs->close(); |
e26507b3 SH |
1811 | |
1812 | // Now we have an array of categories we need to add them to the navigation. | |
1813 | while (!empty($categories)) { | |
1814 | $category = reset($categories); | |
1815 | if (array_key_exists($category->id, $this->addedcategories)) { | |
1816 | // Do nothing | |
1817 | } else if ($category->parent == '0') { | |
1818 | $this->add_category($category, $this->rootnodes['courses']); | |
1819 | } else if (array_key_exists($category->parent, $this->addedcategories)) { | |
1820 | $this->add_category($category, $this->addedcategories[$category->parent]); | |
4766a50c | 1821 | } else { |
e26507b3 SH |
1822 | // This category isn't in the navigation and niether is it's parent (yet). |
1823 | // We need to go through the category path and add all of its components in order. | |
1824 | $path = explode('/', trim($category->path, '/')); | |
1825 | foreach ($path as $catid) { | |
1826 | if (!array_key_exists($catid, $this->addedcategories)) { | |
1827 | // This category isn't in the navigation yet so add it. | |
1828 | $subcategory = $categories[$catid]; | |
c4afcf84 SH |
1829 | if ($subcategory->parent == '0') { |
1830 | // Yay we have a root category - this likely means we will now be able | |
1831 | // to add categories without problems. | |
1832 | $this->add_category($subcategory, $this->rootnodes['courses']); | |
1833 | } else if (array_key_exists($subcategory->parent, $this->addedcategories)) { | |
e26507b3 SH |
1834 | // The parent is in the category (as we'd expect) so add it now. |
1835 | $this->add_category($subcategory, $this->addedcategories[$subcategory->parent]); | |
1836 | // Remove the category from the categories array. | |
1837 | unset($categories[$catid]); | |
1838 | } else { | |
1839 | // We should never ever arrive here - if we have then there is a bigger | |
1840 | // problem at hand. | |
5607036a | 1841 | throw new coding_exception('Category path order is incorrect and/or there are missing categories'); |
e26507b3 | 1842 | } |
4766a50c | 1843 | } |
4766a50c SH |
1844 | } |
1845 | } | |
e26507b3 SH |
1846 | // Remove the category from the categories array now that we know it has been added. |
1847 | unset($categories[$category->id]); | |
4766a50c | 1848 | } |
8e8de15f SH |
1849 | if ($categoryid === self::LOAD_ALL_CATEGORIES) { |
1850 | $this->allcategoriesloaded = true; | |
1851 | } | |
e26507b3 | 1852 | // Check if there are any categories to load. |
8e8de15f SH |
1853 | if (count($categoriestoload) > 0) { |
1854 | $readytoloadcourses = array(); | |
1855 | foreach ($categoriestoload as $category) { | |
1856 | if ($this->can_add_more_courses_to_category($category)) { | |
1857 | $readytoloadcourses[] = $category; | |
1858 | } | |
1859 | } | |
1860 | if (count($readytoloadcourses)) { | |
1861 | $this->load_all_courses($readytoloadcourses); | |
1862 | } | |
4766a50c SH |
1863 | } |
1864 | } | |
1865 | ||
1866 | /** | |
1867 | * Adds a structured category to the navigation in the correct order/place | |
1868 | * | |
e26507b3 | 1869 | * @param stdClass $category |
435a512e | 1870 | * @param navigation_node $parent |
4766a50c | 1871 | */ |
e26507b3 SH |
1872 | protected function add_category(stdClass $category, navigation_node $parent) { |
1873 | if (array_key_exists($category->id, $this->addedcategories)) { | |
563329e8 | 1874 | return; |
e26507b3 SH |
1875 | } |
1876 | $url = new moodle_url('/course/category.php', array('id' => $category->id)); | |
63390481 SH |
1877 | $context = get_context_instance(CONTEXT_COURSECAT, $category->id); |
1878 | $categoryname = format_string($category->name, true, array('context' => $context)); | |
1879 | $categorynode = $parent->add($categoryname, $url, self::TYPE_CATEGORY, $categoryname, $category->id); | |
e26507b3 SH |
1880 | if (empty($category->visible)) { |
1881 | if (has_capability('moodle/category:viewhiddencategories', get_system_context())) { | |
1882 | $categorynode->hidden = true; | |
1883 | } else { | |
1884 | $categorynode->display = false; | |
14337688 | 1885 | } |
4766a50c | 1886 | } |
e26507b3 | 1887 | $this->addedcategories[$category->id] = &$categorynode; |
4766a50c SH |
1888 | } |
1889 | ||
3406acde SH |
1890 | /** |
1891 | * Loads the given course into the navigation | |
7d2a0492 | 1892 | * |
3406acde SH |
1893 | * @param stdClass $course |
1894 | * @return navigation_node | |
1895 | */ | |
1896 | protected function load_course(stdClass $course) { | |
1897 | if ($course->id == SITEID) { | |
e26507b3 SH |
1898 | return $this->rootnodes['site']; |
1899 | } else if (array_key_exists($course->id, $this->addedcourses)) { | |
1900 | return $this->addedcourses[$course->id]; | |
3406acde | 1901 | } else { |
e26507b3 | 1902 | return $this->add_course($course); |
3406acde | 1903 | } |
3406acde SH |
1904 | } |
1905 | ||
1906 | /** | |
1907 | * Loads all of the courses section into the navigation. | |
1908 | * | |
1909 | * This function utilisies a callback that can be implemented within the course | |
1910 | * formats lib.php file to customise the navigation that is generated at this | |
1911 | * point for the course. | |
1912 | * | |
93123b17 | 1913 | * By default (if not defined) the method {@link global_navigation::load_generic_course_sections()} is |
3406acde SH |
1914 | * called instead. |
1915 | * | |
3406acde SH |
1916 | * @param stdClass $course Database record for the course |
1917 | * @param navigation_node $coursenode The course node within the navigation | |
1918 | * @return array Array of navigation nodes for the section with key = section id | |
1919 | */ | |
1920 | protected function load_course_sections(stdClass $course, navigation_node $coursenode) { | |
1921 | global $CFG; | |
1922 | $structurefile = $CFG->dirroot.'/course/format/'.$course->format.'/lib.php'; | |
1923 | $structurefunc = 'callback_'.$course->format.'_load_content'; | |
1924 | if (function_exists($structurefunc)) { | |
1925 | return $structurefunc($this, $course, $coursenode); | |
1926 | } else if (file_exists($structurefile)) { | |
1927 | require_once $structurefile; | |
1928 | if (function_exists($structurefunc)) { | |
1929 | return $structurefunc($this, $course, $coursenode); | |
1930 | } else { | |
0f4ab67d | 1931 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1932 | } |
1933 | } else { | |
0f4ab67d | 1934 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1935 | } |
1936 | } | |
1937 | ||
e26507b3 SH |
1938 | /** |
1939 | * Generates an array of sections and an array of activities for the given course. | |
1940 | * | |
1941 | * This method uses the cache to improve performance and avoid the get_fast_modinfo call | |
1942 | * | |
1943 | * @param stdClass $course | |
1944 | * @return array Array($sections, $activities) | |
1945 | */ | |
1946 | protected function generate_sections_and_activities(stdClass $course) { | |
1947 | global $CFG; | |
1948 | require_once($CFG->dirroot.'/course/lib.php'); | |
1949 | ||
c18facf2 | 1950 | $modinfo = get_fast_modinfo($course); |
e26507b3 | 1951 | |
c18facf2 SH |
1952 | $sections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true); |
1953 | $activities = array(); | |
e26507b3 | 1954 | |
c18facf2 SH |
1955 | foreach ($sections as $key => $section) { |
1956 | $sections[$key]->hasactivites = false; | |
1957 | if (!array_key_exists($section->section, $modinfo->sections)) { | |
1958 | continue; | |
1959 | } | |
1960 | foreach ($modinfo->sections[$section->section] as $cmid) { | |
1961 | $cm = $modinfo->cms[$cmid]; | |
1962 | if (!$cm->uservisible) { | |
e26507b3 SH |
1963 | continue; |
1964 | } | |
c18facf2 SH |
1965 | $activity = new stdClass; |
1966 | $activity->id = $cm->id; | |
1967 | $activity->course = $course->id; | |
1968 | $activity->section = $section->section; | |
1969 | $activity->name = $cm->name; | |
1970 | $activity->icon = $cm->icon; | |
1971 | $activity->iconcomponent = $cm->iconcomponent; | |
1972 | $activity->hidden = (!$cm->visible); | |
1973 | $activity->modname = $cm->modname; | |
1974 | $activity->nodetype = navigation_node::NODETYPE_LEAF; | |
1975 | $activity->onclick = $cm->get_on_click(); | |
1976 | $url = $cm->get_url(); | |
1977 | if (!$url) { | |
1978 | $activity->url = null; | |
1979 | $activity->display = false; | |
1980 | } else { | |
1981 | $activity->url = $cm->get_url()->out(); | |
1982 | $activity->display = true; | |
1983 | if (self::module_extends_navigation($cm->modname)) { | |
1984 | $activity->nodetype = navigation_node::NODETYPE_BRANCH; | |
d67e4821 | 1985 | } |
e26507b3 | 1986 | } |
c18facf2 SH |
1987 | $activities[$cmid] = $activity; |
1988 | if ($activity->display) { | |
1989 | $sections[$key]->hasactivites = true; | |
1990 | } | |
e26507b3 | 1991 | } |
e26507b3 | 1992 | } |
c18facf2 | 1993 | |
e26507b3 SH |
1994 | return array($sections, $activities); |
1995 | } | |
1996 | ||
3406acde SH |
1997 | /** |
1998 | * Generically loads the course sections into the course's navigation. | |
1999 | * | |
2000 | * @param stdClass $course | |
2001 | * @param navigation_node $coursenode | |
e26507b3 | 2002 | * @param string $courseformat The course format |
3406acde SH |
2003 | * @return array An array of course section nodes |
2004 | */ | |
0f4ab67d | 2005 | public function load_generic_course_sections(stdClass $course, navigation_node $coursenode, $courseformat='unknown') { |
df997f84 | 2006 | global $CFG, $DB, $USER; |
df997f84 | 2007 | require_once($CFG->dirroot.'/course/lib.php'); |
435a512e | 2008 | |
e26507b3 | 2009 | list($sections, $activities) = $this->generate_sections_and_activities($course); |
0f4ab67d SH |
2010 | |
2011 | $namingfunction = 'callback_'.$courseformat.'_get_section_name'; | |
2012 | $namingfunctionexists = (function_exists($namingfunction)); | |
ad470097 | 2013 | |
e26507b3 | 2014 | $viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context); |
435a512e | 2015 | |
ad470097 | 2016 | $urlfunction = 'callback_'.$courseformat.'_get_section_url'; |
d9e13265 | 2017 | if (function_exists($urlfunction)) { |
10cdacfe DP |
2018 | // This code path is deprecated but we decided not to warn developers as |
2019 | // major changes are likely to follow in 2.4. See MDL-32504. | |
d9e13265 | 2020 | } else { |
ad470097 SH |
2021 | $urlfunction = null; |
2022 | } | |
2023 | ||
b9bcdb54 | 2024 | $key = 0; |
f7558683 DP |
2025 | if (defined('AJAX_SCRIPT') && AJAX_SCRIPT == '0' && $this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { |
2026 | $key = optional_param('section', $key, PARAM_INT); | |
ad470097 SH |
2027 | } |
2028 | ||
7487c856 | 2029 | $navigationsections = array(); |
e26507b3 | 2030 | foreach ($sections as $sectionid => $section) { |
7487c856 | 2031 | $section = clone($section); |
3406acde | 2032 | if ($course->id == SITEID) { |
e26507b3 | 2033 | $this->load_section_activities($coursenode, $section->section, $activities); |
3406acde | 2034 | } else { |
d67e4821 | 2035 | if ((!$viewhiddensections && !$section->visible) || (!$this->showemptysections && |
2036 | !$section->hasactivites && $this->includesectionnum !== $section->section)) { | |
3406acde SH |
2037 | continue; |
2038 | } | |
0f4ab67d SH |
2039 | if ($namingfunctionexists) { |
2040 | $sectionname = $namingfunction($course, $section, $sections); | |
3406acde | 2041 | } else { |
0f4ab67d | 2042 | $sectionname = get_string('section').' '.$section->section; |
3406acde | 2043 | } |
ad470097 | 2044 | |
dbe5050d | 2045 | $url = null; |
d9e13265 DP |
2046 | if ($urlfunction) { |
2047 | // pre 2.3 style format url | |
ad470097 | 2048 | $url = $urlfunction($course->id, $section->section); |
d9e13265 DP |
2049 | }else{ |
2050 | if ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) { | |
2051 | $url = course_get_url($course, $section->section); | |
2052 | } | |
ad470097 | 2053 | } |
3406acde SH |
2054 | $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id); |
2055 | $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; | |
2056 | $sectionnode->hidden = (!$section->visible); | |
ad470097 SH |
2057 | if ($key != '0' && $section->section != '0' && $section->section == $key && $this->page->context->contextlevel != CONTEXT_MODULE && $section->hasactivites) { |
2058 | $sectionnode->make_active(); | |
e26507b3 | 2059 | $this->load_section_activities($sectionnode, $section->section, $activities); |
3406acde SH |
2060 | } |
2061 | $section->sectionnode = $sectionnode; | |
7487c856 | 2062 | $navigationsections[$sectionid] = $section; |
3406acde SH |
2063 | } |
2064 | } | |
7487c856 | 2065 | return $navigationsections; |
3406acde SH |
2066 | } |
2067 | /** | |
2068 | * Loads all of the activities for a section into the navigation structure. | |
2069 | * | |
2070 | * @param navigation_node $sectionnode | |
2071 | * @param int $sectionnumber | |
93123b17 | 2072 | * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()} |
1580cfdb | 2073 | * @param stdClass $course The course object the section and activities relate to. |
3406acde SH |
2074 | * @return array Array of activity nodes |
2075 | */ | |
1580cfdb | 2076 | protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) { |
4037098e | 2077 | global $CFG; |
dee1a0fd SH |
2078 | // A static counter for JS function naming |
2079 | static $legacyonclickcounter = 0; | |
3406acde | 2080 | |
e26507b3 | 2081 | $activitynodes = array(); |
4037098e SH |
2082 | if (empty($activities)) { |
2083 | return $activitynodes; | |
2084 | } | |
2085 | ||
2086 | if (!is_object($course)) { | |
2087 | $activity = reset($activities); | |
2088 | $courseid = $activity->course; | |
2089 | } else { | |
2090 | $courseid = $course->id; | |
2091 | } | |
2092 | $showactivities = ($courseid != SITEID || !empty($CFG->navshowfrontpagemods)); | |
2093 | ||
e26507b3 SH |
2094 | foreach ($activities as $activity) { |
2095 | if ($activity->section != $sectionnumber) { | |
3406acde SH |
2096 | continue; |
2097 | } | |
e26507b3 SH |
2098 | if ($activity->icon) { |
2099 | $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent); | |
3406acde | 2100 | } else { |
e26507b3 | 2101 | $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname); |
3406acde | 2102 | } |
dee1a0fd SH |
2103 | |
2104 | // Prepare the default name and url for the node | |
2105 | $activityname = format_string($activity->name, true, array('context' => get_context_instance(CONTEXT_MODULE, $activity->id))); | |
2106 | $action = new moodle_url($activity->url); | |
2107 | ||
2108 | // Check if the onclick property is set (puke!) | |
2109 | if (!empty($activity->onclick)) { | |
2110 | // Increment the counter so that we have a unique number. | |
2111 | $legacyonclickcounter++; | |
2112 | // Generate the function name we will use | |
2113 | $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter; | |
2114 | $propogrationhandler = ''; | |
2115 | // Check if we need to cancel propogation. Remember inline onclick | |
2116 | // events would return false if they wanted to prevent propogation and the | |
2117 | // default action. | |
2118 | if (strpos($activity->onclick, 'return false')) { | |
2119 | $propogrationhandler = 'e.halt();'; | |
2120 | } | |
2121 | // Decode the onclick - it has already been encoded for display (puke) | |
2122 | $onclick = htmlspecialchars_decode($activity->onclick); | |
2123 | // Build the JS function the click event will call | |
2124 | $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }"; | |
2125 | $this->page->requires->js_init_code($jscode); | |
2126 | // Override the default url with the new action link | |
2127 | $action = new action_link($action, $activityname, new component_action('click', $functionname)); | |
2128 | } | |
2129 | ||
2130 | $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon); | |
e26507b3 SH |
2131 | $activitynode->title(get_string('modulename', $activity->modname)); |
2132 | $activitynode->hidden = $activity->hidden; | |
4037098e | 2133 | $activitynode->display = $showactivities && $activity->display; |
e26507b3 SH |
2134 | $activitynode->nodetype = $activity->nodetype; |
2135 | $activitynodes[$activity->id] = $activitynode; | |
3406acde SH |
2136 | } |
2137 | ||
e26507b3 | 2138 | return $activitynodes; |
3406acde | 2139 | } |
2a62743c PS |
2140 | /** |
2141 | * Loads a stealth module from unavailable section | |
2142 | * @param navigation_node $coursenode | |
2143 | * @param stdClass $modinfo | |
2144 | * @return navigation_node or null if not accessible | |
2145 | */ | |
2146 | protected function load_stealth_activity(navigation_node $coursenode, $modinfo) { | |
2147 | if (empty($modinfo->cms[$this->page->cm->id])) { | |
2148 | return null; | |
2149 | } | |
2150 | $cm = $modinfo->cms[$this->page->cm->id]; | |
2151 | if (!$cm->uservisible) { | |
2152 | return null; | |
2153 | } | |
2154 | if ($cm->icon) { | |
2155 | $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent); | |
2156 | } else { | |
2157 | $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname); | |
2158 | } | |
0d8b6a69 | 2159 | $url = $cm->get_url(); |
2a62743c PS |
2160 | $activitynode = $coursenode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon); |
2161 | $activitynode->title(get_string('modulename', $cm->modname)); | |
2162 | $activitynode->hidden = (!$cm->visible); | |
0d8b6a69 | 2163 | if (!$url) { |
2164 | // Don't show activities that don't have links! | |
2a62743c | 2165 | $activitynode->display = false; |
e26507b3 | 2166 | } else if (self::module_extends_navigation($cm->modname)) { |
2a62743c PS |
2167 | $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; |
2168 | } | |
2169 | return $activitynode; | |
2170 | } | |
3406acde SH |
2171 | /** |
2172 | * Loads the navigation structure for the given activity into the activities node. | |
2173 | * | |
2174 | * This method utilises a callback within the modules lib.php file to load the | |
2175 | * content specific to activity given. | |
2176 | * | |
2177 | * The callback is a method: {modulename}_extend_navigation() | |
2178 | * Examples: | |
93123b17 EL |
2179 | * * {@link forum_extend_navigation()} |
2180 | * * {@link workshop_extend_navigation()} | |
3406acde | 2181 | * |
f0dcc212 | 2182 | * @param cm_info|stdClass $cm |
3406acde SH |
2183 | * @param stdClass $course |
2184 | * @param navigation_node $activity | |
2185 | * @return bool | |
2186 | */ | |
0d8b6a69 | 2187 | protected function load_activity($cm, stdClass $course, navigation_node $activity) { |
3406acde | 2188 | global $CFG, $DB; |
44303ca6 | 2189 | |
f0dcc212 SH |
2190 | // make sure we have a $cm from get_fast_modinfo as this contains activity access details |
2191 | if (!($cm instanceof cm_info)) { | |
2192 | $modinfo = get_fast_modinfo($course); | |
2193 | $cm = $modinfo->get_cm($cm->id); | |
2194 | } | |
3406acde | 2195 | |
577c8964 | 2196 | $activity->nodetype = navigation_node::NODETYPE_LEAF; |
3406acde SH |
2197 | $activity->make_active(); |
2198 | $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'; | |
2199 | $function = $cm->modname.'_extend_navigation'; | |
2200 | ||
2201 | if (file_exists($file)) { | |
2202 | require_once($file); | |
2203 | if (function_exists($function)) { | |
2204 | $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); | |
2205 | $function($activity, $course, $activtyrecord, $cm); | |
3406acde SH |
2206 | } |
2207 | } | |
577c8964 MG |
2208 | |
2209 | // Allow the active advanced grading method plugin to append module navigation | |
2210 | $featuresfunc = $cm->modname.'_supports'; | |
2211 | if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING)) { | |
2212 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
2213 | $gradingman = get_grading_manager($cm->context, $cm->modname); | |
2214 | $gradingman->extend_navigation($this, $activity); | |
2215 | } | |
2216 | ||
2217 | return $activity->has_children(); | |
3406acde SH |
2218 | } |
2219 | /** | |
4d00fded | 2220 | * Loads user specific information into the navigation in the appropriate place. |
3406acde SH |
2221 | * |
2222 | * If no user is provided the current user is assumed. | |
2223 | * | |
3406acde | 2224 | * @param stdClass $user |
4d00fded | 2225 | * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) |
3406acde | 2226 | * @return bool |
7a7e209d | 2227 | */ |
87c215de | 2228 | protected function load_for_user($user=null, $forceforcontext=false) { |
3406acde | 2229 | global $DB, $CFG, $USER; |
4f0c2d00 | 2230 | |
7a7e209d SH |
2231 | if ($user === null) { |
2232 | // We can't require login here but if the user isn't logged in we don't | |
2233 | // want to show anything | |
b9d4c7d3 | 2234 | if (!isloggedin() || isguestuser()) { |
7a7e209d SH |
2235 | return false; |
2236 | } | |
2237 | $user = $USER; | |
7a7e209d SH |
2238 | } else if (!is_object($user)) { |
2239 | // If the user is not an object then get them from the database | |
e141bc81 SH |
2240 | $select = context_helper::get_preload_record_columns_sql('ctx'); |
2241 | $sql = "SELECT u.*, $select | |
2242 | FROM {user} u | |
2243 | JOIN {context} ctx ON u.id = ctx.instanceid | |
2244 | WHERE u.id = :userid AND | |
2245 | ctx.contextlevel = :contextlevel"; | |
2246 | $user = $DB->get_record_sql($sql, array('userid' => (int)$user, 'contextlevel' => CONTEXT_USER), MUST_EXIST); | |
2247 | context_helper::preload_from_record($user); | |
7a7e209d | 2248 | } |
136ca7c8 SH |
2249 | |
2250 | $iscurrentuser = ($user->id == $USER->id); | |
2251 | ||
507a7a9a | 2252 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); |
7a7e209d SH |
2253 | |
2254 | // Get the course set against the page, by default this will be the site | |
3406acde SH |
2255 | $course = $this->page->course; |
2256 | $baseargs = array('id'=>$user->id); | |
44303ca6 | 2257 | if ($course->id != SITEID && (!$iscurrentuser || $forceforcontext)) { |
e26507b3 | 2258 | $coursenode = $this->load_course($course); |
7a7e209d | 2259 | $baseargs['course'] = $course->id; |
3406acde | 2260 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
7a7e209d | 2261 | $issitecourse = false; |
7d2a0492 | 2262 | } else { |
7a7e209d | 2263 | // Load all categories and get the context for the system |
507a7a9a | 2264 | $coursecontext = get_context_instance(CONTEXT_SYSTEM); |
7a7e209d SH |
2265 | $issitecourse = true; |
2266 | } | |
2267 | ||
2268 | // Create a node to add user information under. | |
87c215de | 2269 | if ($iscurrentuser && !$forceforcontext) { |
3406acde SH |
2270 | // If it's the current user the information will go under the profile root node |
2271 | $usernode = $this->rootnodes['myprofile']; | |
e96bc3f9 SH |
2272 | $course = get_site(); |
2273 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); | |
2274 | $issitecourse = true; | |
7a7e209d SH |
2275 | } else { |
2276 | if (!$issitecourse) { | |
2277 | // Not the current user so add it to the participants node for the current course | |
3406acde | 2278 | $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); |
ad93ddd5 | 2279 | $userviewurl = new moodle_url('/user/view.php', $baseargs); |
7a7e209d SH |
2280 | } else { |
2281 | // This is the site so add a users node to the root branch | |
3406acde | 2282 | $usersnode = $this->rootnodes['users']; |
8b0614d9 DP |
2283 | if (has_capability('moodle/course:viewparticipants', $coursecontext)) { |
2284 | $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); | |
2285 | } | |
ad93ddd5 | 2286 | $userviewurl = new moodle_url('/user/profile.php', $baseargs); |
7a7e209d | 2287 | } |
f5c1e621 SH |
2288 | if (!$usersnode) { |
2289 | // We should NEVER get here, if the course hasn't been populated | |
2290 | // with a participants node then the navigaiton either wasn't generated | |
2291 | // for it (you are missing a require_login or set_context call) or | |
2292 | // you don't have access.... in the interests of no leaking informatin | |
2293 | // we simply quit... | |
2294 | return false; | |
2295 | } | |
7a7e209d | 2296 | // Add a branch for the current user |
76565fa1 AG |
2297 | $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); |
2298 | $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, $user->id); | |
3406acde | 2299 | |
5ac851fb SH |
2300 | if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { |
2301 | $usernode->make_active(); | |
2302 | } | |
7a7e209d SH |
2303 | } |
2304 | ||
2305 | // If the user is the current user or has permission to view the details of the requested | |
2306 | // user than add a view profile link. | |
507a7a9a | 2307 | if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { |
87c215de | 2308 | if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { |
03d9401e MD |
2309 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php',$baseargs)); |
2310 | } else { | |
2311 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php',$baseargs)); | |
2312 | } | |
7a7e209d SH |
2313 | } |
2314 | ||
356a6de3 SH |
2315 | if (!empty($CFG->navadduserpostslinks)) { |
2316 | // Add nodes for forum posts and discussions if the user can view either or both | |
2317 | // There are no capability checks here as the content of the page is based | |
2318 | // purely on the forums the current user has access too. | |
2319 | $forumtab = $usernode->add(get_string('forumposts', 'forum')); | |
2320 | $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); | |
2321 | $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode'=>'discussions')))); | |
2322 | } | |
7a7e209d | 2323 | |
27bad0a6 SH |
2324 | // Add blog nodes |
2325 | if (!empty($CFG->bloglevel)) { | |
e26507b3 SH |
2326 | if (!$this->cache->cached('userblogoptions'.$user->id)) { |
2327 | require_once($CFG->dirroot.'/blog/lib.php'); | |
2328 | // Get all options for the user | |
2329 | $options = blog_get_options_for_user($user); | |
2330 | $this->cache->set('userblogoptions'.$user->id, $options); | |
2331 | } else { | |
2332 | $options = $this->cache->{'userblogoptions'.$user->id}; | |
2333 | } | |
2334 | ||
27bad0a6 SH |
2335 | if (count($options) > 0) { |
2336 | $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); | |
c000545d JF |
2337 | foreach ($options as $type => $option) { |
2338 | if ($type == "rss") { | |
2339 | $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); | |
2340 | } else { | |
2341 | $blogs->add($option['string'], $option['link']); | |
2342 | } | |
27bad0a6 SH |
2343 | } |
2344 | } | |
2345 | } | |
2346 | ||
5ac851fb SH |
2347 | if (!empty($CFG->messaging)) { |
2348 | $messageargs = null; | |
2349 | if ($USER->id!=$user->id) { | |
2350 | $messageargs = array('id'=>$user->id); | |
2351 | } | |
2352 | $url = new moodle_url('/message/index.php',$messageargs); | |
2353 | $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); | |
c81b9f69 | 2354 | } |
c81b9f69 | 2355 | |
52d1a804 JG |
2356 | $context = get_context_instance(CONTEXT_USER, $USER->id); |
2357 | if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) { | |
82af55d7 MD |
2358 | $url = new moodle_url('/user/files.php'); |
2359 | $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); | |
78765507 DC |
2360 | } |
2361 | ||
7a7e209d | 2362 | // Add a node to view the users notes if permitted |
507a7a9a | 2363 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { |
3406acde SH |
2364 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id)); |
2365 | if ($coursecontext->instanceid) { | |
2366 | $url->param('course', $coursecontext->instanceid); | |
2367 | } | |
2368 | $usernode->add(get_string('notes', 'notes'), $url); | |
7a7e209d SH |
2369 | } |
2370 | ||
beda8fa8 | 2371 | // Add reports node |
4d00fded | 2372 | $reporttab = $usernode->add(get_string('activityreports')); |
4d00fded PS |
2373 | $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); |
2374 | foreach ($reports as $reportfunction) { | |
2375 | $reportfunction($reporttab, $user, $course); | |
2376 | } | |
beda8fa8 PS |
2377 | $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); |
2378 | if ($anyreport || ($course->showreports && $iscurrentuser && $forceforcontext)) { | |
2379 | // Add grade hardcoded grade report if necessary | |
03d9401e MD |
2380 | $gradeaccess = false; |
2381 | if (has_capability('moodle/grade:viewall', $coursecontext)) { | |
2382 | //ok - can view all course grades | |
7a7e209d | 2383 | $gradeaccess = true; |
03d9401e MD |
2384 | } else if ($course->showgrades) { |
2385 | if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) { | |
2386 | //ok - can view own grades | |
2387 | $gradeaccess = true; | |
2388 | } else if (has_capability('moodle/grade:viewall', $usercontext)) { | |
2389 | // ok - can view grades of this user - parent most probably | |
2390 | $gradeaccess = true; | |
2391 | } else if ($anyreport) { | |
2392 | // ok - can view grades of this user - parent most probably | |
2393 | $gradeaccess = true; | |
2394 | } | |
2395 | } | |
2396 | if ($gradeaccess) { | |
a0b15989 | 2397 | $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array('mode'=>'grade', 'id'=>$course->id, 'user'=>$usercontext->instanceid))); |
7a7e209d | 2398 | } |
7a7e209d | 2399 | } |
beda8fa8 | 2400 | // Check the number of nodes in the report node... if there are none remove the node |
4d00fded PS |
2401 | $reporttab->trim_if_empty(); |
2402 | ||
7a7e209d | 2403 | // If the user is the current user add the repositories for the current user |
9acb8241 | 2404 | $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); |
7a7e209d | 2405 | if ($iscurrentuser) { |
e26507b3 SH |
2406 | if (!$this->cache->cached('contexthasrepos'.$usercontext->id)) { |
2407 | require_once($CFG->dirroot . '/repository/lib.php'); | |
2408 | $editabletypes = repository::get_editable_types($usercontext); | |
2409 | $haseditabletypes = !empty($editabletypes); | |
2410 | unset($editabletypes); | |
2411 | $this->cache->set('contexthasrepos'.$usercontext->id, $haseditabletypes); | |
2412 | } else { | |
2413 | $haseditabletypes = $this->cache->{'contexthasrepos'.$usercontext->id}; | |
2414 | } | |
2415 | if ($haseditabletypes) { | |
ad70376c | 2416 | $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); |
7a7e209d | 2417 | } |
9acb8241 SH |
2418 | } else if ($course->id == SITEID && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { |
2419 | ||
2420 | // Add view grade report is permitted | |
2421 | $reports = get_plugin_list('gradereport'); | |
2422 | arsort($reports); // user is last, we want to test it first | |
2423 | ||
2424 | $userscourses = enrol_get_users_courses($user->id); | |
2425 | $userscoursesnode = $usernode->add(get_string('courses')); | |
69816a5c | 2426 | |
9acb8241 SH |
2427 | foreach ($userscourses as $usercourse) { |
2428 | $usercoursecontext = get_context_instance(CONTEXT_COURSE, $usercourse->id); | |
8ebbb06a SH |
2429 | $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext)); |
2430 | $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$usercourse->id)), self::TYPE_CONTAINER); | |
9acb8241 SH |
2431 | |
2432 | $gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext); | |
2433 | if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { | |
2434 | foreach ($reports as $plugin => $plugindir) { | |
2435 | if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) { | |
2436 | //stop when the first visible plugin is found | |
2437 | $gradeavailable = true; | |
2438 | break; | |
deaf04c7 | 2439 | } |
9acb8241 SH |
2440 | } |
2441 | } | |
2442 | ||
2443 | if ($gradeavailable) { | |
2444 | $url = new moodle_url('/grade/report/index.php', array('id'=>$usercourse->id)); | |
2445 | $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', '')); | |
2446 | } | |
2447 | ||
2448 | // Add a node to view the users notes if permitted | |
2449 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { | |
2450 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id, 'course'=>$usercourse->id)); | |
2451 | $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); | |
2452 | } | |
2453 | ||
1344b0ca | 2454 | if (can_access_course($usercourse, $user->id)) { |
9acb8241 SH |
2455 | $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', '')); |
2456 | } | |
2457 | ||
4d00fded PS |
2458 | $reporttab = $usercoursenode->add(get_string('activityreports')); |
2459 | ||
2460 | $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); | |
2461 | foreach ($reports as $reportfunction) { | |
2462 | $reportfunction($reporttab, $user, $usercourse); | |
2463 | } | |
2464 | ||
4d00fded | 2465 | $reporttab->trim_if_empty(); |
9acb8241 | 2466 | } |
7a7e209d SH |
2467 | } |
2468 | return true; | |
2469 | } | |
2470 | ||
2471 | /** | |
3406acde | 2472 | * This method simply checks to see if a given module can extend the navigation. |
7d2a0492 | 2473 | * |
31fde54f | 2474 | * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation. |
e26507b3 | 2475 | * |
7d2a0492 | 2476 | * @param string $modname |
2477 | * @return bool | |
2478 | */ | |
e26507b3 | 2479 | protected static function module_extends_navigation($modname) { |
7d2a0492 | 2480 | global $CFG; |
e26507b3 SH |
2481 | static $extendingmodules = array(); |
2482 | if (!array_key_exists($modname, $extendingmodules)) { | |
2483 | $extendingmodules[$modname] = false; | |
2484 | $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php'; | |
2485 | if (file_exists($file)) { | |
2486 | $function = $modname.'_extend_navigation'; | |
2487 | require_once($file); | |
2488 | $extendingmodules[$modname] = (function_exists($function)); | |
7d2a0492 | 2489 | } |
2490 | } | |
e26507b3 | 2491 | return $extendingmodules[$modname]; |
7d2a0492 | 2492 | } |
2493 | /** | |
3406acde | 2494 | * Extends the navigation for the given user. |
435a512e | 2495 | * |
3406acde | 2496 | * @param stdClass $user A user from the database |
7d2a0492 | 2497 | */ |
3406acde SH |
2498 | public function extend_for_user($user) { |
2499 | $this->extendforuser[] = $user; | |
5d07e957 SH |
2500 | } |
2501 | ||
2502 | /** | |
2503 | * Returns all of the users the navigation is being extended for | |
2504 | * | |
31fde54f | 2505 | * @return array An array of extending users. |
5d07e957 SH |
2506 | */ |
2507 | public function get_extending_users() { | |
2508 | return $this->extendforuser; | |
7d2a0492 | 2509 | } |
7d2a0492 | 2510 | /** |
3406acde | 2511 | * Adds the given course to the navigation structure. |
7d2a0492 | 2512 | * |
3406acde | 2513 | * @param stdClass $course |
8e8de15f SH |
2514 | * @param bool $forcegeneric |
2515 | * @param bool $ismycourse | |
3406acde | 2516 | * @return navigation_node |
7d2a0492 | 2517 | */ |
e26507b3 | 2518 | public function add_course(stdClass $course, $forcegeneric = false, $ismycourse = false) { |
4766a50c | 2519 | global $CFG; |
44303ca6 | 2520 | |
e26507b3 SH |
2521 | // We found the course... we can return it now :) |
2522 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2523 | return $this->addedcourses[$course->id]; | |
2524 | } | |
2525 | ||
8ebbb06a SH |
2526 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
2527 | ||
e26507b3 SH |
2528 | if ($course->id != SITEID && !$course->visible) { |
2529 | if (is_role_switched($course->id)) { | |
2530 | // user has to be able to access course in order to switch, let's skip the visibility test here | |
8ebbb06a | 2531 | } else if (!has_capability('moodle/course:viewhiddencourses', $coursecontext)) { |
e26507b3 | 2532 | return false; |
44303ca6 | 2533 | } |
7d2a0492 | 2534 | } |
7d2a0492 | 2535 | |
4766a50c | 2536 | $issite = ($course->id == SITEID); |
8ebbb06a | 2537 | $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); |
4766a50c SH |
2538 | |
2539 | if ($issite) { | |
3406acde | 2540 | $parent = $this; |
4766a50c | 2541 | $url = null; |
016ba638 SH |
2542 | if (empty($CFG->usesitenameforsitepages)) { |
2543 | $shortname = get_string('sitepages'); | |
2544 | } | |
8e8de15f | 2545 | } else if ($ismycourse && !$forcegeneric) { |
b0712163 SH |
2546 | if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_CATEGORY))) { |
2547 | // Nothing to do here the above statement set $parent to the category within mycourses. | |
2548 | } else { | |
2549 | $parent = $this->rootnodes['mycourses']; | |
2550 | } | |
3406acde | 2551 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
7a7e209d | 2552 | } else { |
3406acde | 2553 | $parent = $this->rootnodes['courses']; |
a6855934 | 2554 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
8e8de15f SH |
2555 | if (!empty($course->category) && $this->show_categories()) { |
2556 | if ($this->show_categories() && !$this->is_category_fully_loaded($course->category)) { | |
2557 | // We need to load the category structure for this course | |
2558 | $this->load_all_categories($course->category); | |
2559 | } | |
2560 | if (array_key_exists($course->category, $this->addedcategories)) { | |
2561 | $parent = $this->addedcategories[$course->category]; | |
2562 | // This could lead to the course being created so we should check whether it is the case again | |
2563 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2564 | return $this->addedcourses[$course->id]; | |
2565 | } | |
3992a46e | 2566 | } |
4766a50c SH |
2567 | } |
2568 | } | |
2569 | ||
95892197 | 2570 | $coursenode = $parent->add($shortname, $url, self::TYPE_COURSE, $shortname, $course->id); |
3406acde SH |
2571 | $coursenode->nodetype = self::NODETYPE_BRANCH; |
2572 | $coursenode->hidden = (!$course->visible); | |
91d284c1 | 2573 | $coursenode->title(format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))); |
e26507b3 SH |
2574 | if (!$forcegeneric) { |
2575 | $this->addedcourses[$course->id] = &$coursenode; | |
2576 | } | |
e26507b3 | 2577 | |
3406acde | 2578 | return $coursenode; |
7d2a0492 | 2579 | } |
8e8de15f SH |
2580 | |
2581 | /** | |
2582 | * Returns true if the category has already been loaded as have any child categories | |
2583 | * | |
2584 | * @param int $categoryid | |
2585 | * @return bool | |
2586 | */ | |
2587 | protected function is_category_fully_loaded($categoryid) { | |
2588 | return (array_key_exists($categoryid, $this->addedcategories) && ($this->allcategoriesloaded || $this->addedcategories[$categoryid]->children->count() > 0)); | |
2589 | } | |
2590 | ||
7d2a0492 | 2591 | /** |
3406acde | 2592 | * Adds essential course nodes to the navigation for the given course. |
7d2a0492 | 2593 | * |
3406acde | 2594 | * This method adds nodes such as reports, blogs and participants |
7d2a0492 | 2595 | * |
3406acde SH |
2596 | * @param navigation_node $coursenode |
2597 | * @param stdClass $course | |
31fde54f | 2598 | * @return bool returns true on successful addition of a node. |
7d2a0492 | 2599 | */ |
2027c10e | 2600 | public function add_course_essentials($coursenode, stdClass $course) { |
3406acde | 2601 | global $CFG; |
7d2a0492 | 2602 | |
4766a50c | 2603 | if ($course->id == SITEID) { |
3406acde | 2604 | return $this->add_front_page_course_essentials($coursenode, $course); |
7d2a0492 | 2605 | } |
7d2a0492 | 2606 | |
2027c10e | 2607 | if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { |
3406acde | 2608 | return true; |
7d2a0492 | 2609 | } |
7d2a0492 | 2610 | |
3406acde SH |
2611 | //Participants |
2612 | if (has_capability('moodle/course:viewparticipants', $this->page->context)) { | |
3406acde SH |
2613 | $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2614 | $currentgroup = groups_get_course_group($course, true); | |
2615 | if ($course->id == SITEID) { | |
2616 | $filterselect = ''; | |
2617 | } else if ($course->id && !$currentgroup) { | |
2618 | $filterselect = $course->id; | |
2619 | } else { | |
2620 | $filterselect = $currentgroup; | |
2621 | } | |
2622 | $filterselect = clean_param($filterselect, PARAM_INT); | |
8f6c1f34 PS |
2623 | if (($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) |
2624 | and has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { | |
3406acde | 2625 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); |
ec21eaba | 2626 | $participants->add(get_string('blogscourse','blog'), $blogsurls->out()); |
3406acde SH |
2627 | } |
2628 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { | |
41eae0d9 | 2629 | $participants->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$course->id))); |
3406acde | 2630 | } |
533299cb | 2631 | } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { |
3406acde SH |
2632 | $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2633 | } | |
2634 | ||
2635 | // View course reports | |
2636 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
275cbac7 PS |
2637 | $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); |
2638 | $coursereports = get_plugin_list('coursereport'); // deprecated | |
3406acde SH |
2639 | foreach ($coursereports as $report=>$dir) { |
2640 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
2641 | if (file_exists($libfile)) { | |
2642 | require_once($libfile); | |
2643 | $reportfunction = $report.'_report_extend_navigation'; | |
2644 | if (function_exists($report.'_report_extend_navigation')) { | |
2645 | $reportfunction($reportnav, $course, $this->page->context); | |
7d2a0492 | 2646 | } |
2647 | } | |
2648 | } | |
275cbac7 PS |
2649 | |
2650 | $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); | |
2651 | foreach ($reports as $reportfunction) { | |
2652 | $reportfunction($reportnav, $course, $this->page->context); | |
2653 | } | |
7d2a0492 | 2654 | } |
2655 | return true; | |
2656 | } | |
deaf04c7 | 2657 | /** |
31fde54f | 2658 | * This generates the structure of the course that won't be generated when |
deaf04c7 SH |
2659 | * the modules and sections are added. |
2660 | * | |
2661 | * Things such as the reports branch, the participants branch, blogs... get | |
2662 | * added to the course node by this method. | |
2663 | * | |
2664 | * @param navigation_node $coursenode | |
2665 | * @param stdClass $course | |
2666 | * @return bool True for successfull generation | |
2667 | */ | |
3406acde SH |
2668 | public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { |
2669 | global $CFG; | |
7d2a0492 | 2670 | |
1fa692ed | 2671 | if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { |
3406acde | 2672 | return true; |
7a7e209d SH |
2673 | } |
2674 | ||
1fa692ed SH |
2675 | // Hidden node that we use to determine if the front page navigation is loaded. |
2676 | // This required as there are not other guaranteed nodes that may be loaded. | |
2677 | $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false; | |
2678 | ||
3406acde | 2679 | //Participants |
b475cf4c | 2680 | if (has_capability('moodle/course:viewparticipants', get_system_context())) { |
3406acde SH |
2681 | $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); |
2682 | } | |
435a512e | 2683 | |
83a5e4fc | 2684 | $filterselect = 0; |
593270c6 MD |
2685 | |
2686 | // Blogs | |
8f6c1f34 PS |
2687 | if (!empty($CFG->bloglevel) |
2688 | and ($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) | |
2689 | and has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { | |
2690 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); | |
ec21eaba | 2691 | $coursenode->add(get_string('blogssite','blog'), $blogsurls->out()); |
7d2a0492 | 2692 | } |
593270c6 MD |
2693 | |
2694 | // Notes | |
3406acde SH |
2695 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { |
2696 | $coursenode->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect))); | |
2697 | } | |
593270c6 MD |
2698 | |
2699 | // Tags | |
2700 | if (!empty($CFG->usetags) && isloggedin()) { | |
3406acde | 2701 | $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php')); |
7d2a0492 | 2702 | } |
6644741d | 2703 | |
e7f9b7ab SH |
2704 | if (isloggedin()) { |
2705 | // Calendar | |
2706 | $calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month')); | |
2707 | $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar'); | |
2708 | } | |
6644741d | 2709 | |
3406acde SH |
2710 | // View course reports |
2711 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
275cbac7 PS |
2712 | $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); |
2713 | $coursereports = get_plugin_list('coursereport'); // deprecated | |
3406acde SH |
2714 | foreach ($coursereports as $report=>$dir) { |
2715 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
2716 | if (file_exists($libfile)) { | |
2717 | require_once($libfile); | |
2718 | $reportfunction = $report.'_report_extend_navigation'; | |
2719 | if (function_exists($report.'_report_extend_navigation')) { | |
2720 | $reportfunction($reportnav, $course, $this->page->context); | |
2721 | } | |
6644741d | 2722 | } |
6644741d | 2723 | } |
275cbac7 PS |
2724 | |
2725 | $reports = get_plugin_list_with_function('report', 'extend_navigation_course', 'lib.php'); | |
2726 | foreach ($reports as $reportfunction) { | |
2727 | $reportfunction($reportnav, $course, $this->page->context); | |
2728 | } | |
6644741d | 2729 | } |
3406acde | 2730 | return true; |
6644741d | 2731 | } |
da3ab9c4 | 2732 | |
3406acde SH |
2733 | /** |
2734 | * Clears the navigation cache | |
2735 | */ | |
2736 | public function clear_cache() { | |
2737 | $this->cache->clear(); | |
da3ab9c4 | 2738 | } |
88f77c3c | 2739 | |
deaf04c7 SH |
2740 | /** |
2741 | * Sets an expansion limit for the navigation | |
2742 | * | |
2743 | * The expansion limit is used to prevent the display of content that has a type | |
2744 | * greater than the provided $type. | |
2745 | * | |
2746 | * Can be used to ensure things such as activities or activity content don't get | |
2747 | * shown on the navigation. | |
2748 | * They are still generated in order to ensure the navbar still makes sense. | |
2749 | * | |
2750 | * @param int $type One of navigation_node::TYPE_* | |
31fde54f | 2751 | * @return bool true when complete. |
deaf04c7 | 2752 | */ |
88f77c3c SH |
2753 | public function set_expansion_limit($type) { |
2754 | $nodes = $this->find_all_of_type($type); | |
2755 | foreach ($nodes as &$node) { | |
1af67ecb SH |
2756 | // We need to generate the full site node |
2757 | if ($type == self::TYPE_COURSE && $node->key == SITEID) { | |
2758 | continue; | |
2759 | } | |
88f77c3c | 2760 | foreach ($node->children as &$child) { |
1af67ecb SH |
2761 | // We still want to show course reports and participants containers |
2762 | // or there will be navigation missing. | |
2763 | if ($type == self::TYPE_COURSE && $child->type === self::TYPE_CONTAINER) { | |
2764 | continue; | |
2765 | } | |
88f77c3c SH |
2766 | $child->display = false; |
2767 | } | |
2768 | } | |
2769 | return true; | |
2770 | } | |
deaf04c7 SH |
2771 | /** |
2772 | * Attempts to get the navigation with the given key from this nodes children. | |
2773 | * | |
2774 | * This function only looks at this nodes children, it does NOT look recursivily. | |
2775 | * If the node can't be found then false is returned. | |
2776 | * | |
93123b17 | 2777 | * If you need to search recursivily then use the {@link global_navigation::find()} method. |
deaf04c7 | 2778 | * |
93123b17 | 2779 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2780 | * may be of more use to you. |
2781 | * | |
2782 | * @param string|int $key The key of the node you wish to receive. | |
2783 | * @param int $type One of navigation_node::TYPE_* | |
2784 | * @return navigation_node|false | |
2785 | */ | |
e2b436d0 | 2786 | public function get($key, $type = null) { |
246a9b05 SH |
2787 | if (!$this->initialised) { |
2788 | $this->initialise(); | |
2789 | } | |
54dc15ab | 2790 | return parent::get($key, $type); |
e2b436d0 SH |
2791 | } |
2792 | ||
deaf04c7 | 2793 | /** |
31fde54f | 2794 | * Searches this nodes children and their children to find a navigation node |
deaf04c7 SH |
2795 | * with the matching key and type. |
2796 | * | |
2797 | * This method is recursive and searches children so until either a node is | |
31fde54f | 2798 | * found or there are no more nodes to search. |
deaf04c7 SH |
2799 | * |
2800 | * If you know that the node being searched for is a child of this node | |
93123b17 | 2801 | * then use the {@link global_navigation::get()} method instead. |
deaf04c7 | 2802 | * |
93123b17 | 2803 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2804 | * may be of more use to you. |
2805 | * | |
2806 | * @param string|int $key The key of the node you wish to receive. | |
2807 | * @param int $type One of navigation_node::TYPE_* | |
2808 | * @return navigation_node|false | |
2809 | */ | |
e2b436d0 | 2810 | public function find($key, $type) { |
246a9b05 SH |
2811 | if (!$this->initialised) { |
2812 | $this->initialise(); | |
2813 | } | |
54dc15ab | 2814 | return parent::find($key, $type); |
e2b436d0 | 2815 | } |
7d2a0492 | 2816 | } |
2817 | ||
2818 | /** | |
2819 | * The limited global navigation class used for the AJAX extension of the global | |
2820 | * navigation class. | |
2821 | * | |
2822 | * The primary methods that are used in the global navigation class have been overriden | |
2823 | * to ensure that only the relevant branch is generated at the root of the tree. | |
2824 | * This can be done because AJAX is only used when the backwards structure for the | |
2825 | * requested branch exists. | |
2826 | * This has been done only because it shortens the amounts of information that is generated | |
2827 | * which of course will speed up the response time.. because no one likes laggy AJAX. | |
2828 | * | |
31fde54f AG |
2829 | * @package core |
2830 | * @category navigation | |
7d2a0492 | 2831 | * @copyright 2009 Sam Hemelryk |
2832 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2833 | */ | |
507a7a9a | 2834 | class global_navigation_for_ajax extends global_navigation { |
3406acde | 2835 | |
31fde54f | 2836 | /** @var int used for determining what type of navigation_node::TYPE_* is being used */ |
39ae5e54 | 2837 | protected $branchtype; |
31fde54f AG |
2838 | |
2839 | /** @var int the instance id */ | |
39ae5e54 SH |
2840 | protected $instanceid; |
2841 | ||
31fde54f | 2842 | /** @var array Holds an array of expandable nodes */ |
3406acde SH |
2843 | protected $expandable = array(); |
2844 | ||
7d2a0492 | 2845 | /** |
31fde54f AG |
2846 | * Constructs the navigation for use in an AJAX request |
2847 | * | |
2848 | * @param moodle_page $page moodle_page object | |
2849 | * @param int $branchtype | |
2850 | * @param int $id | |
3406acde | 2851 | */ |
246a9b05 | 2852 | public function __construct($page, $branchtype, $id) { |
4766a50c | 2853 | $this->page = $page; |
3406acde SH |
2854 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
2855 | $this->children = new navigation_node_collection(); | |
39ae5e54 SH |
2856 | $this->branchtype = $branchtype; |
2857 | $this->instanceid = $id; | |
2858 | $this->initialise(); | |
3406acde SH |
2859 | } |
2860 | /** | |
2861 | * Initialise the navigation given the type and id for the branch to expand. | |
7d2a0492 | 2862 | * |
31fde54f | 2863 | * @return array An array of the expandable nodes |
7d2a0492 | 2864 | */ |
39ae5e54 SH |
2865 | public function initialise() { |
2866 | global $CFG, $DB, $SITE; | |
507a7a9a | 2867 | |
7d2a0492 | 2868 | if ($this->initialised || during_initial_install()) { |
95b97515 | 2869 | return $this->expandable; |
7d2a0492 | 2870 | } |
246a9b05 SH |
2871 | $this->initialised = true; |
2872 | ||
2873 | $this->rootnodes = array(); | |
e26507b3 | 2874 | $this->rootnodes['site'] = $this->add_course($SITE); |
246a9b05 | 2875 | $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); |
507a7a9a SH |
2876 | |
2877 | // Branchtype will be one of navigation_node::TYPE_* | |
39ae5e54 | 2878 | switch ($this->branchtype) { |
4766a50c | 2879 | case self::TYPE_CATEGORY : |
39ae5e54 | 2880 | $this->load_all_categories($this->instanceid); |
4766a50c SH |
2881 | $limit = 20; |
2882 | if (!empty($CFG->navcourselimit)) { | |
2883 | $limit = (int)$CFG->navcourselimit; | |
2884 | } | |
39ae5e54 | 2885 | $courses = $DB->get_records('course', array('category' => $this->instanceid), 'sortorder','*', 0, $limit); |
4766a50c SH |
2886 | foreach ($courses as $course) { |
2887 | $this->add_course($course); | |
2888 | } | |
2889 | break; | |
507a7a9a | 2890 | case self::TYPE_COURSE : |
39ae5e54 | 2891 | $course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST); |
04bcd1ce | 2892 | require_course_login($course, true, null, false, true); |
87c215de | 2893 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
2894 | $coursenode = $this->add_course($course); |
2895 | $this->add_course_essentials($coursenode, $course); | |
2896 | if ($this->format_display_course_content($course->format)) { | |
2897 | $this->load_course_sections($course, $coursenode); | |
2898 | } | |
7d2a0492 | 2899 | break; |
507a7a9a | 2900 | case self::TYPE_SECTION : |
3406acde | 2901 | $sql = 'SELECT c.*, cs.section AS sectionnumber |
507a7a9a SH |
2902 | FROM {course} c |
2903 | LEFT JOIN {course_sections} cs ON cs.course = c.id | |
2904 | WHERE cs.id = ?'; | |
39ae5e54 | 2905 | $course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST); |
04bcd1ce | 2906 | require_course_login($course, true, null, false, true); |
87c215de | 2907 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
2908 | $coursenode = $this->add_course($course); |
2909 | $this->add_course_essentials($coursenode, $course); | |
2910 | $sections = $this->load_course_sections($course, $coursenode); | |
e26507b3 SH |
2911 | list($sectionarray, $activities) = $this->generate_sections_and_activities($course); |
2912 | $this->load_section_activities($sections[$course->sectionnumber]->sectionnode, $course->sectionnumber, $activities); | |
7d2a0492 | 2913 | break; |
507a7a9a | 2914 | case self::TYPE_ACTIVITY : |
c78262b5 SH |
2915 | $sql = "SELECT c.* |
2916 | FROM {course} c | |
2917 | JOIN {course_modules} cm ON cm.course = c.id | |
2918 | WHERE cm.id = :cmid"; | |
2919 | $params = array('cmid' => $this->instanceid); | |
2920 | $course = $DB->get_record_sql($sql, $params, MUST_EXIST); | |
f0dcc212 SH |
2921 | $modinfo = get_fast_modinfo($course); |
2922 | $cm = $modinfo->get_cm($this->instanceid); | |
04bcd1ce | 2923 | require_course_login($course, true, $cm, false, true); |
87c215de | 2924 | $this->page->set_context(get_context_instance(CONTEXT_MODULE, $cm->id)); |
3406acde | 2925 | $coursenode = $this->load_course($course); |
1aa1e9b5 SH |
2926 | if ($course->id == SITEID) { |
2927 | $modulenode = $this->load_activity($cm, $course, $coursenode->find($cm->id, self::TYPE_ACTIVITY)); | |
2928 | } else { | |
c78262b5 | 2929 | $sections = $this->load_course_sections($course, $coursenode); |
e26507b3 SH |
2930 | list($sectionarray, $activities) = $this->generate_sections_and_activities($course); |
2931 | $activities = $this->load_section_activities($sections[$cm->sectionnum]->sectionnode, $cm->sectionnum, $activities); | |
1aa1e9b5 SH |
2932 | $modulenode = $this->load_activity($cm, $course, $activities[$cm->id]); |
2933 | } | |
7d2a0492 | 2934 | break; |
507a7a9a | 2935 | default: |
3406acde | 2936 | throw new Exception('Unknown type'); |
507a7a9a | 2937 | return $this->expandable; |
7d2a0492 | 2938 | } |
588a3953 SH |
2939 | |
2940 | if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != SITEID) { | |
2941 | $this->load_for_user(null, true); | |
2942 | } | |
2943 | ||
507a7a9a | 2944 | $this->find_expandable($this->expandable); |
507a7a9a | 2945 | return $this->expandable; |
246a9b05 SH |
2946 | } |
2947 | ||
deaf04c7 SH |
2948 | /** |
2949 | * Returns an array of expandable nodes | |
2950 | * @return array | |
2951 | */ | |
246a9b05 SH |
2952 | public function get_expandable() { |
2953 | return $this->expandable; | |
7d2a0492 | 2954 | } |
7d2a0492 | 2955 | } |
2956 | ||
2957 | /** | |
2958 | * Navbar class | |
2959 | * | |
2960 | * This class is used to manage the navbar, which is initialised from the navigation | |
2961 | * object held by PAGE | |
2962 | * | |
31fde54f AG |
2963 | * @package core |
2964 | * @category navigation | |
7d2a0492 | 2965 | * @copyright 2009 Sam Hemelryk |
2966 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2967 | */ | |
2968 | class navbar extends navigation_node { | |
31fde54f | 2969 | /** @var bool A switch for whether the navbar is initialised or not */ |
7d2a0492 | 2970 | protected $initialised = false; |
31fde54f | 2971 | /** @var mixed keys used to reference the nodes on the navbar */ |
7d2a0492 | 2972 | protected $keys = array(); |
31fde54f | 2973 | /** @var null|string content of the navbar */ |
7d2a0492 | 2974 | protected $content = null; |
31fde54f | 2975 | /** @var moodle_page object the moodle page that this navbar belongs to */ |
7d2a0492 | 2976 | protected $page; |
31fde54f | 2977 | /** @var bool A switch for whether to ignore the active navigation information */ |
31759089 | 2978 | protected $ignoreactive = false; |
31fde54f | 2979 | /** @var bool A switch to let us know if we are in the middle of an install */ |
7d2a0492 | 2980 | protected $duringinstall = false; |
31fde54f | 2981 | /** @var bool A switch for whether the navbar has items */ |
7a7e209d | 2982 | protected $hasitems = false; |
31fde54f | 2983 | /** @var array An array of navigation nodes for the navbar */ |
3406acde | 2984 | protected $items; |
31fde54f | 2985 | /** @var array An array of child node objects */ |
3406acde | 2986 | public $children = array(); |
31fde54f | 2987 | /** @var bool A switch for whether we want to include the root node in the navbar */ |
4d5059d4 | 2988 | public $includesettingsbase = false; |
7d2a0492 | 2989 | /** |
2990 | * The almighty constructor | |
3406acde SH |
2991 | * |
2992 | * @param moodle_page $page | |
7d2a0492 | 2993 | */ |
3406acde | 2994 | public function __construct(moodle_page $page) { |
507a7a9a | 2995 | global $CFG; |
7d2a0492 | 2996 | if (during_initial_install()) { |
2997 | $this->duringinstall = true; | |
2998 | return false; | |
2999 | } | |
3000 | $this->page = $page; | |
3001 | $this->text = get_string('home'); | |
3002 | $this->shorttext = get_string('home'); | |
3003 | $this->action = new moodle_url($CFG->wwwroot); | |
3004 | $this->nodetype = self::NODETYPE_BRANCH; | |
3005 | $this->type = self::TYPE_SYSTEM; | |
3006 | } | |
3007 | ||
3008 | /** | |
3009 | * Quick check to see if the navbar will have items in. | |
3010 | * | |
3011 | * @return bool Returns true if the navbar will have items, false otherwise | |
3012 | */ | |
3013 | public function has_items() { | |
3014 | if ($this->duringinstall) { | |
3015 | return false; | |
7a7e209d SH |
3016 | } else if ($this->hasitems !== false) { |
3017 | return true; | |
7d2a0492 | 3018 | } |
3406acde | 3019 | $this->page->navigation->initialise($this->page); |
bf6c37c7 | 3020 | |
7a7e209d | 3021 | $activenodefound = ($this->page->navigation->contains_active_node() || |
3406acde | 3022 | $this->page->settingsnav->contains_active_node()); |
bf6c37c7 | 3023 | |
3406acde | 3024 | $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound)); |
7a7e209d | 3025 | $this->hasitems = $outcome; |
bf6c37c7 | 3026 | return $outcome; |
31759089 | 3027 | } |
3028 | ||
3406acde SH |
3029 | /** |
3030 | * Turn on/off ignore active | |
3031 | * | |
3032 | * @param bool $setting | |
3033 | */ | |
31759089 | 3034 | public function ignore_active($setting=true) { |
3035 | $this->ignoreactive = ($setting); | |
7d2a0492 | 3036 | } |
31fde54f AG |
3037 | |
3038 | /** | |
3039 | * Gets a navigation node | |
3040 | * | |
3041 | * @param string|int $key for referencing the navbar nodes | |
3042 | * @param int $type navigation_node::TYPE_* | |
3043 | * @return navigation_node|bool | |
3044 | */ | |
3406acde SH |
3045 | public function get($key, $type = null) { |
3046 | foreach ($this->children as &$child) { | |
3047 | if ($child->key === $key && ($type == null || $type == $child->type)) { | |
3048 | return $child; | |
3049 | } | |
3050 | } | |
3051 | return false; | |
3052 | } | |
7d2a0492 | 3053 | /** |
3406acde | 3054 | * Returns an array of navigation_node's that make up the navbar. |
435a512e | 3055 | * |
3406acde | 3056 | * @return array |
7d2a0492 | 3057 | */ |
3406acde SH |
3058 | public function get_items() { |
3059 | $items = array(); | |
7d2a0492 | 3060 | // Make sure that navigation is initialised |
7a7e209d | 3061 | if (!$this->has_items()) { |
3406acde | 3062 | return $items; |
7a7e209d | 3063 | } |
3406acde SH |
3064 | if ($this->items !== null) { |
3065 | return $this->items; | |
7d2a0492 | 3066 | } |
3067 | ||
3406acde SH |
3068 | if (count($this->children) > 0) { |
3069 | // Add the custom children | |
3070 | $items = array_reverse($this->children); | |
3071 | } | |
117bd748 | 3072 | |
3406acde SH |
3073 | $navigationactivenode = $this->page->navigation->find_active_node(); |
3074 | $settingsactivenode = $this->page->settingsnav->find_active_node(); | |
0b29477b | 3075 | |
7d2a0492 | 3076 | // Check if navigation contains the active node |
0b29477b | 3077 | if (!$this->ignoreactive) { |
435a512e | 3078 | |
3406acde | 3079 | if ($navigationactivenode && $settingsactivenode) { |
0b29477b | 3080 | // Parse a combined navigation tree |
3406acde SH |
3081 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
3082 | if (!$settingsactivenode->mainnavonly) { | |
3083 | $items[] = $settingsactivenode; | |
3084 | } | |
3085 | $settingsactivenode = $settingsactivenode->parent; | |
3086 | } | |
4d5059d4 SH |
3087 | if (!$this->includesettingsbase) { |
3088 | // Removes the first node from the settings (root node) from the list | |
3089 | array_pop($items); | |
3090 | } | |
3406acde SH |
3091 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
3092 | if (!$navigationactivenode->mainnavonly) { | |
3093 | $items[] = $navigationactivenode; | |
3094 | } | |
3095 | $navigationactivenode = $navigationactivenode->parent; | |
0b29477b | 3096 | } |
3406acde | 3097 | } else if ($navigationactivenode) { |
0b29477b | 3098 | // Parse the navigation tree to get the active node |
3406acde SH |
3099 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
3100 | if (!$navigationactivenode->mainnavonly) { | |
3101 | $items[] = $navigationactivenode; | |
3102 | } | |
3103 | $navigationactivenode = $navigationactivenode->parent; | |
3104 | } | |
3105 | } else if ($settingsactivenode) { | |
0b29477b | 3106 | // Parse the settings navigation to get the active node |
3406acde SH |
3107 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
3108 | if (!$settingsactivenode->mainnavonly) { | |
3109 | $items[] = $settingsactivenode; | |
3110 | } | |
3111 | $settingsactivenode = $settingsactivenode->parent; | |
3112 | } | |
0b29477b | 3113 | } |
7d2a0492 | 3114 | } |
a3bbac8b | 3115 | |
3406acde SH |
3116 | $items[] = new navigation_node(array( |
3117 | 'text'=>$this->page->navigation->text, | |
3118 | 'shorttext'=>$this->page->navigation->shorttext, | |
3119 | 'key'=>$this->page->navigation->key, | |
3120 | 'action'=>$this->page->navigation->action | |
3121 | )); | |
a3bbac8b | 3122 | |
3406acde SH |
3123 | $this->items = array_reverse($items); |
3124 | return $this->items; | |
7d2a0492 | 3125 | } |
507a7a9a | 3126 | |
7d2a0492 | 3127 | /** |
3406acde | 3128 | * Add a new navigation_node to the navbar, overrides parent::add |
7d2a0492 | 3129 | * |
3130 | * This function overrides {@link navigation_node::add()} so that we can change | |
3131 | * the way nodes get added to allow us to simply call add and have the node added to the | |
3132 | * end of the navbar | |
3133 | * | |
3134 | * @param string $text | |
7d2a0492 | 3135 | * @param string|moodle_url $action |
d972bad1 | 3136 | * @param int $type |
3137 | * @param string|int $key | |
3138 | * @param string $shorttext | |
7d2a0492 | 3139 | * @param string $icon |
3406acde | 3140 | * @return navigation_node |
7d2a0492 | 3141 | */ |
f9fc1461 | 3142 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { |
3406acde SH |
3143 | if ($this->content !== null) { |
3144 | debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); | |
3145 | } | |
435a512e | 3146 | |
3406acde SH |
3147 | // Properties array used when creating the new navigation node |
3148 | $itemarray = array( | |
3149 | 'text' => $text, | |
3150 | 'type' => $type | |
3151 | ); | |
3152 | // Set the action if one was provided | |