MDL-9506 Elements of the array returned by grade_category::get_children are now index...
[moodle-pu.git] / lib / grade / grade_object.php
blob111d642db64013e6fb4e742eac946d580cde8e9d
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
11 // //
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. //
16 // //
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: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * An abstract object that holds methods and attributes common to all grade_* objects defined here.
28 * @abstract
30 class grade_object {
31 /**
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');
37 /**
38 * Array of required fields (keys) and their default values (values).
39 * @var array $required_fields
41 var $required_fields = array();
43 /**
44 * The PK.
45 * @var int $id
47 var $id;
49 /**
50 * The first time this grade_calculation was created.
51 * @var int $timecreated
53 var $timecreated;
55 /**
56 * The last time this grade_calculation was modified.
57 * @var int $timemodified
59 var $timemodified;
61 /**
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.
65 */
66 function grade_object($params=NULL, $fetch = true) {
67 if (!empty($params) && (is_array($params) || is_object($params))) {
68 $this->assign_to_this($params);
70 if ($fetch) {
71 $records = $this->fetch_all_using_this();
72 if ($records && count($records) > 0) {
73 $this->assign_to_this(current($records));
79 /**
80 * Updates this object in the Database, based on its object variables. ID must be set.
82 * @return boolean
84 function update() {
85 global $USER;
87 $this->timemodified = time();
89 if (empty($this->usermodified)) {
90 $this->usermodified = $USER->id;
93 return update_record($this->table, $this);
96 /**
97 * Deletes this object from the database.
99 function delete() {
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
107 function insert() {
108 global $USER;
110 if (!empty($this->id)) { // Object already exists, so let's do an update instead
111 $this->update();
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) {
124 if (!isset($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.");
141 return false;
142 } else {
143 $class = get_class($this);
144 $object = new $class(array('id' => $this->id));
145 foreach ($object as $var => $val) {
146 if ($this->$var != $val) {
147 $this->$var = $val;
151 return true;
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);
160 $wheresql = '';
162 foreach ($variables as $var => $value) {
163 if (!empty($value) && !in_array($var, $this->nonfields)) {
164 $wheresql .= " $var = '$value' AND ";
168 // Trim trailing 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;
182 } else {
183 return $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;