MDL-33877: Prevent duplicate records in grade items table after running assignment...
authorDamyon Wiese <damyon.wiese@netspot.com.au>
Wed, 20 Jun 2012 06:33:33 +0000 (14:33 +0800)
committerDamyon Wiese <damyon.wiese@netspot.com.au>
Wed, 20 Jun 2012 06:33:33 +0000 (14:33 +0800)
The cause was multiple calls to set_field on the same table. Changed to update_record.

mod/assign/upgradelib.php

index 3852d07..92d59a0 100644 (file)
@@ -154,8 +154,12 @@ class assign_upgrade_manager {
 
             // upgrade completion data
             $DB->set_field('course_modules_completion', 'coursemoduleid', $newcoursemodule->id, array('coursemoduleid'=>$oldcoursemodule->id));
-            $DB->set_field('course_completion_criteria', 'module', 'assign', array('moduleinstance'=>$oldcoursemodule->id));
-            $DB->set_field('course_completion_criteria', 'moduleinstance', $newcoursemodule->id, array('moduleinstance'=>$oldcoursemodule->id));
+            $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance'=>$oldcoursemodule->id));
+            foreach ($allcriteria as $criteria) {
+                $criteria->module = 'assign';
+                $criteria->moduleinstance = $newcoursemodule->id;
+                $DB->update_record('course_completion_criteria', $criteria);
+            }
             $completiondone = true;
 
 
@@ -221,8 +225,13 @@ class assign_upgrade_manager {
             $newassignment->update_calendar($newcoursemodule->id);
 
             // copy the grades from the old assignment to the new one
-            $DB->set_field('grade_items', 'itemmodule', 'assign', array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assignment'));
-            $DB->set_field('grade_items', 'iteminstance', $newassignment->get_instance()->id, array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assign'));
+
+            $gradeitem = $DB->get_record('grade_items', array('iteminstance'=>$oldassignment->id, 'itemmodule'=>'assignment'), 'id', IGNORE_MISSING);
+            if ($gradeitem) {
+                $gradeitem->iteminstance = $newassignment->get_instance()->id;
+                $gradeitem->itemmodule = 'assign';
+                $DB->update_record('grade_items', $gradeitem);
+            }
             $gradesdone = true;
 
         } catch (Exception $exception) {
@@ -234,14 +243,22 @@ class assign_upgrade_manager {
             // roll back the grades changes
             if ($gradesdone) {
                 // copy the grades from the old assignment to the new one
-                $DB->set_field('grade_items', 'itemmodule', 'assignment', array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assign'));
-                $DB->set_field('grade_items', 'iteminstance', $oldassignment->id, array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assignment'));
+                $gradeitem = $DB->get_record('grade_items', array('iteminstance'=>$newassignment->get_instance()->id, 'itemmodule'=>'assign'), 'id', IGNORE_MISSING);
+                if ($gradeitem) {
+                    $gradeitem->iteminstance = $oldassignment->id;
+                    $gradeitem->itemmodule = 'assignment';
+                    $DB->update_record('grade_items', $gradeitem);
+                }
             }
             // roll back the completion changes
             if ($completiondone) {
                 $DB->set_field('course_modules_completion', 'coursemoduleid', $oldcoursemodule->id, array('coursemoduleid'=>$newcoursemodule->id));
-                $DB->set_field('course_completion_criteria', 'module', 'assignment', array('moduleinstance'=>$newcoursemodule->id));
-                $DB->set_field('course_completion_criteria', 'moduleinstance', $oldcoursemodule->id, array('moduleinstance'=>$newcoursemodule->id));
+                $allcriteria = $DB->get_records('course_completion_criteria', array('moduleinstance'=>$newcoursemodule->id));
+                foreach ($allcriteria as $criteria) {
+                    $criteria->module = 'assignment';
+                    $criteria->moduleinstance = $oldcoursemodule->id;
+                    $DB->update_record('course_completion_criteria', $criteria);
+                }
             }
             // roll back the advanced grading update
             if ($gradingarea) {