PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pagelib.php

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