MDL-54567 backup: Process the calculations_freeze item upon restore
This commit is contained in:
@@ -930,8 +930,11 @@ class backup_gradebook_structure_step extends backup_structure_step {
|
||||
$grade_setting = new backup_nested_element('grade_setting', 'id', array(
|
||||
'name', 'value'));
|
||||
|
||||
$gradebook_attributes = new backup_nested_element('attributes', null, array('calculations_freeze'));
|
||||
|
||||
// Build the tree
|
||||
$gradebook->add_child($gradebook_attributes);
|
||||
|
||||
$gradebook->add_child($grade_categories);
|
||||
$grade_categories->add_child($grade_category);
|
||||
|
||||
@@ -946,14 +949,15 @@ class backup_gradebook_structure_step extends backup_structure_step {
|
||||
$gradebook->add_child($grade_settings);
|
||||
$grade_settings->add_child($grade_setting);
|
||||
|
||||
// Define sources
|
||||
|
||||
// Add attribute with gradebook calculation freeze date if needed.
|
||||
$attributes = new stdClass();
|
||||
$gradebookcalculationfreeze = get_config('core', 'gradebook_calculations_freeze_' . $this->get_courseid());
|
||||
if ($gradebookcalculationfreeze) {
|
||||
$gradebook->add_attributes(array('calculations_freeze'));
|
||||
$gradebook->get_attribute('calculations_freeze')->set_value($gradebookcalculationfreeze);
|
||||
$attributes->calculations_freeze = $gradebookcalculationfreeze;
|
||||
}
|
||||
|
||||
// Define sources
|
||||
$gradebook_attributes->set_source_array([$attributes]);
|
||||
|
||||
//Include manual, category and the course grade item
|
||||
$grade_items_sql ="SELECT * FROM {grade_items}
|
||||
|
||||
@@ -121,6 +121,21 @@ class restore_gradebook_structure_step extends restore_structure_step {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Identify the backup we're dealing with.
|
||||
$backuprelease = floatval($this->get_task()->get_info()->backup_release); // The major version: 2.9, 3.0, ...
|
||||
$backupbuild = 0;
|
||||
preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches);
|
||||
if (!empty($matches[1])) {
|
||||
$backupbuild = (int) $matches[1]; // The date of Moodle build at the time of the backup.
|
||||
}
|
||||
|
||||
// On older versions the freeze value has to be converted.
|
||||
// We do this from here as it is happening right before the file is read.
|
||||
// This only targets the backup files that can contain the legacy freeze.
|
||||
if ($backupbuild > 20150618 && ($backuprelease < 3.0 || $backupbuild < 20160527)) {
|
||||
$this->rewrite_step_backup_file_for_legacy_freeze($fullpath);
|
||||
}
|
||||
|
||||
// Arrived here, execute the step
|
||||
return true;
|
||||
}
|
||||
@@ -129,7 +144,7 @@ class restore_gradebook_structure_step extends restore_structure_step {
|
||||
$paths = array();
|
||||
$userinfo = $this->task->get_setting_value('users');
|
||||
|
||||
$paths[] = new restore_path_element('gradebook', '/gradebook');
|
||||
$paths[] = new restore_path_element('attributes', '/gradebook/attributes');
|
||||
$paths[] = new restore_path_element('grade_category', '/gradebook/grade_categories/grade_category');
|
||||
$paths[] = new restore_path_element('grade_item', '/gradebook/grade_items/grade_item');
|
||||
if ($userinfo) {
|
||||
@@ -141,7 +156,7 @@ class restore_gradebook_structure_step extends restore_structure_step {
|
||||
return $paths;
|
||||
}
|
||||
|
||||
protected function process_gradebook($data) {
|
||||
protected function process_attributes($data) {
|
||||
// For non-merge restore types:
|
||||
// Unset 'gradebook_calculations_freeze_' in the course and replace with the one from the backup.
|
||||
$target = $this->get_task()->get_target();
|
||||
@@ -581,6 +596,85 @@ class restore_gradebook_structure_step extends restore_structure_step {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite step definition to handle the legacy freeze attribute.
|
||||
*
|
||||
* In previous backups the calculations_freeze property was stored as an attribute of the
|
||||
* top level node <gradebook>. The backup API, however, do not process grandparent nodes.
|
||||
* It only processes definitive children, and their parent attributes.
|
||||
*
|
||||
* We had:
|
||||
*
|
||||
* <gradebook calculations_freeze="20160511">
|
||||
* <grade_categories>
|
||||
* <grade_category id="10">
|
||||
* <depth>1</depth>
|
||||
* ...
|
||||
* </grade_category>
|
||||
* </grade_categories>
|
||||
* ...
|
||||
* </gradebook>
|
||||
*
|
||||
* And this method will convert it to:
|
||||
*
|
||||
* <gradebook >
|
||||
* <attributes>
|
||||
* <calculations_freeze>20160511</calculations_freeze>
|
||||
* </attributes>
|
||||
* <grade_categories>
|
||||
* <grade_category id="10">
|
||||
* <depth>1</depth>
|
||||
* ...
|
||||
* </grade_category>
|
||||
* </grade_categories>
|
||||
* ...
|
||||
* </gradebook>
|
||||
*
|
||||
* Note that we cannot just load the XML file in memory as it could potentially be huge.
|
||||
* We can also completely ignore if the node <attributes> is already in the backup
|
||||
* file as it never existed before.
|
||||
*
|
||||
* @param string $filepath The absolute path to the XML file.
|
||||
* @return void
|
||||
*/
|
||||
protected function rewrite_step_backup_file_for_legacy_freeze($filepath) {
|
||||
$foundnode = false;
|
||||
$newfile = make_request_directory(true) . DIRECTORY_SEPARATOR . 'file.xml';
|
||||
$fr = fopen($filepath, 'r');
|
||||
$fw = fopen($newfile, 'w');
|
||||
if ($fr && $fw) {
|
||||
while (($line = fgets($fr, 4096)) !== false) {
|
||||
if (!$foundnode && strpos($line, '<gradebook ') === 0) {
|
||||
$foundnode = true;
|
||||
$matches = array();
|
||||
$pattern = '@calculations_freeze=.([0-9]+).@';
|
||||
if (preg_match($pattern, $line, $matches)) {
|
||||
$freeze = $matches[1];
|
||||
$line = preg_replace($pattern, '', $line);
|
||||
$line .= " <attributes>\n <calculations_freeze>$freeze</calculations_freeze>\n </attributes>\n";
|
||||
}
|
||||
}
|
||||
fputs($fw, $line);
|
||||
}
|
||||
if (!feof($fr)) {
|
||||
throw new restore_step_exception('Error while attempting to rewrite the gradebook step file.');
|
||||
}
|
||||
fclose($fr);
|
||||
fclose($fw);
|
||||
if (!rename($newfile, $filepath)) {
|
||||
throw new restore_step_exception('Error while attempting to rename the gradebook step file.');
|
||||
}
|
||||
} else {
|
||||
if ($fr) {
|
||||
fclose($fr);
|
||||
}
|
||||
if ($fw) {
|
||||
fclose($fw);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user