[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * This file contains the backup user interface class 20 * 21 * @package moodlecore 22 * @copyright 2010 Sam Hemelryk 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 /** 27 * This is the backup user interface class 28 * 29 * The backup user interface class manages the user interface and backup for 30 * Moodle. 31 * 32 * @copyright 2010 Sam Hemelryk 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 abstract class base_ui { 36 /** 37 * The progress of this instance of the backup ui class 38 */ 39 const PROGRESS_INTIAL = 0; 40 const PROGRESS_PROCESSED = 1; 41 const PROGRESS_SAVED = 2; 42 const PROGRESS_EXECUTED = 3; 43 /** 44 * The controller 45 * @var backup_controller|restore_controller 46 */ 47 protected $controller; 48 /** 49 * The current stage 50 * @var base_ui_stage 51 */ 52 protected $stage; 53 /** 54 * The current progress of the UI 55 * @var int One of self::PROGRESS_* 56 */ 57 protected $progress; 58 /** 59 * The number of changes made by dependency enforcement 60 * @var int 61 */ 62 protected $dependencychanges = 0; 63 64 /** 65 * Yay for constructors 66 * @param backup_controller $controller 67 */ 68 public function __construct($controller, array $params=null) { 69 $this->controller = $controller; 70 $this->progress = self::PROGRESS_INTIAL; 71 $this->stage = $this->initialise_stage(null, $params); 72 // Process UI event before to be safe 73 $this->controller->process_ui_event(); 74 } 75 /** 76 * Destorys the backup controller and the loaded stage. 77 */ 78 public function destroy() { 79 80 if ($this->controller) { 81 $this->controller->destroy(); 82 } 83 unset($this->stage); 84 85 } 86 /** 87 * Intialises what ever stage is requested. If none are requested we check 88 * params for 'stage' and default to initial 89 * 90 * @param int|null $stage The desired stage to intialise or null for the default 91 * @return base_ui_stage 92 */ 93 abstract protected function initialise_stage($stage = null, array $params=null); 94 /** 95 * This processes the current stage of the backup 96 * @return bool 97 */ 98 public function process() { 99 if ($this->progress >= self::PROGRESS_PROCESSED) { 100 throw new backup_ui_exception('backupuialreadyprocessed'); 101 } 102 $this->progress = self::PROGRESS_PROCESSED; 103 104 if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > $this->get_first_stage_id()) { 105 $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params()); 106 return false; 107 } 108 109 // Process the stage 110 $processoutcome = $this->stage->process(); 111 112 if ($processoutcome !== false) { 113 $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params()); 114 } 115 116 // Process UI event after to check changes are valid 117 $this->controller->process_ui_event(); 118 return $processoutcome; 119 } 120 /** 121 * Saves the backup controller. 122 * 123 * Once this has been called nothing else can be changed in the controller. 124 * 125 * @return bool 126 */ 127 public function save_controller() { 128 if ($this->progress >= self::PROGRESS_SAVED) { 129 throw new base_ui_exception('backupuialreadysaved'); 130 } 131 $this->progress = self::PROGRESS_SAVED; 132 // First enforce dependencies 133 $this->enforce_dependencies(); 134 // Process UI event after to check any changes are valid 135 $this->controller->process_ui_event(); 136 // Save the controller 137 $this->controller->save_controller(); 138 return true; 139 } 140 /** 141 * Displays the UI for the backup! 142 * 143 * @param core_backup_renderer $renderer 144 * @return string HTML code to echo 145 */ 146 public function display(core_backup_renderer $renderer) { 147 if ($this->progress < self::PROGRESS_SAVED) { 148 throw new base_ui_exception('backupsavebeforedisplay'); 149 } 150 return $this->stage->display($renderer); 151 } 152 /** 153 * Gets all backup tasks from the controller 154 * @return array Array of backup_task 155 */ 156 public function get_tasks() { 157 $plan = $this->controller->get_plan(); 158 $tasks = $plan->get_tasks(); 159 return $tasks; 160 } 161 /** 162 * Gets the stage we are on 163 * @return int 164 */ 165 public function get_stage() { 166 return $this->stage->get_stage(); 167 } 168 /** 169 * Gets the name of the stage we are on 170 * @return string 171 */ 172 public function get_stage_name() { 173 return $this->stage->get_name(); 174 } 175 /** 176 * Gets the backup id from the controller 177 * @return string 178 */ 179 abstract public function get_uniqueid(); 180 /** 181 * Executes the backup plan 182 * @return bool 183 */ 184 abstract public function execute(); 185 /** 186 * Enforces dependencies on all settings. Call before save 187 * @return bool True if dependencies were enforced and changes were made 188 */ 189 protected function enforce_dependencies() { 190 // Get the plan 191 $plan = $this->controller->get_plan(); 192 // Get the tasks as a var so we can iterate by reference 193 $tasks = $plan->get_tasks(); 194 $changes = 0; 195 foreach ($tasks as &$task) { 196 // Store as a var so we can iterate by reference 197 $settings = $task->get_settings(); 198 foreach ($settings as &$setting) { 199 // Get all dependencies for iteration by reference 200 $dependencies = $setting->get_dependencies(); 201 foreach ($dependencies as &$dependency) { 202 // Enforce each dependency 203 if ($dependency->enforce()) { 204 $changes++; 205 } 206 } 207 } 208 } 209 // Store the number of settings that changed through enforcement 210 $this->dependencychanges = $changes; 211 return ($changes>0); 212 } 213 /** 214 * Returns true if enforce_dependencies changed any settings 215 * @return bool 216 */ 217 public function enforce_changed_dependencies() { 218 return ($this->dependencychanges > 0); 219 } 220 /** 221 * Loads the backup controller if we are tracking one 222 * @return backup_controller|false 223 */ 224 public static function load_controller($uniqueid=false) { 225 throw new coding_exception('load_controller() method needs to be overridden in each subclass of base_ui'); 226 } 227 228 /** 229 * Cancels the current backup/restore and redirects the user back to the relevant place 230 */ 231 public function cancel_process() { 232 global $PAGE; 233 // Determine the appropriate URL to redirect the user to 234 if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) { 235 $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id'=>$PAGE->cm->id)); 236 } else { 237 $relevanturl = new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)); 238 } 239 redirect($relevanturl); 240 } 241 242 /** 243 * Gets an array of progress bar items that can be displayed through the backup renderer. 244 * @return array Array of items for the progress bar 245 */ 246 abstract public function get_progress_bar(); 247 /** 248 * Gets the format for the backup 249 * @return int 250 */ 251 public function get_format() { 252 return $this->controller->get_format(); 253 } 254 /** 255 * Gets the type of the backup 256 * @return int 257 */ 258 public function get_type() { 259 return $this->controller->get_type(); 260 } 261 public function get_controller() { 262 return $this->controller; 263 } 264 /** 265 * Gets the ID used in creating the controller. Relates to course/section/cm 266 * @return int 267 */ 268 public function get_controller_id() { 269 return $this->controller->get_id(); 270 } 271 /** 272 * Gets the requested setting 273 * @param string $name 274 * @return mixed 275 */ 276 public function get_setting($name, $default = false) { 277 try { 278 return $this->controller->get_plan()->get_setting($name); 279 } catch (Exception $e) { 280 debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER); 281 return $default; 282 } 283 } 284 /** 285 * Gets the value for the requested setting 286 * 287 * @param string $name 288 * @return mixed 289 */ 290 public function get_setting_value($name, $default = false) { 291 try { 292 return $this->controller->get_plan()->get_setting($name)->get_value(); 293 } catch (Exception $e) { 294 debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER); 295 return $default; 296 } 297 } 298 299 abstract public function get_name(); 300 301 abstract public function get_first_stage_id(); 302 } 303 304 /** 305 * Backup user interface exception. Modelled off the backup_exception class 306 */ 307 class base_ui_exception extends backup_exception {}
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |