PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pagelib.php

https://github.com/lsuits/moodle
PHP | 1930 lines | 847 code | 206 blank | 877 comment | 204 complexity | 1826d0a30b4fe834d8af32e99ff735d0 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0

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. /**
  209. * @var string The capability required by the user in order to edit blocks
  210. * and block settings on this page.
  211. */
  212. protected $_blockseditingcap = 'moodle/site:manageblocks';
  213. /**
  214. * @var bool An internal flag to record when block actions have been processed.
  215. * Remember block actions occur on the current URL and it is important that
  216. * even they are never executed more than once.
  217. */
  218. protected $_block_actions_done = false;
  219. /**
  220. * @var array An array of any other capabilities the current user must have
  221. * in order to editing the page and/or its content (not just blocks).
  222. */
  223. protected $_othereditingcaps = array();
  224. /**
  225. * @var bool Sets whether this page should be cached by the browser or not.
  226. * If it is set to true (default) the page is served with caching headers.
  227. */
  228. protected $_cacheable = true;
  229. /**
  230. * @var string Can be set to the ID of an element on the page, if done that
  231. * element receives focus when the page loads.
  232. */
  233. protected $_focuscontrol = '';
  234. /**
  235. * @var string HTML to go where the turn on editing button is located. This
  236. * is nearly a legacy item and not used very often any more.
  237. */
  238. protected $_button = '';
  239. /**
  240. * @var theme_config The theme to use with this page. This has to be properly
  241. * initialised via {@link moodle_page::initialise_theme_and_output()} which
  242. * happens magically before any operation that requires it.
  243. */
  244. protected $_theme = null;
  245. /**
  246. * @var global_navigation Contains the global navigation structure.
  247. */
  248. protected $_navigation = null;
  249. /**
  250. * @var settings_navigation Contains the settings navigation structure.
  251. */
  252. protected $_settingsnav = null;
  253. /**
  254. * @var navbar Contains the navbar structure.
  255. */
  256. protected $_navbar = null;
  257. /**
  258. * @var string The menu (or actions) to display in the heading.
  259. */
  260. protected $_headingmenu = null;
  261. /**
  262. * @var array stack trace. Then the theme is initialised, we save the stack
  263. * trace, for use in error messages.
  264. */
  265. protected $_wherethemewasinitialised = null;
  266. /**
  267. * @var xhtml_container_stack Tracks XHTML tags on this page that have been
  268. * opened but not closed.
  269. */
  270. protected $_opencontainers;
  271. /**
  272. * @var int Sets the page to refresh after a given delay (in seconds) using
  273. * meta refresh in {@link standard_head_html()} in outputlib.php
  274. * If set to null(default) the page is not refreshed
  275. */
  276. protected $_periodicrefreshdelay = null;
  277. /**
  278. * @var array Associative array of browser shortnames (as used by check_browser_version)
  279. * and their minimum required versions
  280. */
  281. protected $_legacybrowsers = array('MSIE' => 6.0);
  282. /**
  283. * @var string Is set to the name of the device type in use.
  284. * This will we worked out when it is first used.
  285. */
  286. protected $_devicetypeinuse = null;
  287. /**
  288. * @var bool Used to determine if HTTPS should be required for login.
  289. */
  290. protected $_https_login_required = false;
  291. /**
  292. * @var bool Determines if popup notifications allowed on this page.
  293. * Code such as the quiz module disables popup notifications in situations
  294. * such as upgrading or completing a quiz.
  295. */
  296. protected $_popup_notification_allowed = true;
  297. // Magic getter methods =============================================================
  298. // Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
  299. // methods, but instead use the $PAGE->x syntax.
  300. /**
  301. * Please do not call this method directly, use the ->state syntax. {@link moodle_page::__get()}.
  302. * @return integer one of the STATE_XXX constants. You should not normally need
  303. * to use this in your code. It is intended for internal use by this class
  304. * and its friends like print_header, to check that everything is working as
  305. * expected. Also accessible as $PAGE->state.
  306. */
  307. protected function magic_get_state() {
  308. return $this->_state;
  309. }
  310. /**
  311. * Please do not call this method directly, use the ->headerprinted syntax. {@link moodle_page::__get()}.
  312. * @return bool has the header already been printed?
  313. */
  314. protected function magic_get_headerprinted() {
  315. return $this->_state >= self::STATE_IN_BODY;
  316. }
  317. /**
  318. * Please do not call this method directly, use the ->course syntax. {@link moodle_page::__get()}.
  319. * @return stdClass the current course that we are inside - a row from the
  320. * course table. (Also available as $COURSE global.) If we are not inside
  321. * an actual course, this will be the site course.
  322. */
  323. protected function magic_get_course() {
  324. global $SITE;
  325. if (is_null($this->_course)) {
  326. return $SITE;
  327. }
  328. return $this->_course;
  329. }
  330. /**
  331. * Please do not call this method directly, use the ->cm syntax. {@link moodle_page::__get()}.
  332. * @return cm_info the course_module that this page belongs to. Will be null
  333. * if this page is not within a module. This is a full cm object, as loaded
  334. * by get_coursemodule_from_id or get_coursemodule_from_instance,
  335. * so the extra modname and name fields are present.
  336. */
  337. protected function magic_get_cm() {
  338. return $this->_cm;
  339. }
  340. /**
  341. * Please do not call this method directly, use the ->activityrecord syntax. {@link moodle_page::__get()}.
  342. * @return stdClass the row from the activities own database table (for example
  343. * the forum or quiz table) that this page belongs to. Will be null
  344. * if this page is not within a module.
  345. */
  346. protected function magic_get_activityrecord() {
  347. if (is_null($this->_module) && !is_null($this->_cm)) {
  348. $this->load_activity_record();
  349. }
  350. return $this->_module;
  351. }
  352. /**
  353. * Please do not call this method directly, use the ->activityname syntax. {@link moodle_page::__get()}.
  354. * @return string the The type of activity we are in, for example 'forum' or 'quiz'.
  355. * Will be null if this page is not within a module.
  356. */
  357. protected function magic_get_activityname() {
  358. if (is_null($this->_cm)) {
  359. return null;
  360. }
  361. return $this->_cm->modname;
  362. }
  363. /**
  364. * Please do not call this method directly, use the ->category syntax. {@link moodle_page::__get()}.
  365. * @return stdClass the category that the page course belongs to. If there isn't one
  366. * (that is, if this is the front page course) returns null.
  367. */
  368. protected function magic_get_category() {
  369. $this->ensure_category_loaded();
  370. if (!empty($this->_categories)) {
  371. return reset($this->_categories);
  372. } else {
  373. return null;
  374. }
  375. }
  376. /**
  377. * Please do not call this method directly, use the ->categories syntax. {@link moodle_page::__get()}.
  378. * @return array an array of all the categories the page course belongs to,
  379. * starting with the immediately containing category, and working out to
  380. * the top-level category. This may be the empty array if we are in the
  381. * front page course.
  382. */
  383. protected function magic_get_categories() {
  384. $this->ensure_categories_loaded();
  385. return $this->_categories;
  386. }
  387. /**
  388. * Please do not call this method directly, use the ->context syntax. {@link moodle_page::__get()}.
  389. * @return context the main context to which this page belongs.
  390. */
  391. protected function magic_get_context() {
  392. if (is_null($this->_context)) {
  393. if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
  394. // Cli scripts work in system context, do not annoy devs with debug info.
  395. // Very few scripts do not use cookies, we can safely use system as default context there.
  396. } else {
  397. debugging('Coding problem: $PAGE->context was not set. You may have forgotten '
  398. .'to call require_login() or $PAGE->set_context(). The page may not display '
  399. .'correctly as a result');
  400. }
  401. $this->_context = context_system::instance();
  402. }
  403. return $this->_context;
  404. }
  405. /**
  406. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  407. * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
  408. */
  409. protected function magic_get_pagetype() {
  410. global $CFG;
  411. if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
  412. $this->initialise_default_pagetype();
  413. }
  414. return $this->_pagetype;
  415. }
  416. /**
  417. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  418. * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
  419. */
  420. protected function magic_get_bodyid() {
  421. return 'page-'.$this->pagetype;
  422. }
  423. /**
  424. * Please do not call this method directly, use the ->pagelayout syntax. {@link moodle_page::__get()}.
  425. * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
  426. * Allows the theme to display things differently, if it wishes to.
  427. */
  428. protected function magic_get_pagelayout() {
  429. return $this->_pagelayout;
  430. }
  431. /**
  432. * Please do not call this method directly, use the ->layout_options syntax. {@link moodle_page::__get()}.
  433. * @return array returns arrays with options for layout file
  434. */
  435. protected function magic_get_layout_options() {
  436. if (!is_array($this->_layout_options)) {
  437. $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
  438. }
  439. return $this->_layout_options;
  440. }
  441. /**
  442. * Please do not call this method directly, use the ->subpage syntax. {@link moodle_page::__get()}.
  443. * @return string The subpage identifier, if any.
  444. */
  445. protected function magic_get_subpage() {
  446. return $this->_subpage;
  447. }
  448. /**
  449. * Please do not call this method directly, use the ->bodyclasses syntax. {@link moodle_page::__get()}.
  450. * @return string the class names to put on the body element in the HTML.
  451. */
  452. protected function magic_get_bodyclasses() {
  453. return implode(' ', array_keys($this->_bodyclasses));
  454. }
  455. /**
  456. * Please do not call this method directly, use the ->title syntax. {@link moodle_page::__get()}.
  457. * @return string the title that should go in the <head> section of the HTML of this page.
  458. */
  459. protected function magic_get_title() {
  460. return $this->_title;
  461. }
  462. /**
  463. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  464. * @return string the main heading that should be displayed at the top of the <body>.
  465. */
  466. protected function magic_get_heading() {
  467. return $this->_heading;
  468. }
  469. /**
  470. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  471. * @return string The menu (or actions) to display in the heading
  472. */
  473. protected function magic_get_headingmenu() {
  474. return $this->_headingmenu;
  475. }
  476. /**
  477. * Please do not call this method directly, use the ->docspath syntax. {@link moodle_page::__get()}.
  478. * @return string the path to the Moodle docs for this page.
  479. */
  480. protected function magic_get_docspath() {
  481. if (is_string($this->_docspath)) {
  482. return $this->_docspath;
  483. } else {
  484. return str_replace('-', '/', $this->pagetype);
  485. }
  486. }
  487. /**
  488. * Please do not call this method directly, use the ->url syntax. {@link moodle_page::__get()}.
  489. * @return moodle_url the clean URL required to load the current page. (You
  490. * should normally use this in preference to $ME or $FULLME.)
  491. */
  492. protected function magic_get_url() {
  493. global $FULLME;
  494. if (is_null($this->_url)) {
  495. debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
  496. $this->_url = new moodle_url($FULLME);
  497. // Make sure the guessed URL cannot lead to dangerous redirects.
  498. $this->_url->remove_params('sesskey');
  499. }
  500. return new moodle_url($this->_url); // Return a clone for safety.
  501. }
  502. /**
  503. * The list of alternate versions of this page.
  504. * @return array mime type => object with ->url and ->title.
  505. */
  506. protected function magic_get_alternateversions() {
  507. return $this->_alternateversions;
  508. }
  509. /**
  510. * Please do not call this method directly, use the ->blocks syntax. {@link moodle_page::__get()}.
  511. * @return block_manager the blocks manager object for this page.
  512. */
  513. protected function magic_get_blocks() {
  514. global $CFG;
  515. if (is_null($this->_blocks)) {
  516. if (!empty($CFG->blockmanagerclass)) {
  517. if (!empty($CFG->blockmanagerclassfile)) {
  518. require_once($CFG->blockmanagerclassfile);
  519. }
  520. $classname = $CFG->blockmanagerclass;
  521. } else {
  522. $classname = 'block_manager';
  523. }
  524. $this->_blocks = new $classname($this);
  525. }
  526. return $this->_blocks;
  527. }
  528. /**
  529. * Please do not call this method directly, use the ->requires syntax. {@link moodle_page::__get()}.
  530. * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
  531. */
  532. protected function magic_get_requires() {
  533. if (is_null($this->_requires)) {
  534. $this->_requires = new page_requirements_manager();
  535. }
  536. return $this->_requires;
  537. }
  538. /**
  539. * Please do not call this method directly, use the ->cacheable syntax. {@link moodle_page::__get()}.
  540. * @return bool can this page be cached by the user's browser.
  541. */
  542. protected function magic_get_cacheable() {
  543. return $this->_cacheable;
  544. }
  545. /**
  546. * Please do not call this method directly, use the ->focuscontrol syntax. {@link moodle_page::__get()}.
  547. * @return string the id of the HTML element to be focused when the page has loaded.
  548. */
  549. protected function magic_get_focuscontrol() {
  550. return $this->_focuscontrol;
  551. }
  552. /**
  553. * Please do not call this method directly, use the ->button syntax. {@link moodle_page::__get()}.
  554. * @return string the HTML to go where the Turn editing on button normally goes.
  555. */
  556. protected function magic_get_button() {
  557. return $this->_button;
  558. }
  559. /**
  560. * Please do not call this method directly, use the ->theme syntax. {@link moodle_page::__get()}.
  561. * @return theme_config the initialised theme for this page.
  562. */
  563. protected function magic_get_theme() {
  564. if (is_null($this->_theme)) {
  565. $this->initialise_theme_and_output();
  566. }
  567. return $this->_theme;
  568. }
  569. /**
  570. * Returns an array of minipulations or false if there are none to make.
  571. *
  572. * @since Moodle 2.5.1 2.6
  573. * @return bool|array
  574. */
  575. protected function magic_get_blockmanipulations() {
  576. if (!right_to_left()) {
  577. return false;
  578. }
  579. if (is_null($this->_theme)) {
  580. $this->initialise_theme_and_output();
  581. }
  582. return $this->_theme->blockrtlmanipulations;
  583. }
  584. /**
  585. * Please do not call this method directly, use the ->devicetypeinuse syntax. {@link moodle_page::__get()}.
  586. * @return string The device type being used.
  587. */
  588. protected function magic_get_devicetypeinuse() {
  589. if (empty($this->_devicetypeinuse)) {
  590. $this->_devicetypeinuse = core_useragent::get_user_device_type();
  591. }
  592. return $this->_devicetypeinuse;
  593. }
  594. /**
  595. * Please do not call this method directly use the ->periodicrefreshdelay syntax
  596. * {@link moodle_page::__get()}
  597. * @return int The periodic refresh delay to use with meta refresh
  598. */
  599. protected function magic_get_periodicrefreshdelay() {
  600. return $this->_periodicrefreshdelay;
  601. }
  602. /**
  603. * Please do not call this method directly use the ->opencontainers syntax. {@link moodle_page::__get()}
  604. * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
  605. * mainly for internal use by the rendering code.
  606. */
  607. protected function magic_get_opencontainers() {
  608. if (is_null($this->_opencontainers)) {
  609. $this->_opencontainers = new xhtml_container_stack();
  610. }
  611. return $this->_opencontainers;
  612. }
  613. /**
  614. * Return the navigation object
  615. * @return global_navigation
  616. */
  617. protected function magic_get_navigation() {
  618. if ($this->_navigation === null) {
  619. $this->_navigation = new global_navigation($this);
  620. }
  621. return $this->_navigation;
  622. }
  623. /**
  624. * Return a navbar object
  625. * @return navbar
  626. */
  627. protected function magic_get_navbar() {
  628. if ($this->_navbar === null) {
  629. $this->_navbar = new navbar($this);
  630. }
  631. return $this->_navbar;
  632. }
  633. /**
  634. * Returns the settings navigation object
  635. * @return settings_navigation
  636. */
  637. protected function magic_get_settingsnav() {
  638. if ($this->_settingsnav === null) {
  639. $this->_settingsnav = new settings_navigation($this);
  640. $this->_settingsnav->initialise();
  641. }
  642. return $this->_settingsnav;
  643. }
  644. /**
  645. * Returns request IP address.
  646. *
  647. * @return string IP address or null if unknown
  648. */
  649. protected function magic_get_requestip() {
  650. return getremoteaddr(null);
  651. }
  652. /**
  653. * Returns the origin of current request.
  654. *
  655. * Note: constants are not required because we need to use these values in logging and reports.
  656. *
  657. * @return string 'web', 'ws', 'cli', 'restore', etc.
  658. */
  659. protected function magic_get_requestorigin() {
  660. if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
  661. return 'restore';
  662. }
  663. if (WS_SERVER) {
  664. return 'ws';
  665. }
  666. if (CLI_SCRIPT) {
  667. return 'cli';
  668. }
  669. return 'web';
  670. }
  671. /**
  672. * PHP overloading magic to make the $PAGE->course syntax work by redirecting
  673. * it to the corresponding $PAGE->magic_get_course() method if there is one, and
  674. * throwing an exception if not.
  675. *
  676. * @param string $name property name
  677. * @return mixed
  678. * @throws coding_exception
  679. */
  680. public function __get($name) {
  681. $getmethod = 'magic_get_' . $name;
  682. if (method_exists($this, $getmethod)) {
  683. return $this->$getmethod();
  684. } else {
  685. throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
  686. }
  687. }
  688. /**
  689. * PHP overloading magic to catch obvious coding errors.
  690. *
  691. * This method has been created to catch obvious coding errors where the
  692. * developer has tried to set a page property using $PAGE->key = $value.
  693. * In the moodle_page class all properties must be set using the appropriate
  694. * $PAGE->set_something($value) method.
  695. *
  696. * @param string $name property name
  697. * @param mixed $value Value
  698. * @return void Throws exception if field not defined in page class
  699. * @throws coding_exception
  700. */
  701. public function __set($name, $value) {
  702. if (method_exists($this, 'set_' . $name)) {
  703. throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
  704. } else {
  705. throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
  706. }
  707. }
  708. // Other information getting methods ==========================================.
  709. /**
  710. * Returns instance of page renderer
  711. *
  712. * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
  713. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
  714. * @param string $target one of rendering target constants
  715. * @return renderer_base
  716. */
  717. public function get_renderer($component, $subtype = null, $target = null) {
  718. if ($this->pagelayout === 'maintenance') {
  719. // If the page is using the maintenance layout then we're going to force target to maintenance.
  720. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  721. // page layout.
  722. $target = RENDERER_TARGET_MAINTENANCE;
  723. }
  724. return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
  725. }
  726. /**
  727. * Checks to see if there are any items on the navbar object
  728. *
  729. * @return bool true if there are, false if not
  730. */
  731. public function has_navbar() {
  732. if ($this->_navbar === null) {
  733. $this->_navbar = new navbar($this);
  734. }
  735. return $this->_navbar->has_items();
  736. }
  737. /**
  738. * Should the current user see this page in editing mode.
  739. * That is, are they allowed to edit this page, and are they currently in
  740. * editing mode.
  741. * @return bool
  742. */
  743. public function user_is_editing() {
  744. global $USER;
  745. return !empty($USER->editing) && $this->user_allowed_editing();
  746. }
  747. /**
  748. * Does the user have permission to edit blocks on this page.
  749. * @return bool
  750. */
  751. public function user_can_edit_blocks() {
  752. return has_capability($this->_blockseditingcap, $this->_context);
  753. }
  754. /**
  755. * Does the user have permission to see this page in editing mode.
  756. * @return bool
  757. */
  758. public function user_allowed_editing() {
  759. return has_any_capability($this->all_editing_caps(), $this->_context);
  760. }
  761. /**
  762. * Get a description of this page. Normally displayed in the footer in developer debug mode.
  763. * @return string
  764. */
  765. public function debug_summary() {
  766. $summary = '';
  767. $summary .= 'General type: ' . $this->pagelayout . '. ';
  768. if (!during_initial_install()) {
  769. $summary .= 'Context ' . $this->context->get_context_name() . ' (context id ' . $this->_context->id . '). ';
  770. }
  771. $summary .= 'Page type ' . $this->pagetype . '. ';
  772. if ($this->subpage) {
  773. $summary .= 'Sub-page ' . $this->subpage . '. ';
  774. }
  775. return $summary;
  776. }
  777. // Setter methods =============================================================.
  778. /**
  779. * Set the state.
  780. *
  781. * The state must be one of that STATE_... constants, and the state is only allowed to advance one step at a time.
  782. *
  783. * @param int $state The new state.
  784. * @throws coding_exception
  785. */
  786. public function set_state($state) {
  787. if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
  788. throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
  789. $this->_state . ' and state ' . $state . ' was requested.');
  790. }
  791. if ($state == self::STATE_PRINTING_HEADER) {
  792. $this->starting_output();
  793. }
  794. $this->_state = $state;
  795. }
  796. /**
  797. * Set the current course. This sets both $PAGE->course and $COURSE. It also
  798. * sets the right theme and locale.
  799. *
  800. * Normally you don't need to call this function yourself, require_login will
  801. * call it for you if you pass a $course to it. You can use this function
  802. * on pages that do need to call require_login().
  803. *
  804. * Sets $PAGE->context to the course context, if it is not already set.
  805. *
  806. * @param stdClass $course the course to set as the global course.
  807. * @throws coding_exception
  808. */
  809. public function set_course($course) {
  810. global $COURSE, $PAGE, $CFG, $SITE;
  811. if (empty($course->id)) {
  812. throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
  813. }
  814. $this->ensure_theme_not_set();
  815. if (!empty($this->_course->id) && $this->_course->id != $course->id) {
  816. $this->_categories = null;
  817. }
  818. $this->_course = clone($course);
  819. if ($this === $PAGE) {
  820. $COURSE = $this->_course;
  821. moodle_setlocale();
  822. }
  823. if (!$this->_context) {
  824. $this->set_context(context_course::instance($this->_course->id));
  825. }
  826. // Notify course format that this page is set for the course.
  827. if ($this->_course->id != $SITE->id) {
  828. require_once($CFG->dirroot.'/course/lib.php');
  829. $courseformat = course_get_format($this->_course);
  830. $this->add_body_class('format-'. $courseformat->get_format());
  831. $courseformat->page_set_course($this);
  832. } else {
  833. $this->add_body_class('format-site');
  834. }
  835. }
  836. /**
  837. * Set the main context to which this page belongs.
  838. *
  839. * @param context $context a context object. You normally get this with context_xxxx::instance().
  840. */
  841. public function set_context($context) {
  842. if ($context === null) {
  843. // Extremely ugly hack which sets context to some value in order to prevent warnings,
  844. // use only for core error handling!!!!
  845. if (!$this->_context) {
  846. $this->_context = context_system::instance();
  847. }
  848. return;
  849. }
  850. // Ideally we should set context only once.
  851. if (isset($this->_context) && $context->id !== $this->_context->id) {
  852. $current = $this->_context->contextlevel;
  853. if ($current == CONTEXT_SYSTEM or $current == CONTEXT_COURSE) {
  854. // Hmm - not ideal, but it might produce too many warnings due to the design of require_login.
  855. } else if ($current == CONTEXT_MODULE and ($parentcontext = $context->get_parent_context()) and
  856. $this->_context->id == $parentcontext->id) {
  857. // Hmm - most probably somebody did require_login() and after that set the block context.
  858. } else {
  859. // We do not want devs to do weird switching of context levels on the fly because we might have used
  860. // the context already such as in text filter in page title.
  861. // This is explicitly allowed for webservices though which may
  862. // call "external_api::validate_context on many contexts in a single request.
  863. if (!WS_SERVER) {
  864. debugging("Coding problem: unsupported modification of PAGE->context from {$current} to {$context->contextlevel}");
  865. }
  866. }
  867. }
  868. $this->_context = $context;
  869. }
  870. /**
  871. * The course module that this page belongs to (if it does belong to one).
  872. *
  873. * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
  874. * @param stdClass $course
  875. * @param stdClass $module
  876. * @return void
  877. * @throws coding_exception
  878. */
  879. public function set_cm($cm, $course = null, $module = null) {
  880. global $DB, $CFG, $SITE;
  881. if (!isset($cm->id) || !isset($cm->course)) {
  882. throw new coding_exception('Invalid $cm. It has to be instance of cm_info or record from the course_modules table.');
  883. }
  884. if (!$this->_course || $this->_course->id != $cm->course) {
  885. if (!$course) {
  886. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  887. }
  888. if ($course->id != $cm->course) {
  889. throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
  890. }
  891. $this->set_course($course);
  892. }
  893. // Make sure we have a $cm from get_fast_modinfo as this contains activity access details.
  894. if (!($cm instanceof cm_info)) {
  895. $modinfo = get_fast_modinfo($this->_course);
  896. $cm = $modinfo->get_cm($cm->id);
  897. }
  898. $this->_cm = $cm;
  899. // Unfortunately the context setting is a mess.
  900. // Let's try to work around some common block problems and show some debug messages.
  901. if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
  902. $context = context_module::instance($cm->id);
  903. $this->set_context($context);
  904. }
  905. if ($module) {
  906. $this->set_activity_record($module);
  907. }
  908. // Notify course format that this page is set for the course module.
  909. if ($this->_course->id != $SITE->id) {
  910. require_once($CFG->dirroot.'/course/lib.php');
  911. course_get_format($this->_course)->page_set_cm($this);
  912. }
  913. }
  914. /**
  915. * Sets the activity record. This could be a row from the main table for a
  916. * module. For instance if the current module (cm) is a forum this should be a row
  917. * from the forum table.
  918. *
  919. * @param stdClass $module A row from the main database table for the module that this page belongs to.
  920. * @throws coding_exception
  921. */
  922. public function set_activity_record($module) {
  923. if (is_null($this->_cm)) {
  924. throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
  925. }
  926. if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
  927. throw new coding_exception('The activity record does not seem to correspond to the cm that has been set.');
  928. }
  929. $this->_module = $module;
  930. }
  931. /**
  932. * Sets the pagetype to use for this page.
  933. *
  934. * Normally you do not need to set this manually, it is automatically created
  935. * from the script name. However, on some pages this is overridden.
  936. * For example the page type for course/view.php includes the course format,
  937. * for example 'course-view-weeks'. This gets used as the id attribute on
  938. * <body> and also for determining which blocks are displayed.
  939. *
  940. * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'.
  941. */
  942. public function set_pagetype($pagetype) {
  943. $this->_pagetype = $pagetype;
  944. }
  945. /**
  946. * Sets the layout to use for this page.
  947. *
  948. * The page layout determines how the page will be displayed, things such as
  949. * block regions, content areas, etc are controlled by the layout.
  950. * The theme in use for the page will determine that the layout contains.
  951. *
  952. * This properly defaults to 'base', so you only need to call this function if
  953. * you want something different. The exact range of supported layouts is specified
  954. * in the standard theme.
  955. *
  956. * For an idea of the common page layouts see
  957. * {@link http://docs.moodle.org/dev/Themes_2.0#The_different_layouts_as_of_August_17th.2C_2010}
  958. * But please keep in mind that it may be (and normally is) out of date.
  959. * The only place to find an accurate up-to-date list of the page layouts
  960. * available for your version of Moodle is {@link theme/base/config.php}
  961. *
  962. * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
  963. */
  964. public function set_pagelayout($pagelayout) {
  965. // Uncomment this to debug theme pagelayout issues like missing blocks.
  966. // if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout)
  967. // debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
  968. $this->_pagelayout = $pagelayout;
  969. }
  970. /**
  971. * If context->id and pagetype are not enough to uniquely identify this page,
  972. * then you can set a subpage id as well. For example, the tags page sets
  973. *
  974. * @param string $subpage an arbitrary identifier that, along with context->id
  975. * and pagetype, uniquely identifies this page.
  976. */
  977. public function set_subpage($subpage) {
  978. if (empty($subpage)) {
  979. $this->_subpage = '';
  980. } else {
  981. $this->_subpage = $subpage;
  982. }
  983. }
  984. /**
  985. * Adds a CSS class to the body tag of the page.
  986. *
  987. * @param string $class add this class name ot the class attribute on the body tag.
  988. * @throws coding_exception
  989. */
  990. public function add_body_class($class) {
  991. if ($this->_state > self::STATE_BEFORE_HEADER) {
  992. throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
  993. }
  994. $this->_bodyclasses[$class] = 1;
  995. }
  996. /**
  997. * Adds an array of body classes to the body tag of this page.
  998. *
  999. * @param array $classes this utility method calls add_body_class for each array element.
  1000. */
  1001. public function add_body_classes($classes) {
  1002. foreach ($classes as $class) {
  1003. $this->add_body_class($class);
  1004. }
  1005. }
  1006. /**
  1007. * Sets the title for the page.
  1008. * This is normally used within the title tag in the head of the page.
  1009. *
  1010. * @param string $title the title that should go in the <head> section of the HTML of this page.
  1011. */
  1012. public function set_title($title) {
  1013. $title = format_string($title);
  1014. $title = strip_tags($title);
  1015. $title = str_replace('"', '&quot;', $title);
  1016. $this->_title = $title;
  1017. }
  1018. /**
  1019. * Sets the heading to use for the page.
  1020. * This is normally used as the main heading at the top of the content.
  1021. *
  1022. * @param string $heading the main heading that should be displayed at the top of the <body>.
  1023. */
  1024. public function set_heading($heading) {
  1025. $this->_heading = format_string($heading);
  1026. }
  1027. /**
  1028. * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
  1029. *
  1030. * @param string $menu The menu/content to show in the heading
  1031. */
  1032. public function set_headingmenu($menu) {
  1033. $this->_headingmenu = $menu;
  1034. }
  1035. /**
  1036. * Set the course category this page belongs to manually.
  1037. *
  1038. * This automatically sets $PAGE->course to be the site course. You cannot
  1039. * use this method if you have already set $PAGE->course - in that case,
  1040. * the category must be the one that the course belongs to. This also
  1041. * automatically sets the page context to the category context.
  1042. *
  1043. * @param int $categoryid The id of the category to set.
  1044. * @throws coding_exception
  1045. */
  1046. public function set_category_by_id($categoryid) {
  1047. global $SITE;
  1048. if (!is_null($this->_course)) {
  1049. throw new coding_exception('Course has already been set. You cannot change the category now.');
  1050. }
  1051. if (is_array($this->_categories)) {
  1052. throw new coding_exception('Course category has already been set. You cannot to change it now.');
  1053. }
  1054. $this->ensure_theme_not_set();
  1055. $this->set_course($SITE);
  1056. $this->load_category($categoryid);
  1057. $this->set_context(context_coursecat::instance($categoryid));
  1058. }
  1059. /**
  1060. * Set a different path to use for the 'Moodle docs for this page' link.
  1061. *
  1062. * By default, it uses the pagetype, which is normally the same as the
  1063. * script name. So, for example, for mod/quiz/attempt.php, pagetype is
  1064. * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
  1065. *
  1066. * @param string $path the path to use at the end of the moodle docs URL.
  1067. */
  1068. public function set_docs_path($path) {
  1069. $this->_docspath = $path;
  1070. }
  1071. /**
  1072. * You should call this method from every page to set the URL that should be used to return to this page.
  1073. *
  1074. * Used, for example, by the blocks editing UI to know where to return the
  1075. * user after an action.
  1076. * For example, course/view.php does:
  1077. * $id = optional_param('id', 0, PARAM_INT);
  1078. * $PAGE->set_url('/course/view.php', array('id' => $id));
  1079. *
  1080. * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
  1081. * @param array $params parameters to add to the URL
  1082. * @throws coding_exception
  1083. */
  1084. public function set_url($url, array $params = null) {
  1085. global $CFG;
  1086. if (is_string($url) && strpos($url, 'http') !== 0) {
  1087. if (strpos($url, '/') === 0) {
  1088. // We have to use httpswwwroot here, because of loginhttps pages.
  1089. $url = $CFG->httpswwwroot . $url;
  1090. } else {
  1091. throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
  1092. }
  1093. }
  1094. $this->_url = new moodle_url($url, $params);
  1095. $fullurl = $this->_url->out_omit_querystring();
  1096. if (strpos($fullurl, "$CFG->httpswwwroot/") !== 0) {
  1097. debugging('Most probably incorrect set_page() url argument, it does not match the httpswwwroot!');
  1098. }
  1099. $shorturl = str_replace("$CFG->httpswwwroot/", '', $fullurl);
  1100. if (is_null($this->_pagetype)) {
  1101. $this->initialise_default_pagetype($shorturl);
  1102. }
  1103. }
  1104. /**
  1105. * Make sure page URL does not contain the given URL parameter.
  1106. *
  1107. * This should not be necessary if the script has called set_url properly.
  1108. * However, in some situations like the block editing actions; when the URL
  1109. * has been guessed, it will contain dangerous block-related actions.
  1110. * Therefore, the blocks code calls this function to clean up such parameters
  1111. * before doing any redirect.
  1112. *
  1113. * @param string $param the name of the parameter to make sure is not in the
  1114. * page URL.
  1115. */
  1116. public function ensure_param_not_in_url($param) {
  1117. $this->_url->remove_params($param);
  1118. }
  1119. /**
  1120. * Sets an alternative version of this page.
  1121. *
  1122. * There can be alternate versions of some pages (for example an RSS feed version).
  1123. * Call this method for each alternative version available.
  1124. * For each alternative version a link will be included in the <head> tag.
  1125. *
  1126. * @param string $title The title to give the alternate version.
  1127. * @param string|moodle_url $url The URL of the alternate version.
  1128. * @param string $mimetype The mime-type of the alternate version.
  1129. * @throws coding_exception
  1130. */
  1131. public function add_alternate_version($title, $url, $mimetype) {
  1132. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1133. throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
  1134. }
  1135. $alt = new stdClass;
  1136. $alt->title = $title;
  1137. $alt->url = $url;
  1138. $this->_alternateversions[$mimetype] = $alt;
  1139. }
  1140. /**
  1141. * Specify a form control should be focused when the page has loaded.
  1142. *
  1143. * @param string $controlid the id of the HTML element to be focused.
  1144. */
  1145. public function set_focuscontrol($controlid) {
  1146. $this->_focuscontrol = $controlid;
  1147. }
  1148. /**
  1149. * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
  1150. *
  1151. * @param string $html the HTML to display there.
  1152. */
  1153. public function set_button($html) {
  1154. $this->_button = $html;
  1155. }
  1156. /**
  1157. * Set the capability that allows users to edit blocks on this page.
  1158. *
  1159. * Normally the default of 'moodle/site:manageblocks' is used, but a few
  1160. * pages like the My Moodle page need to useā€¦

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