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) | |
111 | // - it is one "user", "group" or "grade" component file | |
b8bb45b0 | 112 | $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id); |
76cfb124 EL |
113 | $iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'grade'); |
114 | if ($isfileref || $iscomponent) { | |
b8bb45b0 | 115 | restore_dbops::set_backup_files_record($this->get_restoreid(), $data); |
76cfb124 EL |
116 | } |
117 | } | |
118 | } | |
119 | ||
482aac65 EL |
120 | /** |
121 | * Execution step that, *conditionally* (if there isn't preloaded information | |
122 | * and users have been selected in settings, will load all the needed users | |
123 | * to backup_temp_ids. They will be stored with "user" itemname and with | |
76cfb124 | 124 | * their original contextid as paremitemid |
482aac65 EL |
125 | */ |
126 | class restore_load_included_users extends restore_execution_step { | |
127 | ||
128 | protected function define_execution() { | |
129 | ||
130 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
131 | return; | |
132 | } | |
133 | if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do | |
134 | return; | |
135 | } | |
136 | $file = $this->get_basepath() . '/users.xml'; | |
137 | restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
142 | * Execution step that, *conditionally* (if there isn't preloaded information | |
143 | * and users have been selected in settings, will process all the needed users | |
144 | * in order to decide and perform any action with them (create / map / error) | |
145 | * Note: Any error will cause exception, as far as this is the same processing | |
146 | * than the one into restore prechecks (that should have stopped process earlier) | |
147 | */ | |
76cfb124 | 148 | class restore_process_included_users extends restore_execution_step { |
482aac65 EL |
149 | |
150 | protected function define_execution() { | |
151 | ||
152 | if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do | |
153 | return; | |
154 | } | |
155 | if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do | |
156 | return; | |
157 | } | |
158 | restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite()); | |
159 | } | |
160 | } | |
161 | ||
76cfb124 EL |
162 | /** |
163 | * Execution step that will create all the needed users as calculated | |
164 | * by @restore_process_included_users (those having newiteind = 0) | |
165 | */ | |
166 | class restore_create_included_users extends restore_execution_step { | |
167 | ||
168 | protected function define_execution() { | |
169 | ||
170 | restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files')); | |
171 | } | |
172 | } | |
173 | ||
174 | /** | |
175 | * Structure step that will create all the needed groups and groupings | |
176 | * by loading them from the groups.xml file performing the required matches. | |
177 | * Note group members only will be added if restoring user info | |
178 | */ | |
179 | class restore_groups_structure_step extends restore_structure_step { | |
180 | ||
c0440b3f EL |
181 | protected function define_structure() { |
182 | ||
183 | $paths = array(); // Add paths here | |
184 | ||
185 | $paths[] = new restore_path_element('group', '/groups/group'); | |
186 | if ($this->get_setting_value('users')) { | |
187 | $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member'); | |
188 | } | |
189 | $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping'); | |
190 | $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group'); | |
191 | ||
192 | return $paths; | |
193 | } | |
194 | ||
195 | // Processing functions go here | |
196 | public function process_group($data) { | |
197 | global $DB; | |
198 | ||
199 | $data = (object)$data; // handy | |
200 | $data->courseid = $this->get_courseid(); | |
201 | ||
202 | $oldid = $data->id; // need this saved for later | |
76cfb124 | 203 | |
c0440b3f EL |
204 | $restorefiles = false; // Only if we end creating the group |
205 | ||
206 | // Search if the group already exists (by name & description) in the target course | |
207 | $description_clause = ''; | |
208 | $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name); | |
209 | if (!empty($data->description)) { | |
210 | $description_clause = ' AND ' . | |
211 | $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc'); | |
212 | $params['desc'] = $data->description; | |
213 | } | |
214 | if (!$groupdb = $DB->get_record_sql("SELECT * | |
215 | FROM {groups} | |
216 | WHERE courseid = :courseid | |
217 | AND name = :grname $description_clause", $params)) { | |
218 | // group doesn't exist, create | |
219 | $newitemid = $DB->insert_record('groups', $data); | |
220 | $restorefiles = true; // We'll restore the files | |
221 | } else { | |
222 | // group exists, use it | |
223 | $newitemid = $groupdb->id; | |
224 | } | |
225 | // Save the id mapping | |
226 | $this->set_mapping('group', $oldid, $newitemid, $restorefiles); | |
227 | } | |
228 | ||
229 | public function process_member($data) { | |
230 | global $DB; | |
231 | ||
232 | $data = (object)$data; // handy | |
233 | ||
234 | // get parent group->id | |
235 | $data->groupid = $this->get_new_parentid('group'); | |
236 | ||
237 | // map user newitemid and insert if not member already | |
238 | if ($data->userid = $this->get_mappingid('user', $data->userid)) { | |
239 | if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) { | |
240 | $DB->insert_record('groups_members', $data); | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | public function process_grouping($data) { | |
246 | debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER); | |
247 | } | |
248 | ||
249 | public function process_grouping_group($data) { | |
250 | debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER); | |
251 | } | |
252 | ||
253 | protected function after_execute() { | |
254 | // Add group related files, matching with "group" mappings | |
255 | $this->add_related_files('group', 'icon', 'group'); | |
256 | $this->add_related_files('group', 'description', 'group'); | |
257 | } | |
258 | ||
259 | } | |
260 | ||
261 | /** | |
262 | * Structure step that will create all the needed scales | |
263 | * by loading them from the scales.xml | |
264 | * Note group members only will be added if restoring user info | |
265 | */ | |
266 | class restore_scales_structure_step extends restore_structure_step { | |
267 | ||
268 | protected function define_structure() { | |
269 | ||
270 | $paths = array(); // Add paths here | |
271 | $paths[] = new restore_path_element('scale', '/scales_definition/scale'); | |
272 | return $paths; | |
273 | } | |
274 | ||
275 | protected function process_scale($data) { | |
276 | global $DB; | |
277 | ||
278 | $data = (object)$data; | |
279 | ||
280 | $restorefiles = false; // Only if we end creating the group | |
281 | ||
282 | $oldid = $data->id; // need this saved for later | |
283 | ||
284 | // Look for scale (by 'scale' both in standard (course=0) and current course | |
285 | // with priority to standard scales (ORDER clause) | |
286 | // scale is not course unique, use get_record_sql to suppress warning | |
287 | // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides | |
288 | $compare_scale_clause = $DB->sql_compare_text('scale') . ' = ' . $DB->sql_compare_text(':scaledesc'); | |
289 | $params = array('courseid' => $this->get_courseid(), 'scaledesc' => $data->scale); | |
290 | if (!$scadb = $DB->get_record_sql("SELECT * | |
291 | FROM {scale} | |
292 | WHERE courseid IN (0, :courseid) | |
293 | AND $compare_scale_clause | |
294 | ORDER BY courseid", $params, IGNORE_MULTIPLE)) { | |
295 | // Remap the user if possible, defaut to user performing the restore if not | |
296 | $userid = $this->get_mappingid('user', $data->userid); | |
297 | $data->userid = $userid ? $userid : $this->get_userid(); | |
298 | // Remap the course if course scale | |
299 | $data->courseid = $data->courseid ? $this->get_courseid() : 0; | |
300 | // If global scale (course=0), check the user has perms to create it | |
301 | // falling to course scale if not | |
302 | $systemctx = get_context_instance(CONTEXT_SYSTEM); | |
303 | if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $data->userid)) { | |
304 | $data->courseid = $this->get_courseid(); | |
305 | } | |
306 | // scale doesn't exist, create | |
307 | $newitemid = $DB->insert_record('scale', $data); | |
308 | $restorefiles = true; // We'll restore the files | |
309 | } else { | |
310 | // scale exists, use it | |
311 | $newitemid = $scadb->id; | |
312 | } | |
313 | // Save the id mapping (with files support at system context) | |
314 | $this->set_mapping('scale', $oldid, $newitemid, $restorefiles, $this->task->get_old_system_contextid()); | |
315 | } | |
316 | ||
317 | protected function after_execute() { | |
318 | // Add scales related files, matching with "scale" mappings | |
319 | $this->add_related_files('grade', 'scale', 'scale', $this->task->get_old_system_contextid()); | |
320 | } | |
76cfb124 EL |
321 | } |
322 | ||
c0440b3f | 323 | |
482aac65 EL |
324 | /* |
325 | * Structure step that will read the course.xml file, loading it and performing | |
326 | * various actions depending of the site/restore settings | |
327 | */ | |
328 | class restore_course_structure_step extends restore_structure_step { | |
329 | ||
330 | protected function define_structure() { | |
331 | ||
332 | $course = new restore_path_element('course', '/course', true); // Grouped | |
333 | $category = new restore_path_element('category', '/course/category'); | |
334 | $tag = new restore_path_element('tag', '/course/tags/tag'); | |
335 | $allowed = new restore_path_element('allowed', '/course/allowed_modules/module'); | |
336 | ||
337 | return array($course, $category, $tag, $allowed); | |
338 | } | |
339 | ||
340 | // Processing functions go here | |
341 | public function process_course($data) { | |
342 | print_object('stopped before processing course. Continue here'); | |
343 | } | |
344 | ||
345 | } |