diff --git a/course/lib.php b/course/lib.php index f35bbfa5ce3..f37e6f4342f 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2854,6 +2854,7 @@ 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)); delete_context(CONTEXT_MODULE, $cm->id); return $DB->delete_records('course_modules', array('id'=>$cm->id)); diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index d335eb43482..ef81ebb001b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -204,6 +204,23 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2012030100.02); } + if ($oldversion < 2012030900.01) { + // 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"; + } else { + $sql = "DELETE FROM {course_completion_criteria} + WHERE NOT EXISTS ( + SELECT 'x' FROM {course_modules} + WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)"; + } + $DB->execute($sql); + + // Main savepoint reached + upgrade_main_savepoint(true, 2012030900.01); + } return true; } diff --git a/version.php b/version.php index 3b88d2320bc..a76f0576e90 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2012030900.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2012030900.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes