Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/completion/data_object.php
Go to the documentation of this file.
00001 <?php
00002 
00004 //                                                                       //
00005 // NOTICE OF COPYRIGHT                                                   //
00006 //                                                                       //
00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00008 //          http://moodle.com                                            //
00009 //                                                                       //
00010 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
00011 //                                                                       //
00012 // This program is free software; you can redistribute it and/or modify  //
00013 // it under the terms of the GNU General Public License as published by  //
00014 // the Free Software Foundation; either version 2 of the License, or     //
00015 // (at your option) any later version.                                   //
00016 //                                                                       //
00017 // This program is distributed in the hope that it will be useful,       //
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00020 // GNU General Public License for more details:                          //
00021 //                                                                       //
00022 //          http://www.gnu.org/copyleft/gpl.html                         //
00023 //                                                                       //
00025 
00030 abstract class data_object {
00035     public $table;
00036 
00041     public $required_fields = array('id');
00042 
00048     public $optional_fields = array();
00049 
00054     public $id;
00055 
00062     public function __construct($params=NULL, $fetch=true) {
00063         if (!empty($params) and (is_array($params) or is_object($params))) {
00064             if ($fetch) {
00065                 if ($data = $this->fetch($params)) {
00066                     self::set_properties($this, $data);
00067                 } else {
00068                     self::set_properties($this, $this->optional_fields);//apply defaults for optional fields
00069                     self::set_properties($this, $params);
00070                 }
00071 
00072             } else {
00073                 self::set_properties($this, $params);
00074             }
00075 
00076         } else {
00077             self::set_properties($this, $this->optional_fields);//apply defaults for optional fields
00078         }
00079     }
00080 
00086     public function load_optional_fields() {
00087         global $DB;
00088         foreach ($this->optional_fields as $field=>$default) {
00089             if (property_exists($this, $field)) {
00090                 continue;
00091             }
00092             if (empty($this->id)) {
00093                 $this->$field = $default;
00094             } else {
00095                 $this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
00096             }
00097         }
00098     }
00099 
00107     public static function fetch($params) {
00108         throw new coding_exception('fetch() method needs to be overridden in each subclass of data_object');
00109     }
00110 
00117     public static function fetch_all($params) {
00118         throw new coding_exception('fetch_all() method needs to be overridden in each subclass of data_object');
00119     }
00120 
00126     protected static function fetch_helper($table, $classname, $params) {
00127         if ($instances = self::fetch_all_helper($table, $classname, $params)) {
00128             if (count($instances) > 1) {
00129                 // we should not tolerate any errors here - problems might appear later
00130                 print_error('morethanonerecordinfetch','debug');
00131             }
00132             return reset($instances);
00133         } else {
00134             return false;
00135         }
00136     }
00137 
00143     public static function fetch_all_helper($table, $classname, $params) {
00144         $instance = new $classname();
00145 
00146         $classvars = (array)$instance;
00147         $params    = (array)$params;
00148 
00149         $wheresql = array();
00150 
00151         foreach ($params as $var=>$value) {
00152             if (!in_array($var, $instance->required_fields) and !array_key_exists($var, $instance->optional_fields)) {
00153                 continue;
00154             }
00155             if (is_null($value)) {
00156                 $wheresql[] = " $var IS NULL ";
00157             } else {
00158                 $wheresql[] = " $var = ? ";
00159                 $params[] = $value;
00160             }
00161         }
00162 
00163         if (empty($wheresql)) {
00164             $wheresql = '';
00165         } else {
00166             $wheresql = implode("AND", $wheresql);
00167         }
00168 
00169         global $DB;
00170         if ($datas = $DB->get_records_select($table, $wheresql, $params)) {
00171 
00172             $result = array();
00173             foreach($datas as $data) {
00174                 $instance = new $classname();
00175                 self::set_properties($instance, $data);
00176                 $result[$instance->id] = $instance;
00177             }
00178             return $result;
00179 
00180         } else {
00181 
00182             return false;
00183         }
00184     }
00185 
00190     public function update() {
00191         global $DB;
00192 
00193         if (empty($this->id)) {
00194             debugging('Can not update data object, no id!');
00195             return false;
00196         }
00197 
00198         $data = $this->get_record_data();
00199 
00200         $DB->update_record($this->table, $data);
00201 
00202         $this->notify_changed(false);
00203         return true;
00204     }
00205 
00210     public function delete() {
00211         global $DB;
00212 
00213         if (empty($this->id)) {
00214             debugging('Can not delete data object, no id!');
00215             return false;
00216         }
00217 
00218         $data = $this->get_record_data();
00219 
00220         if ($DB->delete_records($this->table, array('id'=>$this->id))) {
00221             $this->notify_changed(true);
00222             return true;
00223 
00224         } else {
00225             return false;
00226         }
00227     }
00228 
00232     public function get_record_data() {
00233         $data = new stdClass();
00234 
00235         foreach ($this as $var=>$value) {
00236             if (in_array($var, $this->required_fields) or array_key_exists($var, $this->optional_fields)) {
00237                 if (is_object($value) or is_array($value)) {
00238                     debugging("Incorrect property '$var' found when inserting data object");
00239                 } else {
00240                     $data->$var = $value;
00241                 }
00242             }
00243         }
00244         return $data;
00245     }
00246 
00253     public function insert() {
00254         global $DB;
00255 
00256         if (!empty($this->id)) {
00257             debugging("Data object already exists!");
00258             return false;
00259         }
00260 
00261         $data = $this->get_record_data();
00262 
00263         $this->id = $DB->insert_record($this->table, $data);
00264 
00265         // set all object properties from real db data
00266         $this->update_from_db();
00267 
00268         $this->notify_changed(false);
00269         return $this->id;
00270     }
00271 
00278     public function update_from_db() {
00279         if (empty($this->id)) {
00280             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.");
00281             return false;
00282         }
00283         global $DB;
00284         if (!$params = $DB->get_record($this->table, array('id' => $this->id))) {
00285             debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!");
00286             return false;
00287         }
00288 
00289         self::set_properties($this, $params);
00290 
00291         return true;
00292     }
00293 
00299     public static function set_properties(&$instance, $params) {
00300         $params = (array) $params;
00301         foreach ($params as $var => $value) {
00302             if (in_array($var, $instance->required_fields) or array_key_exists($var, $instance->optional_fields)) {
00303                 $instance->$var = $value;
00304             }
00305         }
00306     }
00307 
00315     function notify_changed($deleted) {
00316     }
00317 }
 All Data Structures Namespaces Files Functions Variables Enumerations