3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * An abstract object that holds methods and attributes common to all grade_* objects defined here.
32 * Array of class variables that are not part of the DB table fields
33 * @var array $nonfields
35 var $nonfields = array('nonfields', 'required_fields');
38 * Array of required fields (keys) and their default values (values).
39 * @var array $required_fields
41 var $required_fields = array();
50 * The first time this grade_calculation was created.
51 * @var int $timecreated
56 * The last time this grade_calculation was modified.
57 * @var int $timemodified
62 * Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
63 * @param array $params an array with required parameters for this grade object.
64 * @param boolean $fetch Whether to fetch corresponding row from DB or not.
66 function grade_object($params=NULL, $fetch = true) {
67 if (!empty($params) and (is_array($params) or is_object($params))) {
68 if ($fetch and $data = $this->fetch($params)) {
69 grade_object
::set_properties($this, $data);
72 grade_object
::set_properties($this, $params);
78 * Finds and returns a grade_object instance based on params.
81 * @param array $params associative arrays varname=>value
82 * @return object grade_object instance or false if none found.
84 function fetch($params) {
85 error('Abstract method fetch() not overrided in '.get_class($this));
89 * Finds and returns all grade_object instances based on params.
92 * @param array $params associative arrays varname=>value
93 * @return array array of grade_object insatnces or false if none found.
95 function fetch_all($params) {
96 error('Abstract method fetch_all() not overrided in '.get_class($this));
100 * Factory method - uses the parameters to retrieve matching instance from the DB.
101 * @static final protected
102 * @return mixed object insatnce or false if not found
104 function fetch_helper($table, $classname, $params) {
105 // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-(
106 // in PHP5 we could do it much better
107 if ($instances = grade_object
::fetch_all_helper($table, $classname, $params)) {
108 if (count($instances) > 1) {
109 // we should not tolerate any errors here - proplems might appear later
110 error('Found more than one record in fetch() !');
112 return reset($instances);
119 * Factory method - uses the parameters to retrieve all matching instances from the DB.
120 * @static final protected
121 * @return mixed array of object instances or false if not found
123 function fetch_all_helper($table, $classname, $params) {
124 // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-(
125 // in PHP5 we could do it much better
126 $instance = new $classname();
128 $classvars = (array)$instance;
129 $params = (array)$params;
133 // remove incorrect params - warn developer if needed
134 foreach ($params as $var=>$value) {
135 if (!array_key_exists($var, $classvars) or in_array($var, $instance->nonfields
)) {
136 debugging("Incorrect property name $var for class $classname");
139 if (is_null($value)) {
140 $wheresql[] = " $var IS NULL ";
142 $value = addslashes($value);
143 $wheresql[] = " $var = '$value' ";
147 if (empty($wheresql)) {
150 $wheresql = implode("AND", $wheresql);
153 if ($datas = get_records_select($table, $wheresql, 'id')) {
155 foreach($datas as $data) {
156 $instance = new $classname();
157 grade_object
::set_properties($instance, $data);
158 $result[$instance->id
] = $instance;
168 * Updates this object in the Database, based on its object variables. ID must be set.
175 $this->timemodified
= time();
177 if (array_key_exists('usermodified', $this)) {
178 $this->usermodified
= $USER->id
;
181 // we need to do this to prevent infinite loops in addslashes_recursive - grade_item -> category ->grade_item
182 $data = new object();
183 foreach ($this as $var=>$value) {
184 if (!in_array($var, $this->nonfields
)) {
185 $data->$var = addslashes_recursive($value);
189 return update_record($this->table
, $data);
193 * Deletes this object from the database.
196 return delete_records($this->table
, 'id', $this->id
);
200 * Records this object in the Database, sets its id to the returned value, and returns that value.
201 * If successful this function also fetches the new object data from database and stores it
202 * in object properties.
203 * @return int PK ID if successful, false otherwise
208 if (!empty($this->id
)) {
209 debugging("Grade object already exists!");
213 $this->timecreated
= $this->timemodified
= time();
215 if (array_key_exists('usermodified', $this)) {
216 $this->usermodified
= $USER->id
;
219 // we need to do this to prevent infinite loops in addslashes_recursive - grade_item -> category ->grade_item
220 $data = new object();
221 foreach ($this as $var=>$value) {
222 if (!in_array($var, $this->nonfields
)) {
223 $data->$var = addslashes_recursive($value);
227 if (!$this->id
= insert_record($this->table
, addslashes_recursive($data))) {
228 debugging("Could not insert object into db");
232 // set all object properties from real db data
233 $this->update_from_db();
239 * Using this object's id field, fetches the matching record in the DB, and looks at
240 * each variable in turn. If the DB has different data, the db's data is used to update
241 * the object. This is different from the update() function, which acts on the DB record
242 * based on the object.
244 function update_from_db() {
245 if (empty($this->id
)) {
246 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.");
250 if (!$params = get_record($this->table
, 'id', $this->id
)) {
251 debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!");
255 grade_object
::set_properties($this, $params);
261 * Given an associated array or object, cycles through each key/variable
262 * and assigns the value to the corresponding variable in this object.
265 function set_properties(&$instance, $params) {
266 $classvars = (array)$instance;
267 foreach ($params as $var => $value) {
268 if (array_key_exists($var, $classvars)) {
269 $instance->$var = $value;