PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pagelib.php

https://github.com/dongsheng/moodle
PHP | 2238 lines | 974 code | 239 blank | 1025 comment | 217 complexity | 956daab769380e3de11c6a5638021ab5 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1

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

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