MDL-20636 fix most of the remaining codechecker issues in mod/quiz and lib/questionli...
[moodle.git] / lib / pagelib.php
blob2fef3af923ed090de6de817b95712bbd2e1bf7b2
1 <?php
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.
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/>.
18 /**
19 * This file contains the moodle_page class. There is normally a single instance
20 * of this class in the $PAGE global variable. This class is a central repository
21 * of information about the page we are building up to send back to the user.
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
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * $PAGE is a central store of information about the current page we are
33 * generating in response to the user's request.
35 * It does not do very much itself
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.
40 * @copyright 2009 Tim Hunt
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 * @since Moodle 2.0
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.
52 * @property-read string $button The HTML to go where the Turn editing on button normally goes.
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.
68 * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
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.
73 * @property-read bool $legacythemeinuse Returns true if the legacy theme is being used.
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'.
79 * Allows the theme to display things differently, if it wishes to.
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 $settingsnav
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.
90 class 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;
95 const STATE_DONE = 3;
96 /**#@-*/
98 /// Field declarations =========================================================
100 protected $_state = self::STATE_BEFORE_HEADER;
102 protected $_course = null;
105 * If this page belongs to a module, this is the cm_info module description object.
106 * @var cm_info
108 protected $_cm = null;
111 * If $_cm is not null, then this will hold the corresponding row from the
112 * modname table. For example, if $_cm->modname is 'quiz', this will be a
113 * row from the quiz table.
115 protected $_module = null;
118 * The context that this page belongs to.
120 protected $_context = null;
123 * This holds any categories that $_course belongs to, starting with the
124 * particular category it belongs to, and working out through any parent
125 * categories to the top level. These are loaded progressively, if needed.
126 * There are three states. $_categories = null initially when nothing is
127 * loaded; $_categories = array($id => $cat, $parentid => null) when we have
128 * loaded $_course->category, but not any parents; and a complete array once
129 * everything is loaded.
131 protected $_categories = null;
133 protected $_bodyclasses = array();
135 protected $_title = '';
137 protected $_heading = '';
139 protected $_pagetype = null;
141 protected $_pagelayout = 'base';
144 * List of theme layout options, these are ignored by core.
145 * To be used in individual theme layout files only.
146 * @var array
148 protected $_layout_options = array();
150 protected $_subpage = '';
152 protected $_docspath = null;
154 /** @var string|null A legacy class that will be added to the body tag */
155 protected $_legacyclass = null;
157 protected $_url = null;
159 protected $_alternateversions = array();
161 protected $_blocks = null;
163 protected $_requires = null;
165 protected $_blockseditingcap = 'moodle/site:manageblocks';
167 protected $_block_actions_done = false;
169 protected $_othereditingcaps = array();
171 protected $_cacheable = true;
173 protected $_focuscontrol = '';
175 protected $_button = '';
177 protected $_theme = null;
178 /** @var global_navigation Contains the global navigation structure*/
179 protected $_navigation = null;
180 /** @var null|settings_navigation Contains the settings navigation structure*/
181 protected $_settingsnav = null;
182 /** @var null|navbar Contains the navbar structure*/
183 protected $_navbar = null;
184 /** @var string */
185 protected $_headingmenu = null;
188 * Then the theme is initialised, we save the stack trace, for use in error messages.
189 * @var array stack trace.
191 protected $_wherethemewasinitialised = null;
193 /** @var xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed. */
194 protected $_opencontainers;
197 * Sets the page to refresh after a given delay (in seconds) using meta refresh
198 * in {@link standard_head_html()} in outputlib.php
199 * If set to null(default) the page is not refreshed
200 * @var int|null
202 protected $_periodicrefreshdelay = null;
205 * This is simply to improve backwards compatibility. If old code relies on
206 * a page class that implements print_header, or complex logic in
207 * user_allowed_editing then we stash an instance of that other class here,
208 * and delegate to it in certain situations.
210 protected $_legacypageobject = null;
213 * Associative array of browser shortnames (as used by check_browser_version)
214 * and their minimum required versions
215 * @var array
217 protected $_legacybrowsers = array('MSIE' => 6.0);
220 * Is set to true if the chosen legacy theme is in use. False by default.
221 * @var bool
223 protected $_legacythemeinuse = false;
225 protected $_https_login_required = false;
227 protected $_popup_notification_allowed = true;
229 /// Magic getter methods =============================================================
230 /// Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
231 /// methods, but instead use the $PAGE->x syntax.
234 * Please do not call this method directly, use the ->state syntax. {@link __get()}.
235 * @return integer one of the STATE_... constants. You should not normally need
236 * to use this in your code. It is intended for internal use by this class
237 * and its friends like print_header, to check that everything is working as
238 * expected. Also accessible as $PAGE->state.
240 protected function magic_get_state() {
241 return $this->_state;
245 * Please do not call this method directly, use the ->headerprinted syntax. {@link __get()}.
246 * @return boolean has the header already been printed?
248 protected function magic_get_headerprinted() {
249 return $this->_state >= self::STATE_IN_BODY;
253 * Please do not call this method directly, use the ->course syntax. {@link __get()}.
255 * @global object
256 * @return object the current course that we are inside - a row from the
257 * course table. (Also available as $COURSE global.) If we are not inside
258 * an actual course, this will be the site course.
260 protected function magic_get_course() {
261 global $SITE;
262 if (is_null($this->_course)) {
263 return $SITE;
265 return $this->_course;
269 * Please do not call this method directly, use the ->cm syntax. {@link __get()}.
270 * @return object the course_module that this page belongs to. Will be null
271 * if this page is not within a module. This is a full cm object, as loaded
272 * by get_coursemodule_from_id or get_coursemodule_from_instance,
273 * so the extra modname and name fields are present.
274 * @return cm_info
276 protected function magic_get_cm() {
277 return $this->_cm;
281 * Please do not call this method directly, use the ->activityrecord syntax. {@link __get()}.
282 * @return object the row from the activities own database table (for example
283 * the forum or quiz table) that this page belongs to. Will be null
284 * if this page is not within a module.
286 protected function magic_get_activityrecord() {
287 if (is_null($this->_module) && !is_null($this->_cm)) {
288 $this->load_activity_record();
290 return $this->_module;
294 * Please do not call this method directly, use the ->activityname syntax. {@link __get()}.
295 * @return string|null the The type of activity we are in, for example 'forum' or 'quiz'.
296 * Will be null if this page is not within a module.
298 protected function magic_get_activityname() {
299 if (is_null($this->_cm)) {
300 return null;
302 return $this->_cm->modname;
306 * Please do not call this method directly, use the ->category syntax. {@link __get()}.
307 * @return mixed the category that the page course belongs to. If there isn't one
308 * (that is, if this is the front page course) returns null.
310 protected function magic_get_category() {
311 $this->ensure_category_loaded();
312 if (!empty($this->_categories)) {
313 return reset($this->_categories);
314 } else {
315 return null;
320 * Please do not call this method directly, use the ->categories syntax. {@link __get()}.
321 * @return array an array of all the categories the page course belongs to,
322 * starting with the immediately containing category, and working out to
323 * the top-level category. This may be the empty array if we are in the
324 * front page course.
326 protected function magic_get_categories() {
327 $this->ensure_categories_loaded();
328 return $this->_categories;
332 * Please do not call this method directly, use the ->context syntax. {@link __get()}.
333 * @return object the main context to which this page belongs.
335 protected function magic_get_context() {
336 if (is_null($this->_context)) {
337 if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
338 // cli scripts work in system context, do not annoy devs with debug info
339 // very few scripts do not use cookies, we can safely use system as default context there
340 } else {
341 debugging('Coding problem: this page does not set $PAGE->context properly.');
343 $this->_context = get_context_instance(CONTEXT_SYSTEM);
345 return $this->_context;
349 * Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
350 * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
352 protected function magic_get_pagetype() {
353 global $CFG;
354 if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
355 $this->initialise_default_pagetype();
357 return $this->_pagetype;
361 * Please do not call this method directly, use the ->pagetype syntax. {@link __get()}.
362 * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
364 protected function magic_get_bodyid() {
365 return 'page-'.$this->pagetype;
369 * Please do not call this method directly, use the ->pagelayout syntax. {@link __get()}.
370 * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
371 * Allows the theme to display things differently, if it wishes to.
373 protected function magic_get_pagelayout() {
374 return $this->_pagelayout;
378 * Please do not call this method directly, use the ->layout_tions syntax. {@link __get()}.
379 * @return array returns arrys with options for layout file
381 protected function magic_get_layout_options() {
382 return $this->_layout_options;
386 * Please do not call this method directly, use the ->subpage syntax. {@link __get()}.
387 * @return string|null The subpage identifier, if any.
389 protected function magic_get_subpage() {
390 return $this->_subpage;
394 * Please do not call this method directly, use the ->bodyclasses syntax. {@link __get()}.
395 * @return string the class names to put on the body element in the HTML.
397 protected function magic_get_bodyclasses() {
398 return implode(' ', array_keys($this->_bodyclasses));
402 * Please do not call this method directly, use the ->title syntax. {@link __get()}.
403 * @return string the title that should go in the <head> section of the HTML of this page.
405 protected function magic_get_title() {
406 return $this->_title;
410 * Please do not call this method directly, use the ->heading syntax. {@link __get()}.
411 * @return string the main heading that should be displayed at the top of the <body>.
413 protected function magic_get_heading() {
414 return $this->_heading;
418 * Please do not call this method directly, use the ->heading syntax. {@link __get()}.
419 * @return string The menu (or actions) to display in the heading
421 protected function magic_get_headingmenu() {
422 return $this->_headingmenu;
426 * Please do not call this method directly, use the ->docspath syntax. {@link __get()}.
427 * @return string the path to the Moodle docs for this page.
429 protected function magic_get_docspath() {
430 if (is_string($this->_docspath)) {
431 return $this->_docspath;
432 } else {
433 return str_replace('-', '/', $this->pagetype);
438 * Please do not call this method directly, use the ->url syntax. {@link __get()}.
439 * @return moodle_url the clean URL required to load the current page. (You
440 * should normally use this in preference to $ME or $FULLME.)
442 protected function magic_get_url() {
443 global $FULLME;
444 if (is_null($this->_url)) {
445 debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
446 $this->_url = new moodle_url($FULLME);
447 // Make sure the guessed URL cannot lead to dangerous redirects.
448 $this->_url->remove_params('sesskey');
450 return new moodle_url($this->_url); // Return a clone for safety.
454 * The list of alternate versions of this page.
455 * @return array mime type => object with ->url and ->title.
457 protected function magic_get_alternateversions() {
458 return $this->_alternateversions;
462 * Please do not call this method directly, use the ->blocks syntax. {@link __get()}.
463 * @return blocks_manager the blocks manager object for this page.
465 protected function magic_get_blocks() {
466 global $CFG;
467 if (is_null($this->_blocks)) {
468 if (!empty($CFG->blockmanagerclass)) {
469 $classname = $CFG->blockmanagerclass;
470 } else {
471 $classname = 'block_manager';
473 $this->_blocks = new $classname($this);
475 return $this->_blocks;
479 * Please do not call this method directly, use the ->requires syntax. {@link __get()}.
480 * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
482 protected function magic_get_requires() {
483 global $CFG;
484 if (is_null($this->_requires)) {
485 $this->_requires = new page_requirements_manager();
487 return $this->_requires;
491 * Please do not call this method directly, use the ->cacheable syntax. {@link __get()}.
492 * @return boolean can this page be cached by the user's browser.
494 protected function magic_get_cacheable() {
495 return $this->_cacheable;
499 * Please do not call this method directly, use the ->focuscontrol syntax. {@link __get()}.
500 * @return string the id of the HTML element to be focused when the page has loaded.
502 protected function magic_get_focuscontrol() {
503 return $this->_focuscontrol;
507 * Please do not call this method directly, use the ->button syntax. {@link __get()}.
508 * @return string the HTML to go where the Turn editing on button normally goes.
510 protected function magic_get_button() {
511 return $this->_button;
515 * Please do not call this method directly, use the ->theme syntax. {@link __get()}.
516 * @return theme_config the initialised theme for this page.
518 protected function magic_get_theme() {
519 if (is_null($this->_theme)) {
520 $this->initialise_theme_and_output();
522 return $this->_theme;
526 * Please do not call this method directly, use the ->legacythemeinuse syntax. {@link __get()}.
527 * @return bool
529 protected function magic_get_legacythemeinuse() {
530 return ($this->_legacythemeinuse);
534 * Please do not call this method directly use the ->periodicrefreshdelay syntax
535 * {@link __get()}
536 * @return int The periodic refresh delay to use with meta refresh
538 protected function magic_get_periodicrefreshdelay() {
539 return $this->_periodicrefreshdelay;
543 * Please do not call this method directly use the ->opencontainers syntax. {@link __get()}
544 * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
545 * mainly for internal use by the rendering code.
547 protected function magic_get_opencontainers() {
548 if (is_null($this->_opencontainers)) {
549 $this->_opencontainers = new xhtml_container_stack();
551 return $this->_opencontainers;
555 * Return the navigation object
556 * @return global_navigation
558 protected function magic_get_navigation() {
559 if ($this->_navigation === null) {
560 $this->_navigation = new global_navigation($this);
562 return $this->_navigation;
566 * Return a navbar object
567 * @return navbar
569 protected function magic_get_navbar() {
570 if ($this->_navbar === null) {
571 $this->_navbar = new navbar($this);
573 return $this->_navbar;
577 * Returns the settings navigation object
578 * @return settings_navigation
580 protected function magic_get_settingsnav() {
581 if ($this->_settingsnav === null) {
582 $this->_settingsnav = new settings_navigation($this);
583 $this->_settingsnav->initialise();
585 return $this->_settingsnav;
589 * PHP overloading magic to make the $PAGE->course syntax work by redirecting
590 * it to the corresponding $PAGE->magic_get_course() method if there is one, and
591 * throwing an exception if not.
593 * @param $name string property name
594 * @return mixed
596 public function __get($name) {
597 $getmethod = 'magic_get_' . $name;
598 if (method_exists($this, $getmethod)) {
599 return $this->$getmethod();
600 } else {
601 throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
606 * PHP overloading magic which prevents the $PAGE->context = $context; syntax
608 * @param $name string property name
609 * @param $name mixed value
610 * @return void, throws exception if field not defined in page class
612 public function __set($name, $value) {
613 if (method_exists($this, 'set_' . $name)) {
614 throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
615 } else {
616 throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
620 /// Other information getting methods ==========================================
623 * Returns instance of page renderer
624 * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
625 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
626 * @param string $target one of rendering target constants
627 * @return renderer_base
629 public function get_renderer($component, $subtype = null, $target = null) {
630 return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
634 * Checks to see if there are any items on the navbar object
635 * @return bool true if there are, false if not
637 public function has_navbar() {
638 if ($this->_navbar === null) {
639 $this->_navbar = new navbar($this);
641 return $this->_navbar->has_items();
645 * @return boolean should the current user see this page in editing mode.
646 * That is, are they allowed to edit this page, and are they currently in
647 * editing mode.
649 public function user_is_editing() {
650 global $USER;
651 return !empty($USER->editing) && $this->user_allowed_editing();
655 * @return boolean does the user have permission to edit blocks on this page.
657 public function user_can_edit_blocks() {
658 return has_capability($this->_blockseditingcap, $this->_context);
662 * @return boolean does the user have permission to see this page in editing mode.
664 public function user_allowed_editing() {
665 if ($this->_legacypageobject) {
666 return $this->_legacypageobject->user_allowed_editing();
668 return has_any_capability($this->all_editing_caps(), $this->_context);
672 * @return string a description of this page. Normally displayed in the footer in
673 * developer debug mode.
675 public function debug_summary() {
676 $summary = '';
677 $summary .= 'General type: ' . $this->pagelayout . '. ';
678 if (!during_initial_install()) {
679 $summary .= 'Context ' . print_context_name($this->_context) . ' (context id ' . $this->_context->id . '). ';
681 $summary .= 'Page type ' . $this->pagetype . '. ';
682 if ($this->subpage) {
683 'Sub-page ' . $this->subpage . '. ';
685 return $summary;
688 /// Setter methods =============================================================
691 * Set the state. The state must be one of that STATE_... constants, and
692 * the state is only allowed to advance one step at a time.
693 * @param integer $state the new state.
695 public function set_state($state) {
696 if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
697 throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
698 $this->_state . ' and state ' . $state . ' was requested.');
701 if ($state == self::STATE_PRINTING_HEADER) {
702 $this->starting_output();
705 $this->_state = $state;
709 * Set the current course. This sets both $PAGE->course and $COURSE. It also
710 * sets the right theme and locale.
712 * Normally you don't need to call this function yourself, require_login will
713 * call it for you if you pass a $course to it. You can use this function
714 * on pages that do need to call require_login().
716 * Sets $PAGE->context to the course context, if it is not already set.
718 * @param object the course to set as the global course.
720 public function set_course($course) {
721 global $COURSE, $PAGE;
723 if (empty($course->id)) {
724 throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
727 $this->ensure_theme_not_set();
729 if (!empty($this->_course->id) && $this->_course->id != $course->id) {
730 $this->_categories = null;
733 $this->_course = clone($course);
735 if ($this === $PAGE) {
736 $COURSE = $this->_course;
737 moodle_setlocale();
740 if (!$this->_context) {
741 $this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id));
746 * Set the main context to which this page belongs.
747 * @param object $context a context object, normally obtained with get_context_instance.
749 public function set_context($context) {
750 if ($context === null) {
751 // extremely ugly hack which sets context to some value in order to prevent warnings,
752 // use only for core error handling!!!!
753 if (!$this->_context) {
754 $this->_context = get_context_instance(CONTEXT_SYSTEM);
756 return;
759 // ideally we should set context only once
760 if (isset($this->_context)) {
761 if ($context->id == $this->_context->id) {
762 // fine - no change needed
763 } else if ($this->_context->contextlevel == CONTEXT_SYSTEM or $this->_context->contextlevel == CONTEXT_COURSE) {
764 // hmm - not ideal, but it might produce too many warnings due to the design of require_login
765 } else if ($this->_context->contextlevel == CONTEXT_MODULE and $this->_context->id == get_parent_contextid($context)) {
766 // hmm - most probably somebody did require_login() and after that set the block context
767 } else {
768 // we do not want devs to do weird switching of context levels on the fly,
769 // because we might have used the context already such as in text filter in page title
770 debugging('Coding problem: unsupported modification of PAGE->context from '.$this->_context->contextlevel.' to '.$context->contextlevel);
774 $this->_context = $context;
778 * The course module that this page belongs to (if it does belong to one).
780 * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
781 * @param stdClass $course
782 * @param stdClass $module
783 * @return void
785 public function set_cm($cm, $course = null, $module = null) {
786 global $DB;
788 if (!isset($cm->id) || !isset($cm->course)) {
789 throw new coding_exception('Invalid $cm parameter for $PAGE object, it has to be instance of cm_info or record from the course_modules table.');
792 if (!$this->_course || $this->_course->id != $cm->course) {
793 if (!$course) {
794 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
796 if ($course->id != $cm->course) {
797 throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
799 $this->set_course($course);
802 // make sure we have a $cm from get_fast_modinfo as this contains activity access details
803 if (!($cm instanceof cm_info)) {
804 $modinfo = get_fast_modinfo($this->_course);
805 $cm = $modinfo->get_cm($cm->id);
807 $this->_cm = $cm;
809 // unfortunately the context setting is a mess, let's try to work around some common block problems and show some debug messages
810 if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
811 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
812 $this->set_context($context);
815 if ($module) {
816 $this->set_activity_record($module);
821 * @param $module a row from the main database table for the module that this
822 * page belongs to. For example, if ->cm is a forum, then you can pass the
823 * corresponding row from the forum table here if you have it (saves a database
824 * query sometimes).
826 public function set_activity_record($module) {
827 if (is_null($this->_cm)) {
828 throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
830 if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
831 throw new coding_exception('The activity record your are trying to set does not seem to correspond to the cm that has been set.');
833 $this->_module = $module;
837 * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'. Normally
838 * you do not need to set this manually, it is automatically created from the
839 * script name. However, on some pages this is overridden. For example, the
840 * page type for course/view.php includes the course format, for example
841 * 'course-view-weeks'. This gets used as the id attribute on <body> and
842 * also for determining which blocks are displayed.
844 public function set_pagetype($pagetype) {
845 $this->_pagetype = $pagetype;
849 * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
850 * This properly defaults to 'base', so you only need to call this function if
851 * you want something different. The exact range of supported layouts is specified
852 * in the standard theme.
854 public function set_pagelayout($pagelayout) {
856 * Uncomment this to debug theme pagelayout issues like missing blocks.
858 * if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout) {
859 * debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
862 $this->_pagelayout = $pagelayout;
866 * If context->id and pagetype are not enough to uniquely identify this page,
867 * then you can set a subpage id as well. For example, the tags page sets
868 * @param string $subpage an arbitrary identifier that, along with context->id
869 * and pagetype, uniquely identifies this page.
871 public function set_subpage($subpage) {
872 if (empty($subpage)) {
873 $this->_subpage = '';
874 } else {
875 $this->_subpage = $subpage;
880 * @param string $class add this class name ot the class attribute on the body tag.
882 public function add_body_class($class) {
883 if ($this->_state > self::STATE_BEFORE_HEADER) {
884 throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
886 $this->_bodyclasses[$class] = 1;
890 * @param array $classes this utility method calls add_body_class for each array element.
892 public function add_body_classes($classes) {
893 foreach ($classes as $class) {
894 $this->add_body_class($class);
899 * @param string $title the title that should go in the <head> section of the HTML of this page.
901 public function set_title($title) {
902 $title = format_string($title);
903 $title = str_replace('"', '&quot;', $title);
904 $this->_title = $title;
908 * @param string $heading the main heading that should be displayed at the top of the <body>.
910 public function set_heading($heading) {
911 $this->_heading = format_string($heading);
915 * @param string $menu The menu/content to show in the heading
917 public function set_headingmenu($menu) {
918 $this->_headingmenu = $menu;
922 * Set the course category this page belongs to manually. This automatically
923 * sets $PAGE->course to be the site course. You cannot use this method if
924 * you have already set $PAGE->course - in that case, the category must be
925 * the one that the course belongs to. This also automatically sets the
926 * page context to the category context.
927 * @param integer $categoryid The id of the category to set.
929 public function set_category_by_id($categoryid) {
930 global $SITE, $DB;
931 if (!is_null($this->_course)) {
932 throw new coding_exception('Attempt to manually set the course category when the course has been set. This is not allowed.');
934 if (is_array($this->_categories)) {
935 throw new coding_exception('Course category has already been set. You are not allowed to change it.');
937 $this->ensure_theme_not_set();
938 $this->set_course($SITE);
939 $this->load_category($categoryid);
940 $this->set_context(get_context_instance(CONTEXT_COURSECAT, $categoryid));
944 * Set a different path to use for the 'Moodle docs for this page' link.
945 * By default, it uses the pagetype, which is normally the same as the
946 * script name. So, for example, for mod/quiz/attempt.php, pagetype is
947 * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
948 * @param string $path the path to use at the end of the moodle docs URL.
950 public function set_docs_path($path) {
951 $this->_docspath = $path;
955 * You should call this method from every page to set the cleaned-up URL
956 * that should be used to return to this page. Used, for example, by the
957 * blocks editing UI to know where to return the user after an action.
958 * For example, course/view.php does:
959 * $id = optional_param('id', 0, PARAM_INT);
960 * $PAGE->set_url('/course/view.php', array('id' => $id));
961 * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
962 * @param array $params parameters to add to the URL
964 public function set_url($url, array $params = null) {
965 global $CFG;
967 if (is_string($url)) {
968 if (strpos($url, 'http') === 0) {
969 // ok
970 } else if (strpos($url, '/') === 0) {
971 // we have to use httpswwwroot here, because of loginhttps pages
972 $url = $CFG->httpswwwroot . $url;
973 } else {
974 throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
978 $this->_url = new moodle_url($url, $params);
980 $fullurl = $this->_url->out_omit_querystring();
981 if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
982 debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
984 $shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
986 if (is_null($this->_pagetype)) {
987 $this->initialise_default_pagetype($shorturl);
989 if (!is_null($this->_legacypageobject)) {
990 $this->_legacypageobject->set_url($url, $params);
995 * Make sure page URL does not contain the given URL parameter.
997 * This should not be necessary if the script has called set_url properly.
998 * However, in some situations like the block editing actions; when the URL
999 * has been guessed, it will contain dangerous block-related actions.
1000 * Therefore, the blocks code calls this function to clean up such parameters
1001 * before doing any redirect.
1003 * @param string $param the name of the parameter to make sure is not in the
1004 * page URL.
1006 public function ensure_param_not_in_url($param) {
1007 $discard = $this->url; // Make sure $this->url is lazy-loaded;
1008 $this->_url->remove_params($param);
1012 * There can be alternate versions of some pages (for example an RSS feed version).
1013 * If such other version exist, call this method, and a link to the alternate
1014 * version will be included in the <head> of the page.
1016 * @param $title The title to give the alternate version.
1017 * @param $url The URL of the alternate version.
1018 * @param $mimetype The mime-type of the alternate version.
1020 public function add_alternate_version($title, $url, $mimetype) {
1021 if ($this->_state > self::STATE_BEFORE_HEADER) {
1022 throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
1024 $alt = new stdClass;
1025 $alt->title = $title;
1026 $alt->url = $url;
1027 $this->_alternateversions[$mimetype] = $alt;
1031 * Specify a form control should be focused when the page has loaded.
1033 * @param string $controlid the id of the HTML element to be focused.
1035 public function set_focuscontrol($controlid) {
1036 $this->_focuscontrol = $controlid;
1040 * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
1042 * @param string $html the HTML to display there.
1044 public function set_button($html) {
1045 $this->_button = $html;
1049 * Set the capability that allows users to edit blocks on this page. Normally
1050 * the default of 'moodle/site:manageblocks' is used, but a few pages like
1051 * the My Moodle page need to use a different capability like 'moodle/my:manageblocks'.
1052 * @param string $capability a capability.
1054 public function set_blocks_editing_capability($capability) {
1055 $this->_blockseditingcap = $capability;
1059 * Some pages let you turn editing on for reasons other than editing blocks.
1060 * If that is the case, you can pass other capabilities that let the user
1061 * edit this page here.
1062 * @param string|array $capability either a capability, or an array of capabilities.
1064 public function set_other_editing_capability($capability) {
1065 if (is_array($capability)) {
1066 $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
1067 } else {
1068 $this->_othereditingcaps[] = $capability;
1073 * @return boolean $cacheable can this page be cached by the user's browser.
1075 public function set_cacheable($cacheable) {
1076 $this->_cacheable = $cacheable;
1080 * Sets the page to periodically refresh
1082 * This function must be called before $OUTPUT->header has been called or
1083 * a coding exception will be thrown.
1085 * @param int $delay Sets the delay before refreshing the page, if set to null
1086 * refresh is cancelled
1088 public function set_periodic_refresh_delay($delay=null) {
1089 if ($this->_state > self::STATE_BEFORE_HEADER) {
1090 throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
1092 if ($delay===null) {
1093 $this->_periodicrefreshdelay = null;
1094 } else if (is_int($delay)) {
1095 $this->_periodicrefreshdelay = $delay;
1100 * Force this page to use a particular theme.
1102 * Please use this cautiously. It is only intended to be used by the themes selector admin page.
1104 * @param $themename the name of the theme to use.
1106 public function force_theme($themename) {
1107 $this->ensure_theme_not_set();
1108 $this->_theme = theme_config::load($themename);
1112 * This function indicates that current page requires the https
1113 * when $CFG->loginhttps enabled.
1115 * By using this function properly, we can ensure 100% https-ized pages
1116 * at our entire discretion (login, forgot_password, change_password)
1117 * @return void
1119 public function https_required() {
1120 global $CFG;
1122 if (!is_null($this->_url)) {
1123 throw new coding_exception('https_required() must be used before setting page url!');
1126 $this->ensure_theme_not_set();
1128 $this->_https_login_required = true;
1130 if (!empty($CFG->loginhttps)) {
1131 $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
1132 } else {
1133 $CFG->httpswwwroot = $CFG->wwwroot;
1138 * Makes sure that page previously marked with https_required()
1139 * is really using https://, if not it redirects to https://
1141 * @return void (may redirect to https://self)
1143 public function verify_https_required() {
1144 global $CFG, $FULLME;
1146 if (is_null($this->_url)) {
1147 throw new coding_exception('verify_https_required() must be called after setting page url!');
1150 if (!$this->_https_login_required) {
1151 throw new coding_exception('verify_https_required() must be called only after https_required()!');
1154 if (empty($CFG->loginhttps)) {
1155 // https not required, so stop checking
1156 return;
1159 if (strpos($this->_url, 'https://')) {
1160 // detect if incorrect PAGE->set_url() used, it is recommended to use root-relative paths there
1161 throw new coding_exception('Invalid page url specified, it must start with https:// for pages that set https_required()!');
1164 if (!empty($CFG->sslproxy)) {
1165 // it does not make much sense to use sslproxy and loginhttps at the same time
1166 return;
1169 // now the real test and redirect!
1170 if (strpos($FULLME, 'https:') !== 0) {
1171 // this may lead to infinite redirect on misconfigured sites, in that case use $CFG->loginhttps=0; in /config.php
1172 redirect($this->_url);
1176 /// Initialisation methods =====================================================
1177 /// These set various things up in a default way.
1180 * This method is called when the page first moves out of the STATE_BEFORE_HEADER
1181 * state. This is our last change to initialise things.
1183 protected function starting_output() {
1184 global $CFG;
1186 if (!during_initial_install()) {
1187 $this->blocks->load_blocks();
1188 if (empty($this->_block_actions_done)) {
1189 $this->_block_actions_done = true;
1190 if ($this->blocks->process_url_actions($this)) {
1191 redirect($this->url->out(false));
1194 $this->blocks->create_all_block_instances();
1197 // If maintenance mode is on, change the page header.
1198 if (!empty($CFG->maintenance_enabled)) {
1199 $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
1200 '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
1201 '</a> ' . $this->button);
1203 $title = $this->title;
1204 if ($title) {
1205 $title .= ' - ';
1207 $this->set_title($title . get_string('maintenancemode', 'admin'));
1208 } else {
1209 // Show the messaging popup if there are messages
1210 message_popup_window();
1213 $this->initialise_standard_body_classes();
1217 * Method for use by Moodle core to set up the theme. Do not
1218 * use this in your own code.
1220 * Make sure the right theme for this page is loaded. Tell our
1221 * blocks_manager about the theme block regions, and then, if
1222 * we are $PAGE, set up the global $OUTPUT.
1224 public function initialise_theme_and_output() {
1225 global $OUTPUT, $PAGE, $SITE;
1227 if (!empty($this->_wherethemewasinitialised)) {
1228 return;
1231 if (!during_initial_install()) {
1232 // detect PAGE->context mess
1233 $this->magic_get_context();
1236 if (!$this->_course && !during_initial_install()) {
1237 $this->set_course($SITE);
1240 if (is_null($this->_theme)) {
1241 $themename = $this->resolve_theme();
1242 $this->_theme = theme_config::load($themename);
1243 $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
1246 $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
1248 if ($this === $PAGE) {
1249 $OUTPUT = $this->get_renderer('core');
1252 $this->_wherethemewasinitialised = debug_backtrace();
1256 * Work out the theme this page should use.
1258 * This depends on numerous $CFG settings, and the properties of this page.
1260 * @return string the name of the theme that should be used on this page.
1262 protected function resolve_theme() {
1263 global $CFG, $USER, $SESSION;
1265 if (empty($CFG->themeorder)) {
1266 $themeorder = array('course', 'category', 'session', 'user', 'site');
1267 } else {
1268 $themeorder = $CFG->themeorder;
1269 // Just in case, make sure we always use the site theme if nothing else matched.
1270 $themeorder[] = 'site';
1273 $mnetpeertheme = '';
1274 if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id) {
1275 require_once($CFG->dirroot.'/mnet/peer.php');
1276 $mnetpeer = new mnet_peer();
1277 $mnetpeer->set_id($USER->mnethostid);
1278 if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
1279 $mnetpeertheme = $mnetpeer->theme;
1283 $theme = '';
1284 foreach ($themeorder as $themetype) {
1285 switch ($themetype) {
1286 case 'course':
1287 if (!empty($CFG->allowcoursethemes) and !empty($this->course->theme)) {
1288 return $this->course->theme;
1291 case 'category':
1292 if (!empty($CFG->allowcategorythemes)) {
1293 $categories = $this->categories;
1294 foreach ($categories as $category) {
1295 if (!empty($category->theme)) {
1296 return $category->theme;
1301 case 'session':
1302 if (!empty($SESSION->theme)) {
1303 return $SESSION->theme;
1306 case 'user':
1307 if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) {
1308 if ($mnetpeertheme) {
1309 return $mnetpeertheme;
1310 } else {
1311 return $USER->theme;
1315 case 'site':
1316 if ($mnetpeertheme) {
1317 return $mnetpeertheme;
1318 } else if(!empty($CFG->themelegacy) && $this->browser_is_outdated()) {
1319 $this->_legacythemeinuse = true;
1320 return $CFG->themelegacy;
1321 } else {
1322 return $CFG->theme;
1329 * Determines whether the current browser should
1330 * default to the admin-selected legacy theme
1332 * @return true if legacy theme should be used, otherwise false
1335 protected function browser_is_outdated() {
1336 foreach($this->_legacybrowsers as $browser => $version) {
1337 // Check the browser is valid first then that its version is suitable
1338 if(check_browser_version($browser, '0') &&
1339 !check_browser_version($browser, $version)) {
1340 return true;
1343 return false;
1347 * Sets ->pagetype from the script name. For example, if the script that was
1348 * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
1349 * @param string $script the path to the script that should be used to
1350 * initialise ->pagetype. If not passed the $SCRIPT global will be used.
1351 * If legacy code has set $CFG->pagepath that will be used instead, and a
1352 * developer warning issued.
1354 protected function initialise_default_pagetype($script = null) {
1355 global $CFG, $SCRIPT;
1357 if (isset($CFG->pagepath)) {
1358 debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
1359 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
1360 $script = $CFG->pagepath;
1361 unset($CFG->pagepath);
1364 if (is_null($script)) {
1365 $script = ltrim($SCRIPT, '/');
1366 $len = strlen($CFG->admin);
1367 if (substr($script, 0, $len) == $CFG->admin) {
1368 $script = 'admin' . substr($script, $len);
1372 $path = str_replace('.php', '', $script);
1373 if (substr($path, -1) == '/') {
1374 $path .= 'index';
1377 if (empty($path) || $path == 'index') {
1378 $this->_pagetype = 'site-index';
1379 } else {
1380 $this->_pagetype = str_replace('/', '-', $path);
1384 protected function initialise_standard_body_classes() {
1385 global $CFG, $USER;
1387 $pagetype = $this->pagetype;
1388 if ($pagetype == 'site-index') {
1389 $this->_legacyclass = 'course';
1390 } else if (substr($pagetype, 0, 6) == 'admin-') {
1391 $this->_legacyclass = 'admin';
1393 $this->add_body_class($this->_legacyclass);
1395 $pathbits = explode('-', trim($pagetype));
1396 for ($i=1;$i<count($pathbits);$i++) {
1397 $this->add_body_class('path-'.join('-',array_slice($pathbits, 0, $i)));
1400 $this->add_body_classes(get_browser_version_classes());
1401 $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
1402 $this->add_body_class('lang-' . current_language());
1403 $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
1404 $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
1405 $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
1407 $this->add_body_class('pagelayout-' . $this->_pagelayout); // extra class describing current page layout
1409 if (!during_initial_install()) {
1410 $this->add_body_class('course-' . $this->_course->id);
1411 $this->add_body_class('context-' . $this->_context->id);
1414 if (!empty($this->_cm)) {
1415 $this->add_body_class('cmid-' . $this->_cm->id);
1418 if (!empty($CFG->allowcategorythemes)) {
1419 $this->ensure_category_loaded();
1420 foreach ($this->_categories as $catid => $notused) {
1421 $this->add_body_class('category-' . $catid);
1423 } else {
1424 $catid = 0;
1425 if (is_array($this->_categories)) {
1426 $catids = array_keys($this->_categories);
1427 $catid = reset($catids);
1428 } else if (!empty($this->_course->category)) {
1429 $catid = $this->_course->category;
1431 if ($catid) {
1432 $this->add_body_class('category-' . $catid);
1436 if (!isloggedin()) {
1437 $this->add_body_class('notloggedin');
1440 if (!empty($USER->editing)) {
1441 $this->add_body_class('editing');
1442 if (optional_param('bui_moveid', false, PARAM_INT)) {
1443 $this->add_body_class('blocks-moving');
1447 if (!empty($CFG->blocksdrag)) {
1448 $this->add_body_class('drag');
1451 if ($this->_legacythemeinuse) {
1452 $this->add_body_class('legacytheme');
1456 protected function load_activity_record() {
1457 global $DB;
1458 if (is_null($this->_cm)) {
1459 return;
1461 $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
1464 protected function ensure_category_loaded() {
1465 if (is_array($this->_categories)) {
1466 return; // Already done.
1468 if (is_null($this->_course)) {
1469 throw new coding_exception('Attempt to get the course category for this page before the course was set.');
1471 if ($this->_course->category == 0) {
1472 $this->_categories = array();
1473 } else {
1474 $this->load_category($this->_course->category);
1478 protected function load_category($categoryid) {
1479 global $DB;
1480 $category = $DB->get_record('course_categories', array('id' => $categoryid));
1481 if (!$category) {
1482 throw new moodle_exception('unknowncategory');
1484 $this->_categories[$category->id] = $category;
1485 $parentcategoryids = explode('/', trim($category->path, '/'));
1486 array_pop($parentcategoryids);
1487 foreach (array_reverse($parentcategoryids) as $catid) {
1488 $this->_categories[$catid] = null;
1492 protected function ensure_categories_loaded() {
1493 global $DB;
1494 $this->ensure_category_loaded();
1495 if (!is_null(end($this->_categories))) {
1496 return; // Already done.
1498 $idstoload = array_keys($this->_categories);
1499 array_shift($idstoload);
1500 $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
1501 foreach ($idstoload as $catid) {
1502 $this->_categories[$catid] = $categories[$catid];
1506 protected function ensure_theme_not_set() {
1507 if (!is_null($this->_theme)) {
1508 throw new coding_exception('The theme has already been set up for this page ready for output. ' .
1509 'Therefore, you can no longer change the theme, or anything that might affect what ' .
1510 'the current theme is, for example, the course.',
1511 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
1515 protected function url_to_class_name($url) {
1516 $bits = parse_url($url);
1517 $class = str_replace('.', '-', $bits['host']);
1518 if (!empty($bits['port'])) {
1519 $class .= '--' . $bits['port'];
1521 if (!empty($bits['path'])) {
1522 $path = trim($bits['path'], '/');
1523 if ($path) {
1524 $class .= '--' . str_replace('/', '-', $path);
1527 return $class;
1530 protected function all_editing_caps() {
1531 $caps = $this->_othereditingcaps;
1532 $caps[] = $this->_blockseditingcap;
1533 return $caps;
1536 /// Deprecated fields and methods for backwards compatibility ==================
1539 * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
1540 * @return string page type.
1542 public function get_type() {
1543 debugging('Call to deprecated method moodle_page::get_type. Please use $PAGE->pagetype instead.');
1544 return $this->get_pagetype();
1548 * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
1549 * @return string this is what page_id_and_class used to return via the $getclass parameter.
1551 function get_format_name() {
1552 return $this->get_pagetype();
1556 * @deprecated since Moodle 2.0 - use $PAGE->course instead.
1557 * @return object course.
1559 public function get_courserecord() {
1560 debugging('Call to deprecated method moodle_page::get_courserecord. Please use $PAGE->course instead.');
1561 return $this->get_course();
1565 * @deprecated since Moodle 2.0
1566 * @return string this is what page_id_and_class used to return via the $getclass parameter.
1568 public function get_legacyclass() {
1569 if (is_null($this->_legacyclass)) {
1570 $this->initialise_standard_body_classes();
1572 debugging('Call to deprecated method moodle_page::get_legacyclass.');
1573 return $this->_legacyclass;
1577 * @deprecated since Moodle 2.0 - use $PAGE->blocks->get_regions() instead
1578 * @return string the places on this page where blocks can go.
1580 function blocks_get_positions() {
1581 debugging('Call to deprecated method moodle_page::blocks_get_positions. Use $PAGE->blocks->get_regions() instead.');
1582 return $this->blocks->get_regions();
1586 * @deprecated since Moodle 2.0 - use $PAGE->blocks->get_default_region() instead
1587 * @return string the default place for blocks on this page.
1589 function blocks_default_position() {
1590 debugging('Call to deprecated method moodle_page::blocks_default_position. Use $PAGE->blocks->get_default_region() instead.');
1591 return $this->blocks->get_default_region();
1595 * @deprecated since Moodle 2.0 - no longer used.
1597 function blocks_get_default() {
1598 debugging('Call to deprecated method moodle_page::blocks_get_default. This method has no function any more.');
1602 * @deprecated since Moodle 2.0 - no longer used.
1604 function blocks_move_position(&$instance, $move) {
1605 debugging('Call to deprecated method moodle_page::blocks_move_position. This method has no function any more.');
1609 * @deprecated since Moodle 2.0 - use $this->url->params() instead.
1610 * @return array URL parameters for this page.
1612 function url_get_parameters() {
1613 debugging('Call to deprecated method moodle_page::url_get_parameters. Use $this->url->params() instead.');
1614 return $this->url->params();
1618 * @deprecated since Moodle 2.0 - use $this->url->params() instead.
1619 * @return string URL for this page without parameters.
1621 function url_get_path() {
1622 debugging('Call to deprecated method moodle_page::url_get_path. Use $this->url->out() instead.');
1623 return $this->url->out();
1627 * @deprecated since Moodle 2.0 - use $this->url->out() instead.
1628 * @return string full URL for this page.
1630 function url_get_full($extraparams = array()) {
1631 debugging('Call to deprecated method moodle_page::url_get_full. Use $this->url->out() instead.');
1632 return $this->url->out(true, $extraparams);
1636 * @deprecated since Moodle 2.0 - just a backwards compatibility hook.
1638 function set_legacy_page_object($pageobject) {
1639 return $this->_legacypageobject = $pageobject;
1643 * @deprecated since Moodle 2.0 - page objects should no longer be doing print_header.
1644 * @param $_,...
1646 function print_header($_) {
1647 if (is_null($this->_legacypageobject)) {
1648 throw new coding_exception('You have called print_header on $PAGE when there is not a legacy page class present.');
1650 debugging('You should not longer be doing print_header via a page class.', DEBUG_DEVELOPER);
1651 $args = func_get_args();
1652 call_user_func_array(array($this->_legacypageobject, 'print_header'), $args);
1656 * @deprecated since Moodle 2.0
1657 * @return the 'page id'. This concept no longer exists.
1659 function get_id() {
1660 debugging('Call to deprecated method moodle_page::get_id(). It should not be necessary any more.', DEBUG_DEVELOPER);
1661 if (!is_null($this->_legacypageobject)) {
1662 return $this->_legacypageobject->get_id();
1664 return 0;
1668 * @deprecated since Moodle 2.0
1669 * @return the 'page id'. This concept no longer exists.
1671 function get_pageid() {
1672 debugging('Call to deprecated method moodle_page::get_pageid(). It should not be necessary any more.', DEBUG_DEVELOPER);
1673 if (!is_null($this->_legacypageobject)) {
1674 return $this->_legacypageobject->get_id();
1676 return 0;
1680 * @deprecated since Moodle 2.0 - user $PAGE->cm instead.
1681 * @return $this->cm;
1683 function get_modulerecord() {
1684 return $this->cm;
1687 public function has_set_url() {
1688 return ($this->_url!==null);
1691 public function set_block_actions_done($setting = true) {
1692 $this->_block_actions_done = $setting;
1696 * Are popup notifications allowed on this page?
1697 * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
1698 * @return boolean true if popup notifications may be displayed
1700 public function get_popup_notification_allowed() {
1701 return $this->_popup_notification_allowed;
1705 * Allow or disallow popup notifications on this page. Popups are allowed by default.
1706 * @param boolean true if notifications are allowed. False if not allowed. They are allowed by default.
1707 * @return null
1709 public function set_popup_notification_allowed($allowed) {
1710 $this->_popup_notification_allowed = $allowed;
1715 * @deprecated since Moodle 2.0
1716 * Not needed any more.
1717 * @param $path the folder path
1718 * @return array an array of page types.
1720 function page_import_types($path) {
1721 global $CFG;
1722 debugging('Call to deprecated function page_import_types.', DEBUG_DEVELOPER);
1726 * @deprecated since Moodle 2.0
1727 * Do not use this any more. The global $PAGE is automatically created for you.
1728 * If you need custom behaviour, you should just set properties of that object.
1729 * @param integer $instance legacy page instance id.
1730 * @return the global $PAGE object.
1732 function page_create_instance($instance) {
1733 global $PAGE;
1734 return page_create_object($PAGE->pagetype, $instance);
1738 * @deprecated since Moodle 2.0
1739 * Do not use this any more. The global $PAGE is automatically created for you.
1740 * If you need custom behaviour, you should just set properties of that object.
1742 function page_create_object($type, $id = NULL) {
1743 global $CFG, $PAGE, $SITE, $ME;
1744 debugging('Call to deprecated function page_create_object.', DEBUG_DEVELOPER);
1746 $data = new stdClass;
1747 $data->pagetype = $type;
1748 $data->pageid = $id;
1750 $classname = page_map_class($type);
1751 if (!$classname) {
1752 return $PAGE;
1754 $legacypage = new $classname;
1755 $legacypage->init_quick($data);
1757 $course = $PAGE->course;
1758 if ($course->id != $SITE->id) {
1759 $legacypage->set_course($course);
1760 } else {
1761 try {
1762 $category = $PAGE->category;
1763 } catch (coding_exception $e) {
1764 // Was not set before, so no need to try to set it again.
1765 $category = false;
1767 if ($category) {
1768 $legacypage->set_category_by_id($category->id);
1769 } else {
1770 $legacypage->set_course($SITE);
1774 $legacypage->set_pagetype($type);
1776 $legacypage->set_url($ME);
1777 $PAGE->set_url(str_replace($CFG->wwwroot . '/', '', $legacypage->url_get_full()));
1779 $PAGE->set_pagetype($type);
1780 $PAGE->set_legacy_page_object($legacypage);
1781 return $PAGE;
1785 * @deprecated since Moodle 2.0
1786 * You should not be writing page subclasses any more. Just set properties on the
1787 * global $PAGE object to control its behaviour.
1789 function page_map_class($type, $classname = NULL) {
1790 global $CFG;
1792 static $mappings = array(
1793 PAGE_COURSE_VIEW => 'page_course',
1796 if (!empty($type) && !empty($classname)) {
1797 $mappings[$type] = $classname;
1800 if (!isset($mappings[$type])) {
1801 debugging('Page class mapping requested for unknown type: '.$type);
1802 return null;
1803 } else if (empty($classname) && !class_exists($mappings[$type])) {
1804 debugging('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined');
1805 return null;
1808 return $mappings[$type];
1812 * @deprecated since Moodle 2.0
1813 * Parent class from which all Moodle page classes derive
1815 * @package core
1816 * @subpackage lib
1817 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1818 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1820 class page_base extends moodle_page {
1822 * The numeric identifier of the page being described.
1823 * @var int $id
1825 var $id = NULL;
1827 /// Class Functions
1829 // HTML OUTPUT SECTION
1831 // SELF-REPORTING SECTION
1833 // Simple stuff, do not override this.
1834 function get_id() {
1835 return $this->id;
1838 // Initialize the data members of the parent class
1839 function init_quick($data) {
1840 $this->id = $data->pageid;
1843 function init_full() {
1848 * @deprecated since Moodle 2.0
1849 * Class that models the behavior of a moodle course.
1850 * Although this does nothing, this class declaration should be left for now
1851 * since there may be legacy class doing class page_... extends page_course
1853 * @package core
1854 * @subpackage lib
1855 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1856 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1858 class page_course extends page_base {
1862 * @deprecated since Moodle 2.0
1863 * Class that models the common parts of all activity modules
1865 * @package core
1866 * @subpackage lib
1867 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1868 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1870 class page_generic_activity extends page_base {
1871 // Although this function is deprecated, it should be left here because
1872 // people upgrading legacy code need to copy it. See
1873 // http://docs.moodle.org/en/Development:Migrating_your_code_code_to_the_2.0_rendering_API
1874 function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
1875 global $USER, $CFG, $PAGE, $OUTPUT;
1877 $this->init_full();
1878 $replacements = array(
1879 '%fullname%' => format_string($this->activityrecord->name)
1881 foreach ($replacements as $search => $replace) {
1882 $title = str_replace($search, $replace, $title);
1885 $buttons = '<table><tr><td>'.$OUTPUT->update_module_button($this->modulerecord->id, $this->activityname).'</td>';
1886 if ($this->user_allowed_editing()) {
1887 $buttons .= '<td><form method="get" action="view.php"><div>'.
1888 '<input type="hidden" name="id" value="'.$this->modulerecord->id.'" />'.
1889 '<input type="hidden" name="edit" value="'.($this->user_is_editing()?'off':'on').'" />'.
1890 '<input type="submit" value="'.get_string($this->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td>';
1892 $buttons .= '</tr></table>';
1894 if (!empty($morenavlinks) && is_array($morenavlinks)) {
1895 foreach ($morenavlinks as $navitem) {
1896 if (is_array($navitem) && array_key_exists('name', $navitem)) {
1897 $link = null;
1898 if (array_key_exists('link', $navitem)) {
1899 $link = $navitem['link'];
1901 $PAGE->navbar->add($navitem['name'], $link);
1906 $PAGE->set_title($title);
1907 $PAGE->set_heading($this->course->fullname);
1908 $PAGE->set_button($buttons);
1909 echo $OUTPUT->header();