diff --git a/admin/pagelib.php b/admin/pagelib.php index ac4e3a8d03fac..4b99ab83a20bb 100644 --- a/admin/pagelib.php +++ b/admin/pagelib.php @@ -151,10 +151,6 @@ function print_header($section = '', $focus='') { print_header("$SITE->shortname: " . implode(": ",$this->visiblepathtosection), $SITE->fullname, $navigation, $focus, '', true, $buttons, ''); } - - function get_type() { - return PAGE_ADMIN; - } } ?> diff --git a/admin/settings.php b/admin/settings.php index cc39b20956b9f..15217708ab5a9 100644 --- a/admin/settings.php +++ b/admin/settings.php @@ -11,6 +11,7 @@ /// no guest autologin require_login(0, false); +$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); $adminroot = admin_get_root(); // need all settings $page = $adminroot->locate($section); diff --git a/blog/blogpage.php b/blog/blogpage.php index baff9b6fb1b84..f246bf1ceeb8a 100644 --- a/blog/blogpage.php +++ b/blog/blogpage.php @@ -19,13 +19,6 @@ class page_blog extends page_base { var $filterselect = NULL; var $tagid = NULL; - // Mandatory; should return our identifier. - function get_type() { - global $CFG; - require_once($CFG->dirroot .'/blog/lib.php'); - return PAGE_BLOG_VIEW; - } - // we have no format type, use 'blog' //I think it's a bug, but if this is left the default NULL value then pages can //fail to load completely @@ -154,8 +147,8 @@ function blocks_get_default() { $this->init_full(); // It's a normal blog page - if (!empty($CFG->{'defaultblocks_'. $this->get_type()})) { - $blocknames = $CFG->{'defaultblocks_'. $this->get_type()}; + if (!empty($CFG->{'defaultblocks_'. $this->pagetype})) { + $blocknames = $CFG->{'defaultblocks_'. $this->pagetype}; } else { /// Failsafe - in case nothing was defined. $blocknames = 'admin,calendar_month,online_users,blog_menu'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 0c830faaf9423..59fb555809d71 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3904,6 +3904,13 @@ function admin_externalpage_setup($section, $extrabutton='', $extraurlparams=arr die; } + page_map_class(PAGE_ADMIN, 'page_admin'); + $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number + $PAGE->init_extra($section); // hack alert! + $PAGE->set_extra_button($extrabutton); + $PAGE->set_extra_url_params($extraurlparams, $actualurl); + $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); + $adminroot = admin_get_root(false, false); // settings not required for external pages $extpage = $adminroot->locate($section); @@ -3918,12 +3925,6 @@ function admin_externalpage_setup($section, $extrabutton='', $extraurlparams=arr die; } - page_map_class(PAGE_ADMIN, 'page_admin'); - $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number - $PAGE->init_extra($section); // hack alert! - $PAGE->set_extra_button($extrabutton); - $PAGE->set_extra_url_params($extraurlparams, $actualurl); - $adminediting = optional_param('adminedit', -1, PARAM_BOOL); if (!isset($USER->adminediting)) { diff --git a/lib/blocklib.php b/lib/blocklib.php index c863808c0a1a6..80350491141ef 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -645,13 +645,13 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, $sql = "SELECT 1, MAX(weight) + 1 AS nextfree FROM {block_pinned} WHERE pagetype = ? AND position = ?"; - $params = array($page->get_type(), $newpos); + $params = array($page->pagetype, $newpos); } else { $sql = "SELECT 1, MAX(weight) + 1 AS nextfree FROM {block_instance} WHERE pageid = ? AND pagetype = ? AND position = ?"; - $params = array($page->get_id(), $page->get_type(), $newpos); + $params = array($page->get_id(), $page->pagetype, $newpos); } $weight = $DB->get_record_sql($sql, $params); @@ -660,7 +660,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, if (empty($pinned)) { $newinstance->pageid = $page->get_id(); } - $newinstance->pagetype = $page->get_type(); + $newinstance->pagetype = $page->pagetype; $newinstance->position = $newpos; $newinstance->weight = empty($weight->nextfree) ? 0 : $weight->nextfree; $newinstance->visible = 1; @@ -835,7 +835,7 @@ function blocks_get_pinned($page) { } $select = "pagetype = ?"; - $params = array($page->get_type()); + $params = array($page->pagetype); if ($visible) { $select .= " AND visible = 1"; @@ -902,8 +902,8 @@ function blocks_get_by_page_pinned($page) { function blocks_get_by_page($page) { global $DB; - $blocks = $DB->get_records_select('block_instance', "pageid = ? AND pagetype = ?", array($page->get_id(), $page->get_type()), - 'position, weight'); + $blocks = $DB->get_records_select('block_instance', "pageid = ? AND pagetype = ?", + array($page->get_id(), $page->pagetype), 'position, weight'); $positions = $page->blocks_get_positions(); $arr = array(); @@ -1002,7 +1002,7 @@ function blocks_repopulate_page($page) { // indexed and the indexes match, so we can work straight away... but CAREFULLY! // Ready to start creating block instances, but first drop any existing ones - blocks_delete_all_on_page($page->get_type(), $page->get_id()); + blocks_delete_all_on_page($page->pagetype, $page->get_id()); // Here we slyly count $posblocks and NOT $positions. This can actually make a difference // if the textual representation has undefined slots in the end. So we only work with as many @@ -1016,7 +1016,7 @@ function blocks_repopulate_page($page) { $newinstance = new stdClass; $newinstance->blockid = $idforname[$blockname]; $newinstance->pageid = $page->get_id(); - $newinstance->pagetype = $page->get_type(); + $newinstance->pagetype = $page->pagetype; $newinstance->position = $position; $newinstance->weight = $weight; $newinstance->visible = 1; diff --git a/lib/pagelib.php b/lib/pagelib.php index eafd37a44d1d6..697bccb9997d0 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -54,6 +54,8 @@ class moodle_page { protected $_context = null; + protected $_pagetype = null; + /** * @return integer one of the STATE_... constants. You should not normally need * to use this in your code. It is indended for internal use by this class @@ -96,6 +98,16 @@ public function get_context() { return $this->_context; } + /** + * @return string e.g. 'my-index' or 'mod-quiz-attempt'. Same as the id attribute on
. + */ + public function get_pagetype() { + if (is_null($this->_pagetype)) { + throw new coding_exception('$PAGE->pagetype accessed before it was known.'); + } + return $this->_pagetype; + } + /** * Set the state. The state must be one of that STATE_... constants, and * the state is only allowed to advance one step at a time. @@ -157,6 +169,18 @@ public function set_context($context) { $this->_context = $context; } + /** + * @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'. Normally + * you do not need to set this manually, it is automatically created from the + * script name. However, on some pages this is overridden. For example, the + * page type for coures/view.php includes the course format, for example + * 'coures-view-weeks'. This gets used as the id attribute on and + * also for determining which blocks are displayed. + */ + public function set_pagetype($pagetype) { + $this->_pagetype = $pagetype; + } + /** * PHP overloading magic to make the $PAGE->course syntax work. */ @@ -168,6 +192,16 @@ public function __get($field) { throw new coding_exception('Unknown field ' . $field . ' of $PAGE.'); } } + +/// Deperecated fields and methods for backwards compatibility ================= + /** + * @deprecated since Moodle 2.0 - use $PAGE->pagetype instaed. + * @return string page type. + */ + public function get_type() { + debugging('Call to deprecated method moodle_page::get_type. Please use $PAGE->pagetype instead.'); + return $this->get_pagetype(); + } } /** @@ -203,17 +237,11 @@ function page_create_object($type, $id = NULL) { $data->pageid = $id; $classname = page_map_class($type); - $object = new $classname; - // TODO: subclassing check here - - if ($object->get_type() !== $type) { - // Somehow somewhere someone made a mistake - debugging('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')'); - } $object->init_quick($data); $object->set_course($PAGE->course); + $object->set_pagetype($type); return $object; } @@ -389,14 +417,6 @@ function url_get_full($extraparams = array()) { return $path; } - // This forces implementers to actually hardwire their page identification constant in the class. - // Good thing, if you ask me. That way we can later auto-detect "installed" page types by querying - // the classes themselves in the future. - function get_type() { - trigger_error('Page class does not implement method get_type()', E_USER_ERROR); - return NULL; - } - // Simple stuff, do not override this. function get_id() { return $this->id; @@ -582,12 +602,6 @@ function print_header($title, $morenavlinks=NULL, $meta='', $bodytags='', $extra // SELF-REPORTING SECTION - // This is hardwired here so the factory function page_create_object() can be sure there was no mistake. - // Also, it doubles as a way to let others inquire about our type. - function get_type() { - return PAGE_COURSE_VIEW; - } - // This is like the "category" of a page of this "type". For example, if the type is PAGE_COURSE_VIEW // the format_name is the actual name of the course format. If the type were PAGE_ACTIVITY_VIEW, then // the format_name might be that activity's name etc. diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index f7de99017f908..84c5ddf7a5599 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -166,6 +166,20 @@ public function test_set_context() { // Validate $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context); } + + public function test_cant_get_pagetype_before_set() { + // Set expectation. + $this->expectException(); + // Exercise SUT + $this->testpage->pagetype; + } + + public function test_set_pagetype() { + // Exercise SUT + $this->testpage->set_pagetype('a-page-type'); + // Validate + $this->assertEqual('a-page-type', $this->testpage->pagetype); + } } /** diff --git a/mod/chat/pagelib.php b/mod/chat/pagelib.php index 3c5ca7a104834..23b438a59053c 100644 --- a/mod/chat/pagelib.php +++ b/mod/chat/pagelib.php @@ -24,10 +24,6 @@ function init_quick($data) { $this->activityname = 'chat'; parent::init_quick($data); } - - function get_type() { - return PAGE_CHAT_VIEW; - } } ?> diff --git a/mod/data/pagelib.php b/mod/data/pagelib.php index b0b80ea16bab1..8b99f8c51ff3a 100755 --- a/mod/data/pagelib.php +++ b/mod/data/pagelib.php @@ -31,10 +31,6 @@ function init_quick($data) { function print_header($title, $morenavlinks = NULL, $meta) { parent::print_header($title, $morenavlinks, '', $meta); } - - function get_type() { - return PAGE_DATA_VIEW; - } } ?> diff --git a/mod/lesson/pagelib.php b/mod/lesson/pagelib.php index c309433f43d45..0d25e9bffad27 100644 --- a/mod/lesson/pagelib.php +++ b/mod/lesson/pagelib.php @@ -140,10 +140,6 @@ function print_header($title = '', $morenavlinks = array()) { lesson_print_messages(); } - function get_type() { - return PAGE_LESSON_VIEW; - } - function blocks_get_positions() { return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT); } diff --git a/mod/quiz/pagelib.php b/mod/quiz/pagelib.php index 884a227f09d4c..c8b9aaa5bcd9c 100644 --- a/mod/quiz/pagelib.php +++ b/mod/quiz/pagelib.php @@ -25,10 +25,6 @@ function init_quick($data) { $this->activityname = 'quiz'; parent::init_quick($data); } - - function get_type() { - return PAGE_QUIZ_VIEW; - } } ?> diff --git a/my/pagelib.php b/my/pagelib.php index 3ed3f6abdd3df..ce7cdd4d94bb2 100644 --- a/my/pagelib.php +++ b/my/pagelib.php @@ -4,10 +4,6 @@ class page_my_moodle extends page_base { - function get_type() { - return PAGE_MY_MOODLE; - } - function user_allowed_editing() { page_id_and_class($id,$class); if ($id == PAGE_MY_MOODLE) { diff --git a/tag/pagelib.php b/tag/pagelib.php index d18908fdd9b9c..c1611d2b95aa8 100644 --- a/tag/pagelib.php +++ b/tag/pagelib.php @@ -9,10 +9,6 @@ class page_tag extends page_base { var $tag_object = NULL; - - function get_type() { - return PAGE_TAG_INDEX; - } function user_allowed_editing() { $systemcontext = get_context_instance(CONTEXT_SYSTEM);