Commit | Line | Data |
---|---|---|
482aac65 EL |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
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. | |
9 | // | |
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. | |
14 | // | |
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/>. | |
17 | ||
18 | /** | |
19 | * @package moodlecore | |
20 | * @subpackage backup-moodle2 | |
21 | * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | /** | |
26 | * Define all the restore steps that will be used by common tasks in restore | |
27 | */ | |
28 | ||
29 | /** | |
30 | * delete old directories and conditionally create backup_temp_ids table | |
31 | */ | |
32 | class restore_create_and_clean_temp_stuff extends restore_execution_step { | |
33 | ||
34 | protected function define_execution() { | |
b8bb45b0 | 35 | $exists = restore_controller_dbops::create_restore_temp_tables($this->get_restoreid()); // temp tables conditionally |
482aac65 EL |
36 | // If the table already exists, it's because restore_prechecks have been executed in the same |
37 | // request (without problems) and it already contains a bunch of preloaded information (users...) | |
38 | // that we aren't going to execute again | |
39 | if ($exists) { // Inform plan about preloaded information | |
40 | $this->task->set_preloaded_information(); | |
41 | } | |
76cfb124 EL |
42 | // Create the old-course-ctxid to new-course-ctxid mapping, we need that available since the beginning |
43 | $itemid = $this->task->get_old_contextid(); | |
44 | $newitemid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; | |
45 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid); | |
c0440b3f EL |
46 | // Create the old-system-ctxid to new-system-ctxid mapping, we need that available since the beginning |
47 | $itemid = $this->task->get_old_system_contextid(); | |
48 | $newitemid = get_context_instance(CONTEXT_SYSTEM)->id; | |
49 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid); | |
d90e49f2 EL |
50 | // Create the old-course-id to new-course-id mapping, we need that available since the beginning |
51 | $itemid = $this->task->get_old_courseid(); | |
52 | $newitemid = $this->get_courseid(); | |
53 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'course', $itemid, $newitemid); | |
54 | ||
482aac65 EL |
55 | } |
56 | } | |
57 | ||
58 | /** | |
59 | * delete the temp dir used by backup/restore (conditionally), | |
60 | * delete old directories and drop temp ids table | |
61 | */ | |
62 | class restore_drop_and_clean_temp_stuff extends restore_execution_step { | |
63 | ||
64 | protected function define_execution() { | |
65 | global $CFG; | |
b8bb45b0 | 66 | restore_controller_dbops::drop_restore_temp_tables($this->get_restoreid()); // Drop ids temp table |
b1eaf633 | 67 | backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs |
482aac65 | 68 | if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally |
b1eaf633 | 69 | backup_helper::delete_backup_dir($this->task->get_tempdir()); // Empty restore dir |
482aac65 EL |
70 | } |
71 | } | |
72 | } | |
73 | ||
0067d939 AD |
74 | /** |
75 | * Restore calculated grade items, grade categories etc | |
76 | */ | |
9a20df96 | 77 | class restore_gradebook_structure_step extends restore_structure_step { |
0067d939 | 78 | |
a0d27102 AD |
79 | /** |
80 | * To conditionally decide if this step must be executed | |
81 | * Note the "settings" conditions are evaluated in the | |
82 | * corresponding task. Here we check for other conditions | |
83 | * not being restore settings (files, site settings...) | |
84 | */ | |
85 | protected function execute_condition() { | |
9a20df96 | 86 | global $CFG, $DB; |
a0d27102 AD |
87 | |
88 | // No gradebook info found, don't execute | |
89 | $fullpath = $this->task->get_taskbasepath(); | |
90 | $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; | |
91 | if (!file_exists($fullpath)) { | |
92 | return false; | |
93 | } | |
94 | ||
58328ce8 EL |
95 | // Some module present in backup file isn't available to restore |
96 | // in this site, don't execute | |
97 | if ($this->task->is_missing_modules()) { | |
98 | return false; | |
99 | } | |
100 | ||
101 | // Some activity has been excluded to be restored, don't execute | |
102 | if ($this->task->is_excluding_activities()) { | |
103 | return false; | |
104 | } | |
105 | ||
9a20df96 AD |
106 | // There should only be one grade category (the 1 associated with the course itself) |
107 | // If other categories already exist we're restoring into an existing course. | |
108 | // Restoring categories into a course with an existing category structure is unlikely to go well | |
109 | $category = new stdclass(); | |
110 | $category->courseid = $this->get_courseid(); | |
111 | $catcount = $DB->count_records('grade_categories', (array)$category); | |
112 | if ($catcount>1) { | |
113 | return false; | |
114 | } | |
115 | ||
a0d27102 AD |
116 | // Arrived here, execute the step |
117 | return true; | |
118 | } | |
119 | ||
0067d939 AD |
120 | protected function define_structure() { |
121 | $paths = array(); | |
122 | $userinfo = $this->task->get_setting_value('users'); | |
123 | ||
124 | $paths[] = new restore_path_element('gradebook', '/gradebook'); | |
125 | $paths[] = new restore_path_element('grade_category', '/gradebook/grade_categories/grade_category'); | |
126 | $paths[] = new restore_path_element('grade_item', '/gradebook/grade_items/grade_item'); | |
127 | if ($userinfo) { | |
128 | $paths[] = new restore_path_element('grade_grade', '/gradebook/grade_items/grade_item/grade_grades/grade_grade'); | |
129 | } | |
130 | $paths[] = new restore_path_element('grade_letter', '/gradebook/grade_letters/grade_letter'); | |
b8040c83 | 131 | $paths[] = new restore_path_element('grade_setting', '/gradebook/grade_settings/grade_setting'); |
0067d939 AD |
132 | |
133 | return $paths; | |
134 | } | |
135 | ||
136 | protected function process_gradebook($data) { | |
137 | } | |
138 | ||
139 | protected function process_grade_item($data) { | |
140 | global $DB; | |
141 | ||
142 | $data = (object)$data; | |
143 | ||
144 | $oldid = $data->id; | |
145 | $data->course = $this->get_courseid(); | |
146 | ||
147 | $data->courseid = $this->get_courseid(); | |
148 | ||
149 | //manual grade items store category id in categoryid | |
150 | if ($data->itemtype=='manual') { | |
151 | $data->categoryid = $this->get_mappingid('grade_category', $data->categoryid); | |
152 | } //course and category grade items store their category id in iteminstance | |
153 | else if ($data->itemtype=='course' || $data->itemtype=='category') { | |
154 | $data->iteminstance = $this->get_mappingid('grade_category', $data->iteminstance); | |
155 | } | |
156 | ||
157 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); | |
158 | $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid); | |
159 | ||
160 | $data->locktime = $this->apply_date_offset($data->locktime); | |
161 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
162 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
163 | ||
44a25cd2 | 164 | $coursecategory = $newitemid = null; |
0067d939 AD |
165 | //course grade item should already exist so updating instead of inserting |
166 | if($data->itemtype=='course') { | |
167 | ||
168 | //get the ID of the already created grade item | |
169 | $gi = new stdclass(); | |
170 | $gi->courseid = $this->get_courseid(); | |
171 | ||
172 | $gi->itemtype = $data->itemtype; | |
173 | if ($data->itemtype=='course') { | |
174 | //need to get the id of the grade_category that was automatically created for the course | |
175 | $category = new stdclass(); | |
176 | $category->courseid = $this->get_courseid(); | |
177 | $category->parent = null; | |
9a20df96 AD |
178 | //course category fullname starts out as ? but may be edited |
179 | //$category->fullname = '?'; | |
0067d939 AD |
180 | |
181 | $coursecategory = $DB->get_record('grade_categories', (array)$category); | |
182 | $gi->iteminstance = $coursecategory->id; | |
183 | } | |
184 | ||
185 | $existinggradeitem = $DB->get_record('grade_items', (array)$gi); | |
44a25cd2 AD |
186 | if (!empty($existinggradeitem)) { |
187 | $data->id = $newitemid = $existinggradeitem->id; | |
188 | $DB->update_record('grade_items', $data); | |
189 | } | |
190 | } | |
0067d939 | 191 | |
44a25cd2 AD |
192 | if (empty($newitemid)) { |
193 | //in case we found the course category but still need to insert the course grade item | |
194 | if ($data->itemtype=='course' && !empty($coursecategory)) { | |
195 | $data->iteminstance = $coursecategory->id; | |
196 | } | |
034ef761 | 197 | |
0067d939 AD |
198 | $newitemid = $DB->insert_record('grade_items', $data); |
199 | } | |
200 | $this->set_mapping('grade_item', $oldid, $newitemid); | |
201 | } | |
202 | ||
203 | protected function process_grade_grade($data) { | |
204 | global $DB; | |
205 | ||
206 | $data = (object)$data; | |
207 | $oldid = $data->id; | |
208 | ||
209 | $data->itemid = $this->get_new_parentid('grade_item'); | |
210 | ||
211 | $data->userid = $this->get_mappingid('user', $data->userid); | |
212 | $data->usermodified = $this->get_mappingid('user', $data->usermodified); | |
213 | $data->locktime = $this->apply_date_offset($data->locktime); | |
9a20df96 AD |
214 | // TODO: Ask, all the rest of locktime/exported... work with time... to be rolled? |
215 | $data->overridden = $this->apply_date_offset($data->overridden); | |
0067d939 AD |
216 | $data->timecreated = $this->apply_date_offset($data->timecreated); |
217 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
218 | ||
219 | $newitemid = $DB->insert_record('grade_grades', $data); | |
0df2981f | 220 | //$this->set_mapping('grade_grade', $oldid, $newitemid); |
0067d939 AD |
221 | } |
222 | protected function process_grade_category($data) { | |
223 | global $DB; | |
224 | ||
225 | $data = (object)$data; | |
226 | $oldid = $data->id; | |
227 | ||
228 | $data->course = $this->get_courseid(); | |
229 | $data->courseid = $data->course; | |
230 | ||
231 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
232 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
233 | ||
499dbce8 AD |
234 | $newitemid = null; |
235 | //no parent means a course level grade category. That may have been created when the course was created | |
0067d939 | 236 | if(empty($data->parent)) { |
499dbce8 AD |
237 | //parent was being saved as 0 when it should be null |
238 | $data->parent = null; | |
239 | ||
0067d939 AD |
240 | //get the already created course level grade category |
241 | $category = new stdclass(); | |
499dbce8 | 242 | $category->courseid = $this->get_courseid(); |
0067d939 AD |
243 | |
244 | $coursecategory = $DB->get_record('grade_categories', (array)$category); | |
499dbce8 AD |
245 | if (!empty($coursecategory)) { |
246 | $data->id = $newitemid = $coursecategory->id; | |
247 | $DB->update_record('grade_categories', $data); | |
248 | } | |
249 | } | |
0067d939 | 250 | |
499dbce8 AD |
251 | //need to insert a course category |
252 | if (empty($newitemid)) { | |
0067d939 AD |
253 | $newitemid = $DB->insert_record('grade_categories', $data); |
254 | } | |
255 | $this->set_mapping('grade_category', $oldid, $newitemid); | |
0067d939 AD |
256 | } |
257 | protected function process_grade_letter($data) { | |
258 | global $DB; | |
259 | ||
260 | $data = (object)$data; | |
261 | $oldid = $data->id; | |
262 | ||
e101180d | 263 | $data->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; |
0067d939 AD |
264 | |
265 | $newitemid = $DB->insert_record('grade_letters', $data); | |
266 | $this->set_mapping('grade_letter', $oldid, $newitemid); | |
267 | } | |
b8040c83 AD |
268 | protected function process_grade_setting($data) { |
269 | global $DB; | |
270 | ||
271 | $data = (object)$data; | |
272 | $oldid = $data->id; | |
273 | ||
274 | $data->courseid = $this->get_courseid(); | |
275 | ||
276 | $newitemid = $DB->insert_record('grade_settings', $data); | |
0df2981f | 277 | //$this->set_mapping('grade_setting', $oldid, $newitemid); |
b8040c83 | 278 | } |
0f94550c AD |
279 | |
280 | //put all activity grade items in the correct grade category and mark all for recalculation | |
c4d2c745 AD |
281 | protected function after_execute() { |
282 | global $DB; | |
283 | ||
c4d2c745 AD |
284 | $conditions = array( |
285 | 'backupid' => $this->get_restoreid(), | |
286 | 'itemname' => 'grade_item'//, | |
287 | //'itemid' => $itemid | |
288 | ); | |
289 | $rs = $DB->get_recordset('backup_ids_temp', $conditions); | |
290 | ||
291 | if (!empty($rs)) { | |
292 | foreach($rs as $grade_item_backup) { | |
0f94550c AD |
293 | $updateobj = new stdclass(); |
294 | $updateobj->id = $grade_item_backup->newitemid; | |
074a765f | 295 | |
0f94550c AD |
296 | //if this is an activity grade item that needs to be put back in its correct category |
297 | if (!empty($grade_item_backup->parentitemid)) { | |
298 | $updateobj->categoryid = $this->get_mappingid('grade_category', $grade_item_backup->parentitemid); | |
074a765f | 299 | } else { |
0f94550c AD |
300 | //mark course and category items as needing to be recalculated |
301 | $updateobj->needsupdate=1; | |
c4d2c745 | 302 | } |
0f94550c | 303 | $DB->update_record('grade_items', $updateobj); |
9a20df96 AD |
304 | } |
305 | } | |
306 | $rs->close(); | |
0f94550c | 307 | |
9a20df96 AD |
308 | //need to correct the grade category path and parent |
309 | $conditions = array( | |
310 | 'courseid' => $this->get_courseid() | |
311 | ); | |
312 | $grade_category = new stdclass(); | |
034ef761 | 313 | |
9a20df96 AD |
314 | $rs = $DB->get_recordset('grade_categories', $conditions); |
315 | if (!empty($rs)) { | |
316 | //get all the parents correct first as grade_category::build_path() loads category parents from the DB | |
317 | foreach($rs as $gc) { | |
318 | if (!empty($gc->parent)) { | |
319 | $grade_category->id = $gc->id; | |
320 | $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent); | |
321 | $DB->update_record('grade_categories', $grade_category); | |
322 | } | |
323 | } | |
324 | } | |
325 | if (isset($grade_category->parent)) { | |
326 | unset($grade_category->parent); | |
327 | } | |
328 | $rs->close(); | |
329 | ||
330 | $rs = $DB->get_recordset('grade_categories', $conditions); | |
331 | if (!empty($rs)) { | |
332 | //now we can rebuild all the paths | |
333 | foreach($rs as $gc) { | |
334 | $grade_category->id = $gc->id; | |
335 | $grade_category->path = grade_category::build_path($gc); | |
336 | $DB->update_record('grade_categories', $grade_category); | |
c4d2c745 AD |
337 | } |
338 | } | |
339 | $rs->close(); | |
340 | } | |
0067d939 AD |
341 | } |
342 | ||
394edb7e EL |
343 | /** |
344 | * decode all the interlinks present in restored content | |
345 | * relying 100% in the restore_decode_processor that handles | |
346 | * both the contents to modify and the rules to be applied | |
347 | */ | |
348 | class restore_decode_interlinks extends restore_execution_step { | |
349 | ||
350 | protected function define_execution() { | |
9f68f2d5 EL |
351 | // Get the decoder (from the plan) |
352 | $decoder = $this->task->get_decoder(); | |
353 | restore_decode_processor::register_link_decoders($decoder); // Add decoder contents and rules | |
354 | // And launch it, everything will be processed | |
355 | $decoder->execute(); | |
394edb7e EL |
356 | } |
357 | } | |
358 | ||
359 | /** | |
e3c2e1b2 EL |
360 | * first, ensure that we have no gaps in section numbers |
361 | * and then, rebuid the course cache | |
394edb7e EL |
362 | */ |
363 | class restore_rebuild_course_cache extends restore_execution_step { | |
364 | ||
365 | protected function define_execution() { | |
e3c2e1b2 EL |
366 | global $DB; |
367 | ||
368 | // Although there is some sort of auto-recovery of missing sections | |
369 | // present in course/formats... here we check that all the sections | |
370 | // from 0 to MAX(section->section) exist, creating them if necessary | |
371 | $maxsection = $DB->get_field('course_sections', 'MAX(section)', array('course' => $this->get_courseid())); | |
372 | // Iterate over all sections | |
373 | for ($i = 0; $i <= $maxsection; $i++) { | |
374 | // If the section $i doesn't exist, create it | |
375 | if (!$DB->record_exists('course_sections', array('course' => $this->get_courseid(), 'section' => $i))) { | |
376 | $sectionrec = array( | |
377 | 'course' => $this->get_courseid(), | |
378 | 'section' => $i); | |
379 | $DB->insert_record('course_sections', $sectionrec); // missing section created | |
380 | } | |
381 | } | |
382 | ||
383 | // Rebuild cache now that all sections are in place | |
394edb7e EL |
384 | rebuild_course_cache($this->get_courseid()); |
385 | } | |
386 | } | |
387 | ||
b8e455a7 EL |
388 | |
389 | /** | |
390 | * Review all the (pending) block positions in backup_ids, matching by | |
391 | * contextid, creating positions as needed. This is executed by the | |
392 | * final task, once all the contexts have been created | |
393 | */ | |
394 | class restore_review_pending_block_positions extends restore_execution_step { | |
395 | ||
396 | protected function define_execution() { | |
397 | global $DB; | |
398 | ||
399 | // Get all the block_position objects pending to match | |
400 | $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'block_position'); | |
401 | $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid'); | |
402 | // Process block positions, creating them or accumulating for final step | |
403 | foreach($rs as $posrec) { | |
404 | // Get the complete position object (stored as info) | |
405 | $position = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'block_position', $posrec->itemid)->info; | |
406 | // If position is for one already mapped (known) contextid | |
407 | // process it now, creating the position, else nothing to | |
408 | // do, position finally discarded | |
409 | if ($newctx = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $position->contextid)) { | |
410 | $position->contextid = $newctx->newitemid; | |
411 | // Create the block position | |
412 | $DB->insert_record('block_positions', $position); | |
413 | } | |
414 | } | |
415 | $rs->close(); | |
416 | } | |
417 | } | |
418 | ||
5095f325 EL |
419 | /** |
420 | * Process all the saved module availability records in backup_ids, matching | |
421 | * course modules and grade item id once all them have been already restored. | |
422 | * only if all matchings are satisfied the availability condition will be created. | |
423 | * At the same time, it is required for the site to have that functionality enabled. | |
424 | */ | |
425 | class restore_process_course_modules_availability extends restore_execution_step { | |
426 | ||
427 | protected function define_execution() { | |
428 | global $CFG, $DB; | |
429 | ||
430 | // Site hasn't availability enabled | |
431 | if (empty($CFG->enableavailability)) { | |
432 | return; | |
433 | } | |
434 | ||
435 | // Get all the module_availability objects to process | |
436 | $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability'); | |
437 | $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid'); | |
438 | // Process availabilities, creating them if everything matches ok | |
439 | foreach($rs as $availrec) { | |
440 | $allmatchesok = true; | |
441 | // Get the complete availabilityobject | |
442 | $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability', $availrec->itemid)->info; | |
443 | // Map the sourcecmid if needed and possible | |
444 | if (!empty($availability->sourcecmid)) { | |
445 | $newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'course_module', $availability->sourcecmid); | |
446 | if ($newcm) { | |
447 | $availability->sourcecmid = $newcm->newitemid; | |
448 | } else { | |
449 | $allmatchesok = false; // Failed matching, we won't create this availability rule | |
450 | } | |
451 | } | |
452 | // Map the gradeitemid if needed and possible | |
453 | if (!empty($availability->gradeitemid)) { | |
454 | $newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'grade_item', $availability->gradeitemid); | |
455 | if ($newgi) { | |
456 | $availability->gradeitemid = $newgi->newitemid; | |
457 | } else { | |
458 | $allmatchesok = false; // Failed matching, we won't create this availability rule | |
459 | } | |
460 | } | |
461 | if ($allmatchesok) { // Everything ok, create the availability rule | |
462 | $DB->insert_record('course_modules_availability', $availability); | |
463 | } | |
464 | } | |
465 | $rs->close(); | |
466 | } | |
467 | } | |
468 | ||
469 | ||
482aac65 EL |
470 | /* |
471 | * Execution step that, *conditionally* (if there isn't preloaded information) | |
472 | * will load the inforef files for all the included course/section/activity tasks | |
473 | * to backup_temp_ids. They will be stored with "xxxxref" as itemname | |
474 | */ | |
475 | class restore_load_included_inforef_records extends restore_execution_step { | |
476 | ||
477 | protected function define_execution() { | |
478 | ||
479 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
480 | return; | |
481 | } | |
482 | ||
483 | // Get all the included inforef files | |
484 | $files = restore_dbops::get_needed_inforef_files($this->get_restoreid()); | |
485 | foreach ($files as $file) { | |
486 | restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $file); // Load each inforef file to temp_ids | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
76cfb124 | 491 | /* |
b8bb45b0 | 492 | * Execution step that will load all the needed files into backup_files_temp |
76cfb124 EL |
493 | * - info: contains the whole original object (times, names...) |
494 | * (all them being original ids as loaded from xml) | |
495 | */ | |
496 | class restore_load_included_files extends restore_structure_step { | |
497 | ||
498 | protected function define_structure() { | |
499 | ||
500 | $file = new restore_path_element('file', '/files/file'); | |
501 | ||
502 | return array($file); | |
503 | } | |
504 | ||
505 | // Processing functions go here | |
506 | public function process_file($data) { | |
507 | ||
508 | $data = (object)$data; // handy | |
509 | ||
76cfb124 EL |
510 | // load it if needed: |
511 | // - it it is one of the annotated inforef files (course/section/activity/block) | |
41941110 EL |
512 | // - it is one "user", "group", "grouping", "grade", "question" or "qtype_xxxx" component file (that aren't sent to inforef ever) |
513 | // TODO: qtype_xxx should be replaced by proper backup_qtype_plugin::get_components_and_fileareas() use, | |
514 | // but then we'll need to change it to load plugins itself (because this is executed too early in restore) | |
b8bb45b0 | 515 | $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id); |
71a50b13 | 516 | $iscomponent = ($data->component == 'user' || $data->component == 'group' || |
41941110 EL |
517 | $data->component == 'grouping' || $data->component == 'grade' || |
518 | $data->component == 'question' || substr($data->component, 0, 5) == 'qtype'); | |
76cfb124 | 519 | if ($isfileref || $iscomponent) { |
b8bb45b0 | 520 | restore_dbops::set_backup_files_record($this->get_restoreid(), $data); |
76cfb124 EL |
521 | } |
522 | } | |
523 | } | |
524 | ||
71a50b13 EL |
525 | /** |
526 | * Execution step that, *conditionally* (if there isn't preloaded information), | |
527 | * will load all the needed roles to backup_temp_ids. They will be stored with | |
528 | * "role" itemname. Also it will perform one automatic mapping to roles existing | |
529 | * in the target site, based in permissions of the user performing the restore, | |
530 | * archetypes and other bits. At the end, each original role will have its associated | |
531 | * target role or 0 if it's going to be skipped. Note we wrap everything over one | |
532 | * restore_dbops method, as far as the same stuff is going to be also executed | |
533 | * by restore prechecks | |
534 | */ | |
535 | class restore_load_and_map_roles extends restore_execution_step { | |
536 | ||
537 | protected function define_execution() { | |
8d4e41f4 | 538 | if ($this->task->get_preloaded_information()) { // if info is already preloaded |
71a50b13 EL |
539 | return; |
540 | } | |
541 | ||
542 | $file = $this->get_basepath() . '/roles.xml'; | |
543 | // Load needed toles to temp_ids | |
544 | restore_dbops::load_roles_to_tempids($this->get_restoreid(), $file); | |
8d4e41f4 | 545 | |
71a50b13 | 546 | // Process roles, mapping/skipping. Any error throws exception |
8d4e41f4 EL |
547 | // Note we pass controller's info because it can contain role mapping information |
548 | // about manual mappings performed by UI | |
549 | 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); | |
71a50b13 EL |
550 | } |
551 | } | |
552 | ||
482aac65 EL |
553 | /** |
554 | * Execution step that, *conditionally* (if there isn't preloaded information | |
555 | * and users have been selected in settings, will load all the needed users | |
556 | * to backup_temp_ids. They will be stored with "user" itemname and with | |
76cfb124 | 557 | * their original contextid as paremitemid |
482aac65 EL |
558 | */ |
559 | class restore_load_included_users extends restore_execution_step { | |
560 | ||
561 | protected function define_execution() { | |
562 | ||
563 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
564 | return; | |
565 | } | |
25d3cf44 | 566 | if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do |
482aac65 EL |
567 | return; |
568 | } | |
569 | $file = $this->get_basepath() . '/users.xml'; | |
570 | restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids | |
571 | } | |
572 | } | |
573 | ||
574 | /** | |
575 | * Execution step that, *conditionally* (if there isn't preloaded information | |
576 | * and users have been selected in settings, will process all the needed users | |
577 | * in order to decide and perform any action with them (create / map / error) | |
578 | * Note: Any error will cause exception, as far as this is the same processing | |
579 | * than the one into restore prechecks (that should have stopped process earlier) | |
580 | */ | |
76cfb124 | 581 | class restore_process_included_users extends restore_execution_step { |
482aac65 EL |
582 | |
583 | protected function define_execution() { | |
584 | ||
585 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
586 | return; | |
587 | } | |
25d3cf44 | 588 | if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do |
482aac65 EL |
589 | return; |
590 | } | |
591 | restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
592 | } | |
593 | } | |
594 | ||
76cfb124 EL |
595 | /** |
596 | * Execution step that will create all the needed users as calculated | |
597 | * by @restore_process_included_users (those having newiteind = 0) | |
598 | */ | |
599 | class restore_create_included_users extends restore_execution_step { | |
600 | ||
601 | protected function define_execution() { | |
602 | ||
b212f87e | 603 | restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files'), $this->task->get_userid()); |
76cfb124 EL |
604 | } |
605 | } | |
606 | ||
607 | /** | |
608 | * Structure step that will create all the needed groups and groupings | |
609 | * by loading them from the groups.xml file performing the required matches. | |
610 | * Note group members only will be added if restoring user info | |
611 | */ | |
612 | class restore_groups_structure_step extends restore_structure_step { | |
613 | ||
c0440b3f EL |
614 | protected function define_structure() { |
615 | ||
616 | $paths = array(); // Add paths here | |
617 | ||
618 | $paths[] = new restore_path_element('group', '/groups/group'); | |
619 | if ($this->get_setting_value('users')) { | |
620 | $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member'); | |
621 | } | |
622 | $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping'); | |
623 | $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group'); | |
624 | ||
625 | return $paths; | |
626 | } | |
627 | ||
628 | // Processing functions go here | |
629 | public function process_group($data) { | |
630 | global $DB; | |
631 | ||
632 | $data = (object)$data; // handy | |
633 | $data->courseid = $this->get_courseid(); | |
634 | ||
635 | $oldid = $data->id; // need this saved for later | |
76cfb124 | 636 | |
c0440b3f EL |
637 | $restorefiles = false; // Only if we end creating the group |
638 | ||
639 | // Search if the group already exists (by name & description) in the target course | |
640 | $description_clause = ''; | |
641 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
642 | if (!empty($data->description)) { | |
643 | $description_clause = ' AND ' . | |
644 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
645 | $params['desc'] = $data->description; | |
646 | } | |
647 | if (!$groupdb = $DB->get_record_sql("SELECT * | |
648 | FROM {groups} | |
649 | WHERE courseid = :courseid | |
650 | AND name = :grname $description_clause", $params)) { | |
651 | // group doesn't exist, create | |
652 | $newitemid = $DB->insert_record('groups', $data); | |
653 | $restorefiles = true; // We'll restore the files | |
654 | } else { | |
655 | // group exists, use it | |
656 | $newitemid = $groupdb->id; | |
657 | } | |
658 | // Save the id mapping | |
659 | $this->set_mapping('group', $oldid, $newitemid, $restorefiles); | |
660 | } | |
661 | ||
662 | public function process_member($data) { | |
663 | global $DB; | |
664 | ||
665 | $data = (object)$data; // handy | |
666 | ||
667 | // get parent group->id | |
668 | $data->groupid = $this->get_new_parentid('group'); | |
669 | ||
670 | // map user newitemid and insert if not member already | |
671 | if ($data->userid = $this->get_mappingid('user', $data->userid)) { | |
672 | if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) { | |
673 | $DB->insert_record('groups_members', $data); | |
674 | } | |
675 | } | |
676 | } | |
677 | ||
678 | public function process_grouping($data) { | |
71a50b13 EL |
679 | global $DB; |
680 | ||
681 | $data = (object)$data; // handy | |
682 | $data->courseid = $this->get_courseid(); | |
683 | ||
684 | $oldid = $data->id; // need this saved for later | |
685 | $restorefiles = false; // Only if we end creating the grouping | |
686 | ||
687 | // Search if the grouping already exists (by name & description) in the target course | |
688 | $description_clause = ''; | |
689 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
690 | if (!empty($data->description)) { | |
691 | $description_clause = ' AND ' . | |
692 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
693 | $params['desc'] = $data->description; | |
694 | } | |
695 | if (!$groupingdb = $DB->get_record_sql("SELECT * | |
696 | FROM {groupings} | |
697 | WHERE courseid = :courseid | |
698 | AND name = :grname $description_clause", $params)) { | |
699 | // grouping doesn't exist, create | |
700 | $newitemid = $DB->insert_record('groupings', $data); | |
701 | $restorefiles = true; // We'll restore the files | |
702 | } else { | |
703 | // grouping exists, use it | |
704 | $newitemid = $groupingdb->id; | |
705 | } | |
706 | // Save the id mapping | |
707 | $this->set_mapping('grouping', $oldid, $newitemid, $restorefiles); | |
c0440b3f EL |
708 | } |
709 | ||
710 | public function process_grouping_group($data) { | |
71a50b13 EL |
711 | global $DB; |
712 | ||
713 | $data = (object)$data; | |
714 | ||
715 | $data->groupingid = $this->get_new_parentid('grouping'); // Use new parentid | |
716 | $data->groupid = $this->get_mappingid('group', $data->groupid); // Get from mappings | |
717 | $DB->insert_record('groupings_groups', $data); // No need to set this mapping (no child info nor files) | |
c0440b3f EL |
718 | } |
719 | ||
720 | protected function after_execute() { | |
721 | // Add group related files, matching with "group" mappings | |
722 | $this->add_related_files('group', 'icon', 'group'); | |
723 | $this->add_related_files('group', 'description', 'group'); | |
71a50b13 EL |
724 | // Add grouping related files, matching with "grouping" mappings |
725 | $this->add_related_files('grouping', 'description', 'grouping'); | |
c0440b3f EL |
726 | } |
727 | ||
728 | } | |
729 | ||
730 | /** | |
731 | * Structure step that will create all the needed scales | |
732 | * by loading them from the scales.xml | |
c0440b3f EL |
733 | */ |
734 | class restore_scales_structure_step extends restore_structure_step { | |
735 | ||
736 | protected function define_structure() { | |
737 | ||
738 | $paths = array(); // Add paths here | |
739 | $paths[] = new restore_path_element('scale', '/scales_definition/scale'); | |
740 | return $paths; | |
741 | } | |
742 | ||
743 | protected function process_scale($data) { | |
744 | global $DB; | |
745 | ||
746 | $data = (object)$data; | |
747 | ||
748 | $restorefiles = false; // Only if we end creating the group | |
749 | ||
750 | $oldid = $data->id; // need this saved for later | |
751 | ||
752 | // Look for scale (by 'scale' both in standard (course=0) and current course | |
753 | // with priority to standard scales (ORDER clause) | |
754 | // scale is not course unique, use get_record_sql to suppress warning | |
755 | // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides | |
756 | $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc'); | |
757 | $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale); | |
758 | if (!$scadb = $DB->get_record_sql("SELECT * | |
759 | FROM {scale} | |
760 | WHERE courseid IN (0, :courseid) | |
761 | AND $compare_scale_clause | |
762 | ORDER BY courseid", $params, IGNORE_MULTIPLE)) { | |
763 | // Remap the user if possible, defaut to user performing the restore if not | |
764 | $userid = $this->get_mappingid('user', $data->userid); | |
ba8bead5 | 765 | $data->userid = $userid ? $userid : $this->task->get_userid(); |
c0440b3f EL |
766 | // Remap the course if course scale |
767 | $data->courseid = $data->courseid ? $this->get_courseid() : 0; | |
768 | // If global scale (course=0), check the user has perms to create it | |
769 | // falling to course scale if not | |
770 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
394edb7e | 771 | if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $this->task->get_userid())) { |
c0440b3f EL |
772 | $data->courseid = $this->get_courseid(); |
773 | } | |
774 | // scale doesn't exist, create | |
775 | $newitemid = $DB->insert_record('scale', $data); | |
776 | $restorefiles = true; // We'll restore the files | |
777 | } else { | |
778 | // scale exists, use it | |
779 | $newitemid = $scadb->id; | |
780 | } | |
781 | // Save the id mapping (with files support at system context) | |
782 | $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
783 | } | |
784 | ||
785 | protected function after_execute() { | |
786 | // Add scales related files, matching with "scale" mappings | |
787 | $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid()); | |
788 | } | |
76cfb124 EL |
789 | } |
790 | ||
c0440b3f | 791 | |
c8730ff0 EL |
792 | /** |
793 | * Structure step that will create all the needed outocomes | |
794 | * by loading them from the outcomes.xml | |
795 | */ | |
796 | class restore_outcomes_structure_step extends restore_structure_step { | |
797 | ||
798 | protected function define_structure() { | |
799 | ||
800 | $paths = array(); // Add paths here | |
801 | $paths[] = new restore_path_element('outcome', '/outcomes_definition/outcome'); | |
802 | return $paths; | |
803 | } | |
804 | ||
805 | protected function process_outcome($data) { | |
806 | global $DB; | |
807 | ||
808 | $data = (object)$data; | |
809 | ||
810 | $restorefiles = false; // Only if we end creating the group | |
811 | ||
812 | $oldid = $data->id; // need this saved for later | |
813 | ||
814 | // Look for outcome (by shortname both in standard (courseid=null) and current course | |
815 | // with priority to standard outcomes (ORDER clause) | |
816 | // outcome is not course unique, use get_record_sql to suppress warning | |
817 | $params = array('courseid' => $this->get_courseid(), 'shortname' => $data->shortname); | |
818 | if (!$outdb = $DB->get_record_sql('SELECT * | |
819 | FROM {grade_outcomes} | |
820 | WHERE shortname = :shortname | |
821 | AND (courseid = :courseid OR courseid IS NULL) | |
822 | ORDER BY COALESCE(courseid, 0)', $params, IGNORE_MULTIPLE)) { | |
823 | // Remap the user | |
824 | $userid = $this->get_mappingid('user', $data->usermodified); | |
ba8bead5 | 825 | $data->usermodified = $userid ? $userid : $this->task->get_userid(); |
8d4e41f4 EL |
826 | // Remap the scale |
827 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); | |
c8730ff0 EL |
828 | // Remap the course if course outcome |
829 | $data->courseid = $data->courseid ? $this->get_courseid() : null; | |
830 | // If global outcome (course=null), check the user has perms to create it | |
831 | // falling to course outcome if not | |
832 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
394edb7e | 833 | if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $this->task->get_userid())) { |
c8730ff0 EL |
834 | $data->courseid = $this->get_courseid(); |
835 | } | |
836 | // outcome doesn't exist, create | |
837 | $newitemid = $DB->insert_record('grade_outcomes', $data); | |
838 | $restorefiles = true; // We'll restore the files | |
839 | } else { | |
840 | // scale exists, use it | |
841 | $newitemid = $outdb->id; | |
842 | } | |
843 | // Set the corresponding grade_outcomes_courses record | |
844 | $outcourserec = new stdclass(); | |
845 | $outcourserec->courseid = $this->get_courseid(); | |
846 | $outcourserec->outcomeid = $newitemid; | |
847 | if (!$DB->record_exists('grade_outcomes_courses', (array)$outcourserec)) { | |
848 | $DB->insert_record('grade_outcomes_courses', $outcourserec); | |
849 | } | |
850 | // Save the id mapping (with files support at system context) | |
851 | $this->set_mapping('outcome', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
852 | } | |
853 | ||
854 | protected function after_execute() { | |
855 | // Add outcomes related files, matching with "outcome" mappings | |
856 | $this->add_related_files('grade', 'outcome', 'outcome', $this->task->get_old_system_contextid()); | |
857 | } | |
858 | } | |
859 | ||
41941110 EL |
860 | /** |
861 | * Execution step that, *conditionally* (if there isn't preloaded information | |
862 | * will load all the question categories and questions (header info only) | |
863 | * to backup_temp_ids. They will be stored with "question_category" and | |
864 | * "question" itemnames and with their original contextid and question category | |
865 | * id as paremitemids | |
866 | */ | |
867 | class restore_load_categories_and_questions extends restore_execution_step { | |
868 | ||
869 | protected function define_execution() { | |
870 | ||
871 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
872 | return; | |
873 | } | |
874 | $file = $this->get_basepath() . '/questions.xml'; | |
875 | restore_dbops::load_categories_and_questions_to_tempids($this->get_restoreid(), $file); | |
876 | } | |
877 | } | |
878 | ||
879 | /** | |
880 | * Execution step that, *conditionally* (if there isn't preloaded information) | |
881 | * will process all the needed categories and questions | |
882 | * in order to decide and perform any action with them (create / map / error) | |
883 | * Note: Any error will cause exception, as far as this is the same processing | |
884 | * than the one into restore prechecks (that should have stopped process earlier) | |
885 | */ | |
886 | class restore_process_categories_and_questions extends restore_execution_step { | |
887 | ||
888 | protected function define_execution() { | |
889 | ||
890 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
891 | return; | |
892 | } | |
893 | restore_dbops::process_categories_and_questions($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
894 | } | |
895 | } | |
896 | ||
3223cc95 EL |
897 | /** |
898 | * Structure step that will read the section.xml creating/updating sections | |
899 | * as needed, rebuilding course cache and other friends | |
900 | */ | |
901 | class restore_section_structure_step extends restore_structure_step { | |
c8730ff0 | 902 | |
3223cc95 EL |
903 | protected function define_structure() { |
904 | return array(new restore_path_element('section', '/section')); | |
905 | } | |
906 | ||
907 | public function process_section($data) { | |
908 | global $DB; | |
909 | $data = (object)$data; | |
910 | $oldid = $data->id; // We'll need this later | |
911 | ||
912 | $restorefiles = false; | |
913 | ||
914 | // Look for the section | |
915 | $section = new stdclass(); | |
916 | $section->course = $this->get_courseid(); | |
917 | $section->section = $data->number; | |
918 | // Section doesn't exist, create it with all the info from backup | |
919 | if (!$secrec = $DB->get_record('course_sections', (array)$section)) { | |
920 | $section->name = $data->name; | |
921 | $section->summary = $data->summary; | |
922 | $section->summaryformat = $data->summaryformat; | |
923 | $section->sequence = ''; | |
924 | $section->visible = $data->visible; | |
925 | $newitemid = $DB->insert_record('course_sections', $section); | |
926 | $restorefiles = true; | |
927 | ||
928 | // Section exists, update non-empty information | |
929 | } else { | |
930 | $section->id = $secrec->id; | |
931 | if (empty($secrec->name)) { | |
932 | $section->name = $data->name; | |
933 | } | |
934 | if (empty($secrec->summary)) { | |
935 | $section->summary = $data->summary; | |
936 | $section->summaryformat = $data->summaryformat; | |
937 | $restorefiles = true; | |
938 | } | |
939 | $DB->update_record('course_sections', $section); | |
940 | $newitemid = $secrec->id; | |
941 | } | |
942 | ||
943 | // Annotate the section mapping, with restorefiles option if needed | |
944 | $this->set_mapping('course_section', $oldid, $newitemid, $restorefiles); | |
945 | ||
946 | // If needed, adjust course->numsections | |
947 | if ($numsections = $DB->get_field('course', 'numsections', array('id' => $this->get_courseid()))) { | |
948 | if ($numsections < $section->section) { | |
949 | $DB->set_field('course', 'numsections', $section->section, array('id' => $this->get_courseid())); | |
950 | } | |
951 | } | |
952 | } | |
953 | ||
954 | protected function after_execute() { | |
955 | // Add section related files, with 'course_section' itemid to match | |
956 | $this->add_related_files('course', 'section', 'course_section'); | |
957 | } | |
958 | } | |
959 | ||
960 | ||
961 | /** | |
482aac65 | 962 | * Structure step that will read the course.xml file, loading it and performing |
395dae30 EL |
963 | * various actions depending of the site/restore settings. Note that target |
964 | * course always exist before arriving here so this step will be updating | |
965 | * the course record (never inserting) | |
482aac65 EL |
966 | */ |
967 | class restore_course_structure_step extends restore_structure_step { | |
968 | ||
969 | protected function define_structure() { | |
970 | ||
971 | $course = new restore_path_element('course', '/course', true); // Grouped | |
972 | $category = new restore_path_element('category', '/course/category'); | |
973 | $tag = new restore_path_element('tag', '/course/tags/tag'); | |
974 | $allowed = new restore_path_element('allowed', '/course/allowed_modules/module'); | |
975 | ||
976 | return array($course, $category, $tag, $allowed); | |
977 | } | |
978 | ||
7f32340b SH |
979 | /** |
980 | * Processing functions go here | |
981 | * | |
982 | * @global moodledatabase $DB | |
983 | * @param stdClass $data | |
984 | */ | |
482aac65 | 985 | public function process_course($data) { |
395dae30 EL |
986 | global $CFG, $DB; |
987 | ||
988 | $data = (object)$data; | |
989 | $coursetags = isset($data->tags['tag']) ? $data->tags['tag'] : array(); | |
990 | $coursemodules = isset($data->allowed_modules['module']) ? $data->allowed_modules['module'] : array(); | |
991 | $oldid = $data->id; // We'll need this later | |
785d6603 SH |
992 | |
993 | $fullname = $this->get_setting_value('course_fullname'); | |
994 | $shortname = $this->get_setting_value('course_shortname'); | |
b1eaf633 | 995 | $startdate = $this->get_setting_value('course_startdate'); |
395dae30 EL |
996 | |
997 | // Calculate final course names, to avoid dupes | |
998 | list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname); | |
999 | ||
1000 | // Need to change some fields before updating the course record | |
1001 | $data->id = $this->get_courseid(); | |
1002 | $data->fullname = $fullname; | |
1003 | $data->shortname= $shortname; | |
1004 | $data->idnumber = ''; | |
7f32340b SH |
1005 | |
1006 | // Category is set by UI when choosing the destination | |
1007 | unset($data->category); | |
1008 | ||
395dae30 EL |
1009 | $data->startdate= $this->apply_date_offset($data->startdate); |
1010 | if ($data->defaultgroupingid) { | |
1011 | $data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid); | |
1012 | } | |
5095f325 | 1013 | if (empty($CFG->enablecompletion)) { |
395dae30 EL |
1014 | $data->enablecompletion = 0; |
1015 | $data->completionstartonenrol = 0; | |
1016 | $data->completionnotify = 0; | |
1017 | } | |
1018 | $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search | |
1019 | if (!array_key_exists($data->lang, $languages)) { | |
1020 | $data->lang = ''; | |
1021 | } | |
1022 | $themes = get_list_of_themes(); // Get themes for quick search later | |
1023 | if (!in_array($data->theme, $themes) || empty($CFG->allowcoursethemes)) { | |
1024 | $data->theme = ''; | |
1025 | } | |
1026 | ||
1027 | // Course record ready, update it | |
1028 | $DB->update_record('course', $data); | |
1029 | ||
1030 | // Course tags | |
1031 | if (!empty($CFG->usetags) && isset($coursetags)) { // if enabled in server and present in backup | |
1032 | $tags = array(); | |
1033 | foreach ($coursetags as $coursetag) { | |
1034 | $coursetag = (object)$coursetag; | |
1035 | $tags[] = $coursetag->rawname; | |
1036 | } | |
1037 | tag_set('course', $this->get_courseid(), $tags); | |
1038 | } | |
1039 | // Course allowed modules | |
1040 | if (!empty($data->restrictmodules) && !empty($coursemodules)) { | |
1041 | $available = get_plugin_list('mod'); | |
1042 | foreach ($coursemodules as $coursemodule) { | |
1043 | $mname = $coursemodule['modulename']; | |
1044 | if (array_key_exists($mname, $available)) { | |
1045 | if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) { | |
1046 | $rec = new stdclass(); | |
1047 | $rec->course = $this->get_courseid(); | |
1048 | $rec->module = $module->id; | |
1049 | if (!$DB->record_exists('course_allowed_modules', (array)$rec)) { | |
1050 | $DB->insert_record('course_allowed_modules', $rec); | |
1051 | } | |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | // Role name aliases | |
1057 | restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid()); | |
482aac65 EL |
1058 | } |
1059 | ||
395dae30 EL |
1060 | protected function after_execute() { |
1061 | // Add course related files, without itemid to match | |
1062 | $this->add_related_files('course', 'summary', null); | |
1063 | $this->add_related_files('course', 'legacy', null); | |
1064 | } | |
482aac65 | 1065 | } |
024c288d EL |
1066 | |
1067 | ||
1068 | /* | |
1069 | * Structure step that will read the roles.xml file (at course/activity/block levels) | |
1070 | * containig all the role_assignments and overrides for that context. If corresponding to | |
1071 | * one mapped role, they will be applied to target context. Will observe the role_assignments | |
1072 | * setting to decide if ras are restored. | |
1073 | * Note: only ras with component == null are restored as far as the any ra with component | |
1074 | * is handled by one enrolment plugin, hence it will createt the ras later | |
1075 | */ | |
1076 | class restore_ras_and_caps_structure_step extends restore_structure_step { | |
1077 | ||
1078 | protected function define_structure() { | |
1079 | ||
1080 | $paths = array(); | |
1081 | ||
1082 | // Observe the role_assignments setting | |
1083 | if ($this->get_setting_value('role_assignments')) { | |
1084 | $paths[] = new restore_path_element('assignment', '/roles/role_assignments/assignment'); | |
1085 | } | |
1086 | $paths[] = new restore_path_element('override', '/roles/role_overrides/override'); | |
1087 | ||
1088 | return $paths; | |
1089 | } | |
1090 | ||
f2a9be5f EL |
1091 | /** |
1092 | * Assign roles | |
1093 | * | |
1094 | * This has to be called after enrolments processing. | |
1095 | * | |
1096 | * @param mixed $data | |
1097 | * @return void | |
1098 | */ | |
024c288d | 1099 | public function process_assignment($data) { |
28b6ff82 EL |
1100 | global $DB; |
1101 | ||
024c288d EL |
1102 | $data = (object)$data; |
1103 | ||
1104 | // Check roleid, userid are one of the mapped ones | |
f2a9be5f EL |
1105 | if (!$newroleid = $this->get_mappingid('role', $data->roleid)) { |
1106 | return; | |
1107 | } | |
1108 | if (!$newuserid = $this->get_mappingid('user', $data->userid)) { | |
1109 | return; | |
1110 | } | |
1111 | if (!$DB->record_exists('user', array('id' => $newuserid, 'deleted' => 0))) { | |
28b6ff82 | 1112 | // Only assign roles to not deleted users |
f2a9be5f EL |
1113 | return; |
1114 | } | |
1115 | if (!$contextid = $this->task->get_contextid()) { | |
1116 | return; | |
1117 | } | |
1118 | ||
1119 | if (empty($data->component)) { | |
1120 | // assign standard manual roles | |
1121 | // TODO: role_assign() needs one userid param to be able to specify our restore userid | |
1122 | role_assign($newroleid, $newuserid, $contextid); | |
1123 | ||
1124 | } else if ((strpos($data->component, 'enrol_') === 0)) { | |
1125 | // Deal with enrolment roles | |
1126 | if ($enrolid = $this->get_mappingid('enrol', $data->itemid)) { | |
1127 | if ($component = $DB->get_field('enrol', 'component', array('id'=>$enrolid))) { | |
1128 | //note: we have to verify component because it might have changed | |
1129 | if ($component === 'enrol_manual') { | |
1130 | // manual is a special case, we do not use components - this owudl happen when converting from other plugin | |
1131 | role_assign($newroleid, $newuserid, $contextid); //TODO: do we need modifierid? | |
1132 | } else { | |
1133 | role_assign($newroleid, $newuserid, $contextid, $component, $enrolid); //TODO: do we need modifierid? | |
1134 | } | |
1135 | } | |
28b6ff82 | 1136 | } |
024c288d EL |
1137 | } |
1138 | } | |
1139 | ||
1140 | public function process_override($data) { | |
1141 | $data = (object)$data; | |
1142 | ||
1143 | // Check roleid is one of the mapped ones | |
1144 | $newroleid = $this->get_mappingid('role', $data->roleid); | |
b8e455a7 EL |
1145 | // If newroleid and context are valid assign it via API (it handles dupes and so on) |
1146 | if ($newroleid && $this->task->get_contextid()) { | |
024c288d | 1147 | // TODO: assign_capability() needs one userid param to be able to specify our restore userid |
b8e455a7 | 1148 | // TODO: it seems that assign_capability() doesn't check for valid capabilities at all ??? |
024c288d EL |
1149 | assign_capability($data->capability, $data->permission, $newroleid, $this->task->get_contextid()); |
1150 | } | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | /** | |
1155 | * This structure steps restores the enrol plugins and their underlying | |
1156 | * enrolments, performing all the mappings and/or movements required | |
1157 | */ | |
1158 | class restore_enrolments_structure_step extends restore_structure_step { | |
1159 | ||
1160 | protected function define_structure() { | |
1161 | ||
1162 | $paths = array(); | |
1163 | ||
1164 | $paths[] = new restore_path_element('enrol', '/enrolments/enrols/enrol'); | |
1165 | $paths[] = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment'); | |
1166 | ||
1167 | return $paths; | |
1168 | } | |
1169 | ||
f2a9be5f EL |
1170 | /** |
1171 | * Create enrolment instances. | |
1172 | * | |
1173 | * This has to be called after creation of roles | |
1174 | * and before adding of role assignments. | |
1175 | * | |
1176 | * @param mixed $data | |
1177 | * @return void | |
1178 | */ | |
024c288d EL |
1179 | public function process_enrol($data) { |
1180 | global $DB; | |
1181 | ||
1182 | $data = (object)$data; | |
1183 | $oldid = $data->id; // We'll need this later | |
1184 | ||
f2a9be5f EL |
1185 | $restoretype = plugin_supports('enrol', $data->enrol, ENROL_RESTORE_TYPE, null); |
1186 | ||
1187 | if ($restoretype !== ENROL_RESTORE_EXACT and $restoretype !== ENROL_RESTORE_NOUSERS) { | |
1188 | // TODO: add complex restore support via custom class | |
1189 | debugging("Skipping '{$data->enrol}' enrolment plugin. Will be implemented before 2.0 release", DEBUG_DEVELOPER); | |
1190 | $this->set_mapping('enrol', $oldid, 0); | |
024c288d EL |
1191 | return; |
1192 | } | |
1193 | ||
1194 | // Perform various checks to decide what to do with the enrol plugin | |
f2a9be5f EL |
1195 | if (!array_key_exists($data->enrol, enrol_get_plugins(false))) { |
1196 | // TODO: decide if we want to switch to manual enrol - we need UI for this | |
1197 | debugging("Enrol plugin data can not be restored because it is not installed"); | |
1198 | $this->set_mapping('enrol', $oldid, 0); | |
1199 | return; | |
1200 | ||
024c288d | 1201 | } |
f2a9be5f EL |
1202 | if (!enrol_is_enabled($data->enrol)) { |
1203 | // TODO: decide if we want to switch to manual enrol - we need UI for this | |
1204 | debugging("Enrol plugin data can not be restored because it is not enabled"); | |
1205 | $this->set_mapping('enrol', $oldid, 0); | |
1206 | return; | |
1207 | } | |
1208 | ||
1209 | // map standard fields - plugin has to process custom fields from own restore class | |
1210 | $data->roleid = $this->get_mappingid('role', $data->roleid); | |
1211 | //TODO: should we move the enrol start and end date here? | |
1212 | ||
1213 | // always add instance, if the course does not support multiple instances it just returns NULL | |
1214 | $enrol = enrol_get_plugin($data->enrol); | |
1215 | $courserec = $DB->get_record('course', array('id' => $this->get_courseid())); // Requires object, uses only id!! | |
1216 | if ($newitemid = $enrol->add_instance($courserec, (array)$data)) { | |
1217 | // ok | |
1218 | } else { | |
1219 | if ($instances = $DB->get_records('enrol', array('courseid'=>$courserec->id, 'enrol'=>$data->enrol))) { | |
1220 | // most probably plugin that supports only one instance | |
1221 | $newitemid = key($instances); | |
024c288d | 1222 | } else { |
f2a9be5f EL |
1223 | debugging('Can not create new enrol instance or reuse existing'); |
1224 | $newitemid = 0; | |
024c288d | 1225 | } |
f2a9be5f | 1226 | } |
024c288d | 1227 | |
f2a9be5f EL |
1228 | if ($restoretype === ENROL_RESTORE_NOUSERS) { |
1229 | // plugin requests to prevent restore of any users | |
024c288d EL |
1230 | $newitemid = 0; |
1231 | } | |
f2a9be5f | 1232 | |
024c288d EL |
1233 | $this->set_mapping('enrol', $oldid, $newitemid); |
1234 | } | |
1235 | ||
f2a9be5f EL |
1236 | /** |
1237 | * Create user enrolments | |
1238 | * | |
1239 | * This has to be called after creation of enrolment instances | |
1240 | * and before adding of role assignments. | |
1241 | * | |
1242 | * @param mixed $data | |
1243 | * @return void | |
1244 | */ | |
024c288d EL |
1245 | public function process_enrolment($data) { |
1246 | global $DB; | |
1247 | ||
1248 | $data = (object)$data; | |
1249 | ||
1250 | // Process only if parent instance have been mapped | |
1251 | if ($enrolid = $this->get_new_parentid('enrol')) { | |
f2a9be5f EL |
1252 | if ($instance = $DB->get_record('enrol', array('id'=>$enrolid))) { |
1253 | // And only if user is a mapped one | |
1254 | if ($userid = $this->get_mappingid('user', $data->userid)) { | |
1255 | $enrol = enrol_get_plugin($instance->enrol); | |
1256 | //TODO: do we need specify modifierid? | |
1257 | $enrol->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status); | |
1258 | //note: roles are assigned in restore_ras_and_caps_structure_step::process_assignment() processing above | |
024c288d EL |
1259 | } |
1260 | } | |
1261 | } | |
1262 | } | |
1263 | } | |
21e51c86 EL |
1264 | |
1265 | ||
1266 | /** | |
1267 | * This structure steps restores the filters and their configs | |
1268 | */ | |
1269 | class restore_filters_structure_step extends restore_structure_step { | |
1270 | ||
1271 | protected function define_structure() { | |
1272 | ||
1273 | $paths = array(); | |
1274 | ||
1275 | $paths[] = new restore_path_element('active', '/filters/filter_actives/filter_active'); | |
1276 | $paths[] = new restore_path_element('config', '/filters/filter_configs/filter_config'); | |
1277 | ||
1278 | return $paths; | |
1279 | } | |
1280 | ||
1281 | public function process_active($data) { | |
1282 | ||
1283 | $data = (object)$data; | |
1284 | ||
1285 | if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do | |
1286 | return; | |
1287 | } | |
1288 | filter_set_local_state($data->filter, $this->task->get_contextid(), $data->active); | |
1289 | } | |
1290 | ||
1291 | public function process_config($data) { | |
1292 | ||
1293 | $data = (object)$data; | |
1294 | ||
1295 | if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do | |
1296 | return; | |
1297 | } | |
1298 | filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value); | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | ||
1303 | /** | |
1304 | * This structure steps restores the comments | |
1305 | * Note: Cannot use the comments API because defaults to USER->id. | |
1306 | * That should change allowing to pass $userid | |
1307 | */ | |
1308 | class restore_comments_structure_step extends restore_structure_step { | |
1309 | ||
1310 | protected function define_structure() { | |
1311 | ||
1312 | $paths = array(); | |
1313 | ||
1314 | $paths[] = new restore_path_element('comment', '/comments/comment'); | |
1315 | ||
1316 | return $paths; | |
1317 | } | |
1318 | ||
1319 | public function process_comment($data) { | |
1320 | global $DB; | |
1321 | ||
1322 | $data = (object)$data; | |
1323 | ||
1324 | // First of all, if the comment has some itemid, ask to the task what to map | |
1325 | $mapping = false; | |
21e51c86 | 1326 | if ($data->itemid) { |
39aa0280 EL |
1327 | $mapping = $this->task->get_comment_mapping_itemname($data->commentarea); |
1328 | $data->itemid = $this->get_mappingid($mapping, $data->itemid); | |
21e51c86 EL |
1329 | } |
1330 | // Only restore the comment if has no mapping OR we have found the matching mapping | |
39aa0280 | 1331 | if (!$mapping || $data->itemid) { |
b8e455a7 EL |
1332 | // Only if user mapping and context |
1333 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1334 | if ($data->userid && $this->task->get_contextid()) { | |
21e51c86 | 1335 | $data->contextid = $this->task->get_contextid(); |
b8e455a7 EL |
1336 | // Only if there is another comment with same context/user/timecreated |
1337 | $params = array('contextid' => $data->contextid, 'userid' => $data->userid, 'timecreated' => $data->timecreated); | |
1338 | if (!$DB->record_exists('comments', $params)) { | |
1339 | $DB->insert_record('comments', $data); | |
1340 | } | |
1341 | } | |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | ||
bd39b6f2 SH |
1346 | class restore_course_completion_structure_step extends restore_structure_step { |
1347 | ||
1348 | /** | |
1349 | * Conditionally decide if this step should be executed. | |
1350 | * | |
1351 | * This function checks parameters that are not immediate settings to ensure | |
1352 | * that the enviroment is suitable for the restore of course completion info. | |
1353 | * | |
1354 | * This function checks the following four parameters: | |
1355 | * | |
1356 | * 1. Course completion is enabled on the site | |
1357 | * 2. The backup includes course completion information | |
1358 | * 3. All modules are restorable | |
1359 | * 4. All modules are marked for restore. | |
1360 | * | |
1361 | * @return bool True is safe to execute, false otherwise | |
1362 | */ | |
1363 | protected function execute_condition() { | |
1364 | global $CFG; | |
1365 | ||
1366 | // First check course completion is enabled on this site | |
1367 | if (empty($CFG->enablecompletion)) { | |
1368 | // Disabled, don't restore course completion | |
1369 | return false; | |
1370 | } | |
1371 | ||
1372 | // Check it is included in the backup | |
1373 | $fullpath = $this->task->get_taskbasepath(); | |
1374 | $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; | |
1375 | if (!file_exists($fullpath)) { | |
1376 | // Not found, can't restore course completion | |
1377 | return false; | |
1378 | } | |
1379 | ||
1380 | // Check we are able to restore all backed up modules | |
1381 | if ($this->task->is_missing_modules()) { | |
1382 | return false; | |
1383 | } | |
1384 | ||
1385 | // Finally check all modules within the backup are being restored. | |
1386 | if ($this->task->is_excluding_activities()) { | |
1387 | return false; | |
1388 | } | |
1389 | ||
1390 | return true; | |
1391 | } | |
1392 | ||
1393 | /** | |
1394 | * Define the course completion structure | |
1395 | * | |
1396 | * @return array Array of restore_path_element | |
1397 | */ | |
1398 | protected function define_structure() { | |
1399 | ||
1400 | // To know if we are including user completion info | |
1401 | $userinfo = $this->get_setting_value('userscompletion'); | |
1402 | ||
1403 | $paths = array(); | |
1404 | $paths[] = new restore_path_element('course_completion_criteria', '/course_completion/course_completion_criteria'); | |
1405 | $paths[] = new restore_path_element('course_completion_notify', '/course_completion/course_completion_notify'); | |
1406 | $paths[] = new restore_path_element('course_completion_aggr_methd', '/course_completion/course_completion_aggr_methd'); | |
1407 | ||
1408 | if ($userinfo) { | |
1409 | $paths[] = new restore_path_element('course_completion_crit_compl', '/course_completion/course_completion_criteria/course_completion_crit_completions/course_completion_crit_compl'); | |
1410 | $paths[] = new restore_path_element('course_completions', '/course_completion/course_completions'); | |
1411 | } | |
1412 | ||
1413 | return $paths; | |
1414 | ||
1415 | } | |
1416 | ||
1417 | /** | |
1418 | * Process course completion criteria | |
1419 | * | |
1420 | * @global moodle_database $DB | |
1421 | * @param stdClass $data | |
1422 | */ | |
1423 | public function process_course_completion_criteria($data) { | |
1424 | global $DB; | |
1425 | ||
1426 | $data = (object)$data; | |
1427 | $data->course = $this->get_courseid(); | |
1428 | ||
1429 | // Apply the date offset to the time end field | |
1430 | $data->timeend = $this->apply_date_offset($data->timeend); | |
1431 | ||
1432 | // Map the role from the criteria | |
1433 | if (!empty($data->role)) { | |
1434 | $data->role = $this->get_mappingid('role', $data->role); | |
1435 | } | |
1436 | ||
aa548318 SH |
1437 | $skipcriteria = false; |
1438 | ||
bd39b6f2 SH |
1439 | // If the completion criteria is for a module we need to map the module instance |
1440 | // to the new module id. | |
1441 | if (!empty($data->moduleinstance) && !empty($data->module)) { | |
1442 | $data->moduleinstance = $this->get_mappingid('course_module', $data->moduleinstance); | |
aa548318 SH |
1443 | if (empty($data->moduleinstance)) { |
1444 | $skipcriteria = true; | |
1445 | } | |
bd39b6f2 SH |
1446 | } else { |
1447 | $data->module = null; | |
1448 | $data->moduleinstance = null; | |
1449 | } | |
1450 | ||
1451 | // We backup the course shortname rather than the ID so that we can match back to the course | |
bd39b6f2 SH |
1452 | if (!empty($data->courseinstanceshortname)) { |
1453 | $courseinstanceid = $DB->get_field('course', 'id', array('shortname'=>$data->courseinstanceshortname)); | |
1454 | if (!$courseinstanceid) { | |
1455 | $skipcriteria = true; | |
1456 | } | |
1457 | } else { | |
1458 | $courseinstanceid = null; | |
1459 | } | |
1460 | $data->courseinstance = $courseinstanceid; | |
1461 | ||
1462 | if (!$skipcriteria) { | |
1463 | $params = array( | |
1464 | 'course' => $data->course, | |
1465 | 'criteriatype' => $data->criteriatype, | |
1466 | 'enrolperiod' => $data->enrolperiod, | |
1467 | 'courseinstance' => $data->courseinstance, | |
1468 | 'module' => $data->module, | |
1469 | 'moduleinstance' => $data->moduleinstance, | |
1470 | 'timeend' => $data->timeend, | |
1471 | 'gradepass' => $data->gradepass, | |
1472 | 'role' => $data->role | |
1473 | ); | |
1474 | $newid = $DB->insert_record('course_completion_criteria', $params); | |
1475 | $this->set_mapping('course_completion_criteria', $data->id, $newid); | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | /** | |
1480 | * Processes course compltion criteria complete records | |
1481 | * | |
1482 | * @global moodle_database $DB | |
1483 | * @param stdClass $data | |
1484 | */ | |
1485 | public function process_course_completion_crit_compl($data) { | |
1486 | global $DB; | |
1487 | ||
1488 | $data = (object)$data; | |
1489 | ||
1490 | // This may be empty if criteria could not be restored | |
1491 | $data->criteriaid = $this->get_mappingid('course_completion_criteria', $data->criteriaid); | |
034ef761 | 1492 | |
bd39b6f2 SH |
1493 | $data->course = $this->get_courseid(); |
1494 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1495 | ||
1496 | if (!empty($data->criteriaid) && !empty($data->userid)) { | |
1497 | $params = array( | |
1498 | 'userid' => $data->userid, | |
1499 | 'course' => $data->course, | |
1500 | 'criteriaid' => $data->criteriaid, | |
1501 | 'timecompleted' => $this->apply_date_offset($data->timecompleted) | |
1502 | ); | |
1503 | if (isset($data->gradefinal)) { | |
1504 | $params['gradefinal'] = $data->gradefinal; | |
1505 | } | |
1506 | if (isset($data->unenroled)) { | |
1507 | $params['unenroled'] = $data->unenroled; | |
1508 | } | |
1509 | if (isset($data->deleted)) { | |
1510 | $params['deleted'] = $data->deleted; | |
1511 | } | |
1512 | $DB->insert_record('course_completion_crit_compl', $params); | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | /** | |
1517 | * Process course completions | |
1518 | * | |
1519 | * @global moodle_database $DB | |
1520 | * @param stdClass $data | |
1521 | */ | |
1522 | public function process_course_completions($data) { | |
1523 | global $DB; | |
1524 | ||
1525 | $data = (object)$data; | |
1526 | ||
1527 | $data->course = $this->get_courseid(); | |
1528 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1529 | ||
1530 | if (!empty($data->userid)) { | |
1531 | $params = array( | |
1532 | 'userid' => $data->userid, | |
1533 | 'course' => $data->course, | |
1534 | 'deleted' => $data->deleted, | |
1535 | 'timenotified' => $this->apply_date_offset($data->timenotified), | |
1536 | 'timeenrolled' => $this->apply_date_offset($data->timeenrolled), | |
1537 | 'timestarted' => $this->apply_date_offset($data->timestarted), | |
1538 | 'timecompleted' => $this->apply_date_offset($data->timecompleted), | |
1539 | 'reaggregate' => $data->reaggregate | |
1540 | ); | |
1541 | $DB->insert_record('course_completions', $params); | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | /** | |
1546 | * Process course completion notification records. | |
1547 | * | |
1548 | * Note: As of Moodle 2.0 this table is not being used however it has been | |
1549 | * left in in the hopes that one day the functionality there will be completed | |
1550 | * | |
1551 | * @global moodle_database $DB | |
1552 | * @param stdClass $data | |
1553 | */ | |
1554 | public function process_course_completion_notify($data) { | |
1555 | global $DB; | |
1556 | ||
1557 | $data = (object)$data; | |
1558 | ||
1559 | $data->course = $this->get_courseid(); | |
1560 | if (!empty($data->role)) { | |
1561 | $data->role = $this->get_mappingid('role', $data->role); | |
1562 | } | |
1563 | ||
1564 | $params = array( | |
1565 | 'course' => $data->course, | |
1566 | 'role' => $data->role, | |
1567 | 'message' => $data->message, | |
1568 | 'timesent' => $this->apply_date_offset($data->timesent), | |
1569 | ); | |
1570 | $DB->insert_record('course_completion_notify', $params); | |
1571 | } | |
1572 | ||
1573 | /** | |
1574 | * Process course completion aggregate methods | |
1575 | * | |
1576 | * @global moodle_database $DB | |
1577 | * @param stdClass $data | |
1578 | */ | |
1579 | public function process_course_completion_aggr_methd($data) { | |
1580 | global $DB; | |
1581 | ||
1582 | $data = (object)$data; | |
1583 | ||
1584 | $data->course = $this->get_courseid(); | |
1585 | ||
1586 | $params = array( | |
1587 | 'course' => $data->course, | |
1588 | 'criteriatype' => $data->criteriatype, | |
1589 | 'method' => $data->method, | |
1590 | 'value' => $data->value, | |
1591 | ); | |
1592 | $DB->insert_record('course_completion_aggr_methd', $params); | |
1593 | } | |
1594 | ||
1595 | } | |
1596 | ||
9a1cfcbc EL |
1597 | /** |
1598 | * This structure step restores the grade items associated with one activity | |
1599 | * All the grade items are made child of the "course" grade item but the original | |
1600 | * categoryid is saved as parentitemid in the backup_ids table, so, when restoring | |
1601 | * the complete gradebook (categories and calculations), that information is | |
1602 | * available there | |
1603 | */ | |
1604 | class restore_activity_grades_structure_step extends restore_structure_step { | |
1605 | ||
1606 | protected function define_structure() { | |
1607 | ||
1608 | $paths = array(); | |
1609 | $userinfo = $this->get_setting_value('userinfo'); | |
1610 | ||
1611 | $paths[] = new restore_path_element('grade_item', '/activity_gradebook/grade_items/grade_item'); | |
1612 | $paths[] = new restore_path_element('grade_letter', '/activity_gradebook/grade_letters/grade_letter'); | |
1613 | if ($userinfo) { | |
1614 | $paths[] = new restore_path_element('grade_grade', | |
1615 | '/activity_gradebook/grade_items/grade_item/grade_grades/grade_grade'); | |
1616 | } | |
1617 | return $paths; | |
1618 | } | |
1619 | ||
1620 | protected function process_grade_item($data) { | |
1621 | ||
1622 | $data = (object)($data); | |
1623 | $oldid = $data->id; // We'll need these later | |
1624 | $oldparentid = $data->categoryid; | |
1625 | ||
1626 | // make sure top course category exists, all grade items will be associated | |
1627 | // to it. Later, if restoring the whole gradebook, categories will be introduced | |
1628 | $coursecat = grade_category::fetch_course_category($this->get_courseid()); | |
1629 | $coursecatid = $coursecat->id; // Get the categoryid to be used | |
1630 | ||
1631 | unset($data->id); | |
1632 | $data->categoryid = $coursecatid; | |
1633 | $data->courseid = $this->get_courseid(); | |
1634 | $data->iteminstance = $this->task->get_activityid(); | |
1635 | // Don't get any idnumber from course module. Keep them as they are in grade_item->idnumber | |
1636 | // Reason: it's not clear what happens with outcomes->idnumber or activities with multiple items (workshop) | |
1637 | // so the best is to keep the ones already in the gradebook | |
1638 | // Potential problem: duplicates if same items are restored more than once. :-( | |
1639 | // This needs to be fixed in some way (outcomes & activities with multiple items) | |
1640 | // $data->idnumber = get_coursemodule_from_instance($data->itemmodule, $data->iteminstance)->idnumber; | |
1641 | // In any case, verify always for uniqueness | |
a789ac6f | 1642 | $data->idnumber = grade_verify_idnumber($data->idnumber, $this->get_courseid()) ? $data->idnumber : null; |
9a1cfcbc EL |
1643 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); |
1644 | $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid); | |
1645 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
1646 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
1647 | ||
1648 | $gradeitem = new grade_item($data); | |
1649 | $gradeitem->insert('restore'); | |
4d787d8a AD |
1650 | |
1651 | //sortorder is automatically assigned when inserting. Re-instate the previous sortorder | |
1652 | $gradeitem->sortorder = $data->sortorder; | |
1653 | $gradeitem->update('restore'); | |
1654 | ||
dc362c44 EL |
1655 | // Set mapping, saving the original category id into parentitemid |
1656 | // gradebook restore (final task) will need it to reorganise items | |
1657 | $this->set_mapping('grade_item', $oldid, $gradeitem->id, false, null, $oldparentid); | |
9a1cfcbc EL |
1658 | } |
1659 | ||
1660 | protected function process_grade_grade($data) { | |
1661 | $data = (object)($data); | |
1662 | ||
1663 | unset($data->id); | |
1664 | $data->itemid = $this->get_new_parentid('grade_item'); | |
1665 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1666 | $data->usermodified = $this->get_mappingid('user', $data->usermodified); | |
1667 | $data->rawscaleid = $this->get_mappingid('scale', $data->rawscaleid); | |
1668 | // TODO: Ask, all the rest of locktime/exported... work with time... to be rolled? | |
1669 | $data->overridden = $this->apply_date_offset($data->overridden); | |
1670 | ||
1671 | $grade = new grade_grade($data); | |
1672 | $grade->insert('restore'); | |
1673 | // no need to save any grade_grade mapping | |
1674 | } | |
1675 | ||
1676 | /** | |
1677 | * process activity grade_letters. Note that, while these are possible, | |
1678 | * because grade_letters are contextid based, in proctice, only course | |
1679 | * context letters can be defined. So we keep here this method knowing | |
1680 | * it won't be executed ever. gradebook restore will restore course letters. | |
1681 | */ | |
1682 | protected function process_grade_letter($data) { | |
1683 | global $DB; | |
1684 | ||
1685 | $data = (object)$data; | |
1686 | ||
1687 | $data->contextid = $this->task->get_contextid(); | |
1688 | $newitemid = $DB->insert_record('grade_letters', $data); | |
1689 | // no need to save any grade_letter mapping | |
1690 | } | |
1691 | } | |
1692 | ||
b8e455a7 EL |
1693 | |
1694 | /** | |
1695 | * This structure steps restores one instance + positions of one block | |
1696 | * Note: Positions corresponding to one existing context are restored | |
1697 | * here, but all the ones having unknown contexts are sent to backup_ids | |
1698 | * for a later chance to be restored at the end (final task) | |
1699 | */ | |
1700 | class restore_block_instance_structure_step extends restore_structure_step { | |
1701 | ||
1702 | protected function define_structure() { | |
1703 | ||
1704 | $paths = array(); | |
1705 | ||
1706 | $paths[] = new restore_path_element('block', '/block', true); // Get the whole XML together | |
1707 | $paths[] = new restore_path_element('block_position', '/block/block_positions/block_position'); | |
1708 | ||
1709 | return $paths; | |
1710 | } | |
1711 | ||
1712 | public function process_block($data) { | |
0f8941e2 | 1713 | global $DB, $CFG; |
b8e455a7 EL |
1714 | |
1715 | $data = (object)$data; // Handy | |
1716 | $oldcontextid = $data->contextid; | |
1717 | $oldid = $data->id; | |
1718 | $positions = isset($data->block_positions['block_position']) ? $data->block_positions['block_position'] : array(); | |
1719 | ||
1720 | // Look for the parent contextid | |
1721 | if (!$data->parentcontextid = $this->get_mappingid('context', $data->parentcontextid)) { | |
1722 | throw new restore_step_exception('restore_block_missing_parent_ctx', $data->parentcontextid); | |
1723 | } | |
1724 | ||
767cb7f0 | 1725 | // TODO: it would be nice to use standard plugin supports instead of this instance_allow_multiple() |
b8e455a7 EL |
1726 | // If there is already one block of that type in the parent context |
1727 | // and the block is not multiple, stop processing | |
767cb7f0 EL |
1728 | // Use blockslib loader / method executor |
1729 | if (!block_method_result($data->blockname, 'instance_allow_multiple')) { | |
0f8941e2 EL |
1730 | if ($DB->record_exists_sql("SELECT bi.id |
1731 | FROM {block_instances} bi | |
1732 | JOIN {block} b ON b.name = bi.blockname | |
1733 | WHERE bi.parentcontextid = ? | |
1734 | AND bi.blockname = ?", array($data->parentcontextid, $data->blockname))) { | |
1735 | return false; | |
1736 | } | |
b8e455a7 EL |
1737 | } |
1738 | ||
1739 | // If there is already one block of that type in the parent context | |
1740 | // with the same showincontexts, pagetypepattern, subpagepattern, defaultregion and configdata | |
1741 | // stop processing | |
1742 | $params = array( | |
1743 | 'blockname' => $data->blockname, 'parentcontextid' => $data->parentcontextid, | |
1744 | 'showinsubcontexts' => $data->showinsubcontexts, 'pagetypepattern' => $data->pagetypepattern, | |
1745 | 'subpagepattern' => $data->subpagepattern, 'defaultregion' => $data->defaultregion); | |
1746 | if ($birecs = $DB->get_records('block_instances', $params)) { | |
1747 | foreach($birecs as $birec) { | |
1748 | if ($birec->configdata == $data->configdata) { | |
1749 | return false; | |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | // Set task old contextid, blockid and blockname once we know them | |
1755 | $this->task->set_old_contextid($oldcontextid); | |
1756 | $this->task->set_old_blockid($oldid); | |
1757 | $this->task->set_blockname($data->blockname); | |
1758 | ||
1759 | // Let's look for anything within configdata neededing processing | |
1760 | // (nulls and uses of legacy file.php) | |
1761 | if ($attrstotransform = $this->task->get_configdata_encoded_attributes()) { | |
1762 | $configdata = (array)unserialize(base64_decode($data->configdata)); | |
1763 | foreach ($configdata as $attribute => $value) { | |
1764 | if (in_array($attribute, $attrstotransform)) { | |
1765 | $configdata[$attribute] = $this->contentprocessor->process_cdata($value); | |
1766 | } | |
1767 | } | |
1768 | $data->configdata = base64_encode(serialize((object)$configdata)); | |
1769 | } | |
1770 | ||
1771 | // Create the block instance | |
1772 | $newitemid = $DB->insert_record('block_instances', $data); | |
1773 | // Save the mapping (with restorefiles support) | |
1774 | $this->set_mapping('block_instance', $oldid, $newitemid, true); | |
1775 | // Create the block context | |
1776 | $newcontextid = get_context_instance(CONTEXT_BLOCK, $newitemid)->id; | |
1777 | // Save the block contexts mapping and sent it to task | |
1778 | $this->set_mapping('context', $oldcontextid, $newcontextid); | |
1779 | $this->task->set_contextid($newcontextid); | |
1780 | $this->task->set_blockid($newitemid); | |
1781 | ||
1782 | // Restore block fileareas if declared | |
1783 | $component = 'block_' . $this->task->get_blockname(); | |
1784 | foreach ($this->task->get_fileareas() as $filearea) { // Simple match by contextid. No itemname needed | |
1785 | $this->add_related_files($component, $filearea, null); | |
1786 | } | |
1787 | ||
1788 | // Process block positions, creating them or accumulating for final step | |
1789 | foreach($positions as $position) { | |
1790 | $position = (object)$position; | |
1791 | $position->blockinstanceid = $newitemid; // The instance is always the restored one | |
1792 | // If position is for one already mapped (known) contextid | |
1793 | // process it now, creating the position | |
1794 | if ($newpositionctxid = $this->get_mappingid('context', $position->contextid)) { | |
1795 | $position->contextid = $newpositionctxid; | |
1796 | // Create the block position | |
1797 | $DB->insert_record('block_positions', $position); | |
1798 | ||
1799 | // The position belongs to an unknown context, send it to backup_ids | |
1800 | // to process them as part of the final steps of restore. We send the | |
1801 | // whole $position object there, hence use the low level method. | |
1802 | } else { | |
1803 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'block_position', $position->id, 0, null, $position); | |
21e51c86 EL |
1804 | } |
1805 | } | |
1806 | } | |
1807 | } | |
394edb7e EL |
1808 | |
1809 | /** | |
1810 | * Structure step to restore common course_module information | |
1811 | * | |
1812 | * This step will process the module.xml file for one activity, in order to restore | |
1813 | * the corresponding information to the course_modules table, skipping various bits | |
1814 | * of information based on CFG settings (groupings, completion...) in order to fullfill | |
1815 | * all the reqs to be able to create the context to be used by all the rest of steps | |
1816 | * in the activity restore task | |
1817 | */ | |
1818 | class restore_module_structure_step extends restore_structure_step { | |
1819 | ||
1820 | protected function define_structure() { | |
1821 | global $CFG; | |
1822 | ||
1823 | $paths = array(); | |
1824 | ||
1825 | $paths[] = new restore_path_element('module', '/module'); | |
1826 | if ($CFG->enableavailability) { | |
1827 | $paths[] = new restore_path_element('availability', '/module/availability_info/availability'); | |
1828 | } | |
1829 | ||
1830 | return $paths; | |
1831 | } | |
1832 | ||
1833 | protected function process_module($data) { | |
1834 | global $CFG, $DB; | |
1835 | ||
1836 | $data = (object)$data; | |
1837 | $oldid = $data->id; | |
1838 | ||
1839 | $data->course = $this->task->get_courseid(); | |
1840 | $data->module = $DB->get_field('modules', 'id', array('name' => $data->modulename)); | |
aa39be20 EL |
1841 | // Map section (first try by course_section mapping match. Useful in course and section restores) |
1842 | $data->section = $this->get_mappingid('course_section', $data->sectionid); | |
1843 | if (!$data->section) { // mapping failed, try to get section by sectionnumber matching | |
1844 | $params = array( | |
1845 | 'course' => $this->get_courseid(), | |
1846 | 'section' => $data->sectionnumber); | |
1847 | $data->section = $DB->get_field('course_sections', 'id', $params); | |
1848 | } | |
1849 | if (!$data->section) { // sectionnumber failed, try to get first section in course | |
1850 | $params = array( | |
1851 | 'course' => $this->get_courseid()); | |
1852 | $data->section = $DB->get_field('course_sections', 'MIN(id)', $params); | |
1853 | } | |
1854 | if (!$data->section) { // no sections in course, create section 0 and 1 and assign module to 1 | |
1855 | $sectionrec = array( | |
1856 | 'course' => $this->get_courseid(), | |
1857 | 'section' => 0); | |
1858 | $DB->insert_record('course_sections', $sectionrec); // section 0 | |
1859 | $sectionrec = array( | |
1860 | 'course' => $this->get_courseid(), | |
1861 | 'section' => 1); | |
1862 | $data->section = $DB->insert_record('course_sections', $sectionrec); // section 1 | |
1863 | } | |
394edb7e EL |
1864 | $data->groupingid= $this->get_mappingid('grouping', $data->groupingid); // grouping |
1865 | if (!$CFG->enablegroupmembersonly) { // observe groupsmemberonly | |
1866 | $data->groupmembersonly = 0; | |
1867 | } | |
1868 | if (!grade_verify_idnumber($data->idnumber, $this->get_courseid())) { // idnumber uniqueness | |
1869 | $data->idnumber = ''; | |
1870 | } | |
5095f325 | 1871 | if (empty($CFG->enablecompletion)) { // completion |
394edb7e EL |
1872 | $data->completion = 0; |
1873 | $data->completiongradeitemnumber = null; | |
1874 | $data->completionview = 0; | |
1875 | $data->completionexpected = 0; | |
1876 | } else { | |
1877 | $data->completionexpected = $this->apply_date_offset($data->completionexpected); | |
1878 | } | |
1879 | if (empty($CFG->enableavailability)) { | |
1880 | $data->availablefrom = 0; | |
1881 | $data->availableuntil = 0; | |
1882 | $data->showavailability = 0; | |
1883 | } else { | |
1884 | $data->availablefrom = $this->apply_date_offset($data->availablefrom); | |
1885 | $data->availableuntil= $this->apply_date_offset($data->availableuntil); | |
1886 | } | |
1887 | $data->instance = 0; // Set to 0 for now, going to create it soon (next step) | |
1888 | ||
1889 | // course_module record ready, insert it | |
1890 | $newitemid = $DB->insert_record('course_modules', $data); | |
1891 | // save mapping | |
1892 | $this->set_mapping('course_module', $oldid, $newitemid); | |
1893 | // set the new course_module id in the task | |
1894 | $this->task->set_moduleid($newitemid); | |
1895 | // we can now create the context safely | |
1896 | $ctxid = get_context_instance(CONTEXT_MODULE, $newitemid)->id; | |
1897 | // set the new context id in the task | |
1898 | $this->task->set_contextid($ctxid); | |
1899 | // update sequence field in course_section | |
1900 | if ($sequence = $DB->get_field('course_sections', 'sequence', array('id' => $data->section))) { | |
1901 | $sequence .= ',' . $newitemid; | |
1902 | } else { | |
1903 | $sequence = $newitemid; | |
1904 | } | |
1905 | $DB->set_field('course_sections', 'sequence', $sequence, array('id' => $data->section)); | |
1906 | } | |
1907 | ||
1908 | ||
1909 | protected function process_availability($data) { | |
394edb7e | 1910 | $data = (object)$data; |
5095f325 EL |
1911 | // Simply going to store the whole availability record now, we'll process |
1912 | // all them later in the final task (once all actvivities have been restored) | |
1913 | // Let's call the low level one to be able to store the whole object | |
1914 | $data->coursemoduleid = $this->task->get_moduleid(); // Let add the availability cmid | |
1915 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_availability', $data->id, 0, null, $data); | |
1916 | } | |
1917 | } | |
1918 | ||
1919 | /** | |
1920 | * Structure step that will process the user activity completion | |
1921 | * information if all these conditions are met: | |
1922 | * - Target site has completion enabled ($CFG->enablecompletion) | |
1923 | * - Activity includes completion info (file_exists) | |
1924 | */ | |
1925 | class restore_userscompletion_structure_step extends restore_structure_step { | |
1926 | ||
1927 | /** | |
1928 | * To conditionally decide if this step must be executed | |
1929 | * Note the "settings" conditions are evaluated in the | |
1930 | * corresponding task. Here we check for other conditions | |
1931 | * not being restore settings (files, site settings...) | |
1932 | */ | |
1933 | protected function execute_condition() { | |
1934 | global $CFG; | |
1935 | ||
1936 | // Completion disabled in this site, don't execute | |
1937 | if (empty($CFG->enablecompletion)) { | |
1938 | return false; | |
1939 | } | |
1940 | ||
1941 | // No user completion info found, don't execute | |
1942 | $fullpath = $this->task->get_taskbasepath(); | |
1943 | $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; | |
1944 | if (!file_exists($fullpath)) { | |
1945 | return false; | |
1946 | } | |
1947 | ||
1948 | // Arrived here, execute the step | |
1949 | return true; | |
1950 | } | |
1951 | ||
1952 | protected function define_structure() { | |
1953 | ||
1954 | $paths = array(); | |
1955 | ||
1956 | $paths[] = new restore_path_element('completion', '/completions/completion'); | |
1957 | ||
1958 | return $paths; | |
1959 | } | |
1960 | ||
1961 | protected function process_completion($data) { | |
1962 | global $DB; | |
1963 | ||
1964 | $data = (object)$data; | |
1965 | ||
1966 | $data->coursemoduleid = $this->task->get_moduleid(); | |
1967 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1968 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
1969 | ||
1970 | $DB->insert_record('course_modules_completion', $data); | |
394edb7e EL |
1971 | } |
1972 | } | |
1973 | ||
1974 | /** | |
1975 | * Abstract structure step, parent of all the activity structure steps. Used to suuport | |
1976 | * the main <activity ...> tag and process it. Also provides subplugin support for | |
1977 | * activities. | |
1978 | */ | |
1979 | abstract class restore_activity_structure_step extends restore_structure_step { | |
1980 | ||
91d11057 EL |
1981 | protected function add_subplugin_structure($subplugintype, $element) { |
1982 | ||
1983 | global $CFG; | |
1984 | ||
1985 | // Check the requested subplugintype is a valid one | |
1986 | $subpluginsfile = $CFG->dirroot . '/mod/' . $this->task->get_modulename() . '/db/subplugins.php'; | |
1987 | if (!file_exists($subpluginsfile)) { | |
1988 | throw new restore_step_exception('activity_missing_subplugins_php_file', $this->task->get_modulename()); | |
1989 | } | |
1990 | include($subpluginsfile); | |
1991 | if (!array_key_exists($subplugintype, $subplugins)) { | |
1992 | throw new restore_step_exception('incorrect_subplugin_type', $subplugintype); | |
1993 | } | |
1994 | // Get all the restore path elements, looking across all the subplugin dirs | |
1995 | $subpluginsdirs = get_plugin_list($subplugintype); | |
1996 | foreach ($subpluginsdirs as $name => $subpluginsdir) { | |
1997 | $classname = 'restore_' . $subplugintype . '_' . $name . '_subplugin'; | |
1998 | $restorefile = $subpluginsdir . '/backup/moodle2/' . $classname . '.class.php'; | |
1999 | if (file_exists($restorefile)) { | |
2000 | require_once($restorefile); | |
2001 | $restoresubplugin = new $classname($subplugintype, $name, $this); | |
2002 | // Add subplugin paths to the step | |
2003 | $this->prepare_pathelements($restoresubplugin->define_subplugin_structure($element)); | |
2004 | } | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | /** | |
2009 | * As far as activity restore steps are implementing restore_subplugin stuff, they need to | |
2010 | * have the parent task available for wrapping purposes (get course/context....) | |
2011 | */ | |
2012 | public function get_task() { | |
2013 | return $this->task; | |
394edb7e EL |
2014 | } |
2015 | ||
2016 | /** | |
2017 | * Adds support for the 'activity' path that is common to all the activities | |
2018 | * and will be processed globally here | |
2019 | */ | |
2020 | protected function prepare_activity_structure($paths) { | |
2021 | ||
2022 | $paths[] = new restore_path_element('activity', '/activity'); | |
2023 | ||
2024 | return $paths; | |
2025 | } | |
2026 | ||
2027 | /** | |
2028 | * Process the activity path, informing the task about various ids, needed later | |
2029 | */ | |
2030 | protected function process_activity($data) { | |
2031 | $data = (object)$data; | |
2032 | $this->task->set_old_contextid($data->contextid); // Save old contextid in task | |
2033 | $this->set_mapping('context', $data->contextid, $this->task->get_contextid()); // Set the mapping | |
2034 | $this->task->set_old_activityid($data->id); // Save old activityid in task | |
2035 | } | |
2036 | ||
2037 | /** | |
034ef761 | 2038 | * This must be invoked immediately after creating the "module" activity record (forum, choice...) |
394edb7e EL |
2039 | * and will adjust the new activity id (the instance) in various places |
2040 | */ | |
2041 | protected function apply_activity_instance($newitemid) { | |
2042 | global $DB; | |
2043 | ||
2044 | $this->task->set_activityid($newitemid); // Save activity id in task | |
2045 | // Apply the id to course_sections->instanceid | |
2046 | $DB->set_field('course_modules', 'instance', $newitemid, array('id' => $this->task->get_moduleid())); | |
2047 | // Do the mapping for modulename, preparing it for files by oldcontext | |
2048 | $modulename = $this->task->get_modulename(); | |
2049 | $oldid = $this->task->get_old_activityid(); | |
2050 | $this->set_mapping($modulename, $oldid, $newitemid, true); | |
2051 | } | |
2052 | } | |
41941110 EL |
2053 | |
2054 | /** | |
2055 | * Structure step in charge of creating/mapping all the qcats and qs | |
2056 | * by parsing the questions.xml file and checking it against the | |
2057 | * results calculated by {@link restore_process_categories_and_questions} | |
2058 | * and stored in backup_ids_temp | |
2059 | */ | |
2060 | class restore_create_categories_and_questions extends restore_structure_step { | |
2061 | ||
2062 | protected function define_structure() { | |
2063 | ||
2064 | $category = new restore_path_element('question_category', '/question_categories/question_category'); | |
2065 | $question = new restore_path_element('question', '/question_categories/question_category/questions/question'); | |
2066 | ||
2067 | // Apply for 'qtype' plugins optional paths at question level | |
2068 | $this->add_plugin_structure('qtype', $question); | |
2069 | ||
2070 | return array($category, $question); | |
2071 | } | |
2072 | ||
2073 | protected function process_question_category($data) { | |
2074 | global $DB; | |
2075 | ||
2076 | $data = (object)$data; | |
2077 | $oldid = $data->id; | |
2078 | ||
2079 | // Check we have one mapping for this category | |
2080 | if (!$mapping = $this->get_mapping('question_category', $oldid)) { | |
2081 | return; // No mapping = this category doesn't need to be created/mapped | |
2082 | } | |
2083 | ||
2084 | // Check we have to create the category (newitemid = 0) | |
2085 | if ($mapping->newitemid) { | |
2086 | return; // newitemid != 0, this category is going to be mapped. Nothing to do | |
2087 | } | |
2088 | ||
2089 | // Arrived here, newitemid = 0, we need to create the category | |
2090 | // we'll do it at parentitemid context, but for CONTEXT_MODULE | |
2091 | // categories, that will be created at CONTEXT_COURSE and moved | |
2092 | // to module context later when the activity is created | |
2093 | if ($mapping->info->contextlevel == CONTEXT_MODULE) { | |
2094 | $mapping->parentitemid = $this->get_mappingid('context', $this->task->get_old_contextid()); | |
2095 | } | |
2096 | $data->contextid = $mapping->parentitemid; | |
2097 | ||
2098 | // Let's create the question_category and save mapping | |
2099 | $newitemid = $DB->insert_record('question_categories', $data); | |
2100 | $this->set_mapping('question_category', $oldid, $newitemid); | |
2101 | // Also annotate them as question_category_created, we need | |
2102 | // that later when remapping parents | |
2103 | $this->set_mapping('question_category_created', $oldid, $newitemid, false, null, $data->contextid); | |
2104 | } | |
2105 | ||
2106 | protected function process_question($data) { | |
2107 | global $DB; | |
2108 | ||
2109 | $data = (object)$data; | |
2110 | $oldid = $data->id; | |
2111 | ||
2112 | // Check we have one mapping for this question | |
2113 | if (!$questionmapping = $this->get_mapping('question', $oldid)) { | |
2114 | return; // No mapping = this question doesn't need to be created/mapped | |
2115 | } | |
2116 | ||
2117 | // Get the mapped category (cannot use get_new_parentid() because not | |
2118 | // all the categories have been created, so it is not always available | |
2119 | // Instead we get the mapping for the question->parentitemid because | |
2120 | // we have loaded qcatids there for all parsed questions | |
2121 | $data->category = $this->get_mappingid('question_category', $questionmapping->parentitemid); | |
2122 | ||
2123 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
2124 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
2125 | ||
2126 | $userid = $this->get_mappingid('user', $data->createdby); | |
2127 | $data->createdby = $userid ? $userid : $this->task->get_userid(); | |
2128 | ||
2129 | $userid = $this->get_mappingid('user', $data->modifiedby); | |
2130 | $data->modifiedby = $userid ? $userid : $this->task->get_userid(); | |
2131 | ||
2132 | // With newitemid = 0, let's create the question | |
2133 | if (!$questionmapping->newitemid) { | |
2134 | $newitemid = $DB->insert_record('question', $data); | |
2135 | $this->set_mapping('question', $oldid, $newitemid); | |
2136 | // Also annotate them as question_created, we need | |
2137 | // that later when remapping parents (keeping the old categoryid as parentid) | |
2138 | $this->set_mapping('question_created', $oldid, $newitemid, false, null, $questionmapping->parentitemid); | |
2139 | } else { | |
2140 | // By performing this set_mapping() we make get_old/new_parentid() to work for all the | |
2141 | // children elements of the 'question' one (so qtype plugins will know the question they belong to) | |
2142 | $this->set_mapping('question', $oldid, $questionmapping->newitemid); | |
2143 | } | |
2144 | ||
2145 | // Note, we don't restore any question files yet | |
2146 | // as far as the CONTEXT_MODULE categories still | |
2147 | // haven't their contexts to be restored to | |
2148 | // The {@link restore_create_question_files}, executed in the final step | |
2149 | // step will be in charge of restoring all the question files | |
2150 | } | |
2151 | ||
2152 | protected function after_execute() { | |
2153 | global $DB; | |
2154 | ||
2155 | // First of all, recode all the created question_categories->parent fields | |
2156 | $qcats = $DB->get_records('backup_ids_temp', array( | |
2157 | 'backupid' => $this->get_restoreid(), | |
2158 | 'itemname' => 'question_category_created')); | |
2159 | foreach ($qcats as $qcat) { | |
2160 | $newparent = 0; | |
2161 | $dbcat = $DB->get_record('question_categories', array('id' => $qcat->newitemid)); | |
2162 | // Get new parent (mapped or created, so we look in quesiton_category mappings) | |
2163 | if ($newparent = $DB->get_field('backup_ids_temp', 'newitemid', array( | |
2164 | 'backupid' => $this->get_restoreid(), | |
2165 | 'itemname' => 'question_category', | |
2166 | 'itemid' => $dbcat->parent))) { | |
2167 | // contextids must match always, as far as we always include complete qbanks, just check it | |
2168 | $newparentctxid = $DB->get_field('question_categories', 'contextid', array('id' => $newparent)); | |
2169 | if ($dbcat->contextid == $newparentctxid) { | |
2170 | $DB->set_field('question_categories', 'parent', $newparent, array('id' => $dbcat->id)); | |
2171 | } else { | |
2172 | $newparent = 0; // No ctx match for both cats, no parent relationship | |
2173 | } | |
2174 | } | |
2175 | // Here with $newparent empty, problem with contexts or remapping, set it to top cat | |
2176 | if (!$newparent) { | |
2177 | $DB->set_field('question_categories', 'parent', 0, array('id' => $dbcat->id)); | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | // Now, recode all the created question->parent fields | |
2182 | $qs = $DB->get_records('backup_ids_temp', array( | |
2183 | 'backupid' => $this->get_restoreid(), | |
2184 | 'itemname' => 'question_created')); | |
2185 | foreach ($qs as $q) { | |
2186 | $newparent = 0; | |
2187 | $dbq = $DB->get_record('question', array('id' => $q->newitemid)); | |
2188 | // Get new parent (mapped or created, so we look in question mappings) | |
2189 | if ($newparent = $DB->get_field('backup_ids_temp', 'newitemid', array( | |
2190 | 'backupid' => $this->get_restoreid(), | |
2191 | 'itemname' => 'question', | |
2192 | 'itemid' => $dbq->parent))) { | |
2193 | $DB->set_field('question', 'parent', $newparent, array('id' => $dbq->id)); | |
2194 | } | |
2195 | } | |
2196 | ||
2197 | // Note, we don't restore any question files yet | |
2198 | // as far as the CONTEXT_MODULE categories still | |
2199 | // haven't their contexts to be restored to | |
2200 | // The {@link restore_create_question_files}, executed in the final step | |
2201 | // step will be in charge of restoring all the question files | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | /** | |
2206 | * Execution step that will move all the CONTEXT_MODULE question categories | |
2207 | * created at early stages of restore in course context (because modules weren't | |
2208 | * created yet) to their target module (matching by old-new-contextid mapping) | |
2209 | */ | |
2210 | class restore_move_module_questions_categories extends restore_execution_step { | |
2211 | ||
2212 | protected function define_execution() { | |
2213 | global $DB; | |
2214 | ||
2215 | $contexts = restore_dbops::restore_get_question_banks($this->get_restoreid(), CONTEXT_MODULE); | |
2216 | foreach ($contexts as $contextid => $contextlevel) { | |
2217 | // Only if context mapping exists (i.e. the module has been restored) | |
2218 | if ($newcontext = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $contextid)) { | |
2219 | // Update all the qcats having their parentitemid set to the original contextid | |
2220 | $modulecats = $DB->get_records_sql("SELECT itemid, newitemid | |
2221 | FROM {backup_ids_temp} | |
2222 | WHERE backupid = ? | |
2223 | AND itemname = 'question_category' | |
2224 | AND parentitemid = ?", array($this->get_restoreid(), $contextid)); | |
2225 | foreach ($modulecats as $modulecat) { | |
2226 | $DB->set_field('question_categories', 'contextid', $newcontext->newitemid, array('id' => $modulecat->newitemid)); | |
2227 | // And set new contextid also in question_category mapping (will be | |
2228 | // used by {@link restore_create_question_files} later | |
2229 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'question_category', $modulecat->itemid, $modulecat->newitemid, $newcontext->newitemid); | |
2230 | } | |
2231 | } | |
2232 | } | |
2233 | } | |
2234 | } | |
2235 | ||
2236 | /** | |
2237 | * Execution step that will create all the question/answers/qtype-specific files for the restored | |
2238 | * questions. It must be executed after {@link restore_move_module_questions_categories} | |
2239 | * because only then each question is in its final category and only then the | |
2240 | * context can be determined | |
2241 | * | |
2242 | * TODO: Improve this. Instead of looping over each question, it can be reduced to | |
2243 | * be done by contexts (this will save a huge ammount of queries) | |
2244 | */ | |
2245 | class restore_create_question_files extends restore_execution_step { | |
2246 | ||
2247 | protected function define_execution() { | |
2248 | global $DB; | |
2249 | ||
2250 | // Let's process only created questions | |
2251 | $questionsrs = $DB->get_recordset_sql("SELECT bi.itemid, bi.newitemid, bi.parentitemid, q.qtype | |
2252 | FROM {backup_ids_temp} bi | |
2253 | JOIN {question} q ON q.id = bi.newitemid | |
2254 | WHERE bi.backupid = ? | |
2255 | AND bi.itemname = 'question_created'", array($this->get_restoreid())); | |
2256 | foreach ($questionsrs as $question) { | |
2257 | // Get question_category mapping, it contains the target context for the question | |
2258 | if (!$qcatmapping = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'question_category', $question->parentitemid)) { | |
2259 | // Something went really wrong, cannot find the question_category for the question | |
2260 | debugging('Error fetching target context for question', DEBUG_DEVELOPER); | |
2261 | continue; | |
2262 | } | |
2263 | // Calculate source and target contexts | |
2264 | $oldctxid = $qcatmapping->info->contextid; | |
2265 | $newctxid = $qcatmapping->parentitemid; | |
2266 | ||
2267 | // Add common question files (question and question_answer ones) | |
2268 | restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'questiontext', | |
2269 | $oldctxid, $this->task->get_userid(), 'question_created', $question->itemid, $newctxid, true); | |
2270 | restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'generalfeedback', | |
2271 | $oldctxid, $this->task->get_userid(), 'question_created', $question->itemid, $newctxid, true); | |
2272 | restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'answerfeedback', | |
2273 | $oldctxid, $this->task->get_userid(), 'question_answer', null, $newctxid, true); | |
2274 | // Add qtype dependent files | |
2275 | $components = backup_qtype_plugin::get_components_and_fileareas($question->qtype); | |
2276 | foreach ($components as $component => $fileareas) { | |
2277 | foreach ($fileareas as $filearea => $mapping) { | |
2278 | // Use itemid only if mapping is question_created | |
2279 | $itemid = ($mapping == 'question_created') ? $question->itemid : null; | |
2280 | restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), $component, $filearea, | |
2281 | $oldctxid, $this->task->get_userid(), $mapping, $itemid, $newctxid, true); | |
2282 | } | |
2283 | } | |
2284 | } | |
2285 | $questionsrs->close(); | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | /** | |
2290 | * Abstract structure step, to be used by all the activities using core questions stuff | |
2291 | * (like the quiz module), to support qtype plugins, states and sessions | |
2292 | */ | |
2293 | abstract class restore_questions_activity_structure_step extends restore_activity_structure_step { | |
2294 | ||
2295 | /** | |
2296 | * Attach below $element (usually attempts) the needed restore_path_elements | |
2297 | * to restore question_states | |
2298 | */ | |
2299 | protected function add_question_attempts_states($element, &$paths) { | |
2300 | // Check $element is restore_path_element | |
2301 | if (! $element instanceof restore_path_element) { | |
2302 | throw new restore_step_exception('element_must_be_restore_path_element', $element); | |
2303 | } | |
2304 | // Check $paths is one array | |
2305 | if (!is_array($paths)) { | |
2306 | throw new restore_step_exception('paths_must_be_array', $paths); | |
2307 | } | |
2308 | $paths[] = new restore_path_element('question_state', $element->get_path() . '/states/state'); | |
2309 | } | |
2310 | ||
2311 | /** | |
2312 | * Attach below $element (usually attempts) the needed restore_path_elements | |
2313 | * to restore question_sessions | |
2314 | */ | |
2315 | protected function add_question_attempts_sessions($element, &$paths) { | |
2316 | // Check $element is restore_path_element | |
2317 | if (! $element instanceof restore_path_element) { | |
2318 | throw new restore_step_exception('element_must_be_restore_path_element', $element); | |
2319 | } | |
2320 | // Check $paths is one array | |
2321 | if (!is_array($paths)) { | |
2322 | throw new restore_step_exception('paths_must_be_array', $paths); | |
2323 | } | |
2324 | $paths[] = new restore_path_element('question_session', $element->get_path() . '/sessions/session'); | |
2325 | } | |
2326 | ||
2327 | /** | |
2328 | * Process question_states | |
2329 | */ | |
2330 | protected function process_question_state($data) { | |
2331 | global $DB; | |
2332 | ||
2333 | $data = (object)$data; | |
2334 | $oldid = $data->id; | |
2335 | ||
2336 | // Get complete question mapping, we'll need info | |
2337 | $question = $this->get_mapping('question', $data->question); | |
2338 | ||
2339 | // In the quiz_attempt mapping we are storing uniqueid | |
2340 | // and not id, so this gets the correct question_attempt to point to | |
2341 | $data->attempt = $this->get_new_parentid('quiz_attempt'); | |
2342 | $data->question = $question->newitemid; | |
2343 | $data->answer = $this->restore_recode_answer($data, $question->info->qtype); // Delegate recoding of answer | |
2344 | $data->timestamp= $this->apply_date_offset($data->timestamp); | |
2345 | ||
2346 | // Everything ready, insert and create mapping (needed by question_sessions) | |
2347 | $newitemid = $DB->insert_record('question_states', $data); | |
2348 | $this->set_mapping('question_state', $oldid, $newitemid); | |
2349 | } | |
2350 | ||
2351 | /** | |
2352 | * Process question_sessions | |
2353 | */ | |
2354 | protected function process_question_session($data) { | |
2355 | global $DB; | |
2356 | ||
2357 | $data = (object)$data; | |
2358 | $oldid = $data->id; | |
2359 | ||
2360 | // In the quiz_attempt mapping we are storing uniqueid | |
2361 | // and not id, so this gets the correct question_attempt to point to | |
2362 | $data->attemptid = $this->get_new_parentid('quiz_attempt'); | |
2363 | $data->questionid = $this->get_mappingid('question', $data->questionid); | |
2364 | $data->newest = $this->get_mappingid('question_state', $data->newest); | |
2365 | $data->newgraded = $this->get_mappingid('question_state', $data->newgraded); | |
2366 | ||
2367 | // Everything ready, insert (no mapping needed) | |
2368 | $newitemid = $DB->insert_record('question_sessions', $data); | |
2369 | ||
2370 | // Note: question_sessions haven't files associated. On purpose manualcomment is lacking | |
2371 | // support for them, so we don't need to handle them here. | |
2372 | } | |
2373 | ||
2374 | /** | |
2375 | * Given a list of question->ids, separated by commas, returns the | |
2376 | * recoded list, with all the restore question mappings applied. | |
2377 | * Note: Used by quiz->questions and quiz_attempts->layout | |
2378 | * Note: 0 = page break (unconverted) | |
2379 | */ | |
2380 | protected function questions_recode_layout($layout) { | |
2381 | // Extracts question id from sequence | |
2382 | if ($questionids = explode(',', $layout)) { | |
2383 | foreach ($questionids as $id => $questionid) { | |
2384 | if ($questionid) { // If it is zero then this is a pagebreak, don't translate | |
2385 | $newquestionid = $this->get_mappingid('question', $questionid); | |
2386 | $questionids[$id] = $newquestionid; | |
2387 | } | |
2388 | } | |
2389 | } | |
2390 | return implode(',', $questionids); | |
2391 | } | |
2392 | ||
2393 | /** | |
2394 | * Given one question_states record, return the answer | |
2395 | * recoded pointing to all the restored stuff | |
2396 | */ | |
2397 | public function restore_recode_answer($state, $qtype) { | |
2398 | // Build one static cache to store {@link restore_qtype_plugin} | |
2399 | // while we are needing them, just to save zillions of instantiations | |
2400 | // or using static stuff that will break our nice API | |
2401 | static $qtypeplugins = array(); | |
2402 | ||
2403 | // If we haven't the corresponding restore_qtype_plugin for current qtype | |
2404 | // instantiate it and add to cache | |
2405 | if (!isset($qtypeplugins[$qtype])) { | |
2406 | $classname = 'restore_qtype_' . $qtype . '_plugin'; | |
2407 | if (class_exists($classname)) { | |
2408 | $qtypeplugins[$qtype] = new $classname('qtype', $qtype, $this); | |
2409 | } else { | |
2410 | $qtypeplugins[$qtype] = false; | |
2411 | } | |
2412 | } | |
2413 | return !empty($qtypeplugins[$qtype]) ? $qtypeplugins[$qtype]->recode_state_answer($state) : $state->answer; | |
2414 | } | |
2415 | } |