|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00041 abstract class backup_ui_stage extends base_ui_stage { 00042 00043 public function __construct(backup_ui $ui, array $params = null) { 00044 parent::__construct($ui, $params); 00045 } 00050 final public function get_backupid() { 00051 return $this->get_uniqueid(); 00052 } 00053 } 00054 00063 class backup_ui_stage_initial extends backup_ui_stage { 00064 00069 public function __construct(backup_ui $ui, array $params=null) { 00070 $this->stage = backup_ui::STAGE_INITIAL; 00071 parent::__construct($ui, $params); 00072 } 00073 00079 public function process(base_moodleform $m = null) { 00080 00081 $form = $this->initialise_stage_form(); 00082 00083 if ($form->is_cancelled()) { 00084 $this->ui->cancel_process(); 00085 } 00086 00087 $data = $form->get_data(); 00088 if ($data && confirm_sesskey()) { 00089 $tasks = $this->ui->get_tasks(); 00090 $changes = 0; 00091 foreach ($tasks as &$task) { 00092 // We are only interesting in the backup root task for this stage 00093 if ($task instanceof backup_root_task) { 00094 // Get all settings into a var so we can iterate by reference 00095 $settings = $task->get_settings(); 00096 foreach ($settings as &$setting) { 00097 $name = $setting->get_ui_name(); 00098 if (isset($data->$name) && $data->$name != $setting->get_value()) { 00099 $setting->set_value($data->$name); 00100 $changes++; 00101 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) { 00102 $setting->set_value(0); 00103 $changes++; 00104 } 00105 } 00106 } 00107 } 00108 // Return the number of changes the user made 00109 return $changes; 00110 } else { 00111 return false; 00112 } 00113 } 00114 00120 protected function initialise_stage_form() { 00121 global $PAGE; 00122 if ($this->stageform === null) { 00123 $form = new backup_initial_form($this, $PAGE->url); 00124 // Store as a variable so we can iterate by reference 00125 $tasks = $this->ui->get_tasks(); 00126 // Iterate all tasks by reference 00127 foreach ($tasks as &$task) { 00128 // For the initial stage we are only interested in the root settings 00129 if ($task instanceof backup_root_task) { 00130 $form->add_heading('rootsettings', get_string('rootsettings', 'backup')); 00131 $settings = $task->get_settings(); 00132 // First add all settings except the filename setting 00133 foreach ($settings as &$setting) { 00134 if ($setting->get_name() == 'filename') { 00135 continue; 00136 } 00137 $form->add_setting($setting, $task); 00138 } 00139 // Then add all dependencies 00140 foreach ($settings as &$setting) { 00141 if ($setting->get_name() == 'filename') { 00142 continue; 00143 } 00144 $form->add_dependencies($setting); 00145 } 00146 } 00147 } 00148 $this->stageform = $form; 00149 } 00150 // Return the form 00151 return $this->stageform; 00152 } 00153 } 00154 00164 class backup_ui_stage_schema extends backup_ui_stage { 00169 public function __construct(backup_ui $ui, array $params=null) { 00170 $this->stage = backup_ui::STAGE_SCHEMA; 00171 parent::__construct($ui, $params); 00172 } 00179 public function process(base_moodleform $form = null) { 00180 $form = $this->initialise_stage_form(); 00181 // Check it wasn't cancelled 00182 if ($form->is_cancelled()) { 00183 $this->ui->cancel_process(); 00184 } 00185 00186 // Check it has been submit 00187 $data = $form->get_data(); 00188 if ($data && confirm_sesskey()) { 00189 // Get the tasks into a var so we can iterate by reference 00190 $tasks = $this->ui->get_tasks(); 00191 $changes = 0; 00192 // Iterate all tasks by reference 00193 foreach ($tasks as &$task) { 00194 // We are only interested in schema settings 00195 if (!($task instanceof backup_root_task)) { 00196 // Store as a variable so we can iterate by reference 00197 $settings = $task->get_settings(); 00198 // Iterate by reference 00199 foreach ($settings as &$setting) { 00200 $name = $setting->get_ui_name(); 00201 if (isset($data->$name) && $data->$name != $setting->get_value()) { 00202 $setting->set_value($data->$name); 00203 $changes++; 00204 } else if (!isset($data->$name) && $setting->get_ui_type() == backup_setting::UI_HTML_CHECKBOX && $setting->get_value()) { 00205 $setting->set_value(0); 00206 $changes++; 00207 } 00208 } 00209 } 00210 } 00211 // Return the number of changes the user made 00212 return $changes; 00213 } else { 00214 return false; 00215 } 00216 } 00222 protected function initialise_stage_form() { 00223 global $PAGE; 00224 if ($this->stageform === null) { 00225 $form = new backup_schema_form($this, $PAGE->url); 00226 $tasks = $this->ui->get_tasks(); 00227 $content = ''; 00228 $courseheading = false; 00229 foreach ($tasks as $task) { 00230 if (!($task instanceof backup_root_task)) { 00231 if (!$courseheading) { 00232 // If we havn't already display a course heading to group nicely 00233 $form->add_heading('coursesettings', get_string('includeactivities', 'backup')); 00234 $courseheading = true; 00235 } 00236 // First add each setting 00237 foreach ($task->get_settings() as $setting) { 00238 $form->add_setting($setting, $task); 00239 } 00240 // The add all the dependencies 00241 foreach ($task->get_settings() as $setting) { 00242 $form->add_dependencies($setting); 00243 } 00244 } else if ($this->ui->enforce_changed_dependencies()) { 00245 // Only show these settings if dependencies changed them. 00246 // Add a root settings heading to group nicely 00247 $form->add_heading('rootsettings', get_string('rootsettings', 'backup')); 00248 // Iterate all settings and add them to the form as a fixed 00249 // setting. We only want schema settings to be editable 00250 foreach ($task->get_settings() as $setting) { 00251 if ($setting->get_name() != 'filename') { 00252 $form->add_fixed_setting($setting, $task); 00253 } 00254 } 00255 } 00256 } 00257 $this->stageform = $form; 00258 } 00259 return $this->stageform; 00260 } 00261 } 00262 00272 class backup_ui_stage_confirmation extends backup_ui_stage { 00277 public function __construct($ui, array $params=null) { 00278 $this->stage = backup_ui::STAGE_CONFIRMATION; 00279 parent::__construct($ui, $params); 00280 } 00287 public function process(base_moodleform $form = null) { 00288 $form = $this->initialise_stage_form(); 00289 // Check it hasn't been cancelled 00290 if ($form->is_cancelled()) { 00291 $this->ui->cancel_process(); 00292 } 00293 00294 $data = $form->get_data(); 00295 if ($data && confirm_sesskey()) { 00296 // Collect into a variable so we can iterate by reference 00297 $tasks = $this->ui->get_tasks(); 00298 $changes = 0; 00299 // Iterate each task by reference 00300 foreach ($tasks as &$task) { 00301 if ($task instanceof backup_root_task) { 00302 // At this stage all we are interested in is the filename setting 00303 $setting = $task->get_setting('filename'); 00304 $name = $setting->get_ui_name(); 00305 if (isset($data->$name) && $data->$name != $setting->get_value()) { 00306 $setting->set_value($data->$name); 00307 $changes++; 00308 } 00309 } 00310 } 00311 // Return the number of changes the user made 00312 return $changes; 00313 } else { 00314 return false; 00315 } 00316 } 00322 protected function initialise_stage_form() { 00323 global $PAGE; 00324 if ($this->stageform === null) { 00325 // Get the form 00326 $form = new backup_confirmation_form($this, $PAGE->url); 00327 $content = ''; 00328 $courseheading = false; 00329 00330 foreach ($this->ui->get_tasks() as $task) { 00331 if ($setting = $task->get_setting('filename')) { 00332 $form->add_heading('filenamesetting', get_string('filename', 'backup')); 00333 if ($setting->get_value() == 'backup.mbz') { 00334 $format = $this->ui->get_format(); 00335 $type = $this->ui->get_type(); 00336 $id = $this->ui->get_controller_id(); 00337 $users = $this->ui->get_setting_value('users'); 00338 $anonymised = $this->ui->get_setting_value('anonymize'); 00339 $setting->set_value(backup_plan_dbops::get_default_backup_filename($format, $type, $id, $users, $anonymised)); 00340 } 00341 $form->add_setting($setting, $task); 00342 break; 00343 } 00344 } 00345 00346 foreach ($this->ui->get_tasks() as $task) { 00347 if ($task instanceof backup_root_task) { 00348 // If its a backup root add a root settings heading to group nicely 00349 $form->add_heading('rootsettings', get_string('rootsettings', 'backup')); 00350 } else if (!$courseheading) { 00351 // we havn't already add a course heading 00352 $form->add_heading('coursesettings', get_string('includeditems', 'backup')); 00353 $courseheading = true; 00354 } 00355 // Iterate all settings, doesnt need to happen by reference 00356 foreach ($task->get_settings() as $setting) { 00357 // For this stage only the filename setting should be editable 00358 if ($setting->get_name() != 'filename') { 00359 $form->add_fixed_setting($setting, $task); 00360 } 00361 } 00362 } 00363 $this->stageform = $form; 00364 } 00365 return $this->stageform; 00366 } 00367 } 00368 00387 class backup_ui_stage_final extends backup_ui_stage { 00392 public function __construct(backup_ui $ui, array $params=null) { 00393 $this->stage = backup_ui::STAGE_FINAL; 00394 parent::__construct($ui, $params); 00395 } 00401 public function process(base_moodleform $form=null) { 00402 return true; 00403 } 00407 protected function initialise_stage_form() { 00408 throw new backup_ui_exception('backup_ui_must_execute_first'); 00409 } 00413 public function display() { 00414 throw new backup_ui_exception('backup_ui_must_execute_first'); 00415 } 00416 } 00417 00427 class backup_ui_stage_complete extends backup_ui_stage_final { 00432 protected $results; 00439 public function __construct(backup_ui $ui, array $params=null, array $results=null) { 00440 $this->results = $results; 00441 parent::__construct($ui, $params); 00442 $this->stage = backup_ui::STAGE_COMPLETE; 00443 } 00452 public function display() { 00453 global $OUTPUT; 00454 00455 // Get the resulting stored_file record 00456 $type = $this->get_ui()->get_controller()->get_type(); 00457 $courseid = $this->get_ui()->get_controller()->get_courseid(); 00458 switch ($type) { 00459 case 'activity': 00460 $cmid = $this->get_ui()->get_controller()->get_id(); 00461 $cm = get_coursemodule_from_id(null, $cmid, $courseid); 00462 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00463 $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$modcontext->id)); 00464 break; 00465 case 'course': 00466 default: 00467 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); 00468 $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id)); 00469 } 00470 00471 echo $OUTPUT->box_start(); 00472 echo $OUTPUT->notification(get_string('executionsuccess', 'backup'), 'notifysuccess'); 00473 echo $OUTPUT->continue_button($restorerul); 00474 echo $OUTPUT->box_end(); 00475 } 00476 }