|
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 00030 abstract class base_task implements checksumable, executable, loggable { 00031 00032 protected $name; // One simple name for identification purposes 00033 protected $plan; // Plan this is part of 00034 protected $settings; // One array of base_setting elements to define this task 00035 protected $steps; // One array of base_step elements 00036 00037 protected $built; // Flag to know if one task has been built 00038 protected $executed; // Flag to know if one task has been executed 00039 00043 public function __construct($name, $plan = null) { 00044 if (!is_null($plan) && !($plan instanceof base_plan)) { 00045 throw new base_task_exception('wrong_base_plan_specified'); 00046 } 00047 $this->name = $name; 00048 $this->plan = $plan; 00049 $this->settings = array(); 00050 $this->steps = array(); 00051 $this->built = false; 00052 $this->executed = false; 00053 if (!is_null($plan)) { // Add the task to the plan if specified 00054 $plan->add_task($this); 00055 } 00056 } 00057 00058 public function get_name() { 00059 return $this->name; 00060 } 00061 00062 public function get_steps() { 00063 return $this->steps; 00064 } 00065 00066 public function get_settings() { 00067 return $this->settings; 00068 } 00069 00070 public function get_setting($name) { 00071 // First look in task settings 00072 $result = null; 00073 foreach ($this->settings as $key => $setting) { 00074 if ($setting->get_name() == $name) { 00075 if ($result != null) { 00076 throw new base_task_exception('multiple_settings_by_name_found', $name); 00077 } else { 00078 $result = $setting; 00079 } 00080 } 00081 } 00082 if ($result) { 00083 return $result; 00084 } else { 00085 // Fallback to plan settings 00086 return $this->plan->get_setting($name); 00087 } 00088 } 00089 00090 public function setting_exists($name) { 00091 return $this->plan->setting_exists($name); 00092 } 00093 00094 public function get_setting_value($name) { 00095 return $this->get_setting($name)->get_value(); 00096 } 00097 00098 public function get_courseid() { 00099 return $this->plan->get_courseid(); 00100 } 00101 00102 public function get_basepath() { 00103 return $this->plan->get_basepath(); 00104 } 00105 00106 public function get_taskbasepath() { 00107 return $this->get_basepath(); 00108 } 00109 00110 public function get_logger() { 00111 return $this->plan->get_logger(); 00112 } 00113 00114 public function log($message, $level, $a = null, $depth = null, $display = false) { 00115 backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger()); 00116 } 00117 00118 public function add_step($step) { 00119 if (! $step instanceof base_step) { 00120 throw new base_task_exception('wrong_base_step_specified'); 00121 } 00122 // link the step with the task 00123 $step->set_task($this); 00124 $this->steps[] = $step; 00125 } 00126 00127 public function set_plan($plan) { 00128 if (! $plan instanceof base_plan) { 00129 throw new base_task_exception('wrong_base_plan_specified'); 00130 } 00131 $this->plan = $plan; 00132 $this->define_settings(); // Settings are defined when plan & task are linked 00133 } 00134 00139 public abstract function build(); 00140 00145 public function execute() { 00146 if (!$this->built) { 00147 throw new base_task_exception('base_task_not_built', $this->name); 00148 } 00149 if ($this->executed) { 00150 throw new base_task_exception('base_task_already_executed', $this->name); 00151 } 00152 foreach ($this->steps as $step) { 00153 $result = $step->execute(); 00154 // If step returns array, it will be forwarded to plan 00155 // (TODO: shouldn't be array but proper result object) 00156 if (is_array($result) and !empty($result)) { 00157 $this->plan->add_result($result); 00158 } 00159 } 00160 // Mark as executed if any step has been executed 00161 if (!empty($this->steps)) { 00162 $this->executed = true; 00163 } 00164 } 00165 00169 public function destroy() { 00170 // Before reseting anything, call destroy recursively 00171 foreach ($this->steps as $step) { 00172 $step->destroy(); 00173 } 00174 foreach ($this->settings as $setting) { 00175 $setting->destroy(); 00176 } 00177 // Everything has been destroyed recursively, now we can reset safely 00178 $this->steps = array(); 00179 $this->setting = array(); 00180 $this->plan = null; 00181 } 00182 00183 public function is_checksum_correct($checksum) { 00184 return $this->calculate_checksum() === $checksum; 00185 } 00186 00187 public function calculate_checksum() { 00188 // Let's do it using name and settings and steps 00189 return md5($this->name . '-' . 00190 backup_general_helper::array_checksum_recursive($this->settings) . 00191 backup_general_helper::array_checksum_recursive($this->steps)); 00192 } 00193 00194 // Protected API starts here 00195 00201 protected abstract function define_settings(); 00202 00203 protected function add_setting($setting) { 00204 if (! $setting instanceof base_setting) { 00205 throw new base_setting_exception('wrong_base_setting_specified'); 00206 } 00207 $this->settings[] = $setting; 00208 } 00209 } 00210 00211 /* 00212 * Exception class used by all the @base_task stuff 00213 */ 00214 class base_task_exception extends moodle_exception { 00215 00216 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 00217 parent::__construct($errorcode, '', '', $a, $debuginfo); 00218 } 00219 }