From: defacer Date: Mon, 29 Nov 2004 04:19:05 +0000 (+0000) Subject: Fix for bug 2241: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9b12850043cab9d05fd1f00a8a5fc45218f71725;p=moodle.git Fix for bug 2241: I hope this time I got everything changed to the new Page interface! I 'm really sorry for messing up and breaking new installations... :( Also moved the only two functions of the API outside the page_base class. --- diff --git a/admin/site.php b/admin/site.php index 4c0dfc68f5..f1b20bb92f 100644 --- a/admin/site.php +++ b/admin/site.php @@ -41,7 +41,7 @@ if ($newid = insert_record('course', $form)) { // Site created, add blocks for it - $page = page_base::create_object(MOODLE_PAGE_COURSE, $newid); + $page = page_create_object(MOODLE_PAGE_COURSE, $newid); blocks_repopulate_page($page); // Return value not checked because you can always edit later $cat->name = get_string('miscellaneous'); diff --git a/backup/backuplib.php b/backup/backuplib.php index 339a6fce8a..e6fa4f6176 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -616,7 +616,7 @@ // Read all of the block table $blocks = blocks_get_record(); - $page = MoodlePage::create_object(MOODLE_PAGE_COURSE, $preferences->backup_course); + $page = page_create_object(MOODLE_PAGE_COURSE, $preferences->backup_course); if ($instances = blocks_get_by_page($page)) { //Blocks open tag diff --git a/backup/restore_execute.html b/backup/restore_execute.html index cb5e8fc7f0..768d026535 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -117,7 +117,7 @@ if (empty($info->backup_block_format)) { // This is a backup from Moodle < 1.5 if (empty($course_header->blockinfo)) { // Looks like it's from Moodle < 1.3. Let's give the course default blocks... - $newpage = MoodlePage::create_object(MOODLE_PAGE_COURSE, $course_header->course_id); + $newpage = page_create_object(MOODLE_PAGE_COURSE, $course_header->course_id); blocks_repopulate_page($newpage); } else { diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 983ad472f2..82022f8860 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -296,7 +296,7 @@ class block_base { $title = $this->str->show; } - $page = page_base::create_object($this->instance->pagetype, $this->instance->pageid); + $page = page_create_object($this->instance->pagetype, $this->instance->pageid); $script = $page->url_get_full(array('instanceid' => $this->instance->id, 'sesskey' => $USER->sesskey)); $movebuttons .= '' . diff --git a/blocks/pagedemo.php b/blocks/pagedemo.php index 27d837eb71..ada8efc7c6 100644 --- a/blocks/pagedemo.php +++ b/blocks/pagedemo.php @@ -2,12 +2,7 @@ // All of this is standard Moodle fixtures - if (!file_exists('./config.php')) { - header('Location: install.php'); - die; - } - - require_once('config.php'); + require_once('../config.php'); require_once($CFG->dirroot .'/course/lib.php'); require_once($CFG->dirroot .'/lib/blocklib.php'); require_once($CFG->dirroot .'/mod/resource/lib.php'); @@ -111,11 +106,11 @@ // Before creating our page object, we need to map our page identifier to the actual name // of the class which will be handling its operations. Pretty simple, but essential. - MoodlePage::map_page_type(MOODLE_PAGE_TEST, 'MoodlePage_Test'); + page_map_class(MOODLE_PAGE_TEST, 'page_test'); // Now, create our page object. The identifier "1" is passed arbitrarily because we don't // have multiple "testpages"; if we did, that would be the "testpageid" from the database. - $PAGE = MoodlePage::create_object(MOODLE_PAGE_TEST, 1); + $PAGE = page_create_object(MOODLE_PAGE_TEST, 1); $PAGE->print_header(NULL); $editing = $PAGE->user_is_editing(); @@ -190,4 +185,4 @@ echo ''; print_footer(); -?> \ No newline at end of file +?> diff --git a/course/edit.php b/course/edit.php index 2ba4f4b776..42d2c472f1 100644 --- a/course/edit.php +++ b/course/edit.php @@ -52,7 +52,7 @@ if (!empty($course)) { // Test for and remove blocks which aren't appropriate anymore - $page = MoodlePage::create_object(MOODLE_PAGE_COURSE, $course->id); + $page = page_create_object(MOODLE_PAGE_COURSE, $course->id); blocks_remove_inappropriate($page); // Update with the new data @@ -69,7 +69,7 @@ if ($newcourseid = insert_record('course', $form)) { // Set up new course // Setup the blocks - $page = MoodlePage::create_object(MOODLE_PAGE_COURSE, $newcourseid); + $page = page_create_object(MOODLE_PAGE_COURSE, $newcourseid); blocks_repopulate_page($page); // Return value not checked because you can always edit later $section = NULL; diff --git a/course/view.php b/course/view.php index 087339d8c2..6c2986a108 100644 --- a/course/view.php +++ b/course/view.php @@ -37,7 +37,7 @@ $course->format = 'weeks'; // Default format is weeks } - $PAGE = page_base::create_object(MOODLE_PAGE_COURSE, $course->id); + $PAGE = page_create_object(MOODLE_PAGE_COURSE, $course->id); $pageblocks = blocks_get_by_page($PAGE); if (!isset($USER->editing)) { diff --git a/index.php b/index.php index 5f566c9109..23cff164c4 100644 --- a/index.php +++ b/index.php @@ -55,7 +55,7 @@ $langmenu = popup_form ($CFG->wwwroot .'/index.php?lang=', $langs, 'chooselang', $currlang, '', '', '', true); } - $PAGE = page_base::create_object(MOODLE_PAGE_COURSE, SITEID); + $PAGE = page_create_object(MOODLE_PAGE_COURSE, SITEID); print_header(strip_tags($site->fullname), $site->fullname, 'home', '', '', diff --git a/lib/blocklib.php b/lib/blocklib.php index 3d26dee807..2467b1cc85 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -845,14 +845,14 @@ function upgrade_blocks_plugins($continueto) { //Iterate over each course if ($courses = get_records('course')) { foreach ($courses as $course) { - $page = MoodlePage::create_object(MOODLE_PAGE_COURSE, $course->id); + $page = page_create_object(MOODLE_PAGE_COURSE, $course->id); blocks_repopulate_page($page); } } } if (!empty($CFG->siteblocksadded)) { /// This is a once-off hack to make a proper upgrade - $page = MoodlePage::create_object(MOODLE_PAGE_COURSE, SITEID); + $page = page_create_object(MOODLE_PAGE_COURSE, SITEID); blocks_repopulate_page($page); delete_records('config', 'name', 'siteblocksadded'); } diff --git a/lib/pagelib.php b/lib/pagelib.php index dab25f916b..e032d3815e 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -19,12 +19,60 @@ */ define('MOODLE_PAGE_COURSE', 'course'); +/** + * Factory function page_create_object(). Called with a pagetype identifier and possibly with + * its numeric ID. Returns a fully constructed page_base subclass you can work with. + */ + +function page_create_object($type, $id = NULL) { + $data = new stdClass; + $data->pagetype = $type; + $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 + error('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')'); + } + + $object->init_quick($data); + return $object; +} + +/** + * Function page_map_type() is the way for your code to define its own page subclasses and let Moodle recognize them. + * Use it to associate the textual identifier of your Page with the actual class name that has to be instantiated. + */ + +function page_map_class($type, $classname = NULL) { + static $mappings = array( + MOODLE_PAGE_COURSE => 'page_course' + ); + + if(!empty($type) && !empty($classname)) { + $mappings[$type] = $classname; + } + if(!isset($mappings[$type])) { + error('Page class mapping requested for unknown type: '.$type); + } + + if(!class_exists($mappings[$type])) { + error('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined'); + } + + return $mappings[$type]; +} + /** * Parent class from which all Moodle page classes derive * * @author Jon Papaioannou * @package pages - * @todo This parent class is very messy still. Please for the moment ignore it [except maybe create_object()] and move on to the derived class page_course to see the comments there. + * @todo This parent class is very messy still. Please for the moment ignore it and move on to the derived class page_course to see the comments there. */ class page_base { @@ -161,52 +209,6 @@ class page_base { function init_full() { $this->full_init_done = true; } - - // DO NOT TOUCH! NEVER! SECTION - - // Factory method page_base::create_object(). Called with a pagetype identifier and possibly with - // its numeric ID. Returns a fully constructed page_base subclass you can work with. - function create_object($type, $id = NULL) { - - $data = new stdClass; - $data->pagetype = $type; - $data->pageid = $id; - - $classname = page_base::map_page_type($type); - - $object = &new $classname; - // TODO: subclassing check here - - if($object->get_type() !== $type) { - // Somehow somewhere someone made a mistake - error('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')'); - } - - $object->init_quick($data); - return $object; - } - - // Method map_page_type() is the way for your code to define its own Page subclasses and let Moodle recognize them. - // Use it to associate the textual identifier of your Page with the actual class name that has to be instantiated. - function map_page_type($type, $classname = NULL) { - static $mappings = array( - MOODLE_PAGE_COURSE => 'page_course' - ); - - if(!empty($type) && !empty($classname)) { - $mappings[$type] = $classname; - } - if(!isset($mappings[$type])) { - error('Page class mapping requested for unknown type: '.$type); - } - - if(!class_exists($mappings[$type])) { - error('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined'); - } - - return $mappings[$type]; - } - } @@ -283,7 +285,7 @@ class page_course extends page_base { // SELF-REPORTING SECTION - // This is hardwired here so the factory method create_object() can be sure there was no mistake. + // 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 MOODLE_PAGE_COURSE;