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