PageRenderTime 64ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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's browser.
  1157. */
  1158. public function set_cacheable($cacheable) {
  1159. $this->_cacheable = $cacheable;
  1160. }
  1161. /**
  1162. * Sets the page to periodically refresh
  1163. *
  1164. * This function must be called before $OUTPUT->header has been called or
  1165. * a coding exception will be thrown.
  1166. *
  1167. * @param int $delay Sets the delay before refreshing the page, if set to null refresh is cancelled.
  1168. * @throws coding_exception
  1169. */
  1170. public function set_periodic_refresh_delay($delay = null) {
  1171. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1172. throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
  1173. }
  1174. if ($delay === null) {
  1175. $this->_periodicrefreshdelay = null;
  1176. } else if (is_int($delay)) {
  1177. $this->_periodicrefreshdelay = $delay;
  1178. }
  1179. }
  1180. /**
  1181. * Force this page to use a particular theme.
  1182. *
  1183. * Please use this cautiously.
  1184. * It is only intended to be used by the themes selector admin page.
  1185. *
  1186. * @param string $themename the name of the theme to use.
  1187. */
  1188. public function force_theme($themename) {
  1189. $this->ensure_theme_not_set();
  1190. $this->_theme = theme_config::load($themename);
  1191. }
  1192. /**
  1193. * Reload theme settings.
  1194. *
  1195. * This is used when we need to reset settings
  1196. * because they are now double cached in theme.
  1197. */
  1198. public function reload_theme() {
  1199. if (!is_null($this->_theme)) {
  1200. $this->_theme = theme_config::load($this->_theme->name);
  1201. }
  1202. }
  1203. /**
  1204. * This function indicates that current page requires the https when $CFG->loginhttps enabled.
  1205. *
  1206. * By using this function properly, we can ensure 100% https-ized pages
  1207. * at our entire discretion (login, forgot_password, change_password)
  1208. *
  1209. * @return void
  1210. * @throws coding_exception
  1211. */
  1212. public function https_required() {
  1213. global $CFG;
  1214. if (!is_null($this->_url)) {
  1215. throw new coding_exception('https_required() must be used before setting page url!');
  1216. }
  1217. $this->ensure_theme_not_set();
  1218. $this->_https_login_required = true;
  1219. if (!empty($CFG->loginhttps)) {
  1220. $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
  1221. } else {
  1222. $CFG->httpswwwroot = $CFG->wwwroot;
  1223. }
  1224. }
  1225. /**
  1226. * Makes sure that page previously marked with https_required() is really using https://, if not it redirects to https://
  1227. *
  1228. * @return void (may redirect to https://self)
  1229. * @throws coding_exception
  1230. */
  1231. public function verify_https_required() {
  1232. global $CFG, $FULLME;
  1233. if (is_null($this->_url)) {
  1234. throw new coding_exception('verify_https_required() must be called after setting page url!');
  1235. }
  1236. if (!$this->_https_login_required) {
  1237. throw new coding_exception('verify_https_required() must be called only after https_required()!');
  1238. }
  1239. if (empty($CFG->loginhttps)) {
  1240. // Https not required, so stop checking.
  1241. return;
  1242. }
  1243. if (strpos($this->_url, 'https://')) {
  1244. // Detect if incorrect PAGE->set_url() used, it is recommended to use root-relative paths there.
  1245. throw new coding_exception('Invalid page url. It must start with https:// for pages that set https_required()!');
  1246. }
  1247. if (!empty($CFG->sslproxy)) {
  1248. // It does not make much sense to use sslproxy and loginhttps at the same time.
  1249. return;
  1250. }
  1251. // Now the real test and redirect!
  1252. // NOTE: do NOT use this test for detection of https on current page because this code is not compatible with SSL proxies,
  1253. // instead use (strpos($CFG->httpswwwroot, 'https:') === 0).
  1254. if (strpos($FULLME, 'https:') !== 0) {
  1255. // This may lead to infinite redirect on an incorrectly configured site.
  1256. // In that case set $CFG->loginhttps=0; within /config.php.
  1257. redirect($this->_url);
  1258. }
  1259. }
  1260. // Initialisation methods =====================================================
  1261. // These set various things up in a default way.
  1262. /**
  1263. * This method is called when the page first moves out of the STATE_BEFORE_HEADER
  1264. * state. This is our last change to initialise things.
  1265. */
  1266. protected function starting_output() {
  1267. global $CFG;
  1268. if (!during_initial_install()) {
  1269. $this->blocks->load_blocks();
  1270. if (empty($this->_block_actions_done)) {
  1271. $this->_block_actions_done = true;
  1272. if ($this->blocks->process_url_actions($this)) {
  1273. redirect($this->url->out(false));
  1274. }
  1275. }
  1276. $this->blocks->create_all_block_instances();
  1277. }
  1278. // If maintenance mode is on, change the page header.
  1279. if (!empty($CFG->maintenance_enabled)) {
  1280. $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
  1281. '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
  1282. '</a> ' . $this->button);
  1283. $title = $this->title;
  1284. if ($title) {
  1285. $title .= ' - ';
  1286. }
  1287. $this->set_title($title . get_string('maintenancemode', 'admin'));
  1288. } else {
  1289. // Show the messaging popup if there are messages.
  1290. message_popup_window();
  1291. }
  1292. $this->initialise_standard_body_classes();
  1293. }
  1294. /**
  1295. * Method for use by Moodle core to set up the theme. Do not
  1296. * use this in your own code.
  1297. *
  1298. * Make sure the right theme for this page is loaded. Tell our
  1299. * blocks_manager about the theme block regions, and then, if
  1300. * we are $PAGE, set up the global $OUTPUT.
  1301. *
  1302. * @return void
  1303. */
  1304. public function initialise_theme_and_output() {
  1305. global $OUTPUT, $PAGE, $SITE, $CFG;
  1306. if (!empty($this->_wherethemewasinitialised)) {
  1307. return;
  1308. }
  1309. if (!during_initial_install()) {
  1310. // Detect PAGE->context mess.
  1311. $this->magic_get_context();
  1312. }
  1313. if (!$this->_course && !during_initial_install()) {
  1314. $this->set_course($SITE);
  1315. }
  1316. if (is_null($this->_theme)) {
  1317. $themename = $this->resolve_theme();
  1318. $this->_theme = theme_config::load($themename);
  1319. }
  1320. $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
  1321. if ($this->_theme->enable_dock && !empty($CFG->allowblockstodock)) {
  1322. $this->requires->strings_for_js(array('addtodock', 'undockitem', 'dockblock', 'undockblock', 'undockall', 'hidedockpanel', 'hidepanel'), 'block');
  1323. $this->requires->string_for_js('thisdirectionvertical', 'langconfig');
  1324. $this->requires->yui_module('moodle-core-dock-loader', 'M.core.dock.loader.initLoader');
  1325. }
  1326. if ($this === $PAGE) {
  1327. $target = null;
  1328. if ($this->pagelayout === 'maintenance') {
  1329. // If the page is using the maintenance layout then we're going to force target to maintenance.
  1330. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  1331. // page layout.
  1332. $target = RENDERER_TARGET_MAINTENANCE;
  1333. }
  1334. $OUTPUT = $this->get_renderer('core', null, $target);
  1335. }
  1336. $this->_wherethemewasinitialised = debug_backtrace();
  1337. }
  1338. /**
  1339. * Work out the theme this page should use.
  1340. *
  1341. * This depends on numerous $CFG settings, and the properties of this page.
  1342. *
  1343. * @return string the name of the theme that should be used on this page.
  1344. */
  1345. protected function resolve_theme() {
  1346. global $CFG, $USER, $SESSION;
  1347. if (empty($CFG->themeorder)) {
  1348. $themeorder = array('course', 'category', 'session', 'user', 'site');
  1349. } else {
  1350. $themeorder = $CFG->themeorder;
  1351. // Just in case, make sure we always use the site theme if nothing else matched.
  1352. $themeorder[] = 'site';
  1353. }
  1354. $mnetpeertheme = '';
  1355. if (isloggedin() and isset($CFG->mnet_localhost_id) and $USER->mnethostid != $CFG->mnet_localhost_id) {
  1356. require_once($CFG->dirroot.'/mnet/peer.php');
  1357. $mnetpeer = new mnet_peer();
  1358. $mnetpeer->set_id($USER->mnethostid);
  1359. if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
  1360. $mnetpeertheme = $mnetpeer->theme;
  1361. }
  1362. }
  1363. foreach ($themeorder as $themetype) {
  1364. switch ($themetype) {
  1365. case 'course':
  1366. if (!empty($CFG->allowcoursethemes) && !empty($this->_course->theme) && $this->devicetypeinuse == 'default') {
  1367. return $this->_course->theme;
  1368. }
  1369. break;
  1370. case 'category':
  1371. if (!empty($CFG->allowcategorythemes) && $this->devicetypeinuse == 'default') {
  1372. $categories = $this->categories;
  1373. foreach ($categories as $category) {
  1374. if (!empty($category->theme)) {
  1375. return $category->theme;
  1376. }
  1377. }
  1378. }
  1379. break;
  1380. case 'session':
  1381. if (!empty($SESSION->theme)) {
  1382. return $SESSION->theme;
  1383. }
  1384. break;
  1385. case 'user':
  1386. if (!empty($CFG->allowuserthemes) && !empty($USER->theme) && $this->devicetypeinuse == 'default') {
  1387. if ($mnetpeertheme) {
  1388. return $mnetpeertheme;
  1389. } else {
  1390. return $USER->theme;
  1391. }
  1392. }
  1393. break;
  1394. case 'site':
  1395. if ($mnetpeertheme) {
  1396. return $mnetpeertheme;
  1397. }
  1398. // First try for the device the user is using.
  1399. $devicetheme = core_useragent::get_device_type_theme($this->devicetypeinuse);
  1400. if (!empty($devicetheme)) {
  1401. return $devicetheme;
  1402. }
  1403. // Next try for the default device (as a fallback).
  1404. $devicetheme = core_useragent::get_device_type_theme('default');
  1405. if (!empty($devicetheme)) {
  1406. return $devicetheme;
  1407. }
  1408. // The default device theme isn't set up - use the overall default theme.
  1409. return theme_config::DEFAULT_THEME;
  1410. }
  1411. }
  1412. // We should most certainly have resolved a theme by now. Something has gone wrong.
  1413. debugging('Error resolving the theme to use for this page.', DEBUG_DEVELOPER);
  1414. return theme_config::DEFAULT_THEME;
  1415. }
  1416. /**
  1417. * Sets ->pagetype from the script name. For example, if the script that was
  1418. * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
  1419. *
  1420. * @param string $script the path to the script that should be used to
  1421. * initialise ->pagetype. If not passed the $SCRIPT global will be used.
  1422. * If legacy code has set $CFG->pagepath that will be used instead, and a
  1423. * developer warning issued.
  1424. */
  1425. protected function initialise_default_pagetype($script = null) {
  1426. global $CFG, $SCRIPT;
  1427. if (isset($CFG->pagepath)) {
  1428. debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
  1429. 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
  1430. $script = $CFG->pagepath;
  1431. unset($CFG->pagepath);
  1432. }
  1433. if (is_null($script)) {
  1434. $script = ltrim($SCRIPT, '/');
  1435. $len = strlen($CFG->admin);
  1436. if (substr($script, 0, $len) == $CFG->admin) {
  1437. $script = 'admin' . substr($script, $len);
  1438. }
  1439. }
  1440. $path = str_replace('.php', '', $script);
  1441. if (substr($path, -1) == '/') {
  1442. $path .= 'index';
  1443. }
  1444. if (empty($path) || $path == 'index') {
  1445. $this->_pagetype = 'site-index';
  1446. } else {
  1447. $this->_pagetype = str_replace('/', '-', $path);
  1448. }
  1449. }
  1450. /**
  1451. * Initialises the CSS classes that will be added to body tag of the page.
  1452. *
  1453. * The function is responsible for adding all of the critical CSS classes
  1454. * that describe the current page, and its state.
  1455. * This includes classes that describe the following for example:
  1456. * - Current language
  1457. * - Language direction
  1458. * - YUI CSS initialisation
  1459. * - Pagelayout
  1460. * These are commonly used in CSS to target specific types of pages.
  1461. */
  1462. protected function initialise_standard_body_classes() {
  1463. global $CFG, $USER;
  1464. $pagetype = $this->pagetype;
  1465. if ($pagetype == 'site-index') {
  1466. $this->_legacyclass = 'course';
  1467. } else if (substr($pagetype, 0, 6) == 'admin-') {
  1468. $this->_legacyclass = 'admin';
  1469. }
  1470. $this->add_body_class($this->_legacyclass);
  1471. $pathbits = explode('-', trim($pagetype));
  1472. for ($i = 1; $i < count($pathbits); $i++) {
  1473. $this->add_body_class('path-' . join('-', array_slice($pathbits, 0, $i)));
  1474. }
  1475. $this->add_body_classes(core_useragent::get_browser_version_classes());
  1476. $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
  1477. $this->add_body_class('lang-' . current_language());
  1478. $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
  1479. $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
  1480. $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
  1481. // Extra class describing current page layout.
  1482. $this->add_body_class('pagelayout-' . $this->_pagelayout);
  1483. if (!during_initial_install()) {
  1484. $this->add_body_class('course-' . $this->_course->id);
  1485. $this->add_body_class('context-' . $this->_context->id);
  1486. }
  1487. if (!empty($this->_cm)) {
  1488. $this->add_body_class('cmid-' . $this->_cm->id);
  1489. }
  1490. if (!empty($CFG->allowcategorythemes)) {
  1491. $this->ensure_category_loaded();
  1492. foreach ($this->_categories as $catid => $notused) {
  1493. $this->add_body_class('category-' . $catid);
  1494. }
  1495. } else {
  1496. $catid = 0;
  1497. if (is_array($this->_categories)) {
  1498. $catids = array_keys($this->_categories);
  1499. $catid = reset($catids);
  1500. } else if (!empty($this->_course->category)) {
  1501. $catid = $this->_course->category;
  1502. }
  1503. if ($catid) {
  1504. $this->add_body_class('category-' . $catid);
  1505. }
  1506. }
  1507. if (!isloggedin()) {
  1508. $this->add_body_class('notloggedin');
  1509. }
  1510. if (!empty($USER->editing)) {
  1511. $this->add_body_class('editing');
  1512. if (optional_param('bui_moveid', false, PARAM_INT)) {
  1513. $this->add_body_class('blocks-moving');
  1514. }
  1515. }
  1516. if (!empty($CFG->blocksdrag)) {
  1517. $this->add_body_class('drag');
  1518. }
  1519. if ($this->_devicetypeinuse != 'default') {
  1520. $this->add_body_class($this->_devicetypeinuse . 'theme');
  1521. }
  1522. }
  1523. /**
  1524. * Loads the activity record for the current CM object associated with this
  1525. * page.
  1526. *
  1527. * This will load {@link moodle_page::$_module} with a row from the related
  1528. * module table in the database.
  1529. * For instance if {@link moodle_page::$_cm} is a forum then a row from the
  1530. * forum table will be loaded.
  1531. */
  1532. protected function load_activity_record() {
  1533. global $DB;
  1534. if (is_null($this->_cm)) {
  1535. return;
  1536. }
  1537. $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
  1538. }
  1539. /**
  1540. * This function ensures that the category of the current course has been
  1541. * loaded, and if not, the function loads it now.
  1542. *
  1543. * @return void
  1544. * @throws coding_exception
  1545. */
  1546. protected function ensure_category_loaded() {
  1547. if (is_array($this->_categories)) {
  1548. return; // Already done.
  1549. }
  1550. if (is_null($this->_course)) {
  1551. throw new coding_exception('Attempt to get the course category for this page before the course was set.');
  1552. }
  1553. if ($this->_course->category == 0) {
  1554. $this->_categories = array();
  1555. } else {
  1556. $this->load_category($this->_course->category);
  1557. }
  1558. }
  1559. /**
  1560. * Loads the requested category into the pages categories array.
  1561. *
  1562. * @param int $categoryid
  1563. * @throws moodle_exception
  1564. */
  1565. protected function load_category($categoryid) {
  1566. global $DB;
  1567. $category = $DB->get_record('course_categories', array('id' => $categoryid));
  1568. if (!$category) {
  1569. throw new moodle_exception('unknowncategory');
  1570. }
  1571. $this->_categories[$category->id] = $category;
  1572. $parentcategoryids = explode('/', trim($category->path, '/'));
  1573. array_pop($parentcategoryids);
  1574. foreach (array_reverse($parentcategoryids) as $catid) {
  1575. $this->_categories[$catid] = null;
  1576. }
  1577. }
  1578. /**
  1579. * Ensures that the category the current course is within, as well as all of
  1580. * its parent categories, have been loaded.
  1581. *
  1582. * @return void
  1583. */
  1584. protected function ensure_categories_loaded() {
  1585. global $DB;
  1586. $this->ensure_category_loaded();
  1587. if (!is_null(end($this->_categories))) {
  1588. return; // Already done.
  1589. }
  1590. $idstoload = array_keys($this->_categories);
  1591. array_shift($idstoload);
  1592. $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
  1593. foreach ($idstoload as $catid) {
  1594. $this->_categories[$catid] = $categories[$catid];
  1595. }
  1596. }
  1597. /**
  1598. * Ensure the theme has not been loaded yet. If it has an exception is thrown.
  1599. * @source
  1600. *
  1601. * @throws coding_exception
  1602. */
  1603. protected function ensure_theme_not_set() {
  1604. if (!is_null($this->_theme)) {
  1605. throw new coding_exception('The theme has already been set up for this page ready for output. ' .
  1606. 'Therefore, you can no longer change the theme, or anything that might affect what ' .
  1607. 'the current theme is, for example, the course.',
  1608. 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
  1609. }
  1610. }
  1611. /**
  1612. * Converts the provided URL into a CSS class that be used within the page.
  1613. * This is primarily used to add the wwwroot to the body tag as a CSS class.
  1614. *
  1615. * @param string $url
  1616. * @return string
  1617. */
  1618. protected function url_to_class_name($url) {
  1619. $bits = parse_url($url);
  1620. $class = str_replace('.', '-', $bits['host']);
  1621. if (!empty($bits['port'])) {
  1622. $class .= '--' . $bits['port'];
  1623. }
  1624. if (!empty($bits['path'])) {
  1625. $path = trim($bits['path'], '/');
  1626. if ($path) {
  1627. $class .= '--' . str_replace('/', '-', $path);
  1628. }
  1629. }
  1630. return $class;
  1631. }
  1632. /**
  1633. * Combines all of the required editing caps for the page and returns them
  1634. * as an array.
  1635. *
  1636. * @return array
  1637. */
  1638. protected function all_editing_caps() {
  1639. $caps = $this->_othereditingcaps;
  1640. $caps[] = $this->_blockseditingcap;
  1641. return $caps;
  1642. }
  1643. /**
  1644. * Returns true if the page URL has beem set.
  1645. *
  1646. * @return bool
  1647. */
  1648. public function has_set_url() {
  1649. return ($this->_url!==null);
  1650. }
  1651. /**
  1652. * Gets set when the block actions for the page have been processed.
  1653. *
  1654. * @param bool $setting
  1655. */
  1656. public function set_block_actions_done($setting = true) {
  1657. $this->_block_actions_done = $setting;
  1658. }
  1659. /**
  1660. * Are popup notifications allowed on this page?
  1661. * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
  1662. *
  1663. * @return bool true if popup notifications may be displayed
  1664. */
  1665. public function get_popup_notification_allowed() {
  1666. return $this->_popup_notification_allowed;
  1667. }
  1668. /**
  1669. * Allow or disallow popup notifications on this page. Popups are allowed by default.
  1670. *
  1671. * @param bool $allowed true if notifications are allowed. False if not allowed. They are allowed by default.
  1672. */
  1673. public function set_popup_notification_allowed($allowed) {
  1674. $this->_popup_notification_allowed = $allowed;
  1675. }
  1676. /**
  1677. * Returns the block region having made any required theme manipulations.
  1678. *
  1679. * @since 2.5.1 2.6
  1680. * @param string $region
  1681. * @return string
  1682. */
  1683. public function apply_theme_region_manipulations($region) {
  1684. if ($this->blockmanipulations && isset($this->blockmanipulations[$region])) {
  1685. $regionwas = $region;
  1686. $regionnow = $this->blockmanipulations[$region];
  1687. if ($this->blocks->is_known_region($regionwas) && $this->blocks->is_known_region($regionnow)) {
  1688. // Both the before and after regions are known so we can swap them over.
  1689. return $regionnow;
  1690. }
  1691. // We didn't know about both, we won't swap them over.
  1692. return $regionwas;
  1693. }
  1694. return $region;
  1695. }
  1696. }