diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 111dfac90c5..c9038cfc954 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -3051,11 +3051,6 @@ class restore_activity_grades_structure_step extends restore_structure_step { $oldparentid = $data->categoryid; $courseid = $this->get_courseid(); - // make sure top course category exists, all grade items will be associated - // to it. Later, if restoring the whole gradebook, categories will be introduced - $coursecat = grade_category::fetch_course_category($courseid); - $coursecatid = $coursecat->id; // Get the categoryid to be used - $idnumber = null; if (!empty($data->idnumber)) { // Don't get any idnumber from course module. Keep them as they are in grade_item->idnumber @@ -3080,8 +3075,18 @@ class restore_activity_grades_structure_step extends restore_structure_step { } } + if (!empty($data->categoryid)) { + // If the grade category id of the grade item being restored belongs to this course + // then it is a fair assumption that this is the correct grade category for the activity + // and we should leave it in place, if not then unset it. + // TODO MDL-34790 Gradebook does not import if target course has gradebook categories. + $conditions = array('id' => $data->categoryid, 'courseid' => $courseid); + if (!$this->task->is_samesite() || !$DB->record_exists('grade_categories', $conditions)) { + unset($data->categoryid); + } + } + unset($data->id); - $data->categoryid = $coursecatid; $data->courseid = $this->get_courseid(); $data->iteminstance = $this->task->get_activityid(); $data->idnumber = $idnumber;