]> git.mjollnir.org Git - moodle.git/commitdiff
removing duplicate entries in grade_grades prior to adding unique key
authortoyomoyo <toyomoyo>
Wed, 14 Nov 2007 05:07:33 +0000 (05:07 +0000)
committertoyomoyo <toyomoyo>
Wed, 14 Nov 2007 05:07:33 +0000 (05:07 +0000)
lib/db/upgrade.php

index 52b4ca76836fecbb10ec99005e05b48c2c820afb..891e5019f76d22d941bd6387e136f05afea25bd1 100644 (file)
@@ -2587,6 +2587,32 @@ function xmldb_main_upgrade($oldversion=0) {
     
     if ($result && $oldversion < 2007101502) {
 
+    /// try to remove duplicate entries
+    
+        $SQL = "SELECT id, userid, itemid
+               FROM {$CFG->prefix}grade_grades
+               GROUP BY userid, itemid
+               HAVING COUNT( * ) >1";
+        // duplicates found
+        if ($dups = get_records_sql($SQL)) {
+            // for each set of userid, itemid
+            foreach ($dups as $dup) {
+                if ($thisdups = get_records_sql("SELECT id,id FROM {$CFG->prefix}grade_grades 
+                                                 WHERE itemid = $dup->itemid AND userid = $dup->userid
+                                                 ORDER BY timemodified DESC")) {
+
+                    $processed = 0; // keep the first one
+                    foreach ($thisdups as $thisdup) {
+                        if ($processed) {
+                            // remove the duplicates
+                            delete_records('grade_grades', 'id', $thisdup->id);
+                        }
+                        $processed++;
+                    }
+                }
+            }
+        }
+
     /// Define key userid-itemid (unique) to be added to grade_grades
         $table = new XMLDBTable('grade_grades');
         $key = new XMLDBKey('userid-itemid');