Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-31914 Ensure that completion criteria are deleted when removing a…

… course module
  • Loading branch information...
commit 25d11fe9560efb2d187dee54f33172f39dd19e3d 1 parent 43b542b
@andrewnicols andrewnicols authored stronk7 committed
Showing with 23 additions and 2 deletions.
  1. +2 −0  course/lib.php
  2. +21 −2 lib/db/upgrade.php
View
2  course/lib.php
@@ -2836,6 +2836,8 @@ function delete_course_module($id) {
// very quick on an empty table)
$DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_modules_availability', array('coursemoduleid'=> $cm->id));
+ $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
+ 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
delete_context(CONTEXT_MODULE, $cm->id);
return $DB->delete_records('course_modules', array('id'=>$cm->id));
View
23 lib/db/upgrade.php
@@ -6762,6 +6762,25 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2011070104.12);
}
- return true;
-}
+ if ($oldversion < 2011070105.07) {
+ require_once($CFG->libdir . '/completion/completion_criteria.php');
+ // Delete orphaned criteria which were left when modules were removed
+ if ($DB->get_dbfamily() === 'mysql') {
+ $sql = "DELETE cc FROM {course_completion_criteria} cc
+ LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
+ WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
+ } else {
+ $sql = "DELETE FROM {course_completion_criteria}
+ WHERE NOT EXISTS (
+ SELECT 'x' FROM {course_modules}
+ WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
+ AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
+ }
+ $DB->execute($sql);
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2011070105.07);
+ }
+
+ return true;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.