MDL-55074 theme_boost: Navigation and blocks
[moodle.git] / lib / pagelib.php
CommitLineData
50fcb1d8 1<?php
50fcb1d8 2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
f032aa7a 16
e0134f84 17/**
de60de04 18 * This file contains the moodle_page class. There is normally a single instance
78bfb562 19 * of this class in the $PAGE global variable. This class is a central repository
de60de04 20 * of information about the page we are building up to send back to the user.
e0134f84 21 *
670b098b 22 * @package core
cd4ee5ea 23 * @category page
78bfb562
PS
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e0134f84 26 */
27
78bfb562 28defined('MOODLE_INTERNAL') || die();
2c144fc3 29
c13a5e71 30/**
31 * $PAGE is a central store of information about the current page we are
5c5418fe 32 * generating in response to the user's request.
33 *
34 * It does not do very much itself
c13a5e71 35 * except keep track of information, however, it serves as the access point to
36 * some more significant components like $PAGE->theme, $PAGE->requires,
37 * $PAGE->blocks, etc.
50fcb1d8 38 *
5c5418fe 39 * @copyright 2009 Tim Hunt
0a0034f8 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 41 * @since Moodle 2.0
670b098b 42 * @package core
cd4ee5ea 43 * @category page
0a0034f8
SH
44 *
45 * The following properties are alphabetical. Please keep it that way so that its
46 * easy to maintain.
2c144fc3 47 *
4ac8345f
SH
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.
0a0034f8 50 * @property-read stdClass $activityrecord The row from the activities own database table (for example
4ac8345f
SH
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.
06a72e01 54 * @property-read block_manager $blocks The blocks manager object for this page.
36b77e3a 55 * @property-read array $blockmanipulations
cd4ee5ea
SH
56 * @property-read string $bodyclasses A string to use within the class attribute on the body tag.
57 * @property-read string $bodyid A string to use as the id of the body tag.
01ee99f4 58 * @property-read string $button The HTML to go where the Turn editing on button normally goes.
4ac8345f
SH
59 * @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
60 * @property-read array $categories An array of all the categories the page course belongs to,
61 * starting with the immediately containing category, and working out to
62 * the top-level category. This may be the empty array if we are in the
63 * front page course.
cd4ee5ea 64 * @property-read mixed $category The category that the page course belongs to.
0a0034f8 65 * @property-read cm_info $cm The course_module that this page belongs to. Will be null
4ac8345f
SH
66 * if this page is not within a module. This is a full cm object, as loaded
67 * by get_coursemodule_from_id or get_coursemodule_from_instance,
68 * so the extra modname and name fields are present.
0a0034f8
SH
69 * @property-read context $context The main context to which this page belongs.
70 * @property-read stdClass $course The current course that we are inside - a row from the
4ac8345f
SH
71 * course table. (Also available as $COURSE global.) If we are not inside
72 * an actual course, this will be the site course.
e5824bb9 73 * @property-read string $devicetypeinuse The name of the device type in use
4ac8345f 74 * @property-read string $docspath The path to the Moodle docs for this page.
01ee99f4 75 * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
cd4ee5ea 76 * @property-read bool $headerprinted True if the page header has already been printed.
4ac8345f
SH
77 * @property-read string $heading The main heading that should be displayed at the top of the <body>.
78 * @property-read string $headingmenu The menu (or actions) to display in the heading
cd4ee5ea
SH
79 * @property-read array $layout_options An arrays with options for the layout file.
80 * @property-read array $legacythemeinuse True if the legacy browser theme is in use.
81 * @property-read navbar $navbar The navbar object used to display the navbar
82 * @property-read global_navigation $navigation The navigation structure for this page.
e9d412b3 83 * @property-read xhtml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
4ac8345f
SH
84 * mainly for internal use by the rendering code.
85 * @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
34a2777c 86 * Allows the theme to display things differently, if it wishes to.
cd4ee5ea 87 * @property-read string $pagetype The page type string, should be used as the id for the body tag in the theme.
4ac8345f
SH
88 * @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
89 * @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
965a3c66
90 * @property-read string $requestip The IP address of the current request, null if unknown.
91 * @property-read string $requestorigin The type of request 'web', 'ws', 'cli', 'restore', etc.
0a0034f8 92 * @property-read settings_navigation $settingsnav The settings navigation
4ac8345f
SH
93 * @property-read int $state One of the STATE_... constants
94 * @property-read string $subpage The subpage identifier, if any.
cd4ee5ea 95 * @property-read theme_config $theme The theme for this page.
4ac8345f
SH
96 * @property-read string $title The title that should go in the <head> section of the HTML of this page.
97 * @property-read moodle_url $url The moodle url object for this page.
c13a5e71 98 */
99class moodle_page {
0a0034f8 100
cd4ee5ea 101 /** The state of the page before it has printed the header **/
c13a5e71 102 const STATE_BEFORE_HEADER = 0;
cd4ee5ea
SH
103
104 /** The state the page is in temporarily while the header is being printed **/
c13a5e71 105 const STATE_PRINTING_HEADER = 1;
cd4ee5ea
SH
106
107 /** The state the page is in while content is presumably being printed **/
c13a5e71 108 const STATE_IN_BODY = 2;
c13a5e71 109
cd4ee5ea
SH
110 /**
111 * The state the page is when the footer has been printed and its function is
112 * complete.
113 */
114 const STATE_DONE = 3;
753debd2 115
0a0034f8 116 /**
cd4ee5ea
SH
117 * @var int The current state of the page. The state a page is within
118 * determines what actions are possible for it.
0a0034f8 119 */
c13a5e71 120 protected $_state = self::STATE_BEFORE_HEADER;
121
0a0034f8 122 /**
cd4ee5ea
SH
123 * @var stdClass The course currently associated with this page.
124 * If not has been provided the front page course is used.
0a0034f8 125 */
c13a5e71 126 protected $_course = null;
127
5ec434a9 128 /**
cd4ee5ea
SH
129 * @var cm_info If this page belongs to a module, this is the cm_info module
130 * description object.
5ec434a9 131 */
132 protected $_cm = null;
133
134 /**
cd4ee5ea
SH
135 * @var stdClass If $_cm is not null, then this will hold the corresponding
136 * row from the modname table. For example, if $_cm->modname is 'quiz', this
137 * will be a row from the quiz table.
5ec434a9 138 */
139 protected $_module = null;
140
141 /**
cd4ee5ea 142 * @var context The context that this page belongs to.
5ec434a9 143 */
2afe21ee 144 protected $_context = null;
145
948203a5 146 /**
cd4ee5ea 147 * @var array This holds any categories that $_course belongs to, starting with the
948203a5 148 * particular category it belongs to, and working out through any parent
01ee99f4 149 * categories to the top level. These are loaded progressively, if needed.
948203a5 150 * There are three states. $_categories = null initially when nothing is
151 * loaded; $_categories = array($id => $cat, $parentid => null) when we have
152 * loaded $_course->category, but not any parents; and a complete array once
153 * everything is loaded.
154 */
155 protected $_categories = null;
156
0a0034f8 157 /**
cd4ee5ea 158 * @var array An array of CSS classes that should be added to the body tag in HTML.
0a0034f8 159 */
753debd2 160 protected $_bodyclasses = array();
161
0a0034f8 162 /**
cd4ee5ea 163 * @var string The title for the page. Used within the title tag in the HTML head.
0a0034f8 164 */
34a2777c 165 protected $_title = '';
166
0a0034f8 167 /**
cd4ee5ea 168 * @var string The string to use as the heading of the page. Shown near the top of the
0a0034f8 169 * page within most themes.
0a0034f8 170 */
34a2777c 171 protected $_heading = '';
172
0a0034f8 173 /**
cd4ee5ea 174 * @var string The pagetype is used to describe the page and defaults to a representation
0a0034f8 175 * of the physical path to the page e.g. my-index, mod-quiz-attempt
0a0034f8 176 */
f230ce19 177 protected $_pagetype = null;
178
0a0034f8 179 /**
cd4ee5ea
SH
180 * @var string The pagelayout to use when displaying this page. The
181 * pagelayout needs to have been defined by the theme in use, or one of its
182 * parents. By default base is used however standard is the more common layout.
0a0034f8
SH
183 * Note that this gets automatically set by core during operations like
184 * require_login.
0a0034f8 185 */
191b267b 186 protected $_pagelayout = 'base';
78946b9b
PS
187
188 /**
cd4ee5ea 189 * @var array List of theme layout options, these are ignored by core.
78946b9b 190 * To be used in individual theme layout files only.
78946b9b 191 */
e9324a22 192 protected $_layout_options = null;
34a2777c 193
0a0034f8 194 /**
cd4ee5ea 195 * @var string An optional arbitrary parameter that can be set on pages where the context
0a0034f8 196 * and pagetype is not enough to identify the page.
0a0034f8 197 */
08eab897 198 protected $_subpage = '';
5ed70539 199
0a0034f8 200 /**
cd4ee5ea 201 * @var string Set a different path to use for the 'Moodle docs for this page' link.
0a0034f8 202 * By default, it uses the path of the file for instance mod/quiz/attempt.
0a0034f8 203 */
82611d8d 204 protected $_docspath = null;
205
0a0034f8 206 /**
cd4ee5ea 207 * @var string A legacy class that will be added to the body tag
0a0034f8 208 */
d529807a 209 protected $_legacyclass = null;
210
0a0034f8 211 /**
cd4ee5ea
SH
212 * @var moodle_url The URL for this page. This is mandatory and must be set
213 * before output is started.
0a0034f8 214 */
75781f87 215 protected $_url = null;
216
0a0034f8 217 /**
cd4ee5ea
SH
218 * @var array An array of links to alternative versions of this page.
219 * Primarily used for RSS versions of the current page.
0a0034f8 220 */
34a2777c 221 protected $_alternateversions = array();
222
0a0034f8 223 /**
e9d412b3 224 * @var block_manager The blocks manager for this page. It is responsible for
cd4ee5ea 225 * the blocks and there content on this page.
0a0034f8 226 */
ad52c04f 227 protected $_blocks = null;
228
0a0034f8 229 /**
e9d412b3 230 * @var page_requirements_manager Page requirements manager. It is responsible
cd4ee5ea 231 * for all JavaScript and CSS resources required by this page.
0a0034f8 232 */
b2330db6 233 protected $_requires = null;
234
0a0034f8 235 /**
cd4ee5ea
SH
236 * @var string The capability required by the user in order to edit blocks
237 * and block settings on this page.
0a0034f8 238 */
934524d7 239 protected $_blockseditingcap = 'moodle/site:manageblocks';
240
0a0034f8 241 /**
cd4ee5ea 242 * @var bool An internal flag to record when block actions have been processed.
0a0034f8
SH
243 * Remember block actions occur on the current URL and it is important that
244 * even they are never executed more than once.
0a0034f8 245 */
7db8dd1e
PS
246 protected $_block_actions_done = false;
247
0a0034f8 248 /**
cd4ee5ea
SH
249 * @var array An array of any other capabilities the current user must have
250 * in order to editing the page and/or its content (not just blocks).
0a0034f8 251 */
934524d7 252 protected $_othereditingcaps = array();
253
0a0034f8 254 /**
cd4ee5ea
SH
255 * @var bool Sets whether this page should be cached by the browser or not.
256 * If it is set to true (default) the page is served with caching headers.
0a0034f8 257 */
34a2777c 258 protected $_cacheable = true;
259
0a0034f8 260 /**
cd4ee5ea
SH
261 * @var string Can be set to the ID of an element on the page, if done that
262 * element receives focus when the page loads.
0a0034f8 263 */
34a2777c 264 protected $_focuscontrol = '';
265
0a0034f8 266 /**
cd4ee5ea
SH
267 * @var string HTML to go where the turn on editing button is located. This
268 * is nearly a legacy item and not used very often any more.
0a0034f8 269 */
34a2777c 270 protected $_button = '';
571fa828 271
0a0034f8 272 /**
cd4ee5ea 273 * @var theme_config The theme to use with this page. This has to be properly
670b098b 274 * initialised via {@link moodle_page::initialise_theme_and_output()} which
cd4ee5ea 275 * happens magically before any operation that requires it.
0a0034f8 276 */
b7009474 277 protected $_theme = null;
0a0034f8
SH
278
279 /**
cd4ee5ea 280 * @var global_navigation Contains the global navigation structure.
0a0034f8 281 */
7d2a0492 282 protected $_navigation = null;
0a0034f8
SH
283
284 /**
cd4ee5ea 285 * @var settings_navigation Contains the settings navigation structure.
0a0034f8 286 */
7d2a0492 287 protected $_settingsnav = null;
0a0034f8 288
99061152
DW
289 /**
290 * @var flat_navigation Contains a list of nav nodes, most closely related to the current page.
291 */
292 protected $_flatnav = null;
293
0a0034f8 294 /**
cd4ee5ea 295 * @var navbar Contains the navbar structure.
0a0034f8 296 */
7d2a0492 297 protected $_navbar = null;
0a0034f8
SH
298
299 /**
cd4ee5ea 300 * @var string The menu (or actions) to display in the heading.
0a0034f8 301 */
e120c61d 302 protected $_headingmenu = null;
b7009474 303
304 /**
cd4ee5ea
SH
305 * @var array stack trace. Then the theme is initialised, we save the stack
306 * trace, for use in error messages.
b7009474 307 */
308 protected $_wherethemewasinitialised = null;
309
0a0034f8 310 /**
cd4ee5ea
SH
311 * @var xhtml_container_stack Tracks XHTML tags on this page that have been
312 * opened but not closed.
0a0034f8 313 */
db8d89d8 314 protected $_opencontainers;
315
17a6649b 316 /**
cd4ee5ea
SH
317 * @var int Sets the page to refresh after a given delay (in seconds) using
318 * meta refresh in {@link standard_head_html()} in outputlib.php
17a6649b 319 * If set to null(default) the page is not refreshed
17a6649b 320 */
321 protected $_periodicrefreshdelay = null;
322
ee8df661 323 /**
cd4ee5ea 324 * @var array Associative array of browser shortnames (as used by check_browser_version)
ee8df661 325 * and their minimum required versions
ee8df661
SH
326 */
327 protected $_legacybrowsers = array('MSIE' => 6.0);
328
329 /**
cd4ee5ea 330 * @var string Is set to the name of the device type in use.
e5824bb9 331 * This will we worked out when it is first used.
ee8df661 332 */
e5824bb9 333 protected $_devicetypeinuse = null;
78bfb562 334
0a0034f8 335 /**
cd4ee5ea 336 * @var bool Used to determine if HTTPS should be required for login.
0a0034f8 337 */
17c70aa0
PS
338 protected $_https_login_required = false;
339
0a0034f8 340 /**
cd4ee5ea 341 * @var bool Determines if popup notifications allowed on this page.
0a0034f8
SH
342 * Code such as the quiz module disables popup notifications in situations
343 * such as upgrading or completing a quiz.
0a0034f8 344 */
e7f93d5c
AD
345 protected $_popup_notification_allowed = true;
346
0a0034f8
SH
347 // Magic getter methods =============================================================
348 // Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
349 // methods, but instead use the $PAGE->x syntax.
753debd2 350
c13a5e71 351 /**
670b098b 352 * Please do not call this method directly, use the ->state syntax. {@link moodle_page::__get()}.
0a0034f8 353 * @return integer one of the STATE_XXX constants. You should not normally need
01ee99f4 354 * to use this in your code. It is intended for internal use by this class
c13a5e71 355 * and its friends like print_header, to check that everything is working as
356 * expected. Also accessible as $PAGE->state.
357 */
5515b536 358 protected function magic_get_state() {
c13a5e71 359 return $this->_state;
360 }
361
362 /**
670b098b 363 * Please do not call this method directly, use the ->headerprinted syntax. {@link moodle_page::__get()}.
5edb0fb8 364 * @return bool has the header already been printed?
c13a5e71 365 */
5515b536 366 protected function magic_get_headerprinted() {
c13a5e71 367 return $this->_state >= self::STATE_IN_BODY;
368 }
369
370 /**
670b098b 371 * Please do not call this method directly, use the ->course syntax. {@link moodle_page::__get()}.
0a0034f8 372 * @return stdClass the current course that we are inside - a row from the
c13a5e71 373 * course table. (Also available as $COURSE global.) If we are not inside
b7b2d0f3 374 * an actual course, this will be the site course.
c13a5e71 375 */
5515b536 376 protected function magic_get_course() {
c13a5e71 377 global $SITE;
378 if (is_null($this->_course)) {
379 return $SITE;
380 }
381 return $this->_course;
382 }
383
5ec434a9 384 /**
670b098b 385 * Please do not call this method directly, use the ->cm syntax. {@link moodle_page::__get()}.
0a0034f8 386 * @return cm_info the course_module that this page belongs to. Will be null
5ec434a9 387 * if this page is not within a module. This is a full cm object, as loaded
388 * by get_coursemodule_from_id or get_coursemodule_from_instance,
389 * so the extra modname and name fields are present.
390 */
5515b536 391 protected function magic_get_cm() {
5ec434a9 392 return $this->_cm;
393 }
394
395 /**
670b098b 396 * Please do not call this method directly, use the ->activityrecord syntax. {@link moodle_page::__get()}.
0a0034f8 397 * @return stdClass the row from the activities own database table (for example
b7b2d0f3 398 * the forum or quiz table) that this page belongs to. Will be null
399 * if this page is not within a module.
5ec434a9 400 */
5515b536 401 protected function magic_get_activityrecord() {
5ec434a9 402 if (is_null($this->_module) && !is_null($this->_cm)) {
403 $this->load_activity_record();
404 }
405 return $this->_module;
406 }
407
408 /**
670b098b 409 * Please do not call this method directly, use the ->activityname syntax. {@link moodle_page::__get()}.
0a0034f8 410 * @return string the The type of activity we are in, for example 'forum' or 'quiz'.
b7b2d0f3 411 * Will be null if this page is not within a module.
5ec434a9 412 */
5515b536 413 protected function magic_get_activityname() {
5ec434a9 414 if (is_null($this->_cm)) {
415 return null;
416 }
417 return $this->_cm->modname;
418 }
419
948203a5 420 /**
670b098b 421 * Please do not call this method directly, use the ->category syntax. {@link moodle_page::__get()}.
0a0034f8 422 * @return stdClass the category that the page course belongs to. If there isn't one
948203a5 423 * (that is, if this is the front page course) returns null.
424 */
5515b536 425 protected function magic_get_category() {
948203a5 426 $this->ensure_category_loaded();
427 if (!empty($this->_categories)) {
428 return reset($this->_categories);
429 } else {
430 return null;
431 }
432 }
433
434 /**
670b098b 435 * Please do not call this method directly, use the ->categories syntax. {@link moodle_page::__get()}.
948203a5 436 * @return array an array of all the categories the page course belongs to,
437 * starting with the immediately containing category, and working out to
438 * the top-level category. This may be the empty array if we are in the
439 * front page course.
440 */
5515b536 441 protected function magic_get_categories() {
948203a5 442 $this->ensure_categories_loaded();
443 return $this->_categories;
444 }
445
2afe21ee 446 /**
670b098b 447 * Please do not call this method directly, use the ->context syntax. {@link moodle_page::__get()}.
0a0034f8 448 * @return context the main context to which this page belongs.
2afe21ee 449 */
5515b536 450 protected function magic_get_context() {
67fc782f 451 global $CFG;
2afe21ee 452 if (is_null($this->_context)) {
eb5bdb35 453 if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
e9d412b3
SH
454 // Cli scripts work in system context, do not annoy devs with debug info.
455 // Very few scripts do not use cookies, we can safely use system as default context there.
67fc782f
MG
456 } else if (AJAX_SCRIPT && $CFG->debugdeveloper) {
457 // Throw exception inside AJAX script in developer mode, otherwise the debugging message may be missed.
458 throw new coding_exception('$PAGE->context was not set. You may have forgotten '
459 .'to call require_login() or $PAGE->set_context()');
4ee51346 460 } else {
c78f8443
MJ
461 debugging('Coding problem: $PAGE->context was not set. You may have forgotten '
462 .'to call require_login() or $PAGE->set_context(). The page may not display '
463 .'correctly as a result');
4ee51346 464 }
b0c6dc1c 465 $this->_context = context_system::instance();
2afe21ee 466 }
467 return $this->_context;
468 }
469
f230ce19 470 /**
670b098b 471 * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
90723839 472 * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
f230ce19 473 */
5515b536 474 protected function magic_get_pagetype() {
bd348bda 475 global $CFG;
d529807a 476 if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
477 $this->initialise_default_pagetype();
f230ce19 478 }
479 return $this->_pagetype;
480 }
481
90723839 482 /**
670b098b 483 * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
90723839
SH
484 * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
485 */
486 protected function magic_get_bodyid() {
487 return 'page-'.$this->pagetype;
488 }
489
5ed70539 490 /**
670b098b 491 * Please do not call this method directly, use the ->pagelayout syntax. {@link moodle_page::__get()}.
191b267b 492 * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
34a2777c 493 * Allows the theme to display things differently, if it wishes to.
494 */
5515b536 495 protected function magic_get_pagelayout() {
78946b9b
PS
496 return $this->_pagelayout;
497 }
498
499 /**
88b907fd
AB
500 * Please do not call this method directly, use the ->layout_options syntax. {@link moodle_page::__get()}.
501 * @return array returns arrays with options for layout file
78946b9b 502 */
5515b536 503 protected function magic_get_layout_options() {
e9324a22
PS
504 if (!is_array($this->_layout_options)) {
505 $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
506 }
78946b9b 507 return $this->_layout_options;
34a2777c 508 }
509
510 /**
670b098b 511 * Please do not call this method directly, use the ->subpage syntax. {@link moodle_page::__get()}.
0a0034f8 512 * @return string The subpage identifier, if any.
5ed70539 513 */
5515b536 514 protected function magic_get_subpage() {
5ed70539 515 return $this->_subpage;
516 }
517
753debd2 518 /**
670b098b 519 * Please do not call this method directly, use the ->bodyclasses syntax. {@link moodle_page::__get()}.
753debd2 520 * @return string the class names to put on the body element in the HTML.
521 */
5515b536 522 protected function magic_get_bodyclasses() {
753debd2 523 return implode(' ', array_keys($this->_bodyclasses));
524 }
525
82611d8d 526 /**
670b098b 527 * Please do not call this method directly, use the ->title syntax. {@link moodle_page::__get()}.
34a2777c 528 * @return string the title that should go in the <head> section of the HTML of this page.
529 */
5515b536 530 protected function magic_get_title() {
34a2777c 531 return $this->_title;
532 }
533
534 /**
670b098b 535 * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
34a2777c 536 * @return string the main heading that should be displayed at the top of the <body>.
537 */
5515b536 538 protected function magic_get_heading() {
34a2777c 539 return $this->_heading;
540 }
541
e120c61d 542 /**
670b098b 543 * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
e120c61d 544 * @return string The menu (or actions) to display in the heading
545 */
5515b536 546 protected function magic_get_headingmenu() {
e120c61d 547 return $this->_headingmenu;
548 }
549
34a2777c 550 /**
670b098b 551 * Please do not call this method directly, use the ->docspath syntax. {@link moodle_page::__get()}.
b7b2d0f3 552 * @return string the path to the Moodle docs for this page.
82611d8d 553 */
5515b536 554 protected function magic_get_docspath() {
82611d8d 555 if (is_string($this->_docspath)) {
556 return $this->_docspath;
557 } else {
558 return str_replace('-', '/', $this->pagetype);
559 }
560 }
561
75781f87 562 /**
670b098b 563 * Please do not call this method directly, use the ->url syntax. {@link moodle_page::__get()}.
75781f87 564 * @return moodle_url the clean URL required to load the current page. (You
565 * should normally use this in preference to $ME or $FULLME.)
566 */
5515b536 567 protected function magic_get_url() {
c1df9d7c 568 global $FULLME;
75781f87 569 if (is_null($this->_url)) {
c1df9d7c 570 debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
727ae436 571 $this->_url = new moodle_url($FULLME);
572 // Make sure the guessed URL cannot lead to dangerous redirects.
573 $this->_url->remove_params('sesskey');
75781f87 574 }
ad52c04f 575 return new moodle_url($this->_url); // Return a clone for safety.
576 }
577
578 /**
34a2777c 579 * The list of alternate versions of this page.
580 * @return array mime type => object with ->url and ->title.
581 */
5515b536 582 protected function magic_get_alternateversions() {
34a2777c 583 return $this->_alternateversions;
584 }
585
586 /**
670b098b 587 * Please do not call this method directly, use the ->blocks syntax. {@link moodle_page::__get()}.
e9d412b3 588 * @return block_manager the blocks manager object for this page.
ad52c04f 589 */
5515b536 590 protected function magic_get_blocks() {
78946b9b 591 global $CFG;
ad52c04f 592 if (is_null($this->_blocks)) {
86b5ea0f 593 if (!empty($CFG->blockmanagerclass)) {
3edc37cb
PS
594 if (!empty($CFG->blockmanagerclassfile)) {
595 require_once($CFG->blockmanagerclassfile);
596 }
86b5ea0f 597 $classname = $CFG->blockmanagerclass;
598 } else {
599 $classname = 'block_manager';
600 }
601 $this->_blocks = new $classname($this);
ad52c04f 602 }
603 return $this->_blocks;
75781f87 604 }
605
b2330db6 606 /**
670b098b 607 * Please do not call this method directly, use the ->requires syntax. {@link moodle_page::__get()}.
571fa828 608 * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
b2330db6 609 */
5515b536 610 protected function magic_get_requires() {
b2330db6 611 if (is_null($this->_requires)) {
612 $this->_requires = new page_requirements_manager();
b2330db6 613 }
614 return $this->_requires;
615 }
616
571fa828 617 /**
670b098b 618 * Please do not call this method directly, use the ->cacheable syntax. {@link moodle_page::__get()}.
5edb0fb8 619 * @return bool can this page be cached by the user's browser.
571fa828 620 */
5515b536 621 protected function magic_get_cacheable() {
34a2777c 622 return $this->_cacheable;
623 }
624
625 /**
670b098b 626 * Please do not call this method directly, use the ->focuscontrol syntax. {@link moodle_page::__get()}.
01ee99f4 627 * @return string the id of the HTML element to be focused when the page has loaded.
34a2777c 628 */
5515b536 629 protected function magic_get_focuscontrol() {
34a2777c 630 return $this->_focuscontrol;
631 }
632
633 /**
670b098b 634 * Please do not call this method directly, use the ->button syntax. {@link moodle_page::__get()}.
01ee99f4 635 * @return string the HTML to go where the Turn editing on button normally goes.
34a2777c 636 */
5515b536 637 protected function magic_get_button() {
34a2777c 638 return $this->_button;
571fa828 639 }
640
b7009474 641 /**
670b098b 642 * Please do not call this method directly, use the ->theme syntax. {@link moodle_page::__get()}.
4ac8345f 643 * @return theme_config the initialised theme for this page.
b7009474 644 */
5515b536 645 protected function magic_get_theme() {
b7009474 646 if (is_null($this->_theme)) {
647 $this->initialise_theme_and_output();
648 }
649 return $this->_theme;
650 }
651
36b77e3a
SH
652 /**
653 * Returns an array of minipulations or false if there are none to make.
654 *
5bcfd504 655 * @since Moodle 2.5.1 2.6
36b77e3a
SH
656 * @return bool|array
657 */
658 protected function magic_get_blockmanipulations() {
659 if (!right_to_left()) {
660 return false;
661 }
662 if (is_null($this->_theme)) {
663 $this->initialise_theme_and_output();
664 }
665 return $this->_theme->blockrtlmanipulations;
666 }
667
ee8df661 668 /**
670b098b 669 * Please do not call this method directly, use the ->devicetypeinuse syntax. {@link moodle_page::__get()}.
e5824bb9 670 * @return string The device type being used.
ee8df661 671 */
37959dd4 672 protected function magic_get_devicetypeinuse() {
e5824bb9 673 if (empty($this->_devicetypeinuse)) {
c3d2fbf9 674 $this->_devicetypeinuse = core_useragent::get_user_device_type();
e5824bb9
SH
675 }
676 return $this->_devicetypeinuse;
677 }
678
17a6649b 679 /**
afa2dcad 680 * Please do not call this method directly use the ->periodicrefreshdelay syntax
670b098b 681 * {@link moodle_page::__get()}
afa2dcad 682 * @return int The periodic refresh delay to use with meta refresh
17a6649b 683 */
5515b536 684 protected function magic_get_periodicrefreshdelay() {
17a6649b 685 return $this->_periodicrefreshdelay;
686 }
687
db8d89d8 688 /**
670b098b 689 * Please do not call this method directly use the ->opencontainers syntax. {@link moodle_page::__get()}
db8d89d8 690 * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
691 * mainly for internal use by the rendering code.
692 */
5515b536 693 protected function magic_get_opencontainers() {
db8d89d8 694 if (is_null($this->_opencontainers)) {
695 $this->_opencontainers = new xhtml_container_stack();
696 }
697 return $this->_opencontainers;
698 }
699
7d2a0492 700 /**
701 * Return the navigation object
702 * @return global_navigation
703 */
5515b536 704 protected function magic_get_navigation() {
7d2a0492 705 if ($this->_navigation === null) {
3406acde 706 $this->_navigation = new global_navigation($this);
7d2a0492 707 }
708 return $this->_navigation;
709 }
710
711 /**
712 * Return a navbar object
713 * @return navbar
714 */
5515b536 715 protected function magic_get_navbar() {
7d2a0492 716 if ($this->_navbar === null) {
717 $this->_navbar = new navbar($this);
718 }
719 return $this->_navbar;
720 }
721
722 /**
723 * Returns the settings navigation object
724 * @return settings_navigation
725 */
5515b536 726 protected function magic_get_settingsnav() {
7d2a0492 727 if ($this->_settingsnav === null) {
728 $this->_settingsnav = new settings_navigation($this);
729 $this->_settingsnav->initialise();
730 }
731 return $this->_settingsnav;
732 }
965a3c66 733
99061152
DW
734 /**
735 * Returns the flat navigation object
736 * @return flat_navigation
737 */
738 protected function magic_get_flatnav() {
739 if ($this->_flatnav === null) {
740 $this->_flatnav = new flat_navigation($this);
741 $this->_flatnav->initialise();
742 }
743 return $this->_flatnav;
744 }
745
965a3c66
746 /**
747 * Returns request IP address.
748 *
749 * @return string IP address or null if unknown
750 */
751 protected function magic_get_requestip() {
752 return getremoteaddr(null);
753 }
754
755 /**
756 * Returns the origin of current request.
757 *
758 * Note: constants are not required because we need to use these values in logging and reports.
759 *
760 * @return string 'web', 'ws', 'cli', 'restore', etc.
761 */
762 protected function magic_get_requestorigin() {
763 if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
764 return 'restore';
765 }
766
767 if (WS_SERVER) {
768 return 'ws';
769 }
770
771 if (CLI_SCRIPT) {
772 return 'cli';
773 }
774
775 return 'web';
776 }
7d2a0492 777
89fbdca3 778 /**
b7b2d0f3 779 * PHP overloading magic to make the $PAGE->course syntax work by redirecting
5515b536 780 * it to the corresponding $PAGE->magic_get_course() method if there is one, and
b7b2d0f3 781 * throwing an exception if not.
7db8dd1e 782 *
0a0034f8 783 * @param string $name property name
5515b536 784 * @return mixed
e9d412b3 785 * @throws coding_exception
89fbdca3 786 */
7db8dd1e
PS
787 public function __get($name) {
788 $getmethod = 'magic_get_' . $name;
89fbdca3 789 if (method_exists($this, $getmethod)) {
790 return $this->$getmethod();
791 } else {
7db8dd1e
PS
792 throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
793 }
794 }
795
796 /**
5edb0fb8
SH
797 * PHP overloading magic to catch obvious coding errors.
798 *
799 * This method has been created to catch obvious coding errors where the
800 * developer has tried to set a page property using $PAGE->key = $value.
801 * In the moodle_page class all properties must be set using the appropriate
802 * $PAGE->set_something($value) method.
7db8dd1e 803 *
0a0034f8 804 * @param string $name property name
670b098b 805 * @param mixed $value Value
0a0034f8 806 * @return void Throws exception if field not defined in page class
e9d412b3 807 * @throws coding_exception
7db8dd1e
PS
808 */
809 public function __set($name, $value) {
810 if (method_exists($this, 'set_' . $name)) {
811 throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
812 } else {
813 throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
89fbdca3 814 }
815 }
816
e9d412b3 817 // Other information getting methods ==========================================.
830dd6e9 818
649cf95d
PS
819 /**
820 * Returns instance of page renderer
0a0034f8 821 *
649cf95d
PS
822 * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
823 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 824 * @param string $target one of rendering target constants
649cf95d
PS
825 * @return renderer_base
826 */
c927e35c 827 public function get_renderer($component, $subtype = null, $target = null) {
166ac0a3
SH
828 if ($this->pagelayout === 'maintenance') {
829 // If the page is using the maintenance layout then we're going to force target to maintenance.
830 // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
831 // page layout.
832 $target = RENDERER_TARGET_MAINTENANCE;
833 }
c927e35c 834 return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
649cf95d
PS
835 }
836
5515b536
PS
837 /**
838 * Checks to see if there are any items on the navbar object
0a0034f8 839 *
5515b536
PS
840 * @return bool true if there are, false if not
841 */
842 public function has_navbar() {
843 if ($this->_navbar === null) {
844 $this->_navbar = new navbar($this);
845 }
846 return $this->_navbar->has_items();
847 }
848
cc73ea07 849 /**
a2161d57
AG
850 * Switches from the regular requirements manager to the fragment requirements manager to
851 * capture all necessary JavaScript to display a chunk of HTML such as an mform. This is for use
852 * by the get_fragment() web service and not for use elsewhere.
cc73ea07 853 */
a2161d57
AG
854 public function start_collecting_javascript_requirements() {
855 global $CFG;
856 require_once($CFG->libdir.'/outputfragmentrequirementslib.php');
857
858 // Check that the requirements manager has not already been switched.
859 if (get_class($this->_requires) == 'fragment_requirements_manager') {
860 throw new coding_exception('JavaScript collection has already been started.');
861 }
862 // The header needs to have been called to flush out the generic JavaScript for the page. This allows only
863 // JavaScript for the fragment to be collected. _wherethemewasinitialised is set when header() is called.
864 if (!empty($this->_wherethemewasinitialised)) {
865 // Change the current requirements manager over to the fragment manager to capture JS.
866 $this->_requires = new fragment_requirements_manager();
867 } else {
868 throw new coding_exception('$OUTPUT->header() needs to be called before collecting JavaScript requirements.');
869 }
cc73ea07
AG
870 }
871
830dd6e9 872 /**
0a0034f8 873 * Should the current user see this page in editing mode.
830dd6e9 874 * That is, are they allowed to edit this page, and are they currently in
875 * editing mode.
5edb0fb8 876 * @return bool
830dd6e9 877 */
878 public function user_is_editing() {
879 global $USER;
880 return !empty($USER->editing) && $this->user_allowed_editing();
881 }
882
bb46a4fa 883 /**
0a0034f8 884 * Does the user have permission to edit blocks on this page.
5edb0fb8 885 * @return bool
bb46a4fa 886 */
887 public function user_can_edit_blocks() {
888 return has_capability($this->_blockseditingcap, $this->_context);
889 }
890
830dd6e9 891 /**
0a0034f8 892 * Does the user have permission to see this page in editing mode.
5edb0fb8 893 * @return bool
830dd6e9 894 */
895 public function user_allowed_editing() {
eb5bdb35 896 return has_any_capability($this->all_editing_caps(), $this->_context);
830dd6e9 897 }
898
2c0901cb 899 /**
e9d412b3 900 * Get a description of this page. Normally displayed in the footer in developer debug mode.
0a0034f8 901 * @return string
2c0901cb 902 */
903 public function debug_summary() {
a8c310c2 904 $summary = '';
78946b9b 905 $summary .= 'General type: ' . $this->pagelayout . '. ';
a8c310c2 906 if (!during_initial_install()) {
329846f1 907 $summary .= 'Context ' . $this->context->get_context_name() . ' (context id ' . $this->_context->id . '). ';
a8c310c2 908 }
2c0901cb 909 $summary .= 'Page type ' . $this->pagetype . '. ';
910 if ($this->subpage) {
e9d412b3 911 $summary .= 'Sub-page ' . $this->subpage . '. ';
2c0901cb 912 }
913 return $summary;
914 }
915
e9d412b3 916 // Setter methods =============================================================.
753debd2 917
c13a5e71 918 /**
e9d412b3 919 * Set the state.
0a0034f8 920 *
e9d412b3
SH
921 * The state must be one of that STATE_... constants, and the state is only allowed to advance one step at a time.
922 *
923 * @param int $state The new state.
924 * @throws coding_exception
c13a5e71 925 */
926 public function set_state($state) {
927 if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
928 throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
54caa598 929 $this->_state . ' and state ' . $state . ' was requested.');
c13a5e71 930 }
931
753debd2 932 if ($state == self::STATE_PRINTING_HEADER) {
08eab897 933 $this->starting_output();
c13a5e71 934 }
935
936 $this->_state = $state;
937 }
938
939 /**
940 * Set the current course. This sets both $PAGE->course and $COURSE. It also
941 * sets the right theme and locale.
942 *
943 * Normally you don't need to call this function yourself, require_login will
944 * call it for you if you pass a $course to it. You can use this function
945 * on pages that do need to call require_login().
946 *
2afe21ee 947 * Sets $PAGE->context to the course context, if it is not already set.
948 *
670b098b 949 * @param stdClass $course the course to set as the global course.
e9d412b3 950 * @throws coding_exception
c13a5e71 951 */
952 public function set_course($course) {
695705f5 953 global $COURSE, $PAGE, $CFG, $SITE;
c13a5e71 954
955 if (empty($course->id)) {
956 throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
957 }
958
b7009474 959 $this->ensure_theme_not_set();
c13a5e71 960
d7ab8879 961 if (!empty($this->_course->id) && $this->_course->id != $course->id) {
962 $this->_categories = null;
963 }
964
c13a5e71 965 $this->_course = clone($course);
b7009474 966
967 if ($this === $PAGE) {
968 $COURSE = $this->_course;
969 moodle_setlocale();
970 }
c13a5e71 971
2afe21ee 972 if (!$this->_context) {
b0c6dc1c 973 $this->set_context(context_course::instance($this->_course->id));
2afe21ee 974 }
695705f5 975
e9d412b3 976 // Notify course format that this page is set for the course.
695705f5
MG
977 if ($this->_course->id != $SITE->id) {
978 require_once($CFG->dirroot.'/course/lib.php');
27efa6bf
MG
979 $courseformat = course_get_format($this->_course);
980 $this->add_body_class('format-'. $courseformat->get_format());
981 $courseformat->page_set_course($this);
982 } else {
983 $this->add_body_class('format-site');
695705f5 984 }
c13a5e71 985 }
986
2afe21ee 987 /**
988 * Set the main context to which this page belongs.
0a0034f8 989 *
bea86e84 990 * @param context $context a context object. You normally get this with context_xxxx::instance().
2afe21ee 991 */
992 public function set_context($context) {
eb5bdb35 993 if ($context === null) {
e9d412b3 994 // Extremely ugly hack which sets context to some value in order to prevent warnings,
eb5bdb35
PS
995 // use only for core error handling!!!!
996 if (!$this->_context) {
b0c6dc1c 997 $this->_context = context_system::instance();
eb5bdb35
PS
998 }
999 return;
1000 }
e9d412b3
SH
1001 // Ideally we should set context only once.
1002 if (isset($this->_context) && $context->id !== $this->_context->id) {
1003 $current = $this->_context->contextlevel;
1004 if ($current == CONTEXT_SYSTEM or $current == CONTEXT_COURSE) {
1005 // Hmm - not ideal, but it might produce too many warnings due to the design of require_login.
1006 } else if ($current == CONTEXT_MODULE and ($parentcontext = $context->get_parent_context()) and
1007 $this->_context->id == $parentcontext->id) {
1008 // Hmm - most probably somebody did require_login() and after that set the block context.
eb5bdb35 1009 } else {
e9d412b3
SH
1010 // We do not want devs to do weird switching of context levels on the fly because we might have used
1011 // the context already such as in text filter in page title.
56fa860e 1012 debugging("Coding problem: unsupported modification of PAGE->context from {$current} to {$context->contextlevel}");
eb5bdb35
PS
1013 }
1014 }
1015
2afe21ee 1016 $this->_context = $context;
1017 }
1018
5ec434a9 1019 /**
1020 * The course module that this page belongs to (if it does belong to one).
1021 *
4478743c 1022 * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
6a630a10
PS
1023 * @param stdClass $course
1024 * @param stdClass $module
1025 * @return void
e9d412b3 1026 * @throws coding_exception
5ec434a9 1027 */
1028 public function set_cm($cm, $course = null, $module = null) {
695705f5 1029 global $DB, $CFG, $SITE;
eb5bdb35 1030
4478743c 1031 if (!isset($cm->id) || !isset($cm->course)) {
e9d412b3 1032 throw new coding_exception('Invalid $cm. It has to be instance of cm_info or record from the course_modules table.');
5ec434a9 1033 }
4478743c 1034
5ec434a9 1035 if (!$this->_course || $this->_course->id != $cm->course) {
1036 if (!$course) {
74df2951 1037 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
5ec434a9 1038 }
1039 if ($course->id != $cm->course) {
4478743c 1040 throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
5ec434a9 1041 }
1042 $this->set_course($course);
1043 }
4478743c 1044
e9d412b3 1045 // Make sure we have a $cm from get_fast_modinfo as this contains activity access details.
4478743c
PS
1046 if (!($cm instanceof cm_info)) {
1047 $modinfo = get_fast_modinfo($this->_course);
1048 $cm = $modinfo->get_cm($cm->id);
1049 }
1050 $this->_cm = $cm;
ce8b66c6 1051
e9d412b3
SH
1052 // Unfortunately the context setting is a mess.
1053 // Let's try to work around some common block problems and show some debug messages.
ce8b66c6 1054 if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
b0c6dc1c 1055 $context = context_module::instance($cm->id);
ce8b66c6
PS
1056 $this->set_context($context);
1057 }
4478743c 1058
5ec434a9 1059 if ($module) {
1060 $this->set_activity_record($module);
1061 }
695705f5 1062
e9d412b3 1063 // Notify course format that this page is set for the course module.
695705f5
MG
1064 if ($this->_course->id != $SITE->id) {
1065 require_once($CFG->dirroot.'/course/lib.php');
1066 course_get_format($this->_course)->page_set_cm($this);
1067 }
5ec434a9 1068 }
1069
1070 /**
0a0034f8 1071 * Sets the activity record. This could be a row from the main table for a
5edb0fb8 1072 * module. For instance if the current module (cm) is a forum this should be a row
0a0034f8
SH
1073 * from the forum table.
1074 *
e9d412b3
SH
1075 * @param stdClass $module A row from the main database table for the module that this page belongs to.
1076 * @throws coding_exception
5ec434a9 1077 */
1078 public function set_activity_record($module) {
1079 if (is_null($this->_cm)) {
1080 throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
1081 }
1082 if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
e9d412b3 1083 throw new coding_exception('The activity record does not seem to correspond to the cm that has been set.');
5ec434a9 1084 }
1085 $this->_module = $module;
1086 }
1087
f230ce19 1088 /**
0a0034f8
SH
1089 * Sets the pagetype to use for this page.
1090 *
1091 * Normally you do not need to set this manually, it is automatically created
1092 * from the script name. However, on some pages this is overridden.
1093 * For example the page type for course/view.php includes the course format,
1094 * for example 'course-view-weeks'. This gets used as the id attribute on
1095 * <body> and also for determining which blocks are displayed.
1096 *
cd4ee5ea 1097 * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'.
f230ce19 1098 */
1099 public function set_pagetype($pagetype) {
1100 $this->_pagetype = $pagetype;
1101 }
1102
34a2777c 1103 /**
0a0034f8
SH
1104 * Sets the layout to use for this page.
1105 *
1106 * The page layout determines how the page will be displayed, things such as
1107 * block regions, content areas, etc are controlled by the layout.
1108 * The theme in use for the page will determine that the layout contains.
1109 *
191b267b 1110 * This properly defaults to 'base', so you only need to call this function if
78946b9b
PS
1111 * you want something different. The exact range of supported layouts is specified
1112 * in the standard theme.
0a0034f8 1113 *
670b098b
SH
1114 * For an idea of the common page layouts see
1115 * {@link http://docs.moodle.org/dev/Themes_2.0#The_different_layouts_as_of_August_17th.2C_2010}
5edb0fb8
SH
1116 * But please keep in mind that it may be (and normally is) out of date.
1117 * The only place to find an accurate up-to-date list of the page layouts
670b098b 1118 * available for your version of Moodle is {@link theme/base/config.php}
5edb0fb8 1119 *
0a0034f8 1120 * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
34a2777c 1121 */
78946b9b 1122 public function set_pagelayout($pagelayout) {
466a1713
MN
1123 global $SESSION;
1124
1125 if (!empty($SESSION->forcepagelayout)) {
1126 $this->_pagelayout = $SESSION->forcepagelayout;
1127 } else {
1128 // Uncomment this to debug theme pagelayout issues like missing blocks.
1129 // if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout)
1130 // debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
1131 $this->_pagelayout = $pagelayout;
1132 }
34a2777c 1133 }
1134
5ed70539 1135 /**
1136 * If context->id and pagetype are not enough to uniquely identify this page,
1137 * then you can set a subpage id as well. For example, the tags page sets
0a0034f8 1138 *
5ed70539 1139 * @param string $subpage an arbitrary identifier that, along with context->id
1140 * and pagetype, uniquely identifies this page.
1141 */
1142 public function set_subpage($subpage) {
08eab897 1143 if (empty($subpage)) {
1144 $this->_subpage = '';
1145 } else {
1146 $this->_subpage = $subpage;
1147 }
5ed70539 1148 }
1149
753debd2 1150 /**
0a0034f8
SH
1151 * Adds a CSS class to the body tag of the page.
1152 *
753debd2 1153 * @param string $class add this class name ot the class attribute on the body tag.
e9d412b3 1154 * @throws coding_exception
753debd2 1155 */
1156 public function add_body_class($class) {
1157 if ($this->_state > self::STATE_BEFORE_HEADER) {
1158 throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
1159 }
1160 $this->_bodyclasses[$class] = 1;
1161 }
1162
c13a5e71 1163 /**
0a0034f8
SH
1164 * Adds an array of body classes to the body tag of this page.
1165 *
89fbdca3 1166 * @param array $classes this utility method calls add_body_class for each array element.
c13a5e71 1167 */
89fbdca3 1168 public function add_body_classes($classes) {
1169 foreach ($classes as $class) {
1170 $this->add_body_class($class);
c13a5e71 1171 }
1172 }
f230ce19 1173
34a2777c 1174 /**
0a0034f8
SH
1175 * Sets the title for the page.
1176 * This is normally used within the title tag in the head of the page.
1177 *
e120c61d 1178 * @param string $title the title that should go in the <head> section of the HTML of this page.
34a2777c 1179 */
1180 public function set_title($title) {
1181 $title = format_string($title);
e97ddbd8 1182 $title = strip_tags($title);
34a2777c 1183 $title = str_replace('"', '&quot;', $title);
1184 $this->_title = $title;
1185 }
1186
1187 /**
0a0034f8
SH
1188 * Sets the heading to use for the page.
1189 * This is normally used as the main heading at the top of the content.
1190 *
e120c61d 1191 * @param string $heading the main heading that should be displayed at the top of the <body>.
34a2777c 1192 */
1193 public function set_heading($heading) {
1194 $this->_heading = format_string($heading);
1195 }
1196
e120c61d 1197 /**
670b098b 1198 * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
0a0034f8 1199 *
e120c61d 1200 * @param string $menu The menu/content to show in the heading
1201 */
1202 public function set_headingmenu($menu) {
1203 $this->_headingmenu = $menu;
1204 }
1205
948203a5 1206 /**
0a0034f8
SH
1207 * Set the course category this page belongs to manually.
1208 *
1209 * This automatically sets $PAGE->course to be the site course. You cannot
1210 * use this method if you have already set $PAGE->course - in that case,
1211 * the category must be the one that the course belongs to. This also
1212 * automatically sets the page context to the category context.
1213 *
e9d412b3
SH
1214 * @param int $categoryid The id of the category to set.
1215 * @throws coding_exception
948203a5 1216 */
1217 public function set_category_by_id($categoryid) {
e9d412b3 1218 global $SITE;
948203a5 1219 if (!is_null($this->_course)) {
e9d412b3 1220 throw new coding_exception('Course has already been set. You cannot change the category now.');
948203a5 1221 }
1222 if (is_array($this->_categories)) {
e9d412b3 1223 throw new coding_exception('Course category has already been set. You cannot to change it now.');
948203a5 1224 }
b7009474 1225 $this->ensure_theme_not_set();
948203a5 1226 $this->set_course($SITE);
1227 $this->load_category($categoryid);
b0c6dc1c 1228 $this->set_context(context_coursecat::instance($categoryid));
948203a5 1229 }
1230
82611d8d 1231 /**
1232 * Set a different path to use for the 'Moodle docs for this page' link.
0a0034f8 1233 *
82611d8d 1234 * By default, it uses the pagetype, which is normally the same as the
1235 * script name. So, for example, for mod/quiz/attempt.php, pagetype is
1236 * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
0a0034f8 1237 *
82611d8d 1238 * @param string $path the path to use at the end of the moodle docs URL.
1239 */
1240 public function set_docs_path($path) {
1241 $this->_docspath = $path;
1242 }
1243
75781f87 1244 /**
e9d412b3 1245 * You should call this method from every page to set the URL that should be used to return to this page.
0a0034f8
SH
1246 *
1247 * Used, for example, by the blocks editing UI to know where to return the
1248 * user after an action.
75781f87 1249 * For example, course/view.php does:
1250 * $id = optional_param('id', 0, PARAM_INT);
a6855934 1251 * $PAGE->set_url('/course/view.php', array('id' => $id));
0a0034f8 1252 *
c909711a 1253 * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
01ee99f4 1254 * @param array $params parameters to add to the URL
e9d412b3 1255 * @throws coding_exception
75781f87 1256 */
a6855934 1257 public function set_url($url, array $params = null) {
75781f87 1258 global $CFG;
42174e18 1259
e9d412b3
SH
1260 if (is_string($url) && strpos($url, 'http') !== 0) {
1261 if (strpos($url, '/') === 0) {
1262 // We have to use httpswwwroot here, because of loginhttps pages.
a6855934
PS
1263 $url = $CFG->httpswwwroot . $url;
1264 } else {
1265 throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
42174e18 1266 }
7dc928b5 1267 }
42174e18 1268
a6855934
PS
1269 $this->_url = new moodle_url($url, $params);
1270
eb788065 1271 $fullurl = $this->_url->out_omit_querystring();
42174e18
PS
1272 if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
1273 debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
1274 }
1275 $shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
1276
75781f87 1277 if (is_null($this->_pagetype)) {
7dc928b5 1278 $this->initialise_default_pagetype($shorturl);
75781f87 1279 }
1280 }
1281
727ae436 1282 /**
1283 * Make sure page URL does not contain the given URL parameter.
1284 *
1285 * This should not be necessary if the script has called set_url properly.
1286 * However, in some situations like the block editing actions; when the URL
1287 * has been guessed, it will contain dangerous block-related actions.
1288 * Therefore, the blocks code calls this function to clean up such parameters
1289 * before doing any redirect.
117bd748 1290 *
727ae436 1291 * @param string $param the name of the parameter to make sure is not in the
1292 * page URL.
1293 */
1294 public function ensure_param_not_in_url($param) {
727ae436 1295 $this->_url->remove_params($param);
1296 }
1297
34a2777c 1298 /**
e9d412b3
SH
1299 * Sets an alternative version of this page.
1300 *
34a2777c 1301 * There can be alternate versions of some pages (for example an RSS feed version).
e9d412b3
SH
1302 * Call this method for each alternative version available.
1303 * For each alternative version a link will be included in the <head> tag.
34a2777c 1304 *
0a0034f8
SH
1305 * @param string $title The title to give the alternate version.
1306 * @param string|moodle_url $url The URL of the alternate version.
1307 * @param string $mimetype The mime-type of the alternate version.
e9d412b3 1308 * @throws coding_exception
34a2777c 1309 */
1310 public function add_alternate_version($title, $url, $mimetype) {
1311 if ($this->_state > self::STATE_BEFORE_HEADER) {
1312 throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
1313 }
1314 $alt = new stdClass;
1315 $alt->title = $title;
9d88a01b 1316 $alt->url = $url;
34a2777c 1317 $this->_alternateversions[$mimetype] = $alt;
1318 }
1319
1320 /**
01ee99f4 1321 * Specify a form control should be focused when the page has loaded.
34a2777c 1322 *
01ee99f4 1323 * @param string $controlid the id of the HTML element to be focused.
34a2777c 1324 */
1325 public function set_focuscontrol($controlid) {
1326 $this->_focuscontrol = $controlid;
1327 }
1328
1329 /**
1330 * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
1331 *
1332 * @param string $html the HTML to display there.
1333 */
1334 public function set_button($html) {
1335 $this->_button = $html;
1336 }
1337
934524d7 1338 /**
0a0034f8
SH
1339 * Set the capability that allows users to edit blocks on this page.
1340 *
1341 * Normally the default of 'moodle/site:manageblocks' is used, but a few
1342 * pages like the My Moodle page need to use a different capability
1343 * like 'moodle/my:manageblocks'.
1344 *
934524d7 1345 * @param string $capability a capability.
1346 */
1347 public function set_blocks_editing_capability($capability) {
1348 $this->_blockseditingcap = $capability;
1349 }
1350
1351 /**
1352 * Some pages let you turn editing on for reasons other than editing blocks.
01ee99f4 1353 * If that is the case, you can pass other capabilities that let the user
934524d7 1354 * edit this page here.
0a0034f8 1355 *
934524d7 1356 * @param string|array $capability either a capability, or an array of capabilities.
1357 */
1358 public function set_other_editing_capability($capability) {
1359 if (is_array($capability)) {
1360 $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
1361 } else {
1362 $this->_othereditingcaps[] = $capability;
1363 }
1364 }
1365
34a2777c 1366 /**
0a0034f8
SH
1367 * Sets whether the browser should cache this page or not.
1368 *
e9d412b3 1369 * @param bool $cacheable can this page be cached by the user's browser.
34a2777c 1370 */
1371 public function set_cacheable($cacheable) {
1372 $this->_cacheable = $cacheable;
1373 }
1374
17a6649b 1375 /**
1376 * Sets the page to periodically refresh
1377 *
1378 * This function must be called before $OUTPUT->header has been called or
1379 * a coding exception will be thrown.
1380 *
e9d412b3
SH
1381 * @param int $delay Sets the delay before refreshing the page, if set to null refresh is cancelled.
1382 * @throws coding_exception
17a6649b 1383 */
e9d412b3 1384 public function set_periodic_refresh_delay($delay = null) {
17a6649b 1385 if ($this->_state > self::STATE_BEFORE_HEADER) {
1386 throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
1387 }
e9d412b3 1388 if ($delay === null) {
17a6649b 1389 $this->_periodicrefreshdelay = null;
1390 } else if (is_int($delay)) {
1391 $this->_periodicrefreshdelay = $delay;
1392 }
1393 }
1394
b7009474 1395 /**
1396 * Force this page to use a particular theme.
1397 *
0a0034f8
SH
1398 * Please use this cautiously.
1399 * It is only intended to be used by the themes selector admin page.
b7009474 1400 *
0a0034f8 1401 * @param string $themename the name of the theme to use.
b7009474 1402 */
1403 public function force_theme($themename) {
b7009474 1404 $this->ensure_theme_not_set();
1405 $this->_theme = theme_config::load($themename);
b7009474 1406 }
1407
fe7b75f8
PS
1408 /**
1409 * Reload theme settings.
1410 *
1411 * This is used when we need to reset settings
1412 * because they are now double cached in theme.
1413 */
1414 public function reload_theme() {
1415 if (!is_null($this->_theme)) {
1416 $this->_theme = theme_config::load($this->_theme->name);
1417 }
1418 }
1419
b7009474 1420 /**
e9d412b3 1421 * This function indicates that current page requires the https when $CFG->loginhttps enabled.
b7009474 1422 *
1423 * By using this function properly, we can ensure 100% https-ized pages
1424 * at our entire discretion (login, forgot_password, change_password)
e9d412b3 1425 *
17c70aa0 1426 * @return void
e9d412b3 1427 * @throws coding_exception
b7009474 1428 */
1429 public function https_required() {
17c70aa0
PS
1430 global $CFG;
1431
1432 if (!is_null($this->_url)) {
1433 throw new coding_exception('https_required() must be used before setting page url!');
1434 }
b7009474 1435
1436 $this->ensure_theme_not_set();
1437
17c70aa0
PS
1438 $this->_https_login_required = true;
1439
b7009474 1440 if (!empty($CFG->loginhttps)) {
b7009474 1441 $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
b7009474 1442 } else {
1443 $CFG->httpswwwroot = $CFG->wwwroot;
b7009474 1444 }
1445 }
1446
17c70aa0 1447 /**
e9d412b3 1448 * Makes sure that page previously marked with https_required() is really using https://, if not it redirects to https://
17c70aa0
PS
1449 *
1450 * @return void (may redirect to https://self)
e9d412b3 1451 * @throws coding_exception
17c70aa0
PS
1452 */
1453 public function verify_https_required() {
1454 global $CFG, $FULLME;
1455
1456 if (is_null($this->_url)) {
1457 throw new coding_exception('verify_https_required() must be called after setting page url!');
1458 }
1459
1460 if (!$this->_https_login_required) {
1461 throw new coding_exception('verify_https_required() must be called only after https_required()!');
1462 }
1463
1464 if (empty($CFG->loginhttps)) {
e9d412b3 1465 // Https not required, so stop checking.
17c70aa0
PS
1466 return;
1467 }
1468
1469 if (strpos($this->_url, 'https://')) {
e9d412b3
SH
1470 // Detect if incorrect PAGE->set_url() used, it is recommended to use root-relative paths there.
1471 throw new coding_exception('Invalid page url. It must start with https:// for pages that set https_required()!');
17c70aa0
PS
1472 }
1473
1474 if (!empty($CFG->sslproxy)) {
e9d412b3 1475 // It does not make much sense to use sslproxy and loginhttps at the same time.
17c70aa0
PS
1476 return;
1477 }
1478
e9d412b3 1479 // Now the real test and redirect!
33dca156 1480 // NOTE: do NOT use this test for detection of https on current page because this code is not compatible with SSL proxies,
140f46a2 1481 // instead use is_https().
17c70aa0 1482 if (strpos($FULLME, 'https:') !== 0) {
e9d412b3
SH
1483 // This may lead to infinite redirect on an incorrectly configured site.
1484 // In that case set $CFG->loginhttps=0; within /config.php.
17c70aa0
PS
1485 redirect($this->_url);
1486 }
1487 }
1488
0a0034f8
SH
1489 // Initialisation methods =====================================================
1490 // These set various things up in a default way.
753debd2 1491
08eab897 1492 /**
1493 * This method is called when the page first moves out of the STATE_BEFORE_HEADER
1494 * state. This is our last change to initialise things.
1495 */
1496 protected function starting_output() {
144390b4 1497 global $CFG;
9d1d606e 1498
94398fb3 1499 if (!during_initial_install()) {
1500 $this->blocks->load_blocks();
a19f419d 1501 if (empty($this->_block_actions_done)) {
2a3b0763 1502 $this->_block_actions_done = true;
a19f419d 1503 if ($this->blocks->process_url_actions($this)) {
b9bc2019 1504 redirect($this->url->out(false));
a19f419d 1505 }
21d33bdf 1506 }
4578a5eb 1507 $this->blocks->create_all_block_instances();
08eab897 1508 }
1509
144390b4 1510 // If maintenance mode is on, change the page header.
1511 if (!empty($CFG->maintenance_enabled)) {
1512 $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
1513 '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
1514 '</a> ' . $this->button);
21d33bdf 1515
144390b4 1516 $title = $this->title;
1517 if ($title) {
1518 $title .= ' - ';
1519 }
1520 $this->set_title($title . get_string('maintenancemode', 'admin'));
1521 }
21d33bdf 1522
21d33bdf 1523 $this->initialise_standard_body_classes();
b7009474 1524 }
1525
1526 /**
1527 * Method for use by Moodle core to set up the theme. Do not
1528 * use this in your own code.
1529 *
1530 * Make sure the right theme for this page is loaded. Tell our
1531 * blocks_manager about the theme block regions, and then, if
cbcc9852 1532 * we are $PAGE, set up the global $OUTPUT.
0a0034f8
SH
1533 *
1534 * @return void
b7009474 1535 */
1536 public function initialise_theme_and_output() {
84192d78 1537 global $OUTPUT, $PAGE, $SITE, $CFG;
b7009474 1538
d4a03c00 1539 if (!empty($this->_wherethemewasinitialised)) {
1540 return;
1541 }
1542
eb5bdb35 1543 if (!during_initial_install()) {
e9d412b3 1544 // Detect PAGE->context mess.
eb5bdb35
PS
1545 $this->magic_get_context();
1546 }
1547
94398fb3 1548 if (!$this->_course && !during_initial_install()) {
b7009474 1549 $this->set_course($SITE);
1550 }
1551
1552 if (is_null($this->_theme)) {
1553 $themename = $this->resolve_theme();
1554 $this->_theme = theme_config::load($themename);
1555 }
1556
78946b9b 1557 $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
84192d78
SH
1558 if ($this->_theme->enable_dock && !empty($CFG->allowblockstodock)) {
1559 $this->requires->strings_for_js(array('addtodock', 'undockitem', 'dockblock', 'undockblock', 'undockall', 'hidedockpanel', 'hidepanel'), 'block');
1560 $this->requires->string_for_js('thisdirectionvertical', 'langconfig');
83fa6ab6 1561 $this->requires->yui_module('moodle-core-dock-loader', 'M.core.dock.loader.initLoader');
84192d78 1562 }
b7009474 1563
1564 if ($this === $PAGE) {
166ac0a3
SH
1565 $target = null;
1566 if ($this->pagelayout === 'maintenance') {
1567 // If the page is using the maintenance layout then we're going to force target to maintenance.
1568 // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
1569 // page layout.
1570 $target = RENDERER_TARGET_MAINTENANCE;
1571 }
1572 $OUTPUT = $this->get_renderer('core', null, $target);
b7009474 1573 }
1574
1575 $this->_wherethemewasinitialised = debug_backtrace();
1576 }
1577
56fa860e
DW
1578 /**
1579 * Reset the theme and output for a new context. This only makes sense from
1580 * external::validate_context(). Do not cheat.
1581 *
1582 * @return string the name of the theme that should be used on this page.
1583 */
1584 public function reset_theme_and_output() {
1585 global $COURSE, $SITE;
1586
1587 $COURSE = clone($SITE);
1588 $this->_theme = null;
1589 $this->_wherethemewasinitialised = null;
1590 $this->_course = null;
e02e7f5a
DW
1591 $this->_cm = null;
1592 $this->_module = null;
56fa860e
DW
1593 $this->_context = null;
1594 }
1595
b7009474 1596 /**
1597 * Work out the theme this page should use.
1598 *
1599 * This depends on numerous $CFG settings, and the properties of this page.
1600 *
1601 * @return string the name of the theme that should be used on this page.
1602 */
1603 protected function resolve_theme() {
1604 global $CFG, $USER, $SESSION;
1605
1606 if (empty($CFG->themeorder)) {
1607 $themeorder = array('course', 'category', 'session', 'user', 'site');
1608 } else {
1609 $themeorder = $CFG->themeorder;
1610 // Just in case, make sure we always use the site theme if nothing else matched.
1611 $themeorder[] = 'site';
1612 }
1613
1614 $mnetpeertheme = '';
1615 if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id) {
1616 require_once($CFG->dirroot.'/mnet/peer.php');
1617 $mnetpeer = new mnet_peer();
1618 $mnetpeer->set_id($USER->mnethostid);
1619 if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
1620 $mnetpeertheme = $mnetpeer->theme;
1621 }
1622 }
1623
e32af34f
FM
1624 $devicetheme = core_useragent::get_device_type_theme($this->devicetypeinuse);
1625
1626 // The user is using another device than default, and we have a theme for that, we should use it.
1627 $hascustomdevicetheme = core_useragent::DEVICETYPE_DEFAULT != $this->devicetypeinuse && !empty($devicetheme);
1628
b7009474 1629 foreach ($themeorder as $themetype) {
e32af34f 1630
b7009474 1631 switch ($themetype) {
1632 case 'course':
e32af34f 1633 if (!empty($CFG->allowcoursethemes) && !empty($this->_course->theme) && !$hascustomdevicetheme) {
37959dd4 1634 return $this->_course->theme;
b7009474 1635 }
c6e8b3b2 1636 break;
b7009474 1637
1638 case 'category':
e32af34f 1639 if (!empty($CFG->allowcategorythemes) && !$hascustomdevicetheme) {
b7009474 1640 $categories = $this->categories;
1641 foreach ($categories as $category) {
1642 if (!empty($category->theme)) {
1643 return $category->theme;
1644 }
1645 }
1646 }
c6e8b3b2 1647 break;
b7009474 1648
1649 case 'session':
1650 if (!empty($SESSION->theme)) {
1651 return $SESSION->theme;
1652 }
c6e8b3b2 1653 break;
b7009474 1654
1655 case 'user':
e32af34f 1656 if (!empty($CFG->allowuserthemes) && !empty($USER->theme) && !$hascustomdevicetheme) {
b7009474 1657 if ($mnetpeertheme) {
1658 return $mnetpeertheme;
1659 } else {
1660 return $USER->theme;
1661 }
1662 }
c6e8b3b2 1663 break;
b7009474 1664
1665 case 'site':
1666 if ($mnetpeertheme) {
1667 return $mnetpeertheme;
b7009474 1668 }
e5824bb9 1669 // First try for the device the user is using.
e5824bb9
SH
1670 if (!empty($devicetheme)) {
1671 return $devicetheme;
1672 }
e9d412b3 1673 // Next try for the default device (as a fallback).
e32af34f 1674 $devicetheme = core_useragent::get_device_type_theme(core_useragent::DEVICETYPE_DEFAULT);
e5824bb9
SH
1675 if (!empty($devicetheme)) {
1676 return $devicetheme;
1677 }
1678 // The default device theme isn't set up - use the overall default theme.
1679 return theme_config::DEFAULT_THEME;
b7009474 1680 }
1681 }
e9d412b3
SH
1682
1683 // We should most certainly have resolved a theme by now. Something has gone wrong.
1684 debugging('Error resolving the theme to use for this page.', DEBUG_DEVELOPER);
1685 return theme_config::DEFAULT_THEME;
08eab897 1686 }
1687
78bfb562 1688
d529807a 1689 /**
1690 * Sets ->pagetype from the script name. For example, if the script that was
1691 * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
0a0034f8 1692 *
d529807a 1693 * @param string $script the path to the script that should be used to
1694 * initialise ->pagetype. If not passed the $SCRIPT global will be used.
1695 * If legacy code has set $CFG->pagepath that will be used instead, and a
1696 * developer warning issued.
1697 */
ad52c04f 1698 protected function initialise_default_pagetype($script = null) {
d529807a 1699 global $CFG, $SCRIPT;
1700
1701 if (isset($CFG->pagepath)) {
1702 debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
1703 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
1704 $script = $CFG->pagepath;
1705 unset($CFG->pagepath);
1706 }
1707
ad52c04f 1708 if (is_null($script)) {
d529807a 1709 $script = ltrim($SCRIPT, '/');
1710 $len = strlen($CFG->admin);
1711 if (substr($script, 0, $len) == $CFG->admin) {
1712 $script = 'admin' . substr($script, $len);
1713 }
1714 }
1715
1716 $path = str_replace('.php', '', $script);
1717 if (substr($path, -1) == '/') {
1718 $path .= 'index';
1719 }
1720
1721 if (empty($path) || $path == 'index') {
1722 $this->_pagetype = 'site-index';
1723 } else {
1724 $this->_pagetype = str_replace('/', '-', $path);
1725 }
1726 }
1727
0a0034f8
SH
1728 /**
1729 * Initialises the CSS classes that will be added to body tag of the page.
1730 *
1731 * The function is responsible for adding all of the critical CSS classes
1732 * that describe the current page, and its state.
1733 * This includes classes that describe the following for example:
1734 * - Current language
1735 * - Language direction
1736 * - YUI CSS initialisation
1737 * - Pagelayout
1738 * These are commonly used in CSS to target specific types of pages.
1739 */
753debd2 1740 protected function initialise_standard_body_classes() {
b7009474 1741 global $CFG, $USER;
89fbdca3 1742
753debd2 1743 $pagetype = $this->pagetype;
1744 if ($pagetype == 'site-index') {
1745 $this->_legacyclass = 'course';
1746 } else if (substr($pagetype, 0, 6) == 'admin-') {
1747 $this->_legacyclass = 'admin';
753debd2 1748 }
1749 $this->add_body_class($this->_legacyclass);
1750
90723839 1751 $pathbits = explode('-', trim($pagetype));
e9d412b3
SH
1752 for ($i = 1; $i < count($pathbits); $i++) {
1753 $this->add_body_class('path-' . join('-', array_slice($pathbits, 0, $i)));
90723839
SH
1754 }
1755
c3d2fbf9 1756 $this->add_body_classes(core_useragent::get_browser_version_classes());
e372f4c7 1757 $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
753debd2 1758 $this->add_body_class('lang-' . current_language());
cf615522 1759 $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
59c58c36 1760 $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
89fbdca3 1761 $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
1762
e9d412b3
SH
1763 // Extra class describing current page layout.
1764 $this->add_body_class('pagelayout-' . $this->_pagelayout);
191b267b 1765
94398fb3 1766 if (!during_initial_install()) {
1767 $this->add_body_class('course-' . $this->_course->id);
eb5bdb35 1768 $this->add_body_class('context-' . $this->_context->id);
a1326170 1769 }
1770
b80856bd 1771 if (!empty($this->_cm)) {
1772 $this->add_body_class('cmid-' . $this->_cm->id);
1773 }
1774
94398fb3 1775 if (!empty($CFG->allowcategorythemes)) {
d7ab8879 1776 $this->ensure_category_loaded();
1777 foreach ($this->_categories as $catid => $notused) {
1778 $this->add_body_class('category-' . $catid);
1779 }
1780 } else {
1781 $catid = 0;
1782 if (is_array($this->_categories)) {
1783 $catids = array_keys($this->_categories);
1784 $catid = reset($catids);
1785 } else if (!empty($this->_course->category)) {
1786 $catid = $this->_course->category;
1787 }
1788 if ($catid) {
1789 $this->add_body_class('category-' . $catid);
1790 }
1791 }
1792
753debd2 1793 if (!isloggedin()) {
1794 $this->add_body_class('notloggedin');
1795 }
1796
496929a2 1797 if ($this->user_is_editing()) {
753debd2 1798 $this->add_body_class('editing');
39726f35 1799 if (optional_param('bui_moveid', false, PARAM_INT)) {
e9d412b3
SH
1800 $this->add_body_class('blocks-moving');
1801 }
753debd2 1802 }
1803
1804 if (!empty($CFG->blocksdrag)) {
1805 $this->add_body_class('drag');
1806 }
ee8df661 1807
37959dd4
AF
1808 if ($this->_devicetypeinuse != 'default') {
1809 $this->add_body_class($this->_devicetypeinuse . 'theme');
ee8df661 1810 }
66fe88db
RT
1811
1812 // Add class for behat site to apply behat related fixes.
1813 if (defined('BEHAT_SITE_RUNNING')) {
1814 $this->add_body_class('behat-site');
1815 }
753debd2 1816 }
1817
0a0034f8
SH
1818 /**
1819 * Loads the activity record for the current CM object associated with this
1820 * page.
1821 *
670b098b 1822 * This will load {@link moodle_page::$_module} with a row from the related
0a0034f8 1823 * module table in the database.
670b098b 1824 * For instance if {@link moodle_page::$_cm} is a forum then a row from the
0a0034f8
SH
1825 * forum table will be loaded.
1826 */
5ec434a9 1827 protected function load_activity_record() {
1828 global $DB;
1829 if (is_null($this->_cm)) {
1830 return;
1831 }
1832 $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
1833 }
1834
0a0034f8 1835 /**
5edb0fb8
SH
1836 * This function ensures that the category of the current course has been
1837 * loaded, and if not, the function loads it now.
0a0034f8
SH
1838 *
1839 * @return void
1840 * @throws coding_exception
1841 */
948203a5 1842 protected function ensure_category_loaded() {
1843 if (is_array($this->_categories)) {
1844 return; // Already done.
1845 }
1846 if (is_null($this->_course)) {
1847 throw new coding_exception('Attempt to get the course category for this page before the course was set.');
1848 }
1849 if ($this->_course->category == 0) {
1850 $this->_categories = array();
1851 } else {
1852 $this->load_category($this->_course->category);
1853 }
1854 }
1855
0a0034f8
SH
1856 /**
1857 * Loads the requested category into the pages categories array.
1858 *
e9d412b3 1859 * @param int $categoryid
0a0034f8
SH
1860 * @throws moodle_exception
1861 */
948203a5 1862 protected function load_category($categoryid) {
1863 global $DB;
1864 $category = $DB->get_record('course_categories', array('id' => $categoryid));
1865 if (!$category) {
1866 throw new moodle_exception('unknowncategory');
1867 }
1868 $this->_categories[$category->id] = $category;
1869 $parentcategoryids = explode('/', trim($category->path, '/'));
1870 array_pop($parentcategoryids);
1871 foreach (array_reverse($parentcategoryids) as $catid) {
1872 $this->_categories[$catid] = null;
1873 }
1874 }
1875
0a0034f8 1876 /**
5edb0fb8
SH
1877 * Ensures that the category the current course is within, as well as all of
1878 * its parent categories, have been loaded.
0a0034f8
SH
1879 *
1880 * @return void
1881 */
948203a5 1882 protected function ensure_categories_loaded() {
1883 global $DB;
1884 $this->ensure_category_loaded();
1885 if (!is_null(end($this->_categories))) {
1886 return; // Already done.
1887 }
1888 $idstoload = array_keys($this->_categories);
1889 array_shift($idstoload);
1890 $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
1891 foreach ($idstoload as $catid) {
1892 $this->_categories[$catid] = $categories[$catid];
1893 }
1894 }
1895
0a0034f8
SH
1896 /**
1897 * Ensure the theme has not been loaded yet. If it has an exception is thrown.
0a0034f8
SH
1898 *
1899 * @throws coding_exception
1900 */
b7009474 1901 protected function ensure_theme_not_set() {
b3e1e2d0
JL
1902 // This is explicitly allowed for webservices though which may process many course contexts in a single request.
1903 if (WS_SERVER) {
1904 return;
1905 }
1906
b7009474 1907 if (!is_null($this->_theme)) {
1908 throw new coding_exception('The theme has already been set up for this page ready for output. ' .
1909 'Therefore, you can no longer change the theme, or anything that might affect what ' .
1910 'the current theme is, for example, the course.',
1911 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
1912 }
1913 }
1914
0a0034f8
SH
1915 /**
1916 * Converts the provided URL into a CSS class that be used within the page.
1917 * This is primarily used to add the wwwroot to the body tag as a CSS class.
1918 *
1919 * @param string $url
1920 * @return string
1921 */
89fbdca3 1922 protected function url_to_class_name($url) {
1923 $bits = parse_url($url);
1924 $class = str_replace('.', '-', $bits['host']);
1925 if (!empty($bits['port'])) {
1926 $class .= '--' . $bits['port'];
1927 }
1928 if (!empty($bits['path'])) {
1929 $path = trim($bits['path'], '/');
1930 if ($path) {
1931 $class .= '--' . str_replace('/', '-', $path);
1932 }
1933 }
1934 return $class;
1935 }
1936
0a0034f8
SH
1937 /**
1938 * Combines all of the required editing caps for the page and returns them
1939 * as an array.
1940 *
1941 * @return array
1942 */
934524d7 1943 protected function all_editing_caps() {
1944 $caps = $this->_othereditingcaps;
1945 $caps[] = $this->_blockseditingcap;
1946 return $caps;
1947 }
1948
0a0034f8
SH
1949 /**
1950 * Returns true if the page URL has beem set.
1951 *
1952 * @return bool
1953 */
bf6c37c7 1954 public function has_set_url() {
1955 return ($this->_url!==null);
1956 }
05c92729 1957
0a0034f8
SH
1958 /**
1959 * Gets set when the block actions for the page have been processed.
1960 *
1961 * @param bool $setting
1962 */
05c92729
SH
1963 public function set_block_actions_done($setting = true) {
1964 $this->_block_actions_done = $setting;
1965 }
e7f93d5c
AD
1966
1967 /**
1968 * Are popup notifications allowed on this page?
1969 * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
0a0034f8 1970 *
5edb0fb8 1971 * @return bool true if popup notifications may be displayed
e7f93d5c
AD
1972 */
1973 public function get_popup_notification_allowed() {
1974 return $this->_popup_notification_allowed;
1975 }
1976
1977 /**
1978 * Allow or disallow popup notifications on this page. Popups are allowed by default.
0a0034f8 1979 *
47e59978 1980 * @param bool $allowed true if notifications are allowed. False if not allowed. They are allowed by default.
e7f93d5c
AD
1981 */
1982 public function set_popup_notification_allowed($allowed) {
1983 $this->_popup_notification_allowed = $allowed;
1984 }
36b77e3a
SH
1985
1986 /**
1987 * Returns the block region having made any required theme manipulations.
1988 *
5bcfd504 1989 * @since Moodle 2.5.1 2.6
36b77e3a
SH
1990 * @param string $region
1991 * @return string
1992 */
1993 public function apply_theme_region_manipulations($region) {
1994 if ($this->blockmanipulations && isset($this->blockmanipulations[$region])) {
2e3fb23f
GB
1995 $regionwas = $region;
1996 $regionnow = $this->blockmanipulations[$region];
1997 if ($this->blocks->is_known_region($regionwas) && $this->blocks->is_known_region($regionnow)) {
1998 // Both the before and after regions are known so we can swap them over.
1999 return $regionnow;
2000 }
2001 // We didn't know about both, we won't swap them over.
2002 return $regionwas;
36b77e3a
SH
2003 }
2004 return $region;
2005 }
12261113
AG
2006
2007 /**
2008 * Add a report node and a specific report to the navigation.
2009 *
2010 * @param int $userid The user ID that we are looking to add this report node to.
2011 * @param array $nodeinfo Name and url of the final node that we are creating.
2012 */
2013 public function add_report_nodes($userid, $nodeinfo) {
2014 global $USER;
2015 // Try to find the specific user node.
2016 $newusernode = $this->navigation->find('user' . $userid, null);
2017 $reportnode = null;
2018 $navigationnodeerror =
2019 'Could not find the navigation node requested. Please check that the node you are looking for exists.';
2020 if ($userid != $USER->id) {
2021 // Check that we have a valid node.
2022 if (empty($newusernode)) {
2023 // Throw an error if we ever reach here.
2024 throw new coding_exception($navigationnodeerror);
2025 }
2026 // Add 'Reports' to the user node.
2027 $reportnode = $newusernode->add(get_string('reports'));
2028 } else {
2029 // We are looking at our own profile.
2030 $myprofilenode = $this->settingsnav->find('myprofile', null);
2031 // Check that we do end up with a valid node.
2032 if (empty($myprofilenode)) {
2033 // Throw an error if we ever reach here.
2034 throw new coding_exception($navigationnodeerror);
2035 }
2036 // Add 'Reports' to our node.
2037 $reportnode = $myprofilenode->add(get_string('reports'));
2038 }
2039 // Finally add the report to the navigation tree.
2040 $reportnode->add($nodeinfo['name'], $nodeinfo['url'], navigation_node::TYPE_COURSE);
2041 }
ad52c04f 2042}