MDL-26319 moved cancel_backup() from backup_ui to cancel_process() in base_ui and...
[moodle.git] / backup / util / ui / backup_ui_stage.class.php
CommitLineData
1904e9b3
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 * Backup user interface stages
20 *
21 * This file contains the classes required to manage the stages that make up the
22 * backup user interface.
23 * These will be primarily operated a {@see backup_ui} instance.
24 *
25 * @package moodlecore
26 * @copyright 2010 Sam Hemelryk
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29
30/**
31 * Abstract stage class
32 *
33 * This class should be extended by all backup stages (a requirement of many backup ui functions).
34 * Each stage must then define two abstract methods
35 * - process : To process the stage
36 * - initialise_stage_form : To get a backup_moodleform instance for the stage
37 *
38 * @copyright 2010 Sam Hemelryk
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
785d6603
SH
41abstract class backup_ui_stage extends base_ui_stage {
42
43 public function __construct(backup_ui $ui, array $params = null) {
44 parent::__construct($ui, $params);
1904e9b3
SH
45 }
46 /**
47 * The backup id from the backup controller
48 * @return string
49 */
50 final public function get_backupid() {
785d6603 51 return $this->get_uniqueid();
1904e9b3 52 }
1904e9b3
SH
53}
54
55/**
56 * Class representing the initial stage of a backup.
64f93798 57 *
1904e9b3
SH
58 * In this stage the user is required to set the root level settings.
59 *
60 * @copyright 2010 Sam Hemelryk
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62 */
63class backup_ui_stage_initial extends backup_ui_stage {
64
65 /**
66 * Initial backup stage constructor
67 * @param backup_ui $ui
68 */
dd6af534 69 public function __construct(backup_ui $ui, array $params=null) {
1904e9b3 70 $this->stage = backup_ui::STAGE_INITIAL;
dd6af534 71 parent::__construct($ui, $params);
1904e9b3 72 }
4886029c 73
1904e9b3
SH
74 /**
75 * Processes the initial backup stage
76 * @param backup_moodleform $form
77 * @return int The number of changes
78 */
785d6603 79 public function process(base_moodleform $m = null) {
1904e9b3 80
4886029c 81 $form = $this->initialise_stage_form();
1904e9b3 82
4886029c 83 if ($form->is_cancelled()) {
ca3617c5 84 $this->ui->cancel_process();
4886029c 85 }
1904e9b3 86
4886029c
SH
87 $data = $form->get_data();
88 if ($data && confirm_sesskey()) {
785d6603 89 $tasks = $this->ui->get_tasks();
4886029c
SH
90 $changes = 0;
91 foreach ($tasks as &$task) {
92 // We are only interesting in the backup root task for this stage
93 if ($task instanceof backup_root_task) {
94 // Get all settings into a var so we can iterate by reference
95 $settings = $task->get_settings();
96 foreach ($settings as &$setting) {
97 $name = $setting->get_ui_name();
98 if (isset($data->$name) && $data->$name != $setting->get_value()) {
99 $setting->set_value($data->$name);
100 $changes++;
101 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) {
102 $setting->set_value(0);
103 $changes++;
104 }
1904e9b3
SH
105 }
106 }
107 }
4886029c
SH
108 // Return the number of changes the user made
109 return $changes;
110 } else {
111 return false;
1904e9b3 112 }
1904e9b3
SH
113 }
114
115 /**
116 * Initialises the backup_moodleform instance for this stage
117 *
118 * @return backup_initial_form
119 */
120 protected function initialise_stage_form() {
121 global $PAGE;
4886029c
SH
122 if ($this->stageform === null) {
123 $form = new backup_initial_form($this, $PAGE->url);
124 // Store as a variable so we can iterate by reference
785d6603 125 $tasks = $this->ui->get_tasks();
4886029c
SH
126 // Iterate all tasks by reference
127 foreach ($tasks as &$task) {
128 // For the initial stage we are only interested in the root settings
129 if ($task instanceof backup_root_task) {
130 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
131 $settings = $task->get_settings();
132 // First add all settings except the filename setting
133 foreach ($settings as &$setting) {
134 if ($setting->get_name() == 'filename') {
135 continue;
136 }
137 $form->add_setting($setting, $task);
1904e9b3 138 }
4886029c
SH
139 // Then add all dependencies
140 foreach ($settings as &$setting) {
141 if ($setting->get_name() == 'filename') {
142 continue;
143 }
144 $form->add_dependencies($setting);
1904e9b3 145 }
1904e9b3
SH
146 }
147 }
4886029c 148 $this->stageform = $form;
1904e9b3
SH
149 }
150 // Return the form
4886029c 151 return $this->stageform;
1904e9b3
SH
152 }
153}
154
155/**
156 * Schema stage of backup process
157 *
158 * During the schema stage the user is required to set the settings that relate
159 * to the area that they are backing up as well as its children.
64f93798 160 *
1904e9b3
SH
161 * @copyright 2010 Sam Hemelryk
162 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
163 */
164class backup_ui_stage_schema extends backup_ui_stage {
165 /**
166 * Schema stage constructor
167 * @param backup_moodleform $ui
168 */
dd6af534 169 public function __construct(backup_ui $ui, array $params=null) {
1904e9b3 170 $this->stage = backup_ui::STAGE_SCHEMA;
dd6af534 171 parent::__construct($ui, $params);
1904e9b3
SH
172 }
173 /**
174 * Processes the schema stage
175 *
176 * @param backup_moodleform|null $form
177 * @return int The number of changes the user made
178 */
785d6603 179 public function process(base_moodleform $form = null) {
4886029c
SH
180 $form = $this->initialise_stage_form();
181 // Check it wasn't cancelled
182 if ($form->is_cancelled()) {
ca3617c5 183 $this->ui->cancel_process();
1904e9b3
SH
184 }
185
186 // Check it has been submit
187 $data = $form->get_data();
4886029c
SH
188 if ($data && confirm_sesskey()) {
189 // Get the tasks into a var so we can iterate by reference
785d6603 190 $tasks = $this->ui->get_tasks();
4886029c
SH
191 $changes = 0;
192 // Iterate all tasks by reference
193 foreach ($tasks as &$task) {
194 // We are only interested in schema settings
195 if (!($task instanceof backup_root_task)) {
196 // Store as a variable so we can iterate by reference
197 $settings = $task->get_settings();
198 // Iterate by reference
199 foreach ($settings as &$setting) {
200 $name = $setting->get_ui_name();
201 if (isset($data->$name) && $data->$name != $setting->get_value()) {
202 $setting->set_value($data->$name);
203 $changes++;
204 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) {
205 $setting->set_value(0);
206 $changes++;
207 }
1904e9b3
SH
208 }
209 }
210 }
4886029c
SH
211 // Return the number of changes the user made
212 return $changes;
213 } else {
214 return false;
64f93798 215 }
1904e9b3
SH
216 }
217 /**
218 * Creates the backup_schema_form instance for this stage
219 *
220 * @return backup_schema_form
221 */
222 protected function initialise_stage_form() {
223 global $PAGE;
4886029c
SH
224 if ($this->stageform === null) {
225 $form = new backup_schema_form($this, $PAGE->url);
785d6603 226 $tasks = $this->ui->get_tasks();
4886029c
SH
227 $content = '';
228 $courseheading = false;
229 foreach ($tasks as $task) {
230 if (!($task instanceof backup_root_task)) {
231 if (!$courseheading) {
232 // If we havn't already display a course heading to group nicely
573cdf85 233 $form->add_heading('coursesettings', get_string('includeactivities', 'backup'));
4886029c
SH
234 $courseheading = true;
235 }
236 // First add each setting
237 foreach ($task->get_settings() as $setting) {
238 $form->add_setting($setting, $task);
239 }
240 // The add all the dependencies
241 foreach ($task->get_settings() as $setting) {
242 $form->add_dependencies($setting);
243 }
244 } else if ($this->ui->enforce_changed_dependencies()) {
245 // Only show these settings if dependencies changed them.
246 // Add a root settings heading to group nicely
247 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
248 // Iterate all settings and add them to the form as a fixed
249 // setting. We only want schema settings to be editable
250 foreach ($task->get_settings() as $setting) {
251 if ($setting->get_name() != 'filename') {
83c000d9 252 $form->add_fixed_setting($setting, $task);
4886029c 253 }
1904e9b3 254 }
1904e9b3
SH
255 }
256 }
4886029c 257 $this->stageform = $form;
1904e9b3 258 }
4886029c 259 return $this->stageform;
1904e9b3
SH
260 }
261}
262
263/**
264 * Confirmation stage
265 *
266 * On this stage the user reviews the setting for the backup and can change the filename
267 * of the file that will be generated.
268 *
269 * @copyright 2010 Sam Hemelryk
270 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271 */
272class backup_ui_stage_confirmation extends backup_ui_stage {
273 /**
274 * Constructs the stage
275 * @param backup_ui $ui
276 */
dd6af534 277 public function __construct($ui, array $params=null) {
1904e9b3 278 $this->stage = backup_ui::STAGE_CONFIRMATION;
dd6af534 279 parent::__construct($ui, $params);
1904e9b3
SH
280 }
281 /**
282 * Processes the confirmation stage
283 *
284 * @param backup_moodleform $form
285 * @return int The number of changes the user made
286 */
785d6603 287 public function process(base_moodleform $form = null) {
4886029c
SH
288 $form = $this->initialise_stage_form();
289 // Check it hasn't been cancelled
290 if ($form->is_cancelled()) {
ca3617c5 291 $this->ui->cancel_process();
1904e9b3
SH
292 }
293
1904e9b3 294 $data = $form->get_data();
4886029c
SH
295 if ($data && confirm_sesskey()) {
296 // Collect into a variable so we can iterate by reference
785d6603 297 $tasks = $this->ui->get_tasks();
4886029c
SH
298 $changes = 0;
299 // Iterate each task by reference
300 foreach ($tasks as &$task) {
301 if ($task instanceof backup_root_task) {
302 // At this stage all we are interested in is the filename setting
303 $setting = $task->get_setting('filename');
304 $name = $setting->get_ui_name();
305 if (isset($data->$name) && $data->$name != $setting->get_value()) {
306 $setting->set_value($data->$name);
307 $changes++;
308 }
1904e9b3
SH
309 }
310 }
4886029c
SH
311 // Return the number of changes the user made
312 return $changes;
313 } else {
314 return false;
1904e9b3 315 }
1904e9b3
SH
316 }
317 /**
318 * Creates the backup_confirmation_form instance this stage requires
319 *
320 * @return backup_confirmation_form
321 */
322 protected function initialise_stage_form() {
323 global $PAGE;
4886029c
SH
324 if ($this->stageform === null) {
325 // Get the form
326 $form = new backup_confirmation_form($this, $PAGE->url);
327 $content = '';
328 $courseheading = false;
329
741b96fd
SH
330 foreach ($this->ui->get_tasks() as $task) {
331 if ($setting = $task->get_setting('filename')) {
332 $form->add_heading('filenamesetting', get_string('filename', 'backup'));
12c80f79 333 if ($setting->get_value() == 'backup.mbz') {
741b96fd
SH
334 $format = $this->ui->get_format();
335 $type = $this->ui->get_type();
336 $id = $this->ui->get_controller_id();
337 $users = $this->ui->get_setting_value('users');
338 $anonymised = $this->ui->get_setting_value('anonymize');
339 $setting->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
340 }
341 $form->add_setting($setting, $task);
342 break;
4886029c 343 }
1904e9b3 344 }
4886029c 345
785d6603 346 foreach ($this->ui->get_tasks() as $task) {
4886029c
SH
347 if ($task instanceof backup_root_task) {
348 // If its a backup root add a root settings heading to group nicely
349 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
350 } else if (!$courseheading) {
351 // we havn't already add a course heading
70a10238 352 $form->add_heading('coursesettings', get_string('includeditems', 'backup'));
4886029c
SH
353 $courseheading = true;
354 }
355 // Iterate all settings, doesnt need to happen by reference
356 foreach ($task->get_settings() as $setting) {
357 // For this stage only the filename setting should be editable
358 if ($setting->get_name() != 'filename') {
83c000d9 359 $form->add_fixed_setting($setting, $task);
8f87420a 360 }
1904e9b3
SH
361 }
362 }
4886029c 363 $this->stageform = $form;
1904e9b3 364 }
4886029c 365 return $this->stageform;
1904e9b3
SH
366 }
367}
368
369/**
370 * Final stage of backup
371 *
372 * This stage is special in that it is does not make use of a form. The reason for
373 * this is the order of procession of backup at this stage.
374 * The processesion is:
375 * 1. The final stage will be intialise.
376 * 2. The confirmation stage will be processed.
377 * 3. The backup will be executed
378 * 4. The complete stage will be loaded by execution
379 * 5. The complete stage will be displayed
380 *
381 * This highlights that we neither need a form nor a display method for this stage
382 * we simply need to process.
383 *
384 * @copyright 2010 Sam Hemelryk
385 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
386 */
387class backup_ui_stage_final extends backup_ui_stage {
388 /**
389 * Constructs the final stage
390 * @param backup_ui $ui
391 */
dd6af534 392 public function __construct(backup_ui $ui, array $params=null) {
1904e9b3 393 $this->stage = backup_ui::STAGE_FINAL;
dd6af534 394 parent::__construct($ui, $params);
1904e9b3
SH
395 }
396 /**
397 * Processes the final stage.
398 *
399 * In this case it ALWAYS passes processing to the previous stage (confirmation)
400 */
785d6603 401 public function process(base_moodleform $form=null) {
4886029c 402 return true;
1904e9b3
SH
403 }
404 /**
405 * should NEVER be called... throws an exception
406 */
407 protected function initialise_stage_form() {
408 throw new backup_ui_exception('backup_ui_must_execute_first');
409 }
410 /**
411 * should NEVER be called... throws an exception
412 */
413 public function display() {
414 throw new backup_ui_exception('backup_ui_must_execute_first');
415 }
416}
417
418/**
419 * The completed backup stage
420 *
421 * At this stage everything is done and the user will be redirected to view the
422 * backup file in the file browser.
423 *
424 * @copyright 2010 Sam Hemelryk
425 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
426 */
427class backup_ui_stage_complete extends backup_ui_stage_final {
428 /**
429 * The results of the backup execution
430 * @var array
431 */
432 protected $results;
433 /**
434 * Constructs the complete backup stage
435 * @param backup_ui $ui
dd6af534 436 * @param array|null $params
1904e9b3
SH
437 * @param array $results
438 */
dd6af534 439 public function __construct(backup_ui $ui, array $params=null, array $results=null) {
1904e9b3 440 $this->results = $results;
dd6af534 441 parent::__construct($ui, $params);
4886029c 442 $this->stage = backup_ui::STAGE_COMPLETE;
1904e9b3
SH
443 }
444 /**
445 * Displays the completed backup stage.
446 *
447 * Currently this just envolves redirecting to the file browser with an
448 * appropriate message.
4886029c
SH
449 *
450 * @global core_renderer $OUTPUT
1904e9b3
SH
451 */
452 public function display() {
4886029c 453 global $OUTPUT;
64f93798 454
1904e9b3 455 // Get the resulting stored_file record
d0da42d1
DC
456 $type = $this->get_ui()->get_controller()->get_type();
457 $courseid = $this->get_ui()->get_controller()->get_courseid();
458 switch ($type) {
459 case 'activity':
460 $cmid = $this->get_ui()->get_controller()->get_id();
461 $cm = get_coursemodule_from_id(null, $cmid, $courseid);
462 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
463 $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$modcontext->id));
464 break;
465 case 'course':
466 default:
467 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
468 $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id));
469 }
4886029c
SH
470
471 echo $OUTPUT->box_start();
39bc4c6f 472 echo $OUTPUT->notification(get_string('executionsuccess', 'backup'), 'notifysuccess');
573cdf85 473 echo $OUTPUT->continue_button($restorerul);
4886029c 474 echo $OUTPUT->box_end();
1904e9b3 475 }
b46ba22b 476}