Skip to content

Commit

Permalink
MDL-36914: Re-associate all grade_item records when upgrading assignm…
Browse files Browse the repository at this point in the history
…ents to prevent data loss
  • Loading branch information
Nathan Mares authored and Damyon Wiese committed Nov 29, 2012
1 parent 1c951c4 commit fb9c26d
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions mod/assign/upgradelib.php
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit fb9c26d

Please sign in to comment.