forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigationlib.php
4564 lines (4202 loc) · 194 KB
/
navigationlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains classes used to manage the navigation structures within Moodle.
*
* @since 2.0
* @package core
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* The name that will be used to separate the navigation cache within SESSION
*/
define('NAVIGATION_CACHE_NAME', 'navigation');
/**
* This class is used to represent a node in a navigation tree
*
* This class is used to represent a node in a navigation tree within Moodle,
* the tree could be one of global navigation, settings navigation, or the navbar.
* Each node can be one of two types either a Leaf (default) or a branch.
* When a node is first created it is created as a leaf, when/if children are added
* the node then becomes a branch.
*
* @package core
* @category navigation
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class navigation_node implements renderable {
/** @var int Used to identify this node a leaf (default) 0 */
const NODETYPE_LEAF = 0;
/** @var int Used to identify this node a branch, happens with children 1 */
const NODETYPE_BRANCH = 1;
/** @var null Unknown node type null */
const TYPE_UNKNOWN = null;
/** @var int System node type 0 */
const TYPE_ROOTNODE = 0;
/** @var int System node type 1 */
const TYPE_SYSTEM = 1;
/** @var int Category node type 10 */
const TYPE_CATEGORY = 10;
/** var int Category displayed in MyHome navigation node */
const TYPE_MY_CATEGORY = 11;
/** @var int Course node type 20 */
const TYPE_COURSE = 20;
/** @var int Course Structure node type 30 */
const TYPE_SECTION = 30;
/** @var int Activity node type, e.g. Forum, Quiz 40 */
const TYPE_ACTIVITY = 40;
/** @var int Resource node type, e.g. Link to a file, or label 50 */
const TYPE_RESOURCE = 50;
/** @var int A custom node type, default when adding without specifing type 60 */
const TYPE_CUSTOM = 60;
/** @var int Setting node type, used only within settings nav 70 */
const TYPE_SETTING = 70;
/** @var int Setting node type, used only within settings nav 80 */
const TYPE_USER = 80;
/** @var int Setting node type, used for containers of no importance 90 */
const TYPE_CONTAINER = 90;
/** var int Course the current user is not enrolled in */
const COURSE_OTHER = 0;
/** var int Course the current user is enrolled in but not viewing */
const COURSE_MY = 1;
/** var int Course the current user is currently viewing */
const COURSE_CURRENT = 2;
/** @var int Parameter to aid the coder in tracking [optional] */
public $id = null;
/** @var string|int The identifier for the node, used to retrieve the node */
public $key = null;
/** @var string The text to use for the node */
public $text = null;
/** @var string Short text to use if requested [optional] */
public $shorttext = null;
/** @var string The title attribute for an action if one is defined */
public $title = null;
/** @var string A string that can be used to build a help button */
public $helpbutton = null;
/** @var moodle_url|action_link|null An action for the node (link) */
public $action = null;
/** @var pix_icon The path to an icon to use for this node */
public $icon = null;
/** @var int See TYPE_* constants defined for this class */
public $type = self::TYPE_UNKNOWN;
/** @var int See NODETYPE_* constants defined for this class */
public $nodetype = self::NODETYPE_LEAF;
/** @var bool If set to true the node will be collapsed by default */
public $collapse = false;
/** @var bool If set to true the node will be expanded by default */
public $forceopen = false;
/** @var array An array of CSS classes for the node */
public $classes = array();
/** @var navigation_node_collection An array of child nodes */
public $children = array();
/** @var bool If set to true the node will be recognised as active */
public $isactive = false;
/** @var bool If set to true the node will be dimmed */
public $hidden = false;
/** @var bool If set to false the node will not be displayed */
public $display = true;
/** @var bool If set to true then an HR will be printed before the node */
public $preceedwithhr = false;
/** @var bool If set to true the the navigation bar should ignore this node */
public $mainnavonly = false;
/** @var bool If set to true a title will be added to the action no matter what */
public $forcetitle = false;
/** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */
public $parent = null;
/** @var bool Override to not display the icon even if one is provided **/
public $hideicon = false;
/** @var bool Set to true if we KNOW that this node can be expanded. */
public $isexpandable = false;
/** @var array */
protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user');
/** @var moodle_url */
protected static $fullmeurl = null;
/** @var bool toogles auto matching of active node */
public static $autofindactive = true;
/** @var mixed If set to an int, that section will be included even if it has no activities */
public $includesectionnum = false;
/**
* Constructs a new navigation_node
*
* @param array|string $properties Either an array of properties or a string to use
* as the text for the node
*/
public function __construct($properties) {
if (is_array($properties)) {
// Check the array for each property that we allow to set at construction.
// text - The main content for the node
// shorttext - A short text if required for the node
// icon - The icon to display for the node
// type - The type of the node
// key - The key to use to identify the node
// parent - A reference to the nodes parent
// action - The action to attribute to this node, usually a URL to link to
if (array_key_exists('text', $properties)) {
$this->text = $properties['text'];
}
if (array_key_exists('shorttext', $properties)) {
$this->shorttext = $properties['shorttext'];
}
if (!array_key_exists('icon', $properties)) {
$properties['icon'] = new pix_icon('i/navigationitem', '');
}
$this->icon = $properties['icon'];
if ($this->icon instanceof pix_icon) {
if (empty($this->icon->attributes['class'])) {
$this->icon->attributes['class'] = 'navicon';
} else {
$this->icon->attributes['class'] .= ' navicon';
}
}
if (array_key_exists('type', $properties)) {
$this->type = $properties['type'];
} else {
$this->type = self::TYPE_CUSTOM;
}
if (array_key_exists('key', $properties)) {
$this->key = $properties['key'];
}
// This needs to happen last because of the check_if_active call that occurs
if (array_key_exists('action', $properties)) {
$this->action = $properties['action'];
if (is_string($this->action)) {
$this->action = new moodle_url($this->action);
}
if (self::$autofindactive) {
$this->check_if_active();
}
}
if (array_key_exists('parent', $properties)) {
$this->set_parent($properties['parent']);
}
} else if (is_string($properties)) {
$this->text = $properties;
}
if ($this->text === null) {
throw new coding_exception('You must set the text for the node when you create it.');
}
// Instantiate a new navigation node collection for this nodes children
$this->children = new navigation_node_collection();
}
/**
* Checks if this node is the active node.
*
* This is determined by comparing the action for the node against the
* defined URL for the page. A match will see this node marked as active.
*
* @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE
* @return bool
*/
public function check_if_active($strength=URL_MATCH_EXACT) {
global $FULLME, $PAGE;
// Set fullmeurl if it hasn't already been set
if (self::$fullmeurl == null) {
if ($PAGE->has_set_url()) {
self::override_active_url(new moodle_url($PAGE->url));
} else {
self::override_active_url(new moodle_url($FULLME));
}
}
// Compare the action of this node against the fullmeurl
if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) {
$this->make_active();
return true;
}
return false;
}
/**
* This sets the URL that the URL of new nodes get compared to when locating
* the active node.
*
* The active node is the node that matches the URL set here. By default this
* is either $PAGE->url or if that hasn't been set $FULLME.
*
* @param moodle_url $url The url to use for the fullmeurl.
*/
public static function override_active_url(moodle_url $url) {
// Clone the URL, in case the calling script changes their URL later.
self::$fullmeurl = new moodle_url($url);
}
/**
* Creates a navigation node, ready to add it as a child using add_node
* function. (The created node needs to be added before you can use it.)
* @param string $text
* @param moodle_url|action_link $action
* @param int $type
* @param string $shorttext
* @param string|int $key
* @param pix_icon $icon
* @return navigation_node
*/
public static function create($text, $action=null, $type=self::TYPE_CUSTOM,
$shorttext=null, $key=null, pix_icon $icon=null) {
// Properties array used when creating the new navigation node
$itemarray = array(
'text' => $text,
'type' => $type
);
// Set the action if one was provided
if ($action!==null) {
$itemarray['action'] = $action;
}
// Set the shorttext if one was provided
if ($shorttext!==null) {
$itemarray['shorttext'] = $shorttext;
}
// Set the icon if one was provided
if ($icon!==null) {
$itemarray['icon'] = $icon;
}
// Set the key
$itemarray['key'] = $key;
// Construct and return
return new navigation_node($itemarray);
}
/**
* Adds a navigation node as a child of this node.
*
* @param string $text
* @param moodle_url|action_link $action
* @param int $type
* @param string $shorttext
* @param string|int $key
* @param pix_icon $icon
* @return navigation_node
*/
public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
// Create child node
$childnode = self::create($text, $action, $type, $shorttext, $key, $icon);
// Add the child to end and return
return $this->add_node($childnode);
}
/**
* Adds a navigation node as a child of this one, given a $node object
* created using the create function.
* @param navigation_node $childnode Node to add
* @param string $beforekey
* @return navigation_node The added node
*/
public function add_node(navigation_node $childnode, $beforekey=null) {
// First convert the nodetype for this node to a branch as it will now have children
if ($this->nodetype !== self::NODETYPE_BRANCH) {
$this->nodetype = self::NODETYPE_BRANCH;
}
// Set the parent to this node
$childnode->set_parent($this);
// Default the key to the number of children if not provided
if ($childnode->key === null) {
$childnode->key = $this->children->count();
}
// Add the child using the navigation_node_collections add method
$node = $this->children->add($childnode, $beforekey);
// If added node is a category node or the user is logged in and it's a course
// then mark added node as a branch (makes it expandable by AJAX)
$type = $childnode->type;
if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY)) {
$node->nodetype = self::NODETYPE_BRANCH;
}
// If this node is hidden mark it's children as hidden also
if ($this->hidden) {
$node->hidden = true;
}
// Return added node (reference returned by $this->children->add()
return $node;
}
/**
* Return a list of all the keys of all the child nodes.
* @return array the keys.
*/
public function get_children_key_list() {
return $this->children->get_key_list();
}
/**
* Searches for a node of the given type with the given key.
*
* This searches this node plus all of its children, and their children....
* If you know the node you are looking for is a child of this node then please
* use the get method instead.
*
* @param int|string $key The key of the node we are looking for
* @param int $type One of navigation_node::TYPE_*
* @return navigation_node|false
*/
public function find($key, $type) {
return $this->children->find($key, $type);
}
/**
* Get the child of this node that has the given key + (optional) type.
*
* If you are looking for a node and want to search all children + thier children
* then please use the find method instead.
*
* @param int|string $key The key of the node we are looking for
* @param int $type One of navigation_node::TYPE_*
* @return navigation_node|false
*/
public function get($key, $type=null) {
return $this->children->get($key, $type);
}
/**
* Removes this node.
*
* @return bool
*/
public function remove() {
return $this->parent->children->remove($this->key, $this->type);
}
/**
* Checks if this node has or could have any children
*
* @return bool Returns true if it has children or could have (by AJAX expansion)
*/
public function has_children() {
return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0 || $this->isexpandable);
}
/**
* Marks this node as active and forces it open.
*
* Important: If you are here because you need to mark a node active to get
* the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}?
* You can use it to specify a different URL to match the active navigation node on
* rather than having to locate and manually mark a node active.
*/
public function make_active() {
$this->isactive = true;
$this->add_class('active_tree_node');
$this->force_open();
if ($this->parent !== null) {
$this->parent->make_inactive();
}
}
/**
* Marks a node as inactive and recusised back to the base of the tree
* doing the same to all parents.
*/
public function make_inactive() {
$this->isactive = false;
$this->remove_class('active_tree_node');
if ($this->parent !== null) {
$this->parent->make_inactive();
}
}
/**
* Forces this node to be open and at the same time forces open all
* parents until the root node.
*
* Recursive.
*/
public function force_open() {
$this->forceopen = true;
if ($this->parent !== null) {
$this->parent->force_open();
}
}
/**
* Adds a CSS class to this node.
*
* @param string $class
* @return bool
*/
public function add_class($class) {
if (!in_array($class, $this->classes)) {
$this->classes[] = $class;
}
return true;
}
/**
* Removes a CSS class from this node.
*
* @param string $class
* @return bool True if the class was successfully removed.
*/
public function remove_class($class) {
if (in_array($class, $this->classes)) {
$key = array_search($class,$this->classes);
if ($key!==false) {
unset($this->classes[$key]);
return true;
}
}
return false;
}
/**
* Sets the title for this node and forces Moodle to utilise it.
* @param string $title
*/
public function title($title) {
$this->title = $title;
$this->forcetitle = true;
}
/**
* Resets the page specific information on this node if it is being unserialised.
*/
public function __wakeup(){
$this->forceopen = false;
$this->isactive = false;
$this->remove_class('active_tree_node');
}
/**
* Checks if this node or any of its children contain the active node.
*
* Recursive.
*
* @return bool
*/
public function contains_active_node() {
if ($this->isactive) {
return true;
} else {
foreach ($this->children as $child) {
if ($child->isactive || $child->contains_active_node()) {
return true;
}
}
}
return false;
}
/**
* Finds the active node.
*
* Searches this nodes children plus all of the children for the active node
* and returns it if found.
*
* Recursive.
*
* @return navigation_node|false
*/
public function find_active_node() {
if ($this->isactive) {
return $this;
} else {
foreach ($this->children as &$child) {
$outcome = $child->find_active_node();
if ($outcome !== false) {
return $outcome;
}
}
}
return false;
}
/**
* Searches all children for the best matching active node
* @return navigation_node|false
*/
public function search_for_active_node() {
if ($this->check_if_active(URL_MATCH_BASE)) {
return $this;
} else {
foreach ($this->children as &$child) {
$outcome = $child->search_for_active_node();
if ($outcome !== false) {
return $outcome;
}
}
}
return false;
}
/**
* Gets the content for this node.
*
* @param bool $shorttext If true shorttext is used rather than the normal text
* @return string
*/
public function get_content($shorttext=false) {
if ($shorttext && $this->shorttext!==null) {
return format_string($this->shorttext);
} else {
return format_string($this->text);
}
}
/**
* Gets the title to use for this node.
*
* @return string
*/
public function get_title() {
if ($this->forcetitle || $this->action != null){
return $this->title;
} else {
return '';
}
}
/**
* Gets the CSS class to add to this node to describe its type
*
* @return string
*/
public function get_css_type() {
if (array_key_exists($this->type, $this->namedtypes)) {
return 'type_'.$this->namedtypes[$this->type];
}
return 'type_unknown';
}
/**
* Finds all nodes that are expandable by AJAX
*
* @param array $expandable An array by reference to populate with expandable nodes.
*/
public function find_expandable(array &$expandable) {
foreach ($this->children as &$child) {
if ($child->display && $child->has_children() && $child->children->count() == 0) {
$child->id = 'expandable_branch_'.(count($expandable)+1);
$this->add_class('canexpand');
$expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type);
}
$child->find_expandable($expandable);
}
}
/**
* Finds all nodes of a given type (recursive)
*
* @param int $type One of navigation_node::TYPE_*
* @return array
*/
public function find_all_of_type($type) {
$nodes = $this->children->type($type);
foreach ($this->children as &$node) {
$childnodes = $node->find_all_of_type($type);
$nodes = array_merge($nodes, $childnodes);
}
return $nodes;
}
/**
* Removes this node if it is empty
*/
public function trim_if_empty() {
if ($this->children->count() == 0) {
$this->remove();
}
}
/**
* Creates a tab representation of this nodes children that can be used
* with print_tabs to produce the tabs on a page.
*
* call_user_func_array('print_tabs', $node->get_tabs_array());
*
* @param array $inactive
* @param bool $return
* @return array Array (tabs, selected, inactive, activated, return)
*/
public function get_tabs_array(array $inactive=array(), $return=false) {
$tabs = array();
$rows = array();
$selected = null;
$activated = array();
foreach ($this->children as $node) {
$tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title());
if ($node->contains_active_node()) {
if ($node->children->count() > 0) {
$activated[] = $node->key;
foreach ($node->children as $child) {
if ($child->contains_active_node()) {
$selected = $child->key;
}
$rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title());
}
} else {
$selected = $node->key;
}
}
}
return array(array($tabs, $rows), $selected, $inactive, $activated, $return);
}
/**
* Sets the parent for this node and if this node is active ensures that the tree is properly
* adjusted as well.
*
* @param navigation_node $parent
*/
public function set_parent(navigation_node $parent) {
// Set the parent (thats the easy part)
$this->parent = $parent;
// Check if this node is active (this is checked during construction)
if ($this->isactive) {
// Force all of the parent nodes open so you can see this node
$this->parent->force_open();
// Make all parents inactive so that its clear where we are.
$this->parent->make_inactive();
}
}
/**
* Hides the node and any children it has.
*
* @since 2.5
*/
public function hide() {
$this->display = false;
if ($this->has_children()) {
foreach ($this->children as $child) {
$child->hide();
}
}
}
}
/**
* Navigation node collection
*
* This class is responsible for managing a collection of navigation nodes.
* It is required because a node's unique identifier is a combination of both its
* key and its type.
*
* Originally an array was used with a string key that was a combination of the two
* however it was decided that a better solution would be to use a class that
* implements the standard IteratorAggregate interface.
*
* @package core
* @category navigation
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class navigation_node_collection implements IteratorAggregate {
/**
* A multidimensional array to where the first key is the type and the second
* key is the nodes key.
* @var array
*/
protected $collection = array();
/**
* An array that contains references to nodes in the same order they were added.
* This is maintained as a progressive array.
* @var array
*/
protected $orderedcollection = array();
/**
* A reference to the last node that was added to the collection
* @var navigation_node
*/
protected $last = null;
/**
* The total number of items added to this array.
* @var int
*/
protected $count = 0;
/**
* Adds a navigation node to the collection
*
* @param navigation_node $node Node to add
* @param string $beforekey If specified, adds before a node with this key,
* otherwise adds at end
* @return navigation_node Added node
*/
public function add(navigation_node $node, $beforekey=null) {
global $CFG;
$key = $node->key;
$type = $node->type;
// First check we have a 2nd dimension for this type
if (!array_key_exists($type, $this->orderedcollection)) {
$this->orderedcollection[$type] = array();
}
// Check for a collision and report if debugging is turned on
if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) {
debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER);
}
// Find the key to add before
$newindex = $this->count;
$last = true;
if ($beforekey !== null) {
foreach ($this->collection as $index => $othernode) {
if ($othernode->key === $beforekey) {
$newindex = $index;
$last = false;
break;
}
}
if ($newindex === $this->count) {
debugging('Navigation node add_before: Reference node not found ' . $beforekey .
', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER);
}
}
// Add the node to the appropriate place in the by-type structure (which
// is not ordered, despite the variable name)
$this->orderedcollection[$type][$key] = $node;
if (!$last) {
// Update existing references in the ordered collection (which is the
// one that isn't called 'ordered') to shuffle them along if required
for ($oldindex = $this->count; $oldindex > $newindex; $oldindex--) {
$this->collection[$oldindex] = $this->collection[$oldindex - 1];
}
}
// Add a reference to the node to the progressive collection.
$this->collection[$newindex] = $this->orderedcollection[$type][$key];
// Update the last property to a reference to this new node.
$this->last = $this->orderedcollection[$type][$key];
// Reorder the array by index if needed
if (!$last) {
ksort($this->collection);
}
$this->count++;
// Return the reference to the now added node
return $node;
}
/**
* Return a list of all the keys of all the nodes.
* @return array the keys.
*/
public function get_key_list() {
$keys = array();
foreach ($this->collection as $node) {
$keys[] = $node->key;
}
return $keys;
}
/**
* Fetches a node from this collection.
*
* @param string|int $key The key of the node we want to find.
* @param int $type One of navigation_node::TYPE_*.
* @return navigation_node|null
*/
public function get($key, $type=null) {
if ($type !== null) {
// If the type is known then we can simply check and fetch
if (!empty($this->orderedcollection[$type][$key])) {
return $this->orderedcollection[$type][$key];
}
} else {
// Because we don't know the type we look in the progressive array
foreach ($this->collection as $node) {
if ($node->key === $key) {
return $node;
}
}
}
return false;
}
/**
* Searches for a node with matching key and type.
*
* This function searches both the nodes in this collection and all of
* the nodes in each collection belonging to the nodes in this collection.
*
* Recursive.
*
* @param string|int $key The key of the node we want to find.
* @param int $type One of navigation_node::TYPE_*.
* @return navigation_node|null
*/
public function find($key, $type=null) {
if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) {
return $this->orderedcollection[$type][$key];
} else {
$nodes = $this->getIterator();
// Search immediate children first
foreach ($nodes as &$node) {
if ($node->key === $key && ($type === null || $type === $node->type)) {
return $node;
}
}
// Now search each childs children
foreach ($nodes as &$node) {
$result = $node->children->find($key, $type);
if ($result !== false) {
return $result;
}
}
}
return false;
}
/**
* Fetches the last node that was added to this collection
*
* @return navigation_node
*/
public function last() {
return $this->last;
}
/**
* Fetches all nodes of a given type from this collection
*
* @param string|int $type node type being searched for.
* @return array ordered collection
*/
public function type($type) {
if (!array_key_exists($type, $this->orderedcollection)) {
$this->orderedcollection[$type] = array();
}
return $this->orderedcollection[$type];
}
/**
* Removes the node with the given key and type from the collection
*
* @param string|int $key The key of the node we want to find.
* @param int $type
* @return bool
*/
public function remove($key, $type=null) {
$child = $this->get($key, $type);
if ($child !== false) {
foreach ($this->collection as $colkey => $node) {
if ($node->key == $key && $node->type == $type) {
unset($this->collection[$colkey]);
break;
}
}
unset($this->orderedcollection[$child->type][$child->key]);
$this->count--;
return true;
}
return false;
}
/**
* Gets the number of nodes in this collection
*
* This option uses an internal count rather than counting the actual options to avoid
* a performance hit through the count function.
*
* @return int
*/
public function count() {
return $this->count;
}
/**
* Gets an array iterator for the collection.
*
* This is required by the IteratorAggregator interface and is used by routines
* such as the foreach loop.
*
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->collection);
}
}
/**
* The global navigation class used for... the global navigation
*
* This class is used by PAGE to store the global navigation for the site
* and is then used by the settings nav and navbar to save on processing and DB calls
*
* See
* {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()}
* {@link lib/ajax/getnavbranch.php} Called by ajax
*
* @package core
* @category navigation
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class global_navigation extends navigation_node {
/** @var moodle_page The Moodle page this navigation object belongs to. */
protected $page;
/** @var bool switch to let us know if the navigation object is initialised*/
protected $initialised = false;
/** @var array An array of course information */
protected $mycourses = array();
/** @var array An array for containing root navigation nodes */
protected $rootnodes = array();
/** @var bool A switch for whether to show empty sections in the navigation */
protected $showemptysections = true;
/** @var bool A switch for whether courses should be shown within categories on the navigation. */
protected $showcategories = null;
/** @var null@var bool A switch for whether or not to show categories in the my courses branch. */
protected $showmycategories = null;
/** @var array An array of stdClasses for users that the navigation is extended for */
protected $extendforuser = array();
/** @var navigation_cache */
protected $cache;
/** @var array An array of course ids that are present in the navigation */
protected $addedcourses = array();
/** @var bool */
protected $allcategoriesloaded = false;
/** @var array An array of category ids that are included in the navigation */
protected $addedcategories = array();
/** @var int expansion limit */
protected $expansionlimit = 0;
/** @var int userid to allow parent to see child's profile page navigation */
protected $useridtouseforparentchecks = 0;
/** Used when loading categories to load all top level categories [parent = 0] **/
const LOAD_ROOT_CATEGORIES = 0;
/** Used when loading categories to load all categories **/
const LOAD_ALL_CATEGORIES = -1;
/**
* Constructs a new global navigation
*
* @param moodle_page $page The page this navigation object belongs to
*/
public function __construct(moodle_page $page) {
global $CFG, $SITE, $USER;
if (during_initial_install()) {
return;
}
if (get_home_page() == HOMEPAGE_SITE) {
// We are using the site home for the root element
$properties = array(
'key' => 'home',
'type' => navigation_node::TYPE_SYSTEM,
'text' => get_string('home'),
'action' => new moodle_url('/')