mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
grade_object.php
202 lines (172 loc) · 6.88 KB
/
grade_object.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* An abstract object that holds methods and attributes common to all grade_* objects defined here.
* @abstract
*/
class grade_object {
/**
* Array of class variables that are not part of the DB table fields
* @var array $nonfields
*/
var $nonfields = array('nonfields', 'required_fields');
/**
* Array of required fields (keys) and their default values (values).
* @var array $required_fields
*/
var $required_fields = array();
/**
* The PK.
* @var int $id
*/
var $id;
/**
* The first time this grade_calculation was created.
* @var int $timecreated
*/
var $timecreated;
/**
* The last time this grade_calculation was modified.
* @var int $timemodified
*/
var $timemodified;
/**
* Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
* @param object $params an object with named parameters for this grade item.
* @param boolean $fetch Whether to fetch corresponding row from DB or not.
*/
function grade_object($params=NULL, $fetch = true) {
if (!empty($params) && (is_array($params) || is_object($params))) {
$this->assign_to_this($params);
if ($fetch) {
$records = $this->fetch_all_using_this();
if ($records && count($records) > 0) {
$this->assign_to_this(current($records));
}
}
}
}
/**
* Updates this object in the Database, based on its object variables. ID must be set.
*
* @return boolean
*/
function update() {
global $USER;
$this->timemodified = time();
if (empty($this->usermodified)) {
$this->usermodified = $USER->id;
}
return update_record($this->table, addslashes_recursive($this));
}
/**
* Deletes this object from the database.
*/
function delete() {
return delete_records($this->table, 'id', $this->id);
}
/**
* Records this object in the Database, sets its id to the returned value, and returns that value.
* @return int PK ID if successful, false otherwise
*/
function insert() {
global $USER;
if (!empty($this->id)) { // Object already exists, so let's do an update instead
debugging("Grade object already exists!");
return false;
}
$this->timecreated = $this->timemodified = time();
if (empty($this->usermodified)) {
$this->usermodified = $USER->id;
}
$clonethis = fullclone($this);
// Unset non-set fields
foreach ($clonethis as $var => $val) {
if (!isset($val)) {
unset($clonethis->$var);
}
}
return $this->id = insert_record($this->table, addslashes_recursive($clonethis), true);
}
/**
* Using this object's id field, fetches the matching record in the DB, and looks at
* each variable in turn. If the DB has different data, the db's data is used to update
* the object. This is different from the update() function, which acts on the DB record
* based on the object.
*/
function update_from_db() {
if (empty($this->id)) {
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.");
return false;
} else {
$class = get_class($this);
$object = new $class(array('id' => $this->id));
foreach ($object as $var => $val) {
if ($this->$var != $val) {
$this->$var = $val;
}
}
}
return true;
}
/**
* Uses the variables of this object to retrieve all matching objects from the DB.
* @return array $objects
*/
function fetch_all_using_this() {
$variables = get_object_vars($this);
$wheresql = '';
foreach ($variables as $var => $value) {
if (!empty($value) && !in_array($var, $this->nonfields)) {
$wheresql .= " $var = '$value' AND ";
}
}
// Trim trailing AND
$wheresql = substr($wheresql, 0, strrpos($wheresql, 'AND'));
$objects = get_records_select($this->table, $wheresql, 'id');
if (!empty($objects)) {
$full_objects = array();
// Convert the stdClass objects returned by the get_records_select method into proper objects
$classname = get_class($this);
foreach ($objects as $id => $stdobject) {
$full_objects[$id] = new $classname($stdobject, false);
}
return $full_objects;
} else {
return $objects;
}
}
/**
* Given an associated array or object, cycles through each key/variable
* and assigns the value to the corresponding variable in this object.
*/
function assign_to_this($params) {
foreach ($params as $param => $value) {
if (in_object_vars($param, $this)) {
$this->$param = $value;
}
}
}
}
?>