MDL-69687 Course: remove_course_contents orphaned completion data
authorsam marshall <s.marshall@open.ac.uk>
Mon, 19 Oct 2020 09:14:09 +0000 (10:14 +0100)
committersam marshall <s.marshall@open.ac.uk>
Mon, 19 Oct 2020 09:24:44 +0000 (10:24 +0100)
The remove_course_contents function did not delete data from
course_module_completion table in most cases, resulting in large
quantities of orphaned data.

lib/db/upgrade.php
lib/moodlelib.php
version.php

index c1f539b..e9d5609 100644 (file)
@@ -2782,5 +2782,16 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2020101300.01);
     }
 
+    if ($oldversion < 2020101600.01) {
+        // 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, 2020101600.01);
+    }
+
     return true;
 }
index fde434d..b571bb7 100644 (file)
@@ -5306,6 +5306,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);
                     }
index 7888047..2c43adf 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020101600.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2020101600.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '3.10dev+ (Build: 20201016)';// Human-friendly version name