MDL-26319 moved cancel_backup() from backup_ui to cancel_process() in base_ui and...
[moodle.git] / backup / util / ui / base_ui.class.php
CommitLineData
785d6603
SH
1<?php
2
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/>.
17
18/**
19 * This file contains the backup 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 */
25
26/**
27 * This is the backup user interface class
28 *
29 * The backup user interface class manages the user interface and backup for
30 * Moodle.
31 *
32 * @copyright 2010 Sam Hemelryk
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35abstract class base_ui {
36 /**
37 * The progress of this instance of the backup ui class
38 */
39 const PROGRESS_INTIAL = 0;
40 const PROGRESS_PROCESSED = 1;
41 const PROGRESS_SAVED = 2;
42 const PROGRESS_EXECUTED = 3;
43 /**
44 * The controller
45 * @var backup_controller|restore_controller
46 */
47 protected $controller;
48 /**
49 * The current stage
50 * @var base_ui_stage
51 */
52 protected $stage;
53 /**
54 * The current progress of the UI
55 * @var int One of self::PROGRESS_*
56 */
57 protected $progress;
58 /**
59 * The number of changes made by dependency enforcement
60 * @var int
61 */
62 protected $dependencychanges = 0;
63
64 /**
65 * Yay for constructors
66 * @param backup_controller $controller
67 */
68 public function __construct($controller, array $params=null) {
69 $this->controller = $controller;
70 $this->progress = self::PROGRESS_INTIAL;
71 $this->stage = $this->initialise_stage(null, $params);
72 // Process UI event before to be safe
73 $this->controller->process_ui_event();
74 }
44bb101d
SH
75 /**
76 * Destorys the backup controller and the loaded stage.
77 */
78 public function destroy() {
79
80 if ($this->controller) {
81 $this->controller->destroy();
82 }
83 unset($this->stage);
84
85 }
785d6603
SH
86 /**
87 * Intialises what ever stage is requested. If none are requested we check
88 * params for 'stage' and default to initial
89 *
90 * @param int|null $stage The desired stage to intialise or null for the default
91 * @return base_ui_stage
92 */
93 abstract protected function initialise_stage($stage = null, array $params=null);
94 /**
95 * This processes the current stage of the backup
96 * @return bool
97 */
98 public function process() {
99 if ($this->progress >= self::PROGRESS_PROCESSED) {
100 throw new backup_ui_exception('backupuialreadyprocessed');
101 }
102 $this->progress = self::PROGRESS_PROCESSED;
103
2a923f7d 104 if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > $this->get_first_stage_id()) {
785d6603
SH
105 $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params());
106 return false;
107 }
108
109 // Process the stage
110 $processoutcome = $this->stage->process();
111
112 if ($processoutcome !== false) {
113 $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params());
114 }
115
116 // Process UI event after to check changes are valid
117 $this->controller->process_ui_event();
118 return $processoutcome;
119 }
120 /**
121 * Saves the backup controller.
122 *
123 * Once this has been called nothing else can be changed in the controller.
124 *
125 * @return bool
126 */
127 public function save_controller() {
128 if ($this->progress >= self::PROGRESS_SAVED) {
129 throw new base_ui_exception('backupuialreadysaved');
130 }
131 $this->progress = self::PROGRESS_SAVED;
132 // First enforce dependencies
133 $this->enforce_dependencies();
134 // Process UI event after to check any changes are valid
135 $this->controller->process_ui_event();
136 // Save the controller
137 $this->controller->save_controller();
138 return true;
139 }
140 /**
141 * Displays the UI for the backup!
142 *
143 * Note: The UI makes use of mforms (ewww!) thus it will automatically print
144 * out the result rather than returning a string of HTML like other parts of Moodle
145 *
146 * @return bool
147 */
148 public function display() {
149 if ($this->progress < self::PROGRESS_SAVED) {
150 throw new base_ui_exception('backupsavebeforedisplay');
151 }
152 $this->stage->display();
153 }
154 /**
155 * Gets all backup tasks from the controller
156 * @return array Array of backup_task
157 */
158 public function get_tasks() {
159 $plan = $this->controller->get_plan();
160 $tasks = $plan->get_tasks();
161 return $tasks;
162 }
163 /**
164 * Gets the stage we are on
165 * @return int
166 */
167 public function get_stage() {
168 return $this->stage->get_stage();
169 }
170 /**
171 * Gets the name of the stage we are on
172 * @return string
173 */
174 public function get_stage_name() {
175 return $this->stage->get_name();
176 }
177 /**
178 * Gets the backup id from the controller
179 * @return string
180 */
181 abstract public function get_uniqueid();
182 /**
183 * Executes the backup plan
184 * @return bool
185 */
186 abstract public function execute();
187 /**
188 * Enforces dependencies on all settings. Call before save
189 * @return bool True if dependencies were enforced and changes were made
190 */
191 protected function enforce_dependencies() {
192 // Get the plan
193 $plan = $this->controller->get_plan();
194 // Get the tasks as a var so we can iterate by reference
195 $tasks = $plan->get_tasks();
196 $changes = 0;
197 foreach ($tasks as &$task) {
198 // Store as a var so we can iterate by reference
199 $settings = $task->get_settings();
200 foreach ($settings as &$setting) {
201 // Get all dependencies for iteration by reference
202 $dependencies = $setting->get_dependencies();
203 foreach ($dependencies as &$dependency) {
204 // Enforce each dependency
205 if ($dependency->enforce()) {
206 $changes++;
207 }
208 }
209 }
210 }
211 // Store the number of settings that changed through enforcement
212 $this->dependencychanges = $changes;
213 return ($changes>0);
214 }
215 /**
216 * Returns true if enforce_dependencies changed any settings
217 * @return bool
218 */
219 public function enforce_changed_dependencies() {
220 return ($this->dependencychanges > 0);
221 }
222 /**
223 * Loads the backup controller if we are tracking one
224 * @return backup_controller|false
225 */
7c109ea3
PS
226 public static function load_controller($uniqueid=false) {
227 throw new coding_exception('load_controller() method needs to be overridden in each subclass of base_ui');
228 }
ca3617c5
AB
229
230 /**
231 * Cancels the current backup/restore and redirects the user back to the relevant place
232 */
233 public function cancel_process() {
234 global $PAGE;
235 // Determine the appropriate URL to redirect the user to
236 if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) {
237 $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id'=>$PAGE->cm->id));
238 } else {
239 $relevanturl = new moodle_url('/course/view.php', array('id'=>$PAGE->course->id));
240 }
241 redirect($relevanturl);
242 }
243
785d6603
SH
244 /**
245 * Gets an array of progress bar items that can be displayed through the backup renderer.
246 * @return array Array of items for the progress bar
247 */
248 abstract public function get_progress_bar();
249 /**
250 * Gets the format for the backup
251 * @return int
252 */
253 public function get_format() {
254 return $this->controller->get_format();
255 }
256 /**
257 * Gets the type of the backup
258 * @return int
259 */
260 public function get_type() {
261 return $this->controller->get_type();
262 }
263 public function get_controller() {
264 return $this->controller;
265 }
266 /**
267 * Gets the ID used in creating the controller. Relates to course/section/cm
268 * @return int
269 */
270 public function get_controller_id() {
271 return $this->controller->get_id();
272 }
273 /**
274 * Gets the requested setting
275 * @param string $name
276 * @return mixed
277 */
278 public function get_setting($name, $default = false) {
279 try {
280 return $this->controller->get_plan()->get_setting($name);
281 } catch (Exception $e) {
282 debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
283 return $default;
284 }
285 }
286 /**
287 * Gets the value for the requested setting
288 *
289 * @param string $name
290 * @return mixed
291 */
292 public function get_setting_value($name, $default = false) {
293 try {
294 return $this->controller->get_plan()->get_setting($name)->get_value();
295 } catch (Exception $e) {
296 debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
297 return $default;
298 }
299 }
300
301 abstract public function get_name();
302
303 abstract public function get_first_stage_id();
304}
305
306/**
307 * Backup user interface exception. Modelled off the backup_exception class
308 */
309class base_ui_exception extends backup_exception {}