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:26:12 +0000 (10:26 +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 412f94b..1eb9c95 100644 (file)
@@ -2782,5 +2782,16 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2021052500.24);
     }
 
+    if ($oldversion < 2021052500.26) {
+        // 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, 2021052500.26);
+    }
+
     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 d65f2ff..eab1c5f 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2021052500.25;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2021052500.26;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '4.0dev (Build: 20201016)'; // Human-friendly version name