PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/pagelib.php

http://github.com/moodle/moodle
PHP | 2084 lines | 916 code | 221 blank | 947 comment | 213 complexity | c759c903708dfec207e222d0c0399448 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains the moodle_page class. There is normally a single instance
  18. * of this class in the $PAGE global variable. This class is a central repository
  19. * of information about the page we are building up to send back to the user.
  20. *
  21. * @package core
  22. * @category page
  23. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. /**
  28. * $PAGE is a central store of information about the current page we are
  29. * generating in response to the user's request.
  30. *
  31. * It does not do very much itself
  32. * except keep track of information, however, it serves as the access point to
  33. * some more significant components like $PAGE->theme, $PAGE->requires,
  34. * $PAGE->blocks, etc.
  35. *
  36. * @copyright 2009 Tim Hunt
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. * @since Moodle 2.0
  39. * @package core
  40. * @category page
  41. *
  42. * The following properties are alphabetical. Please keep it that way so that its
  43. * easy to maintain.
  44. *
  45. * @property-read string $activityname The type of activity we are in, for example 'forum' or 'quiz'.
  46. * Will be null if this page is not within a module.
  47. * @property-read stdClass $activityrecord The row from the activities own database table (for example
  48. * the forum or quiz table) that this page belongs to. Will be null
  49. * if this page is not within a module.
  50. * @property-read array $alternativeversions Mime type => object with ->url and ->title.
  51. * @property-read block_manager $blocks The blocks manager object for this page.
  52. * @property-read array $blockmanipulations
  53. * @property-read string $bodyclasses A string to use within the class attribute on the body tag.
  54. * @property-read string $bodyid A string to use as the id of the body tag.
  55. * @property-read string $button The HTML to go where the Turn editing on button normally goes.
  56. * @property-read bool $cacheable Defaults to true. Set to false to stop the page being cached at all.
  57. * @property-read array $categories An array of all the categories the page course belongs to,
  58. * starting with the immediately containing category, and working out to
  59. * the top-level category. This may be the empty array if we are in the
  60. * front page course.
  61. * @property-read mixed $category The category that the page course belongs to.
  62. * @property-read cm_info $cm The course_module that this page belongs to. Will be null
  63. * if this page is not within a module. This is a full cm object, as loaded
  64. * by get_coursemodule_from_id or get_coursemodule_from_instance,
  65. * so the extra modname and name fields are present.
  66. * @property-read context $context The main context to which this page belongs.
  67. * @property-read stdClass $course The current course that we are inside - a row from the
  68. * course table. (Also available as $COURSE global.) If we are not inside
  69. * an actual course, this will be the site course.
  70. * @property-read string $devicetypeinuse The name of the device type in use
  71. * @property-read string $docspath The path to the Moodle docs for this page.
  72. * @property-read string $focuscontrol The id of the HTML element to be focused when the page has loaded.
  73. * @property-read bool $headerprinted True if the page header has already been printed.
  74. * @property-read string $heading The main heading that should be displayed at the top of the <body>.
  75. * @property-read string $headingmenu The menu (or actions) to display in the heading
  76. * @property-read array $layout_options An arrays with options for the layout file.
  77. * @property-read array $legacythemeinuse True if the legacy browser theme is in use.
  78. * @property-read navbar $navbar The navbar object used to display the navbar
  79. * @property-read global_navigation $navigation The navigation structure for this page.
  80. * @property-read xhtml_container_stack $opencontainers Tracks XHTML tags on this page that have been opened but not closed.
  81. * mainly for internal use by the rendering code.
  82. * @property-read string $pagelayout The general type of page this is. For example 'normal', 'popup', 'home'.
  83. * Allows the theme to display things differently, if it wishes to.
  84. * @property-read string $pagetype The page type string, should be used as the id for the body tag in the theme.
  85. * @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
  86. * @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
  87. * @property-read string $requestip The IP address of the current request, null if unknown.
  88. * @property-read string $requestorigin The type of request 'web', 'ws', 'cli', 'restore', etc.
  89. * @property-read settings_navigation $settingsnav The settings navigation
  90. * @property-read int $state One of the STATE_... constants
  91. * @property-read string $subpage The subpage identifier, if any.
  92. * @property-read theme_config $theme The theme for this page.
  93. * @property-read string $title The title that should go in the <head> section of the HTML of this page.
  94. * @property-read moodle_url $url The moodle url object for this page.
  95. */
  96. class moodle_page {
  97. /** The state of the page before it has printed the header **/
  98. const STATE_BEFORE_HEADER = 0;
  99. /** The state the page is in temporarily while the header is being printed **/
  100. const STATE_PRINTING_HEADER = 1;
  101. /** The state the page is in while content is presumably being printed **/
  102. const STATE_IN_BODY = 2;
  103. /**
  104. * The state the page is when the footer has been printed and its function is
  105. * complete.
  106. */
  107. const STATE_DONE = 3;
  108. /**
  109. * @var int The current state of the page. The state a page is within
  110. * determines what actions are possible for it.
  111. */
  112. protected $_state = self::STATE_BEFORE_HEADER;
  113. /**
  114. * @var stdClass The course currently associated with this page.
  115. * If not has been provided the front page course is used.
  116. */
  117. protected $_course = null;
  118. /**
  119. * @var cm_info If this page belongs to a module, this is the cm_info module
  120. * description object.
  121. */
  122. protected $_cm = null;
  123. /**
  124. * @var stdClass If $_cm is not null, then this will hold the corresponding
  125. * row from the modname table. For example, if $_cm->modname is 'quiz', this
  126. * will be a row from the quiz table.
  127. */
  128. protected $_module = null;
  129. /**
  130. * @var context The context that this page belongs to.
  131. */
  132. protected $_context = null;
  133. /**
  134. * @var array This holds any categories that $_course belongs to, starting with the
  135. * particular category it belongs to, and working out through any parent
  136. * categories to the top level. These are loaded progressively, if needed.
  137. * There are three states. $_categories = null initially when nothing is
  138. * loaded; $_categories = array($id => $cat, $parentid => null) when we have
  139. * loaded $_course->category, but not any parents; and a complete array once
  140. * everything is loaded.
  141. */
  142. protected $_categories = null;
  143. /**
  144. * @var array An array of CSS classes that should be added to the body tag in HTML.
  145. */
  146. protected $_bodyclasses = array();
  147. /**
  148. * @var string The title for the page. Used within the title tag in the HTML head.
  149. */
  150. protected $_title = '';
  151. /**
  152. * @var string The string to use as the heading of the page. Shown near the top of the
  153. * page within most themes.
  154. */
  155. protected $_heading = '';
  156. /**
  157. * @var string The pagetype is used to describe the page and defaults to a representation
  158. * of the physical path to the page e.g. my-index, mod-quiz-attempt
  159. */
  160. protected $_pagetype = null;
  161. /**
  162. * @var string The pagelayout to use when displaying this page. The
  163. * pagelayout needs to have been defined by the theme in use, or one of its
  164. * parents. By default base is used however standard is the more common layout.
  165. * Note that this gets automatically set by core during operations like
  166. * require_login.
  167. */
  168. protected $_pagelayout = 'base';
  169. /**
  170. * @var array List of theme layout options, these are ignored by core.
  171. * To be used in individual theme layout files only.
  172. */
  173. protected $_layout_options = null;
  174. /**
  175. * @var string An optional arbitrary parameter that can be set on pages where the context
  176. * and pagetype is not enough to identify the page.
  177. */
  178. protected $_subpage = '';
  179. /**
  180. * @var string Set a different path to use for the 'Moodle docs for this page' link.
  181. * By default, it uses the path of the file for instance mod/quiz/attempt.
  182. */
  183. protected $_docspath = null;
  184. /**
  185. * @var string A legacy class that will be added to the body tag
  186. */
  187. protected $_legacyclass = null;
  188. /**
  189. * @var moodle_url The URL for this page. This is mandatory and must be set
  190. * before output is started.
  191. */
  192. protected $_url = null;
  193. /**
  194. * @var array An array of links to alternative versions of this page.
  195. * Primarily used for RSS versions of the current page.
  196. */
  197. protected $_alternateversions = array();
  198. /**
  199. * @var block_manager The blocks manager for this page. It is responsible for
  200. * the blocks and there content on this page.
  201. */
  202. protected $_blocks = null;
  203. /**
  204. * @var page_requirements_manager Page requirements manager. It is responsible
  205. * for all JavaScript and CSS resources required by this page.
  206. */
  207. protected $_requires = null;
  208. /** @var page_requirements_manager Saves the requirement manager object used before switching to to fragments one. */
  209. protected $savedrequires = null;
  210. /**
  211. * @var string The capability required by the user in order to edit blocks
  212. * and block settings on this page.
  213. */
  214. protected $_blockseditingcap = 'moodle/site:manageblocks';
  215. /**
  216. * @var bool An internal flag to record when block actions have been processed.
  217. * Remember block actions occur on the current URL and it is important that
  218. * even they are never executed more than once.
  219. */
  220. protected $_block_actions_done = false;
  221. /**
  222. * @var array An array of any other capabilities the current user must have
  223. * in order to editing the page and/or its content (not just blocks).
  224. */
  225. protected $_othereditingcaps = array();
  226. /**
  227. * @var bool Sets whether this page should be cached by the browser or not.
  228. * If it is set to true (default) the page is served with caching headers.
  229. */
  230. protected $_cacheable = true;
  231. /**
  232. * @var string Can be set to the ID of an element on the page, if done that
  233. * element receives focus when the page loads.
  234. */
  235. protected $_focuscontrol = '';
  236. /**
  237. * @var string HTML to go where the turn on editing button is located. This
  238. * is nearly a legacy item and not used very often any more.
  239. */
  240. protected $_button = '';
  241. /**
  242. * @var theme_config The theme to use with this page. This has to be properly
  243. * initialised via {@link moodle_page::initialise_theme_and_output()} which
  244. * happens magically before any operation that requires it.
  245. */
  246. protected $_theme = null;
  247. /**
  248. * @var global_navigation Contains the global navigation structure.
  249. */
  250. protected $_navigation = null;
  251. /**
  252. * @var settings_navigation Contains the settings navigation structure.
  253. */
  254. protected $_settingsnav = null;
  255. /**
  256. * @var flat_navigation Contains a list of nav nodes, most closely related to the current page.
  257. */
  258. protected $_flatnav = null;
  259. /**
  260. * @var navbar Contains the navbar structure.
  261. */
  262. protected $_navbar = null;
  263. /**
  264. * @var string The menu (or actions) to display in the heading.
  265. */
  266. protected $_headingmenu = null;
  267. /**
  268. * @var array stack trace. Then the theme is initialised, we save the stack
  269. * trace, for use in error messages.
  270. */
  271. protected $_wherethemewasinitialised = null;
  272. /**
  273. * @var xhtml_container_stack Tracks XHTML tags on this page that have been
  274. * opened but not closed.
  275. */
  276. protected $_opencontainers;
  277. /**
  278. * @var int Sets the page to refresh after a given delay (in seconds) using
  279. * meta refresh in {@link standard_head_html()} in outputlib.php
  280. * If set to null(default) the page is not refreshed
  281. */
  282. protected $_periodicrefreshdelay = null;
  283. /**
  284. * @var array Associative array of browser shortnames (as used by check_browser_version)
  285. * and their minimum required versions
  286. */
  287. protected $_legacybrowsers = array('MSIE' => 6.0);
  288. /**
  289. * @var string Is set to the name of the device type in use.
  290. * This will we worked out when it is first used.
  291. */
  292. protected $_devicetypeinuse = null;
  293. /**
  294. * @var bool Used to determine if HTTPS should be required for login.
  295. */
  296. protected $_https_login_required = false;
  297. /**
  298. * @var bool Determines if popup notifications allowed on this page.
  299. * Code such as the quiz module disables popup notifications in situations
  300. * such as upgrading or completing a quiz.
  301. */
  302. protected $_popup_notification_allowed = true;
  303. /**
  304. * @var bool Is the settings menu being forced to display on this page (activities / resources only).
  305. * This is only used by themes that use the settings menu.
  306. */
  307. protected $_forcesettingsmenu = false;
  308. /**
  309. * @var array Array of header actions HTML to add to the page header actions menu.
  310. */
  311. protected $_headeractions = [];
  312. /**
  313. * @var bool Should the region main settings menu be rendered in the header.
  314. */
  315. protected $_regionmainsettingsinheader = false;
  316. /**
  317. * Force the settings menu to be displayed on this page. This will only force the
  318. * settings menu on an activity / resource page that is being displayed on a theme that
  319. * uses a settings menu.
  320. *
  321. * @param bool $forced default of true, can be sent false to turn off the force.
  322. */
  323. public function force_settings_menu($forced = true) {
  324. $this->_forcesettingsmenu = $forced;
  325. }
  326. /**
  327. * Check to see if the settings menu is forced to display on this activity / resource page.
  328. * This only applies to themes that use the settings menu.
  329. *
  330. * @return bool True if the settings menu is forced to display.
  331. */
  332. public function is_settings_menu_forced() {
  333. return $this->_forcesettingsmenu;
  334. }
  335. // Magic getter methods =============================================================
  336. // Due to the __get magic below, you normally do not call these as $PAGE->magic_get_x
  337. // methods, but instead use the $PAGE->x syntax.
  338. /**
  339. * Please do not call this method directly, use the ->state syntax. {@link moodle_page::__get()}.
  340. * @return integer one of the STATE_XXX constants. You should not normally need
  341. * to use this in your code. It is intended for internal use by this class
  342. * and its friends like print_header, to check that everything is working as
  343. * expected. Also accessible as $PAGE->state.
  344. */
  345. protected function magic_get_state() {
  346. return $this->_state;
  347. }
  348. /**
  349. * Please do not call this method directly, use the ->headerprinted syntax. {@link moodle_page::__get()}.
  350. * @return bool has the header already been printed?
  351. */
  352. protected function magic_get_headerprinted() {
  353. return $this->_state >= self::STATE_IN_BODY;
  354. }
  355. /**
  356. * Please do not call this method directly, use the ->course syntax. {@link moodle_page::__get()}.
  357. * @return stdClass the current course that we are inside - a row from the
  358. * course table. (Also available as $COURSE global.) If we are not inside
  359. * an actual course, this will be the site course.
  360. */
  361. protected function magic_get_course() {
  362. global $SITE;
  363. if (is_null($this->_course)) {
  364. return $SITE;
  365. }
  366. return $this->_course;
  367. }
  368. /**
  369. * Please do not call this method directly, use the ->cm syntax. {@link moodle_page::__get()}.
  370. * @return cm_info the course_module that this page belongs to. Will be null
  371. * if this page is not within a module. This is a full cm object, as loaded
  372. * by get_coursemodule_from_id or get_coursemodule_from_instance,
  373. * so the extra modname and name fields are present.
  374. */
  375. protected function magic_get_cm() {
  376. return $this->_cm;
  377. }
  378. /**
  379. * Please do not call this method directly, use the ->activityrecord syntax. {@link moodle_page::__get()}.
  380. * @return stdClass the row from the activities own database table (for example
  381. * the forum or quiz table) that this page belongs to. Will be null
  382. * if this page is not within a module.
  383. */
  384. protected function magic_get_activityrecord() {
  385. if (is_null($this->_module) && !is_null($this->_cm)) {
  386. $this->load_activity_record();
  387. }
  388. return $this->_module;
  389. }
  390. /**
  391. * Please do not call this method directly, use the ->activityname syntax. {@link moodle_page::__get()}.
  392. * @return string the The type of activity we are in, for example 'forum' or 'quiz'.
  393. * Will be null if this page is not within a module.
  394. */
  395. protected function magic_get_activityname() {
  396. if (is_null($this->_cm)) {
  397. return null;
  398. }
  399. return $this->_cm->modname;
  400. }
  401. /**
  402. * Please do not call this method directly, use the ->category syntax. {@link moodle_page::__get()}.
  403. * @return stdClass the category that the page course belongs to. If there isn't one
  404. * (that is, if this is the front page course) returns null.
  405. */
  406. protected function magic_get_category() {
  407. $this->ensure_category_loaded();
  408. if (!empty($this->_categories)) {
  409. return reset($this->_categories);
  410. } else {
  411. return null;
  412. }
  413. }
  414. /**
  415. * Please do not call this method directly, use the ->categories syntax. {@link moodle_page::__get()}.
  416. * @return array an array of all the categories the page course belongs to,
  417. * starting with the immediately containing category, and working out to
  418. * the top-level category. This may be the empty array if we are in the
  419. * front page course.
  420. */
  421. protected function magic_get_categories() {
  422. $this->ensure_categories_loaded();
  423. return $this->_categories;
  424. }
  425. /**
  426. * Please do not call this method directly, use the ->context syntax. {@link moodle_page::__get()}.
  427. * @return context the main context to which this page belongs.
  428. */
  429. protected function magic_get_context() {
  430. global $CFG;
  431. if (is_null($this->_context)) {
  432. if (CLI_SCRIPT or NO_MOODLE_COOKIES) {
  433. // Cli scripts work in system context, do not annoy devs with debug info.
  434. // Very few scripts do not use cookies, we can safely use system as default context there.
  435. } else if (AJAX_SCRIPT && $CFG->debugdeveloper) {
  436. // Throw exception inside AJAX script in developer mode, otherwise the debugging message may be missed.
  437. throw new coding_exception('$PAGE->context was not set. You may have forgotten '
  438. .'to call require_login() or $PAGE->set_context()');
  439. } else {
  440. debugging('Coding problem: $PAGE->context was not set. You may have forgotten '
  441. .'to call require_login() or $PAGE->set_context(). The page may not display '
  442. .'correctly as a result');
  443. }
  444. $this->_context = context_system::instance();
  445. }
  446. return $this->_context;
  447. }
  448. /**
  449. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  450. * @return string e.g. 'my-index' or 'mod-quiz-attempt'.
  451. */
  452. protected function magic_get_pagetype() {
  453. global $CFG;
  454. if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
  455. $this->initialise_default_pagetype();
  456. }
  457. return $this->_pagetype;
  458. }
  459. /**
  460. * Please do not call this method directly, use the ->pagetype syntax. {@link moodle_page::__get()}.
  461. * @return string The id to use on the body tag, uses {@link magic_get_pagetype()}.
  462. */
  463. protected function magic_get_bodyid() {
  464. return 'page-'.$this->pagetype;
  465. }
  466. /**
  467. * Please do not call this method directly, use the ->pagelayout syntax. {@link moodle_page::__get()}.
  468. * @return string the general type of page this is. For example 'standard', 'popup', 'home'.
  469. * Allows the theme to display things differently, if it wishes to.
  470. */
  471. protected function magic_get_pagelayout() {
  472. return $this->_pagelayout;
  473. }
  474. /**
  475. * Please do not call this method directly, use the ->layout_options syntax. {@link moodle_page::__get()}.
  476. * @return array returns arrays with options for layout file
  477. */
  478. protected function magic_get_layout_options() {
  479. if (!is_array($this->_layout_options)) {
  480. $this->_layout_options = $this->_theme->pagelayout_options($this->pagelayout);
  481. }
  482. return $this->_layout_options;
  483. }
  484. /**
  485. * Please do not call this method directly, use the ->subpage syntax. {@link moodle_page::__get()}.
  486. * @return string The subpage identifier, if any.
  487. */
  488. protected function magic_get_subpage() {
  489. return $this->_subpage;
  490. }
  491. /**
  492. * Please do not call this method directly, use the ->bodyclasses syntax. {@link moodle_page::__get()}.
  493. * @return string the class names to put on the body element in the HTML.
  494. */
  495. protected function magic_get_bodyclasses() {
  496. return implode(' ', array_keys($this->_bodyclasses));
  497. }
  498. /**
  499. * Please do not call this method directly, use the ->title syntax. {@link moodle_page::__get()}.
  500. * @return string the title that should go in the <head> section of the HTML of this page.
  501. */
  502. protected function magic_get_title() {
  503. return $this->_title;
  504. }
  505. /**
  506. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  507. * @return string the main heading that should be displayed at the top of the <body>.
  508. */
  509. protected function magic_get_heading() {
  510. return $this->_heading;
  511. }
  512. /**
  513. * Please do not call this method directly, use the ->heading syntax. {@link moodle_page::__get()}.
  514. * @return string The menu (or actions) to display in the heading
  515. */
  516. protected function magic_get_headingmenu() {
  517. return $this->_headingmenu;
  518. }
  519. /**
  520. * Please do not call this method directly, use the ->docspath syntax. {@link moodle_page::__get()}.
  521. * @return string the path to the Moodle docs for this page.
  522. */
  523. protected function magic_get_docspath() {
  524. if (is_string($this->_docspath)) {
  525. return $this->_docspath;
  526. } else {
  527. return str_replace('-', '/', $this->pagetype);
  528. }
  529. }
  530. /**
  531. * Please do not call this method directly, use the ->url syntax. {@link moodle_page::__get()}.
  532. * @return moodle_url the clean URL required to load the current page. (You
  533. * should normally use this in preference to $ME or $FULLME.)
  534. */
  535. protected function magic_get_url() {
  536. global $FULLME;
  537. if (is_null($this->_url)) {
  538. debugging('This page did not call $PAGE->set_url(...). Using '.s($FULLME), DEBUG_DEVELOPER);
  539. $this->_url = new moodle_url($FULLME);
  540. // Make sure the guessed URL cannot lead to dangerous redirects.
  541. $this->_url->remove_params('sesskey');
  542. }
  543. return new moodle_url($this->_url); // Return a clone for safety.
  544. }
  545. /**
  546. * The list of alternate versions of this page.
  547. * @return array mime type => object with ->url and ->title.
  548. */
  549. protected function magic_get_alternateversions() {
  550. return $this->_alternateversions;
  551. }
  552. /**
  553. * Please do not call this method directly, use the ->blocks syntax. {@link moodle_page::__get()}.
  554. * @return block_manager the blocks manager object for this page.
  555. */
  556. protected function magic_get_blocks() {
  557. global $CFG;
  558. if (is_null($this->_blocks)) {
  559. if (!empty($CFG->blockmanagerclass)) {
  560. if (!empty($CFG->blockmanagerclassfile)) {
  561. require_once($CFG->blockmanagerclassfile);
  562. }
  563. $classname = $CFG->blockmanagerclass;
  564. } else {
  565. $classname = 'block_manager';
  566. }
  567. $this->_blocks = new $classname($this);
  568. }
  569. return $this->_blocks;
  570. }
  571. /**
  572. * Please do not call this method directly, use the ->requires syntax. {@link moodle_page::__get()}.
  573. * @return page_requirements_manager tracks the JavaScript, CSS files, etc. required by this page.
  574. */
  575. protected function magic_get_requires() {
  576. if (is_null($this->_requires)) {
  577. $this->_requires = new page_requirements_manager();
  578. }
  579. return $this->_requires;
  580. }
  581. /**
  582. * Please do not call this method directly, use the ->cacheable syntax. {@link moodle_page::__get()}.
  583. * @return bool can this page be cached by the user's browser.
  584. */
  585. protected function magic_get_cacheable() {
  586. return $this->_cacheable;
  587. }
  588. /**
  589. * Please do not call this method directly, use the ->focuscontrol syntax. {@link moodle_page::__get()}.
  590. * @return string the id of the HTML element to be focused when the page has loaded.
  591. */
  592. protected function magic_get_focuscontrol() {
  593. return $this->_focuscontrol;
  594. }
  595. /**
  596. * Please do not call this method directly, use the ->button syntax. {@link moodle_page::__get()}.
  597. * @return string the HTML to go where the Turn editing on button normally goes.
  598. */
  599. protected function magic_get_button() {
  600. return $this->_button;
  601. }
  602. /**
  603. * Please do not call this method directly, use the ->theme syntax. {@link moodle_page::__get()}.
  604. * @return theme_config the initialised theme for this page.
  605. */
  606. protected function magic_get_theme() {
  607. if (is_null($this->_theme)) {
  608. $this->initialise_theme_and_output();
  609. }
  610. return $this->_theme;
  611. }
  612. /**
  613. * Returns an array of minipulations or false if there are none to make.
  614. *
  615. * @since Moodle 2.5.1 2.6
  616. * @return bool|array
  617. */
  618. protected function magic_get_blockmanipulations() {
  619. if (!right_to_left()) {
  620. return false;
  621. }
  622. if (is_null($this->_theme)) {
  623. $this->initialise_theme_and_output();
  624. }
  625. return $this->_theme->blockrtlmanipulations;
  626. }
  627. /**
  628. * Please do not call this method directly, use the ->devicetypeinuse syntax. {@link moodle_page::__get()}.
  629. * @return string The device type being used.
  630. */
  631. protected function magic_get_devicetypeinuse() {
  632. if (empty($this->_devicetypeinuse)) {
  633. $this->_devicetypeinuse = core_useragent::get_user_device_type();
  634. }
  635. return $this->_devicetypeinuse;
  636. }
  637. /**
  638. * Please do not call this method directly use the ->periodicrefreshdelay syntax
  639. * {@link moodle_page::__get()}
  640. * @return int The periodic refresh delay to use with meta refresh
  641. */
  642. protected function magic_get_periodicrefreshdelay() {
  643. return $this->_periodicrefreshdelay;
  644. }
  645. /**
  646. * Please do not call this method directly use the ->opencontainers syntax. {@link moodle_page::__get()}
  647. * @return xhtml_container_stack tracks XHTML tags on this page that have been opened but not closed.
  648. * mainly for internal use by the rendering code.
  649. */
  650. protected function magic_get_opencontainers() {
  651. if (is_null($this->_opencontainers)) {
  652. $this->_opencontainers = new xhtml_container_stack();
  653. }
  654. return $this->_opencontainers;
  655. }
  656. /**
  657. * Return the navigation object
  658. * @return global_navigation
  659. */
  660. protected function magic_get_navigation() {
  661. if ($this->_navigation === null) {
  662. $this->_navigation = new global_navigation($this);
  663. }
  664. return $this->_navigation;
  665. }
  666. /**
  667. * Return a navbar object
  668. * @return navbar
  669. */
  670. protected function magic_get_navbar() {
  671. if ($this->_navbar === null) {
  672. $this->_navbar = new navbar($this);
  673. }
  674. return $this->_navbar;
  675. }
  676. /**
  677. * Returns the settings navigation object
  678. * @return settings_navigation
  679. */
  680. protected function magic_get_settingsnav() {
  681. if ($this->_settingsnav === null) {
  682. $this->_settingsnav = new settings_navigation($this);
  683. $this->_settingsnav->initialise();
  684. }
  685. return $this->_settingsnav;
  686. }
  687. /**
  688. * Returns the flat navigation object
  689. * @return flat_navigation
  690. */
  691. protected function magic_get_flatnav() {
  692. if ($this->_flatnav === null) {
  693. $this->_flatnav = new flat_navigation($this);
  694. $this->_flatnav->initialise();
  695. }
  696. return $this->_flatnav;
  697. }
  698. /**
  699. * Returns request IP address.
  700. *
  701. * @return string IP address or null if unknown
  702. */
  703. protected function magic_get_requestip() {
  704. return getremoteaddr(null);
  705. }
  706. /**
  707. * Returns the origin of current request.
  708. *
  709. * Note: constants are not required because we need to use these values in logging and reports.
  710. *
  711. * @return string 'web', 'ws', 'cli', 'restore', etc.
  712. */
  713. protected function magic_get_requestorigin() {
  714. if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
  715. return 'restore';
  716. }
  717. if (WS_SERVER) {
  718. return 'ws';
  719. }
  720. if (CLI_SCRIPT) {
  721. return 'cli';
  722. }
  723. return 'web';
  724. }
  725. /**
  726. * PHP overloading magic to make the $PAGE->course syntax work by redirecting
  727. * it to the corresponding $PAGE->magic_get_course() method if there is one, and
  728. * throwing an exception if not.
  729. *
  730. * @param string $name property name
  731. * @return mixed
  732. * @throws coding_exception
  733. */
  734. public function __get($name) {
  735. $getmethod = 'magic_get_' . $name;
  736. if (method_exists($this, $getmethod)) {
  737. return $this->$getmethod();
  738. } else {
  739. throw new coding_exception('Unknown property ' . $name . ' of $PAGE.');
  740. }
  741. }
  742. /**
  743. * PHP overloading magic to catch obvious coding errors.
  744. *
  745. * This method has been created to catch obvious coding errors where the
  746. * developer has tried to set a page property using $PAGE->key = $value.
  747. * In the moodle_page class all properties must be set using the appropriate
  748. * $PAGE->set_something($value) method.
  749. *
  750. * @param string $name property name
  751. * @param mixed $value Value
  752. * @return void Throws exception if field not defined in page class
  753. * @throws coding_exception
  754. */
  755. public function __set($name, $value) {
  756. if (method_exists($this, 'set_' . $name)) {
  757. throw new coding_exception('Invalid attempt to modify page object', "Use \$PAGE->set_$name() instead.");
  758. } else {
  759. throw new coding_exception('Invalid attempt to modify page object', "Unknown property $name");
  760. }
  761. }
  762. // Other information getting methods ==========================================.
  763. /**
  764. * Returns instance of page renderer
  765. *
  766. * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
  767. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
  768. * @param string $target one of rendering target constants
  769. * @return renderer_base
  770. */
  771. public function get_renderer($component, $subtype = null, $target = null) {
  772. if ($this->pagelayout === 'maintenance') {
  773. // If the page is using the maintenance layout then we're going to force target to maintenance.
  774. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  775. // page layout.
  776. $target = RENDERER_TARGET_MAINTENANCE;
  777. }
  778. return $this->magic_get_theme()->get_renderer($this, $component, $subtype, $target);
  779. }
  780. /**
  781. * Checks to see if there are any items on the navbar object
  782. *
  783. * @return bool true if there are, false if not
  784. */
  785. public function has_navbar() {
  786. if ($this->_navbar === null) {
  787. $this->_navbar = new navbar($this);
  788. }
  789. return $this->_navbar->has_items();
  790. }
  791. /**
  792. * Switches from the regular requirements manager to the fragment requirements manager to
  793. * capture all necessary JavaScript to display a chunk of HTML such as an mform. This is for use
  794. * by the get_fragment() web service and not for use elsewhere.
  795. */
  796. public function start_collecting_javascript_requirements() {
  797. global $CFG;
  798. require_once($CFG->libdir.'/outputfragmentrequirementslib.php');
  799. // Check that the requirements manager has not already been switched.
  800. if (get_class($this->_requires) == 'fragment_requirements_manager') {
  801. throw new coding_exception('JavaScript collection has already been started.');
  802. }
  803. // The header needs to have been called to flush out the generic JavaScript for the page. This allows only
  804. // JavaScript for the fragment to be collected. _wherethemewasinitialised is set when header() is called.
  805. if (!empty($this->_wherethemewasinitialised)) {
  806. // Change the current requirements manager over to the fragment manager to capture JS.
  807. $this->savedrequires = $this->_requires;
  808. $this->_requires = new fragment_requirements_manager();
  809. } else {
  810. throw new coding_exception('$OUTPUT->header() needs to be called before collecting JavaScript requirements.');
  811. }
  812. }
  813. /**
  814. * Switches back from collecting fragment JS requirement to the original requirement manager
  815. */
  816. public function end_collecting_javascript_requirements() {
  817. if ($this->savedrequires === null) {
  818. throw new coding_exception('JavaScript collection has not been started.');
  819. }
  820. $this->_requires = $this->savedrequires;
  821. $this->savedrequires = null;
  822. }
  823. /**
  824. * Should the current user see this page in editing mode.
  825. * That is, are they allowed to edit this page, and are they currently in
  826. * editing mode.
  827. * @return bool
  828. */
  829. public function user_is_editing() {
  830. global $USER;
  831. return !empty($USER->editing) && $this->user_allowed_editing();
  832. }
  833. /**
  834. * Does the user have permission to edit blocks on this page.
  835. * @return bool
  836. */
  837. public function user_can_edit_blocks() {
  838. return has_capability($this->_blockseditingcap, $this->_context);
  839. }
  840. /**
  841. * Does the user have permission to see this page in editing mode.
  842. * @return bool
  843. */
  844. public function user_allowed_editing() {
  845. return has_any_capability($this->all_editing_caps(), $this->_context);
  846. }
  847. /**
  848. * Get a description of this page. Normally displayed in the footer in developer debug mode.
  849. * @return string
  850. */
  851. public function debug_summary() {
  852. $summary = '';
  853. $summary .= 'General type: ' . $this->pagelayout . '. ';
  854. if (!during_initial_install()) {
  855. $summary .= 'Context ' . $this->context->get_context_name() . ' (context id ' . $this->_context->id . '). ';
  856. }
  857. $summary .= 'Page type ' . $this->pagetype . '. ';
  858. if ($this->subpage) {
  859. $summary .= 'Sub-page ' . $this->subpage . '. ';
  860. }
  861. return $summary;
  862. }
  863. // Setter methods =============================================================.
  864. /**
  865. * Set the state.
  866. *
  867. * The state must be one of that STATE_... constants, and the state is only allowed to advance one step at a time.
  868. *
  869. * @param int $state The new state.
  870. * @throws coding_exception
  871. */
  872. public function set_state($state) {
  873. if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
  874. throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
  875. $this->_state . ' and state ' . $state . ' was requested.');
  876. }
  877. if ($state == self::STATE_PRINTING_HEADER) {
  878. $this->starting_output();
  879. }
  880. $this->_state = $state;
  881. }
  882. /**
  883. * Set the current course. This sets both $PAGE->course and $COURSE. It also
  884. * sets the right theme and locale.
  885. *
  886. * Normally you don't need to call this function yourself, require_login will
  887. * call it for you if you pass a $course to it. You can use this function
  888. * on pages that do need to call require_login().
  889. *
  890. * Sets $PAGE->context to the course context, if it is not already set.
  891. *
  892. * @param stdClass $course the course to set as the global course.
  893. * @throws coding_exception
  894. */
  895. public function set_course($course) {
  896. global $COURSE, $PAGE, $CFG, $SITE;
  897. if (empty($course->id)) {
  898. throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
  899. }
  900. $this->ensure_theme_not_set();
  901. if (!empty($this->_course->id) && $this->_course->id != $course->id) {
  902. $this->_categories = null;
  903. }
  904. $this->_course = clone($course);
  905. if ($this === $PAGE) {
  906. $COURSE = $this->_course;
  907. moodle_setlocale();
  908. }
  909. if (!$this->_context) {
  910. $this->set_context(context_course::instance($this->_course->id));
  911. }
  912. // Notify course format that this page is set for the course.
  913. if ($this->_course->id != $SITE->id) {
  914. require_once($CFG->dirroot.'/course/lib.php');
  915. $courseformat = course_get_format($this->_course);
  916. $this->add_body_class('format-'. $courseformat->get_format());
  917. $courseformat->page_set_course($this);
  918. } else {
  919. $this->add_body_class('format-site');
  920. }
  921. }
  922. /**
  923. * Set the main context to which this page belongs.
  924. *
  925. * @param context $context a context object. You normally get this with context_xxxx::instance().
  926. */
  927. public function set_context($context) {
  928. if ($context === null) {
  929. // Extremely ugly hack which sets context to some value in order to prevent warnings,
  930. // use only for core error handling!!!!
  931. if (!$this->_context) {
  932. $this->_context = context_system::instance();
  933. }
  934. return;
  935. }
  936. // Ideally we should set context only once.
  937. if (isset($this->_context) && $context->id !== $this->_context->id) {
  938. $current = $this->_context->contextlevel;
  939. if ($current == CONTEXT_SYSTEM or $current == CONTEXT_COURSE) {
  940. // Hmm - not ideal, but it might produce too many warnings due to the design of require_login.
  941. } else if ($current == CONTEXT_MODULE and ($parentcontext = $context->get_parent_context()) and
  942. $this->_context->id == $parentcontext->id) {
  943. // Hmm - most probably somebody did require_login() and after that set the block context.
  944. } else {
  945. // We do not want devs to do weird switching of context levels on the fly because we might have used
  946. // the context already such as in text filter in page title.
  947. debugging("Coding problem: unsupported modification of PAGE->context from {$current} to {$context->contextlevel}");
  948. }
  949. }
  950. $this->_context = $context;
  951. }
  952. /**
  953. * The course module that this page belongs to (if it does belong to one).
  954. *
  955. * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo().
  956. * @param stdClass $course
  957. * @param stdClass $module
  958. * @return void
  959. * @throws coding_exception
  960. */
  961. public function set_cm($cm, $course = null, $module = null) {
  962. global $DB, $CFG, $SITE;
  963. if (!isset($cm->id) || !isset($cm->course)) {
  964. throw new coding_exception('Invalid $cm. It has to be instance of cm_info or record from the course_modules table.');
  965. }
  966. if (!$this->_course || $this->_course->id != $cm->course) {
  967. if (!$course) {
  968. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  969. }
  970. if ($course->id != $cm->course) {
  971. throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.');
  972. }
  973. $this->set_course($course);
  974. }
  975. // Make sure we have a $cm from get_fast_modinfo as this contains activity access details.
  976. if (!($cm instanceof cm_info)) {
  977. $modinfo = get_fast_modinfo($this->_course);
  978. $cm = $modinfo->get_cm($cm->id);
  979. }
  980. $this->_cm = $cm;
  981. // Unfortunately the context setting is a mess.
  982. // Let's try to work around some common block problems and show some debug messages.
  983. if (empty($this->_context) or $this->_context->contextlevel != CONTEXT_BLOCK) {
  984. $context = context_module::instance($cm->id);
  985. $this->set_context($context);
  986. }
  987. if ($module) {
  988. $this->set_activity_record($module);
  989. }
  990. // Notify course format that this page is set for the course module.
  991. if ($this->_course->id != $SITE->id) {
  992. require_once($CFG->dirroot.'/course/lib.php');
  993. course_get_format($this->_course)->page_set_cm($this);
  994. }
  995. }
  996. /**
  997. * Sets the activity record. This could be a row from the main table for a
  998. * module. For instance if the current module (cm) is a forum this should be a row
  999. * from the forum table.
  1000. *
  1001. * @param stdClass $module A row from the main database table for the module that this page belongs to.
  1002. * @throws coding_exception
  1003. */
  1004. public function set_activity_record($module) {
  1005. if (is_null($this->_cm)) {
  1006. throw new coding_exception('You cannot call $PAGE->set_activity_record until after $PAGE->cm has been set.');
  1007. }
  1008. if ($module->id != $this->_cm->instance || $module->course != $this->_course->id) {
  1009. throw new coding_exception('The activity record does not seem to correspond to the cm that has been set.');
  1010. }
  1011. $this->_module = $module;
  1012. }
  1013. /**
  1014. * Sets the pagetype to use for this page.
  1015. *
  1016. * Normally you do not need to set this manually, it is automatically created
  1017. * from the script name. However, on some pages this is overridden.
  1018. * For example the page type for course/view.php includes the course format,
  1019. * for example 'course-view-weeks'. This gets used as the id attribute on
  1020. * <body> and also for determining which blocks are displayed.
  1021. *
  1022. * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'.
  1023. */
  1024. public function set_pagetype($pagetype) {
  1025. $this->_pagetype = $pagetype;
  1026. }
  1027. /**
  1028. * Sets the layout to use for this page.
  1029. *
  1030. * The page layout determines how the page will be displayed, things such as
  1031. * block regions, content areas, etc are controlled by the layout.
  1032. * The theme in use for the page will determine that the layout contains.
  1033. *
  1034. * This properly defaults to 'base', so you only need to call this function if
  1035. * you want something different. The exact range of supported layouts is specified
  1036. * in the standard theme.
  1037. *
  1038. * For an idea of the common page layouts see
  1039. * {@link http://docs.moodle.org/dev/Themes_2.0#The_different_layouts_as_of_August_17th.2C_2010}
  1040. * But please keep in mind that it may be (and normally is) out of date.
  1041. * The only place to find an accurate up-to-date list of the page layouts
  1042. * available for your version of Moodle is {@link theme/base/config.php}
  1043. *
  1044. * @param string $pagelayout the page layout this is. For example 'popup', 'home'.
  1045. */
  1046. public function set_pagelayout($pagelayout) {
  1047. global $SESSION;
  1048. if (!empty($SESSION->forcepagelayout)) {
  1049. $this->_pagelayout = $SESSION->forcepagelayout;
  1050. } else {
  1051. // Uncomment this to debug theme pagelayout issues like missing blocks.
  1052. // if (!empty($this->_wherethemewasinitialised) && $pagelayout != $this->_pagelayout)
  1053. // debugging('Page layout has already been set and cannot be changed.', DEBUG_DEVELOPER);
  1054. $this->_pagelayout = $pagelayout;
  1055. }
  1056. }
  1057. /**
  1058. * If context->id and pagetype are not enough to uniquely identify this page,
  1059. * then you can set a subpage id as well. For example, the tags page sets
  1060. *
  1061. * @param string $subpage an arbitrary identifier that, along with context->id
  1062. * and pagetype, uniquely identifies this page.
  1063. */
  1064. public function set_subpage($subpage) {
  1065. if (empty($subpage)) {
  1066. $this->_subpage = '';
  1067. } else {
  1068. $this->_subpage = $subpage;
  1069. }
  1070. }
  1071. /**
  1072. * Adds a CSS class to the body tag of the page.
  1073. *
  1074. * @param string $class add this class name ot the class attribute on the body tag.
  1075. * @throws coding_exception
  1076. */
  1077. public function add_body_class($class) {
  1078. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1079. throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
  1080. }
  1081. $this->_bodyclasses[$class] = 1;
  1082. }
  1083. /**
  1084. * Adds an array of body classes to the body tag of this page.
  1085. *
  1086. * @param array $classes this utility method calls add_body_class for each array element.
  1087. */
  1088. public function add_body_classes($classes) {
  1089. foreach ($classes as $class) {
  1090. $this->add_body_class($class);
  1091. }
  1092. }
  1093. /**
  1094. * Sets the title for the page.
  1095. * This is normally used within the title tag in the head of the page.
  1096. *
  1097. * @param string $title the title that should go in the <head> section of the HTML of this page.
  1098. */
  1099. public function set_title($title) {
  1100. $title = format_string($title);
  1101. $title = strip_tags($title);
  1102. $title = str_replace('"', '&quot;', $title);
  1103. $this->_title = $title;
  1104. }
  1105. /**
  1106. * Sets the heading to use for the page.
  1107. * This is normally used as the main heading at the top of the content.
  1108. *
  1109. * @param string $heading the main heading that should be displayed at the top of the <body>.
  1110. */
  1111. public function set_heading($heading) {
  1112. $this->_heading = format_string($heading);
  1113. }
  1114. /**
  1115. * Sets some HTML to use next to the heading {@link moodle_page::set_heading()}
  1116. *
  1117. * @param string $menu The menu/content to show in the heading
  1118. */
  1119. public function set_headingmenu($menu) {
  1120. $this->_headingmenu = $menu;
  1121. }
  1122. /**
  1123. * Set the course category this page belongs to manually.
  1124. *
  1125. * This automatically sets $PAGE->course to be the site course. You cannot
  1126. * use this method if you have already set $PAGE->course - in that case,
  1127. * the category must be the one that the course belongs to. This also
  1128. * automatically sets the page context to the category context.
  1129. *
  1130. * @param int $categoryid The id of the category to set.
  1131. * @throws coding_exception
  1132. */
  1133. public function set_category_by_id($categoryid) {
  1134. global $SITE;
  1135. if (!is_null($this->_course)) {
  1136. throw new coding_exception('Course has already been set. You cannot change the category now.');
  1137. }
  1138. if (is_array($this->_categories)) {
  1139. throw new coding_exception('Course category has already been set. You cannot to change it now.');
  1140. }
  1141. $this->ensure_theme_not_set();
  1142. $this->set_course($SITE);
  1143. $this->load_category($categoryid);
  1144. $this->set_context(context_coursecat::instance($categoryid));
  1145. }
  1146. /**
  1147. * Set a different path to use for the 'Moodle docs for this page' link.
  1148. *
  1149. * By default, it uses the pagetype, which is normally the same as the
  1150. * script name. So, for example, for mod/quiz/attempt.php, pagetype is
  1151. * mod-quiz-attempt, and so docspath is mod/quiz/attempt.
  1152. *
  1153. * @param string $path the path to use at the end of the moodle docs URL.
  1154. */
  1155. public function set_docs_path($path) {
  1156. $this->_docspath = $path;
  1157. }
  1158. /**
  1159. * You should call this method from every page to set the URL that should be used to return to this page.
  1160. *
  1161. * Used, for example, by the blocks editing UI to know where to return the
  1162. * user after an action.
  1163. * For example, course/view.php does:
  1164. * $id = optional_param('id', 0, PARAM_INT);
  1165. * $PAGE->set_url('/course/view.php', array('id' => $id));
  1166. *
  1167. * @param moodle_url|string $url URL relative to $CFG->wwwroot or {@link moodle_url} instance
  1168. * @param array $params parameters to add to the URL
  1169. * @throws coding_exception
  1170. */
  1171. public function set_url($url, array $params = null) {
  1172. global $CFG;
  1173. if (is_string($url) && strpos($url, 'http') !== 0) {
  1174. if (strpos($url, '/') === 0) {
  1175. // Add the wwwroot to the relative url.
  1176. $url = $CFG->wwwroot . $url;
  1177. } else {
  1178. throw new coding_exception('Invalid parameter $url, has to be full url or in shortened form starting with /.');
  1179. }
  1180. }
  1181. $this->_url = new moodle_url($url, $params);
  1182. $fullurl = $this->_url->out_omit_querystring();
  1183. if (strpos($fullurl, "$CFG->wwwroot/") !== 0) {
  1184. debugging('Most probably incorrect set_page() url argument, it does not match the wwwroot!');
  1185. }
  1186. $shorturl = str_replace("$CFG->wwwroot/", '', $fullurl);
  1187. if (is_null($this->_pagetype)) {
  1188. $this->initialise_default_pagetype($shorturl);
  1189. }
  1190. }
  1191. /**
  1192. * Make sure page URL does not contain the given URL parameter.
  1193. *
  1194. * This should not be necessary if the script has called set_url properly.
  1195. * However, in some situations like the block editing actions; when the URL
  1196. * has been guessed, it will contain dangerous block-related actions.
  1197. * Therefore, the blocks code calls this function to clean up such parameters
  1198. * before doing any redirect.
  1199. *
  1200. * @param string $param the name of the parameter to make sure is not in the
  1201. * page URL.
  1202. */
  1203. public function ensure_param_not_in_url($param) {
  1204. $this->_url->remove_params($param);
  1205. }
  1206. /**
  1207. * Sets an alternative version of this page.
  1208. *
  1209. * There can be alternate versions of some pages (for example an RSS feed version).
  1210. * Call this method for each alternative version available.
  1211. * For each alternative version a link will be included in the <head> tag.
  1212. *
  1213. * @param string $title The title to give the alternate version.
  1214. * @param string|moodle_url $url The URL of the alternate version.
  1215. * @param string $mimetype The mime-type of the alternate version.
  1216. * @throws coding_exception
  1217. */
  1218. public function add_alternate_version($title, $url, $mimetype) {
  1219. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1220. throw new coding_exception('Cannot call moodle_page::add_alternate_version after output has been started.');
  1221. }
  1222. $alt = new stdClass;
  1223. $alt->title = $title;
  1224. $alt->url = $url;
  1225. $this->_alternateversions[$mimetype] = $alt;
  1226. }
  1227. /**
  1228. * Specify a form control should be focused when the page has loaded.
  1229. *
  1230. * @param string $controlid the id of the HTML element to be focused.
  1231. */
  1232. public function set_focuscontrol($controlid) {
  1233. $this->_focuscontrol = $controlid;
  1234. }
  1235. /**
  1236. * Specify a fragment of HTML that goes where the 'Turn editing on' button normally goes.
  1237. *
  1238. * @param string $html the HTML to display there.
  1239. */
  1240. public function set_button($html) {
  1241. $this->_button = $html;
  1242. }
  1243. /**
  1244. * Set the capability that allows users to edit blocks on this page.
  1245. *
  1246. * Normally the default of 'moodle/site:manageblocks' is used, but a few
  1247. * pages like the My Moodle page need to use a different capability
  1248. * like 'moodle/my:manageblocks'.
  1249. *
  1250. * @param string $capability a capability.
  1251. */
  1252. public function set_blocks_editing_capability($capability) {
  1253. $this->_blockseditingcap = $capability;
  1254. }
  1255. /**
  1256. * Some pages let you turn editing on for reasons other than editing blocks.
  1257. * If that is the case, you can pass other capabilities that let the user
  1258. * edit this page here.
  1259. *
  1260. * @param string|array $capability either a capability, or an array of capabilities.
  1261. */
  1262. public function set_other_editing_capability($capability) {
  1263. if (is_array($capability)) {
  1264. $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
  1265. } else {
  1266. $this->_othereditingcaps[] = $capability;
  1267. }
  1268. }
  1269. /**
  1270. * Sets whether the browser should cache this page or not.
  1271. *
  1272. * @param bool $cacheable can this page be cached by the user's browser.
  1273. */
  1274. public function set_cacheable($cacheable) {
  1275. $this->_cacheable = $cacheable;
  1276. }
  1277. /**
  1278. * Sets the page to periodically refresh
  1279. *
  1280. * This function must be called before $OUTPUT->header has been called or
  1281. * a coding exception will be thrown.
  1282. *
  1283. * @param int $delay Sets the delay before refreshing the page, if set to null refresh is cancelled.
  1284. * @throws coding_exception
  1285. */
  1286. public function set_periodic_refresh_delay($delay = null) {
  1287. if ($this->_state > self::STATE_BEFORE_HEADER) {
  1288. throw new coding_exception('You cannot set a periodic refresh delay after the header has been printed');
  1289. }
  1290. if ($delay === null) {
  1291. $this->_periodicrefreshdelay = null;
  1292. } else if (is_int($delay)) {
  1293. $this->_periodicrefreshdelay = $delay;
  1294. }
  1295. }
  1296. /**
  1297. * Force this page to use a particular theme.
  1298. *
  1299. * Please use this cautiously.
  1300. * It is only intended to be used by the themes selector admin page.
  1301. *
  1302. * @param string $themename the name of the theme to use.
  1303. */
  1304. public function force_theme($themename) {
  1305. $this->ensure_theme_not_set();
  1306. $this->_theme = theme_config::load($themename);
  1307. }
  1308. /**
  1309. * Reload theme settings.
  1310. *
  1311. * This is used when we need to reset settings
  1312. * because they are now double cached in theme.
  1313. */
  1314. public function reload_theme() {
  1315. if (!is_null($this->_theme)) {
  1316. $this->_theme = theme_config::load($this->_theme->name);
  1317. }
  1318. }
  1319. /**
  1320. * @deprecated since Moodle 3.4
  1321. */
  1322. public function https_required() {
  1323. throw new coding_exception('https_required() cannot be used anymore.');
  1324. }
  1325. /**
  1326. * @deprecated since Moodle 3.4
  1327. */
  1328. public function verify_https_required() {
  1329. throw new coding_exception('verify_https_required() cannot be used anymore.');
  1330. }
  1331. // Initialisation methods =====================================================
  1332. // These set various things up in a default way.
  1333. /**
  1334. * This method is called when the page first moves out of the STATE_BEFORE_HEADER
  1335. * state. This is our last change to initialise things.
  1336. */
  1337. protected function starting_output() {
  1338. global $CFG;
  1339. if (!during_initial_install()) {
  1340. $this->blocks->load_blocks();
  1341. if (empty($this->_block_actions_done)) {
  1342. $this->_block_actions_done = true;
  1343. if ($this->blocks->process_url_actions($this)) {
  1344. redirect($this->url->out(false));
  1345. }
  1346. }
  1347. $this->blocks->create_all_block_instances();
  1348. }
  1349. // If maintenance mode is on, change the page header.
  1350. if (!empty($CFG->maintenance_enabled)) {
  1351. $this->set_button('<a href="' . $CFG->wwwroot . '/' . $CFG->admin .
  1352. '/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
  1353. '</a> ' . $this->button);
  1354. $title = $this->title;
  1355. if ($title) {
  1356. $title .= ' - ';
  1357. }
  1358. $this->set_title($title . get_string('maintenancemode', 'admin'));
  1359. }
  1360. $this->initialise_standard_body_classes();
  1361. }
  1362. /**
  1363. * Method for use by Moodle core to set up the theme. Do not
  1364. * use this in your own code.
  1365. *
  1366. * Make sure the right theme for this page is loaded. Tell our
  1367. * blocks_manager about the theme block regions, and then, if
  1368. * we are $PAGE, set up the global $OUTPUT.
  1369. *
  1370. * @return void
  1371. */
  1372. public function initialise_theme_and_output() {
  1373. global $OUTPUT, $PAGE, $SITE, $CFG;
  1374. if (!empty($this->_wherethemewasinitialised)) {
  1375. return;
  1376. }
  1377. if (!during_initial_install()) {
  1378. // Detect PAGE->context mess.
  1379. $this->magic_get_context();
  1380. }
  1381. if (!$this->_course && !during_initial_install()) {
  1382. $this->set_course($SITE);
  1383. }
  1384. if (is_null($this->_theme)) {
  1385. $themename = $this->resolve_theme();
  1386. $this->_theme = theme_config::load($themename);
  1387. }
  1388. $this->_theme->setup_blocks($this->pagelayout, $this->blocks);
  1389. if ($this === $PAGE) {
  1390. $target = null;
  1391. if ($this->pagelayout === 'maintenance') {
  1392. // If the page is using the maintenance layout then we're going to force target to maintenance.
  1393. // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
  1394. // page layout.
  1395. $target = RENDERER_TARGET_MAINTENANCE;
  1396. }
  1397. $OUTPUT = $this->get_renderer('core', null, $target);
  1398. }
  1399. if (!during_initial_install()) {
  1400. $filtermanager = filter_manager::instance();
  1401. $filtermanager->setup_page_for_globally_available_filters($this);
  1402. }
  1403. $this->_wherethemewasinitialised = debug_backtrace();
  1404. }
  1405. /**
  1406. * Reset the theme and output for a new context. This only makes sense from
  1407. * external::validate_context(). Do not cheat.
  1408. *
  1409. * @return string the name of the theme that should be used on this page.
  1410. */
  1411. public function reset_theme_and_output() {
  1412. global $COURSE, $SITE;
  1413. $COURSE = clone($SITE);
  1414. $this->_theme = null;
  1415. $this->_wherethemewasinitialised = null;
  1416. $this->_course = null;
  1417. $this->_cm = null;
  1418. $this->_module = null;
  1419. $this->_context = null;
  1420. }
  1421. /**
  1422. * Work out the theme this page should use.
  1423. *
  1424. * This depends on numerous $CFG settings, and the properties of this page.
  1425. *
  1426. * @return string the name of the theme that should be used on this page.
  1427. */
  1428. protected function resolve_theme() {
  1429. global $CFG, $USER, $SESSION;
  1430. if (empty($CFG->themeorder)) {
  1431. $themeorder = array('course', 'category', 'session', 'user', 'cohort', 'site');
  1432. } else {
  1433. $themeorder = $CFG->themeorder;
  1434. // Just in case, make sure we always use the site theme if nothing else matched.
  1435. $themeorder[] = 'site';
  1436. }
  1437. $mnetpeertheme = '';
  1438. $mnetvarsok = isset($CFG->mnet_localhost_id) && isset($USER->mnethostid);
  1439. if (isloggedin() and $mnetvarsok and $USER->mnethostid != $CFG->mnet_localhost_id) {
  1440. require_once($CFG->dirroot.'/mnet/peer.php');
  1441. $mnetpeer = new mnet_peer();
  1442. $mnetpeer->set_id($USER->mnethostid);
  1443. if ($mnetpeer->force_theme == 1 && $mnetpeer->theme != '') {
  1444. $mnetpeertheme = $mnetpeer->theme;
  1445. }
  1446. }
  1447. $devicetheme = core_useragent::get_device_type_theme($this->devicetypeinuse);
  1448. // The user is using another device than default, and we have a theme for that, we should use it.
  1449. $hascustomdevicetheme = core_useragent::DEVICETYPE_DEFAULT != $this->devicetypeinuse && !empty($devicetheme);
  1450. foreach ($themeorder as $themetype) {
  1451. switch ($themetype) {
  1452. case 'course':
  1453. if (!empty($CFG->allowcoursethemes) && !empty($this->_course->theme) && !$hascustomdevicetheme) {
  1454. return $this->_course->theme;
  1455. }
  1456. break;
  1457. case 'category':
  1458. if (!empty($CFG->allowcategorythemes) && !$hascustomdevicetheme) {
  1459. $categories = $this->categories;
  1460. foreach ($categories as $category) {
  1461. if (!empty($category->theme)) {
  1462. return $category->theme;
  1463. }
  1464. }
  1465. }
  1466. break;
  1467. case 'session':
  1468. if (!empty($SESSION->theme)) {
  1469. return $SESSION->theme;
  1470. }
  1471. break;
  1472. case 'user':
  1473. if (!empty($CFG->allowuserthemes) && !empty($USER->theme) && !$hascustomdevicetheme) {
  1474. if ($mnetpeertheme) {
  1475. return $mnetpeertheme;
  1476. } else {
  1477. return $USER->theme;
  1478. }
  1479. }
  1480. break;
  1481. case 'cohort':
  1482. if (!empty($CFG->allowcohortthemes) && !empty($USER->cohorttheme) && !$hascustomdevicetheme) {
  1483. return $USER->cohorttheme;
  1484. }
  1485. break;
  1486. case 'site':
  1487. if ($mnetpeertheme) {
  1488. return $mnetpeertheme;
  1489. }
  1490. // First try for the device the user is using.
  1491. if (!empty($devicetheme)) {
  1492. return $devicetheme;
  1493. }
  1494. // Next try for the default device (as a fallback).
  1495. $devicetheme = core_useragent::get_device_type_theme(core_useragent::DEVICETYPE_DEFAULT);
  1496. if (!empty($devicetheme)) {
  1497. return $devicetheme;
  1498. }
  1499. // The default device theme isn't set up - use the overall default theme.
  1500. return theme_config::DEFAULT_THEME;
  1501. }
  1502. }
  1503. // We should most certainly have resolved a theme by now. Something has gone wrong.
  1504. debugging('Error resolving the theme to use for this page.', DEBUG_DEVELOPER);
  1505. return theme_config::DEFAULT_THEME;
  1506. }
  1507. /**
  1508. * Sets ->pagetype from the script name. For example, if the script that was
  1509. * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
  1510. *
  1511. * @param string $script the path to the script that should be used to
  1512. * initialise ->pagetype. If not passed the $SCRIPT global will be used.
  1513. * If legacy code has set $CFG->pagepath that will be used instead, and a
  1514. * developer warning issued.
  1515. */
  1516. protected function initialise_default_pagetype($script = null) {
  1517. global $CFG, $SCRIPT;
  1518. if (isset($CFG->pagepath)) {
  1519. debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
  1520. 'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
  1521. $script = $CFG->pagepath;
  1522. unset($CFG->pagepath);
  1523. }
  1524. if (is_null($script)) {
  1525. $script = ltrim($SCRIPT, '/');
  1526. $len = strlen($CFG->admin);
  1527. if (substr($script, 0, $len) == $CFG->admin) {
  1528. $script = 'admin' . substr($script, $len);
  1529. }
  1530. }
  1531. $path = str_replace('.php', '', $script);
  1532. if (substr($path, -1) == '/') {
  1533. $path .= 'index';
  1534. }
  1535. if (empty($path) || $path == 'index') {
  1536. $this->_pagetype = 'site-index';
  1537. } else {
  1538. $this->_pagetype = str_replace('/', '-', $path);
  1539. }
  1540. }
  1541. /**
  1542. * Initialises the CSS classes that will be added to body tag of the page.
  1543. *
  1544. * The function is responsible for adding all of the critical CSS classes
  1545. * that describe the current page, and its state.
  1546. * This includes classes that describe the following for example:
  1547. * - Current language
  1548. * - Language direction
  1549. * - YUI CSS initialisation
  1550. * - Pagelayout
  1551. * These are commonly used in CSS to target specific types of pages.
  1552. */
  1553. protected function initialise_standard_body_classes() {
  1554. global $CFG, $USER;
  1555. $pagetype = $this->pagetype;
  1556. if ($pagetype == 'site-index') {
  1557. $this->_legacyclass = 'course';
  1558. } else if (substr($pagetype, 0, 6) == 'admin-') {
  1559. $this->_legacyclass = 'admin';
  1560. }
  1561. $this->add_body_class($this->_legacyclass);
  1562. $pathbits = explode('-', trim($pagetype));
  1563. for ($i = 1; $i < count($pathbits); $i++) {
  1564. $this->add_body_class('path-' . join('-', array_slice($pathbits, 0, $i)));
  1565. }
  1566. $this->add_body_classes(core_useragent::get_browser_version_classes());
  1567. $this->add_body_class('dir-' . get_string('thisdirection', 'langconfig'));
  1568. $this->add_body_class('lang-' . current_language());
  1569. $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
  1570. $this->add_body_class('yui3-skin-sam'); // Make YUI3 happy, if it is used.
  1571. $this->add_body_class($this->url_to_class_name($CFG->wwwroot));
  1572. // Extra class describing current page layout.
  1573. $this->add_body_class('pagelayout-' . $this->_pagelayout);
  1574. if (!during_initial_install()) {
  1575. $this->add_body_class('course-' . $this->_course->id);
  1576. $this->add_body_class('context-' . $this->_context->id);
  1577. }
  1578. if (!empty($this->_cm)) {
  1579. $this->add_body_class('cmid-' . $this->_cm->id);
  1580. }
  1581. if (!empty($CFG->allowcategorythemes)) {
  1582. $this->ensure_category_loaded();
  1583. foreach ($this->_categories as $catid => $notused) {
  1584. $this->add_body_class('category-' . $catid);
  1585. }
  1586. } else {
  1587. $catid = 0;
  1588. if (is_array($this->_categories)) {
  1589. $catids = array_keys($this->_categories);
  1590. $catid = reset($catids);
  1591. } else if (!empty($this->_course->category)) {
  1592. $catid = $this->_course->category;
  1593. }
  1594. if ($catid) {
  1595. $this->add_body_class('category-' . $catid);
  1596. }
  1597. }
  1598. if (!isloggedin()) {
  1599. $this->add_body_class('notloggedin');
  1600. }
  1601. if ($this->user_is_editing()) {
  1602. $this->add_body_class('editing');
  1603. if (optional_param('bui_moveid', false, PARAM_INT)) {
  1604. $this->add_body_class('blocks-moving');
  1605. }
  1606. }
  1607. if (!empty($CFG->blocksdrag)) {
  1608. $this->add_body_class('drag');
  1609. }
  1610. if ($this->_devicetypeinuse != 'default') {
  1611. $this->add_body_class($this->_devicetypeinuse . 'theme');
  1612. }
  1613. // Add class for behat site to apply behat related fixes.
  1614. if (defined('BEHAT_SITE_RUNNING')) {
  1615. $this->add_body_class('behat-site');
  1616. }
  1617. }
  1618. /**
  1619. * Loads the activity record for the current CM object associated with this
  1620. * page.
  1621. *
  1622. * This will load {@link moodle_page::$_module} with a row from the related
  1623. * module table in the database.
  1624. * For instance if {@link moodle_page::$_cm} is a forum then a row from the
  1625. * forum table will be loaded.
  1626. */
  1627. protected function load_activity_record() {
  1628. global $DB;
  1629. if (is_null($this->_cm)) {
  1630. return;
  1631. }
  1632. $this->_module = $DB->get_record($this->_cm->modname, array('id' => $this->_cm->instance));
  1633. }
  1634. /**
  1635. * This function ensures that the category of the current course has been
  1636. * loaded, and if not, the function loads it now.
  1637. *
  1638. * @return void
  1639. * @throws coding_exception
  1640. */
  1641. protected function ensure_category_loaded() {
  1642. if (is_array($this->_categories)) {
  1643. return; // Already done.
  1644. }
  1645. if (is_null($this->_course)) {
  1646. throw new coding_exception('Attempt to get the course category for this page before the course was set.');
  1647. }
  1648. if ($this->_course->category == 0) {
  1649. $this->_categories = array();
  1650. } else {
  1651. $this->load_category($this->_course->category);
  1652. }
  1653. }
  1654. /**
  1655. * Loads the requested category into the pages categories array.
  1656. *
  1657. * @param int $categoryid
  1658. * @throws moodle_exception
  1659. */
  1660. protected function load_category($categoryid) {
  1661. global $DB;
  1662. $category = $DB->get_record('course_categories', array('id' => $categoryid));
  1663. if (!$category) {
  1664. throw new moodle_exception('unknowncategory');
  1665. }
  1666. $this->_categories[$category->id] = $category;
  1667. $parentcategoryids = explode('/', trim($category->path, '/'));
  1668. array_pop($parentcategoryids);
  1669. foreach (array_reverse($parentcategoryids) as $catid) {
  1670. $this->_categories[$catid] = null;
  1671. }
  1672. }
  1673. /**
  1674. * Ensures that the category the current course is within, as well as all of
  1675. * its parent categories, have been loaded.
  1676. *
  1677. * @return void
  1678. */
  1679. protected function ensure_categories_loaded() {
  1680. global $DB;
  1681. $this->ensure_category_loaded();
  1682. if (!is_null(end($this->_categories))) {
  1683. return; // Already done.
  1684. }
  1685. $idstoload = array_keys($this->_categories);
  1686. array_shift($idstoload);
  1687. $categories = $DB->get_records_list('course_categories', 'id', $idstoload);
  1688. foreach ($idstoload as $catid) {
  1689. $this->_categories[$catid] = $categories[$catid];
  1690. }
  1691. }
  1692. /**
  1693. * Ensure the theme has not been loaded yet. If it has an exception is thrown.
  1694. *
  1695. * @throws coding_exception
  1696. */
  1697. protected function ensure_theme_not_set() {
  1698. // This is explicitly allowed for webservices though which may process many course contexts in a single request.
  1699. if (WS_SERVER) {
  1700. return;
  1701. }
  1702. if (!is_null($this->_theme)) {
  1703. throw new coding_exception('The theme has already been set up for this page ready for output. ' .
  1704. 'Therefore, you can no longer change the theme, or anything that might affect what ' .
  1705. 'the current theme is, for example, the course.',
  1706. 'Stack trace when the theme was set up: ' . format_backtrace($this->_wherethemewasinitialised));
  1707. }
  1708. }
  1709. /**
  1710. * Converts the provided URL into a CSS class that be used within the page.
  1711. * This is primarily used to add the wwwroot to the body tag as a CSS class.
  1712. *
  1713. * @param string $url
  1714. * @return string
  1715. */
  1716. protected function url_to_class_name($url) {
  1717. $bits = parse_url($url);
  1718. $class = str_replace('.', '-', $bits['host']);
  1719. if (!empty($bits['port'])) {
  1720. $class .= '--' . $bits['port'];
  1721. }
  1722. if (!empty($bits['path'])) {
  1723. $path = trim($bits['path'], '/');
  1724. if ($path) {
  1725. $class .= '--' . str_replace('/', '-', $path);
  1726. }
  1727. }
  1728. return $class;
  1729. }
  1730. /**
  1731. * Combines all of the required editing caps for the page and returns them
  1732. * as an array.
  1733. *
  1734. * @return array
  1735. */
  1736. protected function all_editing_caps() {
  1737. $caps = $this->_othereditingcaps;
  1738. $caps[] = $this->_blockseditingcap;
  1739. return $caps;
  1740. }
  1741. /**
  1742. * Returns true if the page URL has beem set.
  1743. *
  1744. * @return bool
  1745. */
  1746. public function has_set_url() {
  1747. return ($this->_url!==null);
  1748. }
  1749. /**
  1750. * Gets set when the block actions for the page have been processed.
  1751. *
  1752. * @param bool $setting
  1753. */
  1754. public function set_block_actions_done($setting = true) {
  1755. $this->_block_actions_done = $setting;
  1756. }
  1757. /**
  1758. * Are popup notifications allowed on this page?
  1759. * Popup notifications may be disallowed in situations such as while upgrading or completing a quiz
  1760. *
  1761. * @return bool true if popup notifications may be displayed
  1762. */
  1763. public function get_popup_notification_allowed() {
  1764. return $this->_popup_notification_allowed;
  1765. }
  1766. /**
  1767. * Allow or disallow popup notifications on this page. Popups are allowed by default.
  1768. *
  1769. * @param bool $allowed true if notifications are allowed. False if not allowed. They are allowed by default.
  1770. */
  1771. public function set_popup_notification_allowed($allowed) {
  1772. $this->_popup_notification_allowed = $allowed;
  1773. }
  1774. /**
  1775. * Returns the block region having made any required theme manipulations.
  1776. *
  1777. * @since Moodle 2.5.1 2.6
  1778. * @param string $region
  1779. * @return string
  1780. */
  1781. public function apply_theme_region_manipulations($region) {
  1782. if ($this->blockmanipulations && isset($this->blockmanipulations[$region])) {
  1783. $regionwas = $region;
  1784. $regionnow = $this->blockmanipulations[$region];
  1785. if ($this->blocks->is_known_region($regionwas) && $this->blocks->is_known_region($regionnow)) {
  1786. // Both the before and after regions are known so we can swap them over.
  1787. return $regionnow;
  1788. }
  1789. // We didn't know about both, we won't swap them over.
  1790. return $regionwas;
  1791. }
  1792. return $region;
  1793. }
  1794. /**
  1795. * Add a report node and a specific report to the navigation.
  1796. *
  1797. * @param int $userid The user ID that we are looking to add this report node to.
  1798. * @param array $nodeinfo Name and url of the final node that we are creating.
  1799. */
  1800. public function add_report_nodes($userid, $nodeinfo) {
  1801. global $USER;
  1802. // Try to find the specific user node.
  1803. $newusernode = $this->navigation->find('user' . $userid, null);
  1804. $reportnode = null;
  1805. $navigationnodeerror =
  1806. 'Could not find the navigation node requested. Please check that the node you are looking for exists.';
  1807. if ($userid != $USER->id) {
  1808. // Check that we have a valid node.
  1809. if (empty($newusernode)) {
  1810. // Throw an error if we ever reach here.
  1811. throw new coding_exception($navigationnodeerror);
  1812. }
  1813. // Add 'Reports' to the user node.
  1814. $reportnode = $newusernode->add(get_string('reports'));
  1815. } else {
  1816. // We are looking at our own profile.
  1817. $myprofilenode = $this->settingsnav->find('myprofile', null);
  1818. // Check that we do end up with a valid node.
  1819. if (empty($myprofilenode)) {
  1820. // Throw an error if we ever reach here.
  1821. throw new coding_exception($navigationnodeerror);
  1822. }
  1823. // Add 'Reports' to our node.
  1824. $reportnode = $myprofilenode->add(get_string('reports'));
  1825. }
  1826. // Finally add the report to the navigation tree.
  1827. $reportnode->add($nodeinfo['name'], $nodeinfo['url'], navigation_node::TYPE_COURSE);
  1828. }
  1829. /**
  1830. * Add some HTML to the list of actions to render in the header actions menu.
  1831. *
  1832. * @param string $html The HTML to add.
  1833. */
  1834. public function add_header_action(string $html) : void {
  1835. $this->_headeractions[] = $html;
  1836. }
  1837. /**
  1838. * Get the list of HTML for actions to render in the header actions menu.
  1839. *
  1840. * @return string[]
  1841. */
  1842. public function get_header_actions() : array {
  1843. return $this->_headeractions;
  1844. }
  1845. /**
  1846. * Set the flag to indicate if the region main settings should be rendered as an action
  1847. * in the header actions menu rather than at the top of the content.
  1848. *
  1849. * @param bool $value If the settings should be in the header.
  1850. */
  1851. public function set_include_region_main_settings_in_header_actions(bool $value) : void {
  1852. $this->_regionmainsettingsinheader = $value;
  1853. }
  1854. /**
  1855. * Check if the region main settings should be rendered as an action in the header actions
  1856. * menu rather than at the top of the content.
  1857. *
  1858. * @return bool
  1859. */
  1860. public function include_region_main_settings_in_header_actions() : bool {
  1861. return $this->_regionmainsettingsinheader;
  1862. }
  1863. }