Permalink
Browse files

MDL-36914: Re-associate all grade_item records when upgrading assignm…

…ents to prevent data loss
  • Loading branch information...
1 parent 1c951c4 commit fb9c26d0929eeb9489b7b10c42fc05034244f198 Nathan Mares committed with damyon Nov 27, 2012
Showing with 6 additions and 14 deletions.
  1. +6 −14 mod/assign/upgradelib.php
View
@@ -229,14 +229,10 @@ public function upgrade_assignment($oldassignmentid, & $log) {
$newassignment->update_calendar($newcoursemodule->id);
- // copy the grades from the old assignment to the new one
+ // reassociate grade_items from the old assignment instance to the new assign instance. This includes outcome linked grade_items
+ $sql = "UPDATE {grade_items} SET itemmodule = 'assign', iteminstance = ? WHERE itemmodule = 'assignment' AND iteminstance = ?";
+ $DB->execute($sql, array($newassignment->get_instance()->id, $oldassignment->id));
- $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) {
@@ -247,13 +243,9 @@ public function upgrade_assignment($oldassignmentid, & $log) {
if ($rollback) {
// roll back the grades changes
if ($gradesdone) {
- // copy the grades from the old assignment to the new one
- $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);
- }
+ // reassociate grade_items from the new assign instance to the old assignment instance
+ $sql = "UPDATE {grade_items} SET itemmodule = 'assign', iteminstance = ? WHERE itemmodule = 'assignment' AND iteminstance = ?";
+ $DB->execute($sql, array($oldassignment->id, $newassignment->get_instance()->id));
}
// roll back the completion changes
if ($completiondone) {

0 comments on commit fb9c26d

Please sign in to comment.