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 |
482aac65 EL |
62 | backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs |
63 | if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally | |
64 | backup_helper::delete_backup_dir($this->get_restoreid()); // Empty backup dir | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | /* | |
70 | * Execution step that, *conditionally* (if there isn't preloaded information) | |
71 | * will load the inforef files for all the included course/section/activity tasks | |
72 | * to backup_temp_ids. They will be stored with "xxxxref" as itemname | |
73 | */ | |
74 | class restore_load_included_inforef_records extends restore_execution_step { | |
75 | ||
76 | protected function define_execution() { | |
77 | ||
78 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
79 | return; | |
80 | } | |
81 | ||
82 | // Get all the included inforef files | |
83 | $files = restore_dbops::get_needed_inforef_files($this->get_restoreid()); | |
84 | foreach ($files as $file) { | |
85 | restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $file); // Load each inforef file to temp_ids | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
76cfb124 | 90 | /* |
b8bb45b0 | 91 | * Execution step that will load all the needed files into backup_files_temp |
76cfb124 EL |
92 | * - info: contains the whole original object (times, names...) |
93 | * (all them being original ids as loaded from xml) | |
94 | */ | |
95 | class restore_load_included_files extends restore_structure_step { | |
96 | ||
97 | protected function define_structure() { | |
98 | ||
99 | $file = new restore_path_element('file', '/files/file'); | |
100 | ||
101 | return array($file); | |
102 | } | |
103 | ||
104 | // Processing functions go here | |
105 | public function process_file($data) { | |
106 | ||
107 | $data = (object)$data; // handy | |
108 | ||
76cfb124 EL |
109 | // load it if needed: |
110 | // - it it is one of the annotated inforef files (course/section/activity/block) | |
71a50b13 | 111 | // - it is one "user", "group", "grouping" or "grade" component file (that aren't sent to inforef ever) |
b8bb45b0 | 112 | $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id); |
71a50b13 EL |
113 | $iscomponent = ($data->component == 'user' || $data->component == 'group' || |
114 | $data->component == 'grouping' || $data->component == 'grade'); | |
76cfb124 | 115 | if ($isfileref || $iscomponent) { |
b8bb45b0 | 116 | restore_dbops::set_backup_files_record($this->get_restoreid(), $data); |
76cfb124 EL |
117 | } |
118 | } | |
119 | } | |
120 | ||
71a50b13 EL |
121 | /** |
122 | * Execution step that, *conditionally* (if there isn't preloaded information), | |
123 | * will load all the needed roles to backup_temp_ids. They will be stored with | |
124 | * "role" itemname. Also it will perform one automatic mapping to roles existing | |
125 | * in the target site, based in permissions of the user performing the restore, | |
126 | * archetypes and other bits. At the end, each original role will have its associated | |
127 | * target role or 0 if it's going to be skipped. Note we wrap everything over one | |
128 | * restore_dbops method, as far as the same stuff is going to be also executed | |
129 | * by restore prechecks | |
130 | */ | |
131 | class restore_load_and_map_roles extends restore_execution_step { | |
132 | ||
133 | protected function define_execution() { | |
134 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
135 | return; | |
136 | } | |
137 | ||
138 | $file = $this->get_basepath() . '/roles.xml'; | |
139 | // Load needed toles to temp_ids | |
140 | restore_dbops::load_roles_to_tempids($this->get_restoreid(), $file); | |
141 | // Process roles, mapping/skipping. Any error throws exception | |
142 | restore_dbops::process_included_roles($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
143 | } | |
144 | } | |
145 | ||
482aac65 EL |
146 | /** |
147 | * Execution step that, *conditionally* (if there isn't preloaded information | |
148 | * and users have been selected in settings, will load all the needed users | |
149 | * to backup_temp_ids. They will be stored with "user" itemname and with | |
76cfb124 | 150 | * their original contextid as paremitemid |
482aac65 EL |
151 | */ |
152 | class restore_load_included_users extends restore_execution_step { | |
153 | ||
154 | protected function define_execution() { | |
155 | ||
156 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
157 | return; | |
158 | } | |
159 | if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do | |
160 | return; | |
161 | } | |
162 | $file = $this->get_basepath() . '/users.xml'; | |
163 | restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids | |
164 | } | |
165 | } | |
166 | ||
167 | /** | |
168 | * Execution step that, *conditionally* (if there isn't preloaded information | |
169 | * and users have been selected in settings, will process all the needed users | |
170 | * in order to decide and perform any action with them (create / map / error) | |
171 | * Note: Any error will cause exception, as far as this is the same processing | |
172 | * than the one into restore prechecks (that should have stopped process earlier) | |
173 | */ | |
76cfb124 | 174 | class restore_process_included_users extends restore_execution_step { |
482aac65 EL |
175 | |
176 | protected function define_execution() { | |
177 | ||
178 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
179 | return; | |
180 | } | |
181 | if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do | |
182 | return; | |
183 | } | |
184 | restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
185 | } | |
186 | } | |
187 | ||
76cfb124 EL |
188 | /** |
189 | * Execution step that will create all the needed users as calculated | |
190 | * by @restore_process_included_users (those having newiteind = 0) | |
191 | */ | |
192 | class restore_create_included_users extends restore_execution_step { | |
193 | ||
194 | protected function define_execution() { | |
195 | ||
196 | restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files')); | |
197 | } | |
198 | } | |
199 | ||
200 | /** | |
201 | * Structure step that will create all the needed groups and groupings | |
202 | * by loading them from the groups.xml file performing the required matches. | |
203 | * Note group members only will be added if restoring user info | |
204 | */ | |
205 | class restore_groups_structure_step extends restore_structure_step { | |
206 | ||
c0440b3f EL |
207 | protected function define_structure() { |
208 | ||
209 | $paths = array(); // Add paths here | |
210 | ||
211 | $paths[] = new restore_path_element('group', '/groups/group'); | |
212 | if ($this->get_setting_value('users')) { | |
213 | $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member'); | |
214 | } | |
215 | $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping'); | |
216 | $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group'); | |
217 | ||
218 | return $paths; | |
219 | } | |
220 | ||
221 | // Processing functions go here | |
222 | public function process_group($data) { | |
223 | global $DB; | |
224 | ||
225 | $data = (object)$data; // handy | |
226 | $data->courseid = $this->get_courseid(); | |
227 | ||
228 | $oldid = $data->id; // need this saved for later | |
76cfb124 | 229 | |
c0440b3f EL |
230 | $restorefiles = false; // Only if we end creating the group |
231 | ||
232 | // Search if the group already exists (by name & description) in the target course | |
233 | $description_clause = ''; | |
234 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
235 | if (!empty($data->description)) { | |
236 | $description_clause = ' AND ' . | |
237 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
238 | $params['desc'] = $data->description; | |
239 | } | |
240 | if (!$groupdb = $DB->get_record_sql("SELECT * | |
241 | FROM {groups} | |
242 | WHERE courseid = :courseid | |
243 | AND name = :grname $description_clause", $params)) { | |
244 | // group doesn't exist, create | |
245 | $newitemid = $DB->insert_record('groups', $data); | |
246 | $restorefiles = true; // We'll restore the files | |
247 | } else { | |
248 | // group exists, use it | |
249 | $newitemid = $groupdb->id; | |
250 | } | |
251 | // Save the id mapping | |
252 | $this->set_mapping('group', $oldid, $newitemid, $restorefiles); | |
253 | } | |
254 | ||
255 | public function process_member($data) { | |
256 | global $DB; | |
257 | ||
258 | $data = (object)$data; // handy | |
259 | ||
260 | // get parent group->id | |
261 | $data->groupid = $this->get_new_parentid('group'); | |
262 | ||
263 | // map user newitemid and insert if not member already | |
264 | if ($data->userid = $this->get_mappingid('user', $data->userid)) { | |
265 | if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) { | |
266 | $DB->insert_record('groups_members', $data); | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | public function process_grouping($data) { | |
71a50b13 EL |
272 | global $DB; |
273 | ||
274 | $data = (object)$data; // handy | |
275 | $data->courseid = $this->get_courseid(); | |
276 | ||
277 | $oldid = $data->id; // need this saved for later | |
278 | $restorefiles = false; // Only if we end creating the grouping | |
279 | ||
280 | // Search if the grouping already exists (by name & description) in the target course | |
281 | $description_clause = ''; | |
282 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
283 | if (!empty($data->description)) { | |
284 | $description_clause = ' AND ' . | |
285 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
286 | $params['desc'] = $data->description; | |
287 | } | |
288 | if (!$groupingdb = $DB->get_record_sql("SELECT * | |
289 | FROM {groupings} | |
290 | WHERE courseid = :courseid | |
291 | AND name = :grname $description_clause", $params)) { | |
292 | // grouping doesn't exist, create | |
293 | $newitemid = $DB->insert_record('groupings', $data); | |
294 | $restorefiles = true; // We'll restore the files | |
295 | } else { | |
296 | // grouping exists, use it | |
297 | $newitemid = $groupingdb->id; | |
298 | } | |
299 | // Save the id mapping | |
300 | $this->set_mapping('grouping', $oldid, $newitemid, $restorefiles); | |
c0440b3f EL |
301 | } |
302 | ||
303 | public function process_grouping_group($data) { | |
71a50b13 EL |
304 | global $DB; |
305 | ||
306 | $data = (object)$data; | |
307 | ||
308 | $data->groupingid = $this->get_new_parentid('grouping'); // Use new parentid | |
309 | $data->groupid = $this->get_mappingid('group', $data->groupid); // Get from mappings | |
310 | $DB->insert_record('groupings_groups', $data); // No need to set this mapping (no child info nor files) | |
c0440b3f EL |
311 | } |
312 | ||
313 | protected function after_execute() { | |
314 | // Add group related files, matching with "group" mappings | |
315 | $this->add_related_files('group', 'icon', 'group'); | |
316 | $this->add_related_files('group', 'description', 'group'); | |
71a50b13 EL |
317 | // Add grouping related files, matching with "grouping" mappings |
318 | $this->add_related_files('grouping', 'description', 'grouping'); | |
c0440b3f EL |
319 | } |
320 | ||
321 | } | |
322 | ||
323 | /** | |
324 | * Structure step that will create all the needed scales | |
325 | * by loading them from the scales.xml | |
c0440b3f EL |
326 | */ |
327 | class restore_scales_structure_step extends restore_structure_step { | |
328 | ||
329 | protected function define_structure() { | |
330 | ||
331 | $paths = array(); // Add paths here | |
332 | $paths[] = new restore_path_element('scale', '/scales_definition/scale'); | |
333 | return $paths; | |
334 | } | |
335 | ||
336 | protected function process_scale($data) { | |
337 | global $DB; | |
338 | ||
339 | $data = (object)$data; | |
340 | ||
341 | $restorefiles = false; // Only if we end creating the group | |
342 | ||
343 | $oldid = $data->id; // need this saved for later | |
344 | ||
345 | // Look for scale (by 'scale' both in standard (course=0) and current course | |
346 | // with priority to standard scales (ORDER clause) | |
347 | // scale is not course unique, use get_record_sql to suppress warning | |
348 | // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides | |
349 | $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc'); | |
350 | $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale); | |
351 | if (!$scadb = $DB->get_record_sql("SELECT * | |
352 | FROM {scale} | |
353 | WHERE courseid IN (0, :courseid) | |
354 | AND $compare_scale_clause | |
355 | ORDER BY courseid", $params, IGNORE_MULTIPLE)) { | |
356 | // Remap the user if possible, defaut to user performing the restore if not | |
357 | $userid = $this->get_mappingid('user', $data->userid); | |
358 | $data->userid = $userid ? $userid : $this->get_userid(); | |
359 | // Remap the course if course scale | |
360 | $data->courseid = $data->courseid ? $this->get_courseid() : 0; | |
361 | // If global scale (course=0), check the user has perms to create it | |
362 | // falling to course scale if not | |
363 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
364 | if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $data->userid)) { | |
365 | $data->courseid = $this->get_courseid(); | |
366 | } | |
367 | // scale doesn't exist, create | |
368 | $newitemid = $DB->insert_record('scale', $data); | |
369 | $restorefiles = true; // We'll restore the files | |
370 | } else { | |
371 | // scale exists, use it | |
372 | $newitemid = $scadb->id; | |
373 | } | |
374 | // Save the id mapping (with files support at system context) | |
375 | $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
376 | } | |
377 | ||
378 | protected function after_execute() { | |
379 | // Add scales related files, matching with "scale" mappings | |
380 | $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid()); | |
381 | } | |
76cfb124 EL |
382 | } |
383 | ||
c0440b3f | 384 | |
c8730ff0 EL |
385 | /** |
386 | * Structure step that will create all the needed outocomes | |
387 | * by loading them from the outcomes.xml | |
388 | */ | |
389 | class restore_outcomes_structure_step extends restore_structure_step { | |
390 | ||
391 | protected function define_structure() { | |
392 | ||
393 | $paths = array(); // Add paths here | |
394 | $paths[] = new restore_path_element('outcome', '/outcomes_definition/outcome'); | |
395 | return $paths; | |
396 | } | |
397 | ||
398 | protected function process_outcome($data) { | |
399 | global $DB; | |
400 | ||
401 | $data = (object)$data; | |
402 | ||
403 | $restorefiles = false; // Only if we end creating the group | |
404 | ||
405 | $oldid = $data->id; // need this saved for later | |
406 | ||
407 | // Look for outcome (by shortname both in standard (courseid=null) and current course | |
408 | // with priority to standard outcomes (ORDER clause) | |
409 | // outcome is not course unique, use get_record_sql to suppress warning | |
410 | $params = array('courseid' => $this->get_courseid(), 'shortname' => $data->shortname); | |
411 | if (!$outdb = $DB->get_record_sql('SELECT * | |
412 | FROM {grade_outcomes} | |
413 | WHERE shortname = :shortname | |
414 | AND (courseid = :courseid OR courseid IS NULL) | |
415 | ORDER BY COALESCE(courseid, 0)', $params, IGNORE_MULTIPLE)) { | |
416 | // Remap the user | |
417 | $userid = $this->get_mappingid('user', $data->usermodified); | |
418 | $data->usermodified = $userid ? $userid : $this->get_userid(); | |
419 | // Remap the course if course outcome | |
420 | $data->courseid = $data->courseid ? $this->get_courseid() : null; | |
421 | // If global outcome (course=null), check the user has perms to create it | |
422 | // falling to course outcome if not | |
423 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
424 | if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $data->userid)) { | |
425 | $data->courseid = $this->get_courseid(); | |
426 | } | |
427 | // outcome doesn't exist, create | |
428 | $newitemid = $DB->insert_record('grade_outcomes', $data); | |
429 | $restorefiles = true; // We'll restore the files | |
430 | } else { | |
431 | // scale exists, use it | |
432 | $newitemid = $outdb->id; | |
433 | } | |
434 | // Set the corresponding grade_outcomes_courses record | |
435 | $outcourserec = new stdclass(); | |
436 | $outcourserec->courseid = $this->get_courseid(); | |
437 | $outcourserec->outcomeid = $newitemid; | |
438 | if (!$DB->record_exists('grade_outcomes_courses', (array)$outcourserec)) { | |
439 | $DB->insert_record('grade_outcomes_courses', $outcourserec); | |
440 | } | |
441 | // Save the id mapping (with files support at system context) | |
442 | $this->set_mapping('outcome', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
443 | } | |
444 | ||
445 | protected function after_execute() { | |
446 | // Add outcomes related files, matching with "outcome" mappings | |
447 | $this->add_related_files('grade', 'outcome', 'outcome', $this->task->get_old_system_contextid()); | |
448 | } | |
449 | } | |
450 | ||
451 | ||
482aac65 EL |
452 | /* |
453 | * Structure step that will read the course.xml file, loading it and performing | |
454 | * various actions depending of the site/restore settings | |
455 | */ | |
456 | class restore_course_structure_step extends restore_structure_step { | |
457 | ||
458 | protected function define_structure() { | |
459 | ||
460 | $course = new restore_path_element('course', '/course', true); // Grouped | |
461 | $category = new restore_path_element('category', '/course/category'); | |
462 | $tag = new restore_path_element('tag', '/course/tags/tag'); | |
463 | $allowed = new restore_path_element('allowed', '/course/allowed_modules/module'); | |
464 | ||
465 | return array($course, $category, $tag, $allowed); | |
466 | } | |
467 | ||
468 | // Processing functions go here | |
469 | public function process_course($data) { | |
71a50b13 | 470 | // TODO: don't forget to remap defaultgroupingid |
482aac65 EL |
471 | print_object('stopped before processing course. Continue here'); |
472 | } | |
473 | ||
474 | } |