diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 870b40af311..b1d07025e3d 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -926,6 +926,7 @@ class backup_gradebook_structure_step extends backup_structure_step { } protected function define_structure() { + global $CFG, $DB; // are we including user info? $userinfo = $this->get_setting_value('users'); @@ -1017,7 +1018,18 @@ class backup_gradebook_structure_step extends backup_structure_step { $letter->set_source_table('grade_letters', array('contextid' => backup::VAR_CONTEXTID)); - $grade_setting->set_source_table('grade_settings', array('courseid' => backup::VAR_COURSEID)); + // Set the grade settings source, forcing the inclusion of minmaxtouse if not present. + $settings = array(); + $rs = $DB->get_recordset('grade_settings', array('courseid' => $this->get_courseid())); + foreach ($rs as $record) { + $settings[$record->name] = $record; + } + $rs->close(); + if (!isset($settings['minmaxtouse'])) { + $settings['minmaxtouse'] = (object) array('name' => 'minmaxtouse', 'value' => $CFG->grade_minmaxtouse); + } + $grade_setting->set_source_array($settings); + // Annotations (both as final as far as they are going to be exported in next steps) $grade_item->annotate_ids('scalefinal', 'scaleid'); // Straight as scalefinal because it's > 0 diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 593384f850e..17d6fedb1d3 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -459,6 +459,9 @@ class restore_gradebook_structure_step extends restore_structure_step { } $rs->close(); + // Check what to do with the minmaxtouse setting. + $this->check_minmaxtouse(); + // Freeze gradebook calculations if needed. $this->gradebook_calculation_freeze(); @@ -483,6 +486,66 @@ class restore_gradebook_structure_step extends restore_structure_step { upgrade_extra_credit_weightoverride($this->get_courseid()); } } + + /** + * Checks what should happen with the course grade setting minmaxtouse. + * + * This is related to the upgrade step at the time the setting was added. + * + * @see MDL-48618 + * @return void + */ + protected function check_minmaxtouse() { + global $CFG, $DB; + require_once($CFG->libdir . '/gradelib.php'); + + $userinfo = $this->task->get_setting_value('users'); + $settingname = 'minmaxtouse'; + $courseid = $this->get_courseid(); + $minmaxtouse = $DB->get_field('grade_settings', 'value', array('courseid' => $courseid, 'name' => $settingname)); + $version28start = 2014111000.00; + $version28last = 2014111006.05; + + if ($minmaxtouse === false) { + // The setting was not found because this setting did not exist at the time the backup was made. + $version = $this->get_task()->get_info()->moodle_version; + + if ($version < $version28start) { + // We need to set it to use grade_item, but only if the site-wide setting is different. No need to notice them. + if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_ITEM) { + grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_ITEM); + } + + } else if ($version >= $version28start && $version < $version28last) { + // They should be using grade_grade when the course has inconsistencies. + + $sql = "SELECT gi.id + FROM {grade_items} gi + JOIN {grade_grades} gg + ON gg.itemid = gi.id + WHERE gi.courseid = ? + AND (gi.itemtype != ? AND gi.itemtype != ?) + AND (gg.rawgrademax != gi.grademax OR gg.rawgrademin != gi.grademin)"; + + // The course can only have inconsistencies when we restore the user info, + // we do not need to act on existing grades that were not restored as part of this backup. + if ($userinfo && $DB->record_exists_sql($sql, array($courseid, 'course', 'category'))) { + + // Display the notice as we do during upgrade. + set_config('show_min_max_grades_changed_' . $courseid, 1); + + if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_GRADE) { + // We need set the setting as their site-wise setting is not GRADE_MIN_MAX_FROM_GRADE_GRADE. + // If they are using the site-wide grade_grade setting, we only want to notice them. + grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_GRADE); + } + } + + } else { + // This should never happen because from now on minmaxtouse is always saved in backups. + } + } + } } /**