enrol/ldap: MDL-23424 don't use $CFG->enrol_ldap_user_memberfield if it's not defined
[moodle.git] / backup / moodle2 / restore_stepslib.php
CommitLineData
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 */
32class 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 */
57class 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 */
74class 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 */
95class 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 */
131class 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 */
152class 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 174class 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 */
192class 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 */
205class 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
326 * Note group members only will be added if restoring user info
327 */
328class restore_scales_structure_step extends restore_structure_step {
329
330 protected function define_structure() {
331
332 $paths = array(); // Add paths here
333 $paths[] = new restore_path_element('scale', '/scales_definition/scale');
334 return $paths;
335 }
336
337 protected function process_scale($data) {
338 global $DB;
339
340 $data = (object)$data;
341
342 $restorefiles = false; // Only if we end creating the group
343
344 $oldid = $data->id; // need this saved for later
345
346 // Look for scale (by 'scale' both in standard (course=0) and current course
347 // with priority to standard scales (ORDER clause)
348 // scale is not course unique, use get_record_sql to suppress warning
349 // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides
350 $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc');
351 $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale);
352 if (!$scadb = $DB->get_record_sql("SELECT *
353 FROM {scale}
354 WHERE courseid IN (0, :courseid)
355 AND $compare_scale_clause
356 ORDER BY courseid", $params, IGNORE_MULTIPLE)) {
357 // Remap the user if possible, defaut to user performing the restore if not
358 $userid = $this->get_mappingid('user', $data->userid);
359 $data->userid = $userid ? $userid : $this->get_userid();
360 // Remap the course if course scale
361 $data->courseid = $data->courseid ? $this->get_courseid() : 0;
362 // If global scale (course=0), check the user has perms to create it
363 // falling to course scale if not
364 $systemctx = get_context_instance(CONTEXT_SYSTEM);
365 if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $data->userid)) {
366 $data->courseid = $this->get_courseid();
367 }
368 // scale doesn't exist, create
369 $newitemid = $DB->insert_record('scale', $data);
370 $restorefiles = true; // We'll restore the files
371 } else {
372 // scale exists, use it
373 $newitemid = $scadb->id;
374 }
375 // Save the id mapping (with files support at system context)
376 $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid());
377 }
378
379 protected function after_execute() {
380 // Add scales related files, matching with "scale" mappings
381 $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid());
382 }
76cfb124
EL
383}
384
c0440b3f 385
482aac65
EL
386/*
387 * Structure step that will read the course.xml file, loading it and performing
388 * various actions depending of the site/restore settings
389 */
390class restore_course_structure_step extends restore_structure_step {
391
392 protected function define_structure() {
393
394 $course = new restore_path_element('course', '/course', true); // Grouped
395 $category = new restore_path_element('category', '/course/category');
396 $tag = new restore_path_element('tag', '/course/tags/tag');
397 $allowed = new restore_path_element('allowed', '/course/allowed_modules/module');
398
399 return array($course, $category, $tag, $allowed);
400 }
401
402 // Processing functions go here
403 public function process_course($data) {
71a50b13 404 // TODO: don't forget to remap defaultgroupingid
482aac65
EL
405 print_object('stopped before processing course. Continue here');
406 }
407
408}