PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pagelib.php

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

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