moodle_page: MDL-12212 implement ->pagetype
This commit is contained in:
@@ -151,10 +151,6 @@ class page_admin extends page_base {
|
||||
|
||||
print_header("$SITE->shortname: " . implode(": ",$this->visiblepathtosection), $SITE->fullname, $navigation, $focus, '', true, $buttons, '');
|
||||
}
|
||||
|
||||
function get_type() {
|
||||
return PAGE_ADMIN;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -11,6 +11,7 @@ $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
|
||||
|
||||
/// 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);
|
||||
|
||||
+2
-9
@@ -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 @@ class page_blog extends page_base {
|
||||
$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';
|
||||
|
||||
+7
-6
@@ -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)) {
|
||||
|
||||
+8
-8
@@ -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;
|
||||
|
||||
+35
-21
@@ -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 @@ class moodle_page {
|
||||
return $this->_context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string e.g. 'my-index' or 'mod-quiz-attempt'. Same as the id attribute on <body>.
|
||||
*/
|
||||
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 @@ class moodle_page {
|
||||
$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 <body> 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 @@ class moodle_page {
|
||||
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 @@ class page_base extends moodle_page {
|
||||
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 <strong>get_type()</strong>', E_USER_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Simple stuff, do not override this.
|
||||
function get_id() {
|
||||
return $this->id;
|
||||
@@ -582,12 +602,6 @@ class page_course extends page_base {
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -166,6 +166,20 @@ class moodle_page_test extends UnitTestCase {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,10 +24,6 @@ class page_chat extends page_generic_activity {
|
||||
$this->activityname = 'chat';
|
||||
parent::init_quick($data);
|
||||
}
|
||||
|
||||
function get_type() {
|
||||
return PAGE_CHAT_VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -31,10 +31,6 @@ class page_data extends page_generic_activity {
|
||||
function print_header($title, $morenavlinks = NULL, $meta) {
|
||||
parent::print_header($title, $morenavlinks, '', $meta);
|
||||
}
|
||||
|
||||
function get_type() {
|
||||
return PAGE_DATA_VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -140,10 +140,6 @@ class page_lesson extends page_generic_activity {
|
||||
lesson_print_messages();
|
||||
}
|
||||
|
||||
function get_type() {
|
||||
return PAGE_LESSON_VIEW;
|
||||
}
|
||||
|
||||
function blocks_get_positions() {
|
||||
return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT);
|
||||
}
|
||||
|
||||
@@ -25,10 +25,6 @@ class page_quiz extends page_generic_activity {
|
||||
$this->activityname = 'quiz';
|
||||
parent::init_quick($data);
|
||||
}
|
||||
|
||||
function get_type() {
|
||||
return PAGE_QUIZ_VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -4,10 +4,6 @@ require_once($CFG->libdir.'/pagelib.php');
|
||||
|
||||
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) {
|
||||
|
||||
@@ -9,10 +9,6 @@ define('TAG_FORMAT', 'tag');
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user