PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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. }
  1159. $this->_bodyclasses[$class] = 1;
  1160. }
  1161. /**
  1162. * Adds an array of body classes to the body tag of this page.
  1163. *
  1164. * @param array $classes this utility method calls add_body_class for each array element.
  1165. */
  1166. public function add_body_classes($classes) {
  1167. foreach ($classes as $class) {
  1168. $this->add_body_class($class);
  1169. }
  1170. }
  1171. /**
  1172. * Sets the title for the page.
  1173. * This is normally used within the title tag in the head of the page.
  1174. *
  1175. * @param string $title the title that should go in the <head> section of the HTML of this page.
  1176. */
  1177. public function set_title($title) {
  1178. $title = format_string($title);
  1179. $title = strip_tags($title);
  1180. $title = str_replace('"', '&quot;', $title);
  1181. $this->_title = $title;
  1182. }
  1183. /**
  1184. * Sets the heading to use for the page.
  1185. * This is normally used as the main heading at the top of the content.
  1186. *
  1187. * @param string $heading the main heading that should be displayed at the top of the <body>.
  1188. * @param bool $applyformatting apply format_string() - by default true.
  1189. */
  1190. public function set_heading($heading, bool $applyformatting = true) {
  1191. $this->_heading = $applyformatting ? format_string($heading) : clean_text($heading);
  1192. }
  1193. /**
  1194. * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
  1195. *
  1196. * @param string $menu The menu/content to show in the heading
  1197. */
  1198. public function set_headingmenu($menu) {
  1199. $this->_headingmenu = $menu;
  1200. }
  1201. /**
  1202. * Set the course category this page belongs to manually.
  1203. *
  1204. * This automatically sets $PAGE->course to be the site course. You cannot
  1205. * use this method if you have already set $PAGE->course - in that case,
  1206. * the category must be the one that the course belongs to. This also
  1207. * automatically sets the page context to the category context.
  1208. *
  1209. * @param int $categoryid The id of the category to set.
  1210. * @throws coding_exception
  1211. */
  1212. public function set_category_by_id($categoryid) {
  1213. global $SITE;
  1214. if (!is_null($this->_course)) {
  1215. throw new coding_exception('Course has already been set. You cannot change the category now.');
  1216. }
  1217. if (is_array($this->_categories)) {
  1218. throw new coding_exception('Course category has already been set. You cannot to change it now.');
  1219. }
  1220. $this->ensure_theme_not_set();
  1221. $this->set_course($SITE);
  1222. $this->load_category($categoryid);
  1223. $this->set_context(context_coursecat::instance($categoryid));
  1224. }
  1225. /**
  1226. * Set a different path to use for the 'Help and documentation' link.
  1227. *
  1228. * By default, it uses the pagetype, which is normally the same as the
  1229. * script name. So, for example, for mod/quiz/attempt.php, pagetype is
  1230. * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
  1231. *
  1232. * @param string $path the path to use at the end of the moodle docs URL.
  1233. */
  1234. public function set_docs_path($path) {
  1235. $this->_docspath = $path;
  1236. }
  1237. /**
  1238. * You should call this method from every page to set the URL that should be used to return to this page.
  1239. *
  1240. * Used, for example, by the blocks editing UI to know where to return the
  1241. * user after an action.
  1242. * For example, course/view.php does:
  1243. * $id = optional_param('id', 0, PARAM_INT);
  1244. * $PAGE->set_url('/course/view.php', array('id' => $id));
  1245. *
  1246. * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
  1247. * @param array $params parameters to add to the URL
  1248. * @throws coding_exception
  1249. */
  1250. public function set_url($url, array $params = null) {
  1251. global $CFG;
  1252. if (is_string($url) && strpos($url, 'http') !== 0) {
  1253. if (strpos($url, '/') === 0) {
  1254. // Add the wwwroot to the relative url.
  1255. $url = $CFG->wwwroot . $url;
  1256. } else {
  1257. throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
  1258. }
  1259. }
  1260. $this->_url = new moodle_url($url, $params);
  1261. $fullurl = $this->_url->out_omit_querystring();
  1262. if (strpos($fullurl, "$CFG->wwwroot/") !== 0) {
  1263. debugging('Most probably incorrect set_page() url argument, it does not match the wwwroot!');
  1264. }
  1265. $shorturl = str_replace("$CFG->wwwroot/", '', $fullurl);
  1266. if (is_null($this->_pagetype)) {
  1267. $this->initialise_default_pagetype($shorturl);
  1268. }
  1269. }
  1270. /**
  1271. * Make sure page URL does not contain the given URL parameter.
  1272. *
  1273. * This should not be necessary if the script has called set_url properly.
  1274. * However, in some situations like the block editing actions; when the URL
  1275. * has been guessed, it will contain dangerous block-related actions.
  1276. * Therefore, the blocks code calls this function to clean up such parameters
  1277. * before doing any redirect.
  1278. *
  1279. * @param string $param the name of the parameter to make sure is not in the
  1280. * page URL.
  1281. */
  1282. public function ensure_param_not_in_url($param) {
  1283. $this->_url->remove_params($param);
  1284. }
  1285. /**
  1286. * Sets an alternative version of this page.
  1287. *
  1288. * There can be alternate versions of some pages (for example an RSS feed version).
  1289. * Call this method for each alternative version available.
  1290. * For each alternative version a link will be included in the <head> tag.
  1291. *
  1292. * @param string $title The title to give the alternate version.
  1293. * @param string|moodle_url $url The URL of the alternate version.
  1294. * @param string $mimetype The mime-type of the alternate version.
  1295. * @throws coding_exception
  1296. */
  1297. public function add_alternate_version($title, $url, $mimetype) {
  1298. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1299. throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
  1300. }
  1301. $alt = new stdClass;
  1302. $alt->title = $title;
  1303. $alt->url = $url;
  1304. $this->_alternateversions[$mimetype] = $alt;
  1305. }
  1306. /**
  1307. * Specify a form control should be focused when the page has loaded.
  1308. *
  1309. * @param string $controlid the id of the HTML element to be focused.
  1310. */
  1311. public function set_focuscontrol($controlid) {
  1312. $this->_focuscontrol = $controlid;
  1313. }
  1314. /**
  1315. * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
  1316. *
  1317. * @param string $html the HTML to display there.
  1318. */
  1319. public function set_button($html) {
  1320. $this->_button = $html;
  1321. }
  1322. /**
  1323. * Set the capability that allows users to edit blocks on this page.
  1324. *
  1325. * Normally the default of 'moodle/site:manageblocks' is used, but a few
  1326. * pages like the My Moodle page need to use a different capability
  1327. * like 'moodle/my:manageblocks'.
  1328. *
  1329. * @param string $capability a capability.
  1330. */
  1331. public function set_blocks_editing_capability($capability) {
  1332. $this->_blockseditingcap = $capability;
  1333. }
  1334. /**
  1335. * Some pages let you turn editing on for reasons other than editing blocks.
  1336. * If that is the case, you can pass other capabilities that let the user
  1337. * edit this page here.
  1338. *
  1339. * @param string|array $capability either a capability, or an array of capabilities.
  1340. */
  1341. public function set_other_editing_capability($capability) {
  1342. if (is_array($capability)) {
  1343. $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
  1344. } else {
  1345. $this->_othereditingcaps[] = $capability;
  1346. }
  1347. }
  1348. /**
  1349. * Sets whether the browser should cache this page or not.
  1350. *
  1351. * @param bool $cacheable can this page be cached by the user's browser.
  1352. */
  1353. public function set_cacheable($cacheable) {
  1354. $this->_cacheable = $cacheable;
  1355. }
  1356. /**
  1357. * Sets the page to periodically refresh
  1358. *
  1359. * This function must be called before $OUTPUT->header has been called or
  1360. * a coding exception will be thrown.
  1361. *
  1362. * @param int $delay Sets the delay before refreshing the page, if set to null refresh is cancelled.
  1363. * @throws coding_exception
  1364. */
  1365. public function set_periodic_refresh_delay($delay = null) {
  1366. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1367. throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
  1368. }
  1369. if ($delay === null) {
  1370. $this->_periodicrefreshdelay = null;
  1371. } else if (is_int($delay)) {
  1372. $this->_periodicrefreshdelay = $delay;
  1373. }
  1374. }
  1375. /**
  1376. * Force this page to use a particular theme.
  1377. *
  1378. * Please use this cautiously.
  1379. * It is only intended to be used by the themes selector admin page.
  1380. *
  1381. * @param string $themename the name of the theme to use.
  1382. */
  1383. public function force_theme($themename) {
  1384. $this->ensure_theme_not_set();
  1385. $this->_theme = theme_config::load($themename);
  1386. }
  1387. /**
  1388. * Reload theme settings.
  1389. *
  1390. * This is used when we need to reset settings
  1391. * because they are now double cached in theme.
  1392. */
  1393. public function reload_theme() {
  1394. if (!is_null($this->_theme)) {
  1395. $this->_theme = theme_config::load($this->_theme->name);
  1396. }
  1397. }
  1398. /**
  1399. * @deprecated since Moodle 3.4
  1400. */
  1401. public function https_required() {
  1402. throw new coding_exception('https_required() cannot be used anymore.');
  1403. }
  1404. /**
  1405. * @deprecated since Moodle 3.4
  1406. */
  1407. public function verify_https_required() {
  1408. throw new coding_exception('verify_https_required() cannot be used anymore.');
  1409. }
  1410. // Initialisation methods =====================================================
  1411. // These set various things up in a default way.
  1412. /**
  1413. * This method is called when the page first moves out of the STATE_BEFORE_HEADER
  1414. * state. This is our last change to initialise things.
  1415. */
  1416. protected function starting_output() {
  1417. global $CFG;
  1418. if (!during_initial_install()) {
  1419. $this->blocks->load_blocks();
  1420. if (empty($this->_block_actions_done)) {
  1421. $this->_block_actions_done = true;
  1422. if ($this->blocks->process_url_actions($this)) {
  1423. redirect($this->url->out(false));
  1424. }
  1425. }
  1426. $this->blocks->create_all_block_instances();
  1427. }
  1428. // If maintenance mode is on, change the page header.
  1429. if (!empty($CFG->maintenance_enabled)) {
  1430. $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
  1431. '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
  1432. '</a> ' . $this->button);
  1433. $title = $this->title;
  1434. if ($title) {
  1435. $title .= ' - ';
  1436. }
  1437. $this->set_title($title . get_string('maintenancemode', 'admin'));
  1438. }
  1439. $this->initialise_standard_body_classes();
  1440. }
  1441. /**
  1442. * Method for use by Moodle core to set up the theme. Do not
  1443. * use this in your own code.
  1444. *
  1445. * Make sure the right theme for this page is loaded. Tell our
  1446. * blocks_manager about the theme block regions, and then, if
  1447. * we are $PAGE, set up the global $OUTPUT.
  1448. *
  1449. * @return void
  1450. */
  1451. public function initialise_theme_and_output() {
  1452. global $OUTPUT, $PAGE, $SITE, $CFG;
  1453. if (!empty($this->_wherethemewasinitialised)) {
  1454. return;
  1455. }
  1456. if (!during_initial_install()) {
  1457. // Detect PAGE->context mess.
  1458. $this->magic_get_context();
  1459. }
  1460. if (!$this->_course && !during_initial_install()) {
  1461. $this->set_course($SITE);
  1462. }
  1463. if (is_null($this->_theme)) {
  1464. $themename = $this->resolve_theme();
  1465. $this->_theme = theme_config::load($themename);
  1466. }
  1467. $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
  1468. if ($this === $PAGE) {
  1469. $target = null;
  1470. if ($this->pagelayout === 'maintenance') {
  1471. // If the page is using the maintenance layout then we're going to force target to maintenance.
  1472. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  1473. // page layout.
  1474. $target = RENDERER_TARGET_MAINTENANCE;
  1475. }
  1476. $OUTPUT = $this->get_renderer('core', null, $target);
  1477. }
  1478. if (!during_initial_install()) {
  1479. $filtermanager = filter_manager::instance();
  1480. $filtermanager->setup_page_for_globally_available_filters($this);
  1481. }
  1482. $this->_wherethemewasinitialised = debug_backtrace();
  1483. }
  1484. /**
  1485. * For diagnostic/debugging purposes, find where the theme setup was triggered.
  1486. *
  1487. * @return null|array null if theme not yet setup. Stacktrace if it was.
  1488. */
  1489. public function get_where_theme_was_initialised() {
  1490. return $this->_wherethemewasinitialised;
  1491. }
  1492. /**
  1493. * Reset the theme and output for a new context. This only makes sense from
  1494. * external::validate_context(). Do not cheat.
  1495. *
  1496. * @return string the name of the theme that should be used on this page.
  1497. */
  1498. public function reset_theme_and_output() {
  1499. global $COURSE, $SITE;
  1500. $COURSE = clone($SITE);
  1501. $this->_theme = null;
  1502. $this->_wherethemewasinitialised = null;
  1503. $this->_course = null;
  1504. $this->_cm = null;
  1505. $this->_module = null;
  1506. $this->_context = null;
  1507. }
  1508. /**
  1509. * Work out the theme this page should use.
  1510. *
  1511. * This depends on numerous $CFG settings, and the properties of this page.
  1512. *
  1513. * @return string the name of the theme that should be used on this page.
  1514. */
  1515. protected function resolve_theme() {
  1516. global $CFG, $USER, $SESSION;
  1517. if (empty($CFG->themeorder)) {
  1518. $themeorder = array('course', 'category', 'session', 'user', 'cohort', 'site');
  1519. } else {
  1520. $themeorder = $CFG->themeorder;
  1521. // Just in case, make sure we always use the site theme if nothing else matched.
  1522. $themeorder[] = 'site';
  1523. }
  1524. $mnetpeertheme = '';
  1525. $mnetvarsok = isset($CFG->mnet_localhost_id) && isset($USER->mnethostid);
  1526. if (isloggedin() and $mnetvarsok and $USER->mnethostid != $CFG->mnet_localhost_id) {
  1527. require_once($CFG->dirroot.'/mnet/peer.php');
  1528. $mnetpeer = new mnet_peer();
  1529. $mnetpeer->set_id($USER->mnethostid);
  1530. if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
  1531. $mnetpeertheme = $mnetpeer->theme;
  1532. }
  1533. }
  1534. $devicetheme = core_useragent::get_device_type_theme($this->devicetypeinuse);
  1535. // The user is using another device than default, and we have a theme for that, we should use it.
  1536. $hascustomdevicetheme = core_useragent::DEVICETYPE_DEFAULT != $this->devicetypeinuse && !empty($devicetheme);
  1537. foreach ($themeorder as $themetype) {
  1538. switch ($themetype) {
  1539. case 'course':
  1540. if (!empty($CFG->allowcoursethemes) && !empty($this->_course->theme) && !$hascustomdevicetheme) {
  1541. return $this->_course->theme;
  1542. }
  1543. break;
  1544. case 'category':
  1545. if (!empty($CFG->allowcategorythemes) && !$hascustomdevicetheme) {
  1546. $categories = $this->categories;
  1547. foreach ($categories as $category) {
  1548. if (!empty($category->theme)) {
  1549. return $category->theme;
  1550. }
  1551. }
  1552. }
  1553. break;
  1554. case 'session':
  1555. if (!empty($SESSION->theme)) {
  1556. return $SESSION->theme;
  1557. }
  1558. break;
  1559. case 'user':
  1560. if (!empty($CFG->allowuserthemes) && !empty($USER->theme) && !$hascustomdevicetheme) {
  1561. if ($mnetpeertheme) {
  1562. return $mnetpeertheme;
  1563. } else {
  1564. return $USER->theme;
  1565. }
  1566. }
  1567. break;
  1568. case 'cohort':
  1569. if (!empty($CFG->allowcohortthemes) && !empty($USER->cohorttheme) && !$hascustomdevicetheme) {
  1570. return $USER->cohorttheme;
  1571. }
  1572. break;
  1573. case 'site':
  1574. if ($mnetpeertheme) {
  1575. return $mnetpeertheme;
  1576. }
  1577. // First try for the device the user is using.
  1578. if (!empty($devicetheme)) {
  1579. return $devicetheme;
  1580. }
  1581. // Next try for the default device (as a fallback).
  1582. $devicetheme = core_useragent::get_device_type_theme(core_useragent::DEVICETYPE_DEFAULT);
  1583. if (!empty($devicetheme)) {
  1584. return $devicetheme;
  1585. }
  1586. // The default device theme isn't set up - use the overall default theme.
  1587. return theme_config::DEFAULT_THEME;
  1588. }
  1589. }
  1590. // We should most certainly have resolved a theme by now. Something has gone wrong.
  1591. debugging('Error resolving the theme to use for this page.', DEBUG_DEVELOPER);
  1592. return theme_config::DEFAULT_THEME;
  1593. }
  1594. /**
  1595. * Sets ->pagetype from the script name. For example, if the script that was
  1596. * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
  1597. *
  1598. * @param string $script the path to the script that should be used to
  1599. * initialise ->pagetype. If not passed the $SCRIPT global will be used.
  1600. * If legacy code has set $CFG->pagepath that will be used instead, and a
  1601. * developer warning issued.
  1602. */
  1603. protected function initialise_default_pagetype($script = null) {
  1604. global $CFG, $SCRIPT;
  1605. if (isset($CFG->pagepath)) {
  1606. debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
  1607. 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
  1608. $script = $CFG->pagepath;
  1609. unset($CFG->pagepath);
  1610. }
  1611. if (is_null($script)) {
  1612. $script = ltrim($SCRIPT, '/');
  1613. $len = strlen($CFG->admin);
  1614. if (substr($script, 0, $len) == $CFG->admin) {
  1615. $script = 'admin' . substr($script, $len);
  1616. }
  1617. }
  1618. $path = str_replace('.php', '', $script);
  1619. if (substr($path, -1) == '/') {
  1620. $path .= 'index';
  1621. }
  1622. if (empty($path) || $path == 'index') {
  1623. $this->_pagetype = 'site-index';
  1624. } else {
  1625. $this->_pagetype = str_replace('/', '-', $path);
  1626. }
  1627. }
  1628. /**
  1629. * Initialises the CSS classes that will be added to body tag of the page.
  1630. *
  1631. * The function is responsible for adding all of the critical CSS classes
  1632. * that describe the current page, and its state.
  1633. * This includes classes that describe the following for example:
  1634. * - Current language
  1635. * - Language direction
  1636. * - YUI CSS initialisation
  1637. * - Pagelayout
  1638. * These are commonly used in CSS to target specific types of pages.
  1639. */
  1640. protected function initialise_standard_body_classes() {
  1641. global $CFG, $USER;
  1642. $pagetype = $this->pagetype;
  1643. if ($pagetype == 'site-index') {
  1644. $this->_legacyclass = 'course';
  1645. } else if (substr($pagetype, 0, 6) == 'admin-') {
  1646. $this->_legacyclass = 'admin';
  1647. }
  1648. $this->add_body_class($this->_legacyclass);
  1649. $pathbits = explode('-', trim($pagetype));
  1650. for ($i = 1; $i < count($pathbits); $i++) {
  1651. $this->add_body_class('path-' . join('-', array_slice($pathbits, 0, $i)));
  1652. }
  1653. $this->add_body_classes(core_useragent::get_browser_version_classes());
  1654. $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
  1655. $this->add_body_class('lang-' . current_language());
  1656. $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
  1657. $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
  1658. $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
  1659. // Extra class describing current page layout.
  1660. $this->add_body_class('pagelayout-' . $this->_pagelayout);
  1661. if (!during_initial_install()) {
  1662. $this->add_body_class('course-' . $this->_course->id);
  1663. $this->add_body_class('context-' . $this->_context->id);
  1664. }
  1665. if (!empty($this->_cm)) {
  1666. $this->add_body_class('cmid-' . $this->_cm->id);
  1667. }
  1668. if (!empty($CFG->allowcategorythemes)) {
  1669. $this->ensure_category_loaded();
  1670. foreach ($this->_categories as $catid => $notused) {
  1671. $this->add_body_class('category-' . $catid);
  1672. }
  1673. } else {
  1674. $catid = 0;
  1675. if (is_array($this->_categories)) {
  1676. $catids = array_keys($this->_categories);
  1677. $catid = reset($catids);
  1678. } else if (!empty($this->_course->category)) {
  1679. $catid = $this->_course->category;
  1680. }
  1681. if ($catid) {
  1682. $this->add_body_class('category-' . $catid);
  1683. }
  1684. }
  1685. if (!isloggedin()) {
  1686. $this->add_body_class('notloggedin');
  1687. }
  1688. if ($this->user_is_editing()) {
  1689. $this->add_body_class('editing');
  1690. if (optional_param('bui_moveid', false, PARAM_INT)) {
  1691. $this->add_body_class('blocks-moving');
  1692. }
  1693. }
  1694. if (!empty($CFG->blocksdrag)) {
  1695. $this->add_body_class('drag');
  1696. }
  1697. if ($this->_devicetypeinuse != 'default') {
  1698. $this->add_body_class($this->_devicetypeinuse . 'theme');
  1699. }
  1700. // Add class for behat site to apply behat related fixes.
  1701. if (defined('BEHAT_SITE_RUNNING')) {
  1702. $this->add_body_class('behat-site');
  1703. }
  1704. }
  1705. /**
  1706. * Loads the activity record for the current CM object associated with this
  1707. * page.
  1708. *
  1709. * This will load {@link moodle_page::$_module} with a row from the related
  1710. * module table in the database.
  1711. * For instance if {@link moodle_page::$_cm} is a forum then a row from the
  1712. * forum table will be loaded.
  1713. */
  1714. protected function load_activity_record() {
  1715. global $DB;
  1716. if (is_null($this->_cm)) {
  1717. return;
  1718. }
  1719. $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
  1720. }
  1721. /**
  1722. * This function ensures that the category of the current course has been
  1723. * loaded, and if not, the function loads it now.
  1724. *
  1725. * @return void
  1726. * @throws coding_exception
  1727. */
  1728. protected function ensure_category_loaded() {
  1729. if (is_array($this->_categories)) {
  1730. return; // Already done.
  1731. }
  1732. if (is_null($this->_course)) {
  1733. throw new coding_exception('Attempt to get the course category for this page before the course was set.');
  1734. }
  1735. if ($this->_course->category == 0) {
  1736. $this->_categories = array();
  1737. } else {
  1738. $this->load_category($this->_course->category);
  1739. }
  1740. }
  1741. /**
  1742. * Loads the requested category into the pages categories array.
  1743. *
  1744. * @param int $categoryid
  1745. * @throws moodle_exception
  1746. */
  1747. protected function load_category($categoryid) {
  1748. global $DB;
  1749. $category = $DB->get_record('course_categories', array('id' => $categoryid));
  1750. if (!$category) {
  1751. throw new moodle_exception('unknowncategory');
  1752. }
  1753. $this->_categories[$category->id] = $category;
  1754. $parentcategoryids = explode('/', trim($category->path, '/'));
  1755. array_pop($parentcategoryids);
  1756. foreach (array_reverse($parentcategoryids) as $catid) {
  1757. $this->_categories[$catid] = null;
  1758. }
  1759. }
  1760. /**
  1761. * Ensures that the category the current course is within, as well as all of
  1762. * its parent categories, have been loaded.
  1763. *
  1764. * @return void
  1765. */
  1766. protected function ensure_categories_loaded() {
  1767. global $DB;
  1768. $this->ensure_category_loaded();
  1769. if (!is_null(end($this->_categories))) {
  1770. return; // Already done.
  1771. }
  1772. $idstoload = array_keys($this->_categories);
  1773. array_shift($idstoload);
  1774. $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
  1775. foreach ($idstoload as $catid) {
  1776. $this->_categories[$catid] = $categories[$catid];
  1777. }
  1778. }
  1779. /**
  1780. * Ensure the theme has not been loaded yet. If it has an exception is thrown.
  1781. *
  1782. * @throws coding_exception
  1783. */
  1784. protected function ensure_theme_not_set() {
  1785. // This is explicitly allowed for webservices though which may process many course contexts in a single request.
  1786. if (WS_SERVER) {
  1787. return;
  1788. }
  1789. if (!is_null($this->_theme)) {
  1790. throw new coding_exception('The theme has already been set up for this page ready for output. ' .
  1791. 'Therefore, you can no longer change the theme, or anything that might affect what ' .
  1792. 'the current theme is, for example, the course.',
  1793. 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
  1794. }
  1795. }
  1796. /**
  1797. * Converts the provided URL into a CSS class that be used within the page.
  1798. * This is primarily used to add the wwwroot to the body tag as a CSS class.
  1799. *
  1800. * @param string $url
  1801. * @return string
  1802. */
  1803. protected function url_to_class_name($url) {
  1804. $bits = parse_url($url);
  1805. $class = str_replace('.', '-', $bits['host']);
  1806. if (!empty($bits['port'])) {
  1807. $class .= '--' . $bits['port'];
  1808. }
  1809. if (!empty($bits['path'])) {
  1810. $path = trim($bits['path'], '/');
  1811. if ($path) {
  1812. $class .= '--' . str_replace('/', '-', $path);
  1813. }
  1814. }
  1815. return $class;
  1816. }
  1817. /**
  1818. * Combines all of the required editing caps for the page and returns them
  1819. * as an array.
  1820. *
  1821. * @return array
  1822. */
  1823. protected function all_editing_caps() {
  1824. $caps = $this->_othereditingcaps;
  1825. $caps[] = $this->_blockseditingcap;
  1826. return $caps;
  1827. }
  1828. /**
  1829. * Returns true if the page URL has beem set.
  1830. *
  1831. * @return bool
  1832. */
  1833. public function has_set_url() {
  1834. return ($this->_url!==null);
  1835. }
  1836. /**
  1837. * Gets set when the block actions for the page have been processed.
  1838. *
  1839. * @param bool $setting
  1840. */
  1841. public function set_block_actions_done($setting = true) {
  1842. $this->_block_actions_done = $setting;
  1843. }
  1844. /**
  1845. * Are popup notifications allowed on this page?
  1846. * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
  1847. *
  1848. * @return bool true if popup notifications may be displayed
  1849. */
  1850. public function get_popup_notification_allowed() {
  1851. return $this->_popup_notification_allowed;
  1852. }
  1853. /**
  1854. * Allow or disallow popup notifications on this page. Popups are allowed by default.
  1855. *
  1856. * @param bool $allowed true if notifications are allowed. False if not allowed. They are allowed by default.
  1857. */
  1858. public function set_popup_notification_allowed($allowed) {
  1859. $this->_popup_notification_allowed = $allowed;
  1860. }
  1861. /**
  1862. * Returns the block region having made any required theme manipulations.
  1863. *
  1864. * @since Moodle 2.5.1 2.6
  1865. * @param string $region
  1866. * @return string
  1867. */
  1868. public function apply_theme_region_manipulations($region) {
  1869. if ($this->blockmanipulations && isset($this->blockmanipulations[$region])) {
  1870. $regionwas = $region;
  1871. $regionnow = $this->blockmanipulations[$region];
  1872. if ($this->blocks->is_known_region($regionwas) && $this->blocks->is_known_region($regionnow)) {
  1873. // Both the before and after regions are known so we can swap them over.
  1874. return $regionnow;
  1875. }
  1876. // We didn't know about both, we won't swap them over.
  1877. return $regionwas;
  1878. }
  1879. return $region;
  1880. }
  1881. /**
  1882. * Add a report node and a specific report to the navigation.
  1883. *
  1884. * @param int $userid The user ID that we are looking to add this report node to.
  1885. * @param array $nodeinfo Name and url of the final node that we are creating.
  1886. */
  1887. public function add_report_nodes($userid, $nodeinfo) {
  1888. global $USER;
  1889. // Try to find the specific user node.
  1890. $newusernode = $this->navigation->find('user' . $userid, null);
  1891. $reportnode = null;
  1892. $navigationnodeerror =
  1893. 'Could not find the navigation node requested. Please check that the node you are looking for exists.';
  1894. if ($userid != $USER->id) {
  1895. // Check that we have a valid node.
  1896. if (empty($newusernode)) {
  1897. // Throw an error if we ever reach here.
  1898. throw new coding_exception($navigationnodeerror);
  1899. }
  1900. // Add 'Reports' to the user node.
  1901. $reportnode = $newusernode->add(get_string('reports'));
  1902. } else {
  1903. // We are looking at our own profile.
  1904. $myprofilenode = $this->settingsnav->find('myprofile', null);
  1905. // Check that we do end up with a valid node.
  1906. if (empty($myprofilenode)) {
  1907. // Throw an error if we ever reach here.
  1908. throw new coding_exception($navigationnodeerror);
  1909. }
  1910. // Add 'Reports' to our node.
  1911. $reportnode = $myprofilenode->add(get_string('reports'));
  1912. }
  1913. // Finally add the report to the navigation tree.
  1914. $reportnode->add($nodeinfo['name'], $nodeinfo['url'], navigation_node::TYPE_CUSTOM, null, null,
  1915. new pix_icon('i/report', $nodeinfo['name']));
  1916. }
  1917. /**
  1918. * Add some HTML to the list of actions to render in the header actions menu.
  1919. *
  1920. * @param string $html The HTML to add.
  1921. */
  1922. public function add_header_action(string $html) : void {
  1923. $this->_headeractions[] = $html;
  1924. }
  1925. /**
  1926. * Get the list of HTML for actions to render in the header actions menu.
  1927. *
  1928. * @return string[]
  1929. */
  1930. public function get_header_actions() : array {
  1931. return $this->_headeractions;
  1932. }
  1933. /**
  1934. * Set the flag to indicate if the region main settings should be rendered as an action
  1935. * in the header actions menu rather than at the top of the content.
  1936. *
  1937. * @param bool $value If the settings should be in the header.
  1938. */
  1939. public function set_include_region_main_settings_in_header_actions(bool $value) : void {
  1940. $this->_regionmainsettingsinheader = $value;
  1941. }
  1942. /**
  1943. * Check if the region main settings should be rendered as an action in the header actions
  1944. * menu rather than at the top of the content.
  1945. *
  1946. * @return bool
  1947. */
  1948. public function include_region_main_settings_in_header_actions() : bool {
  1949. return $this->_regionmainsettingsinheader;
  1950. }
  1951. /**
  1952. * Set the flag to indicate if the secondary navigation should be rendered.
  1953. *
  1954. * @param bool $value If the secondary navigation should be rendered.
  1955. */
  1956. public function has_secondary_navigation_setter(bool $value) : void {
  1957. $this->_hassecondarynavigation = $value;
  1958. }
  1959. /**
  1960. * Check if the secondary navigation should be rendered.
  1961. *
  1962. * @return bool
  1963. */
  1964. public function has_secondary_navigation() : bool {
  1965. return $this->_hassecondarynavigation;
  1966. }
  1967. /**
  1968. * Set the key of the secondary nav node to be activated.
  1969. *
  1970. * @param string $navkey the key of the secondary nav node to be activated.
  1971. */
  1972. public function set_secondary_active_tab(string $navkey) : void {
  1973. $this->_activekeysecondary = $navkey;
  1974. }
  1975. /**
  1976. * The key of secondary nav node to activate.
  1977. *
  1978. * @return string|null get the key of the secondary node to activate.
  1979. */
  1980. public function get_secondary_active_tab(): ?string {
  1981. return $this->_activekeysecondary;
  1982. }
  1983. /**
  1984. * Set the key of the primary nav node to be activated.
  1985. *
  1986. * @param string $navkey
  1987. */
  1988. public function set_primary_active_tab(string $navkey): void {
  1989. $this->_activenodeprimary = $navkey;
  1990. }
  1991. /**
  1992. * The key of the primary nav node to activate.
  1993. *
  1994. * @return string|null get the key of the primary nav node to activate.
  1995. */
  1996. public function get_primary_activate_tab(): ?string {
  1997. return $this->_activenodeprimary;
  1998. }
  1999. }