MDL-21432 - backup finished groupings restore + introduced role precheck/execution...
[moodle.git] / backup / moodle2 / restore_root_task.class.php
1 <?php
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/>.
18 /**
19  * @package moodlecore
20  * @subpackage backup-moodle2
21  * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 /**
26  * Start task that provides all the settings common to all restores and other initial steps
27  *
28  * TODO: Finish phpdocs
29  */
30 class restore_root_task extends restore_task {
32     /**
33      * Create all the steps that will be part of this task
34      */
35     public function build() {
37         // Conditionally create the temp table (can exist from prechecks) and delete old stuff
38         $this->add_step(new restore_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));
40         // If we haven't preloaded information, load all the included inforef records to temp_ids table
41         $this->add_step(new restore_load_included_inforef_records('load_inforef_records'));
43         // If we haven't preloaded information, load all the needed roles to temp_ids_table
44         $this->add_step(new restore_load_and_map_roles('load_and_map_roles'));
46         // If we haven't preloaded information and are restoring user info, load all the needed users to temp_ids table
47         $this->add_step(new restore_load_included_users('load_user_records'));
49         // If we haven't preloaded information and are restoring user info, process all those needed users
50         // creating/mapping them as needed. Any problem here will cause exception as far as prechecks have
51         // performed the same process so, it's not possible to have errors here
52         $this->add_step(new restore_process_included_users('process_user_records'));
54         // Load all the needed files to temp_ids table
55         $this->add_step(new restore_load_included_files('load_file_records', 'files.xml'));
57         // Unconditionally, create all the needed users calculated in the previous step
58         $this->add_step(new restore_create_included_users('create_users'));
60         // Unconditionally, load create all the needed groups and groupings
61         $this->add_step(new restore_groups_structure_step('create_groups_and_groupings', 'groups.xml'));
63         // Unconditionally, load create all the needed scales
64         $this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml'));
66         // Unconditionally, load create all the needed outcomes.
67         // TODO: restore outcomes
68         // $this->add_step(new restore_outcomes_structure_step('create_outcomes', 'outcomes.xml'));
70         // At the end, mark it as built
71         $this->built = true;
72     }
74 // Protected API starts here
76     /**
77      * Define the common setting that any restore type will have
78      */
79     protected function define_settings() {
81         // Load all the root settings found in backup file from controller
82         $rootsettings = $this->get_info()->root_settings;
84         // Define users setting (keeping it on hand to define dependencies)
85         $selectvalues = array(0=>get_string('no')); // Safer options
86         $defaultvalue = false;                      // Safer default
87         if (isset($rootsettings['users']) && $rootsettings['users']) { // Only enabled when available
88             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
89             $defaultvalue = true;
90         }
91         $users = new restore_users_setting('users', base_setting::IS_BOOLEAN, $defaultvalue);
92         $users->set_ui(new backup_setting_ui_select($users, $users->get_name(), $selectvalues));
93         $this->add_setting($users);
95         // Define role_assignments (dependent of users)
96         $selectvalues = array(0=>get_string('no')); // Safer options
97         $defaultvalue = false;                      // Safer default
98         if (isset($rootsettings['role_assignments']) && $rootsettings['role_assignments']) { // Only enabled when available
99             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
100             $defaultvalue = true;
101         }
102         $roleassignments = new restore_role_assignments_setting('role_assignments', base_setting::IS_BOOLEAN, $defaultvalue);
103         $roleassignments->set_ui(new backup_setting_ui_select($roleassignments, $roleassignments->get_name(), $selectvalues));
104         $this->add_setting($roleassignments);
105         $users->add_dependency($roleassignments);
107         // Define user_files (dependent of users)
108         $selectvalues = array(0=>get_string('no')); // Safer options
109         $defaultvalue = false;                      // Safer default
110         if (isset($rootsettings['user_files']) && $rootsettings['user_files']) { // Only enabled when available
111             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
112             $defaultvalue = true;
113         }
114         $userfiles = new restore_user_files_setting('user_files', base_setting::IS_BOOLEAN, $defaultvalue);
115         $userfiles->set_ui(new backup_setting_ui_select($userfiles, $userfiles->get_name(), $selectvalues));
116         $this->add_setting($userfiles);
117         $users->add_dependency($userfiles);
119         // Define activitites
120         $selectvalues = array(0=>get_string('no')); // Safer options
121         $defaultvalue = false;                      // Safer default
122         if (isset($rootsettings['activities']) && $rootsettings['activities']) { // Only enabled when available
123             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
124             $defaultvalue = true;
125         }
126         $activities = new restore_activities_setting('activities', base_setting::IS_BOOLEAN, $defaultvalue);
127         $activities->set_ui(new backup_setting_ui_select($activities, $activities->get_name(), $selectvalues));
128         $this->add_setting($activities);
130         // Define blocks
131         $selectvalues = array(0=>get_string('no')); // Safer options
132         $defaultvalue = false;                      // Safer default
133         if (isset($rootsettings['blocks']) && $rootsettings['blocks']) { // Only enabled when available
134             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
135             $defaultvalue = true;
136         }
137         $blocks = new restore_generic_setting('blocks', base_setting::IS_BOOLEAN, $defaultvalue);
138         $blocks->set_ui(new backup_setting_ui_select($blocks, $blocks->get_name(), $selectvalues));
139         $this->add_setting($blocks);
141         // Define filters
142         $selectvalues = array(0=>get_string('no')); // Safer options
143         $defaultvalue = false;                      // Safer default
144         if (isset($rootsettings['filters']) && $rootsettings['filters']) { // Only enabled when available
145             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
146             $defaultvalue = true;
147         }
148         $filters = new restore_generic_setting('filters', base_setting::IS_BOOLEAN, $defaultvalue);
149         $filters->set_ui(new backup_setting_ui_select($filters, $filters->get_name(), $selectvalues));
150         $this->add_setting($filters);
152         // Define comments (dependent of users)
153         $selectvalues = array(0=>get_string('no')); // Safer options
154         $defaultvalue = false;                      // Safer default
155         if (isset($rootsettings['comments']) && $rootsettings['comments']) { // Only enabled when available
156             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
157             $defaultvalue = true;
158         }
159         $comments = new restore_comments_setting('comments', base_setting::IS_BOOLEAN, $defaultvalue);
160         $comments->set_ui(new backup_setting_ui_select($comments, $comments->get_name(), $selectvalues));
161         $this->add_setting($comments);
162         $users->add_dependency($comments);
164         // Define completion (dependent of users)
165         $selectvalues = array(0=>get_string('no')); // Safer options
166         $defaultvalue = false;                      // Safer default
167         if (isset($rootsettings['userscompletion']) && $rootsettings['userscompletion']) { // Only enabled when available
168             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
169             $defaultvalue = true;
170         }
171         $completion = new restore_userscompletion_setting('userscompletion', base_setting::IS_BOOLEAN, $defaultvalue);
172         $completion->set_ui(new backup_setting_ui_select($completion, $completion->get_name(), $selectvalues));
173         $this->add_setting($completion);
174         $users->add_dependency($completion);
176         // Define logs (dependent of users)
177         $selectvalues = array(0=>get_string('no')); // Safer options
178         $defaultvalue = false;                      // Safer default
179         if (isset($rootsettings['logs']) && $rootsettings['logs']) { // Only enabled when available
180             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
181             $defaultvalue = true;
182         }
183         $logs = new restore_logs_setting('logs', base_setting::IS_BOOLEAN, $defaultvalue);
184         $logs->set_ui(new backup_setting_ui_select($logs, $logs->get_name(), $selectvalues));
185         $this->add_setting($logs);
186         $users->add_dependency($logs);
188         // Define grade_histories (dependent of users)
189         $selectvalues = array(0=>get_string('no')); // Safer options
190         $defaultvalue = false;                      // Safer default
191         if (isset($rootsettings['grade_histories']) && $rootsettings['grade_histories']) { // Only enabled when available
192             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
193             $defaultvalue = true;
194         }
195         $gradehistories = new restore_grade_histories_setting('grade_histories', base_setting::IS_BOOLEAN, $defaultvalue);
196         $gradehistories->set_ui(new backup_setting_ui_select($gradehistories, $gradehistories->get_name(), $selectvalues));
197         $this->add_setting($gradehistories);
198         $users->add_dependency($gradehistories);
199     }