|
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/>. 00026 defined('MOODLE_INTERNAL') || die(); 00031 abstract class grade_object { 00032 public $table; 00033 00038 public $required_fields = array('id', 'timecreated', 'timemodified', 'hidden'); 00039 00045 public $optional_fields = array(); 00046 00051 public $id; 00052 00057 public $timecreated; 00058 00063 public $timemodified; 00064 00069 var $hidden = 0; 00070 00077 public function __construct($params=NULL, $fetch=true) { 00078 if (!empty($params) and (is_array($params) or is_object($params))) { 00079 if ($fetch) { 00080 if ($data = $this->fetch($params)) { 00081 grade_object::set_properties($this, $data); 00082 } else { 00083 grade_object::set_properties($this, $this->optional_fields);//apply defaults for optional fields 00084 grade_object::set_properties($this, $params); 00085 } 00086 00087 } else { 00088 grade_object::set_properties($this, $params); 00089 } 00090 00091 } else { 00092 grade_object::set_properties($this, $this->optional_fields);//apply defaults for optional fields 00093 } 00094 } 00095 00101 public function load_optional_fields() { 00102 global $DB; 00103 foreach ($this->optional_fields as $field=>$default) { 00104 if (property_exists($this, $field)) { 00105 continue; 00106 } 00107 if (empty($this->id)) { 00108 $this->$field = $default; 00109 } else { 00110 $this->$field = $DB->get_field($this->table, $field, array('id', $this->id)); 00111 } 00112 } 00113 } 00114 00122 public static function fetch($params) { 00123 throw new coding_exception('fetch() method needs to be overridden in each subclass of grade_object'); 00124 } 00125 00133 public static function fetch_all($params) { 00134 throw new coding_exception('fetch_all() method needs to be overridden in each subclass of grade_object'); 00135 } 00136 00142 protected static function fetch_helper($table, $classname, $params) { 00143 if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { 00144 if (count($instances) > 1) { 00145 // we should not tolerate any errors here - problems might appear later 00146 print_error('morethanonerecordinfetch','debug'); 00147 } 00148 return reset($instances); 00149 } else { 00150 return false; 00151 } 00152 } 00153 00159 public static function fetch_all_helper($table, $classname, $params) { 00160 $instance = new $classname(); 00161 00162 $classvars = (array)$instance; 00163 $params = (array)$params; 00164 00165 $wheresql = array(); 00166 $newparams = array(); 00167 00168 foreach ($params as $var=>$value) { 00169 if (!in_array($var, $instance->required_fields) and !array_key_exists($var, $instance->optional_fields)) { 00170 continue; 00171 } 00172 if (is_null($value)) { 00173 $wheresql[] = " $var IS NULL "; 00174 } else { 00175 $wheresql[] = " $var = ? "; 00176 $newparams[] = $value; 00177 } 00178 } 00179 00180 if (empty($wheresql)) { 00181 $wheresql = ''; 00182 } else { 00183 $wheresql = implode("AND", $wheresql); 00184 } 00185 00186 global $DB; 00187 $rs = $DB->get_recordset_select($table, $wheresql, $newparams); 00188 //returning false rather than empty array if nothing found 00189 if (!$rs->valid()) { 00190 $rs->close(); 00191 return false; 00192 } 00193 00194 $result = array(); 00195 foreach($rs as $data) { 00196 $instance = new $classname(); 00197 grade_object::set_properties($instance, $data); 00198 $result[$instance->id] = $instance; 00199 } 00200 $rs->close(); 00201 00202 return $result; 00203 } 00204 00210 public function update($source=null) { 00211 global $USER, $CFG, $DB; 00212 00213 if (empty($this->id)) { 00214 debugging('Can not update grade object, no id!'); 00215 return false; 00216 } 00217 00218 $data = $this->get_record_data(); 00219 00220 $DB->update_record($this->table, $data); 00221 00222 if (empty($CFG->disablegradehistory)) { 00223 unset($data->timecreated); 00224 $data->action = GRADE_HISTORY_UPDATE; 00225 $data->oldid = $this->id; 00226 $data->source = $source; 00227 $data->timemodified = time(); 00228 $data->loggeduser = $USER->id; 00229 $DB->insert_record($this->table.'_history', $data); 00230 } 00231 00232 $this->notify_changed(false); 00233 return true; 00234 } 00235 00241 public function delete($source=null) { 00242 global $USER, $CFG, $DB; 00243 00244 if (empty($this->id)) { 00245 debugging('Can not delete grade object, no id!'); 00246 return false; 00247 } 00248 00249 $data = $this->get_record_data(); 00250 00251 if ($DB->delete_records($this->table, array('id'=>$this->id))) { 00252 if (empty($CFG->disablegradehistory)) { 00253 unset($data->id); 00254 unset($data->timecreated); 00255 $data->action = GRADE_HISTORY_DELETE; 00256 $data->oldid = $this->id; 00257 $data->source = $source; 00258 $data->timemodified = time(); 00259 $data->loggeduser = $USER->id; 00260 $DB->insert_record($this->table.'_history', $data); 00261 } 00262 $this->notify_changed(true); 00263 return true; 00264 00265 } else { 00266 return false; 00267 } 00268 } 00269 00273 public function get_record_data() { 00274 $data = new stdClass(); 00275 00276 foreach ($this as $var=>$value) { 00277 if (in_array($var, $this->required_fields) or array_key_exists($var, $this->optional_fields)) { 00278 if (is_object($value) or is_array($value)) { 00279 debugging("Incorrect property '$var' found when inserting grade object"); 00280 } else { 00281 $data->$var = $value; 00282 } 00283 } 00284 } 00285 return $data; 00286 } 00287 00295 public function insert($source=null) { 00296 global $USER, $CFG, $DB; 00297 00298 if (!empty($this->id)) { 00299 debugging("Grade object already exists!"); 00300 return false; 00301 } 00302 00303 $data = $this->get_record_data(); 00304 00305 $this->id = $DB->insert_record($this->table, $data); 00306 00307 // set all object properties from real db data 00308 $this->update_from_db(); 00309 00310 $data = $this->get_record_data(); 00311 00312 if (empty($CFG->disablegradehistory)) { 00313 unset($data->timecreated); 00314 $data->action = GRADE_HISTORY_INSERT; 00315 $data->oldid = $this->id; 00316 $data->source = $source; 00317 $data->timemodified = time(); 00318 $data->loggeduser = $USER->id; 00319 $DB->insert_record($this->table.'_history', $data); 00320 } 00321 00322 $this->notify_changed(false); 00323 return $this->id; 00324 } 00325 00332 public function update_from_db() { 00333 if (empty($this->id)) { 00334 debugging("The object could not be used in its state to retrieve a matching record from the DB, because its id field is not set."); 00335 return false; 00336 } 00337 global $DB; 00338 if (!$params = $DB->get_record($this->table, array('id' => $this->id))) { 00339 debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!"); 00340 return false; 00341 } 00342 00343 grade_object::set_properties($this, $params); 00344 00345 return true; 00346 } 00347 00353 public static function set_properties(&$instance, $params) { 00354 $params = (array) $params; 00355 foreach ($params as $var => $value) { 00356 if (in_array($var, $instance->required_fields) or array_key_exists($var, $instance->optional_fields)) { 00357 $instance->$var = $value; 00358 } 00359 } 00360 } 00361 00369 function notify_changed($deleted) { 00370 } 00371 00376 function is_hidden() { 00377 return ($this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time())); 00378 } 00379 00384 function is_hiddenuntil() { 00385 return $this->hidden > 1; 00386 } 00387 00392 function get_hidden() { 00393 return $this->hidden; 00394 } 00395 00396 function set_hidden($hidden, $cascade=false) { 00397 $this->hidden = $hidden; 00398 $this->update(); 00399 } 00400 }