]> git.mjollnir.org Git - moodle.git/commitdiff
Move the code for clearing out a course into a new function:
authormoodler <moodler>
Sat, 2 Aug 2003 11:32:59 +0000 (11:32 +0000)
committermoodler <moodler>
Sat, 2 Aug 2003 11:32:59 +0000 (11:32 +0000)
  remove_course_contents()

course/delete.php
lib/moodlelib.php

index 17d0092d3825dc5f47af95edbcb4f23273402c87..a55a121b26919b8e74f5b114b41e77028070e6d4 100644 (file)
 
     print_heading($strdeletingcourse);
 
-    $strdeleted = get_string("deleted");
-    // First delete every instance of every module
-
-    if ($allmods = get_records("modules") ) {
-        foreach ($allmods as $mod) {
-            $modname = $mod->name;
-            $modfile = "../mod/$modname/lib.php";
-            $moddelete = $modname."_delete_instance";
-            $count=0;
-            if (file_exists($modfile)) {
-                include_once($modfile);
-                if (function_exists($moddelete)) {
-                    if ($instances = get_records($modname, "course", $course->id)) {
-                        foreach ($instances as $instance) {
-                            if ($moddelete($instance->id)) {
-                                $count++;
-                            } else {
-                                notify("Could not delete $modname instance $instance->id ($instance->name)");
-                            }
-                        }
-                    }
-                } else {
-                    notify("Function $moddelete() doesn't exist!");
-                }
-
-            }
-            notify("$strdeleted $count instances of $modname");
-        } 
-    } else {
-        error("No modules are installed!");
-    } 
-
-    // Delete any user stuff
-
-    if (delete_records("user_students", "course", $course->id)) {
-        notify("$strdeleted student enrolments");
-    }
-
-    if (delete_records("user_teachers", "course", $course->id)) {
-        notify("$strdeleted teachers");
+    if (!remove_course_contents($course->id)) {
+        notify("An error occurred while deleting some of the course contents.");
     }
 
-    // Delete logs
-
-    if (delete_records("log", "course", $course->id)) {
-        notify("$strdeleted logs");
-    }
-
-    // Delete any course stuff
-
-    if (delete_records("course_sections", "course", $course->id)) {
-        notify("$strdeleted course sections");
-    }
-    if (delete_records("course_modules", "course", $course->id)) {
-        notify("$strdeleted course modules");
-    }
-    if (delete_records("course", "id", $course->id)) {
-        notify("$strdeleted the main course record");
+    if (!delete_records("course", "id", $course->id)) {
+        notify("An error occurred while deleting the main course record.");
     }
 
     print_heading( get_string("deletedcourse", "", $course->shortname) );
index 2ed83d6ba27f14c214f3cfbf74f7954d80a1648a..a3d3e52940936c1cabb15e2dd48ff8101667590c 100644 (file)
@@ -662,6 +662,105 @@ function remove_admin($user) {
 }
 
 
+function remove_course_contents($courseid, $showfeedback=true) {
+/// Clear a course out completely, deleting all content
+/// but don't delete the course itself
+
+    global $CFG;
+
+    $result = true;
+
+    if (! $course = get_record("course", "id", $courseid)) {
+        error("Course ID was incorrect (can't find it)");
+    }
+
+    $strdeleted = get_string("deleted");
+
+    // First delete every instance of every module
+    
+    if ($allmods = get_records("modules") ) {
+        foreach ($allmods as $mod) {
+            $modname = $mod->name;
+            $modfile = "$CFG->dirroot/mod/$modname/lib.php";
+            $moddelete = $modname."_delete_instance";
+            $count=0;
+            if (file_exists($modfile)) {
+                include_once($modfile);
+                if (function_exists($moddelete)) {
+                    if ($instances = get_records($modname, "course", $course->id)) {
+                        foreach ($instances as $instance) {
+                            if ($moddelete($instance->id)) {
+                                $count++;
+                            } else {
+                                notify("Could not delete $modname instance $instance->id ($instance->name)");
+                                $result = false;
+                            }
+                        }
+                    }
+                } else {
+                    notify("Function $moddelete() doesn't exist!");
+                    $result = false;
+                }
+
+            }
+            if ($showfeedback) {
+                notify("$strdeleted $count x $modname");
+            }
+        }
+    } else {
+        error("No modules are installed!");
+    }
+
+    // Delete any user stuff
+
+    if (delete_records("user_students", "course", $course->id)) {
+        if ($showfeedback) {
+            notify("$strdeleted user_students");
+        }
+    } else {
+        $result = false;
+    }
+
+    if (delete_records("user_teachers", "course", $course->id)) {
+        if ($showfeedback) {
+            notify("$strdeleted user_teachers");
+        }
+    } else {
+        $result = false;
+    }
+
+    // Delete logs
+
+    if (delete_records("log", "course", $course->id)) {
+        if ($showfeedback) {
+            notify("$strdeleted log");
+        }
+    } else {
+        $result = false;
+    }
+
+    // Delete any course stuff
+
+    if (delete_records("course_sections", "course", $course->id)) {
+        if ($showfeedback) {
+            notify("$strdeleted course_sections");
+        }
+    } else {
+        $result = false;
+    }
+
+    if (delete_records("course_modules", "course", $course->id)) {
+        if ($showfeedback) {
+            notify("$strdeleted course_modules");
+        }
+    } else {
+        $result = false;
+    }
+
+    return $result;
+
+}
+
 
 /// CORRESPONDENCE  ////////////////////////////////////////////////