3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Defines restore_root_task class
20 * @package core_backup
23 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Start task that provides all the settings common to all restores and other initial steps
32 * TODO: Finish phpdocs
34 class restore_root_task
extends restore_task
{
37 * Create all the steps that will be part of this task
39 public function build() {
41 // Conditionally create the temp table (can exist from prechecks) and delete old stuff
42 $this->add_step(new restore_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));
44 // Now make sure the user that is running the restore can actually access the course
45 // before executing any other step (potentially performing permission checks)
46 $this->add_step(new restore_fix_restorer_access_step('fix_restorer_access'));
48 // If we haven't preloaded information, load all the included inforef records to temp_ids table
49 $this->add_step(new restore_load_included_inforef_records('load_inforef_records'));
51 // Load all the needed files to temp_ids table
52 $this->add_step(new restore_load_included_files('load_file_records', 'files.xml'));
54 // If we haven't preloaded information, load all the needed roles to temp_ids_table
55 $this->add_step(new restore_load_and_map_roles('load_and_map_roles'));
57 // If we haven't preloaded information and are restoring user info, load all the needed users to temp_ids table
58 $this->add_step(new restore_load_included_users('load_user_records'));
60 // If we haven't preloaded information and are restoring user info, process all those needed users
61 // marking for create/map them as needed. Any problem here will cause exception as far as prechecks have
62 // performed the same process so, it's not possible to have errors here
63 $this->add_step(new restore_process_included_users('process_user_records'));
65 // Unconditionally, create all the needed users calculated in the previous step
66 $this->add_step(new restore_create_included_users('create_users'));
68 // Unconditionally, load create all the needed groups and groupings
69 $this->add_step(new restore_groups_structure_step('create_groups_and_groupings', 'groups.xml'));
71 // Unconditionally, load create all the needed scales
72 $this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml'));
74 // Unconditionally, load create all the needed outcomes
75 $this->add_step(new restore_outcomes_structure_step('create_scales', 'outcomes.xml'));
77 // If we haven't preloaded information, load all the needed categories and questions (reduced) to temp_ids_table
78 $this->add_step(new restore_load_categories_and_questions('load_categories_and_questions'));
80 // If we haven't preloaded information, process all the loaded categories and questions
81 // marking them for creation/mapping as needed. Any problem here will cause exception
82 // because this same process has been executed and reported by restore prechecks, so
83 // it is not possible to have errors here.
84 $this->add_step(new restore_process_categories_and_questions('process_categories_and_questions'));
86 // Unconditionally, create and map all the categories and questions
87 $this->add_step(new restore_create_categories_and_questions('create_categories_and_questions', 'questions.xml'));
89 // At the end, mark it as built
93 // Protected API starts here
96 * Define the common setting that any restore type will have
98 protected function define_settings() {
100 // Load all the root settings found in backup file from controller
101 $rootsettings = $this->get_info()->root_settings
;
103 // Define users setting (keeping it on hand to define dependencies)
104 $defaultvalue = false; // Safer default
106 if (isset($rootsettings['users']) && $rootsettings['users']) { // Only enabled when available
107 $defaultvalue = true;
110 $users = new restore_users_setting('users', base_setting
::IS_BOOLEAN
, $defaultvalue);
111 $users->set_ui(new backup_setting_ui_checkbox($users, get_string('rootsettingusers', 'backup')));
112 $users->get_ui()->set_changeable($changeable);
113 $this->add_setting($users);
115 // Restore enrolment methods.
118 backup
::ENROL_NEVER
=> get_string('rootsettingenrolments_never', 'backup'),
119 backup
::ENROL_WITHUSERS
=> get_string('rootsettingenrolments_withusers', 'backup'),
120 backup
::ENROL_ALWAYS
=> get_string('rootsettingenrolments_always', 'backup'),
122 $enroldefault = backup
::ENROL_WITHUSERS
;
124 // Users can not be restored, simplify the dropdown.
126 backup
::ENROL_NEVER
=> get_string('no'),
127 backup
::ENROL_ALWAYS
=> get_string('yes')
129 $enroldefault = backup
::ENROL_NEVER
;
131 $enrolments = new restore_users_setting('enrolments', base_setting
::IS_INTEGER
, $enroldefault);
132 $enrolments->set_ui(new backup_setting_ui_select($enrolments, get_string('rootsettingenrolments', 'backup'),
134 $this->add_setting($enrolments);
136 // Define role_assignments (dependent of users)
137 $defaultvalue = false; // Safer default
139 if (isset($rootsettings['role_assignments']) && $rootsettings['role_assignments']) { // Only enabled when available
140 $defaultvalue = true;
143 $roleassignments = new restore_role_assignments_setting('role_assignments', base_setting
::IS_BOOLEAN
, $defaultvalue);
144 $roleassignments->set_ui(new backup_setting_ui_checkbox($roleassignments,get_string('rootsettingroleassignments', 'backup')));
145 $roleassignments->get_ui()->set_changeable($changeable);
146 $this->add_setting($roleassignments);
147 $users->add_dependency($roleassignments);
149 // Define permissions.
150 $defaultvalue = false; // Safer default.
152 // Enable when available, or key doesn't exist (backward compatibility).
153 if (!array_key_exists('permissions', $rootsettings) ||
!empty($rootsettings['permissions'])) {
154 $defaultvalue = true;
157 $permissions = new restore_permissions_setting('permissions', base_setting
::IS_BOOLEAN
, $defaultvalue);
158 $permissions->set_ui(new backup_setting_ui_checkbox($permissions, get_string('rootsettingpermissions', 'backup')));
159 $permissions->get_ui()->set_changeable($changeable);
160 $this->add_setting($permissions);
162 // Define activitites
163 $defaultvalue = false; // Safer default
165 if (isset($rootsettings['activities']) && $rootsettings['activities']) { // Only enabled when available
166 $defaultvalue = true;
169 $activities = new restore_activities_setting('activities', base_setting
::IS_BOOLEAN
, $defaultvalue);
170 $activities->set_ui(new backup_setting_ui_checkbox($activities, get_string('rootsettingactivities', 'backup')));
171 $activities->get_ui()->set_changeable($changeable);
172 $this->add_setting($activities);
175 $defaultvalue = false; // Safer default
177 if (isset($rootsettings['blocks']) && $rootsettings['blocks']) { // Only enabled when available
178 $defaultvalue = true;
181 $blocks = new restore_generic_setting('blocks', base_setting
::IS_BOOLEAN
, $defaultvalue);
182 $blocks->set_ui(new backup_setting_ui_checkbox($blocks, get_string('rootsettingblocks', 'backup')));
183 $blocks->get_ui()->set_changeable($changeable);
184 $this->add_setting($blocks);
187 $defaultvalue = false; // Safer default
189 if (isset($rootsettings['filters']) && $rootsettings['filters']) { // Only enabled when available
190 $defaultvalue = true;
193 $filters = new restore_generic_setting('filters', base_setting
::IS_BOOLEAN
, $defaultvalue);
194 $filters->set_ui(new backup_setting_ui_checkbox($filters, get_string('rootsettingfilters', 'backup')));
195 $filters->get_ui()->set_changeable($changeable);
196 $this->add_setting($filters);
198 // Define comments (dependent of users)
199 $defaultvalue = false; // Safer default
201 if (isset($rootsettings['comments']) && $rootsettings['comments']) { // Only enabled when available
202 $defaultvalue = true;
205 $comments = new restore_comments_setting('comments', base_setting
::IS_BOOLEAN
, $defaultvalue);
206 $comments->set_ui(new backup_setting_ui_checkbox($comments, get_string('rootsettingcomments', 'backup')));
207 $comments->get_ui()->set_changeable($changeable);
208 $this->add_setting($comments);
209 $users->add_dependency($comments);
211 // Define badges (dependent of activities).
212 $defaultvalue = false; // Safer default.
214 if (isset($rootsettings['badges']) && $rootsettings['badges']) { // Only enabled when available.
215 $defaultvalue = true;
218 $badges = new restore_badges_setting('badges', base_setting
::IS_BOOLEAN
, $defaultvalue);
219 $badges->set_ui(new backup_setting_ui_checkbox($badges, get_string('rootsettingbadges', 'backup')));
220 $badges->get_ui()->set_changeable($changeable);
221 $this->add_setting($badges);
222 $activities->add_dependency($badges);
223 $users->add_dependency($badges);
225 // Define Calendar events.
226 $defaultvalue = false; // Safer default
228 if (isset($rootsettings['calendarevents']) && $rootsettings['calendarevents']) { // Only enabled when available
229 $defaultvalue = true;
232 $events = new restore_calendarevents_setting('calendarevents', base_setting
::IS_BOOLEAN
, $defaultvalue);
233 $events->set_ui(new backup_setting_ui_checkbox($events, get_string('rootsettingcalendarevents', 'backup')));
234 $events->get_ui()->set_changeable($changeable);
235 $this->add_setting($events);
237 // Define completion (dependent of users)
238 $defaultvalue = false; // Safer default
240 if (isset($rootsettings['userscompletion']) && $rootsettings['userscompletion']) { // Only enabled when available
241 $defaultvalue = true;
244 $completion = new restore_userscompletion_setting('userscompletion', base_setting
::IS_BOOLEAN
, $defaultvalue);
245 $completion->set_ui(new backup_setting_ui_checkbox($completion, get_string('rootsettinguserscompletion', 'backup')));
246 $completion->get_ui()->set_changeable($changeable);
247 $this->add_setting($completion);
248 $users->add_dependency($completion);
250 // Define logs (dependent of users)
251 $defaultvalue = false; // Safer default
253 if (isset($rootsettings['logs']) && $rootsettings['logs']) { // Only enabled when available
254 $defaultvalue = true;
257 $logs = new restore_logs_setting('logs', base_setting
::IS_BOOLEAN
, $defaultvalue);
258 $logs->set_ui(new backup_setting_ui_checkbox($logs, get_string('rootsettinglogs', 'backup')));
259 $logs->get_ui()->set_changeable($changeable);
260 $this->add_setting($logs);
261 $users->add_dependency($logs);
263 // Define grade_histories (dependent of users)
264 $defaultvalue = false; // Safer default
266 if (isset($rootsettings['grade_histories']) && $rootsettings['grade_histories']) { // Only enabled when available
267 $defaultvalue = true;
270 $gradehistories = new restore_grade_histories_setting('grade_histories', base_setting
::IS_BOOLEAN
, $defaultvalue);
271 $gradehistories->set_ui(new backup_setting_ui_checkbox($gradehistories, get_string('rootsettinggradehistories', 'backup')));
272 $gradehistories->get_ui()->set_changeable($changeable);
273 $this->add_setting($gradehistories);
274 $users->add_dependency($gradehistories);
276 // The restore does not process the grade histories when some activities are ignored.
277 // So let's define a dependency to prevent false expectations from our users.
278 $activities->add_dependency($gradehistories);
280 // Define groups and groupings.
281 $defaultvalue = false;
283 if (isset($rootsettings['groups']) && $rootsettings['groups']) { // Only enabled when available.
284 $defaultvalue = true;
286 } else if (!isset($rootsettings['groups'])) {
287 // It is likely this is an older backup that does not contain information on the group setting,
288 // in which case groups should be restored and this setting can be changed.
289 $defaultvalue = true;
292 $groups = new restore_groups_setting('groups', base_setting
::IS_BOOLEAN
, $defaultvalue);
293 $groups->set_ui(new backup_setting_ui_checkbox($groups, get_string('rootsettinggroups', 'backup')));
294 $groups->get_ui()->set_changeable($changeable);
295 $this->add_setting($groups);
297 // Competencies restore setting. Show when competencies is enabled and the setting is available.
298 $hascompetencies = !empty($rootsettings['competencies']);
299 $competencies = new restore_competencies_setting($hascompetencies);
300 $competencies->set_ui(new backup_setting_ui_checkbox($competencies, get_string('rootsettingcompetencies', 'backup')));
301 $this->add_setting($competencies);
304 $defaultvalue = false;
306 if (isset($rootsettings['customfield']) && $rootsettings['customfield']) { // Only enabled when available.
307 $defaultvalue = true;
310 $customfields = new restore_customfield_setting('customfield', base_setting
::IS_BOOLEAN
, $defaultvalue);
311 $customfields->set_ui(new backup_setting_ui_checkbox($customfields, get_string('rootsettingcustomfield', 'backup')));
312 $customfields->get_ui()->set_changeable($changeable);
313 $this->add_setting($customfields);
315 // Define Content bank content.
316 $defaultvalue = false;
318 if (isset($rootsettings['contentbankcontent']) && $rootsettings['contentbankcontent']) { // Only enabled when available.
319 $defaultvalue = true;
322 $contents = new restore_contentbankcontent_setting('contentbankcontent', base_setting
::IS_BOOLEAN
, $defaultvalue);
323 $contents->set_ui(new backup_setting_ui_checkbox($contents, get_string('rootsettingcontentbankcontent', 'backup')));
324 $contents->get_ui()->set_changeable($changeable);
325 $this->add_setting($contents);
327 // Define xAPI states.
328 $defaultvalue = false;
330 if (isset($rootsettings['xapistate']) && $rootsettings['xapistate']) { // Only enabled when available.
331 $defaultvalue = true;
334 $xapistate = new restore_xapistate_setting('xapistate', base_setting
::IS_BOOLEAN
, $defaultvalue);
335 $xapistate->set_ui(new backup_setting_ui_checkbox($xapistate, get_string('rootsettingxapistate', 'backup')));
336 $xapistate->get_ui()->set_changeable($changeable);
337 $this->add_setting($xapistate);
339 // Include legacy files.
340 $defaultvalue = true;
342 $legacyfiles = new restore_generic_setting('legacyfiles', base_setting
::IS_BOOLEAN
, $defaultvalue);
343 $legacyfiles->set_ui(new backup_setting_ui_checkbox($legacyfiles, get_string('rootsettinglegacyfiles', 'backup')));
344 $legacyfiles->get_ui()->set_changeable($changeable);
345 $this->add_setting($legacyfiles);