diff --git a/course/delete.php b/course/delete.php index 053b760e03daa..2a9d0da6340e3 100644 --- a/course/delete.php +++ b/course/delete.php @@ -67,17 +67,7 @@ print_heading($strdeletingcourse); - if (!remove_course_contents($course->id)) { - notify("An error occurred while deleting some of the course contents."); - } - - if (!delete_records("course", "id", $course->id)) { - notify("An error occurred while deleting the main course record."); - } - - if (!fulldelete($CFG->dataroot.'/'.$course->id)) { - notify("An error occurred while deleting the course files."); - } + delete_course($course->id); print_heading( get_string("deletedcourse", "", $course->shortname) ); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 4a639a5346736..93aca5bf68fdc 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3057,6 +3057,44 @@ function remove_admin($userid) { return delete_records('user_admins', 'userid', $userid); } +/** + * Delete a course, including all related data from the database, + * and any associated files from the moodledata folder. + * + * @param int $courseid The id of the course to delete. + * @param bool $showfeedback Whether to display notifications of each action the function performs. + * @return bool true if all the removals succeeded. false if there were any failures. If this + * method returns false, some of the removals will probably have succeeded, and others + * failed, but you have no way of knowing which. + */ +function delete_course($courseid, $showfeedback = true) { + global $CFG; + $result = true; + + if (!remove_course_contents($courseid, $showfeedback)) { + if ($showfeedback) { + notify("An error occurred while deleting some of the course contents."); + } + $result = false; + } + + if (!delete_records("course", "id", $courseid)) { + if ($showfeedback) { + notify("An error occurred while deleting the main course record."); + } + $result = false; + } + + if (!fulldelete($CFG->dataroot.'/'.$courseid)) { + if ($showfeedback) { + notify("An error occurred while deleting the course files."); + } + $result = false; + } + + return $result; +} + /** * Clear a course out completely, deleting all content * but don't delete the course itself @@ -3064,7 +3102,9 @@ function remove_admin($userid) { * @uses $CFG * @param int $courseid The id of the course that is being deleted * @param bool $showfeedback Whether to display notifications of each action the function performs. - * @return bool + * @return bool true if all the removals succeeded. false if there were any failures. If this + * method returns false, some of the removals will probably have succeeded, and others + * failed, but you have no way of knowing which. */ function remove_course_contents($courseid, $showfeedback=true) { @@ -3276,7 +3316,7 @@ function remove_course_contents($courseid, $showfeedback=true) { * @param bool $removegroups Whether to remove matching records from the groups table. * @param bool $removeevents Whether to remove matching records from the event table. * @param bool $removelogs Whether to remove matching records from the log table. - * @return bool true if all the removals succeeded. talse if there were any failures. If this + * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */