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