Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/plan/base_plan.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 
00030 abstract class base_plan implements checksumable, executable {
00031 
00032     protected $name;      // One simple name for identification purposes
00033     protected $settings;  // One array of (accumulated from tasks) base_setting elements
00034     protected $tasks;     // One array of base_task elements
00035     protected $results;   // One array of results received from tasks
00036 
00037     protected $built;     // Flag to know if one plan has been built
00038 
00042     public function __construct($name) {
00043         $this->name = $name;
00044         $this->settings = array();
00045         $this->tasks    = array();
00046         $this->results  = array();
00047         $this->built = false;
00048     }
00049 
00050     public function get_name() {
00051         return $this->name;
00052     }
00053 
00054     public function add_task($task) {
00055         if (! $task instanceof base_task) {
00056             throw new base_plan_exception('wrong_base_task_specified');
00057         }
00058         $this->tasks[] = $task;
00059         // link the task with the plan
00060         $task->set_plan($this);
00061         // Append task settings to plan array, if not present, for comodity
00062         foreach ($task->get_settings() as $key => $setting) {
00063             if (!in_array($setting, $this->settings)) {
00064                 $this->settings[] = $setting;
00065             }
00066         }
00067     }
00068 
00069     public function get_tasks() {
00070         return $this->tasks;
00071     }
00072 
00073     public function add_result($result) {
00074         $this->results = array_merge($this->results, $result);
00075     }
00076 
00077     public function get_results() {
00078         return $this->results;
00079     }
00080 
00081     public function get_settings() {
00082         return $this->settings;
00083     }
00084 
00092     public function get_setting($name) {
00093         $result = null;
00094         foreach ($this->settings as $key => $setting) {
00095             if ($setting->get_name() == $name) {
00096                 if ($result != null) {
00097                     throw new base_plan_exception('multiple_settings_by_name_found', $name);
00098                 } else {
00099                     $result = $setting;
00100                 }
00101             }
00102         }
00103         if (!$result) {
00104             throw new base_plan_exception('setting_by_name_not_found', $name);
00105         }
00106         return $result;
00107     }
00108 
00112     public function setting_exists($name) {
00113         try {
00114             $this->get_setting($name);
00115             return true;
00116         } catch (base_plan_exception $e) {
00117             // Nothing to do
00118         }
00119         return false;
00120     }
00121 
00122 
00128     public abstract function build();
00129 
00130     public function is_checksum_correct($checksum) {
00131         return $this->calculate_checksum() === $checksum;
00132     }
00133 
00134     public function calculate_checksum() {
00135         // Let's do it using name and tasks (settings are part of tasks)
00136         return md5($this->name . '-' . backup_general_helper::array_checksum_recursive($this->tasks));
00137     }
00138 
00142     public function execute() {
00143         if (!$this->built) {
00144             throw new base_plan_exception('base_plan_not_built');
00145         }
00146         foreach ($this->tasks as $task) {
00147             $task->build();
00148             $task->execute();
00149         }
00150     }
00151 
00155     public function destroy() {
00156         // Before reseting anything, call destroy recursively
00157         foreach ($this->tasks as $task) {
00158             $task->destroy();
00159         }
00160         foreach ($this->settings as $setting) {
00161             $setting->destroy();
00162         }
00163         // Everything has been destroyed recursively, now we can reset safely
00164         $this->tasks = array();
00165         $this->settings = array();
00166     }
00167 }
00168 
00169 
00170 /*
00171  * Exception class used by all the @base_plan stuff
00172  */
00173 class base_plan_exception extends moodle_exception {
00174 
00175     public function __construct($errorcode, $a=NULL, $debuginfo=null) {
00176         parent::__construct($errorcode, '', '', $a, $debuginfo);
00177     }
00178 }
 All Data Structures Namespaces Files Functions Variables Enumerations