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 * This file contains the restore user interface class
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * This is the restore user interface class
29 * The restore user interface class manages the user interface and restore for
32 * @copyright 2010 Sam Hemelryk
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class restore_ui extends base_ui {
37 * The stages of the restore user interface.
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;
49 * @var restore_ui_stage
51 protected $stage = null;
54 * @var \core\progress\base Progress indicator (where there is no controller)
56 protected $progressreporter = null;
59 * String mappings to the above stages
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'
72 * Intialises what ever stage is requested. If none are requested we check
73 * params for 'stage' and default to initial
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
78 protected function initialise_stage($stage = null, array $params=null) {
80 $stage = optional_param('stage', self::STAGE_CONFIRM, PARAM_INT);
82 $class = 'restore_ui_stage_'.self::$stages[$stage];
83 if (!class_exists($class)) {
84 throw new restore_ui_exception('unknownuistage');
86 $stage = new $class($this, $params);
90 * This processes the current stage of the restore
93 public function process() {
94 if ($this->progress >= self::PROGRESS_PROCESSED) {
95 throw new restore_ui_exception('restoreuialreadyprocessed');
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());
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());
110 // Process UI event after to check changes are valid
111 $this->controller->process_ui_event();
112 return $processoutcome;
115 * Returns true if the stage is independent (not requiring a restore controller)
118 public function is_independent() {
122 * Gets the unique ID associated with this UI
125 public function get_uniqueid() {
126 return $this->get_restoreid();
129 * Gets the restore id from the controller
132 public function get_restoreid() {
133 return $this->controller->get_restoreid();
137 * Gets the progress reporter object in use for this restore UI.
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.
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
149 * @return \core\progress\null
151 public function get_progress_reporter() {
152 if (!$this->progressreporter) {
153 $this->progressreporter = new \core\progress\null();
155 return $this->progressreporter;
159 * Sets the progress reporter that will be returned by get_progress_reporter.
161 * @param c\core\progress\base$progressreporter Progress reporter
163 public function set_progress_reporter(\core\progress\base $progressreporter) {
164 $this->progressreporter = $progressreporter;
168 * Executes the restore plan
171 public function execute() {
172 if ($this->progress >= self::PROGRESS_EXECUTED) {
173 throw new restore_ui_exception('restoreuialreadyexecuted');
175 if ($this->stage->get_stage() < self::STAGE_PROCESS) {
176 throw new restore_ui_exception('restoreuifinalisedbeforeexecute');
178 if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) {
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);
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());
191 * Delete course which is created by restore process
193 public function cleanup() {
194 $courseid = $this->controller->get_courseid();
195 if ($this->is_temporary_course_created($courseid)) {
196 delete_course($courseid, false);
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
205 protected function is_temporary_course_created($courseid) {
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'
223 * Returns true if enforce_dependencies changed any settings
226 public function enforce_changed_dependencies() {
227 return ($this->dependencychanges > 0);
230 * Loads the restore controller if we are tracking one
231 * @return restore_controller|false
233 final public static function load_controller($restoreid=false) {
234 // Get the restore id optional param
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);
241 } catch (Exception $e) {
248 * Initialised the requested independent stage
250 * @param int $stage One of self::STAGE_*
251 * @param int $contextid
252 * @return restore_ui_stage_confirm|restore_ui_stage_destination
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');
258 $class = 'restore_ui_stage_'.self::$stages[$stage];
259 if (!class_exists($class)) {
260 throw new restore_ui_exception('unknownuistage');
262 return new $class($contextid);
265 * Cancels the current restore and redirects the user back to the relevant place
267 public function cancel_process() {
268 //Delete temporary restore course if exists.
269 if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
272 parent::cancel_process();
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
278 public function get_progress_bar() {
281 $stage = self::STAGE_COMPLETE;
282 $currentstage = $this->stage->get_stage();
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';
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));
297 array_unshift($items, $item);
298 $stage = floor($stage/2);
303 * Gets the name of this UI
306 public function get_name() {
310 * Gets the first stage for this UI
311 * @return int STAGE_CONFIRM
313 public function get_first_stage_id() {
314 return self::STAGE_CONFIRM;
317 * Returns true if this stage has substages of which at least one needs to be displayed
320 public function requires_substage() {
321 return ($this->stage->has_sub_stages() && !$this->stage->process());
324 * Displays this stage
326 * @param core_backup_renderer $renderer
327 * @return string HTML code to echo
329 public function display(core_backup_renderer $renderer) {
330 if ($this->progress < self::PROGRESS_SAVED) {
331 throw new base_ui_exception('backupsavebeforedisplay');
333 return $this->stage->display($renderer);
338 * restore user interface exception. Modelled off the restore_exception class
340 class restore_ui_exception extends base_ui_exception {}