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