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

/lib/pagelib.php

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