]> git.mjollnir.org Git - moodle.git/commitdiff
moodle_page: MDL-12212 implement ->pagetype
authortjhunt <tjhunt>
Wed, 6 May 2009 08:34:32 +0000 (08:34 +0000)
committertjhunt <tjhunt>
Wed, 6 May 2009 08:34:32 +0000 (08:34 +0000)
13 files changed:
admin/pagelib.php
admin/settings.php
blog/blogpage.php
lib/adminlib.php
lib/blocklib.php
lib/pagelib.php
lib/simpletest/testpagelib_moodlepage.php
mod/chat/pagelib.php
mod/data/pagelib.php
mod/lesson/pagelib.php
mod/quiz/pagelib.php
my/pagelib.php
tag/pagelib.php

index ac4e3a8d03fac46a25ffacd84b16125102c54c8f..4b99ab83a20bb81cb990b042c03272adc12680a3 100644 (file)
@@ -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;
-    }
 }
 
 ?>
index cc39b20956b9ffde6187d734ae977678cb67c8bf..15217708ab5a9d8ec568e864351c003dae190b9c 100644 (file)
@@ -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);
index baff9b6fb1b8425fd2ff270833de3e965139800c..f246bf1ceeb8ac4097ed74027a2fc86547cd5856 100644 (file)
@@ -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';
index 0c830faaf94234c68e6d0cf97343f7675504d924..59fb555809d71858b762ac884d69cbb6bb201d2f 100644 (file)
@@ -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)) {
index c863808c0a1a662d79f2f59ed97fb481bed0cbdc..80350491141efabbdbeb82b3f96f09bde16c5c6c 100644 (file)
@@ -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;
index eafd37a44d1d6b09d38386dfcb72e113fbee0a09..697bccb9997d0e1287eee6ea58ba6f1d94b4697e 100644 (file)
@@ -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.
index f7de99017f908f1044199ce8826317ffc3a114e2..84c5ddf7a5599e466a59ec3c947e38867b2c6936 100644 (file)
@@ -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);
+    }
 }
 
 /**
index 3c5ca7a1048340a037d4899d3fa300c7f5a9322a..23b438a59053c79b9b51dd1ce3b2dfdff929441b 100644 (file)
@@ -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;
-    }
 }
 
 ?>
index b0b80ea16bab1c01a6a3563c34bfcf6e93b96f0a..8b99f8c51ff3a459521eb6841e6c594f9ffa14d9 100755 (executable)
@@ -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;
-    }
 }
 
 ?>
index c309433f43d45799d4d14ce0a203be8bf0da5086..0d25e9bffad27e4ac287b6d30cd11acf7f8c603f 100644 (file)
@@ -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);
     }
index 884a227f09d4cd5199e662d4b0c67bbe13f0b722..c8b9aaa5bcd9ceb903c8890fd0381acb1a8d5987 100644 (file)
@@ -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;
-    }
 }
 
 ?>
index 3ed3f6abdd3dffceb20e6ece1b4637173ca47b23..ce7cdd4d94bb20e3479c30a1492ca7748272d8ae 100644 (file)
@@ -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) {
index d18908fdd9b9caf8c77af42cabd38328ba243034..c1611d2b95aa8e309a9d19efdc7d85e1e92ac84a 100644 (file)
@@ -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);