diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 28e2f1ef05f..8aabe15767f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3948,5 +3948,16 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2019111804.08); } + if ($oldversion < 2019111805.09) { + // Delete orphaned course_modules_completion rows; these were not deleted properly + // by remove_course_contents function. + $DB->delete_records_subquery('course_modules_completion', 'id', 'id', + "SELECT cmc.id + FROM {course_modules_completion} cmc + LEFT JOIN {course_modules} cm ON cm.id = cmc.coursemoduleid + WHERE cm.id IS NULL"); + upgrade_main_savepoint(true, 2019111805.09); + } + return true; } diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index c0b574ddf56..d182cf117d4 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1970,7 +1970,7 @@ abstract class moodle_database { * @param string $subquery Subquery that will return values of the field to delete * @param array $params Parameters for subquery * @throws dml_exception If there is any error - * @since Moodle 3.10 + * @since Moodle 3.8.6 */ public function delete_records_subquery(string $table, string $field, string $alias, string $subquery, array $params = []): void { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index d4c5e583373..36c4a45ee41 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5256,6 +5256,7 @@ function remove_course_contents($courseid, $showfeedback = true, array $options if ($cm->id) { // Delete cm and its context - orphaned contexts are purged in cron in case of any race condition. context_helper::delete_instance(CONTEXT_MODULE, $cm->id); + $DB->delete_records('course_modules_completion', ['coursemoduleid' => $cm->id]); $DB->delete_records('course_modules', array('id' => $cm->id)); rebuild_course_cache($cm->course, true); } diff --git a/version.php b/version.php index 49781064d44..8dd5df77736 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2019111805.08; // 20191118 = branching date YYYYMMDD - do not modify! +$version = 2019111805.09; // 20191118 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '3.8.5+ (Build: 20201016)'; // Human-friendly version name