gradebook MDL-23362 added a fix to deal with the course grade item already existing...
authorAndrew Davis <andrew@affinitysoftware.net>
Fri, 3 Sep 2010 08:36:58 +0000 (08:36 +0000)
committerAndrew Davis <andrew@affinitysoftware.net>
Fri, 3 Sep 2010 08:36:58 +0000 (08:36 +0000)
backup/moodle2/restore_stepslib.php

index cbb429b..1a06c29 100644 (file)
@@ -193,22 +193,28 @@ class restore_gradebook_step extends restore_structure_step {
         $data->timecreated  = $this->apply_date_offset($data->timecreated);
         $data->timemodified = $this->apply_date_offset($data->timemodified);
 
-        //no parent means a course level grade category. That should have been created when the course was created
+        $newitemid = null;
+        //no parent means a course level grade category. That may have been created when the course was created
         if(empty($data->parent)) {
+            //parent was being saved as 0 when it should be null
+            $data->parent = null;
+
             //get the already created course level grade category
             $category = new stdclass();
-            $category->courseid  = $this->get_courseid();
+            $category->courseid = $this->get_courseid();
 
             $coursecategory = $DB->get_record('grade_categories', (array)$category);
-            $newitemid = $coursecategory->id;
-            $data->id = $newitemid;
-            
-            //parent was being saved as 0 when it should be null
-            $data->parent = null;
+            if (!empty($coursecategory)) {
+                $data->id = $newitemid = $coursecategory->id;
+                $DB->update_record('grade_categories', $data);
+            }
+        }
 
-            $DB->update_record('grade_categories', $data);
-        } else {
-            $data->parent = $this->get_mappingid('grade_category', $data->parent);
+        //need to insert a course category
+        if (empty($newitemid)) {
+            if (!empty($data->parent)) {
+                $data->parent = $this->get_mappingid('grade_category', $data->parent);
+            }
             $newitemid = $DB->insert_record('grade_categories', $data);
         }
         $this->set_mapping('grade_category', $oldid, $newitemid);