Permalink
Browse files

MDL-33877: Prevent duplicate records in grade items table after runni…

…ng assignment upgrade tool

The cause was multiple calls to set_field on the same table. Changed to update_record.
  • Loading branch information...
1 parent e85b7e7 commit 1f417de84a9de7448fa1b55385462e79e67a1791 Damyon Wiese committed Jun 20, 2012
Showing with 25 additions and 8 deletions.
  1. +25 −8 mod/assign/upgradelib.php
@@ -154,8 +154,12 @@ public function upgrade_assignment($oldassignmentid, & $log) {
// 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 @@ public function upgrade_assignment($oldassignmentid, & $log) {
$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 @@ public function upgrade_assignment($oldassignmentid, & $log) {
// 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) {

0 comments on commit 1f417de

Please sign in to comment.