]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-7164, Mark Nielsen's patch for remove_course_content not calling instance_delete()
authortoyomoyo <toyomoyo>
Thu, 22 Mar 2007 02:11:51 +0000 (02:11 +0000)
committertoyomoyo <toyomoyo>
Thu, 22 Mar 2007 02:11:51 +0000 (02:11 +0000)
lib/moodlelib.php

index 0154222118664d61f2f66cfafaf38225796867ab..a822ba8333b2addaf13526525b4587d298e01aa9 100644 (file)
@@ -2991,8 +2991,31 @@ function remove_course_contents($courseid, $showfeedback=true) {
             if ($showfeedback) {
                 notify($strdeleted .' block_instance');
             }
+            
+            require_once($CFG->libdir.'/blocklib.php');
             foreach ($blocks as $block) {  /// Delete any associated contexts for this block
-                delete_context(CONTEXT_BLOCK, $block->id);
+                
+                // Block instances are rarely created. Since the block instance is gone from the above delete
+                // statement, calling delete_context() will generate a warning as get_context_instance could
+                // no longer create the context as the block is already gone. 
+                if (record_exists('context', 'contextlevel', CONTEXT_BLOCK, 'instanceid', $block->id)) {
+                    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;
@@ -3083,7 +3106,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
     if ($courseid != SITEID) {
         delete_context(CONTEXT_COURSE, $course->id);
     }
-    
+
     return $result;
 }