forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querylib.php
341 lines (289 loc) · 12.1 KB
/
querylib.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Returns the aggregated or calculated course grade(s) in given course.
* @public
* @param int $courseid id of course
* @param int $userid_or_ids optional id of the graded user or array of ids; if userid not used, returns only information about grade_item
* @return information about course grade item scaleid, name, grade and locked status, etc. + user grades
*/
function grade_get_course_grades($courseid, $userid_or_ids=null) {
$grade_item = grade_item::fetch_course_item($courseid);
if ($grade_item->needsupdate) {
grade_regrade_final_grades($courseid);
}
$item = new stdClass();
$item->scaleid = $grade_item->scaleid;
$item->name = $grade_item->get_name();
$item->grademin = $grade_item->grademin;
$item->grademax = $grade_item->grademax;
$item->gradepass = $grade_item->gradepass;
$item->locked = $grade_item->is_locked();
$item->hidden = $grade_item->is_hidden();
$item->grades = array();
switch ($grade_item->gradetype) {
case GRADE_TYPE_NONE:
continue;
case GRADE_TYPE_VALUE:
$item->scaleid = 0;
break;
case GRADE_TYPE_TEXT:
$item->scaleid = 0;
$item->grademin = 0;
$item->grademax = 0;
$item->gradepass = 0;
break;
}
if (empty($userid_or_ids)) {
$userids = array();
} else if (is_array($userid_or_ids)) {
$userids = $userid_or_ids;
} else {
$userids = array($userid_or_ids);
}
if ($userids) {
$grade_grades = grade_grade::fetch_users_grades($grade_item, $userids, true);
foreach ($userids as $userid) {
$grade_grades[$userid]->grade_item =& $grade_item;
$grade = new stdClass();
$grade->grade = $grade_grades[$userid]->finalgrade;
$grade->locked = $grade_grades[$userid]->is_locked();
$grade->hidden = $grade_grades[$userid]->is_hidden();
$grade->overridden = $grade_grades[$userid]->overridden;
$grade->feedback = $grade_grades[$userid]->feedback;
$grade->feedbackformat = $grade_grades[$userid]->feedbackformat;
$grade->usermodified = $grade_grades[$userid]->usermodified;
$grade->dategraded = $grade_grades[$userid]->get_dategraded();
// create text representation of grade
if ($grade_item->needsupdate) {
$grade->grade = false;
$grade->str_grade = get_string('error');
$grade->str_long_grade = $grade->str_grade;
} else if (is_null($grade->grade)) {
$grade->str_grade = '-';
$grade->str_long_grade = $grade->str_grade;
} else {
$grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
if ($grade_item->gradetype == GRADE_TYPE_SCALE or $grade_item->get_displaytype() != GRADE_DISPLAY_TYPE_REAL) {
$grade->str_long_grade = $grade->str_grade;
} else {
$a = new stdClass();
$a->grade = $grade->str_grade;
$a->max = grade_format_gradevalue($grade_item->grademax, $grade_item);
$grade->str_long_grade = get_string('gradelong', 'grades', $a);
}
}
// create html representation of feedback
if (is_null($grade->feedback)) {
$grade->str_feedback = '';
} else {
$grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
}
$item->grades[$userid] = $grade;
}
}
return $item;
}
/**
* Returns the aggregated or calculated course grade for the given user(s).
* @public
* @param int $userid
* @param int $courseid optional id of course or array of ids, empty means all uses courses (returns array if not present)
* @return mixed grade info or grades array including item info, false if error
*/
function grade_get_course_grade($userid, $courseid_or_ids=null) {
if (!is_array($courseid_or_ids)) {
if (empty($courseid_or_ids)) {
if (!$courses = enrol_get_users_courses($userid)) {
return false;
}
$courseids = array_keys($courses);
return grade_get_course_grade($userid, $courseids);
}
if (!is_numeric($courseid_or_ids)) {
return false;
}
if (!$grades = grade_get_course_grade($userid, array($courseid_or_ids))) {
return false;
} else {
// only one grade - not array
$grade = reset($grades);
return $grade;
}
}
foreach ($courseid_or_ids as $courseid) {
$grade_item = grade_item::fetch_course_item($courseid);
$course_items[$grade_item->courseid] = $grade_item;
}
$grades = array();
foreach ($course_items as $grade_item) {
if ($grade_item->needsupdate) {
grade_regrade_final_grades($courseid);
}
$item = new stdClass();
$item->scaleid = $grade_item->scaleid;
$item->name = $grade_item->get_name();
$item->grademin = $grade_item->grademin;
$item->grademax = $grade_item->grademax;
$item->gradepass = $grade_item->gradepass;
$item->locked = $grade_item->is_locked();
$item->hidden = $grade_item->is_hidden();
switch ($grade_item->gradetype) {
case GRADE_TYPE_NONE:
continue;
case GRADE_TYPE_VALUE:
$item->scaleid = 0;
break;
case GRADE_TYPE_TEXT:
$item->scaleid = 0;
$item->grademin = 0;
$item->grademax = 0;
$item->gradepass = 0;
break;
}
$grade_grade = new grade_grade(array('userid'=>$userid, 'itemid'=>$grade_item->id));
$grade_grade->grade_item =& $grade_item;
$grade = new stdClass();
$grade->grade = $grade_grade->finalgrade;
$grade->locked = $grade_grade->is_locked();
$grade->hidden = $grade_grade->is_hidden();
$grade->overridden = $grade_grade->overridden;
$grade->feedback = $grade_grade->feedback;
$grade->feedbackformat = $grade_grade->feedbackformat;
$grade->usermodified = $grade_grade->usermodified;
$grade->dategraded = $grade_grade->get_dategraded();
$grade->item = $item;
// create text representation of grade
if ($grade_item->needsupdate) {
$grade->grade = false;
$grade->str_grade = get_string('error');
$grade->str_long_grade = $grade->str_grade;
} else if (is_null($grade->grade)) {
$grade->str_grade = '-';
$grade->str_long_grade = $grade->str_grade;
} else {
$grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
if ($grade_item->gradetype == GRADE_TYPE_SCALE or $grade_item->get_displaytype() != GRADE_DISPLAY_TYPE_REAL) {
$grade->str_long_grade = $grade->str_grade;
} else {
$a = new stdClass();
$a->grade = $grade->str_grade;
$a->max = grade_format_gradevalue($grade_item->grademax, $grade_item);
$grade->str_long_grade = get_string('gradelong', 'grades', $a);
}
}
// create html representation of feedback
if (is_null($grade->feedback)) {
$grade->str_feedback = '';
} else {
$grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
}
$grades[$grade_item->courseid] = $grade;
}
return $grades;
}
/**
* Returns all grade items (including outcomes) or main item for a given activity identified by $cm object.
*
* @param object $cm A course module object (preferably with modname property)
* @return mixed - array of grade item instances (one if $only_main_item true), false if error or not found
*/
function grade_get_grade_items_for_activity($cm, $only_main_item=false) {
global $CFG, $DB;
if (!isset($cm->modname)) {
$params = array($cm->id);
$cm = $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
FROM {course_modules} cm,
{modules} md,
WHERE cm.id = ? AND md.id = cm.module", $params);
}
if (empty($cm) or empty($cm->instance) or empty($cm->course)) {
debugging("Incorrect cm parameter in grade_get_grade_items_for_activity()!");
return false;
}
if ($only_main_item) {
return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0));
} else {
return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
}
}
/**
* Returns whether or not user received grades in main grade item for given activity.
*
* @param object $cm
* @param int $userid
* @return bool True if graded false if user not graded yet
*/
function grade_is_user_graded_in_activity($cm, $userid) {
$grade_items = grade_get_grade_items_for_activity($cm, true);
if (empty($grade_items)) {
return false;
}
$grade_item = reset($grade_items);
if ($grade_item->gradetype == GRADE_TYPE_NONE) {
return false;
}
if ($grade_item->needsupdate) {
// activity items should never fail to regrade
grade_regrade_final_grades($grade_item->courseid);
}
if (!$grade = $grade_item->get_final($userid)) {
return false;
}
if (is_null($grade->finalgrade)) {
return false;
}
return true;
}
/**
* Returns an array of activities (defined as $cm objects) which are gradeable from gradebook, outcomes are ignored.
*
* @param int $courseid If provided then restrict to one course.
* @param string $modulename If defined (could be 'forum', 'assignment' etc) then only that type are returned.
* @return array $cm objects
*/
function grade_get_gradable_activities($courseid, $modulename='') {
global $CFG, $DB;
if (empty($modulename)) {
if (!$modules = $DB->get_records('modules', array('visible' => '1'))) {
return false;
}
$result = array();
foreach ($modules as $module) {
if ($cms = grade_get_gradable_activities($courseid, $module->name)) {
$result = $result + $cms;
}
}
if (empty($result)) {
return false;
} else {
return $result;
}
}
$params = array($courseid, $modulename, GRADE_TYPE_NONE, $modulename);
$sql = "SELECT cm.*, m.name, md.name as modname
FROM {grade_items} gi, {course_modules} cm, {modules} md, {$modulename} m
WHERE gi.courseid = ? AND
gi.itemtype = 'mod' AND
gi.itemmodule = ? AND
gi.itemnumber = 0 AND
gi.gradetype != ? AND
gi.iteminstance = cm.instance AND
cm.instance = m.id AND
md.name = ? AND
md.id = cm.module";
return $DB->get_records_sql($sql, $params);
}