Commit | Line | Data |
---|---|---|
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 |
41 | abstract 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 | */ | |
63 | class 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 SH |
83 | if ($form->is_cancelled()) { |
84 | $this->ui->cancel_backup(); | |
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 | */ | |
164 | class 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()) { | |
183 | $this->ui->cancel_backup(); | |
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 | */ | |
272 | class 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()) { | |
291 | $this->ui->cancel_backup(); | |
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 | ||
330 | if ($setting = $this->ui->get_setting('filename')) { | |
331 | $form->add_heading('filenamesetting', get_string('filename', 'backup')); | |
332 | if ($setting->get_value() == 'backup.zip') { | |
785d6603 SH |
333 | $format = $this->ui->get_format(); |
334 | $type = $this->ui->get_type(); | |
4886029c SH |
335 | $id = $this->ui->get_controller_id(); |
336 | $users = $this->ui->get_setting_value('users'); | |
337 | $anonymised = $this->ui->get_setting_value('anonymize'); | |
338 | $setting->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised)); | |
339 | } | |
340 | $form->add_setting($setting); | |
1904e9b3 | 341 | } |
4886029c | 342 | |
785d6603 | 343 | foreach ($this->ui->get_tasks() as $task) { |
4886029c SH |
344 | if ($task instanceof backup_root_task) { |
345 | // If its a backup root add a root settings heading to group nicely | |
346 | $form->add_heading('rootsettings', get_string('rootsettings', 'backup')); | |
347 | } else if (!$courseheading) { | |
348 | // we havn't already add a course heading | |
70a10238 | 349 | $form->add_heading('coursesettings', get_string('includeditems', 'backup')); |
4886029c SH |
350 | $courseheading = true; |
351 | } | |
352 | // Iterate all settings, doesnt need to happen by reference | |
353 | foreach ($task->get_settings() as $setting) { | |
354 | // For this stage only the filename setting should be editable | |
355 | if ($setting->get_name() != 'filename') { | |
83c000d9 | 356 | $form->add_fixed_setting($setting, $task); |
8f87420a | 357 | } |
1904e9b3 SH |
358 | } |
359 | } | |
4886029c | 360 | $this->stageform = $form; |
1904e9b3 | 361 | } |
4886029c | 362 | return $this->stageform; |
1904e9b3 SH |
363 | } |
364 | } | |
365 | ||
366 | /** | |
367 | * Final stage of backup | |
368 | * | |
369 | * This stage is special in that it is does not make use of a form. The reason for | |
370 | * this is the order of procession of backup at this stage. | |
371 | * The processesion is: | |
372 | * 1. The final stage will be intialise. | |
373 | * 2. The confirmation stage will be processed. | |
374 | * 3. The backup will be executed | |
375 | * 4. The complete stage will be loaded by execution | |
376 | * 5. The complete stage will be displayed | |
377 | * | |
378 | * This highlights that we neither need a form nor a display method for this stage | |
379 | * we simply need to process. | |
380 | * | |
381 | * @copyright 2010 Sam Hemelryk | |
382 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
383 | */ | |
384 | class backup_ui_stage_final extends backup_ui_stage { | |
385 | /** | |
386 | * Constructs the final stage | |
387 | * @param backup_ui $ui | |
388 | */ | |
dd6af534 | 389 | public function __construct(backup_ui $ui, array $params=null) { |
1904e9b3 | 390 | $this->stage = backup_ui::STAGE_FINAL; |
dd6af534 | 391 | parent::__construct($ui, $params); |
1904e9b3 SH |
392 | } |
393 | /** | |
394 | * Processes the final stage. | |
395 | * | |
396 | * In this case it ALWAYS passes processing to the previous stage (confirmation) | |
397 | */ | |
785d6603 | 398 | public function process(base_moodleform $form=null) { |
4886029c | 399 | return true; |
1904e9b3 SH |
400 | } |
401 | /** | |
402 | * should NEVER be called... throws an exception | |
403 | */ | |
404 | protected function initialise_stage_form() { | |
405 | throw new backup_ui_exception('backup_ui_must_execute_first'); | |
406 | } | |
407 | /** | |
408 | * should NEVER be called... throws an exception | |
409 | */ | |
410 | public function display() { | |
411 | throw new backup_ui_exception('backup_ui_must_execute_first'); | |
412 | } | |
413 | } | |
414 | ||
415 | /** | |
416 | * The completed backup stage | |
417 | * | |
418 | * At this stage everything is done and the user will be redirected to view the | |
419 | * backup file in the file browser. | |
420 | * | |
421 | * @copyright 2010 Sam Hemelryk | |
422 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
423 | */ | |
424 | class backup_ui_stage_complete extends backup_ui_stage_final { | |
425 | /** | |
426 | * The results of the backup execution | |
427 | * @var array | |
428 | */ | |
429 | protected $results; | |
430 | /** | |
431 | * Constructs the complete backup stage | |
432 | * @param backup_ui $ui | |
dd6af534 | 433 | * @param array|null $params |
1904e9b3 SH |
434 | * @param array $results |
435 | */ | |
dd6af534 | 436 | public function __construct(backup_ui $ui, array $params=null, array $results=null) { |
1904e9b3 | 437 | $this->results = $results; |
dd6af534 | 438 | parent::__construct($ui, $params); |
4886029c | 439 | $this->stage = backup_ui::STAGE_COMPLETE; |
1904e9b3 SH |
440 | } |
441 | /** | |
442 | * Displays the completed backup stage. | |
443 | * | |
444 | * Currently this just envolves redirecting to the file browser with an | |
445 | * appropriate message. | |
4886029c SH |
446 | * |
447 | * @global core_renderer $OUTPUT | |
1904e9b3 SH |
448 | */ |
449 | public function display() { | |
4886029c | 450 | global $OUTPUT; |
64f93798 | 451 | |
1904e9b3 | 452 | // Get the resulting stored_file record |
573cdf85 SH |
453 | $coursecontext = get_context_instance(CONTEXT_COURSE, $this->get_ui()->get_controller()->get_courseid()); |
454 | $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id)); | |
4886029c SH |
455 | |
456 | echo $OUTPUT->box_start(); | |
39bc4c6f | 457 | echo $OUTPUT->notification(get_string('executionsuccess', 'backup'), 'notifysuccess'); |
573cdf85 | 458 | echo $OUTPUT->continue_button($restorerul); |
4886029c | 459 | echo $OUTPUT->box_end(); |
1904e9b3 | 460 | } |
b46ba22b | 461 | } |