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 backup 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 backup user interface class
29 * The backup user interface class manages the user interface and backup for
32 * @copyright 2010 Sam Hemelryk
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class backup_ui extends base_ui {
37 * The stages of the backup user interface.
39 const STAGE_INITIAL = 1;
40 const STAGE_SCHEMA = 2;
41 const STAGE_CONFIRMATION = 4;
42 const STAGE_FINAL = 8;
43 const STAGE_COMPLETE = 16;
46 * Intialises what ever stage is requested. If none are requested we check
47 * params for 'stage' and default to initial
49 * @param int|null $stage The desired stage to intialise or null for the default
50 * @return backup_ui_stage_initial|backup_ui_stage_schema|backup_ui_stage_confirmation|backup_ui_stage_final
52 protected function initialise_stage($stage = null, array $params=null) {
54 $stage = optional_param('stage', self::STAGE_INITIAL, PARAM_INT);
57 case backup_ui::STAGE_INITIAL:
58 $stage = new backup_ui_stage_initial($this, $params);
60 case backup_ui::STAGE_SCHEMA:
61 $stage = new backup_ui_stage_schema($this, $params);
63 case backup_ui::STAGE_CONFIRMATION:
64 $stage = new backup_ui_stage_confirmation($this, $params);
66 case backup_ui::STAGE_FINAL:
67 $stage = new backup_ui_stage_final($this, $params);
75 public function get_uniqueid() {
76 return $this->get_backupid();
79 * Gets the backup id from the controller
82 public function get_backupid() {
83 return $this->controller->get_backupid();
86 * Executes the backup plan
89 public function execute() {
90 if ($this->progress >= self::PROGRESS_EXECUTED) {
91 throw new backup_ui_exception('backupuialreadyexecuted');
93 if ($this->stage->get_stage() < self::STAGE_FINAL) {
94 throw new backup_ui_exception('backupuifinalisedbeforeexecute');
96 $this->progress = self::PROGRESS_EXECUTED;
97 $this->controller->finish_ui();
98 $this->controller->execute_plan();
99 $this->stage = new backup_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
103 * Loads the backup controller if we are tracking one
104 * @return backup_controller|false
106 final public static function load_controller($backupid=false) {
107 // Get the backup id optional param
110 // Try to load the controller with it.
111 // If it fails at this point it is likely because this is the first load
112 $controller = backup_controller::load_controller($backupid);
114 } catch (Exception $e) {
121 * Cancels the current backup and redirects the user back to the relevant place
123 public function cancel_backup() {
125 // Determine the approriate URL to redirect the user to
126 if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) {
127 $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id'=>$PAGE->cm->id));
129 $relevanturl = new moodle_url('/course/view.php', array('id'=>$PAGE->course->id));
131 redirect($relevanturl);
134 * Gets an array of progress bar items that can be displayed through the backup renderer.
135 * @return array Array of items for the progress bar
137 public function get_progress_bar() {
140 $stage = self::STAGE_COMPLETE;
141 $currentstage = $this->stage->get_stage();
144 $classes = array('backup_stage');
145 if (floor($stage/2) == $currentstage) {
146 $classes[] = 'backup_stage_next';
147 } else if ($stage == $currentstage) {
148 $classes[] = 'backup_stage_current';
149 } else if ($stage < $currentstage) {
150 $classes[] = 'backup_stage_complete';
152 $item = array('text' => strlen(decbin($stage)).'. '.get_string('currentstage'.$stage, 'backup'),'class' => join(' ', $classes));
153 if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE) {
154 $item['link'] = new moodle_url($PAGE->url, array('backup'=>$this->get_backupid(), 'stage'=>$stage));
156 array_unshift($items, $item);
157 $stage = floor($stage/2);
162 public function get_name() {
166 public function get_first_stage_id() {
167 return self::STAGE_INITIAL;
172 * Backup user interface exception. Modelled off the backup_exception class
174 class backup_ui_exception extends base_ui_exception {}