Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/ui/base_ui.class.php
Go to the documentation of this file.
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 
00035 abstract class base_ui {
00039     const PROGRESS_INTIAL = 0;
00040     const PROGRESS_PROCESSED = 1;
00041     const PROGRESS_SAVED = 2;
00042     const PROGRESS_EXECUTED = 3;
00047     protected $controller;
00052     protected $stage;
00057     protected $progress;
00062     protected $dependencychanges = 0;
00063 
00068     public function __construct($controller, array $params=null) {
00069         $this->controller = $controller;
00070         $this->progress = self::PROGRESS_INTIAL;
00071         $this->stage = $this->initialise_stage(null, $params);
00072         // Process UI event before to be safe
00073         $this->controller->process_ui_event();
00074     }
00078     public function destroy() {
00079 
00080         if ($this->controller) {
00081             $this->controller->destroy();
00082         }
00083         unset($this->stage);
00084 
00085     }
00093     abstract protected function initialise_stage($stage = null, array $params=null);
00098     public function process() {
00099         if ($this->progress >= self::PROGRESS_PROCESSED) {
00100             throw new backup_ui_exception('backupuialreadyprocessed');
00101         }
00102         $this->progress = self::PROGRESS_PROCESSED;
00103 
00104         if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > $this->get_first_stage_id()) {
00105             $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params());
00106             return false;
00107         }
00108 
00109         // Process the stage
00110         $processoutcome = $this->stage->process();
00111 
00112         if ($processoutcome !== false) {
00113             $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params());
00114         }
00115 
00116         // Process UI event after to check changes are valid
00117         $this->controller->process_ui_event();
00118         return $processoutcome;
00119     }
00127     public function save_controller() {
00128         if ($this->progress >= self::PROGRESS_SAVED) {
00129             throw new base_ui_exception('backupuialreadysaved');
00130         }
00131         $this->progress = self::PROGRESS_SAVED;
00132         // First enforce dependencies
00133         $this->enforce_dependencies();
00134         // Process UI event after to check any changes are valid
00135         $this->controller->process_ui_event();
00136         // Save the controller
00137         $this->controller->save_controller();
00138         return true;
00139     }
00146     public function display() {
00147         if ($this->progress < self::PROGRESS_SAVED) {
00148             throw new base_ui_exception('backupsavebeforedisplay');
00149         }
00150         return $this->stage->display();
00151     }
00156     public function get_tasks() {
00157         $plan = $this->controller->get_plan();
00158         $tasks = $plan->get_tasks();
00159         return $tasks;
00160     }
00165     public function get_stage() {
00166         return $this->stage->get_stage();
00167     }
00172     public function get_stage_name() {
00173         return $this->stage->get_name();
00174     }
00179     abstract public function get_uniqueid();
00184     abstract public function execute();
00189     protected function enforce_dependencies() {
00190         // Get the plan
00191         $plan = $this->controller->get_plan();
00192         // Get the tasks as a var so we can iterate by reference
00193         $tasks = $plan->get_tasks();
00194         $changes = 0;
00195         foreach ($tasks as &$task) {
00196             // Store as a var so we can iterate by reference
00197             $settings = $task->get_settings();
00198             foreach ($settings as &$setting) {
00199                 // Get all dependencies for iteration by reference
00200                 $dependencies = $setting->get_dependencies();
00201                 foreach ($dependencies as &$dependency) {
00202                     // Enforce each dependency
00203                     if ($dependency->enforce()) {
00204                         $changes++;
00205                     }
00206                 }
00207             }
00208         }
00209         // Store the number of settings that changed through enforcement
00210         $this->dependencychanges = $changes;
00211         return ($changes>0);
00212     }
00217     public function enforce_changed_dependencies() {
00218         return ($this->dependencychanges > 0);
00219     }
00224     public static function load_controller($uniqueid=false) {
00225         throw new coding_exception('load_controller() method needs to be overridden in each subclass of base_ui');
00226     }
00227 
00231     public function cancel_process() {
00232         global $PAGE;
00233         // Determine the appropriate URL to redirect the user to
00234         if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) {
00235             $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id'=>$PAGE->cm->id));
00236         } else {
00237             $relevanturl = new moodle_url('/course/view.php', array('id'=>$PAGE->course->id));
00238         }
00239         redirect($relevanturl);
00240     }
00241 
00246     abstract public function get_progress_bar();
00251     public function get_format() {
00252         return $this->controller->get_format();
00253     }
00258     public function get_type() {
00259         return $this->controller->get_type();
00260     }
00261     public function get_controller() {
00262         return $this->controller;
00263     }
00268     public function get_controller_id() {
00269         return $this->controller->get_id();
00270     }
00276     public function get_setting($name, $default = false) {
00277         try {
00278             return $this->controller->get_plan()->get_setting($name);
00279         } catch (Exception $e) {
00280             debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
00281             return $default;
00282         }
00283     }
00290     public function get_setting_value($name, $default = false) {
00291         try {
00292             return $this->controller->get_plan()->get_setting($name)->get_value();
00293         } catch (Exception $e) {
00294             debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
00295             return $default;
00296         }
00297     }
00298 
00299     abstract public function get_name();
00300 
00301     abstract public function get_first_stage_id();
00302 }
00303 
00307 class base_ui_exception extends backup_exception {}
 All Data Structures Namespaces Files Functions Variables Enumerations