PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/pagelib.php

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

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