forked from moodle/moodle
/
pagelib.php
1896 lines (1658 loc) · 68.5 KB
/
pagelib.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 the moodle_page class. There is normally a single instance
* of this class in the $PAGE global variable. This class is a central repository
* of information about the page we are building up to send back to the user.
*
* @package core
* @subpackage lib
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* $PAGE is a central store of information about the current page we are
* generating in response to the user's request.
*
* It does not do very much itself
* except keep track of information, however, it serves as the access point to
* some more significant components like $PAGE->theme, $PAGE->requires,
* $PAGE->blocks, etc.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*
* @property-read string $activityname The type of activity we are in, for example 'forum' or 'quiz'.
* Will be null if this page is not within a module.
* @property-read object $activityrecord The row from the activities own database table (for example
* the forum or quiz table) that this page belongs to. Will be null
* if this page is not within a module.
* @property-read array $alternativeversions Mime type => object with ->url and ->title.
* @property-read blocks_manager $blocks The blocks manager object for this page.
* @property-read string $bodyclasses Returns a string to use within the class attribute on the body tag.
* @property-read string $button The HTML to go where the Turn editing on button normally goes.
* @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
* @property-read array $categories An array of all the categories the page course belongs to,
* starting with the immediately containing category, and working out to
* the top-level category. This may be the empty array if we are in the
* front page course.
* @property-read mixed $category The category that the page course belongs to. If there isn't one returns null.
* @property-read object $cm The course_module that this page belongs to. Will be null
* if this page is not within a module. This is a full cm object, as loaded
* by get_coursemodule_from_id or get_coursemodule_from_instance,
* so the extra modname and name fields are present.
* @property-read object $context The main context to which this page belongs.
* @property-read object $course The current course that we are inside - a row from the
* course table. (Also available as $COURSE global.) If we are not inside
* an actual course, this will be the site course.
* @property-read bool $devicetypeinuse name of the device type in use
* @property-read string $docspath The path to the Moodle docs for this page.
* @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
* @property-read bool $headerprinted
* @property-read string $heading The main heading that should be displayed at the top of the <body>.
* @property-read string $headingmenu The menu (or actions) to display in the heading
* @property-read array $layout_options Returns arrays with options for layout file.
* @property-read navbar $navbar Returns the navbar object used to display the navbar
* @property-read global_navigation $navigation Returns the global navigation structure
* @property-read xml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
* mainly for internal use by the rendering code.
* @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
* Allows the theme to display things differently, if it wishes to.
* @property-read string $pagetype Returns the page type string, should be used as the id for the body tag in the theme.
* @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
* @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
* @property-read settings_navigation $settingsnav
* @property-read int $state One of the STATE_... constants
* @property-read string $subpage The subpage identifier, if any.
* @property-read theme_config $theme Returns the initialised theme for this page.
* @property-read string $title The title that should go in the <head> section of the HTML of this page.
* @property-read moodle_url $url The moodle url object for this page.
*/
class moodle_page {
/**#@+ Tracks the where we are in the generation of the page. */
const STATE_BEFORE_HEADER = 0;
const STATE_PRINTING_HEADER = 1;
const STATE_IN_BODY = 2;
const STATE_DONE = 3;
/**#@-*/
/// Field declarations =========================================================
protected $_state = self::STATE_BEFORE_HEADER;
protected $_course = null;
/**
* If this page belongs to a module, this is the cm_info module description object.
* @var cm_info
*/
protected $_cm = null;
/**
* If $_cm is not null, then this will hold the corresponding row from the
* modname table. For example, if $_cm->modname is 'quiz', this will be a
* row from the quiz table.
*/
protected $_module = null;
/**
* The context that this page belongs to.
*/
protected $_context = null;
/**
* This holds any categories that $_course belongs to, starting with the
* particular category it belongs to, and working out through any parent
* categories to the top level. These are loaded progressively, if needed.
* There are three states. $_categories = null initially when nothing is
* loaded; $_categories = array($id => $cat, $parentid => null) when we have
* loaded $_course->category, but not any parents; and a complete array once
* everything is loaded.
*/
protected $_categories = null;
protected $_bodyclasses = array();
protected $_title = '';
protected $_heading = '';
protected $_pagetype = null;
protected $_pagelayout = 'base';
/**
* List of theme layout options, these are ignored by core.
* To be used in individual theme layout files only.
* @var array
*/
protected $_layout_options = array();
protected $_subpage = '';
protected $_docspath = null;
/** @var string|null A legacy class that will be added to the body tag */
protected $_legacyclass = null;
protected $_url = null;
protected $_alternateversions = array();
protected $_blocks = null;
protected $_requires = null;
protected $_blockseditingcap = 'moodle/site:manageblocks';
protected $_block_actions_done = false;
protected $_othereditingcaps = array();
protected $_cacheable = true;
protected $_focuscontrol = '';
protected $_button = '';
protected $_theme = null;
/** @var global_navigation Contains the global navigation structure*/
protected $_navigation = null;
/** @var null|settings_navigation Contains the settings navigation structure*/
protected $_settingsnav = null;
/** @var null|navbar Contains the navbar structure*/
protected $_navbar = null;
/** @var string */
protected $_headingmenu = null;
/**
* Then the theme is initialised, we save the stack trace, for use in error messages.
* @var array stack trace.
*/
protected $_wherethemewasinitialised = null;
/** @var xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed. */
protected $_opencontainers;
/**
* Sets the page to refresh after a given delay (in seconds) using meta refresh
* in {@link standard_head_html()} in outputlib.php
* If set to null(default) the page is not refreshed
* @var int|null
*/
protected $_periodicrefreshdelay = null;
/**
* This is simply to improve backwards compatibility. If old code relies on
* a page class that implements print_header, or complex logic in
* user_allowed_editing then we stash an instance of that other class here,
* and delegate to it in certain situations.
*/
protected $_legacypageobject = null;
/**
* Associative array of browser shortnames (as used by check_browser_version)
* and their minimum required versions
* @var array
*/
protected $_legacybrowsers = array('MSIE' => 6.0);
/**
* Is set to the name of the device type in use.
* @var string
*/
protected $_devicetypeinuse = 'default';
protected $_https_login_required = false;
protected $_popup_notification_allowed = true;
/// Magic getter methods =============================================================
/// Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
/// methods, but instead use the $PAGE->x syntax.
/**
* Please do not call this method directly, use the ->state syntax. {@link __get()}.
* @return integer one of the STATE_... constants. You should not normally need
* to use this in your code. It is intended for internal use by this class
* and its friends like print_header, to check that everything is working as
* expected. Also accessible as $PAGE->state.
*/
protected function magic_get_state() {
return $this->_state;
}
/**
* Please do not call this method directly, use the ->headerprinted syntax. {@link __get()}.
* @return boolean has the header already been printed?
*/
protected function magic_get_headerprinted() {
return $this->_state >= self::STATE_IN_BODY;
}
/**
* Please do not call this method directly, use the ->course syntax. {@link __get()}.
*
* @global object
* @return object the current course that we are inside - a row from the
* course table. (Also available as $COURSE global.) If we are not inside
* an actual course, this will be the site course.
*/
protected function magic_get_course() {
global $SITE;
if (is_null($this->_course)) {
return $SITE;
}
return $this->_course;
}
/**
* Please do not call this method directly, use the ->cm syntax. {@link __get()}.
* @return object the course_module that this page belongs to. Will be null
* if this page is not within a module. This is a full cm object, as loaded
* by get_coursemodule_from_id or get_coursemodule_from_instance,
* so the extra modname and name fields are present.
* @return cm_info
*/
protected function magic_get_cm() {
return $this->_cm;
}
/**
* Please do not call this method directly, use the ->activityrecord syntax. {@link __get()}.
* @return object the row from the activities own database table (for example
* the forum or quiz table) that this page belongs to. Will be null
* if this page is not within a module.
*/
protected function magic_get_activityrecord() {
if (is_null($this->_module) && !is_null($this->_cm)) {
$this->load_activity_record();
}
return $this->_module;
}
/**
* Please do not call this method directly, use the ->activityname syntax. {@link __get()}.
* @return string|null the The type of activity we are in, for example 'forum' or 'quiz'.
* Will be null if this page is not within a module.
*/
protected function magic_get_activityname() {
if (is_null($this->_cm)) {
return null;
}
return $this->_cm->modname;
}
/**
* Please do not call this method directly, use the ->category syntax. {@link __get()}.
* @return mixed the category that the page course belongs to. If there isn't one
* (that is, if this is the front page course) returns null.
*/
protected function magic_get_category() {
$this->ensure_category_loaded();
if (!empty($this->_categories)) {
return reset($this->_categories);
} else {
return null;
}
}
/**
* Please do not call this method directly, use the ->categories syntax. {@link __get()}.
* @return array an array of all the categories the page course belongs to,
* starting with the immediately containing category, and working out to
* the top-level category. This may be the empty array if we are in the
* front page course.
*/
protected function magic_get_categories() {
$this->ensure_categories_loaded();
return $this->_categories;
}
/**
* Please do not call this method directly, use the ->context syntax. {@link __get()}.
* @return object the main context to which this page belongs.
*/
protected function magic_get_context() {
if (is_null($this->_context)) {
if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
// cli scripts work in system context, do not annoy devs with debug info
// very few scripts do not use cookies, we can safely use system as default context there
} else {
debugging('Coding problem: this page does not set $PAGE->context properly.');
}
$this->_context = get_context_instance(CONTEXT_SYSTEM);
}
return $this->_context;
}
/**
* Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
* @return string e.g. 'my-index' or 'mod-quiz-attempt'.
*/
protected function magic_get_pagetype() {
global $CFG;
if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
$this->initialise_default_pagetype();
}
return $this->_pagetype;
}
/**
* Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
* @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
*/
protected function magic_get_bodyid() {
return 'page-'.$this->pagetype;
}
/**
* Please do not call this method directly, use the ->pagelayout syntax. {@link __get()}.
* @return string the general type of page this is. For example 'standard', 'popup', 'home'.
* Allows the theme to display things differently, if it wishes to.
*/
protected function magic_get_pagelayout() {
return $this->_pagelayout;
}
/**
* Please do not call this method directly, use the ->layout_tions syntax. {@link __get()}.
* @return array returns arrys with options for layout file
*/
protected function magic_get_layout_options() {
return $this->_layout_options;
}
/**
* Please do not call this method directly, use the ->subpage syntax. {@link __get()}.
* @return string|null The subpage identifier, if any.
*/
protected function magic_get_subpage() {
return $this->_subpage;
}
/**
* Please do not call this method directly, use the ->bodyclasses syntax. {@link __get()}.
* @return string the class names to put on the body element in the HTML.
*/
protected function magic_get_bodyclasses() {
return implode(' ', array_keys($this->_bodyclasses));
}
/**
* Please do not call this method directly, use the ->title syntax. {@link __get()}.
* @return string the title that should go in the <head> section of the HTML of this page.
*/
protected function magic_get_title() {
return $this->_title;
}
/**
* Please do not call this method directly, use the ->heading syntax. {@link __get()}.
* @return string the main heading that should be displayed at the top of the <body>.
*/
protected function magic_get_heading() {
return $this->_heading;
}
/**
* Please do not call this method directly, use the ->heading syntax. {@link __get()}.
* @return string The menu (or actions) to display in the heading
*/
protected function magic_get_headingmenu() {
return $this->_headingmenu;
}
/**
* Please do not call this method directly, use the ->docspath syntax. {@link __get()}.
* @return string the path to the Moodle docs for this page.
*/
protected function magic_get_docspath() {
if (is_string($this->_docspath)) {
return $this->_docspath;
} else {
return str_replace('-', '/', $this->pagetype);
}
}
/**
* Please do not call this method directly, use the ->url syntax. {@link __get()}.
* @return moodle_url the clean URL required to load the current page. (You
* should normally use this in preference to $ME or $FULLME.)
*/
protected function magic_get_url() {
global $FULLME;
if (is_null($this->_url)) {
debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
$this->_url = new moodle_url($FULLME);
// Make sure the guessed URL cannot lead to dangerous redirects.
$this->_url->remove_params('sesskey');
}
return new moodle_url($this->_url); // Return a clone for safety.
}
/**
* The list of alternate versions of this page.
* @return array mime type => object with ->url and ->title.
*/
protected function magic_get_alternateversions() {
return $this->_alternateversions;
}
/**
* Please do not call this method directly, use the ->blocks syntax. {@link __get()}.
* @return blocks_manager the blocks manager object for this page.
*/
protected function magic_get_blocks() {
global $CFG;
if (is_null($this->_blocks)) {
if (!empty($CFG->blockmanagerclass)) {
$classname = $CFG->blockmanagerclass;
} else {
$classname = 'block_manager';
}
$this->_blocks = new $classname($this);
}
return $this->_blocks;
}
/**
* Please do not call this method directly, use the ->requires syntax. {@link __get()}.
* @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
*/
protected function magic_get_requires() {
global $CFG;
if (is_null($this->_requires)) {
$this->_requires = new page_requirements_manager();
}
return $this->_requires;
}
/**
* Please do not call this method directly, use the ->cacheable syntax. {@link __get()}.
* @return boolean can this page be cached by the user's browser.
*/
protected function magic_get_cacheable() {
return $this->_cacheable;
}
/**
* Please do not call this method directly, use the ->focuscontrol syntax. {@link __get()}.
* @return string the id of the HTML element to be focused when the page has loaded.
*/
protected function magic_get_focuscontrol() {
return $this->_focuscontrol;
}
/**
* Please do not call this method directly, use the ->button syntax. {@link __get()}.
* @return string the HTML to go where the Turn editing on button normally goes.
*/
protected function magic_get_button() {
return $this->_button;
}
/**
* Please do not call this method directly, use the ->theme syntax. {@link __get()}.
* @return theme_config the initialised theme for this page.
*/
protected function magic_get_theme() {
if (is_null($this->_theme)) {
$this->initialise_theme_and_output();
}
return $this->_theme;
}
/**
* Please do not call this method directly, use the ->devicetypeinuse syntax. {@link __get()}.
* @return bool
*/
protected function magic_get_devicetypeinuse() {
return ($this->_devicetypeinuse);
}
/**
* Please do not call this method directly use the ->periodicrefreshdelay syntax
* {@link __get()}
* @return int The periodic refresh delay to use with meta refresh
*/
protected function magic_get_periodicrefreshdelay() {
return $this->_periodicrefreshdelay;
}
/**
* Please do not call this method directly use the ->opencontainers syntax. {@link __get()}
* @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
* mainly for internal use by the rendering code.
*/
protected function magic_get_opencontainers() {
if (is_null($this->_opencontainers)) {
$this->_opencontainers = new xhtml_container_stack();
}
return $this->_opencontainers;
}
/**
* Return the navigation object
* @return global_navigation
*/
protected function magic_get_navigation() {
if ($this->_navigation === null) {
$this->_navigation = new global_navigation($this);
}
return $this->_navigation;
}
/**
* Return a navbar object
* @return navbar
*/
protected function magic_get_navbar() {
if ($this->_navbar === null) {
$this->_navbar = new navbar($this);
}
return $this->_navbar;
}
/**
* Returns the settings navigation object
* @return settings_navigation
*/
protected function magic_get_settingsnav() {
if ($this->_settingsnav === null) {
$this->_settingsnav = new settings_navigation($this);
$this->_settingsnav->initialise();
}
return $this->_settingsnav;
}
/**
* PHP overloading magic to make the $PAGE->course syntax work by redirecting
* it to the corresponding $PAGE->magic_get_course() method if there is one, and
* throwing an exception if not.
*
* @param $name string property name
* @return mixed
*/
public function __get($name) {
$getmethod = 'magic_get_' . $name;
if (method_exists($this, $getmethod)) {
return $this->$getmethod();
} else {
throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
}
}
/**
* PHP overloading magic which prevents the $PAGE->context = $context; syntax
*
* @param $name string property name
* @param $name mixed value
* @return void, throws exception if field not defined in page class
*/
public function __set($name, $value) {
if (method_exists($this, 'set_' . $name)) {
throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
} else {
throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
}
}
/// Other information getting methods ==========================================
/**
* Returns instance of page renderer
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
* @param string $target one of rendering target constants
* @return renderer_base
*/
public function get_renderer($component, $subtype = null, $target = null) {
return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
}
/**
* Checks to see if there are any items on the navbar object
* @return bool true if there are, false if not
*/
public function has_navbar() {
if ($this->_navbar === null) {
$this->_navbar = new navbar($this);
}
return $this->_navbar->has_items();
}
/**
* @return boolean should the current user see this page in editing mode.
* That is, are they allowed to edit this page, and are they currently in
* editing mode.
*/
public function user_is_editing() {
global $USER;
return !empty($USER->editing) && $this->user_allowed_editing();
}
/**
* @return boolean does the user have permission to edit blocks on this page.
*/
public function user_can_edit_blocks() {
return has_capability($this->_blockseditingcap, $this->_context);
}
/**
* @return boolean does the user have permission to see this page in editing mode.
*/
public function user_allowed_editing() {
if ($this->_legacypageobject) {
return $this->_legacypageobject->user_allowed_editing();
}
return has_any_capability($this->all_editing_caps(), $this->_context);
}
/**
* @return string a description of this page. Normally displayed in the footer in
* developer debug mode.
*/
public function debug_summary() {
$summary = '';
$summary .= 'General type: ' . $this->pagelayout . '. ';
if (!during_initial_install()) {
$summary .= 'Context ' . print_context_name($this->_context) . ' (context id ' . $this->_context->id . '). ';
}
$summary .= 'Page type ' . $this->pagetype . '. ';
if ($this->subpage) {
'Sub-page ' . $this->subpage . '. ';
}
return $summary;
}
/// Setter methods =============================================================
/**
* Set the state. The state must be one of that STATE_... constants, and
* the state is only allowed to advance one step at a time.
* @param integer $state the new state.
*/
public function set_state($state) {
if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
$this->_state . ' and state ' . $state . ' was requested.');
}
if ($state == self::STATE_PRINTING_HEADER) {
$this->starting_output();
}
$this->_state = $state;
}
/**
* Set the current course. This sets both $PAGE->course and $COURSE. It also
* sets the right theme and locale.
*
* Normally you don't need to call this function yourself, require_login will
* call it for you if you pass a $course to it. You can use this function
* on pages that do need to call require_login().
*
* Sets $PAGE->context to the course context, if it is not already set.
*
* @param object the course to set as the global course.
*/
public function set_course($course) {
global $COURSE, $PAGE;
if (empty($course->id)) {
throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
}
$this->ensure_theme_not_set();
if (!empty($this->_course->id) && $this->_course->id != $course->id) {
$this->_categories = null;
}
$this->_course = clone($course);
if ($this === $PAGE) {
$COURSE = $this->_course;
moodle_setlocale();
}
if (!$this->_context) {
$this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id));
}
}
/**
* Set the main context to which this page belongs.
* @param object $context a context object, normally obtained with get_context_instance.
*/
public function set_context($context) {
if ($context === null) {
// extremely ugly hack which sets context to some value in order to prevent warnings,
// use only for core error handling!!!!
if (!$this->_context) {
$this->_context = get_context_instance(CONTEXT_SYSTEM);
}
return;
}
// ideally we should set context only once
if (isset($this->_context)) {
if ($context->id == $this->_context->id) {
// fine - no change needed
} else if ($this->_context->contextlevel == CONTEXT_SYSTEM or $this->_context->contextlevel == CONTEXT_COURSE) {
// hmm - not ideal, but it might produce too many warnings due to the design of require_login
} else if ($this->_context->contextlevel == CONTEXT_MODULE and $this->_context->id == get_parent_contextid($context)) {
// hmm - most probably somebody did require_login() and after that set the block context
} else {
// we do not want devs to do weird switching of context levels on the fly,
// because we might have used the context already such as in text filter in page title
debugging('Coding problem: unsupported modification of PAGE->context from '.$this->_context->contextlevel.' to '.$context->contextlevel);
}
}
$this->_context = $context;
}
/**
* The course module that this page belongs to (if it does belong to one).
*
* @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
* @param stdClass $course
* @param stdClass $module
* @return void
*/
public function set_cm($cm, $course = null, $module = null) {
global $DB;
if (!isset($cm->id) || !isset($cm->course)) {
throw new coding_exception('Invalid $cm parameter for $PAGE object, it has to be instance of cm_info or record from the course_modules table.');
}
if (!$this->_course || $this->_course->id != $cm->course) {
if (!$course) {
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
}
if ($course->id != $cm->course) {
throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
}
$this->set_course($course);
}
// make sure we have a $cm from get_fast_modinfo as this contains activity access details
if (!($cm instanceof cm_info)) {
$modinfo = get_fast_modinfo($this->_course);
$cm = $modinfo->get_cm($cm->id);
}
$this->_cm = $cm;
// unfortunately the context setting is a mess, let's try to work around some common block problems and show some debug messages
if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$this->set_context($context);
}
if ($module) {
$this->set_activity_record($module);
}
}
/**
* @param $module a row from the main database table for the module that this
* page belongs to. For example, if ->cm is a forum, then you can pass the
* corresponding row from the forum table here if you have it (saves a database
* query sometimes).
*/
public function set_activity_record($module) {
if (is_null($this->_cm)) {
throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
}
if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
throw new coding_exception('The activity record your are trying to set does not seem to correspond to the cm that has been set.');
}
$this->_module = $module;
}
/**
* @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'. Normally
* you do not need to set this manually, it is automatically created from the
* script name. However, on some pages this is overridden. For example, the
* page type for course/view.php includes the course format, for example
* 'course-view-weeks'. This gets used as the id attribute on <body> and
* also for determining which blocks are displayed.
*/
public function set_pagetype($pagetype) {
$this->_pagetype = $pagetype;
}
/**
* @param string $pagelayout the page layout this is. For example 'popup', 'home'.
* This properly defaults to 'base', so you only need to call this function if
* you want something different. The exact range of supported layouts is specified
* in the standard theme.
*/
public function set_pagelayout($pagelayout) {
/**
* Uncomment this to debug theme pagelayout issues like missing blocks.
*
* if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout) {
* debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
* }
*/
$this->_pagelayout = $pagelayout;
}
/**
* If context->id and pagetype are not enough to uniquely identify this page,
* then you can set a subpage id as well. For example, the tags page sets
* @param string $subpage an arbitrary identifier that, along with context->id
* and pagetype, uniquely identifies this page.
*/
public function set_subpage($subpage) {
if (empty($subpage)) {
$this->_subpage = '';
} else {
$this->_subpage = $subpage;
}
}
/**
* @param string $class add this class name ot the class attribute on the body tag.
*/
public function add_body_class($class) {
if ($this->_state > self::STATE_BEFORE_HEADER) {
throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
}
$this->_bodyclasses[$class] = 1;
}
/**
* @param array $classes this utility method calls add_body_class for each array element.
*/
public function add_body_classes($classes) {
foreach ($classes as $class) {
$this->add_body_class($class);
}
}
/**
* @param string $title the title that should go in the <head> section of the HTML of this page.
*/
public function set_title($title) {
$title = format_string($title);
$title = str_replace('"', '"', $title);
$this->_title = $title;
}
/**
* @param string $heading the main heading that should be displayed at the top of the <body>.
*/
public function set_heading($heading) {
$this->_heading = format_string($heading);
}
/**
* @param string $menu The menu/content to show in the heading
*/
public function set_headingmenu($menu) {
$this->_headingmenu = $menu;
}
/**
* Set the course category this page belongs to manually. This automatically
* sets $PAGE->course to be the site course. You cannot use this method if
* you have already set $PAGE->course - in that case, the category must be
* the one that the course belongs to. This also automatically sets the
* page context to the category context.
* @param integer $categoryid The id of the category to set.
*/
public function set_category_by_id($categoryid) {
global $SITE, $DB;
if (!is_null($this->_course)) {
throw new coding_exception('Attempt to manually set the course category when the course has been set. This is not allowed.');
}
if (is_array($this->_categories)) {
throw new coding_exception('Course category has already been set. You are not allowed to change it.');
}
$this->ensure_theme_not_set();
$this->set_course($SITE);
$this->load_category($categoryid);
$this->set_context(get_context_instance(CONTEXT_COURSECAT, $categoryid));
}
/**
* Set a different path to use for the 'Moodle docs for this page' link.
* By default, it uses the pagetype, which is normally the same as the
* script name. So, for example, for mod/quiz/attempt.php, pagetype is
* mod-quiz-attempt, and so docspath is mod/quiz/attempt.
* @param string $path the path to use at the end of the moodle docs URL.
*/
public function set_docs_path($path) {
$this->_docspath = $path;
}
/**
* You should call this method from every page to set the cleaned-up URL
* that should be used to return to this page. Used, for example, by the
* blocks editing UI to know where to return the user after an action.
* For example, course/view.php does:
* $id = optional_param('id', 0, PARAM_INT);
* $PAGE->set_url('/course/view.php', array('id' => $id));
* @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
* @param array $params parameters to add to the URL
*/
public function set_url($url, array $params = null) {
global $CFG;
if (is_string($url)) {
if (strpos($url, 'http') === 0) {
// ok
} else if (strpos($url, '/') === 0) {
// we have to use httpswwwroot here, because of loginhttps pages
$url = $CFG->httpswwwroot . $url;
} else {
throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
}
}
$this->_url = new moodle_url($url, $params);
$fullurl = $this->_url->out_omit_querystring();
if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
}
$shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
if (is_null($this->_pagetype)) {
$this->initialise_default_pagetype($shorturl);
}
if (!is_null($this->_legacypageobject)) {
$this->_legacypageobject->set_url($url, $params);
}
}
/**
* Make sure page URL does not contain the given URL parameter.
*
* This should not be necessary if the script has called set_url properly.
* However, in some situations like the block editing actions; when the URL
* has been guessed, it will contain dangerous block-related actions.
* Therefore, the blocks code calls this function to clean up such parameters