2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains the moodle_page class. There is normally a single instance
19 * of this class in the $PAGE global variable. This class is a central repository
20 * of information about the page we are building up to send back to the user.
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * $PAGE is a central store of information about the current page we are
32 * generating in response to the user's request.
34 * It does not do very much itself
35 * except keep track of information, however, it serves as the access point to
36 * some more significant components like $PAGE->theme, $PAGE->requires,
39 * @copyright 2009 Tim Hunt
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 * The following properties are alphabetical. Please keep it that way so that its
48 * @property-read string $activityname The type of activity we are in, for example 'forum' or 'quiz'.
49 * Will be null if this page is not within a module.
50 * @property-read stdClass $activityrecord The row from the activities own database table (for example
51 * the forum or quiz table) that this page belongs to. Will be null
52 * if this page is not within a module.
53 * @property-read array $alternativeversions Mime type => object with ->url and ->title.
54 * @property-read blocks_manager $blocks The blocks manager object for this page.
55 * @property-read string $bodyclasses A string to use within the class attribute on the body tag.
56 * @property-read string $bodyid A string to use as the id of the body tag.
57 * @property-read string $button The HTML to go where the Turn editing on button normally goes.
58 * @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
59 * @property-read array $categories An array of all the categories the page course belongs to,
60 * starting with the immediately containing category, and working out to
61 * the top-level category. This may be the empty array if we are in the
63 * @property-read mixed $category The category that the page course belongs to.
64 * @property-read cm_info $cm The course_module that this page belongs to. Will be null
65 * if this page is not within a module. This is a full cm object, as loaded
66 * by get_coursemodule_from_id or get_coursemodule_from_instance,
67 * so the extra modname and name fields are present.
68 * @property-read context $context The main context to which this page belongs.
69 * @property-read stdClass $course The current course that we are inside - a row from the
70 * course table. (Also available as $COURSE global.) If we are not inside
71 * an actual course, this will be the site course.
72 * @property-read string $devicetypeinuse The name of the device type in use
73 * @property-read string $docspath The path to the Moodle docs for this page.
74 * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
75 * @property-read bool $headerprinted True if the page header has already been printed.
76 * @property-read string $heading The main heading that should be displayed at the top of the <body>.
77 * @property-read string $headingmenu The menu (or actions) to display in the heading
78 * @property-read array $layout_options An arrays with options for the layout file.
79 * @property-read array $legacythemeinuse True if the legacy browser theme is in use.
80 * @property-read navbar $navbar The navbar object used to display the navbar
81 * @property-read global_navigation $navigation The navigation structure for this page.
82 * @property-read xml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
83 * mainly for internal use by the rendering code.
84 * @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
85 * Allows the theme to display things differently, if it wishes to.
86 * @property-read string $pagetype The page type string, should be used as the id for the body tag in the theme.
87 * @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
88 * @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
89 * @property-read settings_navigation $settingsnav The settings navigation
90 * @property-read int $state One of the STATE_... constants
91 * @property-read string $subpage The subpage identifier, if any.
92 * @property-read theme_config $theme The theme for this page.
93 * @property-read string $title The title that should go in the <head> section of the HTML of this page.
94 * @property-read moodle_url $url The moodle url object for this page.
98 /** The state of the page before it has printed the header **/
99 const STATE_BEFORE_HEADER
= 0;
101 /** The state the page is in temporarily while the header is being printed **/
102 const STATE_PRINTING_HEADER
= 1;
104 /** The state the page is in while content is presumably being printed **/
105 const STATE_IN_BODY
= 2;
108 * The state the page is when the footer has been printed and its function is
111 const STATE_DONE
= 3;
114 * @var int The current state of the page. The state a page is within
115 * determines what actions are possible for it.
117 protected $_state = self
::STATE_BEFORE_HEADER
;
120 * @var stdClass The course currently associated with this page.
121 * If not has been provided the front page course is used.
123 protected $_course = null;
126 * @var cm_info If this page belongs to a module, this is the cm_info module
127 * description object.
129 protected $_cm = null;
132 * @var stdClass If $_cm is not null, then this will hold the corresponding
133 * row from the modname table. For example, if $_cm->modname is 'quiz', this
134 * will be a row from the quiz table.
136 protected $_module = null;
139 * @var context The context that this page belongs to.
141 protected $_context = null;
144 * @var array This holds any categories that $_course belongs to, starting with the
145 * particular category it belongs to, and working out through any parent
146 * categories to the top level. These are loaded progressively, if needed.
147 * There are three states. $_categories = null initially when nothing is
148 * loaded; $_categories = array($id => $cat, $parentid => null) when we have
149 * loaded $_course->category, but not any parents; and a complete array once
150 * everything is loaded.
152 protected $_categories = null;
155 * @var array An array of CSS classes that should be added to the body tag in HTML.
157 protected $_bodyclasses = array();
160 * @var string The title for the page. Used within the title tag in the HTML head.
162 protected $_title = '';
165 * @var string The string to use as the heading of the page. Shown near the top of the
166 * page within most themes.
168 protected $_heading = '';
171 * @var string The pagetype is used to describe the page and defaults to a representation
172 * of the physical path to the page e.g. my-index, mod-quiz-attempt
174 protected $_pagetype = null;
177 * @var string The pagelayout to use when displaying this page. The
178 * pagelayout needs to have been defined by the theme in use, or one of its
179 * parents. By default base is used however standard is the more common layout.
180 * Note that this gets automatically set by core during operations like
183 protected $_pagelayout = 'base';
186 * @var array List of theme layout options, these are ignored by core.
187 * To be used in individual theme layout files only.
189 protected $_layout_options = null;
192 * @var string An optional arbitrary parameter that can be set on pages where the context
193 * and pagetype is not enough to identify the page.
195 protected $_subpage = '';
198 * @var string Set a different path to use for the 'Moodle docs for this page' link.
199 * By default, it uses the path of the file for instance mod/quiz/attempt.
201 protected $_docspath = null;
204 * @var string A legacy class that will be added to the body tag
206 protected $_legacyclass = null;
209 * @var moodle_url The URL for this page. This is mandatory and must be set
210 * before output is started.
212 protected $_url = null;
215 * @var array An array of links to alternative versions of this page.
216 * Primarily used for RSS versions of the current page.
218 protected $_alternateversions = array();
221 * @var block_manager The blocks manager for this page. It is reponsible for
222 * the blocks and there content on this page.
224 protected $_blocks = null;
227 * @var page_requirements_manager Page requirements manager. It is reponsible
228 * for all JavaScript and CSS resources required by this page.
230 protected $_requires = null;
233 * @var string The capability required by the user in order to edit blocks
234 * and block settings on this page.
236 protected $_blockseditingcap = 'moodle/site:manageblocks';
239 * @var bool An internal flag to record when block actions have been processed.
240 * Remember block actions occur on the current URL and it is important that
241 * even they are never executed more than once.
243 protected $_block_actions_done = false;
246 * @var array An array of any other capabilities the current user must have
247 * in order to editing the page and/or its content (not just blocks).
249 protected $_othereditingcaps = array();
252 * @var bool Sets whether this page should be cached by the browser or not.
253 * If it is set to true (default) the page is served with caching headers.
255 protected $_cacheable = true;
258 * @var string Can be set to the ID of an element on the page, if done that
259 * element receives focus when the page loads.
261 protected $_focuscontrol = '';
264 * @var string HTML to go where the turn on editing button is located. This
265 * is nearly a legacy item and not used very often any more.
267 protected $_button = '';
270 * @var theme_config The theme to use with this page. This has to be properly
271 * initialised via {@link moodle_page::initialise_theme_and_output()} which
272 * happens magically before any operation that requires it.
274 protected $_theme = null;
277 * @var global_navigation Contains the global navigation structure.
279 protected $_navigation = null;
282 * @var settings_navigation Contains the settings navigation structure.
284 protected $_settingsnav = null;
287 * @var navbar Contains the navbar structure.
289 protected $_navbar = null;
292 * @var string The menu (or actions) to display in the heading.
294 protected $_headingmenu = null;
297 * @var array stack trace. Then the theme is initialised, we save the stack
298 * trace, for use in error messages.
300 protected $_wherethemewasinitialised = null;
303 * @var xhtml_container_stack Tracks XHTML tags on this page that have been
304 * opened but not closed.
306 protected $_opencontainers;
309 * @var int Sets the page to refresh after a given delay (in seconds) using
310 * meta refresh in {@link standard_head_html()} in outputlib.php
311 * If set to null(default) the page is not refreshed
313 protected $_periodicrefreshdelay = null;
316 * @var array Associative array of browser shortnames (as used by check_browser_version)
317 * and their minimum required versions
319 protected $_legacybrowsers = array('MSIE' => 6.0);
322 * @var string Is set to the name of the device type in use.
323 * This will we worked out when it is first used.
325 protected $_devicetypeinuse = null;
328 * @var bool Used to determine if HTTPS should be required for login.
330 protected $_https_login_required = false;
333 * @var bool Determines if popup notifications allowed on this page.
334 * Code such as the quiz module disables popup notifications in situations
335 * such as upgrading or completing a quiz.
337 protected $_popup_notification_allowed = true;
339 // Magic getter methods =============================================================
340 // Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
341 // methods, but instead use the $PAGE->x syntax.
344 * Please do not call this method directly, use the ->state syntax. {@link moodle_page::__get()}.
345 * @return integer one of the STATE_XXX constants. You should not normally need
346 * to use this in your code. It is intended for internal use by this class
347 * and its friends like print_header, to check that everything is working as
348 * expected. Also accessible as $PAGE->state.
350 protected function magic_get_state() {
351 return $this->_state
;
355 * Please do not call this method directly, use the ->headerprinted syntax. {@link moodle_page::__get()}.
356 * @return bool has the header already been printed?
358 protected function magic_get_headerprinted() {
359 return $this->_state
>= self
::STATE_IN_BODY
;
363 * Please do not call this method directly, use the ->course syntax. {@link moodle_page::__get()}.
364 * @return stdClass the current course that we are inside - a row from the
365 * course table. (Also available as $COURSE global.) If we are not inside
366 * an actual course, this will be the site course.
368 protected function magic_get_course() {
370 if (is_null($this->_course
)) {
373 return $this->_course
;
377 * Please do not call this method directly, use the ->cm syntax. {@link moodle_page::__get()}.
378 * @return cm_info the course_module that this page belongs to. Will be null
379 * if this page is not within a module. This is a full cm object, as loaded
380 * by get_coursemodule_from_id or get_coursemodule_from_instance,
381 * so the extra modname and name fields are present.
383 protected function magic_get_cm() {
388 * Please do not call this method directly, use the ->activityrecord syntax. {@link moodle_page::__get()}.
389 * @return stdClass the row from the activities own database table (for example
390 * the forum or quiz table) that this page belongs to. Will be null
391 * if this page is not within a module.
393 protected function magic_get_activityrecord() {
394 if (is_null($this->_module
) && !is_null($this->_cm
)) {
395 $this->load_activity_record();
397 return $this->_module
;
401 * Please do not call this method directly, use the ->activityname syntax. {@link moodle_page::__get()}.
402 * @return string the The type of activity we are in, for example 'forum' or 'quiz'.
403 * Will be null if this page is not within a module.
405 protected function magic_get_activityname() {
406 if (is_null($this->_cm
)) {
409 return $this->_cm
->modname
;
413 * Please do not call this method directly, use the ->category syntax. {@link moodle_page::__get()}.
414 * @return stdClass the category that the page course belongs to. If there isn't one
415 * (that is, if this is the front page course) returns null.
417 protected function magic_get_category() {
418 $this->ensure_category_loaded();
419 if (!empty($this->_categories
)) {
420 return reset($this->_categories
);
427 * Please do not call this method directly, use the ->categories syntax. {@link moodle_page::__get()}.
428 * @return array an array of all the categories the page course belongs to,
429 * starting with the immediately containing category, and working out to
430 * the top-level category. This may be the empty array if we are in the
433 protected function magic_get_categories() {
434 $this->ensure_categories_loaded();
435 return $this->_categories
;
439 * Please do not call this method directly, use the ->context syntax. {@link moodle_page::__get()}.
440 * @return context the main context to which this page belongs.
442 protected function magic_get_context() {
443 if (is_null($this->_context
)) {
444 if (CLI_SCRIPT
or NO_MOODLE_COOKIES
) {
445 // cli scripts work in system context, do not annoy devs with debug info
446 // very few scripts do not use cookies, we can safely use system as default context there
448 debugging('Coding problem: $PAGE->context was not set. You may have forgotten '
449 .'to call require_login() or $PAGE->set_context(). The page may not display '
450 .'correctly as a result');
452 $this->_context
= context_system
::instance();
454 return $this->_context
;
458 * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
459 * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
461 protected function magic_get_pagetype() {
463 if (is_null($this->_pagetype
) ||
isset($CFG->pagepath
)) {
464 $this->initialise_default_pagetype();
466 return $this->_pagetype
;
470 * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
471 * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
473 protected function magic_get_bodyid() {
474 return 'page-'.$this->pagetype
;
478 * Please do not call this method directly, use the ->pagelayout syntax. {@link moodle_page::__get()}.
479 * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
480 * Allows the theme to display things differently, if it wishes to.
482 protected function magic_get_pagelayout() {
483 return $this->_pagelayout
;
487 * Please do not call this method directly, use the ->layout_options syntax. {@link moodle_page::__get()}.
488 * @return array returns arrays with options for layout file
490 protected function magic_get_layout_options() {
491 if (!is_array($this->_layout_options
)) {
492 $this->_layout_options
= $this->_theme
->pagelayout_options($this->pagelayout
);
494 return $this->_layout_options
;
498 * Please do not call this method directly, use the ->subpage syntax. {@link moodle_page::__get()}.
499 * @return string The subpage identifier, if any.
501 protected function magic_get_subpage() {
502 return $this->_subpage
;
506 * Please do not call this method directly, use the ->bodyclasses syntax. {@link moodle_page::__get()}.
507 * @return string the class names to put on the body element in the HTML.
509 protected function magic_get_bodyclasses() {
510 return implode(' ', array_keys($this->_bodyclasses
));
514 * Please do not call this method directly, use the ->title syntax. {@link moodle_page::__get()}.
515 * @return string the title that should go in the <head> section of the HTML of this page.
517 protected function magic_get_title() {
518 return $this->_title
;
522 * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
523 * @return string the main heading that should be displayed at the top of the <body>.
525 protected function magic_get_heading() {
526 return $this->_heading
;
530 * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
531 * @return string The menu (or actions) to display in the heading
533 protected function magic_get_headingmenu() {
534 return $this->_headingmenu
;
538 * Please do not call this method directly, use the ->docspath syntax. {@link moodle_page::__get()}.
539 * @return string the path to the Moodle docs for this page.
541 protected function magic_get_docspath() {
542 if (is_string($this->_docspath
)) {
543 return $this->_docspath
;
545 return str_replace('-', '/', $this->pagetype
);
550 * Please do not call this method directly, use the ->url syntax. {@link moodle_page::__get()}.
551 * @return moodle_url the clean URL required to load the current page. (You
552 * should normally use this in preference to $ME or $FULLME.)
554 protected function magic_get_url() {
556 if (is_null($this->_url
)) {
557 debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER
);
558 $this->_url
= new moodle_url($FULLME);
559 // Make sure the guessed URL cannot lead to dangerous redirects.
560 $this->_url
->remove_params('sesskey');
562 return new moodle_url($this->_url
); // Return a clone for safety.
566 * The list of alternate versions of this page.
567 * @return array mime type => object with ->url and ->title.
569 protected function magic_get_alternateversions() {
570 return $this->_alternateversions
;
574 * Please do not call this method directly, use the ->blocks syntax. {@link moodle_page::__get()}.
575 * @return blocks_manager the blocks manager object for this page.
577 protected function magic_get_blocks() {
579 if (is_null($this->_blocks
)) {
580 if (!empty($CFG->blockmanagerclass
)) {
581 if (!empty($CFG->blockmanagerclassfile
)) {
582 require_once($CFG->blockmanagerclassfile
);
584 $classname = $CFG->blockmanagerclass
;
586 $classname = 'block_manager';
588 $this->_blocks
= new $classname($this);
590 return $this->_blocks
;
594 * Please do not call this method directly, use the ->requires syntax. {@link moodle_page::__get()}.
595 * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
597 protected function magic_get_requires() {
599 if (is_null($this->_requires
)) {
600 $this->_requires
= new page_requirements_manager();
602 return $this->_requires
;
606 * Please do not call this method directly, use the ->cacheable syntax. {@link moodle_page::__get()}.
607 * @return bool can this page be cached by the user's browser.
609 protected function magic_get_cacheable() {
610 return $this->_cacheable
;
614 * Please do not call this method directly, use the ->focuscontrol syntax. {@link moodle_page::__get()}.
615 * @return string the id of the HTML element to be focused when the page has loaded.
617 protected function magic_get_focuscontrol() {
618 return $this->_focuscontrol
;
622 * Please do not call this method directly, use the ->button syntax. {@link moodle_page::__get()}.
623 * @return string the HTML to go where the Turn editing on button normally goes.
625 protected function magic_get_button() {
626 return $this->_button
;
630 * Please do not call this method directly, use the ->theme syntax. {@link moodle_page::__get()}.
631 * @return theme_config the initialised theme for this page.
633 protected function magic_get_theme() {
634 if (is_null($this->_theme
)) {
635 $this->initialise_theme_and_output();
637 return $this->_theme
;
641 * Please do not call this method directly, use the ->devicetypeinuse syntax. {@link moodle_page::__get()}.
642 * @return string The device type being used.
644 protected function magic_get_devicetypeinuse() {
645 if (empty($this->_devicetypeinuse
)) {
646 $this->_devicetypeinuse
= get_user_device_type();
648 return $this->_devicetypeinuse
;
652 * Please do not call this method directly use the ->periodicrefreshdelay syntax
653 * {@link moodle_page::__get()}
654 * @return int The periodic refresh delay to use with meta refresh
656 protected function magic_get_periodicrefreshdelay() {
657 return $this->_periodicrefreshdelay
;
661 * Please do not call this method directly use the ->opencontainers syntax. {@link moodle_page::__get()}
662 * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
663 * mainly for internal use by the rendering code.
665 protected function magic_get_opencontainers() {
666 if (is_null($this->_opencontainers
)) {
667 $this->_opencontainers
= new xhtml_container_stack();
669 return $this->_opencontainers
;
673 * Return the navigation object
674 * @return global_navigation
676 protected function magic_get_navigation() {
677 if ($this->_navigation
=== null) {
678 $this->_navigation
= new global_navigation($this);
680 return $this->_navigation
;
684 * Return a navbar object
687 protected function magic_get_navbar() {
688 if ($this->_navbar
=== null) {
689 $this->_navbar
= new navbar($this);
691 return $this->_navbar
;
695 * Returns the settings navigation object
696 * @return settings_navigation
698 protected function magic_get_settingsnav() {
699 if ($this->_settingsnav
=== null) {
700 $this->_settingsnav
= new settings_navigation($this);
701 $this->_settingsnav
->initialise();
703 return $this->_settingsnav
;
707 * PHP overloading magic to make the $PAGE->course syntax work by redirecting
708 * it to the corresponding $PAGE->magic_get_course() method if there is one, and
709 * throwing an exception if not.
711 * @param string $name property name
714 public function __get($name) {
715 $getmethod = 'magic_get_' . $name;
716 if (method_exists($this, $getmethod)) {
717 return $this->$getmethod();
719 throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
724 * PHP overloading magic to catch obvious coding errors.
726 * This method has been created to catch obvious coding errors where the
727 * developer has tried to set a page property using $PAGE->key = $value.
728 * In the moodle_page class all properties must be set using the appropriate
729 * $PAGE->set_something($value) method.
731 * @param string $name property name
732 * @param mixed $value Value
733 * @return void Throws exception if field not defined in page class
735 public function __set($name, $value) {
736 if (method_exists($this, 'set_' . $name)) {
737 throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
739 throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
743 // Other information getting methods ==========================================
746 * Returns instance of page renderer
748 * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
749 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
750 * @param string $target one of rendering target constants
751 * @return renderer_base
753 public function get_renderer($component, $subtype = null, $target = null) {
754 return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
758 * Checks to see if there are any items on the navbar object
760 * @return bool true if there are, false if not
762 public function has_navbar() {
763 if ($this->_navbar
=== null) {
764 $this->_navbar
= new navbar($this);
766 return $this->_navbar
->has_items();
770 * Should the current user see this page in editing mode.
771 * That is, are they allowed to edit this page, and are they currently in
775 public function user_is_editing() {
777 return !empty($USER->editing
) && $this->user_allowed_editing();
781 * Does the user have permission to edit blocks on this page.
784 public function user_can_edit_blocks() {
785 return has_capability($this->_blockseditingcap
, $this->_context
);
789 * Does the user have permission to see this page in editing mode.
792 public function user_allowed_editing() {
793 return has_any_capability($this->all_editing_caps(), $this->_context
);
797 * Get a description of this page. Normally displayed in the footer in
798 * developer debug mode.
801 public function debug_summary() {
803 $summary .= 'General type: ' . $this->pagelayout
. '. ';
804 if (!during_initial_install()) {
805 $summary .= 'Context ' . print_context_name($this->_context
) . ' (context id ' . $this->_context
->id
. '). ';
807 $summary .= 'Page type ' . $this->pagetype
. '. ';
808 if ($this->subpage
) {
809 'Sub-page ' . $this->subpage
. '. ';
814 // Setter methods =============================================================
817 * Set the state. The state must be one of that STATE_... constants, and
818 * the state is only allowed to advance one step at a time.
820 * @param integer $state The new state.
822 public function set_state($state) {
823 if ($state != $this->_state +
1 ||
$state > self
::STATE_DONE
) {
824 throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
825 $this->_state
. ' and state ' . $state . ' was requested.');
828 if ($state == self
::STATE_PRINTING_HEADER
) {
829 $this->starting_output();
832 $this->_state
= $state;
836 * Set the current course. This sets both $PAGE->course and $COURSE. It also
837 * sets the right theme and locale.
839 * Normally you don't need to call this function yourself, require_login will
840 * call it for you if you pass a $course to it. You can use this function
841 * on pages that do need to call require_login().
843 * Sets $PAGE->context to the course context, if it is not already set.
845 * @param stdClass $course the course to set as the global course.
847 public function set_course($course) {
848 global $COURSE, $PAGE, $CFG, $SITE;
850 if (empty($course->id
)) {
851 throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
854 $this->ensure_theme_not_set();
856 if (!empty($this->_course
->id
) && $this->_course
->id
!= $course->id
) {
857 $this->_categories
= null;
860 $this->_course
= clone($course);
862 if ($this === $PAGE) {
863 $COURSE = $this->_course
;
867 if (!$this->_context
) {
868 $this->set_context(context_course
::instance($this->_course
->id
));
871 // notify course format that this page is set for the course
872 if ($this->_course
->id
!= $SITE->id
) {
873 require_once($CFG->dirroot
.'/course/lib.php');
874 $courseformat = course_get_format($this->_course
);
875 $this->add_body_class('format-'. $courseformat->get_format());
876 $courseformat->page_set_course($this);
878 $this->add_body_class('format-site');
883 * Set the main context to which this page belongs.
885 * @param context $context a context object, normally obtained with get_context_instance.
887 public function set_context($context) {
888 if ($context === null) {
889 // extremely ugly hack which sets context to some value in order to prevent warnings,
890 // use only for core error handling!!!!
891 if (!$this->_context
) {
892 $this->_context
= context_system
::instance();
897 // ideally we should set context only once
898 if (isset($this->_context
)) {
899 if ($context->id
== $this->_context
->id
) {
900 // fine - no change needed
901 } else if ($this->_context
->contextlevel
== CONTEXT_SYSTEM
or $this->_context
->contextlevel
== CONTEXT_COURSE
) {
902 // hmm - not ideal, but it might produce too many warnings due to the design of require_login
903 } else if ($this->_context
->contextlevel
== CONTEXT_MODULE
and $this->_context
->id
== get_parent_contextid($context)) {
904 // hmm - most probably somebody did require_login() and after that set the block context
906 // we do not want devs to do weird switching of context levels on the fly,
907 // because we might have used the context already such as in text filter in page title
908 debugging('Coding problem: unsupported modification of PAGE->context from '.$this->_context
->contextlevel
.' to '.$context->contextlevel
);
912 $this->_context
= $context;
916 * The course module that this page belongs to (if it does belong to one).
918 * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
919 * @param stdClass $course
920 * @param stdClass $module
923 public function set_cm($cm, $course = null, $module = null) {
924 global $DB, $CFG, $SITE;
926 if (!isset($cm->id
) ||
!isset($cm->course
)) {
927 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.');
930 if (!$this->_course ||
$this->_course
->id
!= $cm->course
) {
932 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
934 if ($course->id
!= $cm->course
) {
935 throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
937 $this->set_course($course);
940 // make sure we have a $cm from get_fast_modinfo as this contains activity access details
941 if (!($cm instanceof cm_info
)) {
942 $modinfo = get_fast_modinfo($this->_course
);
943 $cm = $modinfo->get_cm($cm->id
);
947 // unfortunately the context setting is a mess, let's try to work around some common block problems and show some debug messages
948 if (empty($this->_context
) or $this->_context
->contextlevel
!= CONTEXT_BLOCK
) {
949 $context = context_module
::instance($cm->id
);
950 $this->set_context($context);
954 $this->set_activity_record($module);
957 // notify course format that this page is set for the course module
958 if ($this->_course
->id
!= $SITE->id
) {
959 require_once($CFG->dirroot
.'/course/lib.php');
960 course_get_format($this->_course
)->page_set_cm($this);
965 * Sets the activity record. This could be a row from the main table for a
966 * module. For instance if the current module (cm) is a forum this should be a row
967 * from the forum table.
969 * @param stdClass $module A row from the main database table for the module that this
973 public function set_activity_record($module) {
974 if (is_null($this->_cm
)) {
975 throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
977 if ($module->id
!= $this->_cm
->instance ||
$module->course
!= $this->_course
->id
) {
978 throw new coding_exception('The activity record your are trying to set does not seem to correspond to the cm that has been set.');
980 $this->_module
= $module;
984 * Sets the pagetype to use for this page.
986 * Normally you do not need to set this manually, it is automatically created
987 * from the script name. However, on some pages this is overridden.
988 * For example the page type for course/view.php includes the course format,
989 * for example 'course-view-weeks'. This gets used as the id attribute on
990 * <body> and also for determining which blocks are displayed.
992 * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'.
994 public function set_pagetype($pagetype) {
995 $this->_pagetype
= $pagetype;
999 * Sets the layout to use for this page.
1001 * The page layout determines how the page will be displayed, things such as
1002 * block regions, content areas, etc are controlled by the layout.
1003 * The theme in use for the page will determine that the layout contains.
1005 * This properly defaults to 'base', so you only need to call this function if
1006 * you want something different. The exact range of supported layouts is specified
1007 * in the standard theme.
1009 * For an idea of the common page layouts see
1010 * {@link http://docs.moodle.org/dev/Themes_2.0#The_different_layouts_as_of_August_17th.2C_2010}
1011 * But please keep in mind that it may be (and normally is) out of date.
1012 * The only place to find an accurate up-to-date list of the page layouts
1013 * available for your version of Moodle is {@link theme/base/config.php}
1015 * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
1017 public function set_pagelayout($pagelayout) {
1019 * Uncomment this to debug theme pagelayout issues like missing blocks.
1021 * if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout) {
1022 * debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
1025 $this->_pagelayout
= $pagelayout;
1029 * If context->id and pagetype are not enough to uniquely identify this page,
1030 * then you can set a subpage id as well. For example, the tags page sets
1032 * @param string $subpage an arbitrary identifier that, along with context->id
1033 * and pagetype, uniquely identifies this page.
1035 public function set_subpage($subpage) {
1036 if (empty($subpage)) {
1037 $this->_subpage
= '';
1039 $this->_subpage
= $subpage;
1044 * Adds a CSS class to the body tag of the page.
1046 * @param string $class add this class name ot the class attribute on the body tag.
1048 public function add_body_class($class) {
1049 if ($this->_state
> self
::STATE_BEFORE_HEADER
) {
1050 throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
1052 $this->_bodyclasses
[$class] = 1;
1056 * Adds an array of body classes to the body tag of this page.
1058 * @param array $classes this utility method calls add_body_class for each array element.
1060 public function add_body_classes($classes) {
1061 foreach ($classes as $class) {
1062 $this->add_body_class($class);
1067 * Sets the title for the page.
1068 * This is normally used within the title tag in the head of the page.
1070 * @param string $title the title that should go in the <head> section of the HTML of this page.
1072 public function set_title($title) {
1073 $title = format_string($title);
1074 $title = strip_tags($title);
1075 $title = str_replace('"', '"', $title);
1076 $this->_title
= $title;
1080 * Sets the heading to use for the page.
1081 * This is normally used as the main heading at the top of the content.
1083 * @param string $heading the main heading that should be displayed at the top of the <body>.
1085 public function set_heading($heading) {
1086 $this->_heading
= format_string($heading);
1090 * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
1092 * @param string $menu The menu/content to show in the heading
1094 public function set_headingmenu($menu) {
1095 $this->_headingmenu
= $menu;
1099 * Set the course category this page belongs to manually.
1101 * This automatically sets $PAGE->course to be the site course. You cannot
1102 * use this method if you have already set $PAGE->course - in that case,
1103 * the category must be the one that the course belongs to. This also
1104 * automatically sets the page context to the category context.
1106 * @param integer $categoryid The id of the category to set.
1108 public function set_category_by_id($categoryid) {
1110 if (!is_null($this->_course
)) {
1111 throw new coding_exception('Attempt to manually set the course category when the course has been set. This is not allowed.');
1113 if (is_array($this->_categories
)) {
1114 throw new coding_exception('Course category has already been set. You are not allowed to change it.');
1116 $this->ensure_theme_not_set();
1117 $this->set_course($SITE);
1118 $this->load_category($categoryid);
1119 $this->set_context(context_coursecat
::instance($categoryid));
1123 * Set a different path to use for the 'Moodle docs for this page' link.
1125 * By default, it uses the pagetype, which is normally the same as the
1126 * script name. So, for example, for mod/quiz/attempt.php, pagetype is
1127 * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
1129 * @param string $path the path to use at the end of the moodle docs URL.
1131 public function set_docs_path($path) {
1132 $this->_docspath
= $path;
1136 * You should call this method from every page to set the cleaned-up URL
1137 * that should be used to return to this page.
1139 * Used, for example, by the blocks editing UI to know where to return the
1140 * user after an action.
1141 * For example, course/view.php does:
1142 * $id = optional_param('id', 0, PARAM_INT);
1143 * $PAGE->set_url('/course/view.php', array('id' => $id));
1145 * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
1146 * @param array $params parameters to add to the URL
1148 public function set_url($url, array $params = null) {
1151 if (is_string($url)) {
1152 if (strpos($url, 'http') === 0) {
1154 } else if (strpos($url, '/') === 0) {
1155 // we have to use httpswwwroot here, because of loginhttps pages
1156 $url = $CFG->httpswwwroot
. $url;
1158 throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
1162 $this->_url
= new moodle_url($url, $params);
1164 $fullurl = $this->_url
->out_omit_querystring();
1165 if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
1166 debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
1168 $shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
1170 if (is_null($this->_pagetype
)) {
1171 $this->initialise_default_pagetype($shorturl);
1176 * Make sure page URL does not contain the given URL parameter.
1178 * This should not be necessary if the script has called set_url properly.
1179 * However, in some situations like the block editing actions; when the URL
1180 * has been guessed, it will contain dangerous block-related actions.
1181 * Therefore, the blocks code calls this function to clean up such parameters
1182 * before doing any redirect.
1184 * @param string $param the name of the parameter to make sure is not in the
1187 public function ensure_param_not_in_url($param) {
1188 $discard = $this->url
; // Make sure $this->url is lazy-loaded;
1189 $this->_url
->remove_params($param);
1193 * There can be alternate versions of some pages (for example an RSS feed version).
1194 * If such other version exist, call this method, and a link to the alternate
1195 * version will be included in the <head> of the page.
1197 * @param string $title The title to give the alternate version.
1198 * @param string|moodle_url $url The URL of the alternate version.
1199 * @param string $mimetype The mime-type of the alternate version.
1201 public function add_alternate_version($title, $url, $mimetype) {
1202 if ($this->_state
> self
::STATE_BEFORE_HEADER
) {
1203 throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
1205 $alt = new stdClass
;
1206 $alt->title
= $title;
1208 $this->_alternateversions
[$mimetype] = $alt;
1212 * Specify a form control should be focused when the page has loaded.
1214 * @param string $controlid the id of the HTML element to be focused.
1216 public function set_focuscontrol($controlid) {
1217 $this->_focuscontrol
= $controlid;
1221 * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
1223 * @param string $html the HTML to display there.
1225 public function set_button($html) {
1226 $this->_button
= $html;
1230 * Set the capability that allows users to edit blocks on this page.
1232 * Normally the default of 'moodle/site:manageblocks' is used, but a few
1233 * pages like the My Moodle page need to use a different capability
1234 * like 'moodle/my:manageblocks'.
1236 * @param string $capability a capability.
1238 public function set_blocks_editing_capability($capability) {
1239 $this->_blockseditingcap
= $capability;
1243 * Some pages let you turn editing on for reasons other than editing blocks.
1244 * If that is the case, you can pass other capabilities that let the user
1245 * edit this page here.
1247 * @param string|array $capability either a capability, or an array of capabilities.
1249 public function set_other_editing_capability($capability) {
1250 if (is_array($capability)) {
1251 $this->_othereditingcaps
= array_unique($this->_othereditingcaps +
$capability);
1253 $this->_othereditingcaps
[] = $capability;
1258 * Sets whether the browser should cache this page or not.
1260 * @return bool $cacheable can this page be cached by the user's browser.
1262 public function set_cacheable($cacheable) {
1263 $this->_cacheable
= $cacheable;
1267 * Sets the page to periodically refresh
1269 * This function must be called before $OUTPUT->header has been called or
1270 * a coding exception will be thrown.
1272 * @param int $delay Sets the delay before refreshing the page, if set to null
1273 * refresh is cancelled
1275 public function set_periodic_refresh_delay($delay=null) {
1276 if ($this->_state
> self
::STATE_BEFORE_HEADER
) {
1277 throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
1279 if ($delay===null) {
1280 $this->_periodicrefreshdelay
= null;
1281 } else if (is_int($delay)) {
1282 $this->_periodicrefreshdelay
= $delay;
1287 * Force this page to use a particular theme.
1289 * Please use this cautiously.
1290 * It is only intended to be used by the themes selector admin page.
1292 * @param string $themename the name of the theme to use.
1294 public function force_theme($themename) {
1295 $this->ensure_theme_not_set();
1296 $this->_theme
= theme_config
::load($themename);
1300 * This function indicates that current page requires the https
1301 * when $CFG->loginhttps enabled.
1303 * By using this function properly, we can ensure 100% https-ized pages
1304 * at our entire discretion (login, forgot_password, change_password)
1307 public function https_required() {
1310 if (!is_null($this->_url
)) {
1311 throw new coding_exception('https_required() must be used before setting page url!');
1314 $this->ensure_theme_not_set();
1316 $this->_https_login_required
= true;
1318 if (!empty($CFG->loginhttps
)) {
1319 $CFG->httpswwwroot
= str_replace('http:', 'https:', $CFG->wwwroot
);
1321 $CFG->httpswwwroot
= $CFG->wwwroot
;
1326 * Makes sure that page previously marked with https_required()
1327 * is really using https://, if not it redirects to https://
1329 * @return void (may redirect to https://self)
1331 public function verify_https_required() {
1332 global $CFG, $FULLME;
1334 if (is_null($this->_url
)) {
1335 throw new coding_exception('verify_https_required() must be called after setting page url!');
1338 if (!$this->_https_login_required
) {
1339 throw new coding_exception('verify_https_required() must be called only after https_required()!');
1342 if (empty($CFG->loginhttps
)) {
1343 // https not required, so stop checking
1347 if (strpos($this->_url
, 'https://')) {
1348 // detect if incorrect PAGE->set_url() used, it is recommended to use root-relative paths there
1349 throw new coding_exception('Invalid page url specified, it must start with https:// for pages that set https_required()!');
1352 if (!empty($CFG->sslproxy
)) {
1353 // it does not make much sense to use sslproxy and loginhttps at the same time
1357 // now the real test and redirect!
1358 // NOTE: do NOT use this test for detection of https on current page because this code is not compatible with SSL proxies,
1359 // instead use strpos($CFG->httpswwwroot, 'https:') === 0
1360 if (strpos($FULLME, 'https:') !== 0) {
1361 // this may lead to infinite redirect on misconfigured sites, in that case use $CFG->loginhttps=0; in /config.php
1362 redirect($this->_url
);
1366 // Initialisation methods =====================================================
1367 // These set various things up in a default way.
1370 * This method is called when the page first moves out of the STATE_BEFORE_HEADER
1371 * state. This is our last change to initialise things.
1373 protected function starting_output() {
1376 if (!during_initial_install()) {
1377 $this->blocks
->load_blocks();
1378 if (empty($this->_block_actions_done
)) {
1379 $this->_block_actions_done
= true;
1380 if ($this->blocks
->process_url_actions($this)) {
1381 redirect($this->url
->out(false));
1384 $this->blocks
->create_all_block_instances();
1387 // If maintenance mode is on, change the page header.
1388 if (!empty($CFG->maintenance_enabled
)) {
1389 $this->set_button('<a href="' . $CFG->wwwroot
. '/' . $CFG->admin
.
1390 '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
1391 '</a> ' . $this->button
);
1393 $title = $this->title
;
1397 $this->set_title($title . get_string('maintenancemode', 'admin'));
1399 // Show the messaging popup if there are messages
1400 message_popup_window();
1403 $this->initialise_standard_body_classes();
1407 * Method for use by Moodle core to set up the theme. Do not
1408 * use this in your own code.
1410 * Make sure the right theme for this page is loaded. Tell our
1411 * blocks_manager about the theme block regions, and then, if
1412 * we are $PAGE, set up the global $OUTPUT.
1416 public function initialise_theme_and_output() {
1417 global $OUTPUT, $PAGE, $SITE;
1419 if (!empty($this->_wherethemewasinitialised
)) {
1423 if (!during_initial_install()) {
1424 // detect PAGE->context mess
1425 $this->magic_get_context();
1428 if (!$this->_course
&& !during_initial_install()) {
1429 $this->set_course($SITE);
1432 if (is_null($this->_theme
)) {
1433 $themename = $this->resolve_theme();
1434 $this->_theme
= theme_config
::load($themename);
1437 $this->_theme
->setup_blocks($this->pagelayout
, $this->blocks
);
1439 if ($this === $PAGE) {
1440 $OUTPUT = $this->get_renderer('core');
1443 $this->_wherethemewasinitialised
= debug_backtrace();
1447 * Work out the theme this page should use.
1449 * This depends on numerous $CFG settings, and the properties of this page.
1451 * @return string the name of the theme that should be used on this page.
1453 protected function resolve_theme() {
1454 global $CFG, $USER, $SESSION;
1456 if (empty($CFG->themeorder
)) {
1457 $themeorder = array('course', 'category', 'session', 'user', 'site');
1459 $themeorder = $CFG->themeorder
;
1460 // Just in case, make sure we always use the site theme if nothing else matched.
1461 $themeorder[] = 'site';
1464 $mnetpeertheme = '';
1465 if (isloggedin() and isset($CFG->mnet_localhost_id
) and $USER->mnethostid
!= $CFG->mnet_localhost_id
) {
1466 require_once($CFG->dirroot
.'/mnet/peer.php');
1467 $mnetpeer = new mnet_peer();
1468 $mnetpeer->set_id($USER->mnethostid
);
1469 if ($mnetpeer->force_theme
== 1 && $mnetpeer->theme
!= '') {
1470 $mnetpeertheme = $mnetpeer->theme
;
1474 foreach ($themeorder as $themetype) {
1475 switch ($themetype) {
1477 if (!empty($CFG->allowcoursethemes
) && !empty($this->_course
->theme
) && $this->devicetypeinuse
== 'default') {
1478 return $this->_course
->theme
;
1483 if (!empty($CFG->allowcategorythemes
) && $this->devicetypeinuse
== 'default') {
1484 $categories = $this->categories
;
1485 foreach ($categories as $category) {
1486 if (!empty($category->theme
)) {
1487 return $category->theme
;
1494 if (!empty($SESSION->theme
)) {
1495 return $SESSION->theme
;
1500 if (!empty($CFG->allowuserthemes
) && !empty($USER->theme
) && $this->devicetypeinuse
== 'default') {
1501 if ($mnetpeertheme) {
1502 return $mnetpeertheme;
1504 return $USER->theme
;
1510 if ($mnetpeertheme) {
1511 return $mnetpeertheme;
1513 // First try for the device the user is using.
1514 $devicetheme = get_selected_theme_for_device_type($this->devicetypeinuse
);
1515 if (!empty($devicetheme)) {
1516 return $devicetheme;
1518 // Next try for the default device (as a fallback)
1519 $devicetheme = get_selected_theme_for_device_type('default');
1520 if (!empty($devicetheme)) {
1521 return $devicetheme;
1523 // The default device theme isn't set up - use the overall default theme.
1524 return theme_config
::DEFAULT_THEME
;
1531 * Sets ->pagetype from the script name. For example, if the script that was
1532 * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
1534 * @param string $script the path to the script that should be used to
1535 * initialise ->pagetype. If not passed the $SCRIPT global will be used.
1536 * If legacy code has set $CFG->pagepath that will be used instead, and a
1537 * developer warning issued.
1539 protected function initialise_default_pagetype($script = null) {
1540 global $CFG, $SCRIPT;
1542 if (isset($CFG->pagepath
)) {
1543 debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
1544 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
1545 $script = $CFG->pagepath
;
1546 unset($CFG->pagepath
);
1549 if (is_null($script)) {
1550 $script = ltrim($SCRIPT, '/');
1551 $len = strlen($CFG->admin
);
1552 if (substr($script, 0, $len) == $CFG->admin
) {
1553 $script = 'admin' . substr($script, $len);
1557 $path = str_replace('.php', '', $script);
1558 if (substr($path, -1) == '/') {
1562 if (empty($path) ||
$path == 'index') {
1563 $this->_pagetype
= 'site-index';
1565 $this->_pagetype
= str_replace('/', '-', $path);
1570 * Initialises the CSS classes that will be added to body tag of the page.
1572 * The function is responsible for adding all of the critical CSS classes
1573 * that describe the current page, and its state.
1574 * This includes classes that describe the following for example:
1575 * - Current language
1576 * - Language direction
1577 * - YUI CSS initialisation
1579 * These are commonly used in CSS to target specific types of pages.
1581 protected function initialise_standard_body_classes() {
1584 $pagetype = $this->pagetype
;
1585 if ($pagetype == 'site-index') {
1586 $this->_legacyclass
= 'course';
1587 } else if (substr($pagetype, 0, 6) == 'admin-') {
1588 $this->_legacyclass
= 'admin';
1590 $this->add_body_class($this->_legacyclass
);
1592 $pathbits = explode('-', trim($pagetype));
1593 for ($i=1;$i<count($pathbits);$i++
) {
1594 $this->add_body_class('path-'.join('-',array_slice($pathbits, 0, $i)));
1597 $this->add_body_classes(get_browser_version_classes());
1598 $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
1599 $this->add_body_class('lang-' . current_language());
1600 $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
1601 $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
1602 $this->add_body_class($this->url_to_class_name($CFG->wwwroot
));
1604 $this->add_body_class('pagelayout-' . $this->_pagelayout
); // extra class describing current page layout
1606 if (!during_initial_install()) {
1607 $this->add_body_class('course-' . $this->_course
->id
);
1608 $this->add_body_class('context-' . $this->_context
->id
);
1611 if (!empty($this->_cm
)) {
1612 $this->add_body_class('cmid-' . $this->_cm
->id
);
1615 if (!empty($CFG->allowcategorythemes
)) {
1616 $this->ensure_category_loaded();
1617 foreach ($this->_categories
as $catid => $notused) {
1618 $this->add_body_class('category-' . $catid);
1622 if (is_array($this->_categories
)) {
1623 $catids = array_keys($this->_categories
);
1624 $catid = reset($catids);
1625 } else if (!empty($this->_course
->category
)) {
1626 $catid = $this->_course
->category
;
1629 $this->add_body_class('category-' . $catid);
1633 if (!isloggedin()) {
1634 $this->add_body_class('notloggedin');
1637 if (!empty($USER->editing
)) {
1638 $this->add_body_class('editing');
1639 if (optional_param('bui_moveid', false, PARAM_INT
)) {
1640 $this->add_body_class('blocks-moving');
1644 if (!empty($CFG->blocksdrag
)) {
1645 $this->add_body_class('drag');
1648 if ($this->_devicetypeinuse
!= 'default') {
1649 $this->add_body_class($this->_devicetypeinuse
. 'theme');
1654 * Loads the activity record for the current CM object associated with this
1657 * This will load {@link moodle_page::$_module} with a row from the related
1658 * module table in the database.
1659 * For instance if {@link moodle_page::$_cm} is a forum then a row from the
1660 * forum table will be loaded.
1662 protected function load_activity_record() {
1664 if (is_null($this->_cm
)) {
1667 $this->_module
= $DB->get_record($this->_cm
->modname
, array('id' => $this->_cm
->instance
));
1671 * This function ensures that the category of the current course has been
1672 * loaded, and if not, the function loads it now.
1675 * @throws coding_exception
1677 protected function ensure_category_loaded() {
1678 if (is_array($this->_categories
)) {
1679 return; // Already done.
1681 if (is_null($this->_course
)) {
1682 throw new coding_exception('Attempt to get the course category for this page before the course was set.');
1684 if ($this->_course
->category
== 0) {
1685 $this->_categories
= array();
1687 $this->load_category($this->_course
->category
);
1692 * Loads the requested category into the pages categories array.
1694 * @param ing $categoryid
1695 * @throws moodle_exception
1697 protected function load_category($categoryid) {
1699 $category = $DB->get_record('course_categories', array('id' => $categoryid));
1701 throw new moodle_exception('unknowncategory');
1703 $this->_categories
[$category->id
] = $category;
1704 $parentcategoryids = explode('/', trim($category->path
, '/'));
1705 array_pop($parentcategoryids);
1706 foreach (array_reverse($parentcategoryids) as $catid) {
1707 $this->_categories
[$catid] = null;
1712 * Ensures that the category the current course is within, as well as all of
1713 * its parent categories, have been loaded.
1717 protected function ensure_categories_loaded() {
1719 $this->ensure_category_loaded();
1720 if (!is_null(end($this->_categories
))) {
1721 return; // Already done.
1723 $idstoload = array_keys($this->_categories
);
1724 array_shift($idstoload);
1725 $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
1726 foreach ($idstoload as $catid) {
1727 $this->_categories
[$catid] = $categories[$catid];
1732 * Ensure the theme has not been loaded yet. If it has an exception is thrown.
1735 * @throws coding_exception
1737 protected function ensure_theme_not_set() {
1738 if (!is_null($this->_theme
)) {
1739 throw new coding_exception('The theme has already been set up for this page ready for output. ' .
1740 'Therefore, you can no longer change the theme, or anything that might affect what ' .
1741 'the current theme is, for example, the course.',
1742 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised
));
1747 * Converts the provided URL into a CSS class that be used within the page.
1748 * This is primarily used to add the wwwroot to the body tag as a CSS class.
1750 * @param string $url
1753 protected function url_to_class_name($url) {
1754 $bits = parse_url($url);
1755 $class = str_replace('.', '-', $bits['host']);
1756 if (!empty($bits['port'])) {
1757 $class .= '--' . $bits['port'];
1759 if (!empty($bits['path'])) {
1760 $path = trim($bits['path'], '/');
1762 $class .= '--' . str_replace('/', '-', $path);
1769 * Combines all of the required editing caps for the page and returns them
1774 protected function all_editing_caps() {
1775 $caps = $this->_othereditingcaps
;
1776 $caps[] = $this->_blockseditingcap
;
1781 * Returns true if the page URL has beem set.
1785 public function has_set_url() {
1786 return ($this->_url
!==null);
1790 * Gets set when the block actions for the page have been processed.
1792 * @param bool $setting
1794 public function set_block_actions_done($setting = true) {
1795 $this->_block_actions_done
= $setting;
1799 * Are popup notifications allowed on this page?
1800 * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
1802 * @return bool true if popup notifications may be displayed
1804 public function get_popup_notification_allowed() {
1805 return $this->_popup_notification_allowed
;
1809 * Allow or disallow popup notifications on this page. Popups are allowed by default.
1811 * @param bool $allowed true if notifications are allowed. False if not allowed. They are allowed by default.
1813 public function set_popup_notification_allowed($allowed) {
1814 $this->_popup_notification_allowed
= $allowed;