Updated the HEAD build version to 20100616
[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 */
41abstract class backup_ui_stage {
42 /**
43 * The current stage
44 * @var int
45 */
46 protected $stage = backup_ui::STAGE_INITIAL;
47 /**
48 * The backuck UI object
49 * @var backup_ui
50 */
51 protected $ui;
4886029c
SH
52 /**
53 * The moodleform for this stage
54 * @var backup_moodleform
55 */
56 protected $stageform = null;
1904e9b3
SH
57 /**
58 *
59 * @param backup_ui $ui
60 */
b46ba22b 61 public function __construct(backup_ui $ui) {
1904e9b3
SH
62 $this->ui = $ui;
63 }
64 /**
65 * The current stage
66 * @return int
67 */
68 final public function get_stage() {
69 return $this->stage;
70 }
71 /**
72 * The next stage
73 * @return int
74 */
75 final public function get_next_stage() {
76 return floor($this->stage*2);
77 }
78 /**
79 * The previous stage
80 * @return int
81 */
82 final public function get_prev_stage() {
83 return floor($this->stage/2);
84 }
85 /**
86 * The name of this stage
87 * @return string
88 */
89 final public function get_name() {
90 return get_string('currentstage'.$this->stage,'backup');
91 }
92 /**
93 * The backup id from the backup controller
94 * @return string
95 */
96 final public function get_backupid() {
97 return $this->ui->get_backupid();
98 }
99 /**
100 * Displays the stage.
101 *
102 * By default this involves instantiating the form for the stage and the calling
103 * it to display. Remember this is a moodleform instance so it will print
104 * rather than return.
105 */
106 public function display() {
107 $form = $this->initialise_stage_form();
108 $form->display();
109 }
110 /**
111 * Processes the stage.
112 *
113 * This must be overridden by every stage as it will be different for every stage
114 *
115 * @abstract
116 * @param backup_moodleform|null $form
117 */
118 abstract public function process(backup_moodleform $form=null);
119 /**
120 * Creates an instance of the correct moodleform properly populated and all
121 * dependencies instantiated
122 *
123 * @abstract
124 * @return backup_moodleform
125 */
126 abstract protected function initialise_stage_form();
127}
128
129/**
130 * Class representing the initial stage of a backup.
131 *
132 * In this stage the user is required to set the root level settings.
133 *
134 * @copyright 2010 Sam Hemelryk
135 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
136 */
137class backup_ui_stage_initial extends backup_ui_stage {
138
139 /**
140 * Initial backup stage constructor
141 * @param backup_ui $ui
142 */
b46ba22b 143 public function __construct(backup_ui $ui) {
1904e9b3
SH
144 $this->stage = backup_ui::STAGE_INITIAL;
145 parent::__construct($ui);
146 }
4886029c 147
1904e9b3
SH
148 /**
149 * Processes the initial backup stage
150 * @param backup_moodleform $form
151 * @return int The number of changes
152 */
4886029c 153 public function process(backup_moodleform $m = null) {
1904e9b3 154
4886029c 155 $form = $this->initialise_stage_form();
1904e9b3 156
4886029c
SH
157 if ($form->is_cancelled()) {
158 $this->ui->cancel_backup();
159 }
1904e9b3 160
4886029c
SH
161 $data = $form->get_data();
162 if ($data && confirm_sesskey()) {
163 $tasks = $this->ui->get_backup_tasks();
164 $changes = 0;
165 foreach ($tasks as &$task) {
166 // We are only interesting in the backup root task for this stage
167 if ($task instanceof backup_root_task) {
168 // Get all settings into a var so we can iterate by reference
169 $settings = $task->get_settings();
170 foreach ($settings as &$setting) {
171 $name = $setting->get_ui_name();
172 if (isset($data->$name) && $data->$name != $setting->get_value()) {
173 $setting->set_value($data->$name);
174 $changes++;
175 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) {
176 $setting->set_value(0);
177 $changes++;
178 }
1904e9b3
SH
179 }
180 }
181 }
4886029c
SH
182 // Return the number of changes the user made
183 return $changes;
184 } else {
185 return false;
1904e9b3 186 }
1904e9b3
SH
187 }
188
189 /**
190 * Initialises the backup_moodleform instance for this stage
191 *
192 * @return backup_initial_form
193 */
194 protected function initialise_stage_form() {
195 global $PAGE;
4886029c
SH
196 if ($this->stageform === null) {
197 $form = new backup_initial_form($this, $PAGE->url);
198 // Store as a variable so we can iterate by reference
199 $tasks = $this->ui->get_backup_tasks();
200 // Iterate all tasks by reference
201 foreach ($tasks as &$task) {
202 // For the initial stage we are only interested in the root settings
203 if ($task instanceof backup_root_task) {
204 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
205 $settings = $task->get_settings();
206 // First add all settings except the filename setting
207 foreach ($settings as &$setting) {
208 if ($setting->get_name() == 'filename') {
209 continue;
210 }
211 $form->add_setting($setting, $task);
1904e9b3 212 }
4886029c
SH
213 // Then add all dependencies
214 foreach ($settings as &$setting) {
215 if ($setting->get_name() == 'filename') {
216 continue;
217 }
218 $form->add_dependencies($setting);
1904e9b3 219 }
1904e9b3
SH
220 }
221 }
4886029c 222 $this->stageform = $form;
1904e9b3
SH
223 }
224 // Return the form
4886029c 225 return $this->stageform;
1904e9b3
SH
226 }
227}
228
229/**
230 * Schema stage of backup process
231 *
232 * During the schema stage the user is required to set the settings that relate
233 * to the area that they are backing up as well as its children.
234 *
235 * @copyright 2010 Sam Hemelryk
236 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
237 */
238class backup_ui_stage_schema extends backup_ui_stage {
239 /**
240 * Schema stage constructor
241 * @param backup_moodleform $ui
242 */
b46ba22b 243 public function __construct(backup_ui $ui) {
1904e9b3
SH
244 $this->stage = backup_ui::STAGE_SCHEMA;
245 parent::__construct($ui);
246 }
247 /**
248 * Processes the schema stage
249 *
250 * @param backup_moodleform|null $form
251 * @return int The number of changes the user made
252 */
253 public function process(backup_moodleform $form = null) {
4886029c
SH
254 $form = $this->initialise_stage_form();
255 // Check it wasn't cancelled
256 if ($form->is_cancelled()) {
257 $this->ui->cancel_backup();
1904e9b3
SH
258 }
259
260 // Check it has been submit
261 $data = $form->get_data();
4886029c
SH
262 if ($data && confirm_sesskey()) {
263 // Get the tasks into a var so we can iterate by reference
264 $tasks = $this->ui->get_backup_tasks();
265 $changes = 0;
266 // Iterate all tasks by reference
267 foreach ($tasks as &$task) {
268 // We are only interested in schema settings
269 if (!($task instanceof backup_root_task)) {
270 // Store as a variable so we can iterate by reference
271 $settings = $task->get_settings();
272 // Iterate by reference
273 foreach ($settings as &$setting) {
274 $name = $setting->get_ui_name();
275 if (isset($data->$name) && $data->$name != $setting->get_value()) {
276 $setting->set_value($data->$name);
277 $changes++;
278 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) {
279 $setting->set_value(0);
280 $changes++;
281 }
1904e9b3
SH
282 }
283 }
284 }
4886029c
SH
285 // Return the number of changes the user made
286 return $changes;
287 } else {
288 return false;
289 }
1904e9b3
SH
290 }
291 /**
292 * Creates the backup_schema_form instance for this stage
293 *
294 * @return backup_schema_form
295 */
296 protected function initialise_stage_form() {
297 global $PAGE;
4886029c
SH
298 if ($this->stageform === null) {
299 $form = new backup_schema_form($this, $PAGE->url);
300 $tasks = $this->ui->get_backup_tasks();
301 $content = '';
302 $courseheading = false;
303 foreach ($tasks as $task) {
304 if (!($task instanceof backup_root_task)) {
305 if (!$courseheading) {
306 // If we havn't already display a course heading to group nicely
307 $form->add_heading('coursesettings', get_string('coursesettings', 'backup'));
308 $courseheading = true;
309 }
310 // First add each setting
311 foreach ($task->get_settings() as $setting) {
312 $form->add_setting($setting, $task);
313 }
314 // The add all the dependencies
315 foreach ($task->get_settings() as $setting) {
316 $form->add_dependencies($setting);
317 }
318 } else if ($this->ui->enforce_changed_dependencies()) {
319 // Only show these settings if dependencies changed them.
320 // Add a root settings heading to group nicely
321 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
322 // Iterate all settings and add them to the form as a fixed
323 // setting. We only want schema settings to be editable
324 foreach ($task->get_settings() as $setting) {
325 if ($setting->get_name() != 'filename') {
326 $form->add_fixed_setting($setting);
327 }
1904e9b3 328 }
1904e9b3
SH
329 }
330 }
4886029c 331 $this->stageform = $form;
1904e9b3 332 }
4886029c 333 return $this->stageform;
1904e9b3
SH
334 }
335}
336
337/**
338 * Confirmation stage
339 *
340 * On this stage the user reviews the setting for the backup and can change the filename
341 * of the file that will be generated.
342 *
343 * @copyright 2010 Sam Hemelryk
344 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
345 */
346class backup_ui_stage_confirmation extends backup_ui_stage {
347 /**
348 * Constructs the stage
349 * @param backup_ui $ui
350 */
b46ba22b 351 public function __construct($ui) {
1904e9b3
SH
352 $this->stage = backup_ui::STAGE_CONFIRMATION;
353 parent::__construct($ui);
354 }
355 /**
356 * Processes the confirmation stage
357 *
358 * @param backup_moodleform $form
359 * @return int The number of changes the user made
360 */
361 public function process(backup_moodleform $form = null) {
4886029c
SH
362 $form = $this->initialise_stage_form();
363 // Check it hasn't been cancelled
364 if ($form->is_cancelled()) {
365 $this->ui->cancel_backup();
1904e9b3
SH
366 }
367
1904e9b3 368 $data = $form->get_data();
4886029c
SH
369 if ($data && confirm_sesskey()) {
370 // Collect into a variable so we can iterate by reference
371 $tasks = $this->ui->get_backup_tasks();
372 $changes = 0;
373 // Iterate each task by reference
374 foreach ($tasks as &$task) {
375 if ($task instanceof backup_root_task) {
376 // At this stage all we are interested in is the filename setting
377 $setting = $task->get_setting('filename');
378 $name = $setting->get_ui_name();
379 if (isset($data->$name) && $data->$name != $setting->get_value()) {
380 $setting->set_value($data->$name);
381 $changes++;
382 }
1904e9b3
SH
383 }
384 }
4886029c
SH
385 // Return the number of changes the user made
386 return $changes;
387 } else {
388 return false;
1904e9b3 389 }
1904e9b3
SH
390 }
391 /**
392 * Creates the backup_confirmation_form instance this stage requires
393 *
394 * @return backup_confirmation_form
395 */
396 protected function initialise_stage_form() {
397 global $PAGE;
4886029c
SH
398 if ($this->stageform === null) {
399 // Get the form
400 $form = new backup_confirmation_form($this, $PAGE->url);
401 $content = '';
402 $courseheading = false;
403
404 if ($setting = $this->ui->get_setting('filename')) {
405 $form->add_heading('filenamesetting', get_string('filename', 'backup'));
406 if ($setting->get_value() == 'backup.zip') {
407 $format = $this->ui->get_backup_format();
408 $type = $this->ui->get_backup_type();
409 $id = $this->ui->get_controller_id();
410 $users = $this->ui->get_setting_value('users');
411 $anonymised = $this->ui->get_setting_value('anonymize');
412 $setting->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised));
413 }
414 $form->add_setting($setting);
1904e9b3 415 }
4886029c
SH
416
417 foreach ($this->ui->get_backup_tasks() as $task) {
418 if ($task instanceof backup_root_task) {
419 // If its a backup root add a root settings heading to group nicely
420 $form->add_heading('rootsettings', get_string('rootsettings', 'backup'));
421 } else if (!$courseheading) {
422 // we havn't already add a course heading
423 $form->add_heading('coursesettings', get_string('coursesettings', 'backup'));
424 $courseheading = true;
425 }
426 // Iterate all settings, doesnt need to happen by reference
427 foreach ($task->get_settings() as $setting) {
428 // For this stage only the filename setting should be editable
429 if ($setting->get_name() != 'filename') {
430 $form->add_fixed_setting($setting);
8f87420a 431 }
1904e9b3
SH
432 }
433 }
4886029c 434 $this->stageform = $form;
1904e9b3 435 }
4886029c 436 return $this->stageform;
1904e9b3
SH
437 }
438}
439
440/**
441 * Final stage of backup
442 *
443 * This stage is special in that it is does not make use of a form. The reason for
444 * this is the order of procession of backup at this stage.
445 * The processesion is:
446 * 1. The final stage will be intialise.
447 * 2. The confirmation stage will be processed.
448 * 3. The backup will be executed
449 * 4. The complete stage will be loaded by execution
450 * 5. The complete stage will be displayed
451 *
452 * This highlights that we neither need a form nor a display method for this stage
453 * we simply need to process.
454 *
455 * @copyright 2010 Sam Hemelryk
456 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
457 */
458class backup_ui_stage_final extends backup_ui_stage {
459 /**
460 * Constructs the final stage
461 * @param backup_ui $ui
462 */
b46ba22b 463 public function __construct(backup_ui $ui) {
1904e9b3
SH
464 $this->stage = backup_ui::STAGE_FINAL;
465 parent::__construct($ui);
466 }
467 /**
468 * Processes the final stage.
469 *
470 * In this case it ALWAYS passes processing to the previous stage (confirmation)
471 */
472 public function process(backup_moodleform $form=null) {
4886029c 473 return true;
1904e9b3
SH
474 }
475 /**
476 * should NEVER be called... throws an exception
477 */
478 protected function initialise_stage_form() {
479 throw new backup_ui_exception('backup_ui_must_execute_first');
480 }
481 /**
482 * should NEVER be called... throws an exception
483 */
484 public function display() {
485 throw new backup_ui_exception('backup_ui_must_execute_first');
486 }
487}
488
489/**
490 * The completed backup stage
491 *
492 * At this stage everything is done and the user will be redirected to view the
493 * backup file in the file browser.
494 *
495 * @copyright 2010 Sam Hemelryk
496 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
497 */
498class backup_ui_stage_complete extends backup_ui_stage_final {
499 /**
500 * The results of the backup execution
501 * @var array
502 */
503 protected $results;
504 /**
505 * Constructs the complete backup stage
506 * @param backup_ui $ui
507 * @param array $results
508 */
509 public function __construct(backup_ui $ui, $results) {
510 $this->results = $results;
511 parent::__construct($ui);
4886029c 512 $this->stage = backup_ui::STAGE_COMPLETE;
1904e9b3
SH
513 }
514 /**
515 * Displays the completed backup stage.
516 *
517 * Currently this just envolves redirecting to the file browser with an
518 * appropriate message.
4886029c
SH
519 *
520 * @global core_renderer $OUTPUT
1904e9b3
SH
521 */
522 public function display() {
4886029c
SH
523 global $OUTPUT;
524
1904e9b3
SH
525 // Get the resulting stored_file record
526 $file = $this->results['backup_destination'];
527 // Turn it into a url for the file browser
528 $fileurl = new moodle_url('/files/index.php', array(
529 'contextid' => $file->get_contextid(),
530 'filearea' => $file->get_filearea(),
531 'itemid' => $file->get_itemid(),
532 'filepath' => $file->get_filepath()
533 ));
4886029c
SH
534
535 echo $OUTPUT->box_start();
536 echo get_string('executionsuccess', 'backup');
537 echo $OUTPUT->continue_button($fileurl);
538 echo $OUTPUT->box_end();
1904e9b3 539 }
b46ba22b 540}