Merge branch 'MDL-46079-master' of https://github.com/sammarshallou/moodle
[moodle.git] / backup / util / dbops / restore_dbops.class.php
CommitLineData
fbc2778d
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-dbops
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 * Base abstract class for all the helper classes providing DB operations
27 *
28 * TODO: Finish phpdocs
29 */
482aac65 30abstract class restore_dbops {
e7b24943
RT
31 /**
32 * Keep cache of backup records.
33 * @var array
34 * @todo MDL-25290 static should be replaced with MUC code.
35 */
36 private static $backupidscache = array();
37 /**
38 * Keep track of backup ids which are cached.
39 * @var array
40 * @todo MDL-25290 static should be replaced with MUC code.
41 */
42 private static $backupidsexist = array();
43 /**
44 * Count is expensive, so manually keeping track of
45 * backupidscache, to avoid memory issues.
46 * @var int
47 * @todo MDL-25290 static should be replaced with MUC code.
48 */
49 private static $backupidscachesize = 2048;
50 /**
51 * Count is expensive, so manually keeping track of
52 * backupidsexist, to avoid memory issues.
53 * @var int
54 * @todo MDL-25290 static should be replaced with MUC code.
55 */
56 private static $backupidsexistsize = 10240;
57 /**
58 * Slice backupids cache to add more data.
59 * @var int
60 * @todo MDL-25290 static should be replaced with MUC code.
61 */
62 private static $backupidsslice = 512;
482aac65
EL
63
64 /**
648a575e
EL
65 * Return one array containing all the tasks that have been included
66 * in the restore process. Note that these tasks aren't built (they
67 * haven't steps nor ids data available)
482aac65 68 */
648a575e 69 public static function get_included_tasks($restoreid) {
482aac65
EL
70 $rc = restore_controller_dbops::load_controller($restoreid);
71 $tasks = $rc->get_plan()->get_tasks();
648a575e
EL
72 $includedtasks = array();
73 foreach ($tasks as $key => $task) {
482aac65
EL
74 // Calculate if the task is being included
75 $included = false;
76 // blocks, based in blocks setting and parent activity/course
77 if ($task instanceof restore_block_task) {
4a15bb76 78 if (!$task->get_setting_value('blocks')) { // Blocks not included, continue
482aac65
EL
79 continue;
80 }
81 $parent = basename(dirname(dirname($task->get_taskbasepath())));
82 if ($parent == 'course') { // Parent is course, always included if present
83 $included = true;
84
85 } else { // Look for activity_included setting
86 $included = $task->get_setting_value($parent . '_included');
87 }
88
89 // ativities, based on included setting
90 } else if ($task instanceof restore_activity_task) {
91 $included = $task->get_setting_value('included');
92
93 // sections, based on included setting
94 } else if ($task instanceof restore_section_task) {
95 $included = $task->get_setting_value('included');
96
97 // course always included if present
98 } else if ($task instanceof restore_course_task) {
99 $included = true;
100 }
101
648a575e 102 // If included, add it
482aac65 103 if ($included) {
648a575e 104 $includedtasks[] = $task;
482aac65
EL
105 }
106 }
648a575e 107 return $includedtasks;
482aac65
EL
108 }
109
110 /**
111 * Load one inforef.xml file to backup_ids table for future reference
4fb31ae6 112 *
113 * @param string $restoreid Restore id
114 * @param string $inforeffile File path
809fdb83 115 * @param \core\progress\base $progress Progress tracker
482aac65 116 */
4fb31ae6 117 public static function load_inforef_to_tempids($restoreid, $inforeffile,
809fdb83 118 \core\progress\base $progress = null) {
482aac65
EL
119
120 if (!file_exists($inforeffile)) { // Shouldn't happen ever, but...
121 throw new backup_helper_exception('missing_inforef_xml_file', $inforeffile);
122 }
4fb31ae6 123
124 // Set up progress tracking (indeterminate).
125 if (!$progress) {
809fdb83 126 $progress = new \core\progress\null();
4fb31ae6 127 }
128 $progress->start_progress('Loading inforef.xml file');
129
482aac65
EL
130 // Let's parse, custom processor will do its work, sending info to DB
131 $xmlparser = new progressive_parser();
132 $xmlparser->set_file($inforeffile);
133 $xmlprocessor = new restore_inforef_parser_processor($restoreid);
134 $xmlparser->set_processor($xmlprocessor);
4fb31ae6 135 $xmlparser->set_progress($progress);
482aac65 136 $xmlparser->process();
4fb31ae6 137
138 // Finish progress
139 $progress->end_progress();
482aac65
EL
140 }
141
71a50b13
EL
142 /**
143 * Load the needed role.xml file to backup_ids table for future reference
144 */
145 public static function load_roles_to_tempids($restoreid, $rolesfile) {
146
147 if (!file_exists($rolesfile)) { // Shouldn't happen ever, but...
148 throw new backup_helper_exception('missing_roles_xml_file', $rolesfile);
149 }
150 // Let's parse, custom processor will do its work, sending info to DB
151 $xmlparser = new progressive_parser();
152 $xmlparser->set_file($rolesfile);
153 $xmlprocessor = new restore_roles_parser_processor($restoreid);
154 $xmlparser->set_processor($xmlprocessor);
155 $xmlparser->process();
156 }
157
158 /**
159 * Precheck the loaded roles, return empty array if everything is ok, and
160 * array with 'errors', 'warnings' elements (suitable to be used by restore_prechecks)
8d4e41f4
EL
161 * with any problem found. At the same time, store all the mapping into backup_ids_temp
162 * and also put the information into $rolemappings (controller->info), so it can be reworked later by
163 * post-precheck stages while at the same time accept modified info in the same object coming from UI
71a50b13 164 */
8d4e41f4
EL
165 public static function precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings) {
166 global $DB;
167
168 $problems = array(); // To store warnings/errors
169
170 // Get loaded roles from backup_ids
3e7e2ab2 171 $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid, info');
8d4e41f4
EL
172 foreach ($rs as $recrole) {
173 // If the rolemappings->modified flag is set, that means that we are coming from
174 // manually modified mappings (by UI), so accept those mappings an put them to backup_ids
175 if ($rolemappings->modified) {
176 $target = $rolemappings->mappings[$recrole->itemid]->targetroleid;
177 self::set_backup_ids_record($restoreid, 'role', $recrole->itemid, $target);
178
179 // Else, we haven't any info coming from UI, let's calculate the mappings, matching
180 // in multiple ways and checking permissions. Note mapping to 0 means "skip"
181 } else {
3e7e2ab2 182 $role = (object)backup_controller_dbops::decode_backup_temp_info($recrole->info);
8d4e41f4
EL
183 $match = self::get_best_assignable_role($role, $courseid, $userid, $samesite);
184 // Send match to backup_ids
185 self::set_backup_ids_record($restoreid, 'role', $recrole->itemid, $match);
186 // Build the rolemappings element for controller
187 unset($role->id);
188 unset($role->nameincourse);
8d4e41f4
EL
189 $role->targetroleid = $match;
190 $rolemappings->mappings[$recrole->itemid] = $role;
191 // Prepare warning if no match found
192 if (!$match) {
193 $problems['warnings'][] = get_string('cannotfindassignablerole', 'backup', $role->name);
194 }
195 }
196 }
197 $rs->close();
198 return $problems;
199 }
200
e7b24943
RT
201 /**
202 * Return cached backup id's
203 *
204 * @param int $restoreid id of backup
205 * @param string $itemname name of the item
206 * @param int $itemid id of item
207 * @return array backup id's
208 * @todo MDL-25290 replace static backupids* with MUC code
209 */
7f98b12f
TL
210 protected static function get_backup_ids_cached($restoreid, $itemname, $itemid) {
211 global $DB;
212
213 $key = "$itemid $itemname $restoreid";
214
e7b24943
RT
215 // If record exists in cache then return.
216 if (isset(self::$backupidsexist[$key]) && isset(self::$backupidscache[$key])) {
217 // Return a copy of cached data, to avoid any alterations in cached data.
218 return clone self::$backupidscache[$key];
7f98b12f
TL
219 }
220
e7b24943
RT
221 // Clean cache, if it's full.
222 if (self::$backupidscachesize <= 0) {
223 // Remove some records, to keep memory in limit.
224 self::$backupidscache = array_slice(self::$backupidscache, self::$backupidsslice, null, true);
225 self::$backupidscachesize = self::$backupidscachesize + self::$backupidsslice;
226 }
227 if (self::$backupidsexistsize <= 0) {
228 self::$backupidsexist = array_slice(self::$backupidsexist, self::$backupidsslice, null, true);
229 self::$backupidsexistsize = self::$backupidsexistsize + self::$backupidsslice;
7f98b12f
TL
230 }
231
e7b24943 232 // Retrive record from database.
7f98b12f
TL
233 $record = array(
234 'backupid' => $restoreid,
235 'itemname' => $itemname,
236 'itemid' => $itemid
237 );
e7b24943
RT
238 if ($dbrec = $DB->get_record('backup_ids_temp', $record)) {
239 self::$backupidsexist[$key] = $dbrec->id;
240 self::$backupidscache[$key] = $dbrec;
241 self::$backupidscachesize--;
242 self::$backupidsexistsize--;
243 return $dbrec;
244 } else {
245 return false;
7f98b12f 246 }
7f98b12f
TL
247 }
248
e7b24943
RT
249 /**
250 * Cache backup ids'
251 *
252 * @param int $restoreid id of backup
253 * @param string $itemname name of the item
254 * @param int $itemid id of item
255 * @param array $extrarecord extra record which needs to be updated
256 * @return void
257 * @todo MDL-25290 replace static BACKUP_IDS_* with MUC code
258 */
7f98b12f
TL
259 protected static function set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord) {
260 global $DB;
261
262 $key = "$itemid $itemname $restoreid";
263
e7b24943
RT
264 $record = array(
265 'backupid' => $restoreid,
266 'itemname' => $itemname,
267 'itemid' => $itemid,
268 );
269
270 // If record is not cached then add one.
271 if (!isset(self::$backupidsexist[$key])) {
272 // If we have this record in db, then just update this.
273 if ($existingrecord = $DB->get_record('backup_ids_temp', $record)) {
274 self::$backupidsexist[$key] = $existingrecord->id;
275 self::$backupidsexistsize--;
276 self::update_backup_cached_record($record, $extrarecord, $key, $existingrecord);
277 } else {
278 // Add new record to cache and db.
279 $recorddefault = array (
280 'newitemid' => 0,
281 'parentitemid' => null,
282 'info' => null);
283 $record = array_merge($record, $recorddefault, $extrarecord);
284 $record['id'] = $DB->insert_record('backup_ids_temp', $record);
285 self::$backupidsexist[$key] = $record['id'];
286 self::$backupidsexistsize--;
287 if (self::$backupidscachesize > 0) {
288 // Cache new records if we haven't got many yet.
289 self::$backupidscache[$key] = (object) $record;
290 self::$backupidscachesize--;
291 }
7f98b12f
TL
292 }
293 } else {
e7b24943
RT
294 self::update_backup_cached_record($record, $extrarecord, $key);
295 }
296 }
297
298 /**
299 * Updates existing backup record
300 *
301 * @param array $record record which needs to be updated
302 * @param array $extrarecord extra record which needs to be updated
303 * @param string $key unique key which is used to identify cached record
304 * @param stdClass $existingrecord (optional) existing record
305 */
306 protected static function update_backup_cached_record($record, $extrarecord, $key, $existingrecord = null) {
307 global $DB;
308 // Update only if extrarecord is not empty.
309 if (!empty($extrarecord)) {
310 $extrarecord['id'] = self::$backupidsexist[$key];
311 $DB->update_record('backup_ids_temp', $extrarecord);
312 // Update existing cache or add new record to cache.
313 if (isset(self::$backupidscache[$key])) {
314 $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
315 self::$backupidscache[$key] = (object) $record;
316 } else if (self::$backupidscachesize > 0) {
317 if ($existingrecord) {
318 self::$backupidscache[$key] = $existingrecord;
319 } else {
320 // Retrive record from database and cache updated records.
321 self::$backupidscache[$key] = $DB->get_record('backup_ids_temp', $record);
7f98b12f 322 }
e7b24943
RT
323 $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
324 self::$backupidscache[$key] = (object) $record;
325 self::$backupidscachesize--;
7f98b12f
TL
326 }
327 }
328 }
329
f0852f47
EL
330 /**
331 * Reset the ids caches completely
332 *
333 * Any destructive operation (partial delete, truncate, drop or recreate) performed
334 * with the backup_ids table must cause the backup_ids caches to be
335 * invalidated by calling this method. See MDL-33630.
336 *
337 * Note that right now, the only operation of that type is the recreation
338 * (drop & restore) of the table that may happen once the prechecks have ended. All
339 * the rest of operations are always routed via {@link set_backup_ids_record()}, 1 by 1,
340 * keeping the caches on sync.
341 *
342 * @todo MDL-25290 static should be replaced with MUC code.
343 */
344 public static function reset_backup_ids_cached() {
345 // Reset the ids cache.
346 $cachetoadd = count(self::$backupidscache);
347 self::$backupidscache = array();
348 self::$backupidscachesize = self::$backupidscachesize + $cachetoadd;
349 // Reset the exists cache.
350 $existstoadd = count(self::$backupidsexist);
351 self::$backupidsexist = array();
352 self::$backupidsexistsize = self::$backupidsexistsize + $existstoadd;
353 }
354
8d4e41f4
EL
355 /**
356 * Given one role, as loaded from XML, perform the best possible matching against the assignable
357 * roles, using different fallback alternatives (shortname, archetype, editingteacher => teacher, defaultcourseroleid)
358 * returning the id of the best matching role or 0 if no match is found
359 */
360 protected static function get_best_assignable_role($role, $courseid, $userid, $samesite) {
361 global $CFG, $DB;
362
363 // Gather various information about roles
a689cd1d 364 $coursectx = context_course::instance($courseid);
8d4e41f4
EL
365 $assignablerolesshortname = get_assignable_roles($coursectx, ROLENAME_SHORT, false, $userid);
366
367 // Note: under 1.9 we had one function restore_samerole() that performed one complete
368 // matching of roles (all caps) and if match was found the mapping was availabe bypassing
369 // any assignable_roles() security. IMO that was wrong and we must not allow such
370 // mappings anymore. So we have left that matching strategy out in 2.0
371
372 // Empty assignable roles, mean no match possible
373 if (empty($assignablerolesshortname)) {
374 return 0;
375 }
376
377 // Match by shortname
378 if ($match = array_search($role->shortname, $assignablerolesshortname)) {
379 return $match;
380 }
381
382 // Match by archetype
383 list($in_sql, $in_params) = $DB->get_in_or_equal(array_keys($assignablerolesshortname));
384 $params = array_merge(array($role->archetype), $in_params);
385 if ($rec = $DB->get_record_select('role', "archetype = ? AND id $in_sql", $params, 'id', IGNORE_MULTIPLE)) {
386 return $rec->id;
387 }
388
389 // Match editingteacher to teacher (happens a lot, from 1.9)
390 if ($role->shortname == 'editingteacher' && in_array('teacher', $assignablerolesshortname)) {
391 return array_search('teacher', $assignablerolesshortname);
392 }
393
394 // No match, return 0
395 return 0;
71a50b13
EL
396 }
397
8d4e41f4 398
71a50b13
EL
399 /**
400 * Process the loaded roles, looking for their best mapping or skipping
401 * Any error will cause exception. Note this is one wrapper over
402 * precheck_included_roles, that contains all the logic, but returns
403 * errors/warnings instead and is executed as part of the restore prechecks
404 */
8d4e41f4 405 public static function process_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings) {
71a50b13
EL
406 global $DB;
407
408 // Just let precheck_included_roles() to do all the hard work
8d4e41f4 409 $problems = self::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings);
71a50b13
EL
410
411 // With problems of type error, throw exception, shouldn't happen if prechecks executed
412 if (array_key_exists('errors', $problems)) {
413 throw new restore_dbops_exception('restore_problems_processing_roles', null, implode(', ', $problems['errors']));
414 }
415 }
416
482aac65
EL
417 /**
418 * Load the needed users.xml file to backup_ids table for future reference
4fb31ae6 419 *
420 * @param string $restoreid Restore id
421 * @param string $usersfile File path
809fdb83 422 * @param \core\progress\base $progress Progress tracker
482aac65 423 */
4fb31ae6 424 public static function load_users_to_tempids($restoreid, $usersfile,
809fdb83 425 \core\progress\base $progress = null) {
482aac65
EL
426
427 if (!file_exists($usersfile)) { // Shouldn't happen ever, but...
2df0f295 428 throw new backup_helper_exception('missing_users_xml_file', $usersfile);
482aac65 429 }
4fb31ae6 430
431 // Set up progress tracking (indeterminate).
432 if (!$progress) {
809fdb83 433 $progress = new \core\progress\null();
4fb31ae6 434 }
435 $progress->start_progress('Loading users into temporary table');
436
482aac65
EL
437 // Let's parse, custom processor will do its work, sending info to DB
438 $xmlparser = new progressive_parser();
439 $xmlparser->set_file($usersfile);
440 $xmlprocessor = new restore_users_parser_processor($restoreid);
441 $xmlparser->set_processor($xmlprocessor);
4fb31ae6 442 $xmlparser->set_progress($progress);
482aac65 443 $xmlparser->process();
4fb31ae6 444
445 // Finish progress.
446 $progress->end_progress();
482aac65
EL
447 }
448
41941110
EL
449 /**
450 * Load the needed questions.xml file to backup_ids table for future reference
451 */
452 public static function load_categories_and_questions_to_tempids($restoreid, $questionsfile) {
453
454 if (!file_exists($questionsfile)) { // Shouldn't happen ever, but...
455 throw new backup_helper_exception('missing_questions_xml_file', $questionsfile);
456 }
457 // Let's parse, custom processor will do its work, sending info to DB
458 $xmlparser = new progressive_parser();
459 $xmlparser->set_file($questionsfile);
460 $xmlprocessor = new restore_questions_parser_processor($restoreid);
461 $xmlparser->set_processor($xmlprocessor);
462 $xmlparser->process();
463 }
464
465 /**
466 * Check all the included categories and questions, deciding the action to perform
467 * for each one (mapping / creation) and returning one array of problems in case
468 * something is wrong.
469 *
470 * There are some basic rules that the method below will always try to enforce:
471 *
472 * Rule1: Targets will be, always, calculated for *whole* question banks (a.k.a. contexid source),
473 * so, given 2 question categories belonging to the same bank, their target bank will be
474 * always the same. If not, we can be incurring into "fragmentation", leading to random/cloze
475 * problems (qtypes having "child" questions).
476 *
477 * Rule2: The 'moodle/question:managecategory' and 'moodle/question:add' capabilities will be
478 * checked before creating any category/question respectively and, if the cap is not allowed
479 * into upper contexts (system, coursecat)) but in lower ones (course), the *whole* question bank
480 * will be created there.
481 *
482 * Rule3: Coursecat question banks not existing in the target site will be created as course
483 * (lower ctx) question banks, never as "guessed" coursecat question banks base on depth or so.
484 *
485 * Rule4: System question banks will be created at system context if user has perms to do so. Else they
486 * will created as course (lower ctx) question banks (similary to rule3). In other words, course ctx
487 * if always a fallback for system and coursecat question banks.
488 *
489 * Also, there are some notes to clarify the scope of this method:
490 *
491 * Note1: This method won't create any question category nor question at all. It simply will calculate
492 * which actions (create/map) must be performed for each element and where, validating that all those
493 * actions are doable by the user executing the restore operation. Any problem found will be
494 * returned in the problems array, causing the restore process to stop with error.
495 *
496 * Note2: To decide if one question bank (all its question categories and questions) is going to be remapped,
497 * then all the categories and questions must exist in the same target bank. If able to do so, missing
498 * qcats and qs will be created (rule2). But if, at the end, something is missing, the whole question bank
499 * will be recreated at course ctx (rule1), no matter if that duplicates some categories/questions.
500 *
501 * Note3: We'll be using the newitemid column in the temp_ids table to store the action to be performed
502 * with each question category and question. newitemid = 0 means the qcat/q needs to be created and
503 * any other value means the qcat/q is mapped. Also, for qcats, parentitemid will contain the target
504 * context where the categories have to be created (but for module contexts where we'll keep the old
505 * one until the activity is created)
506 *
507 * Note4: All these "actions" will be "executed" later by {@link restore_create_categories_and_questions}
508 */
509 public static function precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite) {
510
511 $problems = array();
512
513 // TODO: Check all qs, looking their qtypes are restorable
514
515 // Precheck all qcats and qs looking for target contexts / warnings / errors
516 list($syserr, $syswarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_SYSTEM);
517 list($caterr, $catwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_COURSECAT);
518 list($couerr, $couwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_COURSE);
519 list($moderr, $modwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_MODULE);
520
521 // Acummulate and handle errors and warnings
522 $errors = array_merge($syserr, $caterr, $couerr, $moderr);
523 $warnings = array_merge($syswarn, $catwarn, $couwarn, $modwarn);
524 if (!empty($errors)) {
525 $problems['errors'] = $errors;
526 }
527 if (!empty($warnings)) {
528 $problems['warnings'] = $warnings;
529 }
530 return $problems;
531 }
532
533 /**
534 * This function will process all the question banks present in restore
535 * at some contextlevel (from CONTEXT_SYSTEM to CONTEXT_MODULE), finding
536 * the target contexts where each bank will be restored and returning
537 * warnings/errors as needed.
538 *
539 * Some contextlevels (system, coursecat), will delegate process to
540 * course level if any problem is found (lack of permissions, non-matching
541 * target context...). Other contextlevels (course, module) will
542 * cause return error if some problem is found.
543 *
544 * At the end, if no errors were found, all the categories in backup_temp_ids
545 * will be pointing (parentitemid) to the target context where they must be
546 * created later in the restore process.
547 *
548 * Note: at the time these prechecks are executed, activities haven't been
549 * created yet so, for CONTEXT_MODULE banks, we keep the old contextid
550 * in the parentitemid field. Once the activity (and its context) has been
551 * created, we'll update that context in the required qcats
552 *
553 * Caller {@link precheck_categories_and_questions} will, simply, execute
554 * this function for all the contextlevels, acting as a simple controller
555 * of warnings and errors.
556 *
557 * The function returns 2 arrays, one containing errors and another containing
558 * warnings. Both empty if no errors/warnings are found.
559 */
560 public static function prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, $contextlevel) {
561 global $CFG, $DB;
562
563 // To return any errors and warnings found
564 $errors = array();
565 $warnings = array();
566
567 // Specify which fallbacks must be performed
568 $fallbacks = array(
569 CONTEXT_SYSTEM => CONTEXT_COURSE,
570 CONTEXT_COURSECAT => CONTEXT_COURSE);
571
572 // For any contextlevel, follow this process logic:
573 //
574 // 0) Iterate over each context (qbank)
575 // 1) Iterate over each qcat in the context, matching by stamp for the found target context
576 // 2a) No match, check if user can create qcat and q
577 // 3a) User can, mark the qcat and all dependent qs to be created in that target context
578 // 3b) User cannot, check if we are in some contextlevel with fallback
579 // 4a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
580 // 4b) No fallback, error. End qcat loop.
581 // 2b) Match, mark qcat to be mapped and iterate over each q, matching by stamp and version
582 // 5a) No match, check if user can add q
583 // 6a) User can, mark the q to be created
584 // 6b) User cannot, check if we are in some contextlevel with fallback
585 // 7a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
586 // 7b) No fallback, error. End qcat loop
587 // 5b) Match, mark q to be mapped
588
589 // Get all the contexts (question banks) in restore for the given contextlevel
590 $contexts = self::restore_get_question_banks($restoreid, $contextlevel);
591
592 // 0) Iterate over each context (qbank)
593 foreach ($contexts as $contextid => $contextlevel) {
594 // Init some perms
595 $canmanagecategory = false;
596 $canadd = false;
597 // get categories in context (bank)
598 $categories = self::restore_get_question_categories($restoreid, $contextid);
599 // cache permissions if $targetcontext is found
600 if ($targetcontext = self::restore_find_best_target_context($categories, $courseid, $contextlevel)) {
601 $canmanagecategory = has_capability('moodle/question:managecategory', $targetcontext, $userid);
602 $canadd = has_capability('moodle/question:add', $targetcontext, $userid);
603 }
604 // 1) Iterate over each qcat in the context, matching by stamp for the found target context
605 foreach ($categories as $category) {
606 $matchcat = false;
607 if ($targetcontext) {
608 $matchcat = $DB->get_record('question_categories', array(
609 'contextid' => $targetcontext->id,
610 'stamp' => $category->stamp));
611 }
612 // 2a) No match, check if user can create qcat and q
613 if (!$matchcat) {
614 // 3a) User can, mark the qcat and all dependent qs to be created in that target context
615 if ($canmanagecategory && $canadd) {
616 // Set parentitemid to targetcontext, BUT for CONTEXT_MODULE categories, where
617 // we keep the source contextid unmodified (for easier matching later when the
618 // activities are created)
619 $parentitemid = $targetcontext->id;
620 if ($contextlevel == CONTEXT_MODULE) {
621 $parentitemid = null; // null means "not modify" a.k.a. leave original contextid
622 }
623 self::set_backup_ids_record($restoreid, 'question_category', $category->id, 0, $parentitemid);
624 // Nothing else to mark, newitemid = 0 means create
625
626 // 3b) User cannot, check if we are in some contextlevel with fallback
627 } else {
628 // 4a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
629 if (array_key_exists($contextlevel, $fallbacks)) {
630 foreach ($categories as $movedcat) {
631 $movedcat->contextlevel = $fallbacks[$contextlevel];
632 self::set_backup_ids_record($restoreid, 'question_category', $movedcat->id, 0, $contextid, $movedcat);
633 // Warn about the performed fallback
634 $warnings[] = get_string('qcategory2coursefallback', 'backup', $movedcat);
635 }
636
637 // 4b) No fallback, error. End qcat loop.
638 } else {
639 $errors[] = get_string('qcategorycannotberestored', 'backup', $category);
640 }
641 break; // out from qcat loop (both 4a and 4b), we have decided about ALL categories in context (bank)
642 }
643
644 // 2b) Match, mark qcat to be mapped and iterate over each q, matching by stamp and version
645 } else {
646 self::set_backup_ids_record($restoreid, 'question_category', $category->id, $matchcat->id, $targetcontext->id);
647 $questions = self::restore_get_questions($restoreid, $category->id);
42574d09
RS
648
649 // Collect all the questions for this category into memory so we only talk to the DB once.
650 $questioncache = $DB->get_records_sql_menu("SELECT ".$DB->sql_concat('stamp', "' '", 'version').", id
651 FROM {question}
652 WHERE category = ?", array($matchcat->id));
653
41941110 654 foreach ($questions as $question) {
42574d09
RS
655 if (isset($questioncache[$question->stamp." ".$question->version])) {
656 $matchqid = $questioncache[$question->stamp." ".$question->version];
657 } else {
658 $matchqid = false;
659 }
41941110 660 // 5a) No match, check if user can add q
42574d09 661 if (!$matchqid) {
41941110
EL
662 // 6a) User can, mark the q to be created
663 if ($canadd) {
664 // Nothing to mark, newitemid means create
665
666 // 6b) User cannot, check if we are in some contextlevel with fallback
667 } else {
668 // 7a) There is fallback, move ALL the qcats to fallback, warn. End qcat loo
669 if (array_key_exists($contextlevel, $fallbacks)) {
670 foreach ($categories as $movedcat) {
671 $movedcat->contextlevel = $fallbacks[$contextlevel];
672 self::set_backup_ids_record($restoreid, 'question_category', $movedcat->id, 0, $contextid, $movedcat);
673 // Warn about the performed fallback
674 $warnings[] = get_string('question2coursefallback', 'backup', $movedcat);
675 }
676
677 // 7b) No fallback, error. End qcat loop
678 } else {
679 $errors[] = get_string('questioncannotberestored', 'backup', $question);
680 }
681 break 2; // out from qcat loop (both 7a and 7b), we have decided about ALL categories in context (bank)
682 }
683
684 // 5b) Match, mark q to be mapped
685 } else {
42574d09 686 self::set_backup_ids_record($restoreid, 'question', $question->id, $matchqid);
41941110
EL
687 }
688 }
689 }
690 }
691 }
692
693 return array($errors, $warnings);
694 }
695
696 /**
697 * Return one array of contextid => contextlevel pairs
698 * of question banks to be checked for one given restore operation
699 * ordered from CONTEXT_SYSTEM downto CONTEXT_MODULE
700 * If contextlevel is specified, then only banks corresponding to
701 * that level are returned
702 */
703 public static function restore_get_question_banks($restoreid, $contextlevel = null) {
704 global $DB;
705
706 $results = array();
3e7e2ab2 707 $qcats = $DB->get_recordset_sql("SELECT itemid, parentitemid AS contextid, info
41941110
EL
708 FROM {backup_ids_temp}
709 WHERE backupid = ?
710 AND itemname = 'question_category'", array($restoreid));
711 foreach ($qcats as $qcat) {
712 // If this qcat context haven't been acummulated yet, do that
713 if (!isset($results[$qcat->contextid])) {
3e7e2ab2 714 $info = backup_controller_dbops::decode_backup_temp_info($qcat->info);
41941110 715 // Filter by contextlevel if necessary
3e7e2ab2
RS
716 if (is_null($contextlevel) || $contextlevel == $info->contextlevel) {
717 $results[$qcat->contextid] = $info->contextlevel;
41941110
EL
718 }
719 }
720 }
3e7e2ab2 721 $qcats->close();
41941110
EL
722 // Sort by value (contextlevel from CONTEXT_SYSTEM downto CONTEXT_MODULE)
723 asort($results);
724 return $results;
725 }
726
727 /**
728 * Return one array of question_category records for
729 * a given restore operation and one restore context (question bank)
730 */
731 public static function restore_get_question_categories($restoreid, $contextid) {
732 global $DB;
733
734 $results = array();
3e7e2ab2 735 $qcats = $DB->get_recordset_sql("SELECT itemid, info
41941110
EL
736 FROM {backup_ids_temp}
737 WHERE backupid = ?
738 AND itemname = 'question_category'
739 AND parentitemid = ?", array($restoreid, $contextid));
740 foreach ($qcats as $qcat) {
3e7e2ab2 741 $results[$qcat->itemid] = backup_controller_dbops::decode_backup_temp_info($qcat->info);
41941110 742 }
3e7e2ab2
RS
743 $qcats->close();
744
41941110
EL
745 return $results;
746 }
747
748 /**
749 * Calculates the best context found to restore one collection of qcats,
750 * al them belonging to the same context (question bank), returning the
751 * target context found (object) or false
752 */
753 public static function restore_find_best_target_context($categories, $courseid, $contextlevel) {
754 global $DB;
755
756 $targetcontext = false;
757
758 // Depending of $contextlevel, we perform different actions
759 switch ($contextlevel) {
760 // For system is easy, the best context is the system context
761 case CONTEXT_SYSTEM:
a689cd1d 762 $targetcontext = context_system::instance();
41941110
EL
763 break;
764
765 // For coursecat, we are going to look for stamps in all the
766 // course categories between CONTEXT_SYSTEM and CONTEXT_COURSE
767 // (i.e. in all the course categories in the path)
768 //
769 // And only will return one "best" target context if all the
770 // matches belong to ONE and ONLY ONE context. If multiple
771 // matches are found, that means that there is some annoying
772 // qbank "fragmentation" in the categories, so we'll fallback
773 // to create the qbank at course level
774 case CONTEXT_COURSECAT:
775 // Build the array of stamps we are going to match
776 $stamps = array();
777 foreach ($categories as $category) {
778 $stamps[] = $category->stamp;
779 }
780 $contexts = array();
781 // Build the array of contexts we are going to look
a689cd1d
AG
782 $systemctx = context_system::instance();
783 $coursectx = context_course::instance($courseid);
8e8891b7 784 $parentctxs = $coursectx->get_parent_context_ids();
41941110
EL
785 foreach ($parentctxs as $parentctx) {
786 // Exclude system context
787 if ($parentctx == $systemctx->id) {
788 continue;
789 }
790 $contexts[] = $parentctx;
791 }
792 if (!empty($stamps) && !empty($contexts)) {
793 // Prepare the query
794 list($stamp_sql, $stamp_params) = $DB->get_in_or_equal($stamps);
795 list($context_sql, $context_params) = $DB->get_in_or_equal($contexts);
796 $sql = "SELECT contextid
797 FROM {question_categories}
798 WHERE stamp $stamp_sql
799 AND contextid $context_sql";
800 $params = array_merge($stamp_params, $context_params);
801 $matchingcontexts = $DB->get_records_sql($sql, $params);
802 // Only if ONE and ONLY ONE context is found, use it as valid target
803 if (count($matchingcontexts) == 1) {
a689cd1d 804 $targetcontext = context::instance_by_id(reset($matchingcontexts)->contextid);
41941110
EL
805 }
806 }
807 break;
808
809 // For course is easy, the best context is the course context
810 case CONTEXT_COURSE:
a689cd1d 811 $targetcontext = context_course::instance($courseid);
41941110
EL
812 break;
813
814 // For module is easy, there is not best context, as far as the
815 // activity hasn't been created yet. So we return context course
816 // for them, so permission checks and friends will work. Note this
817 // case is handled by {@link prechek_precheck_qbanks_by_level}
818 // in an special way
819 case CONTEXT_MODULE:
a689cd1d 820 $targetcontext = context_course::instance($courseid);
41941110
EL
821 break;
822 }
823 return $targetcontext;
824 }
825
826 /**
827 * Return one array of question records for
828 * a given restore operation and one question category
829 */
830 public static function restore_get_questions($restoreid, $qcatid) {
831 global $DB;
832
833 $results = array();
3e7e2ab2 834 $qs = $DB->get_recordset_sql("SELECT itemid, info
41941110
EL
835 FROM {backup_ids_temp}
836 WHERE backupid = ?
837 AND itemname = 'question'
838 AND parentitemid = ?", array($restoreid, $qcatid));
839 foreach ($qs as $q) {
3e7e2ab2 840 $results[$q->itemid] = backup_controller_dbops::decode_backup_temp_info($q->info);
41941110 841 }
3e7e2ab2 842 $qs->close();
41941110
EL
843 return $results;
844 }
845
2df0f295
EL
846 /**
847 * Given one component/filearea/context and
848 * optionally one source itemname to match itemids
849 * put the corresponding files in the pool
4b6b087f 850 *
37ff843d 851 * If you specify a progress reporter, it will get called once per file with
852 * indeterminate progress.
853 *
4b6b087f
DM
854 * @param string $basepath the full path to the root of unzipped backup file
855 * @param string $restoreid the restore job's identification
856 * @param string $component
857 * @param string $filearea
858 * @param int $oldcontextid
859 * @param int $dfltuserid default $file->user if the old one can't be mapped
860 * @param string|null $itemname
861 * @param int|null $olditemid
862 * @param int|null $forcenewcontextid explicit value for the new contextid (skip mapping)
863 * @param bool $skipparentitemidctxmatch
809fdb83 864 * @param \core\progress\base $progress Optional progress reporter
3b232aef 865 * @return array of result object
2df0f295 866 */
37ff843d 867 public static function send_files_to_pool($basepath, $restoreid, $component, $filearea,
868 $oldcontextid, $dfltuserid, $itemname = null, $olditemid = null,
869 $forcenewcontextid = null, $skipparentitemidctxmatch = false,
809fdb83 870 \core\progress\base $progress = null) {
b4d33e02 871 global $DB, $CFG;
2df0f295 872
b1850c12
AN
873 $backupinfo = backup_general_helper::get_backup_information(basename($basepath));
874 $includesfiles = $backupinfo->include_files;
875
3b232aef
FM
876 $results = array();
877
41941110
EL
878 if ($forcenewcontextid) {
879 // Some components can have "forced" new contexts (example: questions can end belonging to non-standard context mappings,
880 // with questions originally at system/coursecat context in source being restored to course context in target). So we need
881 // to be able to force the new contextid
882 $newcontextid = $forcenewcontextid;
883 } else {
884 // Get new context, must exist or this will fail
b6cdb4d6 885 $newcontextrecord = self::get_backup_ids_record($restoreid, 'context', $oldcontextid);
886 if (!$newcontextrecord || !$newcontextrecord->newitemid) {
41941110
EL
887 throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);
888 }
b6cdb4d6 889 $newcontextid = $newcontextrecord->newitemid;
41941110
EL
890 }
891
892 // Sometimes it's possible to have not the oldcontextids stored into backup_ids_temp->parentitemid
893 // columns (because we have used them to store other information). This happens usually with
894 // all the question related backup_ids_temp records. In that case, it's safe to ignore that
895 // matching as far as we are always restoring for well known oldcontexts and olditemids
896 $parentitemctxmatchsql = ' AND i.parentitemid = f.contextid ';
897 if ($skipparentitemidctxmatch) {
898 $parentitemctxmatchsql = '';
2df0f295
EL
899 }
900
b212f87e 901 // Important: remember how files have been loaded to backup_files_temp
2df0f295
EL
902 // - info: contains the whole original object (times, names...)
903 // (all them being original ids as loaded from xml)
904
905 // itemname = null, we are going to match only by context, no need to use itemid (all them are 0)
906 if ($itemname == null) {
83b2cc56 907 $sql = "SELECT id AS bftid, contextid, component, filearea, itemid, itemid AS newitemid, info
b8bb45b0 908 FROM {backup_files_temp}
2df0f295 909 WHERE backupid = ?
b8bb45b0
EL
910 AND contextid = ?
911 AND component = ?
83b2cc56 912 AND filearea = ?";
b8bb45b0 913 $params = array($restoreid, $oldcontextid, $component, $filearea);
2df0f295 914
b8bb45b0 915 // itemname not null, going to join with backup_ids to perform the old-new mapping of itemids
2df0f295 916 } else {
83b2cc56 917 $sql = "SELECT f.id AS bftid, f.contextid, f.component, f.filearea, f.itemid, i.newitemid, f.info
b8bb45b0 918 FROM {backup_files_temp} f
2df0f295 919 JOIN {backup_ids_temp} i ON i.backupid = f.backupid
41941110 920 $parentitemctxmatchsql
b8bb45b0 921 AND i.itemid = f.itemid
2df0f295 922 WHERE f.backupid = ?
b8bb45b0
EL
923 AND f.contextid = ?
924 AND f.component = ?
925 AND f.filearea = ?
41941110 926 AND i.itemname = ?";
b8bb45b0 927 $params = array($restoreid, $oldcontextid, $component, $filearea, $itemname);
f2745cbe
EL
928 if ($olditemid !== null) { // Just process ONE olditemid intead of the whole itemname
929 $sql .= ' AND i.itemid = ?';
930 $params[] = $olditemid;
931 }
2df0f295
EL
932 }
933
2df0f295
EL
934 $fs = get_file_storage(); // Get moodle file storage
935 $basepath = $basepath . '/files/';// Get backup file pool base
37ff843d 936 // Report progress before query.
937 if ($progress) {
938 $progress->progress();
939 }
71a50b13 940 $rs = $DB->get_recordset_sql($sql, $params);
2df0f295 941 foreach ($rs as $rec) {
37ff843d 942 // Report progress each time around loop.
943 if ($progress) {
944 $progress->progress();
945 }
946
3e7e2ab2 947 $file = (object)backup_controller_dbops::decode_backup_temp_info($rec->info);
67233725 948
2df0f295
EL
949 // ignore root dirs (they are created automatically)
950 if ($file->filepath == '/' && $file->filename == '.') {
951 continue;
952 }
4b6b087f 953
84cdf7de
EL
954 // set the best possible user
955 $mappeduser = self::get_backup_ids_record($restoreid, 'user', $file->userid);
4b6b087f
DM
956 $mappeduserid = !empty($mappeduser) ? $mappeduser->newitemid : $dfltuserid;
957
958 // dir found (and not root one), let's create it
2df0f295 959 if ($file->filename == '.') {
4b6b087f 960 $fs->create_directory($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $mappeduserid);
2df0f295
EL
961 continue;
962 }
67233725 963
b1850c12
AN
964 // The file record to restore.
965 $file_record = array(
966 'contextid' => $newcontextid,
967 'component' => $component,
968 'filearea' => $filearea,
969 'itemid' => $rec->newitemid,
970 'filepath' => $file->filepath,
971 'filename' => $file->filename,
972 'timecreated' => $file->timecreated,
973 'timemodified'=> $file->timemodified,
974 'userid' => $mappeduserid,
538864df 975 'source' => $file->source,
b1850c12
AN
976 'author' => $file->author,
977 'license' => $file->license,
978 'sortorder' => $file->sortorder
979 );
980
4b6b087f 981 if (empty($file->repositoryid)) {
0daadaae
RT
982 // If contenthash is empty then gracefully skip adding file.
983 if (empty($file->contenthash)) {
984 $result = new stdClass();
985 $result->code = 'file_missing_in_backup';
986 $result->message = sprintf('missing file (%s) contenthash in backup for component %s', $file->filename, $component);
987 $result->level = backup::LOG_WARNING;
988 $results[] = $result;
989 continue;
990 }
4b6b087f
DM
991 // this is a regular file, it must be present in the backup pool
992 $backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);
67233725 993
b1850c12
AN
994 // Some file types do not include the files as they should already be
995 // present. We still need to create entries into the files table.
996 if ($includesfiles) {
997 // The file is not found in the backup.
998 if (!file_exists($backuppath)) {
40e8f026 999 $results[] = self::get_missing_file_result($file);
b1850c12
AN
1000 continue;
1001 }
4b6b087f 1002
b1850c12
AN
1003 // create the file in the filepool if it does not exist yet
1004 if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
b4d33e02 1005
b1850c12
AN
1006 // If no license found, use default.
1007 if ($file->license == null){
1008 $file->license = $CFG->sitedefaultlicense;
1009 }
b4d33e02 1010
b1850c12
AN
1011 $fs->create_file_from_pathname($file_record, $backuppath);
1012 }
1013 } else {
1014 // This backup does not include the files - they should be available in moodle filestorage already.
1015
100360f9
AN
1016 // Create the file in the filepool if it does not exist yet.
1017 if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
1018
1019 // Even if a file has been deleted since the backup was made, the file metadata will remain in the
1020 // files table, and the file will not be moved to the trashdir.
1021 // Files are not cleared from the files table by cron until several days after deletion.
9f7b688c 1022 if ($foundfiles = $DB->get_records('files', array('contenthash' => $file->contenthash), '', '*', 0, 1)) {
100360f9
AN
1023 // Only grab one of the foundfiles - the file content should be the same for all entries.
1024 $foundfile = reset($foundfiles);
1025 $fs->create_file_from_storedfile($file_record, $foundfile->id);
1026 } else {
1027 // A matching existing file record was not found in the database.
40e8f026 1028 $results[] = self::get_missing_file_result($file);
100360f9
AN
1029 continue;
1030 }
b1850c12 1031 }
67233725 1032 }
4b6b087f 1033
83b2cc56
DM
1034 // store the the new contextid and the new itemid in case we need to remap
1035 // references to this file later
1036 $DB->update_record('backup_files_temp', array(
1037 'id' => $rec->bftid,
1038 'newcontextid' => $newcontextid,
1039 'newitemid' => $rec->newitemid), true);
1040
4b6b087f
DM
1041 } else {
1042 // this is an alias - we can't create it yet so we stash it in a temp
1043 // table and will let the final task to deal with it
1044 if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
1045 $info = new stdClass();
1046 // oldfile holds the raw information stored in MBZ (including reference-related info)
1047 $info->oldfile = $file;
1048 // newfile holds the info for the new file_record with the context, user and itemid mapped
b1850c12 1049 $info->newfile = (object) $file_record;
4b6b087f
DM
1050
1051 restore_dbops::set_backup_ids_record($restoreid, 'file_aliases_queue', $file->id, 0, null, $info);
1052 }
2df0f295
EL
1053 }
1054 }
1055 $rs->close();
3b232aef 1056 return $results;
2df0f295
EL
1057 }
1058
40e8f026 1059 /**
1060 * Returns suitable entry to include in log when there is a missing file.
1061 *
1062 * @param stdClass $file File definition
1063 * @return stdClass Log entry
1064 */
1065 protected static function get_missing_file_result($file) {
1066 $result = new stdClass();
1067 $result->code = 'file_missing_in_backup';
1068 $result->message = 'Missing file in backup: ' . $file->filepath . $file->filename .
1069 ' (old context ' . $file->contextid . ', component ' . $file->component .
1070 ', filearea ' . $file->filearea . ', old itemid ' . $file->itemid . ')';
1071 $result->level = backup::LOG_WARNING;
1072 return $result;
1073 }
1074
482aac65
EL
1075 /**
1076 * Given one restoreid, create in DB all the users present
1077 * in backup_ids having newitemid = 0, as far as
1078 * precheck_included_users() have left them there
1079 * ready to be created. Also, annotate their newids
0e263705 1080 * once created for later reference.
1081 *
1082 * This function will start and end a new progress section in the progress
1083 * object.
1084 *
1085 * @param string $basepath Base path of unzipped backup
1086 * @param string $restoreid Restore ID
1087 * @param int $userid Default userid for files
809fdb83 1088 * @param \core\progress\base $progress Object used for progress tracking
482aac65 1089 */
0e263705 1090 public static function create_included_users($basepath, $restoreid, $userid,
809fdb83 1091 \core\progress\base $progress) {
482aac65 1092 global $CFG, $DB;
0e263705 1093 $progress->start_progress('Creating included users');
482aac65
EL
1094
1095 $authcache = array(); // Cache to get some bits from authentication plugins
1096 $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search later
1097 $themes = get_list_of_themes(); // Get themes for quick search later
1098
1099 // Iterate over all the included users with newitemid = 0, have to create them
3e7e2ab2 1100 $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'user', 'newitemid' => 0), '', 'itemid, parentitemid, info');
482aac65 1101 foreach ($rs as $recuser) {
0e263705 1102 $progress->progress();
3e7e2ab2 1103 $user = (object)backup_controller_dbops::decode_backup_temp_info($recuser->info);
482aac65
EL
1104
1105 // if user lang doesn't exist here, use site default
1106 if (!array_key_exists($user->lang, $languages)) {
1107 $user->lang = $CFG->lang;
1108 }
1109
1110 // if user theme isn't available on target site or they are disabled, reset theme
1111 if (!empty($user->theme)) {
1112 if (empty($CFG->allowuserthemes) || !in_array($user->theme, $themes)) {
1113 $user->theme = '';
1114 }
1115 }
1116
1117 // if user to be created has mnet auth and its mnethostid is $CFG->mnet_localhost_id
1118 // that's 100% impossible as own server cannot be accesed over mnet. Change auth to email/manual
1119 if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) {
1120 // Respect registerauth
1121 if ($CFG->registerauth == 'email') {
1122 $user->auth = 'email';
1123 } else {
1124 $user->auth = 'manual';
1125 }
1126 }
1127 unset($user->mnethosturl); // Not needed anymore
1128
1129 // Disable pictures based on global setting
1130 if (!empty($CFG->disableuserimages)) {
1131 $user->picture = 0;
1132 }
1133
1134 // We need to analyse the AUTH field to recode it:
1135 // - if the auth isn't enabled in target site, $CFG->registerauth will decide
1136 // - finally, if the auth resulting isn't enabled, default to 'manual'
1137 if (!is_enabled_auth($user->auth)) {
1138 if ($CFG->registerauth == 'email') {
1139 $user->auth = 'email';
1140 } else {
1141 $user->auth = 'manual';
1142 }
1143 }
1144 if (!is_enabled_auth($user->auth)) { // Final auth check verify, default to manual if not enabled
1145 $user->auth = 'manual';
1146 }
1147
1148 // Now that we know the auth method, for users to be created without pass
1149 // if password handling is internal and reset password is available
1150 // we set the password to "restored" (plain text), so the login process
1151 // will know how to handle that situation in order to allow the user to
1152 // recover the password. MDL-20846
1153 if (empty($user->password)) { // Only if restore comes without password
1154 if (!array_key_exists($user->auth, $authcache)) { // Not in cache
1155 $userauth = new stdClass();
1156 $authplugin = get_auth_plugin($user->auth);
1157 $userauth->preventpassindb = $authplugin->prevent_local_passwords();
1158 $userauth->isinternal = $authplugin->is_internal();
1159 $userauth->canresetpwd = $authplugin->can_reset_password();
1160 $authcache[$user->auth] = $userauth;
1161 } else {
1162 $userauth = $authcache[$user->auth]; // Get from cache
1163 }
1164
1165 // Most external plugins do not store passwords locally
1166 if (!empty($userauth->preventpassindb)) {
ec2d8ceb 1167 $user->password = AUTH_PASSWORD_NOT_CACHED;
482aac65
EL
1168
1169 // If Moodle is responsible for storing/validating pwd and reset functionality is available, mark
1170 } else if ($userauth->isinternal and $userauth->canresetpwd) {
1171 $user->password = 'restored';
1172 }
1173 }
1174
1175 // Creating new user, we must reset the policyagreed always
1176 $user->policyagreed = 0;
1177
1178 // Set time created if empty
1179 if (empty($user->timecreated)) {
1180 $user->timecreated = time();
1181 }
1182
1183 // Done, let's create the user and annotate its id
1184 $newuserid = $DB->insert_record('user', $user);
1185 self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $newuserid);
1186 // Let's create the user context and annotate it (we need it for sure at least for files)
1acad8ee
EL
1187 // but for deleted users that don't have a context anymore (MDL-30192). We are done for them
1188 // and nothing else (custom fields, prefs, tags, files...) will be created.
1189 if (empty($user->deleted)) {
a689cd1d 1190 $newuserctxid = $user->deleted ? 0 : context_user::instance($newuserid)->id;
1acad8ee
EL
1191 self::set_backup_ids_record($restoreid, 'context', $recuser->parentitemid, $newuserctxid);
1192
1193 // Process custom fields
1194 if (isset($user->custom_fields)) { // if present in backup
1195 foreach($user->custom_fields['custom_field'] as $udata) {
1196 $udata = (object)$udata;
1197 // If the profile field has data and the profile shortname-datatype is defined in server
1198 if ($udata->field_data) {
1199 if ($field = $DB->get_record('user_info_field', array('shortname'=>$udata->field_name, 'datatype'=>$udata->field_type))) {
1200 /// Insert the user_custom_profile_field
1201 $rec = new stdClass();
1202 $rec->userid = $newuserid;
1203 $rec->fieldid = $field->id;
1204 $rec->data = $udata->field_data;
1205 $DB->insert_record('user_info_data', $rec);
1206 }
482aac65
EL
1207 }
1208 }
1209 }
482aac65 1210
1acad8ee
EL
1211 // Process tags
1212 if (!empty($CFG->usetags) && isset($user->tags)) { // if enabled in server and present in backup
1213 $tags = array();
1214 foreach($user->tags['tag'] as $usertag) {
1215 $usertag = (object)$usertag;
1216 $tags[] = $usertag->rawname;
1217 }
a9282e41
AA
1218 if (empty($newuserctxid)) {
1219 $newuserctxid = null; // Tag apis expect a null contextid not 0.
1220 }
cc033d48 1221 tag_set('user', $newuserid, $tags, 'core', $newuserctxid);
482aac65 1222 }
482aac65 1223
1acad8ee
EL
1224 // Process preferences
1225 if (isset($user->preferences)) { // if present in backup
1226 foreach($user->preferences['preference'] as $preference) {
1227 $preference = (object)$preference;
1228 // Prepare the record and insert it
1229 $preference->userid = $newuserid;
3d27180e
DW
1230 $status = $DB->insert_record('user_preferences', $preference);
1231 }
1232 }
1233 // Special handling for htmleditor which was converted to a preference.
1234 if (isset($user->htmleditor)) {
1235 if ($user->htmleditor == 0) {
1236 $preference = new stdClass();
1237 $preference->userid = $newuserid;
1238 $preference->name = 'htmleditor';
1239 $preference->value = 'textarea';
1acad8ee
EL
1240 $status = $DB->insert_record('user_preferences', $preference);
1241 }
482aac65 1242 }
482aac65 1243
1acad8ee 1244 // Create user files in pool (profile, icon, private) by context
0e263705 1245 restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'icon',
1246 $recuser->parentitemid, $userid, null, null, null, false, $progress);
1247 restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'profile',
1248 $recuser->parentitemid, $userid, null, null, null, false, $progress);
1acad8ee 1249 }
482aac65
EL
1250 }
1251 $rs->close();
0e263705 1252 $progress->end_progress();
482aac65
EL
1253 }
1254
1255 /**
1256 * Given one user object (from backup file), perform all the neccesary
1257 * checks is order to decide how that user will be handled on restore.
1258 *
1259 * Note the function requires $user->mnethostid to be already calculated
1260 * so it's caller responsibility to set it
1261 *
1262 * This function is used both by @restore_precheck_users() and
1263 * @restore_create_users() to get consistent results in both places
1264 *
1265 * It returns:
1266 * - one user object (from DB), if match has been found and user will be remapped
1267 * - boolean true if the user needs to be created
1268 * - boolean false if some conflict happened and the user cannot be handled
1269 *
1270 * Each test is responsible for returning its results and interrupt
1271 * execution. At the end, boolean true (user needs to be created) will be
1272 * returned if no test has interrupted that.
1273 *
1274 * Here it's the logic applied, keep it updated:
1275 *
1276 * If restoring users from same site backup:
1277 * 1A - Normal check: If match by id and username and mnethost => ok, return target user
f5744d1c
DS
1278 * 1B - If restoring an 'anonymous' user (created via the 'Anonymize user information' option) try to find a
1279 * match by username only => ok, return target user MDL-31484
1280 * 1C - Handle users deleted in DB and "alive" in backup file:
482aac65
EL
1281 * If match by id and mnethost and user is deleted in DB and
1282 * (match by username LIKE 'backup_email.%' or by non empty email = md5(username)) => ok, return target user
f5744d1c 1283 * 1D - Handle users deleted in backup file and "alive" in DB:
482aac65
EL
1284 * If match by id and mnethost and user is deleted in backup file
1285 * and match by email = email_without_time(backup_email) => ok, return target user
f5744d1c
DS
1286 * 1E - Conflict: If match by username and mnethost and doesn't match by id => conflict, return false
1287 * 1F - None of the above, return true => User needs to be created
482aac65
EL
1288 *
1289 * if restoring from another site backup (cannot match by id here, replace it by email/firstaccess combination):
1290 * 2A - Normal check: If match by username and mnethost and (email or non-zero firstaccess) => ok, return target user
1291 * 2B - Handle users deleted in DB and "alive" in backup file:
1292 * 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
1293 * (username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
1294 * 2B2 - If match by mnethost and user is deleted in DB and
1295 * username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
1296 * (to cover situations were md5(username) wasn't implemented on delete we requiere both)
1297 * 2C - Handle users deleted in backup file and "alive" in DB:
1298 * If match mnethost and user is deleted in backup file
1299 * and by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
1300 * 2D - Conflict: If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
1301 * 2E - None of the above, return true => User needs to be created
1302 *
1303 * Note: for DB deleted users email is stored in username field, hence we
1304 * are looking there for emails. See delete_user()
1305 * Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1306 * hence we are looking there for usernames if not empty. See delete_user()
1307 */
1308 protected static function precheck_user($user, $samesite) {
1309 global $CFG, $DB;
1310
1311 // Handle checks from same site backups
1312 if ($samesite && empty($CFG->forcedifferentsitecheckingusersonrestore)) {
1313
1314 // 1A - If match by id and username and mnethost => ok, return target user
1315 if ($rec = $DB->get_record('user', array('id'=>$user->id, 'username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
1316 return $rec; // Matching user found, return it
1317 }
1318
f5744d1c
DS
1319 // 1B - If restoring an 'anonymous' user (created via the 'Anonymize user information' option) try to find a
1320 // match by username only => ok, return target user MDL-31484
1321 // This avoids username / id mis-match problems when restoring subsequent anonymized backups.
1322 if (backup_anonymizer_helper::is_anonymous_user($user)) {
1323 if ($rec = $DB->get_record('user', array('username' => $user->username))) {
1324 return $rec; // Matching anonymous user found - return it
1325 }
1326 }
1327
1328 // 1C - Handle users deleted in DB and "alive" in backup file
482aac65
EL
1329 // Note: for DB deleted users email is stored in username field, hence we
1330 // are looking there for emails. See delete_user()
1331 // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1332 // hence we are looking there for usernames if not empty. See delete_user()
1333 // If match by id and mnethost and user is deleted in DB and
1334 // match by username LIKE 'backup_email.%' or by non empty email = md5(username) => ok, return target user
1335 if ($rec = $DB->get_record_sql("SELECT *
1336 FROM {user} u
1337 WHERE id = ?
1338 AND mnethostid = ?
1339 AND deleted = 1
1340 AND (
ef6b3ba1 1341 UPPER(username) LIKE UPPER(?)
482aac65
EL
1342 OR (
1343 ".$DB->sql_isnotempty('user', 'email', false, false)."
1344 AND email = ?
1345 )
1346 )",
1347 array($user->id, $user->mnethostid, $user->email.'.%', md5($user->username)))) {
1348 return $rec; // Matching user, deleted in DB found, return it
1349 }
1350
f5744d1c 1351 // 1D - Handle users deleted in backup file and "alive" in DB
482aac65
EL
1352 // If match by id and mnethost and user is deleted in backup file
1353 // and match by email = email_without_time(backup_email) => ok, return target user
1354 if ($user->deleted) {
1355 // Note: for DB deleted users email is stored in username field, hence we
1356 // are looking there for emails. See delete_user()
1357 // Trim time() from email
1358 $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
1359 if ($rec = $DB->get_record_sql("SELECT *
1360 FROM {user} u
1361 WHERE id = ?
1362 AND mnethostid = ?
ef6b3ba1 1363 AND UPPER(email) = UPPER(?)",
482aac65
EL
1364 array($user->id, $user->mnethostid, $trimemail))) {
1365 return $rec; // Matching user, deleted in backup file found, return it
1366 }
1367 }
1368
f5744d1c 1369 // 1E - If match by username and mnethost and doesn't match by id => conflict, return false
482aac65
EL
1370 if ($rec = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
1371 if ($user->id != $rec->id) {
1372 return false; // Conflict, username already exists and belongs to another id
1373 }
1374 }
1375
1376 // Handle checks from different site backups
1377 } else {
1378
1379 // 2A - If match by username and mnethost and
1380 // (email or non-zero firstaccess) => ok, return target user
1381 if ($rec = $DB->get_record_sql("SELECT *
1382 FROM {user} u
1383 WHERE username = ?
1384 AND mnethostid = ?
1385 AND (
ef6b3ba1 1386 UPPER(email) = UPPER(?)
482aac65
EL
1387 OR (
1388 firstaccess != 0
1389 AND firstaccess = ?
1390 )
1391 )",
1392 array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
1393 return $rec; // Matching user found, return it
1394 }
1395
1396 // 2B - Handle users deleted in DB and "alive" in backup file
1397 // Note: for DB deleted users email is stored in username field, hence we
1398 // are looking there for emails. See delete_user()
1399 // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1400 // hence we are looking there for usernames if not empty. See delete_user()
1401 // 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
1402 // (by username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
1403 if ($rec = $DB->get_record_sql("SELECT *
1404 FROM {user} u
1405 WHERE mnethostid = ?
1406 AND deleted = 1
1407 AND ".$DB->sql_isnotempty('user', 'email', false, false)."
1408 AND email = ?
1409 AND (
ef6b3ba1 1410 UPPER(username) LIKE UPPER(?)
482aac65
EL
1411 OR (
1412 firstaccess != 0
1413 AND firstaccess = ?
1414 )
1415 )",
1416 array($user->mnethostid, md5($user->username), $user->email.'.%', $user->firstaccess))) {
1417 return $rec; // Matching user found, return it
1418 }
1419
1420 // 2B2 - If match by mnethost and user is deleted in DB and
1421 // username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
1422 // (this covers situations where md5(username) wasn't being stored so we require both
1423 // the email & non-zero firstaccess to match)
1424 if ($rec = $DB->get_record_sql("SELECT *
1425 FROM {user} u
1426 WHERE mnethostid = ?
1427 AND deleted = 1
ef6b3ba1 1428 AND UPPER(username) LIKE UPPER(?)
482aac65
EL
1429 AND firstaccess != 0
1430 AND firstaccess = ?",
1431 array($user->mnethostid, $user->email.'.%', $user->firstaccess))) {
1432 return $rec; // Matching user found, return it
1433 }
1434
1435 // 2C - Handle users deleted in backup file and "alive" in DB
1436 // If match mnethost and user is deleted in backup file
1437 // and match by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
1438 if ($user->deleted) {
1439 // Note: for DB deleted users email is stored in username field, hence we
1440 // are looking there for emails. See delete_user()
1441 // Trim time() from email
1442 $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
1443 if ($rec = $DB->get_record_sql("SELECT *
1444 FROM {user} u
1445 WHERE mnethostid = ?
ef6b3ba1 1446 AND UPPER(email) = UPPER(?)
482aac65
EL
1447 AND firstaccess != 0
1448 AND firstaccess = ?",
1449 array($user->mnethostid, $trimemail, $user->firstaccess))) {
1450 return $rec; // Matching user, deleted in backup file found, return it
1451 }
1452 }
1453
1454 // 2D - If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
1455 if ($rec = $DB->get_record_sql("SELECT *
1456 FROM {user} u
1457 WHERE username = ?
1458 AND mnethostid = ?
1459 AND NOT (
ef6b3ba1 1460 UPPER(email) = UPPER(?)
482aac65
EL
1461 OR (
1462 firstaccess != 0
1463 AND firstaccess = ?
1464 )
1465 )",
1466 array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
1467 return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
1468 }
1469 }
1470
1471 // Arrived here, return true as the user will need to be created and no
1472 // conflicts have been found in the logic above. This covers:
1473 // 1E - else => user needs to be created, return true
1474 // 2E - else => user needs to be created, return true
1475 return true;
1476 }
1477
1478 /**
1479 * Check all the included users, deciding the action to perform
1480 * for each one (mapping / creation) and returning one array
1481 * of problems in case something is wrong (lack of permissions,
1482 * conficts)
4fb31ae6 1483 *
1484 * @param string $restoreid Restore id
1485 * @param int $courseid Course id
1486 * @param int $userid User id
1487 * @param bool $samesite True if restore is to same site
809fdb83 1488 * @param \core\progress\base $progress Progress reporter
482aac65 1489 */
4fb31ae6 1490 public static function precheck_included_users($restoreid, $courseid, $userid, $samesite,
809fdb83 1491 \core\progress\base $progress) {
482aac65
EL
1492 global $CFG, $DB;
1493
1494 // To return any problem found
1495 $problems = array();
1496
1497 // We are going to map mnethostid, so load all the available ones
1498 $mnethosts = $DB->get_records('mnet_host', array(), 'wwwroot', 'wwwroot, id');
1499
1500 // Calculate the context we are going to use for capability checking
a689cd1d 1501 $context = context_course::instance($courseid);
482aac65
EL
1502
1503 // Calculate if we have perms to create users, by checking:
1504 // to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
1505 // and also observe $CFG->disableusercreationonrestore
1506 $cancreateuser = false;
1507 if (has_capability('moodle/restore:createuser', $context, $userid) and
1508 has_capability('moodle/restore:userinfo', $context, $userid) and
1509 empty($CFG->disableusercreationonrestore)) { // Can create users
1510
1511 $cancreateuser = true;
1512 }
1513
4fb31ae6 1514 // Prepare for reporting progress.
1515 $conditions = array('backupid' => $restoreid, 'itemname' => 'user');
1516 $max = $DB->count_records('backup_ids_temp', $conditions);
1517 $done = 0;
1518 $progress->start_progress('Checking users', $max);
1519
482aac65 1520 // Iterate over all the included users
4fb31ae6 1521 $rs = $DB->get_recordset('backup_ids_temp', $conditions, '', 'itemid, info');
482aac65 1522 foreach ($rs as $recuser) {
3e7e2ab2 1523 $user = (object)backup_controller_dbops::decode_backup_temp_info($recuser->info);
482aac65
EL
1524
1525 // Find the correct mnethostid for user before performing any further check
1526 if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) {
1527 $user->mnethostid = $CFG->mnet_localhost_id;
1528 } else {
1529 // fast url-to-id lookups
1530 if (isset($mnethosts[$user->mnethosturl])) {
1531 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
1532 } else {
1533 $user->mnethostid = $CFG->mnet_localhost_id;
1534 }
1535 }
1536
1537 // Now, precheck that user and, based on returned results, annotate action/problem
1538 $usercheck = self::precheck_user($user, $samesite);
1539
1540 if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
1541 // Annotate it, for later process. Set newitemid to mapping user->id
1542 self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $usercheck->id);
1543
1544 } else if ($usercheck === false) { // Found conflict, report it as problem
1545 $problems[] = get_string('restoreuserconflict', '', $user->username);
1546
1547 } else if ($usercheck === true) { // User needs to be created, check if we are able
1548 if ($cancreateuser) { // Can create user, set newitemid to 0 so will be created later
1549 self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, 0, null, (array)$user);
1550
1551 } else { // Cannot create user, report it as problem
1552 $problems[] = get_string('restorecannotcreateuser', '', $user->username);
1553 }
1554
1555 } else { // Shouldn't arrive here ever, something is for sure wrong. Exception
1556 throw new restore_dbops_exception('restore_error_processing_user', $user->username);
1557 }
4fb31ae6 1558 $done++;
1559 $progress->progress($done);
482aac65
EL
1560 }
1561 $rs->close();
4fb31ae6 1562 $progress->end_progress();
482aac65
EL
1563 return $problems;
1564 }
1565
1566 /**
41941110
EL
1567 * Process the needed users in order to decide
1568 * which action to perform with them (create/map)
482aac65
EL
1569 *
1570 * Just wrap over precheck_included_users(), returning
41941110 1571 * exception if any problem is found
4fb31ae6 1572 *
1573 * @param string $restoreid Restore id
1574 * @param int $courseid Course id
1575 * @param int $userid User id
1576 * @param bool $samesite True if restore is to same site
809fdb83 1577 * @param \core\progress\base $progress Optional progress tracker
482aac65 1578 */
4fb31ae6 1579 public static function process_included_users($restoreid, $courseid, $userid, $samesite,
809fdb83 1580 \core\progress\base $progress = null) {
482aac65
EL
1581 global $DB;
1582
1583 // Just let precheck_included_users() to do all the hard work
4fb31ae6 1584 $problems = self::precheck_included_users($restoreid, $courseid, $userid, $samesite, $progress);
482aac65
EL
1585
1586 // With problems, throw exception, shouldn't happen if prechecks were originally
1587 // executed, so be radical here.
1588 if (!empty($problems)) {
1589 throw new restore_dbops_exception('restore_problems_processing_users', null, implode(', ', $problems));
1590 }
482aac65
EL
1591 }
1592
41941110
EL
1593 /**
1594 * Process the needed question categories and questions
1595 * to check all them, deciding about the action to perform
1596 * (create/map) and target.
1597 *
1598 * Just wrap over precheck_categories_and_questions(), returning
1599 * exception if any problem is found
1600 */
1601 public static function process_categories_and_questions($restoreid, $courseid, $userid, $samesite) {
1602 global $DB;
1603
1604 // Just let precheck_included_users() to do all the hard work
1605 $problems = self::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite);
1606
1607 // With problems of type error, throw exception, shouldn't happen if prechecks were originally
1608 // executed, so be radical here.
1609 if (array_key_exists('errors', $problems)) {
ede7105f 1610 throw new restore_dbops_exception('restore_problems_processing_questions', null, implode(', ', $problems['errors']));
41941110
EL
1611 }
1612 }
1613
b8bb45b0
EL
1614 public static function set_backup_files_record($restoreid, $filerec) {
1615 global $DB;
1616
67233725 1617 // Store external files info in `info` field
3e7e2ab2 1618 $filerec->info = backup_controller_dbops::encode_backup_temp_info($filerec); // Encode the whole record into info.
b8bb45b0
EL
1619 $filerec->backupid = $restoreid;
1620 $DB->insert_record('backup_files_temp', $filerec);
1621 }
1622
482aac65 1623 public static function set_backup_ids_record($restoreid, $itemname, $itemid, $newitemid = 0, $parentitemid = null, $info = null) {
482aac65
EL
1624 // Build conditionally the extra record info
1625 $extrarecord = array();
1626 if ($newitemid != 0) {
1627 $extrarecord['newitemid'] = $newitemid;
1628 }
1629 if ($parentitemid != null) {
1630 $extrarecord['parentitemid'] = $parentitemid;
1631 }
1632 if ($info != null) {
3e7e2ab2 1633 $extrarecord['info'] = backup_controller_dbops::encode_backup_temp_info($info);
482aac65
EL
1634 }
1635
7f98b12f 1636 self::set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord);
482aac65
EL
1637 }
1638
1639 public static function get_backup_ids_record($restoreid, $itemname, $itemid) {
7f98b12f 1640 $dbrec = self::get_backup_ids_cached($restoreid, $itemname, $itemid);
482aac65 1641
3e7e2ab2 1642 // We must test if info is a string, as the cache stores info in object form.
7f98b12f 1643 if ($dbrec && isset($dbrec->info) && is_string($dbrec->info)) {
3e7e2ab2 1644 $dbrec->info = backup_controller_dbops::decode_backup_temp_info($dbrec->info);
482aac65 1645 }
7f98b12f 1646
482aac65
EL
1647 return $dbrec;
1648 }
4bca307a
EL
1649
1650 /**
1651 * Given on courseid, fullname and shortname, calculate the correct fullname/shortname to avoid dupes
1652 */
1653 public static function calculate_course_names($courseid, $fullname, $shortname) {
1654 global $CFG, $DB;
1655
1656 $currentfullname = '';
1657 $currentshortname = '';
1658 $counter = 0;
1659 // Iteratere while the name exists
1660 do {
1661 if ($counter) {
1662 $suffixfull = ' ' . get_string('copyasnoun') . ' ' . $counter;
1663 $suffixshort = '_' . $counter;
1664 } else {
1665 $suffixfull = '';
1666 $suffixshort = '';
1667 }
1668 $currentfullname = $fullname.$suffixfull;
1669 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort; // < 100cc
ac1f907e
MN
1670 $coursefull = $DB->get_record_select('course', 'fullname = ? AND id != ?',
1671 array($currentfullname, $courseid), '*', IGNORE_MULTIPLE);
4bca307a
EL
1672 $courseshort = $DB->get_record_select('course', 'shortname = ? AND id != ?', array($currentshortname, $courseid));
1673 $counter++;
1674 } while ($coursefull || $courseshort);
1675
1676 // Return results
1677 return array($currentfullname, $currentshortname);
1678 }
1679
1680 /**
1681 * For the target course context, put as many custom role names as possible
1682 */
1683 public static function set_course_role_names($restoreid, $courseid) {
1684 global $DB;
1685
1686 // Get the course context
a689cd1d 1687 $coursectx = context_course::instance($courseid);
4bca307a 1688 // Get all the mapped roles we have
3e7e2ab2 1689 $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid, info, newitemid');
4bca307a 1690 foreach ($rs as $recrole) {
3e7e2ab2 1691 $info = backup_controller_dbops::decode_backup_temp_info($recrole->info);
024c288d 1692 // If it's one mapped role and we have one name for it
3e7e2ab2 1693 if (!empty($recrole->newitemid) && !empty($info['nameincourse'])) {
4bca307a
EL
1694 // If role name doesn't exist, add it
1695 $rolename = new stdclass();
3e7e2ab2 1696 $rolename->roleid = $recrole->newitemid;
4bca307a
EL
1697 $rolename->contextid = $coursectx->id;
1698 if (!$DB->record_exists('role_names', (array)$rolename)) {
3e7e2ab2 1699 $rolename->name = $info['nameincourse'];
4bca307a
EL
1700 $DB->insert_record('role_names', $rolename);
1701 }
1702 }
1703 }
1704 $rs->close();
1705 }
785d6603 1706
3acc9b81
SH
1707 /**
1708 * Creates a skeleton record within the database using the passed parameters
1709 * and returns the new course id.
1710 *
1711 * @global moodle_database $DB
1712 * @param string $fullname
1713 * @param string $shortname
1714 * @param int $categoryid
1715 * @return int The new course id
1716 */
785d6603
SH
1717 public static function create_new_course($fullname, $shortname, $categoryid) {
1718 global $DB;
1719 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
1720
1721 $course = new stdClass;
1722 $course->fullname = $fullname;
1723 $course->shortname = $shortname;
1724 $course->category = $category->id;
1725 $course->sortorder = 0;
1726 $course->timecreated = time();
1727 $course->timemodified = $course->timecreated;
ed6a14eb
AB
1728 // forcing skeleton courses to be hidden instead of going by $category->visible , until MDL-27790 is resolved.
1729 $course->visible = 0;
785d6603 1730
593fc4a9
SH
1731 $courseid = $DB->insert_record('course', $course);
1732
1733 $category->coursecount++;
1734 $DB->update_record('course_categories', $category);
1735
1736 return $courseid;
785d6603
SH
1737 }
1738
3acc9b81
SH
1739 /**
1740 * Deletes all of the content associated with the given course (courseid)
1741 * @param int $courseid
d53e3298 1742 * @param array $options
3acc9b81
SH
1743 * @return bool True for success
1744 */
d53e3298
PS
1745 public static function delete_course_content($courseid, array $options = null) {
1746 return remove_course_contents($courseid, false, $options);
785d6603 1747 }
482aac65 1748}
fbc2778d
EL
1749
1750/*
1751 * Exception class used by all the @dbops stuff
1752 */
1753class restore_dbops_exception extends backup_exception {
1754
1755 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
1756 parent::__construct($errorcode, 'error', '', $a, null, $debuginfo);
1757 }
1758}