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