Merge branch 'MDL-28949d' of git://github.com/srynot4sale/moodle
[moodle.git] / lib / db / upgrade.php
index a2f0052..f505023 100644 (file)
@@ -648,11 +648,48 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2012052100.00);
     }
 
+    if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
+        //first get all instances of duplicate records
+        $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
+        $duplicates = $DB->get_recordset_sql($sql, array());
+
+        foreach ($duplicates as $duplicate) {
+            $pointer = 0;
+            //now get all the records for this user/course
+            $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
+            $completions = $DB->get_records_select('course_completions', $sql,
+                array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
+            $needsupdate = false;
+            $origcompletion = null;
+            foreach ($completions as $completion) {
+                $pointer++;
+                if ($pointer === 1) { //keep 1st record but delete all others.
+                    $origcompletion = $completion;
+                } else {
+                    //we need to keep the "oldest" of all these fields as the valid completion record.
+                    $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
+                    foreach ($fieldstocheck as $f) {
+                        if ($origcompletion->$f > $completion->$f) {
+                            $origcompletion->$f = $completion->$f;
+                            $needsupdate = true;
+                        }
+                    }
+                    $DB->delete_records('course_completions', array('id'=>$completion->id));
+                }
+            }
+            if ($needsupdate) {
+                $DB->update_record('course_completions', $origcompletion);
+            }
+        }
+
+        // Main savepoint reached
+        upgrade_main_savepoint(true, 2012052500.03);
+    }
 
     /**
      * Major clean up of course completion tables
      */
-    if ($oldversion < 2012052500.02) {
+    if ($oldversion < 2012052900.00) {
 
         // Clean up all instances of duplicate records
         // Add indexes to prevent new duplicates
@@ -673,10 +710,10 @@ function xmldb_main_upgrade($oldversion) {
         }
 
         // Main savepoint reached
-        upgrade_main_savepoint(true, 2012052500.02);
+        upgrade_main_savepoint(true, 2012052900.00);
     }
 
-    if ($oldversion < 2012052500.03) {
+    if ($oldversion < 2012052900.01) {
 
         upgrade_course_completion_remove_duplicates(
             'course_completion_crit_compl',
@@ -695,10 +732,10 @@ function xmldb_main_upgrade($oldversion) {
         }
 
         // Main savepoint reached
-        upgrade_main_savepoint(true, 2012052500.03);
+        upgrade_main_savepoint(true, 2012052900.01);
     }
 
-    if ($oldversion < 2012052500.04) {
+    if ($oldversion < 2012052900.02) {
 
         upgrade_course_completion_remove_duplicates(
             'course_completion_aggr_methd',
@@ -715,8 +752,8 @@ function xmldb_main_upgrade($oldversion) {
         }
 
         // Main savepoint reached
-        upgrade_main_savepoint(true, 2012052500.04);
+        upgrade_main_savepoint(true, 2012052900.02);
     }
 
     return true;
-}
+}
\ No newline at end of file