[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This file contains the restore user interface class 19 * 20 * @package core_backup 21 * @copyright 2010 Sam Hemelryk 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * This is the restore user interface class 27 * 28 * The restore user interface class manages the user interface and restore for 29 * Moodle. 30 * 31 * @package core_backup 32 * @copyright 2010 Sam Hemelryk 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class restore_ui extends base_ui { 36 /** 37 * The stages of the restore user interface. 38 * Confirm the backup you are going to restore. 39 */ 40 const STAGE_CONFIRM = 1; 41 42 /** 43 * The stages of the restore user interface. 44 * Select the destination for the restore. 45 */ 46 const STAGE_DESTINATION = 2; 47 48 /** 49 * The stages of the restore user interface. 50 * Alter the setting for the restore. 51 */ 52 const STAGE_SETTINGS = 4; 53 54 /** 55 * The stages of the restore user interface. 56 * Alter and review the schema that you are going to restore. 57 */ 58 const STAGE_SCHEMA = 8; 59 60 /** 61 * The stages of the restore user interface. 62 * The final review before the restore is run. 63 */ 64 const STAGE_REVIEW = 16; 65 66 /** 67 * The stages of the restore user interface. 68 * The restore is in process right now. 69 */ 70 const STAGE_PROCESS = 32; 71 72 /** 73 * The stages of the restore user interface. 74 * The process is complete. 75 */ 76 const STAGE_COMPLETE = 64; 77 78 /** 79 * The current UI stage. 80 * @var restore_ui_stage 81 */ 82 protected $stage = null; 83 84 /** 85 * @var \core\progress\base Progress indicator (where there is no controller) 86 */ 87 protected $progressreporter = null; 88 89 /** 90 * String mappings to the above stages 91 * @var array 92 */ 93 public static $stages = array( 94 restore_ui::STAGE_CONFIRM => 'confirm', 95 restore_ui::STAGE_DESTINATION => 'destination', 96 restore_ui::STAGE_SETTINGS => 'settings', 97 restore_ui::STAGE_SCHEMA => 'schema', 98 restore_ui::STAGE_REVIEW => 'review', 99 restore_ui::STAGE_PROCESS => 'process', 100 restore_ui::STAGE_COMPLETE => 'complete' 101 ); 102 103 /** 104 * Intialises what ever stage is requested. If none are requested we check 105 * params for 'stage' and default to initial 106 * 107 * @throws restore_ui_exception for an invalid stage 108 * @param int|null $stage The desired stage to intialise or null for the default 109 * @param array $params 110 * @return restore_ui_stage_initial|restore_ui_stage_schema|restore_ui_stage_confirmation|restore_ui_stage_final 111 */ 112 protected function initialise_stage($stage = null, array $params = null) { 113 if ($stage == null) { 114 $stage = optional_param('stage', self::STAGE_CONFIRM, PARAM_INT); 115 } 116 $class = 'restore_ui_stage_'.self::$stages[$stage]; 117 if (!class_exists($class)) { 118 throw new restore_ui_exception('unknownuistage'); 119 } 120 $stage = new $class($this, $params); 121 return $stage; 122 } 123 124 /** 125 * This processes the current stage of the restore 126 * @throws restore_ui_exception if the progress is wrong. 127 * @return bool 128 */ 129 public function process() { 130 if ($this->progress >= self::PROGRESS_PROCESSED) { 131 throw new restore_ui_exception('restoreuialreadyprocessed'); 132 } 133 $this->progress = self::PROGRESS_PROCESSED; 134 135 if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > self::STAGE_CONFIRM) { 136 $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params()); 137 return false; 138 } 139 140 // Process the stage. 141 $processoutcome = $this->stage->process(); 142 if ($processoutcome !== false && !($this->get_stage() == self::STAGE_PROCESS && optional_param('substage', false, PARAM_BOOL))) { 143 $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params()); 144 } 145 146 // Process UI event after to check changes are valid. 147 $this->controller->process_ui_event(); 148 return $processoutcome; 149 } 150 151 /** 152 * Returns true if the stage is independent (not requiring a restore controller) 153 * @return bool 154 */ 155 public function is_independent() { 156 return false; 157 } 158 159 /** 160 * Gets the unique ID associated with this UI 161 * @return string 162 */ 163 public function get_uniqueid() { 164 return $this->get_restoreid(); 165 } 166 167 /** 168 * Gets the restore id from the controller 169 * @return string 170 */ 171 public function get_restoreid() { 172 return $this->controller->get_restoreid(); 173 } 174 175 /** 176 * Gets the progress reporter object in use for this restore UI. 177 * 178 * IMPORTANT: This progress reporter is used only for UI progress that is 179 * outside the restore controller. The restore controller has its own 180 * progress reporter which is used for progress during the main restore. 181 * Use the restore controller's progress reporter to report progress during 182 * a restore operation, not this one. 183 * 184 * This extra reporter is necessary because on some restore UI screens, 185 * there are long-running tasks even though there is no restore controller 186 * in use. 187 * 188 * @return \core\progress\none 189 */ 190 public function get_progress_reporter() { 191 if (!$this->progressreporter) { 192 $this->progressreporter = new \core\progress\none(); 193 } 194 return $this->progressreporter; 195 } 196 197 /** 198 * Sets the progress reporter that will be returned by get_progress_reporter. 199 * 200 * @param \core\progress\base $progressreporter Progress reporter 201 */ 202 public function set_progress_reporter(\core\progress\base $progressreporter) { 203 $this->progressreporter = $progressreporter; 204 } 205 206 /** 207 * Executes the restore plan 208 * @throws restore_ui_exception if the progress or stage is wrong. 209 * @return bool 210 */ 211 public function execute() { 212 if ($this->progress >= self::PROGRESS_EXECUTED) { 213 throw new restore_ui_exception('restoreuialreadyexecuted'); 214 } 215 if ($this->stage->get_stage() < self::STAGE_PROCESS) { 216 throw new restore_ui_exception('restoreuifinalisedbeforeexecute'); 217 } 218 if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) { 219 $options = array(); 220 $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments'); 221 $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings'); 222 restore_dbops::delete_course_content($this->controller->get_courseid(), $options); 223 } 224 $this->controller->execute_plan(); 225 $this->progress = self::PROGRESS_EXECUTED; 226 $this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results()); 227 return true; 228 } 229 230 /** 231 * Delete course which is created by restore process 232 */ 233 public function cleanup() { 234 global $DB; 235 $courseid = $this->controller->get_courseid(); 236 if ($this->is_temporary_course_created($courseid) && $course = $DB->get_record('course', array('id' => $courseid))) { 237 $course->deletesource = 'restore'; 238 delete_course($course, false); 239 } 240 } 241 242 /** 243 * Checks if the course is not restored fully and current controller has created it. 244 * @param int $courseid id of the course which needs to be checked 245 * @return bool 246 */ 247 protected function is_temporary_course_created($courseid) { 248 global $DB; 249 // Check if current controller instance has created new course. 250 if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) { 251 $results = $DB->record_exists_sql("SELECT bc.itemid 252 FROM {backup_controllers} bc, {course} c 253 WHERE bc.operation = 'restore' 254 AND bc.type = 'course' 255 AND bc.itemid = c.id 256 AND bc.itemid = ?", 257 array($courseid) 258 ); 259 return $results; 260 } 261 return false; 262 } 263 264 /** 265 * Returns true if enforce_dependencies changed any settings 266 * @return bool 267 */ 268 public function enforce_changed_dependencies() { 269 return ($this->dependencychanges > 0); 270 } 271 272 /** 273 * Loads the restore controller if we are tracking one 274 * @param string|bool $restoreid 275 * @return string 276 */ 277 final public static function load_controller($restoreid = false) { 278 // Get the restore id optional param. 279 if ($restoreid) { 280 try { 281 // Try to load the controller with it. 282 // If it fails at this point it is likely because this is the first load. 283 $controller = restore_controller::load_controller($restoreid); 284 return $controller; 285 } catch (Exception $e) { 286 return false; 287 } 288 } 289 return $restoreid; 290 } 291 292 /** 293 * Initialised the requested independent stage 294 * 295 * @throws restore_ui_exception 296 * @param int $stage One of self::STAGE_* 297 * @param int $contextid 298 * @return restore_ui_stage_confirm|restore_ui_stage_destination 299 */ 300 final public static function engage_independent_stage($stage, $contextid) { 301 if (!($stage & self::STAGE_CONFIRM + self::STAGE_DESTINATION)) { 302 throw new restore_ui_exception('dependentstagerequested'); 303 } 304 $class = 'restore_ui_stage_'.self::$stages[$stage]; 305 if (!class_exists($class)) { 306 throw new restore_ui_exception('unknownuistage'); 307 } 308 return new $class($contextid); 309 } 310 311 /** 312 * Cancels the current restore and redirects the user back to the relevant place 313 */ 314 public function cancel_process() { 315 // Delete temporary restore course if exists. 316 if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) { 317 $this->cleanup(); 318 } 319 parent::cancel_process(); 320 } 321 322 /** 323 * Gets an array of progress bar items that can be displayed through the restore renderer. 324 * @return array Array of items for the progress bar 325 */ 326 public function get_progress_bar() { 327 global $PAGE; 328 329 $stage = self::STAGE_COMPLETE; 330 $currentstage = $this->stage->get_stage(); 331 $items = array(); 332 while ($stage > 0) { 333 $classes = array('backup_stage'); 334 if (floor($stage / 2) == $currentstage) { 335 $classes[] = 'backup_stage_next'; 336 } else if ($stage == $currentstage) { 337 $classes[] = 'backup_stage_current'; 338 } else if ($stage < $currentstage) { 339 $classes[] = 'backup_stage_complete'; 340 } 341 $item = array('text' => strlen(decbin($stage)).'. '.get_string('restorestage'.$stage, 'backup'), 'class' => join(' ', $classes)); 342 if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE && $stage > self::STAGE_DESTINATION) { 343 $item['link'] = new moodle_url($PAGE->url, array('restore' => $this->get_restoreid(), 'stage' => $stage)); 344 } 345 array_unshift($items, $item); 346 $stage = floor($stage / 2); 347 } 348 return $items; 349 } 350 351 /** 352 * Gets the name of this UI 353 * @return string 354 */ 355 public function get_name() { 356 return 'restore'; 357 } 358 359 /** 360 * Gets the first stage for this UI 361 * @return int STAGE_CONFIRM 362 */ 363 public function get_first_stage_id() { 364 return self::STAGE_CONFIRM; 365 } 366 367 /** 368 * Returns true if this stage has substages of which at least one needs to be displayed 369 * @return bool 370 */ 371 public function requires_substage() { 372 return ($this->stage->has_sub_stages() && !$this->stage->process()); 373 } 374 375 /** 376 * Displays this stage 377 * 378 * @throws base_ui_exception if the progress is wrong. 379 * @param core_backup_renderer $renderer 380 * @return string HTML code to echo 381 */ 382 public function display(core_backup_renderer $renderer) { 383 if ($this->progress < self::PROGRESS_SAVED) { 384 throw new base_ui_exception('backupsavebeforedisplay'); 385 } 386 return $this->stage->display($renderer); 387 } 388 } 389 390 /** 391 * Restore user interface exception. Modelled off the restore_exception class 392 * 393 * @package core_backup 394 * @copyright 2010 Sam Hemelryk 395 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 396 */ 397 class restore_ui_exception extends base_ui_exception {}
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |