forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grade_scale.php
320 lines (285 loc) · 11.7 KB
/
grade_scale.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards 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 //
// //
///////////////////////////////////////////////////////////////////////////
require_once('grade_object.php');
/**
* Class representing a grade scale. It is responsible for handling its DB representation,
* modifying and returning its metadata.
*/
class grade_scale extends grade_object {
/**
* DB Table (used by grade_object).
* @var string $table
*/
public $table = 'scale';
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
public $required_fields = array('id', 'courseid', 'userid', 'name', 'scale', 'description', 'descriptionformat', 'timemodified');
/**
* The course this scale belongs to.
* @var int $courseid
*/
public $courseid;
public $userid;
/**
* The name of the scale.
* @var string $name
*/
public $name;
/**
* The items in this scale.
* @var array $scale_items
*/
public $scale_items = array();
/**
* A string representation of the scale items (a comma-separated list).
* @var string $scale
*/
public $scale;
/**
* A description for this scale.
* @var string $description
*/
public $description;
/**
* Finds and returns a grade_scale instance based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return object grade_scale instance or false if none found.
*/
public static function fetch($params) {
return grade_object::fetch_helper('scale', 'grade_scale', $params);
}
/**
* Finds and returns all grade_scale instances based on params.
* @static
*
* @param array $params associative arrays varname=>value
* @return array array of grade_scale instances or false if none found.
*/
public static function fetch_all($params) {
return grade_object::fetch_all_helper('scale', 'grade_scale', $params);
}
/**
* Records this object in the Database, sets its id to the returned value, and returns that value.
* If successful this function also fetches the new object data from database and stores it
* in object properties.
* @param string $source from where was the object inserted (mod/forum, manual, etc.)
* @return int PK ID if successful, false otherwise
*/
public function insert($source=null) {
$this->timecreated = time();
$this->timemodified = time();
return parent::insert($source);
}
/**
* In addition to update() it also updates grade_outcomes_courses if needed
* @param string $source from where was the object inserted
* @return boolean success
*/
public function update($source=null) {
$this->timemodified = time();
return parent::update($source);
}
/**
* Deletes this outcome from the database.
* @param string $source from where was the object deleted (mod/forum, manual, etc.)
* @return boolean success
*/
public function delete($source=null) {
global $DB;
if (parent::delete($source)) {
$context = get_context_instance(CONTEXT_SYSTEM);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'grade_scale', $this->id);
foreach ($files as $file) {
$file->delete();
}
return true;
}
return false;
}
/**
* Returns the most descriptive field for this object. This is a standard method used
* when we do not know the exact type of an object.
* @return string name
*/
public function get_name() {
return format_string($this->name);
}
/**
* Loads the scale's items into the $scale_items array.
* There are three ways to achieve this:
* 1. No argument given: The $scale string is already loaded and exploded to an array of items.
* 2. A string is given: A comma-separated list of items is exploded into an array of items.
* 3. An array of items is given and saved directly as the array of items for this scale.
*
* @param mixed $items Could be null, a string or an array. The method behaves differently for each case.
* @return array The resulting array of scale items or null if the method failed to produce one.
*/
public function load_items($items=NULL) {
if (empty($items)) {
$this->scale_items = explode(',', $this->scale);
} elseif (is_array($items)) {
$this->scale_items = $items;
} else {
$this->scale_items = explode(',', $items);
}
// Trim whitespace around each value
foreach ($this->scale_items as $key => $val) {
$this->scale_items[$key] = trim($val);
}
return $this->scale_items;
}
/**
* Compacts (implodes) the array of items in $scale_items into a comma-separated string, $scale.
* There are three ways to achieve this:
* 1. No argument given: The $scale_items array is already loaded and imploded to a string of items.
* 2. An array is given and is imploded into a string of items.
* 3. A string of items is given and saved directly as the $scale variable.
* NOTE: This method is the exact reverse of load_items, and their input/output should be interchangeable. However,
* because load_items() trims the whitespace around the items, when the string is reconstructed these whitespaces will
* be missing. This is not an issue, but should be kept in mind when comparing the two strings.
*
* @param mixed $items Could be null, a string or an array. The method behaves differently for each case.
* @return array The resulting string of scale items or null if the method failed to produce one.
*/
public function compact_items($items=NULL) {
if (empty($items)) {
$this->scale = implode(',', $this->scale_items);
} elseif (is_array($items)) {
$this->scale = implode(',', $items);
} else {
$this->scale = $items;
}
return $this->scale;
}
/**
* When called on a loaded scale object (with a valid id) and given a float grade between
* the grademin and grademax, this method returns the scale item that falls closest to the
* float given (which is usually an average of several grades on a scale). If the float falls
* below 1 but above 0, it will be rounded up to 1.
* @param float $grade
* @return string
*/
public function get_nearest_item($grade) {
global $DB;
// Obtain nearest scale item from average
$scales_array = $DB->get_records('scale', array('id' => $this->id));
$scale = $scales_array[$this->id];
$scales = explode(",", $scale->scale);
// this could be a 0 when summed and rounded, e.g, 1, no grade, no grade, no grade
if ($grade < 1) {
$grade = 1;
}
return $scales[$grade-1];
}
/**
* Static function returning all global scales
* @return object
*/
public function fetch_all_global() {
return grade_scale::fetch_all(array('courseid'=>0));
}
/**
* Static function returning all local course scales
* @return object
*/
public static function fetch_all_local($courseid) {
return grade_scale::fetch_all(array('courseid'=>$courseid));
}
/**
* Checks if scale can be deleted.
* @return boolean
*/
public function can_delete() {
return !$this->is_used();
}
/**
* Returns if scale used anywhere - activities, grade items, outcomes, etc.
* @return bool
*/
public function is_used() {
global $DB;
global $CFG;
// count grade items excluding the
$params = array($this->id);
$sql = "SELECT COUNT(id) FROM {grade_items} WHERE scaleid = ? AND outcomeid IS NULL";
if ($DB->count_records_sql($sql, $params)) {
return true;
}
// count outcomes
$sql = "SELECT COUNT(id) FROM {grade_outcomes} WHERE scaleid = ?";
if ($DB->count_records_sql($sql, $params)) {
return true;
}
$legacy_mods = false;
if ($mods = $DB->get_records('modules', array('visible' => 1))) {
foreach ($mods as $mod) {
//Check cm->name/lib.php exists
if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) {
include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
$function_name = $mod->name.'_scale_used_anywhere';
$old_function_name = $mod->name.'_scale_used';
if (function_exists($function_name)) {
if ($function_name($this->id)) {
return true;
}
} else if (function_exists($old_function_name)) {
$legacy_mods = true;
debugging('Please notify the developer of module "'.$mod->name.'" that new function module_scale_used_anywhere() should be implemented.', DEBUG_DEVELOPER);
break;
}
}
}
}
// some mods are missing the new xxx_scale_used_anywhere() - use the really slow old way
if ($legacy_mods) {
if (!empty($this->courseid)) {
if (course_scale_used($this->courseid,$this->id)) {
return true;
}
} else {
$courses = array();
if (site_scale_used($this->id,$courses)) {
return true;
}
}
}
return false;
}
/**
* Returns the formatted grade description with URL's converted
* @return string
*/
public function get_description() {
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$options = new stdClass;
$options->noclean = true;
$description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade_scale', $this->id);
return format_text($description, $this->descriptionformat, $options);
}
}