MDL-29598 backup Avoid possible future duplicate grade letters
authorDavid Monllao <davidm@moodle.com>
Fri, 17 Aug 2012 02:18:20 +0000 (10:18 +0800)
committerDavid Monllao <davidm@moodle.com>
Fri, 24 Aug 2012 00:54:49 +0000 (08:54 +0800)
More info in restore_activity_grades_structure_step->process_grade_letter() comments

backup/moodle2/restore_stepslib.php

index 255808f..6b39852 100644 (file)
@@ -270,10 +270,9 @@ class restore_gradebook_structure_step extends restore_structure_step {
 
         $data->contextid = context_course::instance($this->get_courseid())->id;
 
 
         $data->contextid = context_course::instance($this->get_courseid())->id;
 
-        // MDL-29598 - Don't insert a duplicate record if this grade letter already exists
-        $gltest = (array)$data;
-        unset($gltest['id']);
-        if (!$DB->record_exists('grade_letters', $gltest)) {
+        $gradeletter = (array)$data;
+        unset($gradeletter['id']);
+        if (!$DB->record_exists('grade_letters', $gradeletter)) {
             $newitemid = $DB->insert_record('grade_letters', $data);
         } else {
             $newitemid = $data->id;
             $newitemid = $DB->insert_record('grade_letters', $data);
         } else {
             $newitemid = $data->id;
@@ -2407,17 +2406,21 @@ class restore_activity_grades_structure_step extends restore_structure_step {
 
     /**
      * process activity grade_letters. Note that, while these are possible,
 
     /**
      * process activity grade_letters. Note that, while these are possible,
-     * because grade_letters are contextid based, in proctice, only course
+     * because grade_letters are contextid based, in practice, only course
      * context letters can be defined. So we keep here this method knowing
      * it won't be executed ever. gradebook restore will restore course letters.
      */
     protected function process_grade_letter($data) {
         global $DB;
 
      * context letters can be defined. So we keep here this method knowing
      * it won't be executed ever. gradebook restore will restore course letters.
      */
     protected function process_grade_letter($data) {
         global $DB;
 
-        $data = (object)$data;
+        $data['contextid'] = $this->task->get_contextid();
+        $gradeletter = (object)$data;
 
 
-        $data->contextid = $this->task->get_contextid();
-        $newitemid = $DB->insert_record('grade_letters', $data);
+        // Check if it exists before adding it
+        unset($data['id']);
+        if (!$DB->record_exists('grade_letters', $data)) {
+            $newitemid = $DB->insert_record('grade_letters', $gradeletter);
+        }
         // no need to save any grade_letter mapping
     }
 }
         // no need to save any grade_letter mapping
     }
 }