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); | |
482aac65 EL |
50 | } |
51 | } | |
52 | ||
53 | /** | |
54 | * delete the temp dir used by backup/restore (conditionally), | |
55 | * delete old directories and drop temp ids table | |
56 | */ | |
57 | class restore_drop_and_clean_temp_stuff extends restore_execution_step { | |
58 | ||
59 | protected function define_execution() { | |
60 | global $CFG; | |
b8bb45b0 | 61 | restore_controller_dbops::drop_restore_temp_tables($this->get_restoreid()); // Drop ids temp table |
b1eaf633 | 62 | backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs |
482aac65 | 63 | if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally |
b1eaf633 | 64 | backup_helper::delete_backup_dir($this->task->get_tempdir()); // Empty restore dir |
482aac65 EL |
65 | } |
66 | } | |
67 | } | |
68 | ||
0067d939 AD |
69 | /** |
70 | * Restore calculated grade items, grade categories etc | |
71 | */ | |
72 | class restore_gradebook_step extends restore_structure_step { | |
73 | ||
a0d27102 AD |
74 | /** |
75 | * To conditionally decide if this step must be executed | |
76 | * Note the "settings" conditions are evaluated in the | |
77 | * corresponding task. Here we check for other conditions | |
78 | * not being restore settings (files, site settings...) | |
79 | */ | |
80 | protected function execute_condition() { | |
81 | global $CFG; | |
82 | ||
83 | // No gradebook info found, don't execute | |
84 | $fullpath = $this->task->get_taskbasepath(); | |
85 | $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; | |
86 | if (!file_exists($fullpath)) { | |
87 | return false; | |
88 | } | |
89 | ||
90 | // Arrived here, execute the step | |
91 | return true; | |
92 | } | |
93 | ||
0067d939 AD |
94 | protected function define_structure() { |
95 | $paths = array(); | |
96 | $userinfo = $this->task->get_setting_value('users'); | |
97 | ||
98 | $paths[] = new restore_path_element('gradebook', '/gradebook'); | |
99 | $paths[] = new restore_path_element('grade_category', '/gradebook/grade_categories/grade_category'); | |
100 | $paths[] = new restore_path_element('grade_item', '/gradebook/grade_items/grade_item'); | |
101 | if ($userinfo) { | |
102 | $paths[] = new restore_path_element('grade_grade', '/gradebook/grade_items/grade_item/grade_grades/grade_grade'); | |
103 | } | |
104 | $paths[] = new restore_path_element('grade_letter', '/gradebook/grade_letters/grade_letter'); | |
105 | ||
106 | return $paths; | |
107 | } | |
108 | ||
109 | protected function process_gradebook($data) { | |
110 | } | |
111 | ||
112 | protected function process_grade_item($data) { | |
113 | global $DB; | |
114 | ||
115 | $data = (object)$data; | |
116 | ||
117 | $oldid = $data->id; | |
118 | $data->course = $this->get_courseid(); | |
119 | ||
120 | $data->courseid = $this->get_courseid(); | |
121 | ||
122 | //manual grade items store category id in categoryid | |
123 | if ($data->itemtype=='manual') { | |
124 | $data->categoryid = $this->get_mappingid('grade_category', $data->categoryid); | |
125 | } //course and category grade items store their category id in iteminstance | |
126 | else if ($data->itemtype=='course' || $data->itemtype=='category') { | |
127 | $data->iteminstance = $this->get_mappingid('grade_category', $data->iteminstance); | |
128 | } | |
129 | ||
130 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); | |
131 | $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid); | |
132 | ||
133 | $data->locktime = $this->apply_date_offset($data->locktime); | |
134 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
135 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
136 | ||
137 | //course grade item should already exist so updating instead of inserting | |
138 | if($data->itemtype=='course') { | |
139 | ||
140 | //get the ID of the already created grade item | |
141 | $gi = new stdclass(); | |
142 | $gi->courseid = $this->get_courseid(); | |
143 | ||
144 | $gi->itemtype = $data->itemtype; | |
145 | if ($data->itemtype=='course') { | |
146 | //need to get the id of the grade_category that was automatically created for the course | |
147 | $category = new stdclass(); | |
148 | $category->courseid = $this->get_courseid(); | |
149 | $category->parent = null; | |
150 | $category->fullname = '?'; | |
151 | ||
152 | $coursecategory = $DB->get_record('grade_categories', (array)$category); | |
153 | $gi->iteminstance = $coursecategory->id; | |
154 | } | |
155 | ||
156 | $existinggradeitem = $DB->get_record('grade_items', (array)$gi); | |
157 | $newitemid = $existinggradeitem->id; | |
158 | ||
159 | $data->id = $newitemid; | |
160 | $DB->update_record('grade_items', $data); | |
161 | } else { //insert manual grade items | |
162 | $newitemid = $DB->insert_record('grade_items', $data); | |
163 | } | |
164 | $this->set_mapping('grade_item', $oldid, $newitemid); | |
165 | } | |
166 | ||
167 | protected function process_grade_grade($data) { | |
168 | global $DB; | |
169 | ||
170 | $data = (object)$data; | |
171 | $oldid = $data->id; | |
172 | ||
173 | $data->itemid = $this->get_new_parentid('grade_item'); | |
174 | ||
175 | $data->userid = $this->get_mappingid('user', $data->userid); | |
176 | $data->usermodified = $this->get_mappingid('user', $data->usermodified); | |
177 | $data->locktime = $this->apply_date_offset($data->locktime); | |
178 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
179 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
180 | ||
181 | $newitemid = $DB->insert_record('grade_grades', $data); | |
182 | $this->set_mapping('grade_grade', $oldid, $newitemid); | |
183 | } | |
184 | protected function process_grade_category($data) { | |
185 | global $DB; | |
186 | ||
187 | $data = (object)$data; | |
188 | $oldid = $data->id; | |
189 | ||
190 | $data->course = $this->get_courseid(); | |
191 | $data->courseid = $data->course; | |
192 | ||
193 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
194 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
195 | ||
196 | //no parent means a course level grade category. That should have been created when the course was created | |
197 | if(empty($data->parent)) { | |
198 | //get the already created course level grade category | |
199 | $category = new stdclass(); | |
200 | $category->courseid = $this->get_courseid(); | |
201 | ||
202 | $coursecategory = $DB->get_record('grade_categories', (array)$category); | |
203 | $newitemid = $coursecategory->id; | |
204 | $data->id = $newitemid; | |
205 | ||
206 | //parent was being saved as 0 when it should be null | |
207 | $data->parent = null; | |
208 | ||
209 | $DB->update_record('grade_categories', $data); | |
210 | } else { | |
211 | $data->parent = $this->get_mappingid('grade_category', $data->parent); | |
212 | $newitemid = $DB->insert_record('grade_categories', $data); | |
213 | } | |
214 | $this->set_mapping('grade_category', $oldid, $newitemid); | |
215 | ||
216 | //need to correct the path as its a string that contains grade category IDs | |
217 | $grade_category = new stdclass(); | |
218 | $grade_category->parent = $data->parent; | |
219 | $grade_category->id = $newitemid; | |
220 | $grade_category->path = grade_category::build_path($grade_category); | |
221 | $DB->update_record('grade_categories', $grade_category); | |
222 | } | |
223 | protected function process_grade_letter($data) { | |
224 | global $DB; | |
225 | ||
226 | $data = (object)$data; | |
227 | $oldid = $data->id; | |
228 | ||
229 | $data->contextid = $this->get_mappingid('context', $data->contextid); | |
230 | ||
231 | $newitemid = $DB->insert_record('grade_letters', $data); | |
232 | $this->set_mapping('grade_letter', $oldid, $newitemid); | |
233 | } | |
0f94550c AD |
234 | |
235 | //put all activity grade items in the correct grade category and mark all for recalculation | |
c4d2c745 AD |
236 | protected function after_execute() { |
237 | global $DB; | |
238 | ||
c4d2c745 AD |
239 | $conditions = array( |
240 | 'backupid' => $this->get_restoreid(), | |
241 | 'itemname' => 'grade_item'//, | |
242 | //'itemid' => $itemid | |
243 | ); | |
244 | $rs = $DB->get_recordset('backup_ids_temp', $conditions); | |
245 | ||
246 | if (!empty($rs)) { | |
247 | foreach($rs as $grade_item_backup) { | |
0f94550c AD |
248 | $updateobj = new stdclass(); |
249 | $updateobj->id = $grade_item_backup->newitemid; | |
074a765f | 250 | |
0f94550c AD |
251 | //if this is an activity grade item that needs to be put back in its correct category |
252 | if (!empty($grade_item_backup->parentitemid)) { | |
253 | $updateobj->categoryid = $this->get_mappingid('grade_category', $grade_item_backup->parentitemid); | |
074a765f | 254 | } else { |
0f94550c AD |
255 | //mark course and category items as needing to be recalculated |
256 | $updateobj->needsupdate=1; | |
c4d2c745 | 257 | } |
0f94550c AD |
258 | $DB->update_record('grade_items', $updateobj); |
259 | ||
260 | //todo need to get hold of the grade_items previous sortorder to restore it | |
c4d2c745 AD |
261 | } |
262 | } | |
263 | $rs->close(); | |
264 | } | |
0067d939 AD |
265 | } |
266 | ||
394edb7e EL |
267 | /** |
268 | * decode all the interlinks present in restored content | |
269 | * relying 100% in the restore_decode_processor that handles | |
270 | * both the contents to modify and the rules to be applied | |
271 | */ | |
272 | class restore_decode_interlinks extends restore_execution_step { | |
273 | ||
274 | protected function define_execution() { | |
275 | // Just that | |
276 | $this->task->get_decoder()->execute(); | |
277 | } | |
278 | } | |
279 | ||
280 | /** | |
281 | * rebuid the course cache | |
282 | */ | |
283 | class restore_rebuild_course_cache extends restore_execution_step { | |
284 | ||
285 | protected function define_execution() { | |
286 | // Just that | |
287 | rebuild_course_cache($this->get_courseid()); | |
288 | } | |
289 | } | |
290 | ||
b8e455a7 EL |
291 | |
292 | /** | |
293 | * Review all the (pending) block positions in backup_ids, matching by | |
294 | * contextid, creating positions as needed. This is executed by the | |
295 | * final task, once all the contexts have been created | |
296 | */ | |
297 | class restore_review_pending_block_positions extends restore_execution_step { | |
298 | ||
299 | protected function define_execution() { | |
300 | global $DB; | |
301 | ||
302 | // Get all the block_position objects pending to match | |
303 | $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'block_position'); | |
304 | $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid'); | |
305 | // Process block positions, creating them or accumulating for final step | |
306 | foreach($rs as $posrec) { | |
307 | // Get the complete position object (stored as info) | |
308 | $position = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'block_position', $posrec->itemid)->info; | |
309 | // If position is for one already mapped (known) contextid | |
310 | // process it now, creating the position, else nothing to | |
311 | // do, position finally discarded | |
312 | if ($newctx = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $position->contextid)) { | |
313 | $position->contextid = $newctx->newitemid; | |
314 | // Create the block position | |
315 | $DB->insert_record('block_positions', $position); | |
316 | } | |
317 | } | |
318 | $rs->close(); | |
319 | } | |
320 | } | |
321 | ||
5095f325 EL |
322 | /** |
323 | * Process all the saved module availability records in backup_ids, matching | |
324 | * course modules and grade item id once all them have been already restored. | |
325 | * only if all matchings are satisfied the availability condition will be created. | |
326 | * At the same time, it is required for the site to have that functionality enabled. | |
327 | */ | |
328 | class restore_process_course_modules_availability extends restore_execution_step { | |
329 | ||
330 | protected function define_execution() { | |
331 | global $CFG, $DB; | |
332 | ||
333 | // Site hasn't availability enabled | |
334 | if (empty($CFG->enableavailability)) { | |
335 | return; | |
336 | } | |
337 | ||
338 | // Get all the module_availability objects to process | |
339 | $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability'); | |
340 | $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid'); | |
341 | // Process availabilities, creating them if everything matches ok | |
342 | foreach($rs as $availrec) { | |
343 | $allmatchesok = true; | |
344 | // Get the complete availabilityobject | |
345 | $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability', $availrec->itemid)->info; | |
346 | // Map the sourcecmid if needed and possible | |
347 | if (!empty($availability->sourcecmid)) { | |
348 | $newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'course_module', $availability->sourcecmid); | |
349 | if ($newcm) { | |
350 | $availability->sourcecmid = $newcm->newitemid; | |
351 | } else { | |
352 | $allmatchesok = false; // Failed matching, we won't create this availability rule | |
353 | } | |
354 | } | |
355 | // Map the gradeitemid if needed and possible | |
356 | if (!empty($availability->gradeitemid)) { | |
357 | $newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'grade_item', $availability->gradeitemid); | |
358 | if ($newgi) { | |
359 | $availability->gradeitemid = $newgi->newitemid; | |
360 | } else { | |
361 | $allmatchesok = false; // Failed matching, we won't create this availability rule | |
362 | } | |
363 | } | |
364 | if ($allmatchesok) { // Everything ok, create the availability rule | |
365 | $DB->insert_record('course_modules_availability', $availability); | |
366 | } | |
367 | } | |
368 | $rs->close(); | |
369 | } | |
370 | } | |
371 | ||
372 | ||
482aac65 EL |
373 | /* |
374 | * Execution step that, *conditionally* (if there isn't preloaded information) | |
375 | * will load the inforef files for all the included course/section/activity tasks | |
376 | * to backup_temp_ids. They will be stored with "xxxxref" as itemname | |
377 | */ | |
378 | class restore_load_included_inforef_records extends restore_execution_step { | |
379 | ||
380 | protected function define_execution() { | |
381 | ||
382 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
383 | return; | |
384 | } | |
385 | ||
386 | // Get all the included inforef files | |
387 | $files = restore_dbops::get_needed_inforef_files($this->get_restoreid()); | |
388 | foreach ($files as $file) { | |
389 | restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $file); // Load each inforef file to temp_ids | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
76cfb124 | 394 | /* |
b8bb45b0 | 395 | * Execution step that will load all the needed files into backup_files_temp |
76cfb124 EL |
396 | * - info: contains the whole original object (times, names...) |
397 | * (all them being original ids as loaded from xml) | |
398 | */ | |
399 | class restore_load_included_files extends restore_structure_step { | |
400 | ||
401 | protected function define_structure() { | |
402 | ||
403 | $file = new restore_path_element('file', '/files/file'); | |
404 | ||
405 | return array($file); | |
406 | } | |
407 | ||
408 | // Processing functions go here | |
409 | public function process_file($data) { | |
410 | ||
411 | $data = (object)$data; // handy | |
412 | ||
76cfb124 EL |
413 | // load it if needed: |
414 | // - it it is one of the annotated inforef files (course/section/activity/block) | |
71a50b13 | 415 | // - it is one "user", "group", "grouping" or "grade" component file (that aren't sent to inforef ever) |
b8bb45b0 | 416 | $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id); |
71a50b13 EL |
417 | $iscomponent = ($data->component == 'user' || $data->component == 'group' || |
418 | $data->component == 'grouping' || $data->component == 'grade'); | |
76cfb124 | 419 | if ($isfileref || $iscomponent) { |
b8bb45b0 | 420 | restore_dbops::set_backup_files_record($this->get_restoreid(), $data); |
76cfb124 EL |
421 | } |
422 | } | |
423 | } | |
424 | ||
71a50b13 EL |
425 | /** |
426 | * Execution step that, *conditionally* (if there isn't preloaded information), | |
427 | * will load all the needed roles to backup_temp_ids. They will be stored with | |
428 | * "role" itemname. Also it will perform one automatic mapping to roles existing | |
429 | * in the target site, based in permissions of the user performing the restore, | |
430 | * archetypes and other bits. At the end, each original role will have its associated | |
431 | * target role or 0 if it's going to be skipped. Note we wrap everything over one | |
432 | * restore_dbops method, as far as the same stuff is going to be also executed | |
433 | * by restore prechecks | |
434 | */ | |
435 | class restore_load_and_map_roles extends restore_execution_step { | |
436 | ||
437 | protected function define_execution() { | |
8d4e41f4 | 438 | if ($this->task->get_preloaded_information()) { // if info is already preloaded |
71a50b13 EL |
439 | return; |
440 | } | |
441 | ||
442 | $file = $this->get_basepath() . '/roles.xml'; | |
443 | // Load needed toles to temp_ids | |
444 | restore_dbops::load_roles_to_tempids($this->get_restoreid(), $file); | |
8d4e41f4 | 445 | |
71a50b13 | 446 | // Process roles, mapping/skipping. Any error throws exception |
8d4e41f4 EL |
447 | // Note we pass controller's info because it can contain role mapping information |
448 | // about manual mappings performed by UI | |
449 | 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 |
450 | } |
451 | } | |
452 | ||
482aac65 EL |
453 | /** |
454 | * Execution step that, *conditionally* (if there isn't preloaded information | |
455 | * and users have been selected in settings, will load all the needed users | |
456 | * to backup_temp_ids. They will be stored with "user" itemname and with | |
76cfb124 | 457 | * their original contextid as paremitemid |
482aac65 EL |
458 | */ |
459 | class restore_load_included_users extends restore_execution_step { | |
460 | ||
461 | protected function define_execution() { | |
462 | ||
463 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
464 | return; | |
465 | } | |
25d3cf44 | 466 | if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do |
482aac65 EL |
467 | return; |
468 | } | |
469 | $file = $this->get_basepath() . '/users.xml'; | |
470 | restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids | |
471 | } | |
472 | } | |
473 | ||
474 | /** | |
475 | * Execution step that, *conditionally* (if there isn't preloaded information | |
476 | * and users have been selected in settings, will process all the needed users | |
477 | * in order to decide and perform any action with them (create / map / error) | |
478 | * Note: Any error will cause exception, as far as this is the same processing | |
479 | * than the one into restore prechecks (that should have stopped process earlier) | |
480 | */ | |
76cfb124 | 481 | class restore_process_included_users extends restore_execution_step { |
482aac65 EL |
482 | |
483 | protected function define_execution() { | |
484 | ||
485 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
486 | return; | |
487 | } | |
25d3cf44 | 488 | if (!$this->task->get_setting_value('users')) { // No userinfo being restored, nothing to do |
482aac65 EL |
489 | return; |
490 | } | |
491 | restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
492 | } | |
493 | } | |
494 | ||
76cfb124 EL |
495 | /** |
496 | * Execution step that will create all the needed users as calculated | |
497 | * by @restore_process_included_users (those having newiteind = 0) | |
498 | */ | |
499 | class restore_create_included_users extends restore_execution_step { | |
500 | ||
501 | protected function define_execution() { | |
502 | ||
503 | restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files')); | |
504 | } | |
505 | } | |
506 | ||
507 | /** | |
508 | * Structure step that will create all the needed groups and groupings | |
509 | * by loading them from the groups.xml file performing the required matches. | |
510 | * Note group members only will be added if restoring user info | |
511 | */ | |
512 | class restore_groups_structure_step extends restore_structure_step { | |
513 | ||
c0440b3f EL |
514 | protected function define_structure() { |
515 | ||
516 | $paths = array(); // Add paths here | |
517 | ||
518 | $paths[] = new restore_path_element('group', '/groups/group'); | |
519 | if ($this->get_setting_value('users')) { | |
520 | $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member'); | |
521 | } | |
522 | $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping'); | |
523 | $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group'); | |
524 | ||
525 | return $paths; | |
526 | } | |
527 | ||
528 | // Processing functions go here | |
529 | public function process_group($data) { | |
530 | global $DB; | |
531 | ||
532 | $data = (object)$data; // handy | |
533 | $data->courseid = $this->get_courseid(); | |
534 | ||
535 | $oldid = $data->id; // need this saved for later | |
76cfb124 | 536 | |
c0440b3f EL |
537 | $restorefiles = false; // Only if we end creating the group |
538 | ||
539 | // Search if the group already exists (by name & description) in the target course | |
540 | $description_clause = ''; | |
541 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
542 | if (!empty($data->description)) { | |
543 | $description_clause = ' AND ' . | |
544 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
545 | $params['desc'] = $data->description; | |
546 | } | |
547 | if (!$groupdb = $DB->get_record_sql("SELECT * | |
548 | FROM {groups} | |
549 | WHERE courseid = :courseid | |
550 | AND name = :grname $description_clause", $params)) { | |
551 | // group doesn't exist, create | |
552 | $newitemid = $DB->insert_record('groups', $data); | |
553 | $restorefiles = true; // We'll restore the files | |
554 | } else { | |
555 | // group exists, use it | |
556 | $newitemid = $groupdb->id; | |
557 | } | |
558 | // Save the id mapping | |
559 | $this->set_mapping('group', $oldid, $newitemid, $restorefiles); | |
560 | } | |
561 | ||
562 | public function process_member($data) { | |
563 | global $DB; | |
564 | ||
565 | $data = (object)$data; // handy | |
566 | ||
567 | // get parent group->id | |
568 | $data->groupid = $this->get_new_parentid('group'); | |
569 | ||
570 | // map user newitemid and insert if not member already | |
571 | if ($data->userid = $this->get_mappingid('user', $data->userid)) { | |
572 | if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) { | |
573 | $DB->insert_record('groups_members', $data); | |
574 | } | |
575 | } | |
576 | } | |
577 | ||
578 | public function process_grouping($data) { | |
71a50b13 EL |
579 | global $DB; |
580 | ||
581 | $data = (object)$data; // handy | |
582 | $data->courseid = $this->get_courseid(); | |
583 | ||
584 | $oldid = $data->id; // need this saved for later | |
585 | $restorefiles = false; // Only if we end creating the grouping | |
586 | ||
587 | // Search if the grouping already exists (by name & description) in the target course | |
588 | $description_clause = ''; | |
589 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
590 | if (!empty($data->description)) { | |
591 | $description_clause = ' AND ' . | |
592 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
593 | $params['desc'] = $data->description; | |
594 | } | |
595 | if (!$groupingdb = $DB->get_record_sql("SELECT * | |
596 | FROM {groupings} | |
597 | WHERE courseid = :courseid | |
598 | AND name = :grname $description_clause", $params)) { | |
599 | // grouping doesn't exist, create | |
600 | $newitemid = $DB->insert_record('groupings', $data); | |
601 | $restorefiles = true; // We'll restore the files | |
602 | } else { | |
603 | // grouping exists, use it | |
604 | $newitemid = $groupingdb->id; | |
605 | } | |
606 | // Save the id mapping | |
607 | $this->set_mapping('grouping', $oldid, $newitemid, $restorefiles); | |
c0440b3f EL |
608 | } |
609 | ||
610 | public function process_grouping_group($data) { | |
71a50b13 EL |
611 | global $DB; |
612 | ||
613 | $data = (object)$data; | |
614 | ||
615 | $data->groupingid = $this->get_new_parentid('grouping'); // Use new parentid | |
616 | $data->groupid = $this->get_mappingid('group', $data->groupid); // Get from mappings | |
617 | $DB->insert_record('groupings_groups', $data); // No need to set this mapping (no child info nor files) | |
c0440b3f EL |
618 | } |
619 | ||
620 | protected function after_execute() { | |
621 | // Add group related files, matching with "group" mappings | |
622 | $this->add_related_files('group', 'icon', 'group'); | |
623 | $this->add_related_files('group', 'description', 'group'); | |
71a50b13 EL |
624 | // Add grouping related files, matching with "grouping" mappings |
625 | $this->add_related_files('grouping', 'description', 'grouping'); | |
c0440b3f EL |
626 | } |
627 | ||
628 | } | |
629 | ||
630 | /** | |
631 | * Structure step that will create all the needed scales | |
632 | * by loading them from the scales.xml | |
c0440b3f EL |
633 | */ |
634 | class restore_scales_structure_step extends restore_structure_step { | |
635 | ||
636 | protected function define_structure() { | |
637 | ||
638 | $paths = array(); // Add paths here | |
639 | $paths[] = new restore_path_element('scale', '/scales_definition/scale'); | |
640 | return $paths; | |
641 | } | |
642 | ||
643 | protected function process_scale($data) { | |
644 | global $DB; | |
645 | ||
646 | $data = (object)$data; | |
647 | ||
648 | $restorefiles = false; // Only if we end creating the group | |
649 | ||
650 | $oldid = $data->id; // need this saved for later | |
651 | ||
652 | // Look for scale (by 'scale' both in standard (course=0) and current course | |
653 | // with priority to standard scales (ORDER clause) | |
654 | // scale is not course unique, use get_record_sql to suppress warning | |
655 | // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides | |
656 | $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc'); | |
657 | $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale); | |
658 | if (!$scadb = $DB->get_record_sql("SELECT * | |
659 | FROM {scale} | |
660 | WHERE courseid IN (0, :courseid) | |
661 | AND $compare_scale_clause | |
662 | ORDER BY courseid", $params, IGNORE_MULTIPLE)) { | |
663 | // Remap the user if possible, defaut to user performing the restore if not | |
664 | $userid = $this->get_mappingid('user', $data->userid); | |
ba8bead5 | 665 | $data->userid = $userid ? $userid : $this->task->get_userid(); |
c0440b3f EL |
666 | // Remap the course if course scale |
667 | $data->courseid = $data->courseid ? $this->get_courseid() : 0; | |
668 | // If global scale (course=0), check the user has perms to create it | |
669 | // falling to course scale if not | |
670 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
394edb7e | 671 | if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $this->task->get_userid())) { |
c0440b3f EL |
672 | $data->courseid = $this->get_courseid(); |
673 | } | |
674 | // scale doesn't exist, create | |
675 | $newitemid = $DB->insert_record('scale', $data); | |
676 | $restorefiles = true; // We'll restore the files | |
677 | } else { | |
678 | // scale exists, use it | |
679 | $newitemid = $scadb->id; | |
680 | } | |
681 | // Save the id mapping (with files support at system context) | |
682 | $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
683 | } | |
684 | ||
685 | protected function after_execute() { | |
686 | // Add scales related files, matching with "scale" mappings | |
687 | $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid()); | |
688 | } | |
76cfb124 EL |
689 | } |
690 | ||
c0440b3f | 691 | |
c8730ff0 EL |
692 | /** |
693 | * Structure step that will create all the needed outocomes | |
694 | * by loading them from the outcomes.xml | |
695 | */ | |
696 | class restore_outcomes_structure_step extends restore_structure_step { | |
697 | ||
698 | protected function define_structure() { | |
699 | ||
700 | $paths = array(); // Add paths here | |
701 | $paths[] = new restore_path_element('outcome', '/outcomes_definition/outcome'); | |
702 | return $paths; | |
703 | } | |
704 | ||
705 | protected function process_outcome($data) { | |
706 | global $DB; | |
707 | ||
708 | $data = (object)$data; | |
709 | ||
710 | $restorefiles = false; // Only if we end creating the group | |
711 | ||
712 | $oldid = $data->id; // need this saved for later | |
713 | ||
714 | // Look for outcome (by shortname both in standard (courseid=null) and current course | |
715 | // with priority to standard outcomes (ORDER clause) | |
716 | // outcome is not course unique, use get_record_sql to suppress warning | |
717 | $params = array('courseid' => $this->get_courseid(), 'shortname' => $data->shortname); | |
718 | if (!$outdb = $DB->get_record_sql('SELECT * | |
719 | FROM {grade_outcomes} | |
720 | WHERE shortname = :shortname | |
721 | AND (courseid = :courseid OR courseid IS NULL) | |
722 | ORDER BY COALESCE(courseid, 0)', $params, IGNORE_MULTIPLE)) { | |
723 | // Remap the user | |
724 | $userid = $this->get_mappingid('user', $data->usermodified); | |
ba8bead5 | 725 | $data->usermodified = $userid ? $userid : $this->task->get_userid(); |
8d4e41f4 EL |
726 | // Remap the scale |
727 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); | |
c8730ff0 EL |
728 | // Remap the course if course outcome |
729 | $data->courseid = $data->courseid ? $this->get_courseid() : null; | |
730 | // If global outcome (course=null), check the user has perms to create it | |
731 | // falling to course outcome if not | |
732 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
394edb7e | 733 | if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $this->task->get_userid())) { |
c8730ff0 EL |
734 | $data->courseid = $this->get_courseid(); |
735 | } | |
736 | // outcome doesn't exist, create | |
737 | $newitemid = $DB->insert_record('grade_outcomes', $data); | |
738 | $restorefiles = true; // We'll restore the files | |
739 | } else { | |
740 | // scale exists, use it | |
741 | $newitemid = $outdb->id; | |
742 | } | |
743 | // Set the corresponding grade_outcomes_courses record | |
744 | $outcourserec = new stdclass(); | |
745 | $outcourserec->courseid = $this->get_courseid(); | |
746 | $outcourserec->outcomeid = $newitemid; | |
747 | if (!$DB->record_exists('grade_outcomes_courses', (array)$outcourserec)) { | |
748 | $DB->insert_record('grade_outcomes_courses', $outcourserec); | |
749 | } | |
750 | // Save the id mapping (with files support at system context) | |
751 | $this->set_mapping('outcome', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
752 | } | |
753 | ||
754 | protected function after_execute() { | |
755 | // Add outcomes related files, matching with "outcome" mappings | |
756 | $this->add_related_files('grade', 'outcome', 'outcome', $this->task->get_old_system_contextid()); | |
757 | } | |
758 | } | |
759 | ||
3223cc95 EL |
760 | /** |
761 | * Structure step that will read the section.xml creating/updating sections | |
762 | * as needed, rebuilding course cache and other friends | |
763 | */ | |
764 | class restore_section_structure_step extends restore_structure_step { | |
c8730ff0 | 765 | |
3223cc95 EL |
766 | protected function define_structure() { |
767 | return array(new restore_path_element('section', '/section')); | |
768 | } | |
769 | ||
770 | public function process_section($data) { | |
771 | global $DB; | |
772 | $data = (object)$data; | |
773 | $oldid = $data->id; // We'll need this later | |
774 | ||
775 | $restorefiles = false; | |
776 | ||
777 | // Look for the section | |
778 | $section = new stdclass(); | |
779 | $section->course = $this->get_courseid(); | |
780 | $section->section = $data->number; | |
781 | // Section doesn't exist, create it with all the info from backup | |
782 | if (!$secrec = $DB->get_record('course_sections', (array)$section)) { | |
783 | $section->name = $data->name; | |
784 | $section->summary = $data->summary; | |
785 | $section->summaryformat = $data->summaryformat; | |
786 | $section->sequence = ''; | |
787 | $section->visible = $data->visible; | |
788 | $newitemid = $DB->insert_record('course_sections', $section); | |
789 | $restorefiles = true; | |
790 | ||
791 | // Section exists, update non-empty information | |
792 | } else { | |
793 | $section->id = $secrec->id; | |
794 | if (empty($secrec->name)) { | |
795 | $section->name = $data->name; | |
796 | } | |
797 | if (empty($secrec->summary)) { | |
798 | $section->summary = $data->summary; | |
799 | $section->summaryformat = $data->summaryformat; | |
800 | $restorefiles = true; | |
801 | } | |
802 | $DB->update_record('course_sections', $section); | |
803 | $newitemid = $secrec->id; | |
804 | } | |
805 | ||
806 | // Annotate the section mapping, with restorefiles option if needed | |
807 | $this->set_mapping('course_section', $oldid, $newitemid, $restorefiles); | |
808 | ||
809 | // If needed, adjust course->numsections | |
810 | if ($numsections = $DB->get_field('course', 'numsections', array('id' => $this->get_courseid()))) { | |
811 | if ($numsections < $section->section) { | |
812 | $DB->set_field('course', 'numsections', $section->section, array('id' => $this->get_courseid())); | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | protected function after_execute() { | |
818 | // Add section related files, with 'course_section' itemid to match | |
819 | $this->add_related_files('course', 'section', 'course_section'); | |
820 | } | |
821 | } | |
822 | ||
823 | ||
824 | /** | |
482aac65 | 825 | * Structure step that will read the course.xml file, loading it and performing |
395dae30 EL |
826 | * various actions depending of the site/restore settings. Note that target |
827 | * course always exist before arriving here so this step will be updating | |
828 | * the course record (never inserting) | |
482aac65 EL |
829 | */ |
830 | class restore_course_structure_step extends restore_structure_step { | |
831 | ||
832 | protected function define_structure() { | |
833 | ||
834 | $course = new restore_path_element('course', '/course', true); // Grouped | |
835 | $category = new restore_path_element('category', '/course/category'); | |
836 | $tag = new restore_path_element('tag', '/course/tags/tag'); | |
837 | $allowed = new restore_path_element('allowed', '/course/allowed_modules/module'); | |
838 | ||
839 | return array($course, $category, $tag, $allowed); | |
840 | } | |
841 | ||
842 | // Processing functions go here | |
843 | public function process_course($data) { | |
395dae30 EL |
844 | global $CFG, $DB; |
845 | ||
846 | $data = (object)$data; | |
847 | $coursetags = isset($data->tags['tag']) ? $data->tags['tag'] : array(); | |
848 | $coursemodules = isset($data->allowed_modules['module']) ? $data->allowed_modules['module'] : array(); | |
849 | $oldid = $data->id; // We'll need this later | |
785d6603 SH |
850 | |
851 | $fullname = $this->get_setting_value('course_fullname'); | |
852 | $shortname = $this->get_setting_value('course_shortname'); | |
b1eaf633 | 853 | $startdate = $this->get_setting_value('course_startdate'); |
395dae30 EL |
854 | |
855 | // Calculate final course names, to avoid dupes | |
856 | list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname); | |
857 | ||
858 | // Need to change some fields before updating the course record | |
859 | $data->id = $this->get_courseid(); | |
860 | $data->fullname = $fullname; | |
861 | $data->shortname= $shortname; | |
862 | $data->idnumber = ''; | |
785d6603 SH |
863 | // TODO: Set category from the UI, its not a setting just a param |
864 | $data->category = get_course_category()->id; | |
395dae30 EL |
865 | $data->startdate= $this->apply_date_offset($data->startdate); |
866 | if ($data->defaultgroupingid) { | |
867 | $data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid); | |
868 | } | |
5095f325 | 869 | if (empty($CFG->enablecompletion)) { |
395dae30 EL |
870 | $data->enablecompletion = 0; |
871 | $data->completionstartonenrol = 0; | |
872 | $data->completionnotify = 0; | |
873 | } | |
874 | $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search | |
875 | if (!array_key_exists($data->lang, $languages)) { | |
876 | $data->lang = ''; | |
877 | } | |
878 | $themes = get_list_of_themes(); // Get themes for quick search later | |
879 | if (!in_array($data->theme, $themes) || empty($CFG->allowcoursethemes)) { | |
880 | $data->theme = ''; | |
881 | } | |
882 | ||
883 | // Course record ready, update it | |
884 | $DB->update_record('course', $data); | |
885 | ||
394edb7e EL |
886 | // Set course mapping |
887 | $this->set_mapping('course', $oldid, $data->id); | |
888 | ||
395dae30 EL |
889 | // Course tags |
890 | if (!empty($CFG->usetags) && isset($coursetags)) { // if enabled in server and present in backup | |
891 | $tags = array(); | |
892 | foreach ($coursetags as $coursetag) { | |
893 | $coursetag = (object)$coursetag; | |
894 | $tags[] = $coursetag->rawname; | |
895 | } | |
896 | tag_set('course', $this->get_courseid(), $tags); | |
897 | } | |
898 | // Course allowed modules | |
899 | if (!empty($data->restrictmodules) && !empty($coursemodules)) { | |
900 | $available = get_plugin_list('mod'); | |
901 | foreach ($coursemodules as $coursemodule) { | |
902 | $mname = $coursemodule['modulename']; | |
903 | if (array_key_exists($mname, $available)) { | |
904 | if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) { | |
905 | $rec = new stdclass(); | |
906 | $rec->course = $this->get_courseid(); | |
907 | $rec->module = $module->id; | |
908 | if (!$DB->record_exists('course_allowed_modules', (array)$rec)) { | |
909 | $DB->insert_record('course_allowed_modules', $rec); | |
910 | } | |
911 | } | |
912 | } | |
913 | } | |
914 | } | |
915 | // Role name aliases | |
916 | restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid()); | |
482aac65 EL |
917 | } |
918 | ||
395dae30 EL |
919 | protected function after_execute() { |
920 | // Add course related files, without itemid to match | |
921 | $this->add_related_files('course', 'summary', null); | |
922 | $this->add_related_files('course', 'legacy', null); | |
923 | } | |
482aac65 | 924 | } |
024c288d EL |
925 | |
926 | ||
927 | /* | |
928 | * Structure step that will read the roles.xml file (at course/activity/block levels) | |
929 | * containig all the role_assignments and overrides for that context. If corresponding to | |
930 | * one mapped role, they will be applied to target context. Will observe the role_assignments | |
931 | * setting to decide if ras are restored. | |
932 | * Note: only ras with component == null are restored as far as the any ra with component | |
933 | * is handled by one enrolment plugin, hence it will createt the ras later | |
934 | */ | |
935 | class restore_ras_and_caps_structure_step extends restore_structure_step { | |
936 | ||
937 | protected function define_structure() { | |
938 | ||
939 | $paths = array(); | |
940 | ||
941 | // Observe the role_assignments setting | |
942 | if ($this->get_setting_value('role_assignments')) { | |
943 | $paths[] = new restore_path_element('assignment', '/roles/role_assignments/assignment'); | |
944 | } | |
945 | $paths[] = new restore_path_element('override', '/roles/role_overrides/override'); | |
946 | ||
947 | return $paths; | |
948 | } | |
949 | ||
950 | public function process_assignment($data) { | |
28b6ff82 EL |
951 | global $DB; |
952 | ||
024c288d EL |
953 | $data = (object)$data; |
954 | ||
955 | // Check roleid, userid are one of the mapped ones | |
956 | $newroleid = $this->get_mappingid('role', $data->roleid); | |
957 | $newuserid = $this->get_mappingid('user', $data->userid); | |
b8e455a7 EL |
958 | // If newroleid and newuserid and component is empty and context valid assign via API (handles dupes and friends) |
959 | if ($newroleid && $newuserid && empty($data->component) && $this->task->get_contextid()) { | |
28b6ff82 EL |
960 | // Only assign roles to not deleted users |
961 | if ($DB->record_exists('user', array('id' => $newuserid, 'deleted' => 0))) { | |
962 | // TODO: role_assign() needs one userid param to be able to specify our restore userid | |
963 | role_assign($newroleid, $newuserid, $this->task->get_contextid()); | |
964 | } | |
024c288d EL |
965 | } |
966 | } | |
967 | ||
968 | public function process_override($data) { | |
969 | $data = (object)$data; | |
970 | ||
971 | // Check roleid is one of the mapped ones | |
972 | $newroleid = $this->get_mappingid('role', $data->roleid); | |
b8e455a7 EL |
973 | // If newroleid and context are valid assign it via API (it handles dupes and so on) |
974 | if ($newroleid && $this->task->get_contextid()) { | |
024c288d | 975 | // TODO: assign_capability() needs one userid param to be able to specify our restore userid |
b8e455a7 | 976 | // TODO: it seems that assign_capability() doesn't check for valid capabilities at all ??? |
024c288d EL |
977 | assign_capability($data->capability, $data->permission, $newroleid, $this->task->get_contextid()); |
978 | } | |
979 | } | |
980 | } | |
981 | ||
982 | /** | |
983 | * This structure steps restores the enrol plugins and their underlying | |
984 | * enrolments, performing all the mappings and/or movements required | |
985 | */ | |
986 | class restore_enrolments_structure_step extends restore_structure_step { | |
987 | ||
988 | protected function define_structure() { | |
989 | ||
990 | $paths = array(); | |
991 | ||
992 | $paths[] = new restore_path_element('enrol', '/enrolments/enrols/enrol'); | |
993 | $paths[] = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment'); | |
994 | ||
995 | return $paths; | |
996 | } | |
997 | ||
998 | public function process_enrol($data) { | |
999 | global $DB; | |
1000 | ||
1001 | $data = (object)$data; | |
1002 | $oldid = $data->id; // We'll need this later | |
1003 | ||
1004 | // TODO: Just one quick process of manual enrol_plugin. Add the rest (complex ones) and fix this | |
1005 | if ($data->enrol !== 'manual') { | |
1006 | debugging("Skipping '{$data->enrol}' enrolment plugin. Must be implemented", DEBUG_DEVELOPER); | |
1007 | return; | |
1008 | } | |
1009 | ||
1010 | // Perform various checks to decide what to do with the enrol plugin | |
1011 | $installed = array_key_exists($data->enrol, enrol_get_plugins(false)); | |
1012 | $enabled = enrol_is_enabled($data->enrol); | |
1013 | $exists = 0; | |
1014 | $roleid = $this->get_mappingid('role', $data->roleid); | |
1015 | if ($rec = $DB->get_record('enrol', array('courseid' => $this->get_courseid(), 'enrol' => $data->enrol))) { | |
1016 | $exists = $rec->id; | |
1017 | } | |
1018 | // If installed and enabled, continue processing | |
1019 | if ($installed && $enabled) { | |
1020 | // If not exists in course and we have a target role mapping | |
1021 | if (!$exists && $roleid) { | |
1022 | $data->roleid = $roleid; | |
1023 | $enrol = enrol_get_plugin($data->enrol); | |
1024 | $courserec = $DB->get_record('course', array('id' => $this->get_courseid())); // Requires object, uses only id!! | |
1025 | $newitemid = $enrol->add_instance($courserec, array($data)); | |
1026 | ||
1027 | // Already exists, user it for enrolments | |
1028 | } else { | |
1029 | $newitemid = $exists; | |
1030 | } | |
1031 | ||
1032 | // Not installed and enabled, map to 0 | |
1033 | } else { | |
1034 | $newitemid = 0; | |
1035 | } | |
1036 | // Perform the simple mapping and done | |
1037 | $this->set_mapping('enrol', $oldid, $newitemid); | |
1038 | } | |
1039 | ||
1040 | public function process_enrolment($data) { | |
1041 | global $DB; | |
1042 | ||
1043 | $data = (object)$data; | |
1044 | ||
1045 | // Process only if parent instance have been mapped | |
1046 | if ($enrolid = $this->get_new_parentid('enrol')) { | |
1047 | // And only if user is a mapped one | |
1048 | if ($userid = $this->get_mappingid('user', $data->userid)) { | |
1049 | // TODO: Surely need to use API (enrol_user) here, instead of the current low-level impl | |
1050 | // TODO: Note enrol_user() sticks to $USER->id (need to add userid param) | |
1051 | $enrolment = new stdclass(); | |
1052 | $enrolment->enrolid = $enrolid; | |
1053 | $enrolment->userid = $userid; | |
1054 | if (!$DB->record_exists('user_enrolments', (array)$enrolment)) { | |
1055 | $enrolment->status = $data->status; | |
1056 | $enrolment->timestart = $data->timestart; | |
1057 | $enrolment->timeend = $data->timeend; | |
1058 | $enrolment->modifierid = $this->task->get_userid(); | |
1059 | $enrolment->timecreated = time(); | |
1060 | $enrolment->timemodified = 0; | |
1061 | $DB->insert_record('user_enrolments', $enrolment); | |
1062 | } | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | } | |
21e51c86 EL |
1067 | |
1068 | ||
1069 | /** | |
1070 | * This structure steps restores the filters and their configs | |
1071 | */ | |
1072 | class restore_filters_structure_step extends restore_structure_step { | |
1073 | ||
1074 | protected function define_structure() { | |
1075 | ||
1076 | $paths = array(); | |
1077 | ||
1078 | $paths[] = new restore_path_element('active', '/filters/filter_actives/filter_active'); | |
1079 | $paths[] = new restore_path_element('config', '/filters/filter_configs/filter_config'); | |
1080 | ||
1081 | return $paths; | |
1082 | } | |
1083 | ||
1084 | public function process_active($data) { | |
1085 | ||
1086 | $data = (object)$data; | |
1087 | ||
1088 | if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do | |
1089 | return; | |
1090 | } | |
1091 | filter_set_local_state($data->filter, $this->task->get_contextid(), $data->active); | |
1092 | } | |
1093 | ||
1094 | public function process_config($data) { | |
1095 | ||
1096 | $data = (object)$data; | |
1097 | ||
1098 | if (!filter_is_enabled($data->filter)) { // Not installed or not enabled, nothing to do | |
1099 | return; | |
1100 | } | |
1101 | filter_set_local_config($data->filter, $this->task->get_contextid(), $data->name, $data->value); | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | ||
1106 | /** | |
1107 | * This structure steps restores the comments | |
1108 | * Note: Cannot use the comments API because defaults to USER->id. | |
1109 | * That should change allowing to pass $userid | |
1110 | */ | |
1111 | class restore_comments_structure_step extends restore_structure_step { | |
1112 | ||
1113 | protected function define_structure() { | |
1114 | ||
1115 | $paths = array(); | |
1116 | ||
1117 | $paths[] = new restore_path_element('comment', '/comments/comment'); | |
1118 | ||
1119 | return $paths; | |
1120 | } | |
1121 | ||
1122 | public function process_comment($data) { | |
1123 | global $DB; | |
1124 | ||
1125 | $data = (object)$data; | |
1126 | ||
1127 | // First of all, if the comment has some itemid, ask to the task what to map | |
1128 | $mapping = false; | |
21e51c86 | 1129 | if ($data->itemid) { |
39aa0280 EL |
1130 | $mapping = $this->task->get_comment_mapping_itemname($data->commentarea); |
1131 | $data->itemid = $this->get_mappingid($mapping, $data->itemid); | |
21e51c86 EL |
1132 | } |
1133 | // Only restore the comment if has no mapping OR we have found the matching mapping | |
39aa0280 | 1134 | if (!$mapping || $data->itemid) { |
b8e455a7 EL |
1135 | // Only if user mapping and context |
1136 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1137 | if ($data->userid && $this->task->get_contextid()) { | |
21e51c86 | 1138 | $data->contextid = $this->task->get_contextid(); |
b8e455a7 EL |
1139 | // Only if there is another comment with same context/user/timecreated |
1140 | $params = array('contextid' => $data->contextid, 'userid' => $data->userid, 'timecreated' => $data->timecreated); | |
1141 | if (!$DB->record_exists('comments', $params)) { | |
1142 | $DB->insert_record('comments', $data); | |
1143 | } | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | ||
9a1cfcbc EL |
1149 | /** |
1150 | * This structure step restores the grade items associated with one activity | |
1151 | * All the grade items are made child of the "course" grade item but the original | |
1152 | * categoryid is saved as parentitemid in the backup_ids table, so, when restoring | |
1153 | * the complete gradebook (categories and calculations), that information is | |
1154 | * available there | |
1155 | */ | |
1156 | class restore_activity_grades_structure_step extends restore_structure_step { | |
1157 | ||
1158 | protected function define_structure() { | |
1159 | ||
1160 | $paths = array(); | |
1161 | $userinfo = $this->get_setting_value('userinfo'); | |
1162 | ||
1163 | $paths[] = new restore_path_element('grade_item', '/activity_gradebook/grade_items/grade_item'); | |
1164 | $paths[] = new restore_path_element('grade_letter', '/activity_gradebook/grade_letters/grade_letter'); | |
1165 | if ($userinfo) { | |
1166 | $paths[] = new restore_path_element('grade_grade', | |
1167 | '/activity_gradebook/grade_items/grade_item/grade_grades/grade_grade'); | |
1168 | } | |
1169 | return $paths; | |
1170 | } | |
1171 | ||
1172 | protected function process_grade_item($data) { | |
1173 | ||
1174 | $data = (object)($data); | |
1175 | $oldid = $data->id; // We'll need these later | |
1176 | $oldparentid = $data->categoryid; | |
1177 | ||
1178 | // make sure top course category exists, all grade items will be associated | |
1179 | // to it. Later, if restoring the whole gradebook, categories will be introduced | |
1180 | $coursecat = grade_category::fetch_course_category($this->get_courseid()); | |
1181 | $coursecatid = $coursecat->id; // Get the categoryid to be used | |
1182 | ||
1183 | unset($data->id); | |
1184 | $data->categoryid = $coursecatid; | |
1185 | $data->courseid = $this->get_courseid(); | |
1186 | $data->iteminstance = $this->task->get_activityid(); | |
1187 | // Don't get any idnumber from course module. Keep them as they are in grade_item->idnumber | |
1188 | // Reason: it's not clear what happens with outcomes->idnumber or activities with multiple items (workshop) | |
1189 | // so the best is to keep the ones already in the gradebook | |
1190 | // Potential problem: duplicates if same items are restored more than once. :-( | |
1191 | // This needs to be fixed in some way (outcomes & activities with multiple items) | |
1192 | // $data->idnumber = get_coursemodule_from_instance($data->itemmodule, $data->iteminstance)->idnumber; | |
1193 | // In any case, verify always for uniqueness | |
a789ac6f | 1194 | $data->idnumber = grade_verify_idnumber($data->idnumber, $this->get_courseid()) ? $data->idnumber : null; |
9a1cfcbc EL |
1195 | $data->scaleid = $this->get_mappingid('scale', $data->scaleid); |
1196 | $data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid); | |
1197 | $data->timecreated = $this->apply_date_offset($data->timecreated); | |
1198 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
1199 | ||
1200 | $gradeitem = new grade_item($data); | |
1201 | $gradeitem->insert('restore'); | |
4d787d8a AD |
1202 | |
1203 | //sortorder is automatically assigned when inserting. Re-instate the previous sortorder | |
1204 | $gradeitem->sortorder = $data->sortorder; | |
1205 | $gradeitem->update('restore'); | |
1206 | ||
dc362c44 EL |
1207 | // Set mapping, saving the original category id into parentitemid |
1208 | // gradebook restore (final task) will need it to reorganise items | |
1209 | $this->set_mapping('grade_item', $oldid, $gradeitem->id, false, null, $oldparentid); | |
9a1cfcbc EL |
1210 | } |
1211 | ||
1212 | protected function process_grade_grade($data) { | |
1213 | $data = (object)($data); | |
1214 | ||
1215 | unset($data->id); | |
1216 | $data->itemid = $this->get_new_parentid('grade_item'); | |
1217 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1218 | $data->usermodified = $this->get_mappingid('user', $data->usermodified); | |
1219 | $data->rawscaleid = $this->get_mappingid('scale', $data->rawscaleid); | |
1220 | // TODO: Ask, all the rest of locktime/exported... work with time... to be rolled? | |
1221 | $data->overridden = $this->apply_date_offset($data->overridden); | |
1222 | ||
1223 | $grade = new grade_grade($data); | |
1224 | $grade->insert('restore'); | |
1225 | // no need to save any grade_grade mapping | |
1226 | } | |
1227 | ||
1228 | /** | |
1229 | * process activity grade_letters. Note that, while these are possible, | |
1230 | * because grade_letters are contextid based, in proctice, only course | |
1231 | * context letters can be defined. So we keep here this method knowing | |
1232 | * it won't be executed ever. gradebook restore will restore course letters. | |
1233 | */ | |
1234 | protected function process_grade_letter($data) { | |
1235 | global $DB; | |
1236 | ||
1237 | $data = (object)$data; | |
1238 | ||
1239 | $data->contextid = $this->task->get_contextid(); | |
1240 | $newitemid = $DB->insert_record('grade_letters', $data); | |
1241 | // no need to save any grade_letter mapping | |
1242 | } | |
1243 | } | |
1244 | ||
b8e455a7 EL |
1245 | |
1246 | /** | |
1247 | * This structure steps restores one instance + positions of one block | |
1248 | * Note: Positions corresponding to one existing context are restored | |
1249 | * here, but all the ones having unknown contexts are sent to backup_ids | |
1250 | * for a later chance to be restored at the end (final task) | |
1251 | */ | |
1252 | class restore_block_instance_structure_step extends restore_structure_step { | |
1253 | ||
1254 | protected function define_structure() { | |
1255 | ||
1256 | $paths = array(); | |
1257 | ||
1258 | $paths[] = new restore_path_element('block', '/block', true); // Get the whole XML together | |
1259 | $paths[] = new restore_path_element('block_position', '/block/block_positions/block_position'); | |
1260 | ||
1261 | return $paths; | |
1262 | } | |
1263 | ||
1264 | public function process_block($data) { | |
1265 | global $DB; | |
1266 | ||
1267 | $data = (object)$data; // Handy | |
1268 | $oldcontextid = $data->contextid; | |
1269 | $oldid = $data->id; | |
1270 | $positions = isset($data->block_positions['block_position']) ? $data->block_positions['block_position'] : array(); | |
1271 | ||
1272 | // Look for the parent contextid | |
1273 | if (!$data->parentcontextid = $this->get_mappingid('context', $data->parentcontextid)) { | |
1274 | throw new restore_step_exception('restore_block_missing_parent_ctx', $data->parentcontextid); | |
1275 | } | |
1276 | ||
1277 | // If there is already one block of that type in the parent context | |
1278 | // and the block is not multiple, stop processing | |
1279 | if ($DB->record_exists_sql("SELECT bi.id | |
1280 | FROM {block_instances} bi | |
1281 | JOIN {block} b ON b.name = bi.blockname | |
1282 | WHERE bi.parentcontextid = ? | |
1283 | AND bi.blockname = ? | |
1284 | AND b.multiple = 0", array($data->parentcontextid, $data->blockname))) { | |
1285 | return false; | |
1286 | } | |
1287 | ||
1288 | // If there is already one block of that type in the parent context | |
1289 | // with the same showincontexts, pagetypepattern, subpagepattern, defaultregion and configdata | |
1290 | // stop processing | |
1291 | $params = array( | |
1292 | 'blockname' => $data->blockname, 'parentcontextid' => $data->parentcontextid, | |
1293 | 'showinsubcontexts' => $data->showinsubcontexts, 'pagetypepattern' => $data->pagetypepattern, | |
1294 | 'subpagepattern' => $data->subpagepattern, 'defaultregion' => $data->defaultregion); | |
1295 | if ($birecs = $DB->get_records('block_instances', $params)) { | |
1296 | foreach($birecs as $birec) { | |
1297 | if ($birec->configdata == $data->configdata) { | |
1298 | return false; | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | // Set task old contextid, blockid and blockname once we know them | |
1304 | $this->task->set_old_contextid($oldcontextid); | |
1305 | $this->task->set_old_blockid($oldid); | |
1306 | $this->task->set_blockname($data->blockname); | |
1307 | ||
1308 | // Let's look for anything within configdata neededing processing | |
1309 | // (nulls and uses of legacy file.php) | |
1310 | if ($attrstotransform = $this->task->get_configdata_encoded_attributes()) { | |
1311 | $configdata = (array)unserialize(base64_decode($data->configdata)); | |
1312 | foreach ($configdata as $attribute => $value) { | |
1313 | if (in_array($attribute, $attrstotransform)) { | |
1314 | $configdata[$attribute] = $this->contentprocessor->process_cdata($value); | |
1315 | } | |
1316 | } | |
1317 | $data->configdata = base64_encode(serialize((object)$configdata)); | |
1318 | } | |
1319 | ||
1320 | // Create the block instance | |
1321 | $newitemid = $DB->insert_record('block_instances', $data); | |
1322 | // Save the mapping (with restorefiles support) | |
1323 | $this->set_mapping('block_instance', $oldid, $newitemid, true); | |
1324 | // Create the block context | |
1325 | $newcontextid = get_context_instance(CONTEXT_BLOCK, $newitemid)->id; | |
1326 | // Save the block contexts mapping and sent it to task | |
1327 | $this->set_mapping('context', $oldcontextid, $newcontextid); | |
1328 | $this->task->set_contextid($newcontextid); | |
1329 | $this->task->set_blockid($newitemid); | |
1330 | ||
1331 | // Restore block fileareas if declared | |
1332 | $component = 'block_' . $this->task->get_blockname(); | |
1333 | foreach ($this->task->get_fileareas() as $filearea) { // Simple match by contextid. No itemname needed | |
1334 | $this->add_related_files($component, $filearea, null); | |
1335 | } | |
1336 | ||
1337 | // Process block positions, creating them or accumulating for final step | |
1338 | foreach($positions as $position) { | |
1339 | $position = (object)$position; | |
1340 | $position->blockinstanceid = $newitemid; // The instance is always the restored one | |
1341 | // If position is for one already mapped (known) contextid | |
1342 | // process it now, creating the position | |
1343 | if ($newpositionctxid = $this->get_mappingid('context', $position->contextid)) { | |
1344 | $position->contextid = $newpositionctxid; | |
1345 | // Create the block position | |
1346 | $DB->insert_record('block_positions', $position); | |
1347 | ||
1348 | // The position belongs to an unknown context, send it to backup_ids | |
1349 | // to process them as part of the final steps of restore. We send the | |
1350 | // whole $position object there, hence use the low level method. | |
1351 | } else { | |
1352 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'block_position', $position->id, 0, null, $position); | |
21e51c86 EL |
1353 | } |
1354 | } | |
1355 | } | |
1356 | } | |
394edb7e EL |
1357 | |
1358 | /** | |
1359 | * Structure step to restore common course_module information | |
1360 | * | |
1361 | * This step will process the module.xml file for one activity, in order to restore | |
1362 | * the corresponding information to the course_modules table, skipping various bits | |
1363 | * of information based on CFG settings (groupings, completion...) in order to fullfill | |
1364 | * all the reqs to be able to create the context to be used by all the rest of steps | |
1365 | * in the activity restore task | |
1366 | */ | |
1367 | class restore_module_structure_step extends restore_structure_step { | |
1368 | ||
1369 | protected function define_structure() { | |
1370 | global $CFG; | |
1371 | ||
1372 | $paths = array(); | |
1373 | ||
1374 | $paths[] = new restore_path_element('module', '/module'); | |
1375 | if ($CFG->enableavailability) { | |
1376 | $paths[] = new restore_path_element('availability', '/module/availability_info/availability'); | |
1377 | } | |
1378 | ||
1379 | return $paths; | |
1380 | } | |
1381 | ||
1382 | protected function process_module($data) { | |
1383 | global $CFG, $DB; | |
1384 | ||
1385 | $data = (object)$data; | |
1386 | $oldid = $data->id; | |
1387 | ||
1388 | $data->course = $this->task->get_courseid(); | |
1389 | $data->module = $DB->get_field('modules', 'id', array('name' => $data->modulename)); | |
aa39be20 EL |
1390 | // Map section (first try by course_section mapping match. Useful in course and section restores) |
1391 | $data->section = $this->get_mappingid('course_section', $data->sectionid); | |
1392 | if (!$data->section) { // mapping failed, try to get section by sectionnumber matching | |
1393 | $params = array( | |
1394 | 'course' => $this->get_courseid(), | |
1395 | 'section' => $data->sectionnumber); | |
1396 | $data->section = $DB->get_field('course_sections', 'id', $params); | |
1397 | } | |
1398 | if (!$data->section) { // sectionnumber failed, try to get first section in course | |
1399 | $params = array( | |
1400 | 'course' => $this->get_courseid()); | |
1401 | $data->section = $DB->get_field('course_sections', 'MIN(id)', $params); | |
1402 | } | |
1403 | if (!$data->section) { // no sections in course, create section 0 and 1 and assign module to 1 | |
1404 | $sectionrec = array( | |
1405 | 'course' => $this->get_courseid(), | |
1406 | 'section' => 0); | |
1407 | $DB->insert_record('course_sections', $sectionrec); // section 0 | |
1408 | $sectionrec = array( | |
1409 | 'course' => $this->get_courseid(), | |
1410 | 'section' => 1); | |
1411 | $data->section = $DB->insert_record('course_sections', $sectionrec); // section 1 | |
1412 | } | |
394edb7e EL |
1413 | $data->groupingid= $this->get_mappingid('grouping', $data->groupingid); // grouping |
1414 | if (!$CFG->enablegroupmembersonly) { // observe groupsmemberonly | |
1415 | $data->groupmembersonly = 0; | |
1416 | } | |
1417 | if (!grade_verify_idnumber($data->idnumber, $this->get_courseid())) { // idnumber uniqueness | |
1418 | $data->idnumber = ''; | |
1419 | } | |
5095f325 | 1420 | if (empty($CFG->enablecompletion)) { // completion |
394edb7e EL |
1421 | $data->completion = 0; |
1422 | $data->completiongradeitemnumber = null; | |
1423 | $data->completionview = 0; | |
1424 | $data->completionexpected = 0; | |
1425 | } else { | |
1426 | $data->completionexpected = $this->apply_date_offset($data->completionexpected); | |
1427 | } | |
1428 | if (empty($CFG->enableavailability)) { | |
1429 | $data->availablefrom = 0; | |
1430 | $data->availableuntil = 0; | |
1431 | $data->showavailability = 0; | |
1432 | } else { | |
1433 | $data->availablefrom = $this->apply_date_offset($data->availablefrom); | |
1434 | $data->availableuntil= $this->apply_date_offset($data->availableuntil); | |
1435 | } | |
1436 | $data->instance = 0; // Set to 0 for now, going to create it soon (next step) | |
1437 | ||
1438 | // course_module record ready, insert it | |
1439 | $newitemid = $DB->insert_record('course_modules', $data); | |
1440 | // save mapping | |
1441 | $this->set_mapping('course_module', $oldid, $newitemid); | |
1442 | // set the new course_module id in the task | |
1443 | $this->task->set_moduleid($newitemid); | |
1444 | // we can now create the context safely | |
1445 | $ctxid = get_context_instance(CONTEXT_MODULE, $newitemid)->id; | |
1446 | // set the new context id in the task | |
1447 | $this->task->set_contextid($ctxid); | |
1448 | // update sequence field in course_section | |
1449 | if ($sequence = $DB->get_field('course_sections', 'sequence', array('id' => $data->section))) { | |
1450 | $sequence .= ',' . $newitemid; | |
1451 | } else { | |
1452 | $sequence = $newitemid; | |
1453 | } | |
1454 | $DB->set_field('course_sections', 'sequence', $sequence, array('id' => $data->section)); | |
1455 | } | |
1456 | ||
1457 | ||
1458 | protected function process_availability($data) { | |
394edb7e | 1459 | $data = (object)$data; |
5095f325 EL |
1460 | // Simply going to store the whole availability record now, we'll process |
1461 | // all them later in the final task (once all actvivities have been restored) | |
1462 | // Let's call the low level one to be able to store the whole object | |
1463 | $data->coursemoduleid = $this->task->get_moduleid(); // Let add the availability cmid | |
1464 | restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_availability', $data->id, 0, null, $data); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | /** | |
1469 | * Structure step that will process the user activity completion | |
1470 | * information if all these conditions are met: | |
1471 | * - Target site has completion enabled ($CFG->enablecompletion) | |
1472 | * - Activity includes completion info (file_exists) | |
1473 | */ | |
1474 | class restore_userscompletion_structure_step extends restore_structure_step { | |
1475 | ||
1476 | /** | |
1477 | * To conditionally decide if this step must be executed | |
1478 | * Note the "settings" conditions are evaluated in the | |
1479 | * corresponding task. Here we check for other conditions | |
1480 | * not being restore settings (files, site settings...) | |
1481 | */ | |
1482 | protected function execute_condition() { | |
1483 | global $CFG; | |
1484 | ||
1485 | // Completion disabled in this site, don't execute | |
1486 | if (empty($CFG->enablecompletion)) { | |
1487 | return false; | |
1488 | } | |
1489 | ||
1490 | // No user completion info found, don't execute | |
1491 | $fullpath = $this->task->get_taskbasepath(); | |
1492 | $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; | |
1493 | if (!file_exists($fullpath)) { | |
1494 | return false; | |
1495 | } | |
1496 | ||
1497 | // Arrived here, execute the step | |
1498 | return true; | |
1499 | } | |
1500 | ||
1501 | protected function define_structure() { | |
1502 | ||
1503 | $paths = array(); | |
1504 | ||
1505 | $paths[] = new restore_path_element('completion', '/completions/completion'); | |
1506 | ||
1507 | return $paths; | |
1508 | } | |
1509 | ||
1510 | protected function process_completion($data) { | |
1511 | global $DB; | |
1512 | ||
1513 | $data = (object)$data; | |
1514 | ||
1515 | $data->coursemoduleid = $this->task->get_moduleid(); | |
1516 | $data->userid = $this->get_mappingid('user', $data->userid); | |
1517 | $data->timemodified = $this->apply_date_offset($data->timemodified); | |
1518 | ||
1519 | $DB->insert_record('course_modules_completion', $data); | |
394edb7e EL |
1520 | } |
1521 | } | |
1522 | ||
1523 | /** | |
1524 | * Abstract structure step, parent of all the activity structure steps. Used to suuport | |
1525 | * the main <activity ...> tag and process it. Also provides subplugin support for | |
1526 | * activities. | |
1527 | */ | |
1528 | abstract class restore_activity_structure_step extends restore_structure_step { | |
1529 | ||
91d11057 EL |
1530 | protected function add_subplugin_structure($subplugintype, $element) { |
1531 | ||
1532 | global $CFG; | |
1533 | ||
1534 | // Check the requested subplugintype is a valid one | |
1535 | $subpluginsfile = $CFG->dirroot . '/mod/' . $this->task->get_modulename() . '/db/subplugins.php'; | |
1536 | if (!file_exists($subpluginsfile)) { | |
1537 | throw new restore_step_exception('activity_missing_subplugins_php_file', $this->task->get_modulename()); | |
1538 | } | |
1539 | include($subpluginsfile); | |
1540 | if (!array_key_exists($subplugintype, $subplugins)) { | |
1541 | throw new restore_step_exception('incorrect_subplugin_type', $subplugintype); | |
1542 | } | |
1543 | // Get all the restore path elements, looking across all the subplugin dirs | |
1544 | $subpluginsdirs = get_plugin_list($subplugintype); | |
1545 | foreach ($subpluginsdirs as $name => $subpluginsdir) { | |
1546 | $classname = 'restore_' . $subplugintype . '_' . $name . '_subplugin'; | |
1547 | $restorefile = $subpluginsdir . '/backup/moodle2/' . $classname . '.class.php'; | |
1548 | if (file_exists($restorefile)) { | |
1549 | require_once($restorefile); | |
1550 | $restoresubplugin = new $classname($subplugintype, $name, $this); | |
1551 | // Add subplugin paths to the step | |
1552 | $this->prepare_pathelements($restoresubplugin->define_subplugin_structure($element)); | |
1553 | } | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | /** | |
1558 | * As far as activity restore steps are implementing restore_subplugin stuff, they need to | |
1559 | * have the parent task available for wrapping purposes (get course/context....) | |
1560 | */ | |
1561 | public function get_task() { | |
1562 | return $this->task; | |
394edb7e EL |
1563 | } |
1564 | ||
1565 | /** | |
1566 | * Adds support for the 'activity' path that is common to all the activities | |
1567 | * and will be processed globally here | |
1568 | */ | |
1569 | protected function prepare_activity_structure($paths) { | |
1570 | ||
1571 | $paths[] = new restore_path_element('activity', '/activity'); | |
1572 | ||
1573 | return $paths; | |
1574 | } | |
1575 | ||
1576 | /** | |
1577 | * Process the activity path, informing the task about various ids, needed later | |
1578 | */ | |
1579 | protected function process_activity($data) { | |
1580 | $data = (object)$data; | |
1581 | $this->task->set_old_contextid($data->contextid); // Save old contextid in task | |
1582 | $this->set_mapping('context', $data->contextid, $this->task->get_contextid()); // Set the mapping | |
1583 | $this->task->set_old_activityid($data->id); // Save old activityid in task | |
1584 | } | |
1585 | ||
1586 | /** | |
1587 | * This must be invoked inmediately after creating the "module" activity record (forum, choice...) | |
1588 | * and will adjust the new activity id (the instance) in various places | |
1589 | */ | |
1590 | protected function apply_activity_instance($newitemid) { | |
1591 | global $DB; | |
1592 | ||
1593 | $this->task->set_activityid($newitemid); // Save activity id in task | |
1594 | // Apply the id to course_sections->instanceid | |
1595 | $DB->set_field('course_modules', 'instance', $newitemid, array('id' => $this->task->get_moduleid())); | |
1596 | // Do the mapping for modulename, preparing it for files by oldcontext | |
1597 | $modulename = $this->task->get_modulename(); | |
1598 | $oldid = $this->task->get_old_activityid(); | |
1599 | $this->set_mapping($modulename, $oldid, $newitemid, true); | |
1600 | } | |
1601 | } |