]> git.mjollnir.org Git - moodle.git/commitdiff
blocks: MDL-19010 Fix further blocks-related breakage, including removing all referen...
authortjhunt <tjhunt>
Thu, 7 May 2009 08:55:10 +0000 (08:55 +0000)
committertjhunt <tjhunt>
Thu, 7 May 2009 08:55:10 +0000 (08:55 +0000)
16 files changed:
admin/blocks.php
admin/replace.php
backup/backuplib.php
blocks/moodleblock.class.php
blocks/online_users/block_online_users.php
blocks/rss_client/block_rss_client.php
blocks/rss_client/config_instance.html
blocks/rss_client/config_instance_tabs.php
blog/lib.php
course/delete.php
course/index.php
course/search.php
lib/accesslib.php
lib/blocklib.php
lib/moodlelib.php
lib/pagelib.php

index 196d8382a02c376d56a7d43ce099b2e9c912d407..aa5a0c3078fe7244e3c7215a6700543e5c7d3959 100644 (file)
@@ -82,7 +82,7 @@
             }
 
             // First delete instances and then block
-            $instances = $DB->get_records('block_instance_old', array('blockid'=>$block->id));
+            $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
             if(!empty($instances)) {
                 foreach($instances as $instance) {
                     blocks_delete_instance($instance);
         // MDL-11167, blocks can be placed on mymoodle, or the blogs page
         // and it should not show up on course search page
 
-        $totalcount = $DB->count_records('block_instance_old', array('blockid'=>$blockid));
-        $count      = $DB->count_records('block_instance_old', array('blockid'=>$blockid, 'pagetype'=>'course-view'));
+        $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
+        $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
 
         if ($count>0) {
             $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
index 2ee8f6f6e9a769154a7e7f4dc4ddad92dceafbec..5f72748671baebe44b223b91c6f37b0cf9691ca6 100644 (file)
@@ -42,17 +42,13 @@ print_simple_box_end();
 
 /// Try to replace some well-known serialised contents (html blocks)
 notify('Replacing in html blocks...');
-$sql = "SELECT bi.*
-          FROM {block_instance_old} bi
-          JOIN {block} b ON b.id = bi.blockid
-         WHERE b.name = 'html'";
-if ($instances = $DB->get_records_sql($sql)) {
-    foreach ($instances as $instance) {
-        $blockobject = block_instance('html', $instance);
-        $blockobject->config->text = str_replace($search, $replace, $blockobject->config->text);
-        $blockobject->instance_config_commit($blockobject->pinned);
-    }
+$instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
+foreach ($instances as $instance) {
+    $blockobject = block_instance('html', $instance);
+    $blockobject->config->text = str_replace($search, $replace, $blockobject->config->text);
+    $blockobject->instance_config_commit($blockobject->pinned);
 }
+$instances->close();
 
 /// Rebuild course cache which might be incorrect now
 notify('Rebuilding course cache...', 'notifysuccess');
index fcc8f9654fd001a846c5eeb0c3db4bc0473ffdb9..a5ce442bbafc8f93eab338fb6cca138f60735ae0 100644 (file)
         }
 
         // add all roles assigned at block context
-        if ($courseblocks = $DB->get_records_sql("SELECT *
-                                                    FROM {block_instance_old}
-                                                   WHERE pagetype = '".PAGE_COURSE_VIEW."'
-                                                         AND pageid = ?", array($preferences->backup_course))) {
+        if ($courseblocks = $DB->get_records_sql("SELECT * FROM {block_instances} WHERE contextid = ?",
+                array($coursecontext->id))) {
 
             foreach ($courseblocks as $courseblock) {
 
index c987b183c0f3a353be6ba33b8f653ca92121dd01..41b8d6b482e9051c1d13a4af13673c1e62ac14bf 100644 (file)
@@ -3,8 +3,6 @@
 /**
  * This file contains the parent class for moodle blocks, block_base.
  *
- * @author Jon Papaioannou
- * @version  $Id$
  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  * @package blocks
  */
@@ -303,8 +301,9 @@ class block_base {
      */
     function is_empty() {
 
-        // TODO
-        if (empty($this->instance->pinned)) {
+        // TODO - temporary hack to get the block context only if it already exists.
+        global $DB;
+        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
             $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
             $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
@@ -408,8 +407,9 @@ class block_base {
     function _add_edit_controls($options) {
         global $CFG, $USER;
 
-        // TODO
-        if (empty($this->instance->pinned)) {
+        // TODO - temporary hack to get the block context only if it already exists.
+        global $DB;
+        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
             $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
             $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
@@ -709,21 +709,11 @@ class block_base {
 
     /**
      * Serialize and store config data
-     * @return boolean
-     * @todo finish documenting this function
      */
-    function instance_config_save($data,$pinned=false) {
+    function instance_config_save($data, $nolongerused = false) {
         global $DB;
-
-        $data = $data;
-        $this->config = $data;
-        $table = 'block_instance_old';
-        $field = 'oldid';
-        if (!empty($pinned)) {
-            $table = 'block_pinned_old';
-            $field = 'id';
-        }
-        return $DB->set_field($table, 'configdata', base64_encode(serialize($data)), array($field => $this->instance->id));
+        $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
+                array($field => $this->instance->id));
     }
 
     /**
@@ -731,16 +721,9 @@ class block_base {
      * @return boolean
      * @todo finish documenting this function
      */
-    function instance_config_commit($pinned=false) {
+    function instance_config_commit($nolongerused = false) {
         global $DB;
-
-        $table = 'block_instance_old';
-        $field = 'oldid';
-        if (!empty($pinned)) {
-            $table = 'block_pinned_old';
-            $field = 'id';
-        }
-        return $DB->set_field($table, 'configdata', base64_encode(serialize($this->config)), array($field => $this->instance->id));
+        $this->instance_config_save($this->config);
     }
 
      /**
@@ -801,8 +784,9 @@ class block_list extends block_base {
 
     function is_empty() {
 
-        // TODO
-        if (empty($this->instance->pinned)) {
+        // TODO - temporary hack to get the block context only if it already exists.
+        global $DB;
+        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
             $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
             $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
index 4f16bfe775e0f706065654cd8916742d50dee057..229f7b004d1d392cd8798862d433b02d5f216f97 100644 (file)
@@ -37,11 +37,12 @@ class block_online_users extends block_base {
         // Get context so we can check capabilities.
         $context = $this->page->context;
 
-        // TODO
-        if (empty($this->instance->pinned)) {
-            $blockcontext = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
+        // TODO - temporary hack to get the block context only if it already exists.
+        global $DB;
+        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
+            $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
-            $blockcontext = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
+            $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
         }
 
         //Calculate if we are in separate groups
index b264ea32f5adedbdced92ffb0cdf27c3820cbfd7..4bec6598d2bf8dbbfc5ce55912ab2740e39a2357 100644 (file)
@@ -96,8 +96,9 @@
             }
         }
 
-        // TODO
-        if (empty($this->instance->pinned)) {
+        // TODO - temporary hack to get the block context only if it already exists.
+        global $DB;
+        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
             $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
             $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
index 3afe111524503e3bcff5919721764b44a4ca2a82..2fc69e1aadcb444dadff63ac703c505b47a353a8 100644 (file)
@@ -93,8 +93,9 @@ print_box_start();
                     print $checkbox . $feedtitle .'<br />'."\n";
                 }
             } else {
-                // TODO
-                if (empty($this->instance->pinned)) {
+                // TODO - temporary hack to get the block context only if it already exists.
+                global $DB;
+                if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
                     $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
                 } else {
                     $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
@@ -174,8 +175,9 @@ print_box_start();
 } else {
   global $act, $url, $rssid, $preferredtitle, $shared;
   print '</div></form></div>';   // Closes off page form
-  // TODO
-  if (empty($this->instance->pinned)) {
+  // TODO - temporary hack to get the block context only if it already exists.
+  global $DB;
+  if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
     $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
   } else {
     $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
index a71be2a9ca31b6e516cab7384f09bd396216e489..a577497a1505478ba97e5cada31fc5cbc7e91f1c 100644 (file)
@@ -5,12 +5,13 @@
 global $USER;
 $tabs = $row = array();
 
-// TODO
-if (empty($this->instance->pinned)) {
-    $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
-} else {
-    $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
-}
+    // TODO - temporary hack to get the block context only if it already exists.
+    global $DB;
+    if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
+        $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
+    } else {
+        $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
+    }
 
 if (has_capability('moodle/site:manageblocks', $context)) {
     $script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id, 'section' => 'rss'));
index 5c31eefa05460e7fdabe1feacfb429e15d2a76fe..5272bdd17c7b2edde562d826a1a680600af38294 100755 (executable)
                 $tagsblock = $DB->get_record('block', array('name'=>'blog_tags'));
                 // add those 2 into block_instance page
 
-                // add blog_menu block
-                $newblock = new object();
-                $newblock->blockid  = $menublock->id;
-                $newblock->pageid   = $USER->id;
-                $newblock->pagetype = 'blog-view';
-                $newblock->position = 'r';
-                $newblock->weight   = 0;
-                $newblock->visible  = 1;
-                $DB->insert_record('block_instance_old', $newblock);
-
-                // add blog_tags menu
-                $newblock -> blockid = $tagsblock->id;
-                $newblock -> weight  = 1;
-                $DB->insert_record('block_instance_old', $newblock);
+// Commmented out since the block changes broke it. Hopefully nico will fix it ;-)
+//                // add blog_menu block
+//                $newblock = new object();
+//                $newblock->blockid  = $menublock->id;
+//                $newblock->pageid   = $USER->id;
+//                $newblock->pagetype = 'blog-view';
+//                $newblock->position = 'r';
+//                $newblock->weight   = 0;
+//                $newblock->visible  = 1;
+//                $DB->insert_record('block_instances', $newblock);
+//
+//                // add blog_tags menu
+//                $newblock -> blockid = $tagsblock->id;
+//                $newblock -> weight  = 1;
+//                $DB->insert_record('block_instances', $newblock);
 
                 // finally we set the page size pref
                 set_user_preference('blogpagesize', 10);
index 480068a79db8f1de5441970efbf5a7325e3c5988..a96795cf5251a4b8e1e5dd99c8b7f1c721039d70 100644 (file)
@@ -1,7 +1,8 @@
 <?php // $Id$
       // Admin-only code to delete a course utterly
 
-    require_once("../config.php");
+    require_once(dirname(__FILE__) . '/../config.php');
+    require_once($CFG->dirroot . '/course/lib.php');
 
     $id     = required_param('id', PARAM_INT);              // course id
     $delete = optional_param('delete', '', PARAM_ALPHANUM); // delete confirmation hash
index 5ad01563455feeb7a750426746b5a2de2eb5733f..df064a7253bb66e9c295739b6feb2f8912eb6dfa 100644 (file)
@@ -24,6 +24,9 @@
 
     $systemcontext = get_context_instance(CONTEXT_SYSTEM);
 
+    $PAGE->set_url('course/index.php');
+    $PAGE->set_context($systemcontext);
+
     if (update_category_button()) {
         if ($categoryedit !== -1) {
             $USER->editing = $categoryedit;
index 99e149a89034a1c0bdb1fa26092388e9f311ee9c..780c7897a33affda4f004642f360464023676e4c 100644 (file)
 
     // get list of courses containing blocks if required
     if (!empty($blocklist) and confirm_sesskey()) {
-        $blockid = $blocklist;
-        if (!$blocks = $DB->get_records('block_instance_old', array('blockid'=>$blockid))) {
-            print_error('blockcannotread', '', '',  $blockid);
-        }
-
-        // run through blocks and get (unique) courses
+        $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
+        $courses = $DB->get_recordset_sql("
+                SELECT * FROM {course} WHERE id IN (
+                    SELECT DISTINCT ctx.instanceid
+                    FROM {context} ctx
+                    JOIN {block_instances} bi ON bi.contextid = ctx.id
+                    WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
+                array($blockname));
         $courses = array();
-        foreach ($blocks as $block) {
-            $courseid = $block->pageid;
-            // MDL-11167, blocks can be placed on mymoodle, or the blogs page
-            // and it should not show up on course search page
-            if ($courseid==0 || $block->pagetype != 'course-view') {
-                continue;
-            }
-            if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
-                print_error('invalidcourseid', '', '', $courseid);
-            }
-            $courses[$courseid] = $course;
+        foreach ($courses as $course) {
+            $courses[$course->id] = $course;
         }
-        $totalcount = count( $courses );
+        $totalcount = count($courses);
     }
     // get list of courses containing modules if required
     elseif (!empty($modulelist) and confirm_sesskey()) {
index abcf9e3d6359f022f1537b74e9d1040b3a7c24d9..01afc631f7210e048f722f69b8febe83ce674da1 100755 (executable)
@@ -2091,28 +2091,16 @@ function create_context($contextlevel, $instanceid) {
         case CONTEXT_BLOCK:
             // Only non-pinned & course-page based
             $sql = "SELECT ctx.path, ctx.depth
-                      FROM {context}        ctx
-                      JOIN {block_instance_old} bi
-                           ON (bi.pageid=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
-                     WHERE bi.oldid=? AND bi.pagetype='course-view'";
-            $params = array($instanceid);
+                      FROM {context} ctx
+                      JOIN {block_instances} bi ON (bi.contextid=ctx.id)
+                     WHERE bi.id=? AND ctx.contextlevel=?";
+            $params = array($instanceid, CONTEXT_COURSE);
             if ($p = $DB->get_record_sql($sql, $params)) {
                 $basepath  = $p->path;
                 $basedepth = $p->depth;
-            } else if ($bi = $DB->get_record('block_instance_old', array('oldid'=>$instanceid))) {
-                if ($bi->pagetype != 'course-view') {
-                    // ok - not a course block
-                } else if ($parent = get_context_instance(CONTEXT_COURSE, $bi->pageid)) {
-                    $basepath  = $parent->path;
-                    $basedepth = $parent->depth;
-                } else {
-                    // parent course does not exist - course blocks can not exist without a course
-                    $error_message = 'parent course does not exist - course blocks can not exist without a course';
-                    $result = false;
-                }
             } else {
                 // block does not exist
-                $error_message = 'block does not exist';
+                $error_message = 'block or parent context does not exist';
                 $result = false;
             }
             break;
@@ -2294,8 +2282,8 @@ function create_contexts($contextlevel=null, $buildpaths=true) {
 
     if (empty($contextlevel) or $contextlevel == CONTEXT_BLOCK) {
         $sql = "INSERT INTO {context} (contextlevel, instanceid)
-                SELECT ".CONTEXT_BLOCK.", bi.oldid
-                  FROM {block_instance_old} bi
+                SELECT ".CONTEXT_BLOCK.", bi.id
+                  FROM {block_instances} bi
                  WHERE NOT EXISTS (SELECT 'x'
                                      FROM {context} cx
                                     WHERE bi.id = cx.instanceid AND cx.contextlevel=".CONTEXT_BLOCK.")";
@@ -2358,8 +2346,8 @@ function cleanup_contexts() {
               SELECT c.contextlevel,
                      c.instanceid
                 FROM {context} c
-                LEFT OUTER JOIN {block_instance_old} t
-                     ON c.instanceid = t.oldid
+                LEFT OUTER JOIN {block_instances} t
+                     ON c.instanceid = t.id
                WHERE t.id IS NULL AND c.contextlevel = ".CONTEXT_BLOCK."
            ";
     if ($rs = $DB->get_recordset_sql($sql)) {
@@ -2407,9 +2395,10 @@ function preload_course_contexts($courseid) {
          UNION ALL
 
             SELECT x.instanceid, x.id, x.contextlevel, x.path, x.depth
-              FROM {block_instance_old} bi
-              JOIN {context} x ON x.instanceid=bi.oldid
-             WHERE bi.pageid=? AND bi.pagetype='course-view'
+              FROM {context} px
+              JOIN {block_instances} bi ON bi.contextid = px.id
+              JOIN {context} x ON x.instanceid=bi.id
+              WHERE px.instanceid = ? AND px.contextlevel = ".CONTEXT_COURSE."
                    AND x.contextlevel=".CONTEXT_BLOCK."
 
          UNION ALL
@@ -3426,18 +3415,16 @@ function print_context_name($context, $withprefix = true, $short = false) {
             break;
 
         case CONTEXT_BLOCK: // not necessarily 1 to 1 to course
-            if ($blockinstance = $DB->get_record('block_instance_old', array('oldid'=>$context->instanceid))) {
-                if ($block = $DB->get_record('block', array('id'=>$blockinstance->blockid))) {
-                    global $CFG;
-                    require_once("$CFG->dirroot/blocks/moodleblock.class.php");
-                    require_once("$CFG->dirroot/blocks/$block->name/block_$block->name.php");
-                    $blockname = "block_$block->name";
-                    if ($blockobject = new $blockname()) {
-                        if ($withprefix){
-                            $name = get_string('block').': ';
-                        }
-                        $name .= $blockobject->title;
+            if ($blockinstance = $DB->get_record('block_instances', array('id'=>$context->instanceid))) {
+                global $CFG;
+                require_once("$CFG->dirroot/blocks/moodleblock.class.php");
+                require_once("$CFG->dirroot/blocks/$blockinstance->blockname/block_$blockinstance->blockname.php");
+                $blockname = "block_$blockinstance->blockname";
+                if ($blockobject = new $blockname()) {
+                    if ($withprefix){
+                        $name = get_string('block').': ';
                     }
+                    $name .= $blockobject->title;
                 }
             }
             break;
@@ -3607,15 +3594,13 @@ function fetch_context_capabilities($context) {
         break;
 
         case CONTEXT_BLOCK: // block caps
-            $cb = $DB->get_record('block_instance_old', array('oldid'=>$context->instanceid));
-            $block = $DB->get_record('block', array('id'=>$cb->blockid));
-
-            $extra = "";
-            if ($blockinstance = block_instance($block->name)) {
-                if ($extracaps = $blockinstance->get_extra_capabilities()) {
-                    list($extra, $params) = $DB->get_in_or_equal($extracaps, SQL_PARAMS_NAMED, 'cap0');
-                    $extra = "OR name $extra";
-                }
+            $cb = $DB->get_record('block_instances', array('id'=>$context->instanceid));
+
+            $extra = '';
+            $extracaps = block_method_result($cb->blockname, 'get_extra_capabilities');
+            if ($extracaps) {
+                list($extra, $params) = $DB->get_in_or_equal($extracaps, SQL_PARAMS_NAMED, 'cap0');
+                $extra = "OR name $extra";
             }
 
             $SQL = "SELECT *
@@ -3750,13 +3735,13 @@ function get_sorted_contexts($select, $params = array()) {
     return $DB->get_records_sql("
             SELECT ctx.*
             FROM {context} ctx
-            LEFT JOIN {user} u ON ctx.contextlevel = 30 AND u.id = ctx.instanceid
-            LEFT JOIN {course_categories} cat ON ctx.contextlevel = 40 AND cat.id = ctx.instanceid
-            LEFT JOIN {course} c ON ctx.contextlevel = 50 AND c.id = ctx.instanceid
-            LEFT JOIN {course_modules} cm ON ctx.contextlevel = 70 AND cm.id = ctx.instanceid
-            LEFT JOIN {block_instance_old} bi ON ctx.contextlevel = 80 AND bi.oldid = ctx.instanceid
+            LEFT JOIN {user} u ON ctx.contextlevel = " . CONTEXT_USER . " AND u.id = ctx.instanceid
+            LEFT JOIN {course_categories} cat ON ctx.contextlevel = " . CONTEXT_COURSECAT . " AND cat.id = ctx.instanceid
+            LEFT JOIN {course} c ON ctx.contextlevel = " . CONTEXT_COURSE . " AND c.id = ctx.instanceid
+            LEFT JOIN {course_modules} cm ON ctx.contextlevel = " . CONTEXT_MODULE . " AND cm.id = ctx.instanceid
+            LEFT JOIN {block_instances} bi ON ctx.contextlevel = " . CONTEXT_BLOCK . " AND bi.id = ctx.instanceid
             $select
-            ORDER BY ctx.contextlevel, bi.position, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.weight), u.lastname, u.firstname, cm.id
+            ORDER BY ctx.contextlevel, bi.region, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.weight), u.lastname, u.firstname, cm.id
             ", $params);
 }
 
index 75908b0757c06f679731a0c68c9b267677c85d31..b74752ef7bb31d1d217cf8fef8c6384cfd27e19c 100644 (file)
@@ -1335,6 +1335,23 @@ function blocks_delete_all_on_page($pagetype, $pageid) {
     return false;
 }
 
+/**
+ * Delete a block, and associated data.
+ * @param object $instance a row from the block_instances table
+ * @param $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
+ */
+function blocks_delete_block($instance, $skipblockstables = false) {
+    if ($block = block_instance($block->blockname, $instance)) {
+        $block->instance_delete();
+    }
+    delete_context(CONTEXT_BLOCK, $instance->id);
+
+    if (!$skipblockstables) {
+        $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
+        $DB->delete_records('block_instances', array('id' => $instance->id));
+    }
+}
+
 /**
  * Delete all the blocks that belong to a particular context.
  * @param $contextid the context id.
@@ -1343,7 +1360,7 @@ function blocks_delete_all_for_context($contextid) {
     global $DB;
     $instances = $DB->get_recordset('block_instances', array('contextid' => $contextid));
     foreach ($instances as $instance) {
-        delete_context(CONTEXT_BLOCK, $instance->id);
+        blocks_delete_block($instance, true);
     }
     $instances->close();
     $DB->delete_records('block_instances', array('contextid' => $contextid));
@@ -1356,11 +1373,15 @@ function blocks_delete_all_for_context($contextid) {
  * @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),
-    );
+    $blocks = array();
+    $bits = explode(':', $blocksstr);
+    if (!empty($bits)) {
+        $blocks[BLOCK_POS_LEFT] = explode(',', array_shift($bits));
+    }
+    if (!empty($bits)) {
+        $blocks[BLOCK_POS_RIGHT] = explode(',', array_shift($bits));
+    }
+    return $blocks;
 }
 
 /**
@@ -1370,9 +1391,9 @@ function blocks_get_default_site_course_blocks() {
     global $CFG;
 
     if (!empty($CFG->defaultblocks_site)) {
-        blocks_parse_default_blocks_list($CFG->defaultblocks_site);
+        return blocks_parse_default_blocks_list($CFG->defaultblocks_site);
     } else {
-        $blocknames = array(
+        return array(
             BLOCK_POS_LEFT => array('site_main_menu', 'admin_tree'),
             BLOCK_POS_RIGHT => array('course_summary', 'calendar_month')
         );
@@ -1417,9 +1438,18 @@ function blocks_add_default_course_blocks($course) {
         }
     }
 
+    if ($course->id == SITEID) {
+        $pagetypepattern = 'site-index';
+    } else {
+        $pagetypepattern = 'course-view-*';
+    }
+
     $page = new moodle_page();
     $page->set_course($course);
-    $page->blocks->add_blocks(array($blocknames), 'course-view-*');
+    print_object($page); // DONOTCOMMIT
+    print_object($pagetypepattern); // DONOTCOMMIT
+    print_object($blocknames); // DONOTCOMMIT
+    $page->blocks->add_blocks($blocknames, $pagetypepattern);
 }
 
 /**
index 872124f58fe18b5b2258add29a5149378c5d7aea..0adfb7ed3d885d68fbf2486cc0c392125e0ef8e8 100644 (file)
@@ -3528,6 +3528,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
     if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
         print_error('invalidcourseid');
     }
+    $context = get_context_instance(CONTEXT_COURSE, $courseid);
 
     $strdeleted = get_string('deleted');
 
@@ -3601,40 +3602,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
     notify_local_delete_course($courseid, $showfeedback);
 
 /// Delete course blocks
-
-    if ($blocks = $DB->get_records_sql("SELECT *
-                                          FROM {block_instance_old}
-                                         WHERE pagetype = '".PAGE_COURSE_VIEW."'
-                                               AND pageid = ?", array($course->id))) {
-        if ($DB->delete_records('block_instance_old', array('pagetype'=>PAGE_COURSE_VIEW, 'pageid'=>$course->id))) {
-            if ($showfeedback) {
-                notify($strdeleted .' block_instance_old');
-            }
-
-            foreach ($blocks as $block) {  /// Delete any associated contexts for this block
-
-                delete_context(CONTEXT_BLOCK, $block->id);
-
-                // fix for MDL-7164
-                // Get the block object and call instance_delete()
-                if (!$record = blocks_get_record($block->blockid)) {
-                    $result = false;
-                    continue;
-                }
-                if (!$obj = block_instance($record->name, $block)) {
-                    $result = false;
-                    continue;
-                }
-                // Return value ignored, in core mods this does not do anything, but just in case
-                // third party blocks might have stuff to clean up
-                // we execute this anyway
-                $obj->instance_delete();
-
-            }
-        } else {
-            $result = false;
-        }
-    }
+    blocks_delete_all_for_context($context->id);
 
 /// Delete any groups, removing members and grouping/course links first.
     require_once($CFG->dirroot.'/group/lib.php');
@@ -3688,7 +3656,6 @@ function remove_course_contents($courseid, $showfeedback=true) {
     question_delete_course($course, $showfeedback);
 
 /// Remove all data from gradebook
-    $context = get_context_instance(CONTEXT_COURSE, $courseid);
     remove_course_grades($courseid, $showfeedback);
     remove_grade_letters($context, $showfeedback);
 
index a6690df4b2fa5a0b337659ad4cac9fd46ee28cfb..dece0560b0f851fde612bc2b2f198aec5eaeef79 100644 (file)
@@ -330,7 +330,7 @@ class moodle_page {
         if ($this->_legacypageobject) {
             return $this->_legacypageobject->user_allowed_editing();
         }
-        return has_any_capability($this->all_editing_caps(), $this->_context);
+        return has_any_capability($this->all_editing_caps(), $this->context);
     }
 
 /// Setter methods =============================================================
@@ -974,6 +974,7 @@ function page_create_object($type, $id = NULL) {
             $legacypage->set_course($SITE);
         }
     }
+
     $legacypage->set_pagetype($type);
 
     $legacypage->set_url($ME);