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