]> git.mjollnir.org Git - moodle.git/commitdiff
blocklib: MDL-19010 fix new install.
authortjhunt <tjhunt>
Thu, 7 May 2009 07:05:22 +0000 (07:05 +0000)
committertjhunt <tjhunt>
Thu, 7 May 2009 07:05:22 +0000 (07:05 +0000)
* Fix the blocks bit of build_context_path
* Replace blocks_repopulate_page
* Make starting_output work during setup

lib/accesslib.php
lib/blocklib.php
lib/pagelib.php
lib/upgradelib.php

index d03b96f0097cc079b5bf0ee601036f8f4937db9f..abcf9e3d6359f022f1537b74e9d1040b3a7c24d9 100755 (executable)
@@ -5820,35 +5820,6 @@ function build_context_path($force=false) {
     $DB->execute($updatesql);
     $DB->delete_records('context_temp');
 
-    // Blocks - non-pinned course-view only
-    $sql = "INSERT INTO {context_temp} (id, path, depth)
-            SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1
-              FROM {context} ctx
-              JOIN {block_instance_old} bi ON ctx.instanceid = bi.oldid
-              JOIN {context} pctx ON bi.pageid=pctx.instanceid
-             WHERE ctx.contextlevel=".CONTEXT_BLOCK."
-                   AND pctx.contextlevel=".CONTEXT_COURSE."
-                   AND bi.pagetype='course-view'
-                       AND NOT EXISTS (SELECT 'x'
-                                       FROM {context_temp} temp
-                                       WHERE temp.id = ctx.id)
-                   $ctxemptyclause";
-    $DB->execute($sql);
-
-    $DB->execute($updatesql);
-    $DB->delete_records('context_temp');
-
-    // Blocks - others
-    $sql = "UPDATE {context}
-               SET depth=2, path=".$DB->sql_concat("'$base/'", 'id')."
-             WHERE contextlevel=".CONTEXT_BLOCK."
-                   AND EXISTS (SELECT 'x'
-                                 FROM {block_instance_old} bi
-                                WHERE bi.oldid = {context}.instanceid
-                                      AND bi.pagetype!='course-view')
-                   $emptyclause ";
-    $DB->execute($sql);
-
     // User
     $sql = "UPDATE {context}
                SET depth=2, path=".$DB->sql_concat("'$base/'", 'id')."
@@ -5859,6 +5830,22 @@ function build_context_path($force=false) {
                    $emptyclause ";
     $DB->execute($sql);
 
+    // Blocks
+    $sql = "INSERT INTO {context_temp} (id, path, depth)
+            SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1
+              FROM {context} ctx
+              JOIN {block_instances} bi ON ctx.instanceid = bi.id
+              JOIN {context} pctx ON bi.contextid = pctx.id
+             WHERE ctx.contextlevel=".CONTEXT_BLOCK."
+                   AND NOT EXISTS (SELECT 'x'
+                                   FROM {context_temp} temp
+                                   WHERE temp.id = ctx.id)
+                   $ctxemptyclause";
+    $DB->execute($sql);
+
+    $DB->execute($updatesql);
+    $DB->delete_records('context_temp');
+
     // reset static course cache - it might have incorrect cached data
     global $ACCESSLIB_PRIVATE;
     $ACCESSLIB_PRIVATE->contexts = array();
index a873e4ac099024d5e2ec54f9675ea8ced590d45c..75908b0757c06f679731a0c68c9b267677c85d31 100644 (file)
@@ -364,6 +364,23 @@ class block_manager implements ArrayAccess {
         $DB->insert_record('block_instances', $blockinstance);
     }
 
+    /**
+     * Convenience method, calls add_block repeatedly for all the blocks in $blocks.
+     * @param array $blocks array with arrray keys the region names, and values an array of block names.
+     * @param string $pagetypepattern optional. Passed to @see add_block()
+     * @param string $subpagepattern optional. Passed to @see add_block()
+     */
+    public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL) {
+        $this->add_regions(array_keys($blocks));
+        foreach ($blocks as $region => $regionblocks) {
+            $weight = 0;
+            foreach ($regionblocks as $blockname) {
+                $this->add_block($blockname, $region, $weight, false, $pagetypepattern, $subpagepattern);
+                $weight += 1;
+            }
+        }
+    }
+
 /// Inner workings =============================================================
 
     /**
@@ -1302,6 +1319,7 @@ function blocks_print_adminblock($page, $blockmanager) {
 }
 
 /**
+ * @deprecated since 2.0
  * Delete all the blocks from a particular page.
  *
  * @param string $pagetype the page type.
@@ -1310,14 +1328,17 @@ function blocks_print_adminblock($page, $blockmanager) {
  */
 function blocks_delete_all_on_page($pagetype, $pageid) {
     global $DB;
-    if ($instances = $DB->get_records('block_instance_old', array('pageid' => $pageid, 'pagetype' => $pagetype))) {
-        foreach ($instances as $instance) {
-            delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here.
-        }
-    }
-    return $DB->delete_records('block_instance_old', array('pageid' => $pageid, 'pagetype' => $pagetype));
+
+    debugging('Call to deprecated function blocks_repopulate_page. ' .
+            'This function cannot work any more. Doing nothing. ' .
+            'Please update your code to use another method.', DEBUG_DEVELOPER);
+    return false;
 }
 
+/**
+ * Delete all the blocks that belong to a particular context.
+ * @param $contextid the context id.
+ */
 function blocks_delete_all_for_context($contextid) {
     global $DB;
     $instances = $DB->get_recordset('block_instances', array('contextid' => $contextid));
@@ -1329,66 +1350,110 @@ function blocks_delete_all_for_context($contextid) {
     $DB->delete_records('block_positions', array('contextid' => $contextid));
 }
 
-// Dispite what this function is called, it seems to be mostly used to populate
-// the default blocks when a new course (or whatever) is created.
-function blocks_repopulate_page($page) {
-    global $CFG, $DB;
+/**
+ * Parse a list of default blocks. See config-dist for a description of the format.
+ * @param string $blocksstr
+ * @return array
+ */
+function blocks_parse_default_blocks_list($blocksstr) {
+    list($left, $right) = explode(':', $blocksstr);
+    return array(
+        BLOCK_POS_LEFT => explode(',', $left),
+        BLOCK_POS_RIGHT => explode(',', $right),
+    );
+}
 
-    $allblocks = blocks_get_record();
+/**
+ * @return array the blocks that should be added to the site course by default.
+ */
+function blocks_get_default_site_course_blocks() {
+    global $CFG;
 
-    if(empty($allblocks)) {
-        print_error('cannotgetblock');
+    if (!empty($CFG->defaultblocks_site)) {
+        blocks_parse_default_blocks_list($CFG->defaultblocks_site);
+    } else {
+        $blocknames = array(
+            BLOCK_POS_LEFT => array('site_main_menu', 'admin_tree'),
+            BLOCK_POS_RIGHT => array('course_summary', 'calendar_month')
+        );
     }
+}
+
+/**
+ * Add the default blocks to a course.
+ * @param object $course a course object.
+ */
+function blocks_add_default_course_blocks($course) {
+    global $CFG;
+
+    if (!empty($CFG->defaultblocks_override)) {
+        $blocknames = blocks_parse_default_blocks_list($CFG->defaultblocks_override);
+
+    } else if ($course->id == SITEID) {
+        $blocknames = blocks_get_default_site_course_blocks();
+
+    } else {
+        $defaultblocks = 'defaultblocks_' . $course->format;
+        if (!empty($CFG->$defaultblocks)) {
+            $blocknames = blocks_parse_default_blocks_list($CFG->$defaultblocks);
+
+        } else {
+            $formatconfig = $CFG->dirroot.'/course/format/'.$pageformat.'/config.php';
+            if (file_exists_and_readable($formatconfig)) {
+                require($formatconfig);
+            }
+            if (!empty($format['defaultblocks'])) {
+                $blocknames = blocks_parse_default_blocks_list($format['defaultblocks']);
 
-    // Assemble the information to correlate block names to ids
-    $idforname = array();
-    foreach($allblocks as $block) {
-        $idforname[$block->name] = $block->id;
+            } else if (!empty($CFG->defaultblocks)){
+                $blocknames = blocks_parse_default_blocks_list($CFG->defaultblocks);
+
+            } else {
+                $blocknames = array(
+                    BLOCK_POS_LEFT => array('participants', 'activity_modules', 'search_forums', 'admin', 'course_list'),
+                    BLOCK_POS_RIGHT => array('news_items', 'calendar_upcoming', 'recent_activity')
+                );
+            }
+        }
     }
 
+    $page = new moodle_page();
+    $page->set_course($course);
+    $page->blocks->add_blocks(array($blocknames), 'course-view-*');
+}
+
+/**
+ * Add the default system-context blocks. E.g. the admin tree.
+ */
+function blocks_add_default_system_blocks() {
+    $page = new moodle_page();
+    $page->set_context(get_context_instance(CONTEXT_SYSTEM));
+    $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_tree', 'admin_bookmarks')), 'admin-*');
+}
+
+/**
+ * @deprecated since 2.0
+ * Dispite what this function is called, it seems to be mostly used to populate
+ * the default blocks when a new course (or whatever) is created.
+ * @param object $page the page to add default blocks to.
+ * @return boolean success or failure.
+ */
+function blocks_repopulate_page($page) {
+    global $CFG;
+
+    debugging('Call to deprecated function blocks_repopulate_page. ' .
+            'Use a more specific method like blocks_add_default_course_blocks, ' .
+            'or just call $PAGE->blocks->add_blocks()', DEBUG_DEVELOPER);
+
     /// If the site override has been defined, it is the only valid one.
     if (!empty($CFG->defaultblocks_override)) {
         $blocknames = $CFG->defaultblocks_override;
-    }
-    else {
+    } else {
         $blocknames = $page->blocks_get_default();
     }
 
-    $regions = $page->blocks->get_regions();
-    $posblocks = explode(':', $blocknames);
-
-    // Now one array holds the names of the positions, and the other one holds the blocks
-    // that are going to go in each position. Luckily for us, both arrays are numerically
-    // 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->pagetype, $page->get_id());
-
-    // Here we slyly count $posblocks and NOT $regions. This can actually make a difference
-    // if the textual representation has undefined slots in the end. So we only work with as many
-    // positions were retrieved, not with all the page says it has available.
-    $numpositions = count($posblocks);
-    for($i = 0; $i < $numpositions; ++$i) {
-        $region = $regions[$i];
-        $blocknames = explode(',', $posblocks[$i]);
-        $weight = 0;
-        foreach($blocknames as $blockname) {
-            $newinstance = new stdClass;
-            $newinstance->blockid    = $idforname[$blockname];
-            $newinstance->pageid     = $page->get_id();
-            $newinstance->pagetype   = $page->pagetype;
-            $newinstance->position   = $region;
-            $newinstance->weight     = $weight;
-            $newinstance->visible    = 1;
-            $newinstance->configdata = '';
-
-            if(!empty($newinstance->blockid)) {
-                // Only add block if it was recognized
-                $DB->insert_record('block_instance_old', $newinstance);
-                ++$weight;
-            }
-        }
-    }
+    $blocks = blocks_parse_default_blocks_list($blocknames);
+    $page->blocks->add_blocks($blocks);
 
     return true;
 }
index 1634abe22007f641f105077a4c14b39e204cd77a..a6690df4b2fa5a0b337659ad4cac9fd46ee28cfb 100644 (file)
@@ -573,8 +573,17 @@ class moodle_page {
      * state. This is our last change to initialise things.
      */
     protected function starting_output() {
+        global $SITE, $CFG;
+
+        if (empty($CFG->rolesactive)) {
+            $this->_course = new stdClass;
+            $this->_course->id = 1;
+            moodle_setlocale();
+            theme_setup();
+            return;
+        }
+
         if (!$this->_course) {
-            global $SITE;
             $this->set_course($SITE);
         }
 
@@ -1037,57 +1046,13 @@ class page_base extends moodle_page {
 
 /**
  * @deprecated since Moodle 2.0
- * Class that models the behavior of a moodle course
+ * Class that models the behavior of a moodle course.
+ * Although this does nothing, this class declaration should be left for now
+ * since there may be legacy class doing class page_... extends page_course
  *
  * @package pages
  */
 class page_course extends page_base {
-    // SELF-REPORTING SECTION
-
-    // When we are creating a new page, use the data at your disposal to provide a textual representation of the
-    // blocks that are going to get added to this new page. Delimit block names with commas (,) and use double
-    // colons (:) to delimit between block positions in the page.
-    function _legacy_blocks_get_default() {
-        global $CFG;
-
-        $this->init_full();
-
-        if($this->id == SITEID) {
-        // Is it the site?
-            if (!empty($CFG->defaultblocks_site)) {
-                $blocknames = $CFG->defaultblocks_site;
-            }
-            /// Failsafe - in case nothing was defined.
-            else {
-                $blocknames = 'site_main_menu,admin_tree:course_summary,calendar_month';
-            }
-        }
-        // It's a normal course, so do it according to the course format
-        else {
-            $pageformat = $this->course->format;
-            if (!empty($CFG->{'defaultblocks_'. $pageformat})) {
-                $blocknames = $CFG->{'defaultblocks_'. $pageformat};
-            }
-            else {
-                $format_config = $CFG->dirroot.'/course/format/'.$pageformat.'/config.php';
-                if (@is_file($format_config) && is_readable($format_config)) {
-                    require($format_config);
-                }
-                if (!empty($format['defaultblocks'])) {
-                    $blocknames = $format['defaultblocks'];
-                }
-                else if (!empty($CFG->defaultblocks)){
-                    $blocknames = $CFG->defaultblocks;
-                }
-                /// Failsafe - in case nothing was defined.
-                else {
-                    $blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
-                }
-            }
-        }
-
-        return $blocknames;
-    }
 }
 
 /**
@@ -1097,8 +1062,8 @@ class page_course extends page_base {
  * @package pages
  */
 class page_generic_activity extends page_base {
-    // Although this function is deprecated, it should be left here becuase people
-    // upgrading legacy code need to copy it. See
+    // Although this function is deprecated, it should be left here because
+    // people upgrading legacy code need to copy it. See
     // http://docs.moodle.org/en/Development:Migrating_your_code_code_to_the_2.0_rendering_API
     function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
         global $USER, $CFG;
index bde8d91fa3f8a5f9991ecd33118ebbe15c598439..457c478a3c765b5adb9cd3c5e1268b403588bd4d 100644 (file)
@@ -568,14 +568,11 @@ function upgrade_plugins_blocks($startcallback, $endcallback) {
         //Iterate over each course - there should be only site course here now
         if ($courses = $DB->get_records('course')) {
             foreach ($courses as $course) {
-                $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
-                blocks_repopulate_page($page);
+                blocks_add_default_course_blocks($course);
             }
         }
 
-        $page = new moodle_page();
-        $page->set_pagetype('admin');
-        blocks_repopulate_page($page);
+        blocks_add_default_system_blocks();
     }
 }