PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pagelib.php

http://github.com/moodle/moodle
PHP | 2084 lines | 916 code | 221 blank | 947 comment | 213 complexity | c759c903708dfec207e222d0c0399448 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains the moodle_page class. There is normally a single instance
  18. * of this class in the $PAGE global variable. This class is a central repository
  19. * of information about the page we are building up to send back to the user.
  20. *
  21. * @package core
  22. * @category page
  23. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. /**
  28. * $PAGE is a central store of information about the current page we are
  29. * generating in response to the user's request.
  30. *
  31. * It does not do very much itself
  32. * except keep track of information, however, it serves as the access point to
  33. * some more significant components like $PAGE->theme, $PAGE->requires,
  34. * $PAGE->blocks, etc.
  35. *
  36. * @copyright 2009 Tim Hunt
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. * @since Moodle 2.0
  39. * @package core
  40. * @category page
  41. *
  42. * The following properties are alphabetical. Please keep it that way so that its
  43. * easy to maintain.
  44. *
  45. * @property-read string $activityname The type of activity we are in, for example 'forum' or 'quiz'.
  46. * Will be null if this page is not within a module.
  47. * @property-read stdClass $activityrecord The row from the activities own database table (for example
  48. * the forum or quiz table) that this page belongs to. Will be null
  49. * if this page is not within a module.
  50. * @property-read array $alternativeversions Mime type => object with ->url and ->title.
  51. * @property-read block_manager $blocks The blocks manager object for this page.
  52. * @property-read array $blockmanipulations
  53. * @property-read string $bodyclasses A string to use within the class attribute on the body tag.
  54. * @property-read string $bodyid A string to use as the id of the body tag.
  55. * @property-read string $button The HTML to go where the Turn editing on button normally goes.
  56. * @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
  57. * @property-read array $categories An array of all the categories the page course belongs to,
  58. * starting with the immediately containing category, and working out to
  59. * the top-level category. This may be the empty array if we are in the
  60. * front page course.
  61. * @property-read mixed $category The category that the page course belongs to.
  62. * @property-read cm_info $cm The course_module that this page belongs to. Will be null
  63. * if this page is not within a module. This is a full cm object, as loaded
  64. * by get_coursemodule_from_id or get_coursemodule_from_instance,
  65. * so the extra modname and name fields are present.
  66. * @property-read context $context The main context to which this page belongs.
  67. * @property-read stdClass $course The current course that we are inside - a row from the
  68. * course table. (Also available as $COURSE global.) If we are not inside
  69. * an actual course, this will be the site course.
  70. * @property-read string $devicetypeinuse The name of the device type in use
  71. * @property-read string $docspath The path to the Moodle docs for this page.
  72. * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
  73. * @property-read bool $headerprinted True if the page header has already been printed.
  74. * @property-read string $heading The main heading that should be displayed at the top of the <body>.
  75. * @property-read string $headingmenu The menu (or actions) to display in the heading
  76. * @property-read array $layout_options An arrays with options for the layout file.
  77. * @property-read array $legacythemeinuse True if the legacy browser theme is in use.
  78. * @property-read navbar $navbar The navbar object used to display the navbar
  79. * @property-read global_navigation $navigation The navigation structure for this page.
  80. * @property-read xhtml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
  81. * mainly for internal use by the rendering code.
  82. * @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
  83. * Allows the theme to display things differently, if it wishes to.
  84. * @property-read string $pagetype The page type string, should be used as the id for the body tag in the theme.
  85. * @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
  86. * @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
  87. * @property-read string $requestip The IP address of the current request, null if unknown.
  88. * @property-read string $requestorigin The type of request 'web', 'ws', 'cli', 'restore', etc.
  89. * @property-read settings_navigation $settingsnav The settings navigation
  90. * @property-read int $state One of the STATE_... constants
  91. * @property-read string $subpage The subpage identifier, if any.
  92. * @property-read theme_config $theme The theme for this page.
  93. * @property-read string $title The title that should go in the <head> section of the HTML of this page.
  94. * @property-read moodle_url $url The moodle url object for this page.
  95. */
  96. class moodle_page {
  97. /** The state of the page before it has printed the header **/
  98. const STATE_BEFORE_HEADER = 0;
  99. /** The state the page is in temporarily while the header is being printed **/
  100. const STATE_PRINTING_HEADER = 1;
  101. /** The state the page is in while content is presumably being printed **/
  102. const STATE_IN_BODY = 2;
  103. /**
  104. * The state the page is when the footer has been printed and its function is
  105. * complete.
  106. */
  107. const STATE_DONE = 3;
  108. /**
  109. * @var int The current state of the page. The state a page is within
  110. * determines what actions are possible for it.
  111. */
  112. protected $_state = self::STATE_BEFORE_HEADER;
  113. /**
  114. * @var stdClass The course currently associated with this page.
  115. * If not has been provided the front page course is used.
  116. */
  117. protected $_course = null;
  118. /**
  119. * @var cm_info If this page belongs to a module, this is the cm_info module
  120. * description object.
  121. */
  122. protected $_cm = null;
  123. /**
  124. * @var stdClass If $_cm is not null, then this will hold the corresponding
  125. * row from the modname table. For example, if $_cm->modname is 'quiz', this
  126. * will be a row from the quiz table.
  127. */
  128. protected $_module = null;
  129. /**
  130. * @var context The context that this page belongs to.
  131. */
  132. protected $_context = null;
  133. /**
  134. * @var array This holds any categories that $_course belongs to, starting with the
  135. * particular category it belongs to, and working out through any parent
  136. * categories to the top level. These are loaded progressively, if needed.
  137. * There are three states. $_categories = null initially when nothing is
  138. * loaded; $_categories = array($id => $cat, $parentid => null) when we have
  139. * loaded $_course->category, but not any parents; and a complete array once
  140. * everything is loaded.
  141. */
  142. protected $_categories = null;
  143. /**
  144. * @var array An array of CSS classes that should be added to the body tag in HTML.
  145. */
  146. protected $_bodyclasses = array();
  147. /**
  148. * @var string The title for the page. Used within the title tag in the HTML head.
  149. */
  150. protected $_title = '';
  151. /**
  152. * @var string The string to use as the heading of the page. Shown near the top of the
  153. * page within most themes.
  154. */
  155. protected $_heading = '';
  156. /**
  157. * @var string The pagetype is used to describe the page and defaults to a representation
  158. * of the physical path to the page e.g. my-index, mod-quiz-attempt
  159. */
  160. protected $_pagetype = null;
  161. /**
  162. * @var string The pagelayout to use when displaying this page. The
  163. * pagelayout needs to have been defined by the theme in use, or one of its
  164. * parents. By default base is used however standard is the more common layout.
  165. * Note that this gets automatically set by core during operations like
  166. * require_login.
  167. */
  168. protected $_pagelayout = 'base';
  169. /**
  170. * @var array List of theme layout options, these are ignored by core.
  171. * To be used in individual theme layout files only.
  172. */
  173. protected $_layout_options = null;
  174. /**
  175. * @var string An optional arbitrary parameter that can be set on pages where the context
  176. * and pagetype is not enough to identify the page.
  177. */
  178. protected $_subpage = '';
  179. /**
  180. * @var string Set a different path to use for the 'Moodle docs for this page' link.
  181. * By default, it uses the path of the file for instance mod/quiz/attempt.
  182. */
  183. protected $_docspath = null;
  184. /**
  185. * @var string A legacy class that will be added to the body tag
  186. */
  187. protected $_legacyclass = null;
  188. /**
  189. * @var moodle_url The URL for this page. This is mandatory and must be set
  190. * before output is started.
  191. */
  192. protected $_url = null;
  193. /**
  194. * @var array An array of links to alternative versions of this page.
  195. * Primarily used for RSS versions of the current page.
  196. */
  197. protected $_alternateversions = array();
  198. /**
  199. * @var block_manager The blocks manager for this page. It is responsible for
  200. * the blocks and there content on this page.
  201. */
  202. protected $_blocks = null;
  203. /**
  204. * @var page_requirements_manager Page requirements manager. It is responsible
  205. * for all JavaScript and CSS resources required by this page.
  206. */
  207. protected $_requires = null;
  208. /** @var page_requirements_manager Saves the requirement manager object used before switching to to fragments one. */
  209. protected $savedrequires = null;
  210. /**
  211. * @var string The capability required by the user in order to edit blocks
  212. * and block settings on this page.
  213. */
  214. protected $_blockseditingcap = 'moodle/site:manageblocks';
  215. /**
  216. * @var bool An internal flag to record when block actions have been processed.
  217. * Remember block actions occur on the current URL and it is important that
  218. * even they are never executed more than once.
  219. */
  220. protected $_block_actions_done = false;
  221. /**
  222. * @var array An array of any other capabilities the current user must have
  223. * in order to editing the page and/or its content (not just blocks).
  224. */
  225. protected $_othereditingcaps = array();
  226. /**
  227. * @var bool Sets whether this page should be cached by the browser or not.
  228. * If it is set to true (default) the page is served with caching headers.
  229. */
  230. protected $_cacheable = true;
  231. /**
  232. * @var string Can be set to the ID of an element on the page, if done that
  233. * element receives focus when the page loads.
  234. */
  235. protected $_focuscontrol = '';
  236. /**
  237. * @var string HTML to go where the turn on editing button is located. This
  238. * is nearly a legacy item and not used very often any more.
  239. */
  240. protected $_button = '';
  241. /**
  242. * @var theme_config The theme to use with this page. This has to be properly
  243. * initialised via {@link moodle_page::initialise_theme_and_output()} which
  244. * happens magically before any operation that requires it.
  245. */
  246. protected $_theme = null;
  247. /**
  248. * @var global_navigation Contains the global navigation structure.
  249. */
  250. protected $_navigation = null;
  251. /**
  252. * @var settings_navigation Contains the settings navigation structure.
  253. */
  254. protected $_settingsnav = null;
  255. /**
  256. * @var flat_navigation Contains a list of nav nodes, most closely related to the current page.
  257. */
  258. protected $_flatnav = null;
  259. /**
  260. * @var navbar Contains the navbar structure.
  261. */
  262. protected $_navbar = null;
  263. /**
  264. * @var string The menu (or actions) to display in the heading.
  265. */
  266. protected $_headingmenu = null;
  267. /**
  268. * @var array stack trace. Then the theme is initialised, we save the stack
  269. * trace, for use in error messages.
  270. */
  271. protected $_wherethemewasinitialised = null;
  272. /**
  273. * @var xhtml_container_stack Tracks XHTML tags on this page that have been
  274. * opened but not closed.
  275. */
  276. protected $_opencontainers;
  277. /**
  278. * @var int Sets the page to refresh after a given delay (in seconds) using
  279. * meta refresh in {@link standard_head_html()} in outputlib.php
  280. * If set to null(default) the page is not refreshed
  281. */
  282. protected $_periodicrefreshdelay = null;
  283. /**
  284. * @var array Associative array of browser shortnames (as used by check_browser_version)
  285. * and their minimum required versions
  286. */
  287. protected $_legacybrowsers = array('MSIE' => 6.0);
  288. /**
  289. * @var string Is set to the name of the device type in use.
  290. * This will we worked out when it is first used.
  291. */
  292. protected $_devicetypeinuse = null;
  293. /**
  294. * @var bool Used to determine if HTTPS should be required for login.
  295. */
  296. protected $_https_login_required = false;
  297. /**
  298. * @var bool Determines if popup notifications allowed on this page.
  299. * Code such as the quiz module disables popup notifications in situations
  300. * such as upgrading or completing a quiz.
  301. */
  302. protected $_popup_notification_allowed = true;
  303. /**
  304. * @var bool Is the settings menu being forced to display on this page (activities / resources only).
  305. * This is only used by themes that use the settings menu.
  306. */
  307. protected $_forcesettingsmenu = false;
  308. /**
  309. * @var array Array of header actions HTML to add to the page header actions menu.
  310. */
  311. protected $_headeractions = [];
  312. /**
  313. * @var bool Should the region main settings menu be rendered in the header.
  314. */
  315. protected $_regionmainsettingsinheader = false;
  316. /**
  317. * Force the settings menu to be displayed on this page. This will only force the
  318. * settings menu on an activity / resource page that is being displayed on a theme that
  319. * uses a settings menu.
  320. *
  321. * @param bool $forced default of true, can be sent false to turn off the force.
  322. */
  323. public function force_settings_menu($forced = true) {
  324. $this->_forcesettingsmenu = $forced;
  325. }
  326. /**
  327. * Check to see if the settings menu is forced to display on this activity / resource page.
  328. * This only applies to themes that use the settings menu.
  329. *
  330. * @return bool True if the settings menu is forced to display.
  331. */
  332. public function is_settings_menu_forced() {
  333. return $this->_forcesettingsmenu;
  334. }
  335. // Magic getter methods =============================================================
  336. // Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
  337. // methods, but instead use the $PAGE->x syntax.
  338. /**
  339. * Please do not call this method directly, use the ->state syntax. {@link moodle_page::__get()}.
  340. * @return integer one of the STATE_XXX constants. You should not normally need
  341. * to use this in your code. It is intended for internal use by this class
  342. * and its friends like print_header, to check that everything is working as
  343. * expected. Also accessible as $PAGE->state.
  344. */
  345. protected function magic_get_state() {
  346. return $this->_state;
  347. }
  348. /**
  349. * Please do not call this method directly, use the ->headerprinted syntax. {@link moodle_page::__get()}.
  350. * @return bool has the header already been printed?
  351. */
  352. protected function magic_get_headerprinted() {
  353. return $this->_state >= self::STATE_IN_BODY;
  354. }
  355. /**
  356. * Please do not call this method directly, use the ->course syntax. {@link moodle_page::__get()}.
  357. * @return stdClass the current course that we are inside - a row from the
  358. * course table. (Also available as $COURSE global.) If we are not inside
  359. * an actual course, this will be the site course.
  360. */
  361. protected function magic_get_course() {
  362. global $SITE;
  363. if (is_null($this->_course)) {
  364. return $SITE;
  365. }
  366. return $this->_course;
  367. }
  368. /**
  369. * Please do not call this method directly, use the ->cm syntax. {@link moodle_page::__get()}.
  370. * @return cm_info the course_module that this page belongs to. Will be null
  371. * if this page is not within a module. This is a full cm object, as loaded
  372. * by get_coursemodule_from_id or get_coursemodule_from_instance,
  373. * so the extra modname and name fields are present.
  374. */
  375. protected function magic_get_cm() {
  376. return $this->_cm;
  377. }
  378. /**
  379. * Please do not call this method directly, use the ->activityrecord syntax. {@link moodle_page::__get()}.
  380. * @return stdClass the row from the activities own database table (for example
  381. * the forum or quiz table) that this page belongs to. Will be null
  382. * if this page is not within a module.
  383. */
  384. protected function magic_get_activityrecord() {
  385. if (is_null($this->_module) && !is_null($this->_cm)) {
  386. $this->load_activity_record();
  387. }
  388. return $this->_module;
  389. }
  390. /**
  391. * Please do not call this method directly, use the ->activityname syntax. {@link moodle_page::__get()}.
  392. * @return string the The type of activity we are in, for example 'forum' or 'quiz'.
  393. * Will be null if this page is not within a module.
  394. */
  395. protected function magic_get_activityname() {
  396. if (is_null($this->_cm)) {
  397. return null;
  398. }
  399. return $this->_cm->modname;
  400. }
  401. /**
  402. * Please do not call this method directly, use the ->category syntax. {@link moodle_page::__get()}.
  403. * @return stdClass the category that the page course belongs to. If there isn't one
  404. * (that is, if this is the front page course) returns null.
  405. */
  406. protected function magic_get_category() {
  407. $this->ensure_category_loaded();
  408. if (!empty($this->_categories)) {
  409. return reset($this->_categories);
  410. } else {
  411. return null;
  412. }
  413. }
  414. /**
  415. * Please do not call this method directly, use the ->categories syntax. {@link moodle_page::__get()}.
  416. * @return array an array of all the categories the page course belongs to,
  417. * starting with the immediately containing category, and working out to
  418. * the top-level category. This may be the empty array if we are in the
  419. * front page course.
  420. */
  421. protected function magic_get_categories() {
  422. $this->ensure_categories_loaded();
  423. return $this->_categories;
  424. }
  425. /**
  426. * Please do not call this method directly, use the ->context syntax. {@link moodle_page::__get()}.
  427. * @return context the main context to which this page belongs.
  428. */
  429. protected function magic_get_context() {
  430. global $CFG;
  431. if (is_null($this->_context)) {
  432. if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
  433. // Cli scripts work in system context, do not annoy devs with debug info.
  434. // Very few scripts do not use cookies, we can safely use system as default context there.
  435. } else if (AJAX_SCRIPT && $CFG->debugdeveloper) {
  436. // Throw exception inside AJAX script in developer mode, otherwise the debugging message may be missed.
  437. throw new coding_exception('$PAGE->context was not set. You may have forgotten '
  438. .'to call require_login() or $PAGE->set_context()');
  439. } else {
  440. debugging('Coding problem: $PAGE->context was not set. You may have forgotten '
  441. .'to call require_login() or $PAGE->set_context(). The page may not display '
  442. .'correctly as a result');
  443. }
  444. $this->_context = context_system::instance();
  445. }
  446. return $this->_context;
  447. }
  448. /**
  449. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  450. * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
  451. */
  452. protected function magic_get_pagetype() {
  453. global $CFG;
  454. if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
  455. $this->initialise_default_pagetype();
  456. }
  457. return $this->_pagetype;
  458. }
  459. /**
  460. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  461. * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
  462. */
  463. protected function magic_get_bodyid() {
  464. return 'page-'.$this->pagetype;
  465. }
  466. /**
  467. * Please do not call this method directly, use the ->pagelayout syntax. {@link moodle_page::__get()}.
  468. * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
  469. * Allows the theme to display things differently, if it wishes to.
  470. */
  471. protected function magic_get_pagelayout() {
  472. return $this->_pagelayout;
  473. }
  474. /**
  475. * Please do not call this method directly, use the ->layout_options syntax. {@link moodle_page::__get()}.
  476. * @return array returns arrays with options for layout file
  477. */
  478. protected function magic_get_layout_options() {
  479. if (!is_array($this->_layout_options)) {
  480. $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
  481. }
  482. return $this->_layout_options;
  483. }
  484. /**
  485. * Please do not call this method directly, use the ->subpage syntax. {@link moodle_page::__get()}.
  486. * @return string The subpage identifier, if any.
  487. */
  488. protected function magic_get_subpage() {
  489. return $this->_subpage;
  490. }
  491. /**
  492. * Please do not call this method directly, use the ->bodyclasses syntax. {@link moodle_page::__get()}.
  493. * @return string the class names to put on the body element in the HTML.
  494. */
  495. protected function magic_get_bodyclasses() {
  496. return implode(' ', array_keys($this->_bodyclasses));
  497. }
  498. /**
  499. * Please do not call this method directly, use the ->title syntax. {@link moodle_page::__get()}.
  500. * @return string the title that should go in the <head> section of the HTML of this page.
  501. */
  502. protected function magic_get_title() {
  503. return $this->_title;
  504. }
  505. /**
  506. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  507. * @return string the main heading that should be displayed at the top of the <body>.
  508. */
  509. protected function magic_get_heading() {
  510. return $this->_heading;
  511. }
  512. /**
  513. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  514. * @return string The menu (or actions) to display in the heading
  515. */
  516. protected function magic_get_headingmenu() {
  517. return $this->_headingmenu;
  518. }
  519. /**
  520. * Please do not call this method directly, use the ->docspath syntax. {@link moodle_page::__get()}.
  521. * @return string the path to the Moodle docs for this page.
  522. */
  523. protected function magic_get_docspath() {
  524. if (is_string($this->_docspath)) {
  525. return $this->_docspath;
  526. } else {
  527. return str_replace('-', '/', $this->pagetype);
  528. }
  529. }
  530. /**
  531. * Please do not call this method directly, use the ->url syntax. {@link moodle_page::__get()}.
  532. * @return moodle_url the clean URL required to load the current page. (You
  533. * should normally use this in preference to $ME or $FULLME.)
  534. */
  535. protected function magic_get_url() {
  536. global $FULLME;
  537. if (is_null($this->_url)) {
  538. debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
  539. $this->_url = new moodle_url($FULLME);
  540. // Make sure the guessed URL cannot lead to dangerous redirects.
  541. $this->_url->remove_params('sesskey');
  542. }
  543. return new moodle_url($this->_url); // Return a clone for safety.
  544. }
  545. /**
  546. * The list of alternate versions of this page.
  547. * @return array mime type => object with ->url and ->title.
  548. */
  549. protected function magic_get_alternateversions() {
  550. return $this->_alternateversions;
  551. }
  552. /**
  553. * Please do not call this method directly, use the ->blocks syntax. {@link moodle_page::__get()}.
  554. * @return block_manager the blocks manager object for this page.
  555. */
  556. protected function magic_get_blocks() {
  557. global $CFG;
  558. if (is_null($this->_blocks)) {
  559. if (!empty($CFG->blockmanagerclass)) {
  560. if (!empty($CFG->blockmanagerclassfile)) {
  561. require_once($CFG->blockmanagerclassfile);
  562. }
  563. $classname = $CFG->blockmanagerclass;
  564. } else {
  565. $classname = 'block_manager';
  566. }
  567. $this->_blocks = new $classname($this);
  568. }
  569. return $this->_blocks;
  570. }
  571. /**
  572. * Please do not call this method directly, use the ->requires syntax. {@link moodle_page::__get()}.
  573. * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
  574. */
  575. protected function magic_get_requires() {
  576. if (is_null($this->_requires)) {
  577. $this->_requires = new page_requirements_manager();
  578. }
  579. return $this->_requires;
  580. }
  581. /**
  582. * Please do not call this method directly, use the ->cacheable syntax. {@link moodle_page::__get()}.
  583. * @return bool can this page be cached by the user's browser.
  584. */
  585. protected function magic_get_cacheable() {
  586. return $this->_cacheable;
  587. }
  588. /**
  589. * Please do not call this method directly, use the ->focuscontrol syntax. {@link moodle_page::__get()}.
  590. * @return string the id of the HTML element to be focused when the page has loaded.
  591. */
  592. protected function magic_get_focuscontrol() {
  593. return $this->_focuscontrol;
  594. }
  595. /**
  596. * Please do not call this method directly, use the ->button syntax. {@link moodle_page::__get()}.
  597. * @return string the HTML to go where the Turn editing on button normally goes.
  598. */
  599. protected function magic_get_button() {
  600. return $this->_button;
  601. }
  602. /**
  603. * Please do not call this method directly, use the ->theme syntax. {@link moodle_page::__get()}.
  604. * @return theme_config the initialised theme for this page.
  605. */
  606. protected function magic_get_theme() {
  607. if (is_null($this->_theme)) {
  608. $this->initialise_theme_and_output();
  609. }
  610. return $this->_theme;
  611. }
  612. /**
  613. * Returns an array of minipulations or false if there are none to make.
  614. *
  615. * @since Moodle 2.5.1 2.6
  616. * @return bool|array
  617. */
  618. protected function magic_get_blockmanipulations() {
  619. if (!right_to_left()) {
  620. return false;
  621. }
  622. if (is_null($this->_theme)) {
  623. $this->initialise_theme_and_output();
  624. }
  625. return $this->_theme->blockrtlmanipulations;
  626. }
  627. /**
  628. * Please do not call this method directly, use the ->devicetypeinuse syntax. {@link moodle_page::__get()}.
  629. * @return string The device type being used.
  630. */
  631. protected function magic_get_devicetypeinuse() {
  632. if (empty($this->_devicetypeinuse)) {
  633. $this->_devicetypeinuse = core_useragent::get_user_device_type();
  634. }
  635. return $this->_devicetypeinuse;
  636. }
  637. /**
  638. * Please do not call this method directly use the ->periodicrefreshdelay syntax
  639. * {@link moodle_page::__get()}
  640. * @return int The periodic refresh delay to use with meta refresh
  641. */
  642. protected function magic_get_periodicrefreshdelay() {
  643. return $this->_periodicrefreshdelay;
  644. }
  645. /**
  646. * Please do not call this method directly use the ->opencontainers syntax. {@link moodle_page::__get()}
  647. * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
  648. * mainly for internal use by the rendering code.
  649. */
  650. protected function magic_get_opencontainers() {
  651. if (is_null($this->_opencontainers)) {
  652. $this->_opencontainers = new xhtml_container_stack();
  653. }
  654. return $this->_opencontainers;
  655. }
  656. /**
  657. * Return the navigation object
  658. * @return global_navigation
  659. */
  660. protected function magic_get_navigation() {
  661. if ($this->_navigation === null) {
  662. $this->_navigation = new global_navigation($this);
  663. }
  664. return $this->_navigation;
  665. }
  666. /**
  667. * Return a navbar object
  668. * @return navbar
  669. */
  670. protected function magic_get_navbar() {
  671. if ($this->_navbar === null) {
  672. $this->_navbar = new navbar($this);
  673. }
  674. return $this->_navbar;
  675. }
  676. /**
  677. * Returns the settings navigation object
  678. * @return settings_navigation
  679. */
  680. protected function magic_get_settingsnav() {
  681. if ($this->_settingsnav === null) {
  682. $this->_settingsnav = new settings_navigation($this);
  683. $this->_settingsnav->initialise();
  684. }
  685. return $this->_settingsnav;
  686. }
  687. /**
  688. * Returns the flat navigation object
  689. * @return flat_navigation
  690. */
  691. protected function magic_get_flatnav() {
  692. if ($this->_flatnav === null) {
  693. $this->_flatnav = new flat_navigation($this);
  694. $this->_flatnav->initialise();
  695. }
  696. return $this->_flatnav;
  697. }
  698. /**
  699. * Returns request IP address.
  700. *
  701. * @return string IP address or null if unknown
  702. */
  703. protected function magic_get_requestip() {
  704. return getremoteaddr(null);
  705. }
  706. /**
  707. * Returns the origin of current request.
  708. *
  709. * Note: constants are not required because we need to use these values in logging and reports.
  710. *
  711. * @return string 'web', 'ws', 'cli', 'restore', etc.
  712. */
  713. protected function magic_get_requestorigin() {
  714. if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
  715. return 'restore';
  716. }
  717. if (WS_SERVER) {
  718. return 'ws';
  719. }
  720. if (CLI_SCRIPT) {
  721. return 'cli';
  722. }
  723. return 'web';
  724. }
  725. /**
  726. * PHP overloading magic to make the $PAGE->course syntax work by redirecting
  727. * it to the corresponding $PAGE->magic_get_course() method if there is one, and
  728. * throwing an exception if not.
  729. *
  730. * @param string $name property name
  731. * @return mixed
  732. * @throws coding_exception
  733. */
  734. public function __get($name) {
  735. $getmethod = 'magic_get_' . $name;
  736. if (method_exists($this, $getmethod)) {
  737. return $this->$getmethod();
  738. } else {
  739. throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
  740. }
  741. }
  742. /**
  743. * PHP overloading magic to catch obvious coding errors.
  744. *
  745. * This method has been created to catch obvious coding errors where the
  746. * developer has tried to set a page property using $PAGE->key = $value.
  747. * In the moodle_page class all properties must be set using the appropriate
  748. * $PAGE->set_something($value) method.
  749. *
  750. * @param string $name property name
  751. * @param mixed $value Value
  752. * @return void Throws exception if field not defined in page class
  753. * @throws coding_exception
  754. */
  755. public function __set($name, $value) {
  756. if (method_exists($this, 'set_' . $name)) {
  757. throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
  758. } else {
  759. throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
  760. }
  761. }
  762. // Other information getting methods ==========================================.
  763. /**
  764. * Returns instance of page renderer
  765. *
  766. * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
  767. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
  768. * @param string $target one of rendering target constants
  769. * @return renderer_base
  770. */
  771. public function get_renderer($component, $subtype = null, $target = null) {
  772. if ($this->pagelayout === 'maintenance') {
  773. // If the page is using the maintenance layout then we're going to force target to maintenance.
  774. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  775. // page layout.
  776. $target = RENDERER_TARGET_MAINTENANCE;
  777. }
  778. return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
  779. }
  780. /**
  781. * Checks to see if there are any items on the navbar object
  782. *
  783. * @return bool true if there are, false if not
  784. */
  785. public function has_navbar() {
  786. if ($this->_navbar === null) {
  787. $this->_navbar = new navbar($this);
  788. }
  789. return $this->_navbar->has_items();
  790. }
  791. /**
  792. * Switches from the regular requirements manager to the fragment requirements manager to
  793. * capture all necessary JavaScript to display a chunk of HTML such as an mform. This is for use
  794. * by the get_fragment() web service and not for use elsewhere.
  795. */
  796. public function start_collecting_javascript_requirements() {
  797. global $CFG;
  798. require_once($CFG->libdir.'/outputfragmentrequirementslib.php');
  799. // Check that the requirements manager has not already been switched.
  800. if (get_class($this->_requires) == 'fragment_requirements_manager') {
  801. throw new coding_exception('JavaScript collection has already been started.');
  802. }
  803. // The header needs to have been called to flush out the generic JavaScript for the page. This allows only
  804. // JavaScript for the fragment to be collected. _wherethemewasinitialised is set when header() is called.
  805. if (!empty($this->_wherethemewasinitialised)) {
  806. // Change the current requirements manager over to the fragment manager to capture JS.
  807. $this->savedrequires = $this->_requires;
  808. $this->_requires = new fragment_requirements_manager();
  809. } else {
  810. throw new coding_exception('$OUTPUT->header() needs to be called before collecting JavaScript requirements.');
  811. }
  812. }
  813. /**
  814. * Switches back from collecting fragment JS requirement to the original requirement manager
  815. */
  816. public function end_collecting_javascript_requirements() {
  817. if ($this->savedrequires === null) {
  818. throw new coding_exception('JavaScript collection has not been started.');
  819. }
  820. $this->_requires = $this->savedrequires;
  821. $this->savedrequires = null;
  822. }
  823. /**
  824. * Should the current user see this page in editing mode.
  825. * That is, are they allowed to edit this page, and are they currently in
  826. * editing mode.
  827. * @return bool
  828. */
  829. public function user_is_editing() {
  830. global $USER;
  831. return !empty($USER->editing) && $this->user_allowed_editing();
  832. }
  833. /**
  834. * Does the user have permission to edit blocks on this page.
  835. * @return bool
  836. */
  837. public function user_can_edit_blocks() {
  838. return has_capability($this->_blockseditingcap, $this->_context);
  839. }
  840. /**
  841. * Does the user have permission to see this page in editing mode.
  842. * @return bool
  843. */
  844. public function user_allowed_editing() {
  845. return has_any_capability($this->all_editing_caps(), $this->_context);
  846. }
  847. /**
  848. * Get a description of this page. Normally displayed in the footer in developer debug mode.
  849. * @return string
  850. */
  851. public function debug_summary() {
  852. $summary = '';
  853. $summary .= 'General type: ' . $this->pagelayout . '. ';
  854. if (!during_initial_install()) {
  855. $summary .= 'Context ' . $this->context->get_context_name() . ' (context id ' . $this->_context->id . '). ';
  856. }
  857. $summary .= 'Page type ' . $this->pagetype . '. ';
  858. if ($this->subpage) {
  859. $summary .= 'Sub-page ' . $this->subpage . '. ';
  860. }
  861. return $summary;
  862. }
  863. // Setter methods =============================================================.
  864. /**
  865. * Set the state.
  866. *
  867. * The state must be one of that STATE_... constants, and the state is only allowed to advance one step at a time.
  868. *
  869. * @param int $state The new state.
  870. * @throws coding_exception
  871. */
  872. public function set_state($state) {
  873. if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
  874. throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
  875. $this->_state . ' and state ' . $state . ' was requested.');
  876. }
  877. if ($state == self::STATE_PRINTING_HEADER) {
  878. $this->starting_output();
  879. }
  880. $this->_state = $state;
  881. }
  882. /**
  883. * Set the current course. This sets both $PAGE->course and $COURSE. It also
  884. * sets the right theme and locale.
  885. *
  886. * Normally you don't need to call this function yourself, require_login will
  887. * call it for you if you pass a $course to it. You can use this function
  888. * on pages that do need to call require_login().
  889. *
  890. * Sets $PAGE->context to the course context, if it is not already set.
  891. *
  892. * @param stdClass $course the course to set as the global course.
  893. * @throws coding_exception
  894. */
  895. public function set_course($course) {
  896. global $COURSE, $PAGE, $CFG, $SITE;
  897. if (empty($course->id)) {
  898. throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
  899. }
  900. $this->ensure_theme_not_set();
  901. if (!empty($this->_course->id) && $this->_course->id != $course->id) {
  902. $this->_categories = null;
  903. }
  904. $this->_course = clone($course);
  905. if ($this === $PAGE) {
  906. $COURSE = $this->_course;
  907. moodle_setlocale();
  908. }
  909. if (!$this->_context) {
  910. $this->set_context(context_course::instance($this->_course->id));
  911. }
  912. // Notify course format that this page is set for the course.
  913. if ($this->_course->id != $SITE->id) {
  914. require_once($CFG->dirroot.'/course/lib.php');
  915. $courseformat = course_get_format($this->_course);
  916. $this->add_body_class('format-'. $courseformat->get_format());
  917. $courseformat->page_set_course($this);
  918. } else {
  919. $this->add_body_class('format-site');
  920. }
  921. }
  922. /**
  923. * Set the main context to which this page belongs.
  924. *
  925. * @param context $context a context object. You normally get this with context_xxxx::instance().
  926. */
  927. public function set_context($context) {
  928. if ($context === null) {
  929. // Extremely ugly hack which sets context to some value in order to prevent warnings,
  930. // use only for core error handling!!!!
  931. if (!$this->_context) {
  932. $this->_context = context_system::instance();
  933. }
  934. return;
  935. }
  936. // Ideally we should set context only once.
  937. if (isset($this->_context) && $context->id !== $this->_context->id) {
  938. $current = $this->_context->contextlevel;
  939. if ($current == CONTEXT_SYSTEM or $current == CONTEXT_COURSE) {
  940. // Hmm - not ideal, but it might produce too many warnings due to the design of require_login.
  941. } else if ($current == CONTEXT_MODULE and ($parentcontext = $context->get_parent_context()) and
  942. $this->_context->id == $parentcontext->id) {
  943. // Hmm - most probably somebody did require_login() and after that set the block context.
  944. } else {
  945. // We do not want devs to do weird switching of context levels on the fly because we might have used
  946. // the context already such as in text filter in page title.
  947. debugging("Coding problem: unsupported modification of PAGE->context from {$current} to {$context->contextlevel}");
  948. }
  949. }
  950. $this->_context = $context;
  951. }
  952. /**
  953. * The course module that this page belongs to (if it does belong to one).
  954. *
  955. * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
  956. * @param stdClass $course
  957. * @param stdClass $module
  958. * @return void
  959. * @throws coding_exception
  960. */
  961. public function set_cm($cm, $course = null, $module = null) {
  962. global $DB, $CFG, $SITE;
  963. if (!isset($cm->id) || !isset($cm->course)) {
  964. throw new coding_exception('Invalid $cm. It has to be instance of cm_info or record from the course_modules table.');
  965. }
  966. if (!$this->_course || $this->_course->id != $cm->course) {
  967. if (!$course) {
  968. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  969. }
  970. if ($course->id != $cm->course) {
  971. throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
  972. }
  973. $this->set_course($course);
  974. }
  975. // Make sure we have a $cm from get_fast_modinfo as this contains activity access details.
  976. if (!($cm instanceof cm_info)) {
  977. $modinfo = get_fast_modinfo($this->_course);
  978. $cm = $modinfo->get_cm($cm->id);
  979. }
  980. $this->_cm = $cm;
  981. // Unfortunately the context setting is a mess.
  982. // Let's try to work around some common block problems and show some debug messages.
  983. if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
  984. $context = context_module::instance($cm->id);
  985. $this->set_context($context);
  986. }
  987. if ($module) {
  988. $this->set_activity_record($module);
  989. }
  990. // Notify course format that this page is set for the course module.
  991. if ($this->_course->id != $SITE->id) {
  992. require_once($CFG->dirroot.'/course/lib.php');
  993. course_get_format($this->_course)->page_set_cm($this);
  994. }
  995. }
  996. /**
  997. * Sets the activity record. This could be a row from the main table for a
  998. * module. For instance if the current module (cm) is a forum this should be a row
  999. * from the forum table.
  1000. *
  1001. * @param stdClass $module A row from the main database table for the module that this page belongs to.
  1002. * @throws coding_exception
  1003. */
  1004. public function set_activity_record($module) {
  1005. if (is_null($this->_cm)) {
  1006. throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
  1007. }
  1008. if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
  1009. throw new coding_exception('The activity record does not seem to correspond to the cm that has been set.');
  1010. }
  1011. $this->_module = $module;
  1012. }
  1013. /**
  1014. * Sets the pagetype to use for this page.
  1015. *
  1016. * Normally you do not need to set this manually, it is automatically created
  1017. * from the script name. However, on some pages this is overridden.
  1018. * For example the page type for course/view.php includes the course format,
  1019. * for example 'course-view-weeks'. This gets used as the id attribute on
  1020. * <body> and also for determining which blocks are displayed.
  1021. *
  1022. * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'.
  1023. */
  1024. public function set_pagetype($pagetype) {
  1025. $this->_pagetype = $pagetype;
  1026. }
  1027. /**
  1028. * Sets the layout to use for this page.
  1029. *
  1030. * The page layout determines how the page will be displayed, things such as
  1031. * block regions, content areas, etc are controlled by the layout.
  1032. * The theme in use for the page will determine that the layout contains.
  1033. *
  1034. * This properly defaults to 'base', so you only need to call this function if
  1035. * you want something different. The exact range of supported layouts is specified
  1036. * in the standard theme.
  1037. *
  1038. * For an idea of the common page layouts see
  1039. * {@link http://docs.moodle.org/dev/Themes_2.0#The_different_layouts_as_of_August_17th.2C_2010}
  1040. * But please keep in mind that it may be (and normally is) out of date.
  1041. * The only place to find an accurate up-to-date list of the page layouts
  1042. * available for your version of Moodle is {@link theme/base/config.php}
  1043. *
  1044. * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
  1045. */
  1046. public function set_pagelayout($pagelayout) {
  1047. global $SESSION;
  1048. if (!empty($SESSION->forcepagelayout)) {
  1049. $this->_pagelayout = $SESSION->forcepagelayout;
  1050. } else {
  1051. // Uncomment this to debug theme pagelayout issues like missing blocks.
  1052. // if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout)
  1053. // debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
  1054. $this->_pagelayout = $pagelayout;
  1055. }
  1056. }
  1057. /**
  1058. * If context->id and pagetype are not enough to uniquely identify this page,
  1059. * then you can set a subpage id as well. For example, the tags page sets
  1060. *
  1061. * @param string $subpage an arbitrary identifier that, along with context->id
  1062. * and pagetype, uniquely identifies this page.
  1063. */
  1064. public function set_subpage($subpage) {
  1065. if (empty($subpage)) {
  1066. $this->_subpage = '';
  1067. } else {
  1068. $this->_subpage = $subpage;
  1069. }
  1070. }
  1071. /**
  1072. * Adds a CSS class to the body tag of the page.
  1073. *
  1074. * @param string $class add this class name ot the class attribute on the body tag.
  1075. * @throws coding_exception
  1076. */
  1077. public function add_body_class($class) {
  1078. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1079. throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
  1080. }
  1081. $this->_bodyclasses[$class] = 1;
  1082. }
  1083. /**
  1084. * Adds an array of body classes to the body tag of this page.
  1085. *
  1086. * @param array $classes this utility method calls add_body_class for each array element.
  1087. */
  1088. public function add_body_classes($classes) {
  1089. foreach ($classes as $class) {
  1090. $this->add_body_class($class);
  1091. }
  1092. }
  1093. /**
  1094. * Sets the title for the page.
  1095. * This is normally used within the title tag in the head of the page.
  1096. *
  1097. * @param string $title the title that should go in the <head> section of the HTML of this page.
  1098. */
  1099. public function set_title($title) {
  1100. $title = format_string($title);
  1101. $title = strip_tags($title);
  1102. $title = str_replace('"', '&quot;', $title);
  1103. $this->_title = $title;
  1104. }
  1105. /**
  1106. * Sets the heading to use for the page.
  1107. * This is normally used as the main heading at the top of the content.
  1108. *
  1109. * @param string $heading the main heading that should be displayed at the top of the <body>.
  1110. */
  1111. public function set_heading($heading) {
  1112. $this->_heading = format_string($heading);
  1113. }
  1114. /**
  1115. * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
  1116. *
  1117. * @param string $menu The menu/content to show in the heading
  1118. */
  1119. public function set_headingmenu($menu) {
  1120. $this->_headingmenu = $menu;
  1121. }
  1122. /**
  1123. * Set the course category this page belongs to manually.
  1124. *
  1125. * This automatically sets $PAGE->course to be the site course. You cannot
  1126. * use this method if you have already set $PAGE->course - in that case,
  1127. * the category must be the one that the course belongs to. This also
  1128. * automatically sets the page context to the category context.
  1129. *
  1130. * @param int $categoryid The id of the category to set.
  1131. * @throws coding_exception
  1132. */
  1133. public function set_category_by_id($categoryid) {
  1134. global $SITE;
  1135. if (!is_null($this->_course)) {
  1136. throw new coding_exception('Course has already been set. You cannot change the category now.');
  1137. }
  1138. if (is_array($this->_categories)) {
  1139. throw new coding_exception('Course category has already been set. You cannot to change it now.');
  1140. }
  1141. $this->ensure_theme_not_set();
  1142. $this->set_course($SITE);
  1143. $this->load_category($categoryid);
  1144. $this->set_context(context_coursecat::instance($categoryid));
  1145. }
  1146. /**
  1147. * Set a different path to use for the 'Moodle docs for this page' link.
  1148. *
  1149. * By default, it uses the pagetype, which is normally the same as the
  1150. * script name. So, for example, for mod/quiz/attempt.php, pagetype is
  1151. * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
  1152. *
  1153. * @param string $path the path to use at the end of the mood…

Large files files are truncated, but you can click here to view the full file