Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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