3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Defines various restore steps that will be used by common tasks in restore
21 * @package core_backup
24 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
31 * delete old directories and conditionally create backup_temp_ids table
33 class restore_create_and_clean_temp_stuff extends restore_execution_step {
35 protected function define_execution() {
36 $exists = restore_controller_dbops::create_restore_temp_tables($this->get_restoreid()); // temp tables conditionally
37 // If the table already exists, it's because restore_prechecks have been executed in the same
38 // request (without problems) and it already contains a bunch of preloaded information (users...)
39 // that we aren't going to execute again
40 if ($exists) { // Inform plan about preloaded information
41 $this->task->set_preloaded_information();
43 // Create the old-course-ctxid to new-course-ctxid mapping, we need that available since the beginning
44 $itemid = $this->task->get_old_contextid();
45 $newitemid = context_course::instance($this->get_courseid())->id;
46 restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
47 // Create the old-system-ctxid to new-system-ctxid mapping, we need that available since the beginning
48 $itemid = $this->task->get_old_system_contextid();
49 $newitemid = context_system::instance()->id;
50 restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
51 // Create the old-course-id to new-course-id mapping, we need that available since the beginning
52 $itemid = $this->task->get_old_courseid();
53 $newitemid = $this->get_courseid();
54 restore_dbops::set_backup_ids_record($this->get_restoreid(), 'course', $itemid, $newitemid);
60 * Drop temp ids table and delete the temp dir used by backup/restore (conditionally).
62 class restore_drop_and_clean_temp_stuff extends restore_execution_step {
64 protected function define_execution() {
66 restore_controller_dbops::drop_restore_temp_tables($this->get_restoreid()); // Drop ids temp table
67 if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally
68 $progress = $this->task->get_progress();
69 $progress->start_progress('Deleting backup dir');
70 backup_helper::delete_backup_dir($this->task->get_tempdir(), $progress); // Empty restore dir
71 $progress->end_progress();
77 * Restore calculated grade items, grade categories etc
79 class restore_gradebook_structure_step extends restore_structure_step {
82 * To conditionally decide if this step must be executed
83 * Note the "settings" conditions are evaluated in the
84 * corresponding task. Here we check for other conditions
85 * not being restore settings (files, site settings...)
87 protected function execute_condition() {
90 if ($this->get_courseid() == SITEID) {
94 // No gradebook info found, don't execute
95 $fullpath = $this->task->get_taskbasepath();
96 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
97 if (!file_exists($fullpath)) {
101 // Some module present in backup file isn't available to restore
102 // in this site, don't execute
103 if ($this->task->is_missing_modules()) {
107 // Some activity has been excluded to be restored, don't execute
108 if ($this->task->is_excluding_activities()) {
112 // There should only be one grade category (the 1 associated with the course itself)
113 // If other categories already exist we're restoring into an existing course.
114 // Restoring categories into a course with an existing category structure is unlikely to go well
115 $category = new stdclass();
116 $category->courseid = $this->get_courseid();
117 $catcount = $DB->count_records('grade_categories', (array)$category);
122 // Identify the backup we're dealing with.
123 $backuprelease = $this->get_task()->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10...
125 preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches);
126 if (!empty($matches[1])) {
127 $backupbuild = (int) $matches[1]; // The date of Moodle build at the time of the backup.
130 // On older versions the freeze value has to be converted.
131 // We do this from here as it is happening right before the file is read.
132 // This only targets the backup files that can contain the legacy freeze.
133 if ($backupbuild > 20150618 && (version_compare($backuprelease, '3.0', '<') || $backupbuild < 20160527)) {
134 $this->rewrite_step_backup_file_for_legacy_freeze($fullpath);
137 // Arrived here, execute the step
141 protected function define_structure() {
143 $userinfo = $this->task->get_setting_value('users');
145 $paths[] = new restore_path_element('attributes', '/gradebook/attributes');
146 $paths[] = new restore_path_element('grade_category', '/gradebook/grade_categories/grade_category');
148 $gradeitem = new restore_path_element('grade_item', '/gradebook/grade_items/grade_item');
149 $paths[] = $gradeitem;
150 $this->add_plugin_structure('local', $gradeitem);
153 $paths[] = new restore_path_element('grade_grade', '/gradebook/grade_items/grade_item/grade_grades/grade_grade');
155 $paths[] = new restore_path_element('grade_letter', '/gradebook/grade_letters/grade_letter');
156 $paths[] = new restore_path_element('grade_setting', '/gradebook/grade_settings/grade_setting');
161 protected function process_attributes($data) {
162 // For non-merge restore types:
163 // Unset 'gradebook_calculations_freeze_' in the course and replace with the one from the backup.
164 $target = $this->get_task()->get_target();
165 if ($target == backup::TARGET_CURRENT_DELETING || $target == backup::TARGET_EXISTING_DELETING) {
166 set_config('gradebook_calculations_freeze_' . $this->get_courseid(), null);
168 if (!empty($data['calculations_freeze'])) {
169 if ($target == backup::TARGET_NEW_COURSE || $target == backup::TARGET_CURRENT_DELETING ||
170 $target == backup::TARGET_EXISTING_DELETING) {
171 set_config('gradebook_calculations_freeze_' . $this->get_courseid(), $data['calculations_freeze']);
176 protected function process_grade_item($data) {
179 $data = (object)$data;
182 $data->course = $this->get_courseid();
184 $data->courseid = $this->get_courseid();
186 if ($data->itemtype=='manual') {
187 // manual grade items store category id in categoryid
188 $data->categoryid = $this->get_mappingid('grade_category', $data->categoryid, NULL);
189 // if mapping failed put in course's grade category
190 if (NULL == $data->categoryid) {
191 $coursecat = grade_category::fetch_course_category($this->get_courseid());
192 $data->categoryid = $coursecat->id;
194 } else if ($data->itemtype=='course') {
195 // course grade item stores their category id in iteminstance
196 $coursecat = grade_category::fetch_course_category($this->get_courseid());
197 $data->iteminstance = $coursecat->id;
198 } else if ($data->itemtype=='category') {
199 // category grade items store their category id in iteminstance
200 $data->iteminstance = $this->get_mappingid('grade_category', $data->iteminstance, NULL);
202 throw new restore_step_exception('unexpected_grade_item_type', $data->itemtype);
205 $data->scaleid = $this->get_mappingid('scale', $data->scaleid, NULL);
206 $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid, NULL);
208 $data->locktime = $this->apply_date_offset($data->locktime);
210 $coursecategory = $newitemid = null;
211 //course grade item should already exist so updating instead of inserting
212 if($data->itemtype=='course') {
213 //get the ID of the already created grade item
214 $gi = new stdclass();
215 $gi->courseid = $this->get_courseid();
216 $gi->itemtype = $data->itemtype;
218 //need to get the id of the grade_category that was automatically created for the course
219 $category = new stdclass();
220 $category->courseid = $this->get_courseid();
221 $category->parent = null;
222 //course category fullname starts out as ? but may be edited
223 //$category->fullname = '?';
224 $coursecategory = $DB->get_record('grade_categories', (array)$category);
225 $gi->iteminstance = $coursecategory->id;
227 $existinggradeitem = $DB->get_record('grade_items', (array)$gi);
228 if (!empty($existinggradeitem)) {
229 $data->id = $newitemid = $existinggradeitem->id;
230 $DB->update_record('grade_items', $data);
232 } else if ($data->itemtype == 'manual') {
233 // Manual items aren't assigned to a cm, so don't go duplicating them in the target if one exists.
235 'itemtype' => $data->itemtype,
236 'courseid' => $data->courseid,
237 'itemname' => $data->itemname,
238 'categoryid' => $data->categoryid,
240 $newitemid = $DB->get_field('grade_items', 'id', $gi);
243 if (empty($newitemid)) {
244 //in case we found the course category but still need to insert the course grade item
245 if ($data->itemtype=='course' && !empty($coursecategory)) {
246 $data->iteminstance = $coursecategory->id;
249 $newitemid = $DB->insert_record('grade_items', $data);
250 $data->id = $newitemid;
251 $gradeitem = new grade_item($data);
252 core\event\grade_item_created::create_from_grade_item($gradeitem)->trigger();
254 $this->set_mapping('grade_item', $oldid, $newitemid);
257 protected function process_grade_grade($data) {
260 $data = (object)$data;
262 $olduserid = $data->userid;
264 $data->itemid = $this->get_new_parentid('grade_item');
266 $data->userid = $this->get_mappingid('user', $data->userid, null);
267 if (!empty($data->userid)) {
268 $data->usermodified = $this->get_mappingid('user', $data->usermodified, null);
269 $data->locktime = $this->apply_date_offset($data->locktime);
271 $gradeexists = $DB->record_exists('grade_grades', array('userid' => $data->userid, 'itemid' => $data->itemid));
273 $message = "User id '{$data->userid}' already has a grade entry for grade item id '{$data->itemid}'";
274 $this->log($message, backup::LOG_DEBUG);
276 $newitemid = $DB->insert_record('grade_grades', $data);
277 $this->set_mapping('grade_grades', $oldid, $newitemid);
280 $message = "Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'";
281 $this->log($message, backup::LOG_DEBUG);
285 protected function process_grade_category($data) {
288 $data = (object)$data;
291 $data->course = $this->get_courseid();
292 $data->courseid = $data->course;
295 //no parent means a course level grade category. That may have been created when the course was created
296 if(empty($data->parent)) {
297 //parent was being saved as 0 when it should be null
298 $data->parent = null;
300 //get the already created course level grade category
301 $category = new stdclass();
302 $category->courseid = $this->get_courseid();
303 $category->parent = null;
305 $coursecategory = $DB->get_record('grade_categories', (array)$category);
306 if (!empty($coursecategory)) {
307 $data->id = $newitemid = $coursecategory->id;
308 $DB->update_record('grade_categories', $data);
312 // Add a warning about a removed setting.
313 if (!empty($data->aggregatesubcats)) {
314 set_config('show_aggregatesubcats_upgrade_' . $data->courseid, 1);
317 //need to insert a course category
318 if (empty($newitemid)) {
319 $newitemid = $DB->insert_record('grade_categories', $data);
321 $this->set_mapping('grade_category', $oldid, $newitemid);
323 protected function process_grade_letter($data) {
326 $data = (object)$data;
329 $data->contextid = context_course::instance($this->get_courseid())->id;
331 $gradeletter = (array)$data;
332 unset($gradeletter['id']);
333 if (!$DB->record_exists('grade_letters', $gradeletter)) {
334 $newitemid = $DB->insert_record('grade_letters', $data);
336 $newitemid = $data->id;
339 $this->set_mapping('grade_letter', $oldid, $newitemid);
341 protected function process_grade_setting($data) {
344 $data = (object)$data;
347 $data->courseid = $this->get_courseid();
349 $target = $this->get_task()->get_target();
350 if ($data->name == 'minmaxtouse' &&
351 ($target == backup::TARGET_CURRENT_ADDING || $target == backup::TARGET_EXISTING_ADDING)) {
352 // We never restore minmaxtouse during merge.
356 if (!$DB->record_exists('grade_settings', array('courseid' => $data->courseid, 'name' => $data->name))) {
357 $newitemid = $DB->insert_record('grade_settings', $data);
359 $newitemid = $data->id;
362 if (!empty($oldid)) {
363 // In rare cases (minmaxtouse), it is possible that there wasn't any ID associated with the setting.
364 $this->set_mapping('grade_setting', $oldid, $newitemid);
369 * put all activity grade items in the correct grade category and mark all for recalculation
371 protected function after_execute() {
375 'backupid' => $this->get_restoreid(),
376 'itemname' => 'grade_item'//,
377 //'itemid' => $itemid
379 $rs = $DB->get_recordset('backup_ids_temp', $conditions);
381 // We need this for calculation magic later on.
385 foreach($rs as $grade_item_backup) {
387 // Store the oldid with the new id.
388 $mappings[$grade_item_backup->itemid] = $grade_item_backup->newitemid;
390 $updateobj = new stdclass();
391 $updateobj->id = $grade_item_backup->newitemid;
393 //if this is an activity grade item that needs to be put back in its correct category
394 if (!empty($grade_item_backup->parentitemid)) {
395 $oldcategoryid = $this->get_mappingid('grade_category', $grade_item_backup->parentitemid, null);
396 if (!is_null($oldcategoryid)) {
397 $updateobj->categoryid = $oldcategoryid;
398 $DB->update_record('grade_items', $updateobj);
401 //mark course and category items as needing to be recalculated
402 $updateobj->needsupdate=1;
403 $DB->update_record('grade_items', $updateobj);
409 // We need to update the calculations for calculated grade items that may reference old
410 // grade item ids using ##gi\d+##.
411 // $mappings can be empty, use 0 if so (won't match ever)
412 list($sql, $params) = $DB->get_in_or_equal(array_values($mappings), SQL_PARAMS_NAMED, 'param', true, 0);
413 $sql = "SELECT gi.id, gi.calculation
414 FROM {grade_items} gi
415 WHERE gi.id {$sql} AND
416 calculation IS NOT NULL";
417 $rs = $DB->get_recordset_sql($sql, $params);
418 foreach ($rs as $gradeitem) {
419 // Collect all of the used grade item id references
420 if (preg_match_all('/##gi(\d+)##/', $gradeitem->calculation, $matches) < 1) {
421 // This calculation doesn't reference any other grade items... EASY!
424 // For this next bit we are going to do the replacement of id's in two steps:
425 // 1. We will replace all old id references with a special mapping reference.
426 // 2. We will replace all mapping references with id's
427 // Why do we do this?
428 // Because there potentially there will be an overlap of ids within the query and we
429 // we substitute the wrong id.. safest way around this is the two step system
430 $calculationmap = array();
432 foreach ($matches[1] as $match) {
433 // Check that the old id is known to us, if not it was broken to begin with and will
434 // continue to be broken.
435 if (!array_key_exists($match, $mappings)) {
438 // Our special mapping key
439 $mapping = '##MAPPING'.$mapcount.'##';
440 // The old id that exists within the calculation now
441 $oldid = '##gi'.$match.'##';
442 // The new id that we want to replace the old one with.
443 $newid = '##gi'.$mappings[$match].'##';
444 // Replace in the special mapping key
445 $gradeitem->calculation = str_replace($oldid, $mapping, $gradeitem->calculation);
446 // And record the mapping
447 $calculationmap[$mapping] = $newid;
450 // Iterate all special mappings for this calculation and replace in the new id's
451 foreach ($calculationmap as $mapping => $newid) {
452 $gradeitem->calculation = str_replace($mapping, $newid, $gradeitem->calculation);
454 // Update the calculation now that its being remapped
455 $DB->update_record('grade_items', $gradeitem);
459 // Need to correct the grade category path and parent
461 'courseid' => $this->get_courseid()
464 $rs = $DB->get_recordset('grade_categories', $conditions);
465 // Get all the parents correct first as grade_category::build_path() loads category parents from the DB
466 foreach ($rs as $gc) {
467 if (!empty($gc->parent)) {
468 $grade_category = new stdClass();
469 $grade_category->id = $gc->id;
470 $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent);
471 $DB->update_record('grade_categories', $grade_category);
476 // Now we can rebuild all the paths
477 $rs = $DB->get_recordset('grade_categories', $conditions);
478 foreach ($rs as $gc) {
479 $grade_category = new stdClass();
480 $grade_category->id = $gc->id;
481 $grade_category->path = grade_category::build_path($gc);
482 $grade_category->depth = substr_count($grade_category->path, '/') - 1;
483 $DB->update_record('grade_categories', $grade_category);
487 // Check what to do with the minmaxtouse setting.
488 $this->check_minmaxtouse();
490 // Freeze gradebook calculations if needed.
491 $this->gradebook_calculation_freeze();
493 // Ensure the module cache is current when recalculating grades.
494 rebuild_course_cache($this->get_courseid(), true);
496 // Restore marks items as needing update. Update everything now.
497 grade_regrade_final_grades($this->get_courseid());
501 * Freeze gradebook calculation if needed.
503 * This is similar to various upgrade scripts that check if the freeze is needed.
505 protected function gradebook_calculation_freeze() {
507 $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $this->get_courseid());
508 preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches);
509 $backupbuild = (int)$matches[1];
510 $backuprelease = $this->get_task()->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10...
512 // Extra credits need adjustments only for backups made between 2.8 release (20141110) and the fix release (20150619).
513 if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150619) {
514 require_once($CFG->libdir . '/db/upgradelib.php');
515 upgrade_extra_credit_weightoverride($this->get_courseid());
517 // Calculated grade items need recalculating for backups made between 2.8 release (20141110) and the fix release (20150627).
518 if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150627) {
519 require_once($CFG->libdir . '/db/upgradelib.php');
520 upgrade_calculated_grade_items($this->get_courseid());
522 // Courses from before 3.1 (20160518) may have a letter boundary problem and should be checked for this issue.
523 // Backups from before and including 2.9 could have a build number that is greater than 20160518 and should
524 // be checked for this problem.
525 if (!$gradebookcalculationsfreeze && ($backupbuild < 20160518 || version_compare($backuprelease, '2.9', '<='))) {
526 require_once($CFG->libdir . '/db/upgradelib.php');
527 upgrade_course_letter_boundary($this->get_courseid());
533 * Checks what should happen with the course grade setting minmaxtouse.
535 * This is related to the upgrade step at the time the setting was added.
540 protected function check_minmaxtouse() {
542 require_once($CFG->libdir . '/gradelib.php');
544 $userinfo = $this->task->get_setting_value('users');
545 $settingname = 'minmaxtouse';
546 $courseid = $this->get_courseid();
547 $minmaxtouse = $DB->get_field('grade_settings', 'value', array('courseid' => $courseid, 'name' => $settingname));
548 $version28start = 2014111000.00;
549 $version28last = 2014111006.05;
550 $version29start = 2015051100.00;
551 $version29last = 2015060400.02;
553 $target = $this->get_task()->get_target();
554 if ($minmaxtouse === false &&
555 ($target != backup::TARGET_CURRENT_ADDING && $target != backup::TARGET_EXISTING_ADDING)) {
556 // The setting was not found because this setting did not exist at the time the backup was made.
557 // And we are not restoring as merge, in which case we leave the course as it was.
558 $version = $this->get_task()->get_info()->moodle_version;
560 if ($version < $version28start) {
561 // We need to set it to use grade_item, but only if the site-wide setting is different. No need to notice them.
562 if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_ITEM) {
563 grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_ITEM);
566 } else if (($version >= $version28start && $version < $version28last) ||
567 ($version >= $version29start && $version < $version29last)) {
568 // They should be using grade_grade when the course has inconsistencies.
571 FROM {grade_items} gi
572 JOIN {grade_grades} gg
574 WHERE gi.courseid = ?
575 AND (gi.itemtype != ? AND gi.itemtype != ?)
576 AND (gg.rawgrademax != gi.grademax OR gg.rawgrademin != gi.grademin)";
578 // The course can only have inconsistencies when we restore the user info,
579 // we do not need to act on existing grades that were not restored as part of this backup.
580 if ($userinfo && $DB->record_exists_sql($sql, array($courseid, 'course', 'category'))) {
582 // Display the notice as we do during upgrade.
583 set_config('show_min_max_grades_changed_' . $courseid, 1);
585 if ($CFG->grade_minmaxtouse != GRADE_MIN_MAX_FROM_GRADE_GRADE) {
586 // We need set the setting as their site-wise setting is not GRADE_MIN_MAX_FROM_GRADE_GRADE.
587 // If they are using the site-wide grade_grade setting, we only want to notice them.
588 grade_set_setting($courseid, $settingname, GRADE_MIN_MAX_FROM_GRADE_GRADE);
593 // This should never happen because from now on minmaxtouse is always saved in backups.
599 * Rewrite step definition to handle the legacy freeze attribute.
601 * In previous backups the calculations_freeze property was stored as an attribute of the
602 * top level node <gradebook>. The backup API, however, do not process grandparent nodes.
603 * It only processes definitive children, and their parent attributes.
607 * <gradebook calculations_freeze="20160511">
609 * <grade_category id="10">
613 * </grade_categories>
617 * And this method will convert it to:
621 * <calculations_freeze>20160511</calculations_freeze>
624 * <grade_category id="10">
628 * </grade_categories>
632 * Note that we cannot just load the XML file in memory as it could potentially be huge.
633 * We can also completely ignore if the node <attributes> is already in the backup
634 * file as it never existed before.
636 * @param string $filepath The absolute path to the XML file.
639 protected function rewrite_step_backup_file_for_legacy_freeze($filepath) {
641 $newfile = make_request_directory(true) . DIRECTORY_SEPARATOR . 'file.xml';
642 $fr = fopen($filepath, 'r');
643 $fw = fopen($newfile, 'w');
645 while (($line = fgets($fr, 4096)) !== false) {
646 if (!$foundnode && strpos($line, '<gradebook ') === 0) {
649 $pattern = '@calculations_freeze=.([0-9]+).@';
650 if (preg_match($pattern, $line, $matches)) {
651 $freeze = $matches[1];
652 $line = preg_replace($pattern, '', $line);
653 $line .= " <attributes>\n <calculations_freeze>$freeze</calculations_freeze>\n </attributes>\n";
659 throw new restore_step_exception('Error while attempting to rewrite the gradebook step file.');
663 if (!rename($newfile, $filepath)) {
664 throw new restore_step_exception('Error while attempting to rename the gradebook step file.');
679 * Step in charge of restoring the grade history of a course.
681 * The execution conditions are itendical to {@link restore_gradebook_structure_step} because
682 * we do not want to restore the history if the gradebook and its content has not been
683 * restored. At least for now.
685 class restore_grade_history_structure_step extends restore_structure_step {
687 protected function execute_condition() {
690 if ($this->get_courseid() == SITEID) {
694 // No gradebook info found, don't execute.
695 $fullpath = $this->task->get_taskbasepath();
696 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
697 if (!file_exists($fullpath)) {
701 // Some module present in backup file isn't available to restore in this site, don't execute.
702 if ($this->task->is_missing_modules()) {
706 // Some activity has been excluded to be restored, don't execute.
707 if ($this->task->is_excluding_activities()) {
711 // There should only be one grade category (the 1 associated with the course itself).
712 $category = new stdclass();
713 $category->courseid = $this->get_courseid();
714 $catcount = $DB->count_records('grade_categories', (array)$category);
719 // Arrived here, execute the step.
723 protected function define_structure() {
727 $userinfo = $this->get_setting_value('users');
728 $history = $this->get_setting_value('grade_histories');
730 if ($userinfo && $history) {
731 $paths[] = new restore_path_element('grade_grade',
732 '/grade_history/grade_grades/grade_grade');
738 protected function process_grade_grade($data) {
741 $data = (object)($data);
742 $olduserid = $data->userid;
745 $data->userid = $this->get_mappingid('user', $data->userid, null);
746 if (!empty($data->userid)) {
747 // Do not apply the date offsets as this is history.
748 $data->itemid = $this->get_mappingid('grade_item', $data->itemid);
749 $data->oldid = $this->get_mappingid('grade_grades', $data->oldid);
750 $data->usermodified = $this->get_mappingid('user', $data->usermodified, null);
751 $data->rawscaleid = $this->get_mappingid('scale', $data->rawscaleid);
752 $DB->insert_record('grade_grades_history', $data);
754 $message = "Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'";
755 $this->log($message, backup::LOG_DEBUG);
762 * decode all the interlinks present in restored content
763 * relying 100% in the restore_decode_processor that handles
764 * both the contents to modify and the rules to be applied
766 class restore_decode_interlinks extends restore_execution_step {
768 protected function define_execution() {
769 // Get the decoder (from the plan)
770 $decoder = $this->task->get_decoder();
771 restore_decode_processor::register_link_decoders($decoder); // Add decoder contents and rules
772 // And launch it, everything will be processed
778 * first, ensure that we have no gaps in section numbers
779 * and then, rebuid the course cache
781 class restore_rebuild_course_cache extends restore_execution_step {
783 protected function define_execution() {
786 // Although there is some sort of auto-recovery of missing sections
787 // present in course/formats... here we check that all the sections
788 // from 0 to MAX(section->section) exist, creating them if necessary
789 $maxsection = $DB->get_field('course_sections', 'MAX(section)', array('course' => $this->get_courseid()));
790 // Iterate over all sections
791 for ($i = 0; $i <= $maxsection; $i++) {
792 // If the section $i doesn't exist, create it
793 if (!$DB->record_exists('course_sections', array('course' => $this->get_courseid(), 'section' => $i))) {
795 'course' => $this->get_courseid(),
797 'timemodified' => time());
798 $DB->insert_record('course_sections', $sectionrec); // missing section created
802 // Rebuild cache now that all sections are in place
803 rebuild_course_cache($this->get_courseid());
804 cache_helper::purge_by_event('changesincourse');
805 cache_helper::purge_by_event('changesincoursecat');
810 * Review all the tasks having one after_restore method
811 * executing it to perform some final adjustments of information
812 * not available when the task was executed.
814 class restore_execute_after_restore extends restore_execution_step {
816 protected function define_execution() {
818 // Simply call to the execute_after_restore() method of the task
819 // that always is the restore_final_task
820 $this->task->launch_execute_after_restore();
826 * Review all the (pending) block positions in backup_ids, matching by
827 * contextid, creating positions as needed. This is executed by the
828 * final task, once all the contexts have been created
830 class restore_review_pending_block_positions extends restore_execution_step {
832 protected function define_execution() {
835 // Get all the block_position objects pending to match
836 $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'block_position');
837 $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid, info');
838 // Process block positions, creating them or accumulating for final step
839 foreach($rs as $posrec) {
840 // Get the complete position object out of the info field.
841 $position = backup_controller_dbops::decode_backup_temp_info($posrec->info);
842 // If position is for one already mapped (known) contextid
843 // process it now, creating the position, else nothing to
844 // do, position finally discarded
845 if ($newctx = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $position->contextid)) {
846 $position->contextid = $newctx->newitemid;
847 // Create the block position
848 $DB->insert_record('block_positions', $position);
857 * Updates the availability data for course modules and sections.
859 * Runs after the restore of all course modules, sections, and grade items has
860 * completed. This is necessary in order to update IDs that have changed during
863 * @package core_backup
864 * @copyright 2014 The Open University
865 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
867 class restore_update_availability extends restore_execution_step {
869 protected function define_execution() {
872 // Note: This code runs even if availability is disabled when restoring.
873 // That will ensure that if you later turn availability on for the site,
874 // there will be no incorrect IDs. (It doesn't take long if the restored
875 // data does not contain any availability information.)
877 // Get modinfo with all data after resetting cache.
878 rebuild_course_cache($this->get_courseid(), true);
879 $modinfo = get_fast_modinfo($this->get_courseid());
881 // Get the date offset for this restore.
882 $dateoffset = $this->apply_date_offset(1) - 1;
884 // Update all sections that were restored.
885 $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'course_section');
886 $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'newitemid');
887 $sectionsbyid = null;
888 foreach ($rs as $rec) {
889 if (is_null($sectionsbyid)) {
890 $sectionsbyid = array();
891 foreach ($modinfo->get_section_info_all() as $section) {
892 $sectionsbyid[$section->id] = $section;
895 if (!array_key_exists($rec->newitemid, $sectionsbyid)) {
896 // If the section was not fully restored for some reason
897 // (e.g. due to an earlier error), skip it.
898 $this->get_logger()->process('Section not fully restored: id ' .
899 $rec->newitemid, backup::LOG_WARNING);
902 $section = $sectionsbyid[$rec->newitemid];
903 if (!is_null($section->availability)) {
904 $info = new \core_availability\info_section($section);
905 $info->update_after_restore($this->get_restoreid(),
906 $this->get_courseid(), $this->get_logger(), $dateoffset, $this->task);
911 // Update all modules that were restored.
912 $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'course_module');
913 $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'newitemid');
914 foreach ($rs as $rec) {
915 if (!array_key_exists($rec->newitemid, $modinfo->cms)) {
916 // If the module was not fully restored for some reason
917 // (e.g. due to an earlier error), skip it.
918 $this->get_logger()->process('Module not fully restored: id ' .
919 $rec->newitemid, backup::LOG_WARNING);
922 $cm = $modinfo->get_cm($rec->newitemid);
923 if (!is_null($cm->availability)) {
924 $info = new \core_availability\info_module($cm);
925 $info->update_after_restore($this->get_restoreid(),
926 $this->get_courseid(), $this->get_logger(), $dateoffset, $this->task);
935 * Process legacy module availability records in backup_ids.
937 * Matches course modules and grade item id once all them have been already restored.
938 * Only if all matchings are satisfied the availability condition will be created.
939 * At the same time, it is required for the site to have that functionality enabled.
941 * This step is included only to handle legacy backups (2.6 and before). It does not
942 * do anything for newer backups.
944 * @copyright 2014 The Open University
945 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
947 class restore_process_course_modules_availability extends restore_execution_step {
949 protected function define_execution() {
952 // Site hasn't availability enabled
953 if (empty($CFG->enableavailability)) {
957 // Do both modules and sections.
958 foreach (array('module', 'section') as $table) {
959 // Get all the availability objects to process.
960 $params = array('backupid' => $this->get_restoreid(), 'itemname' => $table . '_availability');
961 $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid, info');
962 // Process availabilities, creating them if everything matches ok.
963 foreach ($rs as $availrec) {
964 $allmatchesok = true;
965 // Get the complete legacy availability object.
966 $availability = backup_controller_dbops::decode_backup_temp_info($availrec->info);
968 // Note: This code used to update IDs, but that is now handled by the
969 // current code (after restore) instead of this legacy code.
971 // Get showavailability option.
972 $thingid = ($table === 'module') ? $availability->coursemoduleid :
973 $availability->coursesectionid;
974 $showrec = restore_dbops::get_backup_ids_record($this->get_restoreid(),
975 $table . '_showavailability', $thingid);
977 // Should not happen.
978 throw new coding_exception('No matching showavailability record');
980 $show = $showrec->info->showavailability;
982 // The $availability object is now in the format used in the old
983 // system. Interpret this and convert to new system.
984 $currentvalue = $DB->get_field('course_' . $table . 's', 'availability',
985 array('id' => $thingid), MUST_EXIST);
986 $newvalue = \core_availability\info::add_legacy_availability_condition(
987 $currentvalue, $availability, $show);
988 $DB->set_field('course_' . $table . 's', 'availability', $newvalue,
989 array('id' => $thingid));
998 * Execution step that, *conditionally* (if there isn't preloaded information)
999 * will load the inforef files for all the included course/section/activity tasks
1000 * to backup_temp_ids. They will be stored with "xxxxref" as itemname
1002 class restore_load_included_inforef_records extends restore_execution_step {
1004 protected function define_execution() {
1006 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
1010 // Get all the included tasks
1011 $tasks = restore_dbops::get_included_tasks($this->get_restoreid());
1012 $progress = $this->task->get_progress();
1013 $progress->start_progress($this->get_name(), count($tasks));
1014 foreach ($tasks as $task) {
1015 // Load the inforef.xml file if exists
1016 $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
1017 if (file_exists($inforefpath)) {
1018 // Load each inforef file to temp_ids.
1019 restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $inforefpath, $progress);
1022 $progress->end_progress();
1027 * Execution step that will load all the needed files into backup_files_temp
1028 * - info: contains the whole original object (times, names...)
1029 * (all them being original ids as loaded from xml)
1031 class restore_load_included_files extends restore_structure_step {
1033 protected function define_structure() {
1035 $file = new restore_path_element('file', '/files/file');
1037 return array($file);
1041 * Process one <file> element from files.xml
1043 * @param array $data the element data
1045 public function process_file($data) {
1047 $data = (object)$data; // handy
1049 // load it if needed:
1050 // - it it is one of the annotated inforef files (course/section/activity/block)
1051 // - it is one "user", "group", "grouping", "grade", "question" or "qtype_xxxx" component file (that aren't sent to inforef ever)
1052 // TODO: qtype_xxx should be replaced by proper backup_qtype_plugin::get_components_and_fileareas() use,
1053 // but then we'll need to change it to load plugins itself (because this is executed too early in restore)
1054 $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id);
1055 $iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'badges' ||
1056 $data->component == 'grouping' || $data->component == 'grade' ||
1057 $data->component == 'question' || substr($data->component, 0, 5) == 'qtype');
1058 if ($isfileref || $iscomponent) {
1059 restore_dbops::set_backup_files_record($this->get_restoreid(), $data);
1065 * Execution step that, *conditionally* (if there isn't preloaded information),
1066 * will load all the needed roles to backup_temp_ids. They will be stored with
1067 * "role" itemname. Also it will perform one automatic mapping to roles existing
1068 * in the target site, based in permissions of the user performing the restore,
1069 * archetypes and other bits. At the end, each original role will have its associated
1070 * target role or 0 if it's going to be skipped. Note we wrap everything over one
1071 * restore_dbops method, as far as the same stuff is going to be also executed
1072 * by restore prechecks
1074 class restore_load_and_map_roles extends restore_execution_step {
1076 protected function define_execution() {
1077 if ($this->task->get_preloaded_information()) { // if info is already preloaded
1081 $file = $this->get_basepath() . '/roles.xml';
1082 // Load needed toles to temp_ids
1083 restore_dbops::load_roles_to_tempids($this->get_restoreid(), $file);
1085 // Process roles, mapping/skipping. Any error throws exception
1086 // Note we pass controller's info because it can contain role mapping information
1087 // about manual mappings performed by UI
1088 restore_dbops::process_included_roles($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite(), $this->task->get_info()->role_mappings);
1093 * Execution step that, *conditionally* (if there isn't preloaded information
1094 * and users have been selected in settings, will load all the needed users
1095 * to backup_temp_ids. They will be stored with "user" itemname and with
1096 * their original contextid as paremitemid
1098 class restore_load_included_users extends restore_execution_step {
1100 protected function define_execution() {
1102 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
1105 if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do
1108 $file = $this->get_basepath() . '/users.xml';
1109 // Load needed users to temp_ids.
1110 restore_dbops::load_users_to_tempids($this->get_restoreid(), $file, $this->task->get_progress());
1115 * Execution step that, *conditionally* (if there isn't preloaded information
1116 * and users have been selected in settings, will process all the needed users
1117 * in order to decide and perform any action with them (create / map / error)
1118 * Note: Any error will cause exception, as far as this is the same processing
1119 * than the one into restore prechecks (that should have stopped process earlier)
1121 class restore_process_included_users extends restore_execution_step {
1123 protected function define_execution() {
1125 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
1128 if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do
1131 restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(),
1132 $this->task->get_userid(), $this->task->is_samesite(), $this->task->get_progress());
1137 * Execution step that will create all the needed users as calculated
1138 * by @restore_process_included_users (those having newiteind = 0)
1140 class restore_create_included_users extends restore_execution_step {
1142 protected function define_execution() {
1144 restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(),
1145 $this->task->get_userid(), $this->task->get_progress());
1150 * Structure step that will create all the needed groups and groupings
1151 * by loading them from the groups.xml file performing the required matches.
1152 * Note group members only will be added if restoring user info
1154 class restore_groups_structure_step extends restore_structure_step {
1156 protected function define_structure() {
1158 $paths = array(); // Add paths here
1160 // Do not include group/groupings information if not requested.
1161 $groupinfo = $this->get_setting_value('groups');
1163 $paths[] = new restore_path_element('group', '/groups/group');
1164 $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
1165 $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
1170 // Processing functions go here
1171 public function process_group($data) {
1174 $data = (object)$data; // handy
1175 $data->courseid = $this->get_courseid();
1177 // Only allow the idnumber to be set if the user has permission and the idnumber is not already in use by
1178 // another a group in the same course
1179 $context = context_course::instance($data->courseid);
1180 if (isset($data->idnumber) and has_capability('moodle/course:changeidnumber', $context, $this->task->get_userid())) {
1181 if (groups_get_group_by_idnumber($data->courseid, $data->idnumber)) {
1182 unset($data->idnumber);
1185 unset($data->idnumber);
1188 $oldid = $data->id; // need this saved for later
1190 $restorefiles = false; // Only if we end creating the group
1192 // This is for backwards compatibility with old backups. If the backup data for a group contains a non-empty value of
1193 // hidepicture, then we'll exclude this group's picture from being restored.
1194 if (!empty($data->hidepicture)) {
1195 // Exclude the group picture from being restored if hidepicture is set to 1 in the backup data.
1196 unset($data->picture);
1199 // Search if the group already exists (by name & description) in the target course
1200 $description_clause = '';
1201 $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
1202 if (!empty($data->description)) {
1203 $description_clause = ' AND ' .
1204 $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':description');
1205 $params['description'] = $data->description;
1207 if (!$groupdb = $DB->get_record_sql("SELECT *
1209 WHERE courseid = :courseid
1210 AND name = :grname $description_clause", $params)) {
1211 // group doesn't exist, create
1212 $newitemid = $DB->insert_record('groups', $data);
1213 $restorefiles = true; // We'll restore the files
1215 // group exists, use it
1216 $newitemid = $groupdb->id;
1218 // Save the id mapping
1219 $this->set_mapping('group', $oldid, $newitemid, $restorefiles);
1221 // Add the related group picture file if it's available at this point.
1222 if (!empty($data->picture)) {
1223 $this->add_related_files('group', 'icon', 'group', null, $oldid);
1226 // Invalidate the course group data cache just in case.
1227 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
1230 public function process_grouping($data) {
1233 $data = (object)$data; // handy
1234 $data->courseid = $this->get_courseid();
1236 // Only allow the idnumber to be set if the user has permission and the idnumber is not already in use by
1237 // another a grouping in the same course
1238 $context = context_course::instance($data->courseid);
1239 if (isset($data->idnumber) and has_capability('moodle/course:changeidnumber', $context, $this->task->get_userid())) {
1240 if (groups_get_grouping_by_idnumber($data->courseid, $data->idnumber)) {
1241 unset($data->idnumber);
1244 unset($data->idnumber);
1247 $oldid = $data->id; // need this saved for later
1248 $restorefiles = false; // Only if we end creating the grouping
1250 // Search if the grouping already exists (by name & description) in the target course
1251 $description_clause = '';
1252 $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
1253 if (!empty($data->description)) {
1254 $description_clause = ' AND ' .
1255 $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':description');
1256 $params['description'] = $data->description;
1258 if (!$groupingdb = $DB->get_record_sql("SELECT *
1260 WHERE courseid = :courseid
1261 AND name = :grname $description_clause", $params)) {
1262 // grouping doesn't exist, create
1263 $newitemid = $DB->insert_record('groupings', $data);
1264 $restorefiles = true; // We'll restore the files
1266 // grouping exists, use it
1267 $newitemid = $groupingdb->id;
1269 // Save the id mapping
1270 $this->set_mapping('grouping', $oldid, $newitemid, $restorefiles);
1271 // Invalidate the course group data cache just in case.
1272 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
1275 public function process_grouping_group($data) {
1278 require_once($CFG->dirroot.'/group/lib.php');
1280 $data = (object)$data;
1281 groups_assign_grouping($this->get_new_parentid('grouping'), $this->get_mappingid('group', $data->groupid), $data->timeadded);
1284 protected function after_execute() {
1285 // Add group related files, matching with "group" mappings.
1286 $this->add_related_files('group', 'description', 'group');
1287 // Add grouping related files, matching with "grouping" mappings
1288 $this->add_related_files('grouping', 'description', 'grouping');
1289 // Invalidate the course group data.
1290 cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($this->get_courseid()));
1296 * Structure step that will create all the needed group memberships
1297 * by loading them from the groups.xml file performing the required matches.
1299 class restore_groups_members_structure_step extends restore_structure_step {
1301 protected $plugins = null;
1303 protected function define_structure() {
1305 $paths = array(); // Add paths here
1307 if ($this->get_setting_value('groups') && $this->get_setting_value('users')) {
1308 $paths[] = new restore_path_element('group', '/groups/group');
1309 $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
1315 public function process_group($data) {
1316 $data = (object)$data; // handy
1319 // Not much to do here, this groups mapping should be already done from restore_groups_structure_step.
1320 // Let's fake internal state to make $this->get_new_parentid('group') work.
1322 $this->set_mapping('group', $data->id, $this->get_mappingid('group', $data->id));
1325 public function process_member($data) {
1327 require_once("$CFG->dirroot/group/lib.php");
1329 // NOTE: Always use groups_add_member() because it triggers events and verifies if user is enrolled.
1331 $data = (object)$data; // handy
1333 // get parent group->id
1334 $data->groupid = $this->get_new_parentid('group');
1336 // map user newitemid and insert if not member already
1337 if ($data->userid = $this->get_mappingid('user', $data->userid)) {
1338 if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
1339 // Check the component, if any, exists.
1340 if (empty($data->component)) {
1341 groups_add_member($data->groupid, $data->userid);
1343 } else if ((strpos($data->component, 'enrol_') === 0)) {
1344 // Deal with enrolment groups - ignore the component and just find out the instance via new id,
1345 // it is possible that enrolment was restored using different plugin type.
1346 if (!isset($this->plugins)) {
1347 $this->plugins = enrol_get_plugins(true);
1349 if ($enrolid = $this->get_mappingid('enrol', $data->itemid)) {
1350 if ($instance = $DB->get_record('enrol', array('id'=>$enrolid))) {
1351 if (isset($this->plugins[$instance->enrol])) {
1352 $this->plugins[$instance->enrol]->restore_group_member($instance, $data->groupid, $data->userid);
1358 $dir = core_component::get_component_directory($data->component);
1359 if ($dir and is_dir($dir)) {
1360 if (component_callback($data->component, 'restore_group_member', array($this, $data), true)) {
1364 // Bad luck, plugin could not restore the data, let's add normal membership.
1365 groups_add_member($data->groupid, $data->userid);
1366 $message = "Restore of '$data->component/$data->itemid' group membership is not supported, using standard group membership instead.";
1367 $this->log($message, backup::LOG_WARNING);
1375 * Structure step that will create all the needed scales
1376 * by loading them from the scales.xml
1378 class restore_scales_structure_step extends restore_structure_step {
1380 protected function define_structure() {
1382 $paths = array(); // Add paths here
1383 $paths[] = new restore_path_element('scale', '/scales_definition/scale');
1387 protected function process_scale($data) {
1390 $data = (object)$data;
1392 $restorefiles = false; // Only if we end creating the group
1394 $oldid = $data->id; // need this saved for later
1396 // Look for scale (by 'scale' both in standard (course=0) and current course
1397 // with priority to standard scales (ORDER clause)
1398 // scale is not course unique, use get_record_sql to suppress warning
1399 // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides
1400 $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc');
1401 $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale);
1402 if (!$scadb = $DB->get_record_sql("SELECT *
1404 WHERE courseid IN (0, :courseid)
1405 AND $compare_scale_clause
1406 ORDER BY courseid", $params, IGNORE_MULTIPLE)) {
1407 // Remap the user if possible, defaut to user performing the restore if not
1408 $userid = $this->get_mappingid('user', $data->userid);
1409 $data->userid = $userid ? $userid : $this->task->get_userid();
1410 // Remap the course if course scale
1411 $data->courseid = $data->courseid ? $this->get_courseid() : 0;
1412 // If global scale (course=0), check the user has perms to create it
1413 // falling to course scale if not
1414 $systemctx = context_system::instance();
1415 if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $this->task->get_userid())) {
1416 $data->courseid = $this->get_courseid();
1418 // scale doesn't exist, create
1419 $newitemid = $DB->insert_record('scale', $data);
1420 $restorefiles = true; // We'll restore the files
1422 // scale exists, use it
1423 $newitemid = $scadb->id;
1425 // Save the id mapping (with files support at system context)
1426 $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid());
1429 protected function after_execute() {
1430 // Add scales related files, matching with "scale" mappings
1431 $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid());
1437 * Structure step that will create all the needed outocomes
1438 * by loading them from the outcomes.xml
1440 class restore_outcomes_structure_step extends restore_structure_step {
1442 protected function define_structure() {
1444 $paths = array(); // Add paths here
1445 $paths[] = new restore_path_element('outcome', '/outcomes_definition/outcome');
1449 protected function process_outcome($data) {
1452 $data = (object)$data;
1454 $restorefiles = false; // Only if we end creating the group
1456 $oldid = $data->id; // need this saved for later
1458 // Look for outcome (by shortname both in standard (courseid=null) and current course
1459 // with priority to standard outcomes (ORDER clause)
1460 // outcome is not course unique, use get_record_sql to suppress warning
1461 $params = array('courseid' => $this->get_courseid(), 'shortname' => $data->shortname);
1462 if (!$outdb = $DB->get_record_sql('SELECT *
1463 FROM {grade_outcomes}
1464 WHERE shortname = :shortname
1465 AND (courseid = :courseid OR courseid IS NULL)
1466 ORDER BY COALESCE(courseid, 0)', $params, IGNORE_MULTIPLE)) {
1468 $userid = $this->get_mappingid('user', $data->usermodified);
1469 $data->usermodified = $userid ? $userid : $this->task->get_userid();
1471 $data->scaleid = $this->get_mappingid('scale', $data->scaleid);
1472 // Remap the course if course outcome
1473 $data->courseid = $data->courseid ? $this->get_courseid() : null;
1474 // If global outcome (course=null), check the user has perms to create it
1475 // falling to course outcome if not
1476 $systemctx = context_system::instance();
1477 if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $this->task->get_userid())) {
1478 $data->courseid = $this->get_courseid();
1480 // outcome doesn't exist, create
1481 $newitemid = $DB->insert_record('grade_outcomes', $data);
1482 $restorefiles = true; // We'll restore the files
1484 // scale exists, use it
1485 $newitemid = $outdb->id;
1487 // Set the corresponding grade_outcomes_courses record
1488 $outcourserec = new stdclass();
1489 $outcourserec->courseid = $this->get_courseid();
1490 $outcourserec->outcomeid = $newitemid;
1491 if (!$DB->record_exists('grade_outcomes_courses', (array)$outcourserec)) {
1492 $DB->insert_record('grade_outcomes_courses', $outcourserec);
1494 // Save the id mapping (with files support at system context)
1495 $this->set_mapping('outcome', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid());
1498 protected function after_execute() {
1499 // Add outcomes related files, matching with "outcome" mappings
1500 $this->add_related_files('grade', 'outcome', 'outcome', $this->task->get_old_system_contextid());
1505 * Execution step that, *conditionally* (if there isn't preloaded information
1506 * will load all the question categories and questions (header info only)
1507 * to backup_temp_ids. They will be stored with "question_category" and
1508 * "question" itemnames and with their original contextid and question category
1509 * id as paremitemids
1511 class restore_load_categories_and_questions extends restore_execution_step {
1513 protected function define_execution() {
1515 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
1518 $file = $this->get_basepath() . '/questions.xml';
1519 restore_dbops::load_categories_and_questions_to_tempids($this->get_restoreid(), $file);
1524 * Execution step that, *conditionally* (if there isn't preloaded information)
1525 * will process all the needed categories and questions
1526 * in order to decide and perform any action with them (create / map / error)
1527 * Note: Any error will cause exception, as far as this is the same processing
1528 * than the one into restore prechecks (that should have stopped process earlier)
1530 class restore_process_categories_and_questions extends restore_execution_step {
1532 protected function define_execution() {
1534 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
1537 restore_dbops::process_categories_and_questions($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite());
1542 * Structure step that will read the section.xml creating/updating sections
1543 * as needed, rebuilding course cache and other friends
1545 class restore_section_structure_step extends restore_structure_step {
1546 /** @var array Cache: Array of id => course format */
1547 private static $courseformats = array();
1550 * Resets a static cache of course formats. Required for unit testing.
1552 public static function reset_caches() {
1553 self::$courseformats = array();
1556 protected function define_structure() {
1561 $section = new restore_path_element('section', '/section');
1562 $paths[] = $section;
1563 if ($CFG->enableavailability) {
1564 $paths[] = new restore_path_element('availability', '/section/availability');
1565 $paths[] = new restore_path_element('availability_field', '/section/availability_field');
1567 $paths[] = new restore_path_element('course_format_options', '/section/course_format_options');
1569 // Apply for 'format' plugins optional paths at section level
1570 $this->add_plugin_structure('format', $section);
1572 // Apply for 'local' plugins optional paths at section level
1573 $this->add_plugin_structure('local', $section);
1578 public function process_section($data) {
1580 $data = (object)$data;
1581 $oldid = $data->id; // We'll need this later
1583 $restorefiles = false;
1585 // Look for the section
1586 $section = new stdclass();
1587 $section->course = $this->get_courseid();
1588 $section->section = $data->number;
1589 $section->timemodified = $data->timemodified ?? 0;
1590 // Section doesn't exist, create it with all the info from backup
1591 if (!$secrec = $DB->get_record('course_sections', ['course' => $this->get_courseid(), 'section' => $data->number])) {
1592 $section->name = $data->name;
1593 $section->summary = $data->summary;
1594 $section->summaryformat = $data->summaryformat;
1595 $section->sequence = '';
1596 $section->visible = $data->visible;
1597 if (empty($CFG->enableavailability)) { // Process availability information only if enabled.
1598 $section->availability = null;
1600 $section->availability = isset($data->availabilityjson) ? $data->availabilityjson : null;
1601 // Include legacy [<2.7] availability data if provided.
1602 if (is_null($section->availability)) {
1603 $section->availability = \core_availability\info::convert_legacy_fields(
1607 $newitemid = $DB->insert_record('course_sections', $section);
1608 $section->id = $newitemid;
1610 core\event\course_section_created::create_from_section($section)->trigger();
1612 $restorefiles = true;
1614 // Section exists, update non-empty information
1616 $section->id = $secrec->id;
1617 if ((string)$secrec->name === '') {
1618 $section->name = $data->name;
1620 if (empty($secrec->summary)) {
1621 $section->summary = $data->summary;
1622 $section->summaryformat = $data->summaryformat;
1623 $restorefiles = true;
1626 // Don't update availability (I didn't see a useful way to define
1627 // whether existing or new one should take precedence).
1629 $DB->update_record('course_sections', $section);
1630 $newitemid = $secrec->id;
1632 // Trigger an event for course section update.
1633 $event = \core\event\course_section_updated::create(
1635 'objectid' => $section->id,
1636 'courseid' => $section->course,
1637 'context' => context_course::instance($section->course),
1638 'other' => array('sectionnum' => $section->section)
1644 // Annotate the section mapping, with restorefiles option if needed
1645 $this->set_mapping('course_section', $oldid, $newitemid, $restorefiles);
1647 // set the new course_section id in the task
1648 $this->task->set_sectionid($newitemid);
1650 // If there is the legacy showavailability data, store this for later use.
1651 // (This data is not present when restoring 'new' backups.)
1652 if (isset($data->showavailability)) {
1653 // Cache the showavailability flag using the backup_ids data field.
1654 restore_dbops::set_backup_ids_record($this->get_restoreid(),
1655 'section_showavailability', $newitemid, 0, null,
1656 (object)array('showavailability' => $data->showavailability));
1659 // Commented out. We never modify course->numsections as far as that is used
1660 // by a lot of people to "hide" sections on purpose (so this remains as used to be in Moodle 1.x)
1661 // Note: We keep the code here, to know about and because of the possibility of making this
1662 // optional based on some setting/attribute in the future
1663 // If needed, adjust course->numsections
1664 //if ($numsections = $DB->get_field('course', 'numsections', array('id' => $this->get_courseid()))) {
1665 // if ($numsections < $section->section) {
1666 // $DB->set_field('course', 'numsections', $section->section, array('id' => $this->get_courseid()));
1672 * Process the legacy availability table record. This table does not exist
1673 * in Moodle 2.7+ but we still support restore.
1675 * @param stdClass $data Record data
1677 public function process_availability($data) {
1678 $data = (object)$data;
1679 // Simply going to store the whole availability record now, we'll process
1680 // all them later in the final task (once all activities have been restored)
1681 // Let's call the low level one to be able to store the whole object.
1682 $data->coursesectionid = $this->task->get_sectionid();
1683 restore_dbops::set_backup_ids_record($this->get_restoreid(),
1684 'section_availability', $data->id, 0, null, $data);
1688 * Process the legacy availability fields table record. This table does not
1689 * exist in Moodle 2.7+ but we still support restore.
1691 * @param stdClass $data Record data
1693 public function process_availability_field($data) {
1695 $data = (object)$data;
1696 // Mark it is as passed by default
1698 $customfieldid = null;
1700 // If a customfield has been used in order to pass we must be able to match an existing
1701 // customfield by name (data->customfield) and type (data->customfieldtype)
1702 if (is_null($data->customfield) xor is_null($data->customfieldtype)) {
1703 // xor is sort of uncommon. If either customfield is null or customfieldtype is null BUT not both.
1704 // If one is null but the other isn't something clearly went wrong and we'll skip this condition.
1706 } else if (!is_null($data->customfield)) {
1707 $params = array('shortname' => $data->customfield, 'datatype' => $data->customfieldtype);
1708 $customfieldid = $DB->get_field('user_info_field', 'id', $params);
1709 $passed = ($customfieldid !== false);
1713 // Create the object to insert into the database
1714 $availfield = new stdClass();
1715 $availfield->coursesectionid = $this->task->get_sectionid();
1716 $availfield->userfield = $data->userfield;
1717 $availfield->customfieldid = $customfieldid;
1718 $availfield->operator = $data->operator;
1719 $availfield->value = $data->value;
1721 // Get showavailability option.
1722 $showrec = restore_dbops::get_backup_ids_record($this->get_restoreid(),
1723 'section_showavailability', $availfield->coursesectionid);
1725 // Should not happen.
1726 throw new coding_exception('No matching showavailability record');
1728 $show = $showrec->info->showavailability;
1730 // The $availfield object is now in the format used in the old
1731 // system. Interpret this and convert to new system.
1732 $currentvalue = $DB->get_field('course_sections', 'availability',
1733 array('id' => $availfield->coursesectionid), MUST_EXIST);
1734 $newvalue = \core_availability\info::add_legacy_availability_field_condition(
1735 $currentvalue, $availfield, $show);
1737 $section = new stdClass();
1738 $section->id = $availfield->coursesectionid;
1739 $section->availability = $newvalue;
1740 $section->timemodified = time();
1741 $DB->update_record('course_sections', $section);
1745 public function process_course_format_options($data) {
1747 $courseid = $this->get_courseid();
1748 if (!array_key_exists($courseid, self::$courseformats)) {
1749 // It is safe to have a static cache of course formats because format can not be changed after this point.
1750 self::$courseformats[$courseid] = $DB->get_field('course', 'format', array('id' => $courseid));
1752 $data = (array)$data;
1753 if (self::$courseformats[$courseid] === $data['format']) {
1754 // Import section format options only if both courses (the one that was backed up
1755 // and the one we are restoring into) have same formats.
1757 'courseid' => $this->get_courseid(),
1758 'sectionid' => $this->task->get_sectionid(),
1759 'format' => $data['format'],
1760 'name' => $data['name']
1762 if ($record = $DB->get_record('course_format_options', $params, 'id, value')) {
1763 // Do not overwrite existing information.
1764 $newid = $record->id;
1766 $params['value'] = $data['value'];
1767 $newid = $DB->insert_record('course_format_options', $params);
1769 $this->set_mapping('course_format_options', $data['id'], $newid);
1773 protected function after_execute() {
1774 // Add section related files, with 'course_section' itemid to match
1775 $this->add_related_files('course', 'section', 'course_section');
1780 * Structure step that will read the course.xml file, loading it and performing
1781 * various actions depending of the site/restore settings. Note that target
1782 * course always exist before arriving here so this step will be updating
1783 * the course record (never inserting)
1785 class restore_course_structure_step extends restore_structure_step {
1787 * @var bool this gets set to true by {@link process_course()} if we are
1788 * restoring an old coures that used the legacy 'module security' feature.
1789 * If so, we have to do more work in {@link after_execute()}.
1791 protected $legacyrestrictmodules = false;
1794 * @var array Used when {@link $legacyrestrictmodules} is true. This is an
1795 * array with array keys the module names ('forum', 'quiz', etc.). These are
1796 * the modules that are allowed according to the data in the backup file.
1797 * In {@link after_execute()} we then have to prevent adding of all the other
1798 * types of activity.
1800 protected $legacyallowedmodules = array();
1802 protected function define_structure() {
1804 $course = new restore_path_element('course', '/course');
1805 $category = new restore_path_element('category', '/course/category');
1806 $tag = new restore_path_element('tag', '/course/tags/tag');
1807 $customfield = new restore_path_element('customfield', '/course/customfields/customfield');
1808 $allowed_module = new restore_path_element('allowed_module', '/course/allowed_modules/module');
1810 // Apply for 'format' plugins optional paths at course level
1811 $this->add_plugin_structure('format', $course);
1813 // Apply for 'theme' plugins optional paths at course level
1814 $this->add_plugin_structure('theme', $course);
1816 // Apply for 'report' plugins optional paths at course level
1817 $this->add_plugin_structure('report', $course);
1819 // Apply for 'course report' plugins optional paths at course level
1820 $this->add_plugin_structure('coursereport', $course);
1822 // Apply for plagiarism plugins optional paths at course level
1823 $this->add_plugin_structure('plagiarism', $course);
1825 // Apply for local plugins optional paths at course level
1826 $this->add_plugin_structure('local', $course);
1828 // Apply for admin tool plugins optional paths at course level.
1829 $this->add_plugin_structure('tool', $course);
1831 return array($course, $category, $tag, $customfield, $allowed_module);
1835 * Processing functions go here
1837 * @global moodledatabase $DB
1838 * @param stdClass $data
1840 public function process_course($data) {
1842 $context = context::instance_by_id($this->task->get_contextid());
1843 $userid = $this->task->get_userid();
1844 $target = $this->get_task()->get_target();
1845 $isnewcourse = $target == backup::TARGET_NEW_COURSE;
1847 // When restoring to a new course we can set all the things except for the ID number.
1848 $canchangeidnumber = $isnewcourse || has_capability('moodle/course:changeidnumber', $context, $userid);
1849 $canchangesummary = $isnewcourse || has_capability('moodle/course:changesummary', $context, $userid);
1850 $canforcelanguage = has_capability('moodle/course:setforcedlanguage', $context, $userid);
1852 $data = (object)$data;
1853 $data->id = $this->get_courseid();
1855 // Calculate final course names, to avoid dupes.
1856 $fullname = $this->get_setting_value('course_fullname');
1857 $shortname = $this->get_setting_value('course_shortname');
1858 list($data->fullname, $data->shortname) = restore_dbops::calculate_course_names($this->get_courseid(),
1859 $fullname === false ? $data->fullname : $fullname,
1860 $shortname === false ? $data->shortname : $shortname);
1861 // Do not modify the course names at all when merging and user selected to keep the names (or prohibited by cap).
1862 if (!$isnewcourse && $fullname === false) {
1863 unset($data->fullname);
1865 if (!$isnewcourse && $shortname === false) {
1866 unset($data->shortname);
1869 // Unset summary if user can't change it.
1870 if (!$canchangesummary) {
1871 unset($data->summary);
1872 unset($data->summaryformat);
1875 // Unset lang if user can't change it.
1876 if (!$canforcelanguage) {
1880 // Only allow the idnumber to be set if the user has permission and the idnumber is not already in use by
1881 // another course on this site.
1882 if (!empty($data->idnumber) && $canchangeidnumber && $this->task->is_samesite()
1883 && !$DB->record_exists('course', array('idnumber' => $data->idnumber))) {
1884 // Do not reset idnumber.
1886 } else if (!$isnewcourse) {
1887 // Prevent override when restoring as merge.
1888 unset($data->idnumber);
1891 $data->idnumber = '';
1894 // If we restore a course from this site, let's capture the original course id.
1895 if ($isnewcourse && $this->get_task()->is_samesite()) {
1896 $data->originalcourseid = $this->get_task()->get_old_courseid();
1899 // Any empty value for course->hiddensections will lead to 0 (default, show collapsed).
1900 // It has been reported that some old 1.9 courses may have it null leading to DB error. MDL-31532
1901 if (empty($data->hiddensections)) {
1902 $data->hiddensections = 0;
1905 // Set legacyrestrictmodules to true if the course was resticting modules. If so
1906 // then we will need to process restricted modules after execution.
1907 $this->legacyrestrictmodules = !empty($data->restrictmodules);
1909 $data->startdate= $this->apply_date_offset($data->startdate);
1910 if (isset($data->enddate)) {
1911 $data->enddate = $this->apply_date_offset($data->enddate);
1914 if ($data->defaultgroupingid) {
1915 $data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid);
1917 if (empty($CFG->enablecompletion)) {
1918 $data->enablecompletion = 0;
1919 $data->completionstartonenrol = 0;
1920 $data->completionnotify = 0;
1922 $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search
1923 if (isset($data->lang) && !array_key_exists($data->lang, $languages)) {
1927 $themes = get_list_of_themes(); // Get themes for quick search later
1928 if (!array_key_exists($data->theme, $themes) || empty($CFG->allowcoursethemes)) {
1932 // Check if this is an old SCORM course format.
1933 if ($data->format == 'scorm') {
1934 $data->format = 'singleactivity';
1935 $data->activitytype = 'scorm';
1938 // Course record ready, update it
1939 $DB->update_record('course', $data);
1941 course_get_format($data)->update_course_format_options($data);
1943 // Role name aliases
1944 restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid());
1947 public function process_category($data) {
1948 // Nothing to do with the category. UI sets it before restore starts
1951 public function process_tag($data) {
1954 $data = (object)$data;
1956 core_tag_tag::add_item_tag('core', 'course', $this->get_courseid(),
1957 context_course::instance($this->get_courseid()), $data->rawname);
1961 * Process custom fields
1963 * @param array $data
1965 public function process_customfield($data) {
1966 $handler = core_course\customfield\course_handler::create();
1967 $handler->restore_instance_data_from_backup($this->task, $data);
1970 public function process_allowed_module($data) {
1971 $data = (object)$data;
1973 // Backwards compatiblity support for the data that used to be in the
1974 // course_allowed_modules table.
1975 if ($this->legacyrestrictmodules) {
1976 $this->legacyallowedmodules[$data->modulename] = 1;
1980 protected function after_execute() {
1983 // Add course related files, without itemid to match
1984 $this->add_related_files('course', 'summary', null);
1985 $this->add_related_files('course', 'overviewfiles', null);
1987 // Deal with legacy allowed modules.
1988 if ($this->legacyrestrictmodules) {
1989 $context = context_course::instance($this->get_courseid());
1991 list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
1992 list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
1993 foreach ($managerroleids as $roleid) {
1994 unset($roleids[$roleid]);
1997 foreach (core_component::get_plugin_list('mod') as $modname => $notused) {
1998 if (isset($this->legacyallowedmodules[$modname])) {
1999 // Module is allowed, no worries.
2003 $capability = 'mod/' . $modname . ':addinstance';
2005 if (!get_capability_info($capability)) {
2006 $this->log("Capability '{$capability}' was not found!", backup::LOG_WARNING);
2010 foreach ($roleids as $roleid) {
2011 assign_capability($capability, CAP_PREVENT, $roleid, $context);
2019 * Execution step that will migrate legacy files if present.
2021 class restore_course_legacy_files_step extends restore_execution_step {
2022 public function define_execution() {
2025 // Do a check for legacy files and skip if there are none.
2026 $sql = 'SELECT count(*)
2027 FROM {backup_files_temp}
2032 $params = array($this->get_restoreid(), $this->task->get_old_contextid(), 'course', 'legacy');
2034 if ($DB->count_records_sql($sql, $params)) {
2035 $DB->set_field('course', 'legacyfiles', 2, array('id' => $this->get_courseid()));
2036 restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'course',
2037 'legacy', $this->task->get_old_contextid(), $this->task->get_userid());
2043 * Structure step that will read the roles.xml file (at course/activity/block levels)
2044 * containing all the role_assignments and overrides for that context. If corresponding to
2045 * one mapped role, they will be applied to target context. Will observe the role_assignments
2046 * setting to decide if ras are restored.
2048 * Note: this needs to be executed after all users are enrolled.
2050 class restore_ras_and_caps_structure_step extends restore_structure_step {
2051 protected $plugins = null;
2053 protected function define_structure() {
2057 // Observe the role_assignments setting
2058 if ($this->get_setting_value('role_assignments')) {
2059 $paths[] = new restore_path_element('assignment', '/roles/role_assignments/assignment');
2061 if ($this->get_setting_value('permissions')) {
2062 $paths[] = new restore_path_element('override', '/roles/role_overrides/override');
2071 * This has to be called after enrolments processing.
2073 * @param mixed $data
2076 public function process_assignment($data) {
2079 $data = (object)$data;
2081 // Check roleid, userid are one of the mapped ones
2082 if (!$newroleid = $this->get_mappingid('role', $data->roleid)) {
2085 if (!$newuserid = $this->get_mappingid('user', $data->userid)) {
2088 if (!$DB->record_exists('user', array('id' => $newuserid, 'deleted' => 0))) {
2089 // Only assign roles to not deleted users
2092 if (!$contextid = $this->task->get_contextid()) {
2096 if (empty($data->component)) {
2097 // assign standard manual roles
2098 // TODO: role_assign() needs one userid param to be able to specify our restore userid
2099 role_assign($newroleid, $newuserid, $contextid);
2101 } else if ((strpos($data->component, 'enrol_') === 0)) {
2102 // Deal with enrolment roles - ignore the component and just find out the instance via new id,
2103 // it is possible that enrolment was restored using different plugin type.
2104 if (!isset($this->plugins)) {
2105 $this->plugins = enrol_get_plugins(true);
2107 if ($enrolid = $this->get_mappingid('enrol', $data->itemid)) {
2108 if ($instance = $DB->get_record('enrol', array('id'=>$enrolid))) {
2109 if (isset($this->plugins[$instance->enrol])) {
2110 $this->plugins[$instance->enrol]->restore_role_assignment($instance, $newroleid, $newuserid, $contextid);
2116 $data->roleid = $newroleid;
2117 $data->userid = $newuserid;
2118 $data->contextid = $contextid;
2119 $dir = core_component::get_component_directory($data->component);
2120 if ($dir and is_dir($dir)) {
2121 if (component_callback($data->component, 'restore_role_assignment', array($this, $data), true)) {
2125 // Bad luck, plugin could not restore the data, let's add normal membership.
2126 role_assign($data->roleid, $data->userid, $data->contextid);
2127 $message = "Restore of '$data->component/$data->itemid' role assignments is not supported, using manual role assignments instead.";
2128 $this->log($message, backup::LOG_WARNING);
2132 public function process_override($data) {
2133 $data = (object)$data;
2135 // Check roleid is one of the mapped ones
2136 $newrole = $this->get_mapping('role', $data->roleid);
2137 $newroleid = $newrole->newitemid ?? false;
2138 $userid = $this->task->get_userid();
2140 // If newroleid and context are valid assign it via API (it handles dupes and so on)
2141 if ($newroleid && $this->task->get_contextid()) {
2142 if (!$capability = get_capability_info($data->capability)) {
2143 $this->log("Capability '{$data->capability}' was not found!", backup::LOG_WARNING);
2145 $context = context::instance_by_id($this->task->get_contextid());
2146 $overrideableroles = get_overridable_roles($context, ROLENAME_SHORT);
2147 $safecapability = is_safe_capability($capability);
2149 // Check if the new role is an overrideable role AND if the user performing the restore has the
2150 // capability to assign the capability.
2151 if (in_array($newrole->info['shortname'], $overrideableroles) &&
2152 ($safecapability && has_capability('moodle/role:safeoverride', $context, $userid) ||
2153 !$safecapability && has_capability('moodle/role:override', $context, $userid))
2155 assign_capability($data->capability, $data->permission, $newroleid, $this->task->get_contextid());
2157 $this->log("Insufficient capability to assign capability '{$data->capability}' to role!", backup::LOG_WARNING);
2165 * If no instances yet add default enrol methods the same way as when creating new course in UI.
2167 class restore_default_enrolments_step extends restore_execution_step {
2169 public function define_execution() {
2172 // No enrolments in front page.
2173 if ($this->get_courseid() == SITEID) {
2177 $course = $DB->get_record('course', array('id'=>$this->get_courseid()), '*', MUST_EXIST);
2178 // Return any existing course enrolment instances.
2179 $enrolinstances = enrol_get_instances($course->id, false);
2181 if ($enrolinstances) {
2182 // Something already added instances.
2183 // Get the existing enrolment methods in the course.
2184 $enrolmethods = array_map(function($enrolinstance) {
2185 return $enrolinstance->enrol;
2186 }, $enrolinstances);
2188 $plugins = enrol_get_plugins(true);
2189 foreach ($plugins as $pluginname => $plugin) {
2190 // Make sure all default enrolment methods exist in the course.
2191 if (!in_array($pluginname, $enrolmethods)) {
2192 $plugin->course_updated(true, $course, null);
2194 $plugin->restore_sync_course($course);
2198 // Looks like a newly created course.
2199 enrol_course_updated(true, $course, null);
2205 * This structure steps restores the enrol plugins and their underlying
2206 * enrolments, performing all the mappings and/or movements required
2208 class restore_enrolments_structure_step extends restore_structure_step {
2209 protected $enrolsynced = false;
2210 protected $plugins = null;
2211 protected $originalstatus = array();
2214 * Conditionally decide if this step should be executed.
2216 * This function checks the following parameter:
2218 * 1. the course/enrolments.xml file exists
2220 * @return bool true is safe to execute, false otherwise
2222 protected function execute_condition() {
2224 if ($this->get_courseid() == SITEID) {
2228 // Check it is included in the backup
2229 $fullpath = $this->task->get_taskbasepath();
2230 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
2231 if (!file_exists($fullpath)) {
2232 // Not found, can't restore enrolments info
2239 protected function define_structure() {
2241 $userinfo = $this->get_setting_value('users');
2244 $paths[] = $enrol = new restore_path_element('enrol', '/enrolments/enrols/enrol');
2246 $paths[] = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment');
2248 // Attach local plugin stucture to enrol element.
2249 $this->add_plugin_structure('enrol', $enrol);
2255 * Create enrolment instances.
2257 * This has to be called after creation of roles
2258 * and before adding of role assignments.
2260 * @param mixed $data
2263 public function process_enrol($data) {
2266 $data = (object)$data;
2267 $oldid = $data->id; // We'll need this later.
2270 $this->originalstatus[$oldid] = $data->status;
2272 if (!$courserec = $DB->get_record('course', array('id' => $this->get_courseid()))) {
2273 $this->set_mapping('enrol', $oldid, 0);
2277 if (!isset($this->plugins)) {
2278 $this->plugins = enrol_get_plugins(true);
2281 if (!$this->enrolsynced) {
2282 // Make sure that all plugin may create instances and enrolments automatically
2283 // before the first instance restore - this is suitable especially for plugins
2284 // that synchronise data automatically using course->idnumber or by course categories.
2285 foreach ($this->plugins as $plugin) {
2286 $plugin->restore_sync_course($courserec);
2288 $this->enrolsynced = true;
2291 // Map standard fields - plugin has to process custom fields manually.
2292 $data->roleid = $this->get_mappingid('role', $data->roleid);
2293 $data->courseid = $courserec->id;
2295 if (!$this->get_setting_value('users') && $this->get_setting_value('enrolments') == backup::ENROL_WITHUSERS) {
2296 $converttomanual = true;
2298 $converttomanual = ($this->get_setting_value('enrolments') == backup::ENROL_NEVER);
2301 if ($converttomanual) {
2302 // Restore enrolments as manual enrolments.
2303 unset($data->sortorder); // Remove useless sortorder from <2.4 backups.
2304 if (!enrol_is_enabled('manual')) {
2305 $this->set_mapping('enrol', $oldid, 0);
2308 if ($instances = $DB->get_records('enrol', array('courseid'=>$data->courseid, 'enrol'=>'manual'), 'id')) {
2309 $instance = reset($instances);
2310 $this->set_mapping('enrol', $oldid, $instance->id);
2312 if ($data->enrol === 'manual') {
2313 $instanceid = $this->plugins['manual']->add_instance($courserec, (array)$data);
2315 $instanceid = $this->plugins['manual']->add_default_instance($courserec);
2317 $this->set_mapping('enrol', $oldid, $instanceid);
2321 if (!enrol_is_enabled($data->enrol) or !isset($this->plugins[$data->enrol])) {
2322 $this->set_mapping('enrol', $oldid, 0);
2323 $message = "Enrol plugin '$data->enrol' data can not be restored because it is not enabled, consider restoring without enrolment methods";
2324 $this->log($message, backup::LOG_WARNING);
2327 if ($task = $this->get_task() and $task->get_target() == backup::TARGET_NEW_COURSE) {
2328 // Let's keep the sortorder in old backups.
2330 // Prevent problems with colliding sortorders in old backups,
2331 // new 2.4 backups do not need sortorder because xml elements are ordered properly.
2332 unset($data->sortorder);
2334 // Note: plugin is responsible for setting up the mapping, it may also decide to migrate to different type.
2335 $this->plugins[$data->enrol]->restore_instance($this, $data, $courserec, $oldid);
2340 * Create user enrolments.
2342 * This has to be called after creation of enrolment instances
2343 * and before adding of role assignments.
2345 * Roles are assigned in restore_ras_and_caps_structure_step::process_assignment() processing afterwards.
2347 * @param mixed $data
2350 public function process_enrolment($data) {
2353 if (!isset($this->plugins)) {
2354 $this->plugins = enrol_get_plugins(true);
2357 $data = (object)$data;
2359 // Process only if parent instance have been mapped.
2360 if ($enrolid = $this->get_new_parentid('enrol')) {
2361 $oldinstancestatus = ENROL_INSTANCE_ENABLED;
2362 $oldenrolid = $this->get_old_parentid('enrol');
2363 if (isset($this->originalstatus[$oldenrolid])) {
2364 $oldinstancestatus = $this->originalstatus[$oldenrolid];
2366 if ($instance = $DB->get_record('enrol', array('id'=>$enrolid))) {
2367 // And only if user is a mapped one.
2368 if ($userid = $this->get_mappingid('user', $data->userid)) {
2369 if (isset($this->plugins[$instance->enrol])) {
2370 $this->plugins[$instance->enrol]->restore_user_enrolment($this, $data, $instance, $userid, $oldinstancestatus);
2380 * Make sure the user restoring the course can actually access it.
2382 class restore_fix_restorer_access_step extends restore_execution_step {
2383 protected function define_execution() {
2386 if (!$userid = $this->task->get_userid()) {
2390 if (empty($CFG->restorernewroleid)) {
2391 // Bad luck, no fallback role for restorers specified
2395 $courseid = $this->get_courseid();
2396 $context = context_course::instance($courseid);
2398 if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
2399 // Current user may access the course (admin, category manager or restored teacher enrolment usually)
2403 // Try to add role only - we do not need enrolment if user has moodle/course:view or is already enrolled
2404 role_assign($CFG->restorernewroleid, $userid, $context);
2406 if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
2407 // Extra role is enough, yay!
2411 // The last chance is to create manual enrol if it does not exist and and try to enrol the current user,
2412 // hopefully admin selected suitable $CFG->restorernewroleid ...
2413 if (!enrol_is_enabled('manual')) {
2416 if (!$enrol = enrol_get_plugin('manual')) {
2419 if (!$DB->record_exists('enrol', array('enrol'=>'manual', 'courseid'=>$courseid))) {
2420 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
2421 $fields = array('status'=>ENROL_INSTANCE_ENABLED, 'enrolperiod'=>$enrol->get_config('enrolperiod', 0), 'roleid'=>$enrol->get_config('roleid', 0));
2422 $enrol->add_instance($course, $fields);
2425 enrol_try_internal_enrol($courseid, $userid);
2431 * This structure steps restores the filters and their configs
2433 class restore_filters_structure_step extends restore_structure_step {
2435 protected function define_structure() {
2439 $paths[] = new restore_path_element('active', '/filters/filter_actives/filter_active');
2440 $paths[] = new restore_path_element('config', '/filters/filter_configs/filter_config');
2445 public function process_active($data) {
2447 $data = (object)$data;
2449 if (strpos($data->filter, 'filter/') === 0) {
2450 $data->filter = substr($data->filter, 7);
2452 } else if (strpos($data->filter, '/') !== false) {
2453 // Unsupported old filter.
2457 if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do
2460 filter_set_local_state($data->filter, $this->task->get_contextid(), $data->active);
2463 public function process_config($data) {
2465 $data = (object)$data;
2467 if (strpos($data->filter, 'filter/') === 0) {
2468 $data->filter = substr($data->filter, 7);
2470 } else if (strpos($data->filter, '/') !== false) {
2471 // Unsupported old filter.
2475 if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do
2478 filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value);
2484 * This structure steps restores the comments
2485 * Note: Cannot use the comments API because defaults to USER->id.
2486 * That should change allowing to pass $userid
2488 class restore_comments_structure_step extends restore_structure_step {
2490 protected function define_structure() {
2494 $paths[] = new restore_path_element('comment', '/comments/comment');
2499 public function process_comment($data) {
2502 $data = (object)$data;
2504 // First of all, if the comment has some itemid, ask to the task what to map
2506 if ($data->itemid) {
2507 $mapping = $this->task->get_comment_mapping_itemname($data->commentarea);
2508 $data->itemid = $this->get_mappingid($mapping, $data->itemid);
2510 // Only restore the comment if has no mapping OR we have found the matching mapping
2511 if (!$mapping || $data->itemid) {
2512 // Only if user mapping and context
2513 $data->userid = $this->get_mappingid('user', $data->userid);
2514 if ($data->userid && $this->task->get_contextid()) {
2515 $data->contextid = $this->task->get_contextid();
2516 // Only if there is another comment with same context/user/timecreated
2517 $params = array('contextid' => $data->contextid, 'userid' => $data->userid, 'timecreated' => $data->timecreated);
2518 if (!$DB->record_exists('comments', $params)) {
2519 $DB->insert_record('comments', $data);
2527 * This structure steps restores the badges and their configs
2529 class restore_badges_structure_step extends restore_structure_step {
2532 * Conditionally decide if this step should be executed.
2534 * This function checks the following parameters:
2536 * 1. Badges and course badges are enabled on the site.
2537 * 2. The course/badges.xml file exists.
2538 * 3. All modules are restorable.
2539 * 4. All modules are marked for restore.
2541 * @return bool True is safe to execute, false otherwise
2543 protected function execute_condition() {
2546 // First check is badges and course level badges are enabled on this site.
2547 if (empty($CFG->enablebadges) || empty($CFG->badges_allowcoursebadges)) {
2548 // Disabled, don't restore course badges.
2552 // Check if badges.xml is included in the backup.
2553 $fullpath = $this->task->get_taskbasepath();
2554 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
2555 if (!file_exists($fullpath)) {
2556 // Not found, can't restore course badges.
2560 // Check we are able to restore all backed up modules.
2561 if ($this->task->is_missing_modules()) {
2565 // Finally check all modules within the backup are being restored.
2566 if ($this->task->is_excluding_activities()) {
2573 protected function define_structure() {
2575 $paths[] = new restore_path_element('badge', '/badges/badge');
2576 $paths[] = new restore_path_element('criterion', '/badges/badge/criteria/criterion');
2577 $paths[] = new restore_path_element('parameter', '/badges/badge/criteria/criterion/parameters/parameter');
2578 $paths[] = new restore_path_element('endorsement', '/badges/badge/endorsement');
2579 $paths[] = new restore_path_element('alignment', '/badges/badge/alignments/alignment');
2580 $paths[] = new restore_path_element('relatedbadge', '/badges/badge/relatedbadges/relatedbadge');
2581 $paths[] = new restore_path_element('manual_award', '/badges/badge/manual_awards/manual_award');
2586 public function process_badge($data) {
2589 require_once($CFG->libdir . '/badgeslib.php');
2591 $data = (object)$data;
2592 $data->usercreated = $this->get_mappingid('user', $data->usercreated);
2593 if (empty($data->usercreated)) {
2594 $data->usercreated = $this->task->get_userid();
2596 $data->usermodified = $this->get_mappingid('user', $data->usermodified);
2597 if (empty($data->usermodified)) {
2598 $data->usermodified = $this->task->get_userid();
2601 // We'll restore the badge image.
2602 $restorefiles = true;
2604 $courseid = $this->get_courseid();
2607 'name' => $data->name,
2608 'description' => $data->description,
2609 'timecreated' => $data->timecreated,
2610 'timemodified' => $data->timemodified,
2611 'usercreated' => $data->usercreated,
2612 'usermodified' => $data->usermodified,
2613 'issuername' => $data->issuername,
2614 'issuerurl' => $data->issuerurl,
2615 'issuercontact' => $data->issuercontact,
2616 'expiredate' => $this->apply_date_offset($data->expiredate),
2617 'expireperiod' => $data->expireperiod,
2618 'type' => BADGE_TYPE_COURSE,
2619 'courseid' => $courseid,
2620 'message' => $data->message,
2621 'messagesubject' => $data->messagesubject,
2622 'attachment' => $data->attachment,
2623 'notification' => $data->notification,
2624 'status' => BADGE_STATUS_INACTIVE,
2625 'nextcron' => $data->nextcron,
2626 'version' => $data->version,
2627 'language' => $data->language,
2628 'imageauthorname' => $data->imageauthorname,
2629 'imageauthoremail' => $data->imageauthoremail,
2630 'imageauthorurl' => $data->imageauthorurl,
2631 'imagecaption' => $data->imagecaption
2634 $newid = $DB->insert_record('badge', $params);
2635 $this->set_mapping('badge', $data->id, $newid, $restorefiles);
2639 * Create an endorsement for a badge.
2641 * @param mixed $data
2644 public function process_endorsement($data) {
2647 $data = (object)$data;
2650 'badgeid' => $this->get_new_parentid('badge'),
2651 'issuername' => $data->issuername,
2652 'issuerurl' => $data->issuerurl,
2653 'issueremail' => $data->issueremail,
2654 'claimid' => $data->claimid,
2655 'claimcomment' => $data->claimcomment,
2656 'dateissued' => $this->apply_date_offset($data->dateissued)
2658 $newid = $DB->insert_record('badge_endorsement', $params);
2659 $this->set_mapping('endorsement', $data->id, $newid);
2663 * Link to related badges for a badge. This relies on post processing in after_execute().
2665 * @param mixed $data
2668 public function process_relatedbadge($data) {
2671 $data = (object)$data;
2672 $relatedbadgeid = $data->relatedbadgeid;
2674 if ($relatedbadgeid) {
2675 // Only backup and restore related badges if they are contained in the backup file.
2677 'badgeid' => $this->get_new_parentid('badge'),
2678 'relatedbadgeid' => $relatedbadgeid
2680 $newid = $DB->insert_record('badge_related', $params);
2685 * Link to an alignment for a badge.
2687 * @param mixed $data
2690 public function process_alignment($data) {
2693 $data = (object)$data;
2695 'badgeid' => $this->get_new_parentid('badge'),
2696 'targetname' => $data->targetname,
2697 'targeturl' => $data->targeturl,
2698 'targetdescription' => $data->targetdescription,
2699 'targetframework' => $data->targetframework,
2700 'targetcode' => $data->targetcode
2702 $newid = $DB->insert_record('badge_alignment', $params);
2703 $this->set_mapping('alignment', $data->id, $newid);
2706 public function process_criterion($data) {
2709 $data = (object)$data;
2712 'badgeid' => $this->get_new_parentid('badge'),
2713 'criteriatype' => $data->criteriatype,
2714 'method' => $data->method,
2715 'description' => isset($data->description) ? $data->description : '',
2716 'descriptionformat' => isset($data->descriptionformat) ? $data->descriptionformat : 0,
2719 $newid = $DB->insert_record('badge_criteria', $params);
2720 $this->set_mapping('criterion', $data->id, $newid);
2723 public function process_parameter($data) {
2726 require_once($CFG->libdir . '/badgeslib.php');
2728 $data = (object)$data;
2729 $criteriaid = $this->get_new_parentid('criterion');
2731 // Parameter array that will go to database.
2733 $params['critid'] = $criteriaid;
2735 $oldparam = explode('_', $data->name);
2737 if ($data->criteriatype == BADGE_CRITERIA_TYPE_ACTIVITY) {
2738 $module = $this->get_mappingid('course_module', $oldparam[1]);
2739 $params['name'] = $oldparam[0] . '_' . $module;
2740 $params['value'] = $oldparam[0] == 'module' ? $module : $data->value;
2741 } else if ($data->criteriatype == BADGE_CRITERIA_TYPE_COURSE) {
2742 $params['name'] = $oldparam[0] . '_' . $this->get_courseid();
2743 $params['value'] = $oldparam[0] == 'course' ? $this->get_courseid() : $data->value;
2744 } else if ($data->criteriatype == BADGE_CRITERIA_TYPE_MANUAL) {
2745 $role = $this->get_mappingid('role', $data->value);
2746 if (!empty($role)) {
2747 $params['name'] = 'role_' . $role;
2748 $params['value'] = $role;
2752 } else if ($data->criteriatype == BADGE_CRITERIA_TYPE_COMPETENCY) {
2753 $competencyid = $this->get_mappingid('competency', $data->value);
2754 if (!empty($competencyid)) {
2755 $params['name'] = 'competency_' . $competencyid;
2756 $params['value'] = $competencyid;
2762 if (!$DB->record_exists('badge_criteria_param', $params)) {
2763 $DB->insert_record('badge_criteria_param', $params);
2767 public function process_manual_award($data) {
2770 $data = (object)$data;
2771 $role = $this->get_mappingid('role', $data->issuerrole);
2773 if (!empty($role)) {
2775 'badgeid' => $this->get_new_parentid('badge'),
2776 'recipientid' => $this->get_mappingid('user', $data->recipientid),
2777 'issuerid' => $this->get_mappingid('user', $data->issuerid),
2778 'issuerrole' => $role,
2779 'datemet' => $this->apply_date_offset($data->datemet)
2782 // Skip the manual award if recipient or issuer can not be mapped to.
2783 if (empty($award['recipientid']) || empty($award['issuerid'])) {
2787 $DB->insert_record('badge_manual_award', $award);
2791 protected function after_execute() {
2793 // Add related files.
2794 $this->add_related_files('badges', 'badgeimage', 'badge');
2796 $badgeid = $this->get_new_parentid('badge');
2797 // Remap any related badges.
2798 // We do this in the DB directly because this is backup/restore it is not valid to call into
2799 // the component API.
2800 $params = array('badgeid' => $badgeid);
2801 $query = "SELECT DISTINCT br.id, br.badgeid, br.relatedbadgeid
2802 FROM {badge_related} br
2803 WHERE (br.badgeid = :badgeid)";
2804 $relatedbadges = $DB->get_records_sql($query, $params);
2805 $newrelatedids = [];
2806 foreach ($relatedbadges as $relatedbadge) {
2807 $relatedid = $this->get_mappingid('badge', $relatedbadge->relatedbadgeid);
2808 $params['relatedbadgeid'] = $relatedbadge->relatedbadgeid;
2809 $DB->delete_records_select('badge_related', '(badgeid = :badgeid AND relatedbadgeid = :relatedbadgeid)', $params);
2811 $newrelatedids[] = $relatedid;
2814 if (!empty($newrelatedids)) {
2815 $relatedbadges = [];
2816 foreach ($newrelatedids as $relatedid) {
2817 $relatedbadge = new stdClass();
2818 $relatedbadge->badgeid = $badgeid;
2819 $relatedbadge->relatedbadgeid = $relatedid;
2820 $relatedbadges[] = $relatedbadge;
2822 $DB->insert_records('badge_related', $relatedbadges);
2828 * This structure steps restores the calendar events
2830 class restore_calendarevents_structure_step extends restore_structure_step {
2832 protected function define_structure() {
2836 $paths[] = new restore_path_element('calendarevents', '/events/event');
2841 public function process_calendarevents($data) {
2842 global $DB, $SITE, $USER;
2844 $data = (object)$data;
2846 $restorefiles = true; // We'll restore the files
2848 // If this is a new action event, it will automatically be populated by the adhoc task.
2849 // Nothing to do here.
2850 if (isset($data->type) && $data->type == CALENDAR_EVENT_TYPE_ACTION) {
2854 // User overrides for activities are identified by having a courseid of zero with
2855 // both a modulename and instance value set.
2856 $isuseroverride = !$data->courseid && $data->modulename && $data->instance;
2858 // If we don't want to include user data and this record is a user override event
2859 // for an activity then we should not create it. (Only activity events can be user override events - which must have this
2861 if ($isuseroverride && $this->task->setting_exists('userinfo') && !$this->task->get_setting_value('userinfo')) {
2865 // Find the userid and the groupid associated with the event.
2866 $data->userid = $this->get_mappingid('user', $data->userid);
2867 if ($data->userid === false) {
2868 // Blank user ID means that we are dealing with module generated events such as quiz starting times.
2869 // Use the current user ID for these events.
2870 $data->userid = $USER->id;
2872 if (!empty($data->groupid)) {
2873 $data->groupid = $this->get_mappingid('group', $data->groupid);
2874 if ($data->groupid === false) {
2878 // Handle events with empty eventtype //MDL-32827
2879 if(empty($data->eventtype)) {
2880 if ($data->courseid == $SITE->id) { // Site event
2881 $data->eventtype = "site";
2882 } else if ($data->courseid != 0 && $data->groupid == 0 && ($data->modulename == 'assignment' || $data->modulename == 'assign')) {
2883 // Course assingment event
2884 $data->eventtype = "due";
2885 } else if ($data->courseid != 0 && $data->groupid == 0) { // Course event
2886 $data->eventtype = "course";
2887 } else if ($data->groupid) { // Group event
2888 $data->eventtype = "group";
2889 } else if ($data->userid) { // User event
2890 $data->eventtype = "user";
2897 'name' => $data->name,
2898 'description' => $data->description,
2899 'format' => $data->format,
2900 // User overrides in activities use a course id of zero. All other event types
2901 // must use the mapped course id.
2902 'courseid' => $data->courseid ? $this->get_courseid() : 0,
2903 'groupid' => $data->groupid,
2904 'userid' => $data->userid,
2905 'repeatid' => $this->get_mappingid('event', $data->repeatid),
2906 'modulename' => $data->modulename,
2907 'type' => isset($data->type) ? $data->type : 0,
2908 'eventtype' => $data->eventtype,
2909 'timestart' => $this->apply_date_offset($data->timestart),
2910 'timeduration' => $data->timeduration,
2911 'timesort' => isset($data->timesort) ? $this->apply_date_offset($data->timesort) : null,
2912 'visible' => $data->visible,
2913 'uuid' => $data->uuid,
2914 'sequence' => $data->sequence,
2915 'timemodified' => $data->timemodified,
2916 'priority' => isset($data->priority) ? $data->priority : null,
2917 'location' => isset($data->location) ? $data->location : null);
2918 if ($this->name == 'activity_calendar') {
2919 $params['instance'] = $this->task->get_activityid();
2921 $params['instance'] = 0;
2925 WHERE " . $DB->sql_compare_text('name', 255) . " = " . $DB->sql_compare_text('?', 255) . "
2930 AND timeduration = ?
2931 AND " . $DB->sql_compare_text('description', 255) . " = " . $DB->sql_compare_text('?', 255);
2932 $arg = array ($params['name'], $params['courseid'], $params['modulename'], $params['instance'], $params['timestart'], $params['timeduration'], $params['description']);
2933 $result = $DB->record_exists_sql($sql, $arg);
2934 if (empty($result)) {
2935 $newitemid = $DB->insert_record('event', $params);
2936 $this->set_mapping('event', $oldid, $newitemid);
2937 $this->set_mapping('event_description', $oldid, $newitemid, $restorefiles);
2939 // With repeating events, each event has the repeatid pointed at the first occurrence.
2940 // Since the repeatid will be empty when the first occurrence is restored,
2941 // Get the repeatid from the second occurrence of the repeating event and use that to update the first occurrence.
2942 // Then keep a list of repeatids so we only perform this update once.
2943 static $repeatids = array();
2944 if (!empty($params['repeatid']) && !in_array($params['repeatid'], $repeatids)) {
2945 // This entry is repeated so the repeatid field must be set.
2946 $DB->set_field('event', 'repeatid', $params['repeatid'], array('id' => $params['repeatid']));
2947 $repeatids[] = $params['repeatid'];
2951 protected function after_execute() {
2952 // Add related files
2953 $this->add_related_files('calendar', 'event_description', 'event_description');
2957 class restore_course_completion_structure_step extends restore_structure_step {
2960 * Conditionally decide if this step should be executed.
2962 * This function checks parameters that are not immediate settings to ensure
2963 * that the enviroment is suitable for the restore of course completion info.
2965 * This function checks the following four parameters:
2967 * 1. Course completion is enabled on the site
2968 * 2. The backup includes course completion information
2969 * 3. All modules are restorable
2970 * 4. All modules are marked for restore.
2971 * 5. No completion criteria already exist for the course.
2973 * @return bool True is safe to execute, false otherwise
2975 protected function execute_condition() {
2978 // First check course completion is enabled on this site
2979 if (empty($CFG->enablecompletion)) {
2980 // Disabled, don't restore course completion
2984 // No course completion on the front page.
2985 if ($this->get_courseid() == SITEID) {
2989 // Check it is included in the backup
2990 $fullpath = $this->task->get_taskbasepath();
2991 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
2992 if (!file_exists($fullpath)) {
2993 // Not found, can't restore course completion
2997 // Check we are able to restore all backed up modules
2998 if ($this->task->is_missing_modules()) {
3002 // Check all modules within the backup are being restored.
3003 if ($this->task->is_excluding_activities()) {
3007 // Check that no completion criteria is already set for the course.
3008 if ($DB->record_exists('course_completion_criteria', array('course' => $this->get_courseid()))) {
3016 * Define the course completion structure
3018 * @return array Array of restore_path_element
3020 protected function define_structure() {
3022 // To know if we are including user completion info
3023 $userinfo = $this->get_setting_value('userscompletion');
3026 $paths[] = new restore_path_element('course_completion_criteria', '/course_completion/course_completion_criteria');
3027 $paths[] = new restore_path_element('course_completion_aggr_methd', '/course_completion/course_completion_aggr_methd');
3030 $paths[] = new restore_path_element('course_completion_crit_compl', '/course_completion/course_completion_criteria/course_completion_crit_completions/course_completion_crit_compl');
3031 $paths[] = new restore_path_element('course_completions', '/course_completion/course_completions');
3039 * Process course completion criteria
3041 * @global moodle_database $DB
3042 * @param stdClass $data
3044 public function process_course_completion_criteria($data) {
3047 $data = (object)$data;
3048 $data->course = $this->get_courseid();
3050 // Apply the date offset to the time end field
3051 $data->timeend = $this->apply_date_offset($data->timeend);
3053 // Map the role from the criteria
3054 if (isset($data->role) && $data->role != '') {
3055 // Newer backups should include roleshortname, which makes this much easier.
3056 if (!empty($data->roleshortname)) {
3057 $roleinstanceid = $DB->get_field('role', 'id', array('shortname' => $data->roleshortname));
3058 if (!$roleinstanceid) {
3060 'Could not match the role shortname in course_completion_criteria, so skipping',
3065 $data->role = $roleinstanceid;
3067 $data->role = $this->get_mappingid('role', $data->role);
3070 // Check we have an id, otherwise it causes all sorts of bugs.
3073 'Could not match role in course_completion_criteria, so skipping',
3080 // If the completion criteria is for a module we need to map the module instance
3081 // to the new module id.
3082 if (!empty($data->moduleinstance) && !empty($data->module)) {
3083 $data->moduleinstance = $this->get_mappingid('course_module', $data->moduleinstance);
3084 if (empty($data->moduleinstance)) {
3086 'Could not match the module instance in course_completion_criteria, so skipping',
3092 $data->module = null;
3093 $data->moduleinstance = null;
3096 // We backup the course shortname rather than the ID so that we can match back to the course
3097 if (!empty($data->courseinstanceshortname)) {
3098 $courseinstanceid = $DB->get_field('course', 'id', array('shortname'=>$data->courseinstanceshortname));
3099 if (!$courseinstanceid) {
3101 'Could not match the course instance in course_completion_criteria, so skipping',
3107 $courseinstanceid = null;
3109 $data->courseinstance = $courseinstanceid;
3112 'course' => $data->course,
3113 'criteriatype' => $data->criteriatype,
3114 'enrolperiod' => $data->enrolperiod,
3115 'courseinstance' => $data->courseinstance,
3116 'module' => $data->module,
3117 'moduleinstance' => $data->moduleinstance,
3118 'timeend' => $data->timeend,
3119 'gradepass' => $data->gradepass,
3120 'role' => $data->role
3122 $newid = $DB->insert_record('course_completion_criteria', $params);
3123 $this->set_mapping('course_completion_criteria', $data->id, $newid);
3127 * Processes course compltion criteria complete records
3129 * @global moodle_database $DB
3130 * @param stdClass $data
3132 public function process_course_completion_crit_compl($data) {
3135 $data = (object)$data;
3137 // This may be empty if criteria could not be restored
3138 $data->criteriaid = $this->get_mappingid('course_completion_criteria', $data->criteriaid);
3140 $data->course = $this->get_courseid();
3141 $data->userid = $this->get_mappingid('user', $data->userid);
3143 if (!empty($data->criteriaid) && !empty($data->userid)) {
3145 'userid' => $data->userid,
3146 'course' => $data->course,
3147 'criteriaid' => $data->criteriaid,
3148 'timecompleted' => $data->timecompleted
3150 if (isset($data->gradefinal)) {
3151 $params['gradefinal'] = $data->gradefinal;
3153 if (isset($data->unenroled)) {
3154 $params['unenroled'] = $data->unenroled;
3156 $DB->insert_record('course_completion_crit_compl', $params);
3161 * Process course completions
3163 * @global moodle_database $DB
3164 * @param stdClass $data
3166 public function process_course_completions($data) {
3169 $data = (object)$data;
3171 $data->course = $this->get_courseid();
3172 $data->userid = $this->get_mappingid('user', $data->userid);
3174 if (!empty($data->userid)) {
3176 'userid' => $data->userid,
3177 'course' => $data->course,
3178 'timeenrolled' => $data->timeenrolled,
3179 'timestarted' => $data->timestarted,
3180 'timecompleted' => $data->timecompleted,
3181 'reaggregate' => $data->reaggregate
3184 $existing = $DB->get_record('course_completions', array(
3185 'userid' => $data->userid,
3186 'course' => $data->course
3189 // MDL-46651 - If cron writes out a new record before we get to it
3190 // then we should replace it with the Truth data from the backup.
3191 // This may be obsolete after MDL-48518 is resolved
3193 $params['id'] = $existing->id;
3194 $DB->update_record('course_completions', $params);
3196 $DB->insert_record('course_completions', $params);
3202 * Process course completion aggregate methods
3204 * @global moodle_database $DB
3205 * @param stdClass $data
3207 public function process_course_completion_aggr_methd($data) {
3210 $data = (object)$data;
3212 $data->course = $this->get_courseid();
3214 // Only create the course_completion_aggr_methd records if
3215 // the target course has not them defined. MDL-28180
3216 if (!$DB->record_exists('course_completion_aggr_methd', array(
3217 'course' => $data->course,
3218 'criteriatype' => $data->criteriatype))) {