]> git.mjollnir.org Git - moodle.git/commitdiff
First pass at a fix for MDL-6601 (delete contexts when deleting course)
authormoodler <moodler>
Fri, 22 Sep 2006 17:49:45 +0000 (17:49 +0000)
committermoodler <moodler>
Fri, 22 Sep 2006 17:49:45 +0000 (17:49 +0000)
lib/moodlelib.php

index 9448ce2436b8ead2bc2430b49853049357634a54..7115487fbba795c845afcc072f350bb606874eac 100644 (file)
@@ -2597,6 +2597,13 @@ function delete_course($courseid, $showfeedback = true) {
         $result = false;
     }
 
+    if (!delete_records('context', 'contextlevel', CONTEXT_COURSE, 'instance', $courseid)) {
+        if ($showfeedback) {
+            notify("An error occurred while deleting the main context record.");
+        }
+        $result = false;
+    }
+
     if (!fulldelete($CFG->dataroot.'/'.$courseid)) {
         if ($showfeedback) {
             notify("An error occurred while deleting the course files.");
@@ -2644,8 +2651,12 @@ function remove_course_contents($courseid, $showfeedback=true) {
                 if (function_exists($moddelete)) {
                     if ($instances = get_records($modname, 'course', $course->id)) {
                         foreach ($instances as $instance) {
+                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
+                                delete_records('context', 'contextlevel', CONTEXT_MODULE, 'instance', $cm->id);
+                            }
                             if ($moddelete($instance->id)) {
                                 $count++;
+
                             } else {
                                 notify('Could not delete '. $modname .' instance '. $instance->id .' ('. format_string($instance->name) .')');
                                 $result = false;
@@ -2674,20 +2685,23 @@ function remove_course_contents($courseid, $showfeedback=true) {
     notify_local_delete_course($courseid, $showfeedback);
 
     // Delete course blocks
-    if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
-        if ($showfeedback) {
-            notify($strdeleted .' block_instance');
+    if ($blocks = get_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
+        foreach ($blocks as $block) {
+            delete_records('context', 'contextlevel', CONTEXT_BLOCK, 'instance', $block->id);
+        }
+        if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
+            if ($showfeedback) {
+                notify($strdeleted .' block_instance');
+            }
+        } else {
+            $result = false;
         }
-    } else {
-        $result = false;
     }
 
     // Delete Other stuff.
     // This array stores the tables that need to be cleared, as
     // table_name => column_name that contains the course id.
     $tablestoclear = array(
-        //'user_students' => 'course', // Delete any user stuff
-        //'user_teachers' => 'course',
         'event' => 'courseid', // Delete events
         'log' => 'course', // Delete logs
         'course_sections' => 'course', // Delete any course stuff