improved PHP docs
[moodle.git] / lib / pagelib.php
CommitLineData
50fcb1d8 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
f032aa7a 17
e0134f84 18/**
de60de04 19 * This file contains the moodle_page class. There is normally a single instance
78bfb562 20 * of this class in the $PAGE global variable. This class is a central repository
de60de04 21 * of information about the page we are building up to send back to the user.
e0134f84 22 *
78bfb562
PS
23 * @package core
24 * @subpackage lib
25 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e0134f84 27 */
28
78bfb562 29defined('MOODLE_INTERNAL') || die();
2c144fc3 30
c13a5e71 31/**
32 * $PAGE is a central store of information about the current page we are
5c5418fe 33 * generating in response to the user's request.
34 *
35 * It does not do very much itself
c13a5e71 36 * except keep track of information, however, it serves as the access point to
37 * some more significant components like $PAGE->theme, $PAGE->requires,
38 * $PAGE->blocks, etc.
50fcb1d8 39 *
5c5418fe 40 * @copyright 2009 Tim Hunt
50fcb1d8 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 42 * @since Moodle 2.0
2c144fc3 43 *
4ac8345f
SH
44 * @property-read string $activityname The type of activity we are in, for example 'forum' or 'quiz'.
45 * Will be null if this page is not within a module.
46 * @property-read object $activityrecord The row from the activities own database table (for example
47 * the forum or quiz table) that this page belongs to. Will be null
48 * if this page is not within a module.
49 * @property-read array $alternativeversions Mime type => object with ->url and ->title.
50 * @property-read blocks_manager $blocks The blocks manager object for this page.
51 * @property-read string $bodyclasses Returns a string to use within the class attribute on the body tag.
01ee99f4 52 * @property-read string $button The HTML to go where the Turn editing on button normally goes.
4ac8345f
SH
53 * @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
54 * @property-read array $categories An array of all the categories the page course belongs to,
55 * starting with the immediately containing category, and working out to
56 * the top-level category. This may be the empty array if we are in the
57 * front page course.
58 * @property-read mixed $category The category that the page course belongs to. If there isn't one returns null.
59 * @property-read object $cm The course_module that this page belongs to. Will be null
60 * if this page is not within a module. This is a full cm object, as loaded
61 * by get_coursemodule_from_id or get_coursemodule_from_instance,
62 * so the extra modname and name fields are present.
63 * @property-read object $context The main context to which this page belongs.
64 * @property-read object $course The current course that we are inside - a row from the
65 * course table. (Also available as $COURSE global.) If we are not inside
66 * an actual course, this will be the site course.
67 * @property-read string $docspath The path to the Moodle docs for this page.
01ee99f4 68 * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
4ac8345f
SH
69 * @property-read bool $headerprinted
70 * @property-read string $heading The main heading that should be displayed at the top of the <body>.
71 * @property-read string $headingmenu The menu (or actions) to display in the heading
72 * @property-read array $layout_options Returns arrays with options for layout file.
ee8df661 73 * @property-read bool $legacythemeinuse Returns true if the legacy theme is being used.
4ac8345f
SH
74 * @property-read navbar $navbar Returns the navbar object used to display the navbar
75 * @property-read global_navigation $navigation Returns the global navigation structure
76 * @property-read xml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
77 * mainly for internal use by the rendering code.
78 * @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
34a2777c 79 * Allows the theme to display things differently, if it wishes to.
4ac8345f
SH
80 * @property-read string $pagetype Returns the page type string, should be used as the id for the body tag in the theme.
81 * @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
82 * @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
83 * @property-read settings_navigation $settignsnav
84 * @property-read int $state One of the STATE_... constants
85 * @property-read string $subpage The subpage identifier, if any.
86 * @property-read theme_config $theme Returns the initialised theme for this page.
87 * @property-read string $title The title that should go in the <head> section of the HTML of this page.
88 * @property-read moodle_url $url The moodle url object for this page.
c13a5e71 89 */
90class moodle_page {
91 /**#@+ Tracks the where we are in the generation of the page. */
92 const STATE_BEFORE_HEADER = 0;
93 const STATE_PRINTING_HEADER = 1;
94 const STATE_IN_BODY = 2;
34a2777c 95 const STATE_DONE = 3;
c13a5e71 96 /**#@-*/
97
753debd2 98/// Field declarations =========================================================
99
c13a5e71 100 protected $_state = self::STATE_BEFORE_HEADER;
101
102 protected $_course = null;
103
5ec434a9 104 /**
105 * If this page belongs to a module, this is the row from the course_modules
106 * table, as fetched by get_coursemodule_from_id or get_coursemodule_from_instance,
107 * so the extra modname and name fields are present.
108 */
109 protected $_cm = null;
110
111 /**
112 * If $_cm is not null, then this will hold the corresponding row from the
113 * modname table. For example, if $_cm->modname is 'quiz', this will be a
114 * row from the quiz table.
115 */
116 protected $_module = null;
117
118 /**
119 * The context that this page belongs to.
120 */
2afe21ee 121 protected $_context = null;
122
948203a5 123 /**
124 * This holds any categories that $_course belongs to, starting with the
125 * particular category it belongs to, and working out through any parent
01ee99f4 126 * categories to the top level. These are loaded progressively, if needed.
948203a5 127 * There are three states. $_categories = null initially when nothing is
128 * loaded; $_categories = array($id => $cat, $parentid => null) when we have
129 * loaded $_course->category, but not any parents; and a complete array once
130 * everything is loaded.
131 */
132 protected $_categories = null;
133
753debd2 134 protected $_bodyclasses = array();
135
34a2777c 136 protected $_title = '';
137
138 protected $_heading = '';
139
f230ce19 140 protected $_pagetype = null;
141
191b267b 142 protected $_pagelayout = 'base';
78946b9b
PS
143
144 /**
01ee99f4 145 * List of theme layout options, these are ignored by core.
78946b9b
PS
146 * To be used in individual theme layout files only.
147 * @var array
148 */
149 protected $_layout_options = array();
34a2777c 150
08eab897 151 protected $_subpage = '';
5ed70539 152
82611d8d 153 protected $_docspath = null;
154
90723839 155 /** @var string|null A legacy class that will be added to the body tag */
d529807a 156 protected $_legacyclass = null;
157
75781f87 158 protected $_url = null;
159
34a2777c 160 protected $_alternateversions = array();
161
ad52c04f 162 protected $_blocks = null;
163
b2330db6 164 protected $_requires = null;
165
934524d7 166 protected $_blockseditingcap = 'moodle/site:manageblocks';
167
7db8dd1e
PS
168 protected $_block_actions_done = false;
169
934524d7 170 protected $_othereditingcaps = array();
171
34a2777c 172 protected $_cacheable = true;
173
174 protected $_focuscontrol = '';
175
176 protected $_button = '';
571fa828 177
b7009474 178 protected $_theme = null;
3406acde 179 /** @var global_navigation Contains the global navigation structure*/
7d2a0492 180 protected $_navigation = null;
181 /** @var null|settings_navigation Contains the settings navigation structure*/
182 protected $_settingsnav = null;
183 /** @var null|navbar Contains the navbar structure*/
184 protected $_navbar = null;
e120c61d 185 /** @var string */
186 protected $_headingmenu = null;
b7009474 187
188 /**
01ee99f4 189 * Then the theme is initialised, we save the stack trace, for use in error messages.
b7009474 190 * @var array stack trace.
191 */
192 protected $_wherethemewasinitialised = null;
193
db8d89d8 194 /** @var xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed. */
195 protected $_opencontainers;
196
17a6649b 197 /**
198 * Sets the page to refresh after a given delay (in seconds) using meta refresh
199 * in {@link standard_head_html()} in outputlib.php
200 * If set to null(default) the page is not refreshed
201 * @var int|null
202 */
203 protected $_periodicrefreshdelay = null;
204
0d5890c4 205 /**
01ee99f4 206 * This is simply to improve backwards compatibility. If old code relies on
0d5890c4 207 * a page class that implements print_header, or complex logic in
208 * user_allowed_editing then we stash an instance of that other class here,
78946b9b 209 * and delegate to it in certain situations.
0d5890c4 210 */
211 protected $_legacypageobject = null;
212
ee8df661
SH
213 /**
214 * Associative array of browser shortnames (as used by check_browser_version)
215 * and their minimum required versions
216 * @var array
217 */
218 protected $_legacybrowsers = array('MSIE' => 6.0);
219
220 /**
221 * Is set to true if the chosen legacy theme is in use. False by default.
222 * @var bool
223 */
224 protected $_legacythemeinuse = false;
78bfb562 225
649cf95d 226/// Magic getter methods =============================================================
5515b536 227/// Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
ad5d5997 228/// methods, but instead use the $PAGE->x syntax.
753debd2 229
c13a5e71 230 /**
34a2777c 231 * Please do not call this method directly, use the ->state syntax. {@link __get()}.
c13a5e71 232 * @return integer one of the STATE_... constants. You should not normally need
01ee99f4 233 * to use this in your code. It is intended for internal use by this class
c13a5e71 234 * and its friends like print_header, to check that everything is working as
235 * expected. Also accessible as $PAGE->state.
236 */
5515b536 237 protected function magic_get_state() {
c13a5e71 238 return $this->_state;
239 }
240
241 /**
34a2777c 242 * Please do not call this method directly, use the ->headerprinted syntax. {@link __get()}.
b7b2d0f3 243 * @return boolean has the header already been printed?
c13a5e71 244 */
5515b536 245 protected function magic_get_headerprinted() {
c13a5e71 246 return $this->_state >= self::STATE_IN_BODY;
247 }
248
249 /**
34a2777c 250 * Please do not call this method directly, use the ->course syntax. {@link __get()}.
50fcb1d8 251 *
252 * @global object
c13a5e71 253 * @return object the current course that we are inside - a row from the
254 * course table. (Also available as $COURSE global.) If we are not inside
b7b2d0f3 255 * an actual course, this will be the site course.
c13a5e71 256 */
5515b536 257 protected function magic_get_course() {
c13a5e71 258 global $SITE;
259 if (is_null($this->_course)) {
260 return $SITE;
261 }
262 return $this->_course;
263 }
264
5ec434a9 265 /**
34a2777c 266 * Please do not call this method directly, use the ->cm syntax. {@link __get()}.
5ec434a9 267 * @return object the course_module that this page belongs to. Will be null
268 * if this page is not within a module. This is a full cm object, as loaded
269 * by get_coursemodule_from_id or get_coursemodule_from_instance,
270 * so the extra modname and name fields are present.
271 */
5515b536 272 protected function magic_get_cm() {
5ec434a9 273 return $this->_cm;
274 }
275
276 /**
34a2777c 277 * Please do not call this method directly, use the ->activityrecord syntax. {@link __get()}.
b7b2d0f3 278 * @return object the row from the activities own database table (for example
279 * the forum or quiz table) that this page belongs to. Will be null
280 * if this page is not within a module.
5ec434a9 281 */
5515b536 282 protected function magic_get_activityrecord() {
5ec434a9 283 if (is_null($this->_module) && !is_null($this->_cm)) {
284 $this->load_activity_record();
285 }
286 return $this->_module;
287 }
288
289 /**
34a2777c 290 * Please do not call this method directly, use the ->activityname syntax. {@link __get()}.
b7b2d0f3 291 * @return string|null the The type of activity we are in, for example 'forum' or 'quiz'.
292 * Will be null if this page is not within a module.
5ec434a9 293 */
5515b536 294 protected function magic_get_activityname() {
5ec434a9 295 if (is_null($this->_cm)) {
296 return null;
297 }
298 return $this->_cm->modname;
299 }
300
948203a5 301 /**
34a2777c 302 * Please do not call this method directly, use the ->category syntax. {@link __get()}.
948203a5 303 * @return mixed the category that the page course belongs to. If there isn't one
304 * (that is, if this is the front page course) returns null.
305 */
5515b536 306 protected function magic_get_category() {
948203a5 307 $this->ensure_category_loaded();
308 if (!empty($this->_categories)) {
309 return reset($this->_categories);
310 } else {
311 return null;
312 }
313 }
314
315 /**
34a2777c 316 * Please do not call this method directly, use the ->categories syntax. {@link __get()}.
948203a5 317 * @return array an array of all the categories the page course belongs to,
318 * starting with the immediately containing category, and working out to
319 * the top-level category. This may be the empty array if we are in the
320 * front page course.
321 */
5515b536 322 protected function magic_get_categories() {
948203a5 323 $this->ensure_categories_loaded();
324 return $this->_categories;
325 }
326
2afe21ee 327 /**
34a2777c 328 * Please do not call this method directly, use the ->context syntax. {@link __get()}.
2afe21ee 329 * @return object the main context to which this page belongs.
330 */
5515b536 331 protected function magic_get_context() {
2afe21ee 332 if (is_null($this->_context)) {
eb5bdb35
PS
333 if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
334 // cli scripts work in system context, do not annoy devs with debug info
335 // very few scripts do not use cookies, we can safely use system as default context there
4ee51346 336 } else {
eb5bdb35 337 debugging('Coding problem: this page does not set $PAGE->context properly.');
4ee51346 338 }
eb5bdb35 339 $this->_context = get_context_instance(CONTEXT_SYSTEM);
2afe21ee 340 }
341 return $this->_context;
342 }
343
f230ce19 344 /**
34a2777c 345 * Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
90723839 346 * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
f230ce19 347 */
5515b536 348 protected function magic_get_pagetype() {
bd348bda 349 global $CFG;
d529807a 350 if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
351 $this->initialise_default_pagetype();
f230ce19 352 }
353 return $this->_pagetype;
354 }
355
90723839
SH
356 /**
357 * Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
358 * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
359 */
360 protected function magic_get_bodyid() {
361 return 'page-'.$this->pagetype;
362 }
363
5ed70539 364 /**
78946b9b 365 * Please do not call this method directly, use the ->pagelayout syntax. {@link __get()}.
191b267b 366 * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
34a2777c 367 * Allows the theme to display things differently, if it wishes to.
368 */
5515b536 369 protected function magic_get_pagelayout() {
78946b9b
PS
370 return $this->_pagelayout;
371 }
372
373 /**
374 * Please do not call this method directly, use the ->layout_tions syntax. {@link __get()}.
4ac8345f 375 * @return array returns arrys with options for layout file
78946b9b 376 */
5515b536 377 protected function magic_get_layout_options() {
78946b9b 378 return $this->_layout_options;
34a2777c 379 }
380
381 /**
382 * Please do not call this method directly, use the ->subpage syntax. {@link __get()}.
5ed70539 383 * @return string|null The subpage identifier, if any.
384 */
5515b536 385 protected function magic_get_subpage() {
5ed70539 386 return $this->_subpage;
387 }
388
753debd2 389 /**
34a2777c 390 * Please do not call this method directly, use the ->bodyclasses syntax. {@link __get()}.
753debd2 391 * @return string the class names to put on the body element in the HTML.
392 */
5515b536 393 protected function magic_get_bodyclasses() {
753debd2 394 return implode(' ', array_keys($this->_bodyclasses));
395 }
396
82611d8d 397 /**
34a2777c 398 * Please do not call this method directly, use the ->title syntax. {@link __get()}.
399 * @return string the title that should go in the <head> section of the HTML of this page.
400 */
5515b536 401 protected function magic_get_title() {
34a2777c 402 return $this->_title;
403 }
404
405 /**
406 * Please do not call this method directly, use the ->heading syntax. {@link __get()}.
407 * @return string the main heading that should be displayed at the top of the <body>.
408 */
5515b536 409 protected function magic_get_heading() {
34a2777c 410 return $this->_heading;
411 }
412
e120c61d 413 /**
414 * Please do not call this method directly, use the ->heading syntax. {@link __get()}.
415 * @return string The menu (or actions) to display in the heading
416 */
5515b536 417 protected function magic_get_headingmenu() {
e120c61d 418 return $this->_headingmenu;
419 }
420
34a2777c 421 /**
422 * Please do not call this method directly, use the ->docspath syntax. {@link __get()}.
b7b2d0f3 423 * @return string the path to the Moodle docs for this page.
82611d8d 424 */
5515b536 425 protected function magic_get_docspath() {
82611d8d 426 if (is_string($this->_docspath)) {
427 return $this->_docspath;
428 } else {
429 return str_replace('-', '/', $this->pagetype);
430 }
431 }
432
75781f87 433 /**
34a2777c 434 * Please do not call this method directly, use the ->url syntax. {@link __get()}.
75781f87 435 * @return moodle_url the clean URL required to load the current page. (You
436 * should normally use this in preference to $ME or $FULLME.)
437 */
5515b536 438 protected function magic_get_url() {
c1df9d7c 439 global $FULLME;
75781f87 440 if (is_null($this->_url)) {
c1df9d7c 441 debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
727ae436 442 $this->_url = new moodle_url($FULLME);
443 // Make sure the guessed URL cannot lead to dangerous redirects.
444 $this->_url->remove_params('sesskey');
75781f87 445 }
ad52c04f 446 return new moodle_url($this->_url); // Return a clone for safety.
447 }
448
449 /**
34a2777c 450 * The list of alternate versions of this page.
451 * @return array mime type => object with ->url and ->title.
452 */
5515b536 453 protected function magic_get_alternateversions() {
34a2777c 454 return $this->_alternateversions;
455 }
456
457 /**
458 * Please do not call this method directly, use the ->blocks syntax. {@link __get()}.
ad52c04f 459 * @return blocks_manager the blocks manager object for this page.
460 */
5515b536 461 protected function magic_get_blocks() {
78946b9b 462 global $CFG;
ad52c04f 463 if (is_null($this->_blocks)) {
86b5ea0f 464 if (!empty($CFG->blockmanagerclass)) {
465 $classname = $CFG->blockmanagerclass;
466 } else {
467 $classname = 'block_manager';
468 }
469 $this->_blocks = new $classname($this);
ad52c04f 470 }
471 return $this->_blocks;
75781f87 472 }
473
b2330db6 474 /**
34a2777c 475 * Please do not call this method directly, use the ->requires syntax. {@link __get()}.
571fa828 476 * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
b2330db6 477 */
5515b536 478 protected function magic_get_requires() {
b2330db6 479 global $CFG;
480 if (is_null($this->_requires)) {
481 $this->_requires = new page_requirements_manager();
b2330db6 482 }
483 return $this->_requires;
484 }
485
571fa828 486 /**
34a2777c 487 * Please do not call this method directly, use the ->cacheable syntax. {@link __get()}.
488 * @return boolean can this page be cached by the user's browser.
571fa828 489 */
5515b536 490 protected function magic_get_cacheable() {
34a2777c 491 return $this->_cacheable;
492 }
493
494 /**
495 * Please do not call this method directly, use the ->focuscontrol syntax. {@link __get()}.
01ee99f4 496 * @return string the id of the HTML element to be focused when the page has loaded.
34a2777c 497 */
5515b536 498 protected function magic_get_focuscontrol() {
34a2777c 499 return $this->_focuscontrol;
500 }
501
502 /**
503 * Please do not call this method directly, use the ->button syntax. {@link __get()}.
01ee99f4 504 * @return string the HTML to go where the Turn editing on button normally goes.
34a2777c 505 */
5515b536 506 protected function magic_get_button() {
34a2777c 507 return $this->_button;
571fa828 508 }
509
b7009474 510 /**
511 * Please do not call this method directly, use the ->theme syntax. {@link __get()}.
4ac8345f 512 * @return theme_config the initialised theme for this page.
b7009474 513 */
5515b536 514 protected function magic_get_theme() {
b7009474 515 if (is_null($this->_theme)) {
516 $this->initialise_theme_and_output();
517 }
518 return $this->_theme;
519 }
520
ee8df661
SH
521 /**
522 * Please do not call this method directly, use the ->legacythemeinuse syntax. {@link __get()}.
523 * @return bool
524 */
525 protected function magic_get_legacythemeinuse() {
526 return ($this->_legacythemeinuse);
527 }
528
17a6649b 529 /**
afa2dcad 530 * Please do not call this method directly use the ->periodicrefreshdelay syntax
531 * {@link __get()}
532 * @return int The periodic refresh delay to use with meta refresh
17a6649b 533 */
5515b536 534 protected function magic_get_periodicrefreshdelay() {
17a6649b 535 return $this->_periodicrefreshdelay;
536 }
537
db8d89d8 538 /**
539 * Please do not call this method directly use the ->opencontainers syntax. {@link __get()}
540 * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
541 * mainly for internal use by the rendering code.
542 */
5515b536 543 protected function magic_get_opencontainers() {
db8d89d8 544 if (is_null($this->_opencontainers)) {
545 $this->_opencontainers = new xhtml_container_stack();
546 }
547 return $this->_opencontainers;
548 }
549
7d2a0492 550 /**
551 * Return the navigation object
552 * @return global_navigation
553 */
5515b536 554 protected function magic_get_navigation() {
7d2a0492 555 if ($this->_navigation === null) {
3406acde 556 $this->_navigation = new global_navigation($this);
7d2a0492 557 }
558 return $this->_navigation;
559 }
560
561 /**
562 * Return a navbar object
563 * @return navbar
564 */
5515b536 565 protected function magic_get_navbar() {
7d2a0492 566 if ($this->_navbar === null) {
567 $this->_navbar = new navbar($this);
568 }
569 return $this->_navbar;
570 }
571
572 /**
573 * Returns the settings navigation object
574 * @return settings_navigation
575 */
5515b536 576 protected function magic_get_settingsnav() {
7d2a0492 577 if ($this->_settingsnav === null) {
578 $this->_settingsnav = new settings_navigation($this);
579 $this->_settingsnav->initialise();
580 }
581 return $this->_settingsnav;
582 }
583
89fbdca3 584 /**
b7b2d0f3 585 * PHP overloading magic to make the $PAGE->course syntax work by redirecting
5515b536 586 * it to the corresponding $PAGE->magic_get_course() method if there is one, and
b7b2d0f3 587 * throwing an exception if not.
7db8dd1e
PS
588 *
589 * @param $name string property name
5515b536 590 * @return mixed
89fbdca3 591 */
7db8dd1e
PS
592 public function __get($name) {
593 $getmethod = 'magic_get_' . $name;
89fbdca3 594 if (method_exists($this, $getmethod)) {
595 return $this->$getmethod();
596 } else {
7db8dd1e
PS
597 throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
598 }
599 }
600
601 /**
602 * PHP overloading magic which prevents the $PAGE->context = $context; syntax
603 *
604 * @param $name string property name
605 * @param $name mixed value
606 * @return void, throws exception if field not defined in page class
607 */
608 public function __set($name, $value) {
609 if (method_exists($this, 'set_' . $name)) {
610 throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
611 } else {
612 throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
89fbdca3 613 }
614 }
615
830dd6e9 616/// Other information getting methods ==========================================
617
649cf95d
PS
618 /**
619 * Returns instance of page renderer
620 * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
621 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 622 * @param string $target one of rendering target constants
649cf95d
PS
623 * @return renderer_base
624 */
c927e35c
PS
625 public function get_renderer($component, $subtype = null, $target = null) {
626 return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
649cf95d
PS
627 }
628
5515b536
PS
629 /**
630 * Checks to see if there are any items on the navbar object
631 * @return bool true if there are, false if not
632 */
633 public function has_navbar() {
634 if ($this->_navbar === null) {
635 $this->_navbar = new navbar($this);
636 }
637 return $this->_navbar->has_items();
638 }
639
830dd6e9 640 /**
641 * @return boolean should the current user see this page in editing mode.
642 * That is, are they allowed to edit this page, and are they currently in
643 * editing mode.
644 */
645 public function user_is_editing() {
646 global $USER;
647 return !empty($USER->editing) && $this->user_allowed_editing();
648 }
649
bb46a4fa 650 /**
651 * @return boolean does the user have permission to edit blocks on this page.
652 */
653 public function user_can_edit_blocks() {
654 return has_capability($this->_blockseditingcap, $this->_context);
655 }
656
830dd6e9 657 /**
658 * @return boolean does the user have permission to see this page in editing mode.
659 */
660 public function user_allowed_editing() {
0d5890c4 661 if ($this->_legacypageobject) {
662 return $this->_legacypageobject->user_allowed_editing();
663 }
eb5bdb35 664 return has_any_capability($this->all_editing_caps(), $this->_context);
830dd6e9 665 }
666
2c0901cb 667 /**
a8c310c2 668 * @return string a description of this page. Normally displayed in the footer in
669 * developer debug mode.
2c0901cb 670 */
671 public function debug_summary() {
a8c310c2 672 $summary = '';
78946b9b 673 $summary .= 'General type: ' . $this->pagelayout . '. ';
a8c310c2 674 if (!during_initial_install()) {
eb5bdb35 675 $summary .= 'Context ' . print_context_name($this->_context) . ' (context id ' . $this->_context->id . '). ';
a8c310c2 676 }
2c0901cb 677 $summary .= 'Page type ' . $this->pagetype . '. ';
678 if ($this->subpage) {
679 'Sub-page ' . $this->subpage . '. ';
680 }
681 return $summary;
682 }
683
753debd2 684/// Setter methods =============================================================
685
c13a5e71 686 /**
687 * Set the state. The state must be one of that STATE_... constants, and
688 * the state is only allowed to advance one step at a time.
689 * @param integer $state the new state.
690 */
691 public function set_state($state) {
692 if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
693 throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
54caa598 694 $this->_state . ' and state ' . $state . ' was requested.');
c13a5e71 695 }
696
753debd2 697 if ($state == self::STATE_PRINTING_HEADER) {
08eab897 698 $this->starting_output();
c13a5e71 699 }
700
701 $this->_state = $state;
702 }
703
704 /**
705 * Set the current course. This sets both $PAGE->course and $COURSE. It also
706 * sets the right theme and locale.
707 *
708 * Normally you don't need to call this function yourself, require_login will
709 * call it for you if you pass a $course to it. You can use this function
710 * on pages that do need to call require_login().
711 *
2afe21ee 712 * Sets $PAGE->context to the course context, if it is not already set.
713 *
c13a5e71 714 * @param object the course to set as the global course.
715 */
716 public function set_course($course) {
b7009474 717 global $COURSE, $PAGE;
c13a5e71 718
719 if (empty($course->id)) {
720 throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
721 }
722
b7009474 723 $this->ensure_theme_not_set();
c13a5e71 724
d7ab8879 725 if (!empty($this->_course->id) && $this->_course->id != $course->id) {
726 $this->_categories = null;
727 }
728
c13a5e71 729 $this->_course = clone($course);
b7009474 730
731 if ($this === $PAGE) {
732 $COURSE = $this->_course;
733 moodle_setlocale();
734 }
c13a5e71 735
2afe21ee 736 if (!$this->_context) {
737 $this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id));
738 }
c13a5e71 739 }
740
2afe21ee 741 /**
742 * Set the main context to which this page belongs.
743 * @param object $context a context object, normally obtained with get_context_instance.
744 */
745 public function set_context($context) {
eb5bdb35
PS
746 if ($context === null) {
747 // extremely ugly hack which sets context to some value in order to prevent warnings,
748 // use only for core error handling!!!!
749 if (!$this->_context) {
750 $this->_context = get_context_instance(CONTEXT_SYSTEM);
751 }
752 return;
753 }
754
755 // ideally we should set context only once
756 if (isset($this->_context)) {
757 if ($context->id == $this->_context->id) {
758 // fine - no change needed
759 } else if ($this->_context->contextlevel == CONTEXT_SYSTEM or $this->_context->contextlevel == CONTEXT_COURSE) {
760 // hmm - not ideal, but it might produce too many warnings due to the design of require_login
761 } else {
762 // we do not want devs to do weird switching of context levels on the fly,
763 // because we might have used the context already such as in text filter in page title
764 debugging('Coding problem: unsupported modification of PAGE->context from '.$this->_context->contextlevel.' to '.$context->contextlevel);
765 }
766 }
767
2afe21ee 768 $this->_context = $context;
769 }
770
5ec434a9 771 /**
772 * The course module that this page belongs to (if it does belong to one).
773 *
6a630a10
PS
774 * @param stdClass $cm a full cm object obtained from get_coursemodule_from_id or get_coursemodule_from_instance.
775 * @param stdClass $course
776 * @param stdClass $module
777 * @return void
5ec434a9 778 */
779 public function set_cm($cm, $course = null, $module = null) {
eb5bdb35
PS
780 global $DB;
781
4f0c2d00 782 if (!isset($cm->name) || !isset($cm->modname) || !isset($cm->id)) {
5ec434a9 783 throw new coding_exception('The $cm you set on $PAGE must have been obtained with get_coursemodule_from_id or get_coursemodule_from_instance. That is, the ->name and -> modname fields must be present and correct.');
784 }
785 $this->_cm = $cm;
4f0c2d00 786 $this->_cm->context = get_context_instance(CONTEXT_MODULE, $cm->id); // hacky shortcut
5ec434a9 787 if (!$this->_context) {
4f0c2d00 788 $this->set_context($this->_cm->context);
5ec434a9 789 }
790 if (!$this->_course || $this->_course->id != $cm->course) {
791 if (!$course) {
eb5bdb35 792 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
5ec434a9 793 }
794 if ($course->id != $cm->course) {
795 throw new coding_exception('The course you passed to $PAGE->set_cm does not seem to correspond to the $cm.');
796 }
797 $this->set_course($course);
798 }
799 if ($module) {
800 $this->set_activity_record($module);
801 }
802 }
803
804 /**
805 * @param $module a row from the main database table for the module that this
806 * page belongs to. For example, if ->cm is a forum, then you can pass the
807 * corresponding row from the forum table here if you have it (saves a database
808 * query sometimes).
809 */
810 public function set_activity_record($module) {
811 if (is_null($this->_cm)) {
812 throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
813 }
814 if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
815 throw new coding_exception('The activity record your are trying to set does not seem to correspond to the cm that has been set.');
816 }
817 $this->_module = $module;
818 }
819
f230ce19 820 /**
821 * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'. Normally
822 * you do not need to set this manually, it is automatically created from the
823 * script name. However, on some pages this is overridden. For example, the
01ee99f4
PS
824 * page type for course/view.php includes the course format, for example
825 * 'course-view-weeks'. This gets used as the id attribute on <body> and
f230ce19 826 * also for determining which blocks are displayed.
827 */
828 public function set_pagetype($pagetype) {
829 $this->_pagetype = $pagetype;
830 }
831
34a2777c 832 /**
78946b9b 833 * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
191b267b 834 * This properly defaults to 'base', so you only need to call this function if
78946b9b
PS
835 * you want something different. The exact range of supported layouts is specified
836 * in the standard theme.
34a2777c 837 */
78946b9b
PS
838 public function set_pagelayout($pagelayout) {
839 $this->_pagelayout = $pagelayout;
34a2777c 840 }
841
5ed70539 842 /**
843 * If context->id and pagetype are not enough to uniquely identify this page,
844 * then you can set a subpage id as well. For example, the tags page sets
845 * @param string $subpage an arbitrary identifier that, along with context->id
846 * and pagetype, uniquely identifies this page.
847 */
848 public function set_subpage($subpage) {
08eab897 849 if (empty($subpage)) {
850 $this->_subpage = '';
851 } else {
852 $this->_subpage = $subpage;
853 }
5ed70539 854 }
855
753debd2 856 /**
857 * @param string $class add this class name ot the class attribute on the body tag.
858 */
859 public function add_body_class($class) {
860 if ($this->_state > self::STATE_BEFORE_HEADER) {
861 throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
862 }
863 $this->_bodyclasses[$class] = 1;
864 }
865
c13a5e71 866 /**
89fbdca3 867 * @param array $classes this utility method calls add_body_class for each array element.
c13a5e71 868 */
89fbdca3 869 public function add_body_classes($classes) {
870 foreach ($classes as $class) {
871 $this->add_body_class($class);
c13a5e71 872 }
873 }
f230ce19 874
34a2777c 875 /**
e120c61d 876 * @param string $title the title that should go in the <head> section of the HTML of this page.
34a2777c 877 */
878 public function set_title($title) {
879 $title = format_string($title);
880 $title = str_replace('"', '&quot;', $title);
881 $this->_title = $title;
882 }
883
884 /**
e120c61d 885 * @param string $heading the main heading that should be displayed at the top of the <body>.
34a2777c 886 */
887 public function set_heading($heading) {
888 $this->_heading = format_string($heading);
889 }
890
e120c61d 891 /**
892 * @param string $menu The menu/content to show in the heading
893 */
894 public function set_headingmenu($menu) {
895 $this->_headingmenu = $menu;
896 }
897
948203a5 898 /**
899 * Set the course category this page belongs to manually. This automatically
01ee99f4 900 * sets $PAGE->course to be the site course. You cannot use this method if
948203a5 901 * you have already set $PAGE->course - in that case, the category must be
902 * the one that the course belongs to. This also automatically sets the
903 * page context to the category context.
904 * @param integer $categoryid The id of the category to set.
905 */
906 public function set_category_by_id($categoryid) {
907 global $SITE, $DB;
908 if (!is_null($this->_course)) {
909 throw new coding_exception('Attempt to manually set the course category when the course has been set. This is not allowed.');
910 }
911 if (is_array($this->_categories)) {
912 throw new coding_exception('Course category has already been set. You are not allowed to change it.');
913 }
b7009474 914 $this->ensure_theme_not_set();
948203a5 915 $this->set_course($SITE);
916 $this->load_category($categoryid);
917 $this->set_context(get_context_instance(CONTEXT_COURSECAT, $categoryid));
918 }
919
82611d8d 920 /**
921 * Set a different path to use for the 'Moodle docs for this page' link.
922 * By default, it uses the pagetype, which is normally the same as the
923 * script name. So, for example, for mod/quiz/attempt.php, pagetype is
924 * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
925 * @param string $path the path to use at the end of the moodle docs URL.
926 */
927 public function set_docs_path($path) {
928 $this->_docspath = $path;
929 }
930
75781f87 931 /**
932 * You should call this method from every page to set the cleaned-up URL
933 * that should be used to return to this page. Used, for example, by the
934 * blocks editing UI to know where to return the user after an action.
935 * For example, course/view.php does:
936 * $id = optional_param('id', 0, PARAM_INT);
a6855934 937 * $PAGE->set_url('/course/view.php', array('id' => $id));
c909711a 938 * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
01ee99f4 939 * @param array $params parameters to add to the URL
75781f87 940 */
a6855934 941 public function set_url($url, array $params = null) {
75781f87 942 global $CFG;
42174e18 943
a6855934 944 if (is_string($url)) {
42174e18 945 if (strpos($url, 'http') === 0) {
a6855934
PS
946 // ok
947 } else if (strpos($url, '/') === 0) {
42174e18 948 // we have to use httpswwwroot here, because of loginhttps pages
a6855934
PS
949 $url = $CFG->httpswwwroot . $url;
950 } else {
951 throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
42174e18 952 }
7dc928b5 953 }
42174e18 954
a6855934
PS
955 $this->_url = new moodle_url($url, $params);
956
eb788065 957 $fullurl = $this->_url->out_omit_querystring();
42174e18
PS
958 if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
959 debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
960 }
961 $shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
962
75781f87 963 if (is_null($this->_pagetype)) {
7dc928b5 964 $this->initialise_default_pagetype($shorturl);
75781f87 965 }
5ec434a9 966 if (!is_null($this->_legacypageobject)) {
de8a1d10 967 $this->_legacypageobject->set_url($url, $params);
5ec434a9 968 }
75781f87 969 }
970
727ae436 971 /**
972 * Make sure page URL does not contain the given URL parameter.
973 *
974 * This should not be necessary if the script has called set_url properly.
975 * However, in some situations like the block editing actions; when the URL
976 * has been guessed, it will contain dangerous block-related actions.
977 * Therefore, the blocks code calls this function to clean up such parameters
978 * before doing any redirect.
117bd748 979 *
727ae436 980 * @param string $param the name of the parameter to make sure is not in the
981 * page URL.
982 */
983 public function ensure_param_not_in_url($param) {
984 $discard = $this->url; // Make sure $this->url is lazy-loaded;
985 $this->_url->remove_params($param);
986 }
987
34a2777c 988 /**
989 * There can be alternate versions of some pages (for example an RSS feed version).
990 * If such other version exist, call this method, and a link to the alternate
991 * version will be included in the <head> of the page.
992 *
993 * @param $title The title to give the alternate version.
994 * @param $url The URL of the alternate version.
995 * @param $mimetype The mime-type of the alternate version.
996 */
997 public function add_alternate_version($title, $url, $mimetype) {
998 if ($this->_state > self::STATE_BEFORE_HEADER) {
999 throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
1000 }
1001 $alt = new stdClass;
1002 $alt->title = $title;
9d88a01b 1003 $alt->url = $url;
34a2777c 1004 $this->_alternateversions[$mimetype] = $alt;
1005 }
1006
1007 /**
01ee99f4 1008 * Specify a form control should be focused when the page has loaded.
34a2777c 1009 *
01ee99f4 1010 * @param string $controlid the id of the HTML element to be focused.
34a2777c 1011 */
1012 public function set_focuscontrol($controlid) {
1013 $this->_focuscontrol = $controlid;
1014 }
1015
1016 /**
1017 * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
1018 *
1019 * @param string $html the HTML to display there.
1020 */
1021 public function set_button($html) {
1022 $this->_button = $html;
1023 }
1024
934524d7 1025 /**
1026 * Set the capability that allows users to edit blocks on this page. Normally
1027 * the default of 'moodle/site:manageblocks' is used, but a few pages like
1028 * the My Moodle page need to use a different capability like 'moodle/my:manageblocks'.
1029 * @param string $capability a capability.
1030 */
1031 public function set_blocks_editing_capability($capability) {
1032 $this->_blockseditingcap = $capability;
1033 }
1034
1035 /**
1036 * Some pages let you turn editing on for reasons other than editing blocks.
01ee99f4 1037 * If that is the case, you can pass other capabilities that let the user
934524d7 1038 * edit this page here.
1039 * @param string|array $capability either a capability, or an array of capabilities.
1040 */
1041 public function set_other_editing_capability($capability) {
1042 if (is_array($capability)) {
1043 $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
1044 } else {
1045 $this->_othereditingcaps[] = $capability;
1046 }
1047 }
1048
34a2777c 1049 /**
1050 * @return boolean $cacheable can this page be cached by the user's browser.
1051 */
1052 public function set_cacheable($cacheable) {
1053 $this->_cacheable = $cacheable;
1054 }
1055
17a6649b 1056 /**
1057 * Sets the page to periodically refresh
1058 *
1059 * This function must be called before $OUTPUT->header has been called or
1060 * a coding exception will be thrown.
1061 *
1062 * @param int $delay Sets the delay before refreshing the page, if set to null
1063 * refresh is cancelled
1064 */
1065 public function set_periodic_refresh_delay($delay=null) {
1066 if ($this->_state > self::STATE_BEFORE_HEADER) {
1067 throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
1068 }
1069 if ($delay===null) {
1070 $this->_periodicrefreshdelay = null;
1071 } else if (is_int($delay)) {
1072 $this->_periodicrefreshdelay = $delay;
1073 }
1074 }
1075
b7009474 1076 /**
1077 * Force this page to use a particular theme.
1078 *
cbcc9852 1079 * Please use this cautiously. It is only intended to be used by the themes selector admin page.
b7009474 1080 *
1081 * @param $themename the name of the theme to use.
1082 */
1083 public function force_theme($themename) {
b7009474 1084 $this->ensure_theme_not_set();
1085 $this->_theme = theme_config::load($themename);
b7009474 1086 }
1087
1088 /**
1089 * This function sets the $HTTPSPAGEREQUIRED global
1090 * (used in some parts of moodle to change some links)
1091 * and calculate the proper wwwroot to be used
1092 *
1093 * By using this function properly, we can ensure 100% https-ized pages
1094 * at our entire discretion (login, forgot_password, change_password)
1095 */
1096 public function https_required() {
1097 global $CFG, $HTTPSPAGEREQUIRED;
1098
1099 $this->ensure_theme_not_set();
1100
1101 if (!empty($CFG->loginhttps)) {
1102 $HTTPSPAGEREQUIRED = true;
1103 $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
b7009474 1104 } else {
1105 $CFG->httpswwwroot = $CFG->wwwroot;
b7009474 1106 }
1107 }
1108
753debd2 1109/// Initialisation methods =====================================================
1110/// These set various things up in a default way.
1111
08eab897 1112 /**
1113 * This method is called when the page first moves out of the STATE_BEFORE_HEADER
1114 * state. This is our last change to initialise things.
1115 */
1116 protected function starting_output() {
144390b4 1117 global $CFG;
9d1d606e 1118
94398fb3 1119 if (!during_initial_install()) {
1120 $this->blocks->load_blocks();
a19f419d 1121 if (empty($this->_block_actions_done)) {
2a3b0763 1122 $this->_block_actions_done = true;
a19f419d 1123 if ($this->blocks->process_url_actions($this)) {
b9bc2019 1124 redirect($this->url->out(false));
a19f419d 1125 }
21d33bdf 1126 }
4578a5eb 1127 $this->blocks->create_all_block_instances();
08eab897 1128 }
1129
144390b4 1130 // If maintenance mode is on, change the page header.
1131 if (!empty($CFG->maintenance_enabled)) {
1132 $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
1133 '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
1134 '</a> ' . $this->button);
21d33bdf 1135
144390b4 1136 $title = $this->title;
1137 if ($title) {
1138 $title .= ' - ';
1139 }
1140 $this->set_title($title . get_string('maintenancemode', 'admin'));
1141 }
21d33bdf 1142
144390b4 1143 // Show the messaging popup, if there are messages.
1144 message_popup_window();
1145
21d33bdf 1146 $this->initialise_standard_body_classes();
b7009474 1147 }
1148
1149 /**
1150 * Method for use by Moodle core to set up the theme. Do not
1151 * use this in your own code.
1152 *
1153 * Make sure the right theme for this page is loaded. Tell our
1154 * blocks_manager about the theme block regions, and then, if
cbcc9852 1155 * we are $PAGE, set up the global $OUTPUT.
b7009474 1156 */
1157 public function initialise_theme_and_output() {
cbcc9852 1158 global $OUTPUT, $PAGE, $SITE;
b7009474 1159
d4a03c00 1160 if (!empty($this->_wherethemewasinitialised)) {
1161 return;
1162 }
1163
eb5bdb35
PS
1164 if (!during_initial_install()) {
1165 // detect PAGE->context mess
1166 $this->magic_get_context();
1167 }
1168
94398fb3 1169 if (!$this->_course && !during_initial_install()) {
b7009474 1170 $this->set_course($SITE);
1171 }
1172
1173 if (is_null($this->_theme)) {
1174 $themename = $this->resolve_theme();
1175 $this->_theme = theme_config::load($themename);
78946b9b 1176 $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
b7009474 1177 }
1178
78946b9b 1179 $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
b7009474 1180
1181 if ($this === $PAGE) {
649cf95d 1182 $OUTPUT = $this->get_renderer('core');
b7009474 1183 }
1184
1185 $this->_wherethemewasinitialised = debug_backtrace();
1186 }
1187
1188 /**
1189 * Work out the theme this page should use.
1190 *
1191 * This depends on numerous $CFG settings, and the properties of this page.
1192 *
1193 * @return string the name of the theme that should be used on this page.
1194 */
1195 protected function resolve_theme() {
1196 global $CFG, $USER, $SESSION;
1197
1198 if (empty($CFG->themeorder)) {
1199 $themeorder = array('course', 'category', 'session', 'user', 'site');
1200 } else {
1201 $themeorder = $CFG->themeorder;
1202 // Just in case, make sure we always use the site theme if nothing else matched.
1203 $themeorder[] = 'site';
1204 }
1205
1206 $mnetpeertheme = '';
1207 if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id) {
1208 require_once($CFG->dirroot.'/mnet/peer.php');
1209 $mnetpeer = new mnet_peer();
1210 $mnetpeer->set_id($USER->mnethostid);
1211 if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
1212 $mnetpeertheme = $mnetpeer->theme;
1213 }
1214 }
1215
1216 $theme = '';
1217 foreach ($themeorder as $themetype) {
1218 switch ($themetype) {
1219 case 'course':
1220 if (!empty($CFG->allowcoursethemes) and !empty($this->course->theme)) {
1221 return $this->course->theme;
1222 }
1223
1224 case 'category':
1225 if (!empty($CFG->allowcategorythemes)) {
1226 $categories = $this->categories;
1227 foreach ($categories as $category) {
1228 if (!empty($category->theme)) {
1229 return $category->theme;
1230 }
1231 }
1232 }
1233
1234 case 'session':
1235 if (!empty($SESSION->theme)) {
1236 return $SESSION->theme;
1237 }
1238
1239 case 'user':
1240 if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) {
1241 if ($mnetpeertheme) {
1242 return $mnetpeertheme;
1243 } else {
1244 return $USER->theme;
1245 }
1246 }
1247
1248 case 'site':
1249 if ($mnetpeertheme) {
1250 return $mnetpeertheme;
ee8df661
SH
1251 } else if(!empty($CFG->themelegacy) && $this->browser_is_outdated()) {
1252 $this->_legacythemeinuse = true;
1253 return $CFG->themelegacy;
b7009474 1254 } else {
ee8df661 1255 return $CFG->theme;
b7009474 1256 }
1257 }
1258 }
08eab897 1259 }
1260
ee8df661
SH
1261 /**
1262 * Determines whether the current browser should
1263 * default to the admin-selected legacy theme
1264 *
1265 * @return true if legacy theme should be used, otherwise false
78bfb562 1266 *
ee8df661
SH
1267 */
1268 protected function browser_is_outdated() {
1269 foreach($this->_legacybrowsers as $browser => $version) {
1270 // Check the browser is valid first then that its version is suitable
1271 if(check_browser_version($browser, '0') &&
1272 !check_browser_version($browser, $version)) {
1273 return true;
1274 }
1275 }
1276 return false;
1277 }
78bfb562 1278
d529807a 1279 /**
1280 * Sets ->pagetype from the script name. For example, if the script that was
1281 * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
1282 * @param string $script the path to the script that should be used to
1283 * initialise ->pagetype. If not passed the $SCRIPT global will be used.
1284 * If legacy code has set $CFG->pagepath that will be used instead, and a
1285 * developer warning issued.
1286 */
ad52c04f 1287 protected function initialise_default_pagetype($script = null) {
d529807a 1288 global $CFG, $SCRIPT;
1289
1290 if (isset($CFG->pagepath)) {
1291 debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
1292 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
1293 $script = $CFG->pagepath;
1294 unset($CFG->pagepath);
1295 }
1296
ad52c04f 1297 if (is_null($script)) {
d529807a 1298 $script = ltrim($SCRIPT, '/');
1299 $len = strlen($CFG->admin);
1300 if (substr($script, 0, $len) == $CFG->admin) {
1301 $script = 'admin' . substr($script, $len);
1302 }
1303 }
1304
1305 $path = str_replace('.php', '', $script);
1306 if (substr($path, -1) == '/') {
1307 $path .= 'index';
1308 }
1309
1310 if (empty($path) || $path == 'index') {
1311 $this->_pagetype = 'site-index';
1312 } else {
1313 $this->_pagetype = str_replace('/', '-', $path);
1314 }
1315 }
1316
753debd2 1317 protected function initialise_standard_body_classes() {
b7009474 1318 global $CFG, $USER;
89fbdca3 1319
753debd2 1320 $pagetype = $this->pagetype;
1321 if ($pagetype == 'site-index') {
1322 $this->_legacyclass = 'course';
1323 } else if (substr($pagetype, 0, 6) == 'admin-') {
1324 $this->_legacyclass = 'admin';
753debd2 1325 }
1326 $this->add_body_class($this->_legacyclass);
1327
90723839
SH
1328 $pathbits = explode('-', trim($pagetype));
1329 for ($i=1;$i<count($pathbits);$i++) {
1330 $this->add_body_class('path-'.join('-',array_slice($pathbits, 0, $i)));
1331 }
1332
89fbdca3 1333 $this->add_body_classes(get_browser_version_classes());
e372f4c7 1334 $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
753debd2 1335 $this->add_body_class('lang-' . current_language());
cf615522 1336 $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
59c58c36 1337 $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
89fbdca3 1338 $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
1339
191b267b
PS
1340 $this->add_body_class('pagelayout-' . $this->_pagelayout); // extra class describing current page layout
1341
94398fb3 1342 if (!during_initial_install()) {
1343 $this->add_body_class('course-' . $this->_course->id);
eb5bdb35 1344 $this->add_body_class('context-' . $this->_context->id);
a1326170 1345 }
1346
b80856bd 1347 if (!empty($this->_cm)) {
1348 $this->add_body_class('cmid-' . $this->_cm->id);
1349 }
1350
94398fb3 1351 if (!empty($CFG->allowcategorythemes)) {
d7ab8879 1352 $this->ensure_category_loaded();
1353 foreach ($this->_categories as $catid => $notused) {
1354 $this->add_body_class('category-' . $catid);
1355 }
1356 } else {
1357 $catid = 0;
1358 if (is_array($this->_categories)) {
1359 $catids = array_keys($this->_categories);
1360 $catid = reset($catids);
1361 } else if (!empty($this->_course->category)) {
1362 $catid = $this->_course->category;
1363 }
1364 if ($catid) {
1365 $this->add_body_class('category-' . $catid);
1366 }
1367 }
1368
753debd2 1369 if (!isloggedin()) {
1370 $this->add_body_class('notloggedin');
1371 }
1372
1373 if (!empty($USER->editing)) {
1374 $this->add_body_class('editing');
39726f35
AB
1375 if (optional_param('bui_moveid', false, PARAM_INT)) {
1376 $this->add_body_class('blocks-moving');
1377 }
753debd2 1378 }
1379
1380 if (!empty($CFG->blocksdrag)) {
1381 $this->add_body_class('drag');
1382 }
ee8df661
SH
1383
1384 if ($this->_legacythemeinuse) {
1385 $this->add_body_class('legacytheme');
1386 }
753debd2 1387 }
1388
5ec434a9 1389 protected function load_activity_record() {
1390 global $DB;
1391 if (is_null($this->_cm)) {
1392 return;
1393 }
1394 $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
1395 }
1396
948203a5 1397 protected function ensure_category_loaded() {
1398 if (is_array($this->_categories)) {
1399 return; // Already done.
1400 }
1401 if (is_null($this->_course)) {
1402 throw new coding_exception('Attempt to get the course category for this page before the course was set.');
1403 }
1404 if ($this->_course->category == 0) {
1405 $this->_categories = array();
1406 } else {
1407 $this->load_category($this->_course->category);
1408 }
1409 }
1410
1411 protected function load_category($categoryid) {
1412 global $DB;
1413 $category = $DB->get_record('course_categories', array('id' => $categoryid));
1414 if (!$category) {
1415 throw new moodle_exception('unknowncategory');
1416 }
1417 $this->_categories[$category->id] = $category;
1418 $parentcategoryids = explode('/', trim($category->path, '/'));
1419 array_pop($parentcategoryids);
1420 foreach (array_reverse($parentcategoryids) as $catid) {
1421 $this->_categories[$catid] = null;
1422 }
1423 }
1424
1425 protected function ensure_categories_loaded() {
1426 global $DB;
1427 $this->ensure_category_loaded();
1428 if (!is_null(end($this->_categories))) {
1429 return; // Already done.
1430 }
1431 $idstoload = array_keys($this->_categories);
1432 array_shift($idstoload);
1433 $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
1434 foreach ($idstoload as $catid) {
1435 $this->_categories[$catid] = $categories[$catid];
1436 }
1437 }
1438
b7009474 1439 protected function ensure_theme_not_set() {
1440 if (!is_null($this->_theme)) {
1441 throw new coding_exception('The theme has already been set up for this page ready for output. ' .
1442 'Therefore, you can no longer change the theme, or anything that might affect what ' .
1443 'the current theme is, for example, the course.',
1444 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
1445 }
1446 }
1447
89fbdca3 1448 protected function url_to_class_name($url) {
1449 $bits = parse_url($url);
1450 $class = str_replace('.', '-', $bits['host']);
1451 if (!empty($bits['port'])) {
1452 $class .= '--' . $bits['port'];
1453 }
1454 if (!empty($bits['path'])) {
1455 $path = trim($bits['path'], '/');
1456 if ($path) {
1457 $class .= '--' . str_replace('/', '-', $path);
1458 }
1459 }
1460 return $class;
1461 }
1462
934524d7 1463 protected function all_editing_caps() {
1464 $caps = $this->_othereditingcaps;
1465 $caps[] = $this->_blockseditingcap;
1466 return $caps;
1467 }
1468
753debd2 1469/// Deprecated fields and methods for backwards compatibility ==================
d529807a 1470
f230ce19 1471 /**
31940ba6 1472 * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
f230ce19 1473 * @return string page type.
1474 */
1475 public function get_type() {
1476 debugging('Call to deprecated method moodle_page::get_type. Please use $PAGE->pagetype instead.');
1477 return $this->get_pagetype();
1478 }
d529807a 1479
1480 /**
1481 * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
1482 * @return string this is what page_id_and_class used to return via the $getclass parameter.
1483 */
1484 function get_format_name() {
1485 return $this->get_pagetype();
1486 }
1487
31940ba6 1488 /**
1489 * @deprecated since Moodle 2.0 - use $PAGE->course instead.
1490 * @return object course.
1491 */
1492 public function get_courserecord() {
1493 debugging('Call to deprecated method moodle_page::get_courserecord. Please use $PAGE->course instead.');
1494 return $this->get_course();
1495 }
d529807a 1496
1497 /**
1498 * @deprecated since Moodle 2.0
1499 * @return string this is what page_id_and_class used to return via the $getclass parameter.
1500 */
1501 public function get_legacyclass() {
1502 if (is_null($this->_legacyclass)) {
753debd2 1503 $this->initialise_standard_body_classes();
d529807a 1504 }
1505 debugging('Call to deprecated method moodle_page::get_legacyclass.');
1506 return $this->_legacyclass;
1507 }
4873f5f7 1508
1509 /**
86b5ea0f 1510 * @deprecated since Moodle 2.0 - use $PAGE->blocks->get_regions() instead
4873f5f7 1511 * @return string the places on this page where blocks can go.
1512 */
1513 function blocks_get_positions() {
86b5ea0f 1514 debugging('Call to deprecated method moodle_page::blocks_get_positions. Use $PAGE->blocks->get_regions() instead.');
1515 return $this->blocks->get_regions();
4873f5f7 1516 }
1517
1518 /**
86b5ea0f 1519 * @deprecated since Moodle 2.0 - use $PAGE->blocks->get_default_region() instead
4873f5f7 1520 * @return string the default place for blocks on this page.
1521 */
1522 function blocks_default_position() {
86b5ea0f 1523 debugging('Call to deprecated method moodle_page::blocks_default_position. Use $PAGE->blocks->get_default_region() instead.');
1524 return $this->blocks->get_default_region();
4873f5f7 1525 }
1526
1527 /**
1528 * @deprecated since Moodle 2.0 - no longer used.
1529 */
1530 function blocks_get_default() {
1531 debugging('Call to deprecated method moodle_page::blocks_get_default. This method has no function any more.');
1532 }
1533
1534 /**
1535 * @deprecated since Moodle 2.0 - no longer used.
1536 */
1537 function blocks_move_position(&$instance, $move) {
1538 debugging('Call to deprecated method moodle_page::blocks_move_position. This method has no function any more.');
1539 }
ad52c04f 1540
1541 /**
1542 * @deprecated since Moodle 2.0 - use $this->url->params() instead.
1543 * @return array URL parameters for this page.
1544 */
1545 function url_get_parameters() {
1546 debugging('Call to deprecated method moodle_page::url_get_parameters. Use $this->url->params() instead.');
1547 return $this->url->params();
1548 }
1549
1550 /**
1551 * @deprecated since Moodle 2.0 - use $this->url->params() instead.
1552 * @return string URL for this page without parameters.
1553 */
1554 function url_get_path() {
47ce714b
PS
1555 debugging('Call to deprecated method moodle_page::url_get_path. Use $this->url->out() instead.');
1556 return $this->url->out();
ad52c04f 1557 }
1558
1559 /**
1560 * @deprecated since Moodle 2.0 - use $this->url->out() instead.
1561 * @return string full URL for this page.
1562 */
1563 function url_get_full($extraparams = array()) {
1564 debugging('Call to deprecated method moodle_page::url_get_full. Use $this->url->out() instead.');
b9bc2019 1565 return $this->url->out(true, $extraparams);
ad52c04f 1566 }
0d5890c4 1567
1568 /**
1569 * @deprecated since Moodle 2.0 - just a backwards compatibility hook.
1570 */
1571 function set_legacy_page_object($pageobject) {
1572 return $this->_legacypageobject = $pageobject;
1573 }
1574
1575 /**
1576 * @deprecated since Moodle 2.0 - page objects should no longer be doing print_header.
1577 * @param $_,...
1578 */
1579 function print_header($_) {
1580 if (is_null($this->_legacypageobject)) {
1581 throw new coding_exception('You have called print_header on $PAGE when there is not a legacy page class present.');
1582 }
1583 debugging('You should not longer be doing print_header via a page class.', DEBUG_DEVELOPER);
1584 $args = func_get_args();
1585 call_user_func_array(array($this->_legacypageobject, 'print_header'), $args);
1586 }
1587
1588 /**
1589 * @deprecated since Moodle 2.0
1590 * @return the 'page id'. This concept no longer exists.
1591 */
1592 function get_id() {
1593 debugging('Call to deprecated method moodle_page::get_id(). It should not be necessary any more.', DEBUG_DEVELOPER);
1594 if (!is_null($this->_legacypageobject)) {
1595 return $this->_legacypageobject->get_id();
1596 }
1597 return 0;
1598 }
5ec434a9 1599
f8b80dbd 1600 /**
1601 * @deprecated since Moodle 2.0
1602 * @return the 'page id'. This concept no longer exists.
1603 */
1604 function get_pageid() {
1605 debugging('Call to deprecated method moodle_page::get_pageid(). It should not be necessary any more.', DEBUG_DEVELOPER);
1606 if (!is_null($this->_legacypageobject)) {
1607 return $this->_legacypageobject->get_id();
1608 }
1609 return 0;
1610 }
1611
5ec434a9 1612 /**
1613 * @deprecated since Moodle 2.0 - user $PAGE->cm instead.
1614 * @return $this->cm;
1615 */
1616 function get_modulerecord() {
1617 return $this->cm;
1618 }
bf6c37c7 1619
1620 public function has_set_url() {
1621 return ($this->_url!==null);
1622 }
05c92729
SH
1623
1624 public function set_block_actions_done($setting = true) {
1625 $this->_block_actions_done = $setting;
1626 }
ad52c04f 1627}
1628
de60de04 1629/**
1630 * @deprecated since Moodle 2.0
0d5890c4 1631 * Not needed any more.
de60de04 1632 * @param $path the folder path
1633 * @return array an array of page types.
1634 */
3f6aba0c 1635function page_import_types($path) {
1636 global $CFG;
de60de04 1637 debugging('Call to deprecated function page_import_types.', DEBUG_DEVELOPER);
3f6aba0c 1638}
1639
7c6c0513 1640/**
d529807a 1641 * @deprecated since Moodle 2.0
0d5890c4 1642 * Do not use this any more. The global $PAGE is automatically created for you.
1643 * If you need custom behaviour, you should just set properties of that object.
d529807a 1644 * @param integer $instance legacy page instance id.
1645 * @return the global $PAGE object.
7c6c0513 1646 */
7c6c0513 1647function page_create_instance($instance) {
c85acc87 1648 global $PAGE;
d529807a 1649 return page_create_object($PAGE->pagetype, $instance);
7c6c0513 1650}
1651
9b128500 1652/**
0d5890c4 1653 * @deprecated since Moodle 2.0
1654 * Do not use this any more. The global $PAGE is automatically created for you.
1655 * If you need custom behaviour, you should just set properties of that object.
9b128500 1656 */
9b128500 1657function page_create_object($type, $id = NULL) {
5ec434a9 1658 global $CFG, $PAGE, $SITE, $ME;
0d5890c4 1659 debugging('Call to deprecated function page_create_object.', DEBUG_DEVELOPER);
93dcb13e 1660
9b128500 1661 $data = new stdClass;
1662 $data->pagetype = $type;
5ec434a9 1663 $data->pageid = $id;
9b128500 1664
1665 $classname = page_map_class($type);
f8b80dbd 1666 if (!$classname) {
1667 return $PAGE;
1668 }
ad52c04f 1669 $legacypage = new $classname;
1670 $legacypage->init_quick($data);
ad52c04f 1671
d7ab8879 1672 $course = $PAGE->course;
1673 if ($course->id != $SITE->id) {
ad52c04f 1674 $legacypage->set_course($course);
d7ab8879 1675 } else {
1676 try {
1677 $category = $PAGE->category;
1678 } catch (coding_exception $e) {
1679 // Was not set before, so no need to try to set it again.
1680 $category = false;
1681 }
1682 if ($category) {
ad52c04f 1683 $legacypage->set_category_by_id($category->id);
d7ab8879 1684 } else {
ad52c04f 1685 $legacypage->set_course($SITE);
d7ab8879 1686 }
1687 }
f474a4e5 1688
0d5890c4 1689 $legacypage->set_pagetype($type);
5ec434a9 1690
1691 $legacypage->set_url($ME);
1692 $PAGE->set_url(str_replace($CFG->wwwroot . '/', '', $legacypage->url_get_full()));
0d5890c4 1693
1694 $PAGE->set_pagetype($type);
1695 $PAGE->set_legacy_page_object($legacypage);
1696 return $PAGE;
9b128500 1697}
1698
1699/**
0d5890c4 1700 * @deprecated since Moodle 2.0
1701 * You should not be writing page subclasses any more. Just set properties on the
1702 * global $PAGE object to control its behaviour.
9b128500 1703 */
9b128500 1704function page_map_class($type, $classname = NULL) {
93dcb13e 1705 global $CFG;
1706
0d5890c4 1707 static $mappings = array(
1708 PAGE_COURSE_VIEW => 'page_course',
1709 );
9b128500 1710
93dcb13e 1711 if (!empty($type) && !empty($classname)) {
9b128500 1712 $mappings[$type] = $classname;
1713 }
93dcb13e 1714
1715 if (!isset($mappings[$type])) {
ea82d6b6 1716 debugging('Page class mapping requested for unknown type: '.$type);
f8b80dbd 1717 return null;
1718 } else if (empty($classname) && !class_exists($mappings[$type])) {
ea82d6b6 1719 debugging('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined');
f8b80dbd 1720 return null;
9b128500 1721 }
1722
1723 return $mappings[$type];
1724}
1725
f032aa7a 1726/**
0d5890c4 1727 * @deprecated since Moodle 2.0
f032aa7a 1728 * Parent class from which all Moodle page classes derive
1729 *
eb5bdb35
PS
1730 * @package core
1731 * @subpackage lib
1732 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1733 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f032aa7a 1734 */
c13a5e71 1735class page_base extends moodle_page {
e0134f84 1736 /**
1737 * The numeric identifier of the page being described.
1738 * @var int $id
1739 */
f032aa7a 1740 var $id = NULL;
e0134f84 1741
e0134f84 1742/// Class Functions
1743
c8e0b579 1744 // HTML OUTPUT SECTION
1745
c8e0b579 1746 // SELF-REPORTING SECTION
1747
c8e0b579 1748 // Simple stuff, do not override this.
f032aa7a 1749 function get_id() {
1750 return $this->id;
1751 }
c8e0b579 1752
c8e0b579 1753 // Initialize the data members of the parent class
f032aa7a 1754 function init_quick($data) {
f032aa7a 1755 $this->id = $data->pageid;
1756 }
1757
c8e0b579 1758 function init_full() {
c8e0b579 1759 }
f032aa7a 1760}
1761
f032aa7a 1762/**
0d5890c4 1763 * @deprecated since Moodle 2.0
9d1d606e 1764 * Class that models the behavior of a moodle course.
1765 * Although this does nothing, this class declaration should be left for now
1766 * since there may be legacy class doing class page_... extends page_course
f032aa7a 1767 *
eb5bdb35
PS
1768 * @package core
1769 * @subpackage lib
1770 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1771 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f032aa7a 1772 */
99110470 1773class page_course extends page_base {
f032aa7a 1774}
1775
cadc69c6 1776/**
0d5890c4 1777 * @deprecated since Moodle 2.0
b83eee7d 1778 * Class that models the common parts of all activity modules
cadc69c6 1779 *
eb5bdb35
PS
1780 * @package core
1781 * @subpackage lib
1782 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1783 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
cadc69c6 1784 */
b83eee7d 1785class page_generic_activity extends page_base {
9d1d606e 1786 // Although this function is deprecated, it should be left here because
1787 // people upgrading legacy code need to copy it. See
3681f9a9 1788 // http://docs.moodle.org/en/Development:Migrating_your_code_code_to_the_2.0_rendering_API
3b27b0fe 1789 function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
de6d81e6 1790 global $USER, $CFG, $PAGE, $OUTPUT;
de60de04 1791
6db8acb6 1792 $this->init_full();
1793 $replacements = array(
1794 '%fullname%' => format_string($this->activityrecord->name)
1795 );
1796 foreach ($replacements as $search => $replace) {
1797 $title = str_replace($search, $replace, $title);
1798 }
de60de04 1799
d8ae33a9 1800 $buttons = '<table><tr><td>'.$OUTPUT->update_module_button($this->modulerecord->id, $this->activityname).'</td>';
59e2121e 1801 if ($this->user_allowed_editing()) {
4aea3cc7 1802 $buttons .= '<td><form method="get" action="view.php"><div>'.
cfcfb9f3 1803 '<input type="hidden" name="id" value="'.$this->modulerecord->id.'" />'.
1804 '<input type="hidden" name="edit" value="'.($this->user_is_editing()?'off':'on').'" />'.
1805 '<input type="submit" value="'.get_string($this->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td>';
6db8acb6 1806 }
cfcfb9f3 1807 $buttons .= '</tr></table>';
de60de04 1808
de6d81e6 1809 if (!empty($morenavlinks) && is_array($morenavlinks)) {
1810 foreach ($morenavlinks as $navitem) {
1811 if (is_array($navitem) && array_key_exists('name', $navitem)) {
1812 $link = null;
1813 if (array_key_exists('link', $navitem)) {
1814 $link = $navitem['link'];
1815 }
91152a35 1816 $PAGE->navbar->add($navitem['name'], $link);
de6d81e6 1817 }
1818 }
38e179a4 1819 }
de6d81e6 1820
1821 $PAGE->set_title($title);
1822 $PAGE->set_heading($this->course->fullname);
1823 $PAGE->set_button($buttons);
91152a35 1824 echo $OUTPUT->header();
6db8acb6 1825 }
b83eee7d 1826}