3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-2003 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 object $params an object with named parameters for this grade item.
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) && (is_array($params) ||
is_object($params))) {
68 $this->assign_to_this($params);
71 $records = $this->fetch_all_using_this();
72 if ($records && count($records) > 0) {
73 $this->assign_to_this(current($records));
80 * Updates this object in the Database, based on its object variables. ID must be set.
87 $this->timemodified
= time();
89 if (empty($this->usermodified
)) {
90 $this->usermodified
= $USER->id
;
93 return update_record($this->table
, $this);
97 * Deletes this object from the database.
100 return delete_records($this->table
, 'id', $this->id
);
104 * Records this object in the Database, sets its id to the returned value, and returns that value.
105 * @return int PK ID if successful, false otherwise
110 if (!empty($this->id
)) { // Object already exists, so let's do an update instead
114 $this->timecreated
= $this->timemodified
= time();
116 if (empty($this->usermodified
)) {
117 $this->usermodified
= $USER->id
;
120 $clonethis = clone($this);
122 // Unset non-set fields
123 foreach ($clonethis as $var => $val) {
125 unset($clonethis->$var);
129 return $this->id
= insert_record($this->table
, $clonethis, true);
133 * Using this object's id field, fetches the matching record in the DB, and looks at
134 * each variable in turn. If the DB has different data, the db's data is used to update
135 * the object. This is different from the update() function, which acts on the DB record
136 * based on the object.
138 function update_from_db() {
139 if (empty($this->id
)) {
140 debugging("The object could not be used in its state to retrieve a matching record from the DB, because it's id field is not set.");
143 $class = get_class($this);
144 $object = new $class(array('id' => $this->id
));
145 foreach ($object as $var => $val) {
146 if ($this->$var != $val) {
155 * Uses the variables of this object to retrieve all matching objects from the DB.
156 * @return array $objects
158 function fetch_all_using_this() {
159 $variables = get_object_vars($this);
162 foreach ($variables as $var => $value) {
163 if (!empty($value) && !in_array($var, $this->nonfields
)) {
164 $wheresql .= " $var = '$value' AND ";
169 $wheresql = substr($wheresql, 0, strrpos($wheresql, 'AND'));
171 $objects = get_records_select($this->table
, $wheresql, 'id');
173 if (!empty($objects)) {
174 $full_objects = array();
176 // Convert the stdClass objects returned by the get_records_select method into proper objects
177 $classname = get_class($this);
178 foreach ($objects as $id => $stdobject) {
179 $full_objects[$id] = new $classname($stdobject, false);
181 return $full_objects;
189 * Given an associated array or object, cycles through each key/variable
190 * and assigns the value to the corresponding variable in this object.
192 function assign_to_this($params) {
193 foreach ($params as $param => $value) {
194 if (in_object_vars($param, $this)) {
195 $this->$param = $value;