Skip to content

Commit

Permalink
moodle_page: MDL-12212 implement ->pagetype
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed May 6, 2009
1 parent e3e40b4 commit f230ce1
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 72 deletions.
4 changes: 0 additions & 4 deletions admin/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

?>
1 change: 1 addition & 0 deletions admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 2 additions & 9 deletions blog/blogpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down
13 changes: 7 additions & 6 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)) {
Expand Down
16 changes: 8 additions & 8 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
56 changes: 35 additions & 21 deletions lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <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.
Expand Down Expand Up @@ -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 <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.
*/
Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 <strong>get_type()</strong>', E_USER_ERROR);
return NULL;
}

// Simple stuff, do not override this.
function get_id() {
return $this->id;
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions lib/simpletest/testpagelib_moodlepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
4 changes: 0 additions & 4 deletions mod/chat/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ function init_quick($data) {
$this->activityname = 'chat';
parent::init_quick($data);
}

function get_type() {
return PAGE_CHAT_VIEW;
}
}

?>
4 changes: 0 additions & 4 deletions mod/data/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

?>
4 changes: 0 additions & 4 deletions mod/lesson/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 0 additions & 4 deletions mod/quiz/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ function init_quick($data) {
$this->activityname = 'quiz';
parent::init_quick($data);
}

function get_type() {
return PAGE_QUIZ_VIEW;
}
}

?>
4 changes: 0 additions & 4 deletions my/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions tag/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f230ce1

Please sign in to comment.