MDL-22309 report_singleview: Fixing wrong uses of get_role_users()
[moodle.git] / backup / util / ui / restore_ui.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  * This file contains the restore user interface class
20  *
21  * @package   moodlecore
22  * @copyright 2010 Sam Hemelryk
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 /**
27  * This is the restore user interface class
28  *
29  * The restore user interface class manages the user interface and restore for
30  * Moodle.
31  *
32  * @copyright 2010 Sam Hemelryk
33  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 class restore_ui extends base_ui {
36     /**
37      * The stages of the restore user interface.
38      */
39     const STAGE_CONFIRM = 1;
40     const STAGE_DESTINATION = 2;
41     const STAGE_SETTINGS = 4;
42     const STAGE_SCHEMA = 8;
43     const STAGE_REVIEW = 16;
44     const STAGE_PROCESS = 32;
45     const STAGE_COMPLETE = 64;
47     /**
48      *
49      * @var restore_ui_stage
50      */
51     protected $stage = null;
53     /**
54      * @var \core\progress\base Progress indicator (where there is no controller)
55      */
56     protected $progressreporter = null;
58     /**
59      * String mappings to the above stages
60      * @var array
61      */
62     public static $stages = array(
63         restore_ui::STAGE_CONFIRM       => 'confirm',
64         restore_ui::STAGE_DESTINATION   => 'destination',
65         restore_ui::STAGE_SETTINGS      => 'settings',
66         restore_ui::STAGE_SCHEMA        => 'schema',
67         restore_ui::STAGE_REVIEW        => 'review',
68         restore_ui::STAGE_PROCESS       => 'process',
69         restore_ui::STAGE_COMPLETE      => 'complete'
70     );
71     /**
72      * Intialises what ever stage is requested. If none are requested we check
73      * params for 'stage' and default to initial
74      *
75      * @param int|null $stage The desired stage to intialise or null for the default
76      * @return restore_ui_stage_initial|restore_ui_stage_schema|restore_ui_stage_confirmation|restore_ui_stage_final
77      */
78     protected function initialise_stage($stage = null, array $params=null) {
79         if ($stage == null) {
80             $stage = optional_param('stage', self::STAGE_CONFIRM, PARAM_INT);
81         }
82         $class = 'restore_ui_stage_'.self::$stages[$stage];
83         if (!class_exists($class)) {
84             throw new restore_ui_exception('unknownuistage');
85         }
86         $stage = new $class($this, $params);
87         return $stage;
88     }
89     /**
90      * This processes the current stage of the restore
91      * @return bool
92      */
93     public function process() {
94         if ($this->progress >= self::PROGRESS_PROCESSED) {
95             throw new restore_ui_exception('restoreuialreadyprocessed');
96         }
97         $this->progress = self::PROGRESS_PROCESSED;
99         if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > self::STAGE_CONFIRM) {
100             $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params());
101             return false;
102         }
104         // Process the stage
105         $processoutcome = $this->stage->process();
106         if ($processoutcome !== false && !($this->get_stage()==self::STAGE_PROCESS && optional_param('substage', false, PARAM_BOOL))) {
107             $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params());
108         }
110         // Process UI event after to check changes are valid
111         $this->controller->process_ui_event();
112         return $processoutcome;
113     }
114     /**
115      * Returns true if the stage is independent (not requiring a restore controller)
116      * @return bool
117      */
118     public function is_independent() {
119         return false;
120     }
121     /**
122      * Gets the unique ID associated with this UI
123      * @return string
124      */
125     public function get_uniqueid() {
126         return $this->get_restoreid();
127     }
128     /**
129      * Gets the restore id from the controller
130      * @return string
131      */
132     public function get_restoreid() {
133         return $this->controller->get_restoreid();
134     }
136     /**
137      * Gets the progress reporter object in use for this restore UI.
138      *
139      * IMPORTANT: This progress reporter is used only for UI progress that is
140      * outside the restore controller. The restore controller has its own
141      * progress reporter which is used for progress during the main restore.
142      * Use the restore controller's progress reporter to report progress during
143      * a restore operation, not this one.
144      *
145      * This extra reporter is necessary because on some restore UI screens,
146      * there are long-running tasks even though there is no restore controller
147      * in use.
148      *
149      * @return \core\progress\null
150      */
151     public function get_progress_reporter() {
152         if (!$this->progressreporter) {
153             $this->progressreporter = new \core\progress\null();
154         }
155         return $this->progressreporter;
156     }
158     /**
159      * Sets the progress reporter that will be returned by get_progress_reporter.
160      *
161      * @param c\core\progress\base$progressreporter Progress reporter
162      */
163     public function set_progress_reporter(\core\progress\base $progressreporter) {
164         $this->progressreporter = $progressreporter;
165     }
167     /**
168      * Executes the restore plan
169      * @return bool
170      */
171     public function execute() {
172         if ($this->progress >= self::PROGRESS_EXECUTED) {
173             throw new restore_ui_exception('restoreuialreadyexecuted');
174         }
175         if ($this->stage->get_stage() < self::STAGE_PROCESS) {
176             throw new restore_ui_exception('restoreuifinalisedbeforeexecute');
177         }
178         if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) {
179             $options = array();
180             $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments');
181             $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings');
182             restore_dbops::delete_course_content($this->controller->get_courseid(), $options);
183         }
184         $this->controller->execute_plan();
185         $this->progress = self::PROGRESS_EXECUTED;
186         $this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
187         return true;
188     }
190     /**
191      * Delete course which is created by restore process
192      */
193     public function cleanup() {
194         $courseid = $this->controller->get_courseid();
195         if ($this->is_temporary_course_created($courseid)) {
196             delete_course($courseid, false);
197         }
198     }
200     /**
201      * Checks if the course is not restored fully and current controller has created it.
202      * @param int $courseid id of the course which needs to be checked
203      * @return bool
204      */
205     protected function is_temporary_course_created($courseid) {
206         global $DB;
207         //Check if current controller instance has created new course.
208         if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
209             $results = $DB->record_exists_sql("SELECT bc.itemid
210                                                FROM {backup_controllers} bc, {course} c
211                                                WHERE bc.operation = 'restore'
212                                                  AND bc.type = 'course'
213                                                  AND bc.itemid = c.id
214                                                  AND bc.itemid = ?",
215                                                array($courseid)
216                                              );
217             return $results;
218         }
219         return false;
220     }
222     /**
223      * Returns true if enforce_dependencies changed any settings
224      * @return bool
225      */
226     public function enforce_changed_dependencies() {
227         return ($this->dependencychanges > 0);
228     }
229     /**
230      * Loads the restore controller if we are tracking one
231      * @return restore_controller|false
232      */
233     final public static function load_controller($restoreid=false) {
234         // Get the restore id optional param
235         if ($restoreid) {
236             try {
237                 // Try to load the controller with it.
238                 // If it fails at this point it is likely because this is the first load
239                 $controller = restore_controller::load_controller($restoreid);
240                 return $controller;
241             } catch (Exception $e) {
242                 return false;
243             }
244         }
245         return $restoreid;
246     }
247     /**
248      * Initialised the requested independent stage
249      *
250      * @param int $stage One of self::STAGE_*
251      * @param int $contextid
252      * @return restore_ui_stage_confirm|restore_ui_stage_destination
253      */
254     final public static function engage_independent_stage($stage, $contextid) {
255         if (!($stage & self::STAGE_CONFIRM + self::STAGE_DESTINATION)) {
256             throw new restore_ui_exception('dependentstagerequested');
257         }
258         $class = 'restore_ui_stage_'.self::$stages[$stage];
259         if (!class_exists($class)) {
260             throw new restore_ui_exception('unknownuistage');
261         }
262         return new $class($contextid);
263     }
264     /**
265      * Cancels the current restore and redirects the user back to the relevant place
266      */
267     public function cancel_process() {
268         //Delete temporary restore course if exists.
269         if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
270             $this->cleanup();
271         }
272         parent::cancel_process();
273     }
274     /**
275      * Gets an array of progress bar items that can be displayed through the restore renderer.
276      * @return array Array of items for the progress bar
277      */
278     public function get_progress_bar() {
279         global $PAGE;
281         $stage = self::STAGE_COMPLETE;
282         $currentstage = $this->stage->get_stage();
283         $items = array();
284         while ($stage > 0) {
285             $classes = array('backup_stage');
286             if (floor($stage/2) == $currentstage) {
287                 $classes[] = 'backup_stage_next';
288             } else if ($stage == $currentstage) {
289                 $classes[] = 'backup_stage_current';
290             } else if ($stage < $currentstage) {
291                 $classes[] = 'backup_stage_complete';
292             }
293             $item = array('text' => strlen(decbin($stage)).'. '.get_string('restorestage'.$stage, 'backup'),'class' => join(' ', $classes));
294             if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE && $stage > self::STAGE_DESTINATION) {
295                 $item['link'] = new moodle_url($PAGE->url, array('restore'=>$this->get_restoreid(), 'stage'=>$stage));
296             }
297             array_unshift($items, $item);
298             $stage = floor($stage/2);
299         }
300         return $items;
301     }
302     /**
303      * Gets the name of this UI
304      * @return string
305      */
306     public function get_name() {
307         return 'restore';
308     }
309     /**
310      * Gets the first stage for this UI
311      * @return int STAGE_CONFIRM
312      */
313     public function get_first_stage_id() {
314         return self::STAGE_CONFIRM;
315     }
316     /**
317      * Returns true if this stage has substages of which at least one needs to be displayed
318      * @return bool
319      */
320     public function requires_substage() {
321         return ($this->stage->has_sub_stages() && !$this->stage->process());
322     }
323     /**
324      * Displays this stage
325      *
326      * @param core_backup_renderer $renderer
327      * @return string HTML code to echo
328      */
329     public function display(core_backup_renderer $renderer) {
330         if ($this->progress < self::PROGRESS_SAVED) {
331             throw new base_ui_exception('backupsavebeforedisplay');
332         }
333         return $this->stage->display($renderer);
334     }
337 /**
338  * restore user interface exception. Modelled off the restore_exception class
339  */
340 class restore_ui_exception extends base_ui_exception {}