MDL-18167 towareds reimplementing of win installer support
[moodle.git] / grade / querylib.php
CommitLineData
b9f49659 1<?php //$Id$
2
3/**
028553e7 4 * Returns the aggregated or calculated course grade(s) in given course.
b9f49659 5 * @public
6 * @param int $courseid id of course
7 * @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
8 * @return information about course grade item scaleid, name, grade and locked status, etc. + user grades
9 */
10function grade_get_course_grades($courseid, $userid_or_ids=null) {
11
12 $grade_item = grade_item::fetch_course_item($courseid);
13
14 if ($grade_item->needsupdate) {
15 grade_regrade_final_grades($courseid);
16 }
17
18 $item = new object();
b9f49659 19 $item->scaleid = $grade_item->scaleid;
20 $item->name = $grade_item->get_name();
21 $item->grademin = $grade_item->grademin;
22 $item->grademax = $grade_item->grademax;
23 $item->gradepass = $grade_item->gradepass;
24 $item->locked = $grade_item->is_locked();
25 $item->hidden = $grade_item->is_hidden();
26 $item->grades = array();
27
28 switch ($grade_item->gradetype) {
29 case GRADE_TYPE_NONE:
30 continue;
31
32 case GRADE_TYPE_VALUE:
33 $item->scaleid = 0;
34 break;
35
36 case GRADE_TYPE_TEXT:
37 $item->scaleid = 0;
38 $item->grademin = 0;
39 $item->grademax = 0;
40 $item->gradepass = 0;
41 break;
42 }
43
44 if (empty($userid_or_ids)) {
45 $userids = array();
46
47 } else if (is_array($userid_or_ids)) {
48 $userids = $userid_or_ids;
49
50 } else {
51 $userids = array($userid_or_ids);
52 }
53
54 if ($userids) {
55 $grade_grades = grade_grade::fetch_users_grades($grade_item, $userids, true);
56 foreach ($userids as $userid) {
57 $grade_grades[$userid]->grade_item =& $grade_item;
58
59 $grade = new object();
60 $grade->grade = $grade_grades[$userid]->finalgrade;
61 $grade->locked = $grade_grades[$userid]->is_locked();
62 $grade->hidden = $grade_grades[$userid]->is_hidden();
63 $grade->overridden = $grade_grades[$userid]->overridden;
64 $grade->feedback = $grade_grades[$userid]->feedback;
65 $grade->feedbackformat = $grade_grades[$userid]->feedbackformat;
66 $grade->usermodified = $grade_grades[$userid]->usermodified;
b9f49659 67 $grade->dategraded = $grade_grades[$userid]->get_dategraded();
68
69 // create text representation of grade
70 if ($grade_item->needsupdate) {
85a0a69f 71 $grade->grade = false;
72 $grade->str_grade = get_string('error');
73 $grade->str_long_grade = $grade->str_grade;
b9f49659 74
75 } else if (is_null($grade->grade)) {
85a0a69f 76 $grade->str_grade = '-';
77 $grade->str_long_grade = $grade->str_grade;
b9f49659 78
79 } else {
80 $grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
85a0a69f 81 if ($grade_item->gradetype == GRADE_TYPE_SCALE or $grade_item->get_displaytype() != GRADE_DISPLAY_TYPE_REAL) {
82 $grade->str_long_grade = $grade->str_grade;
83 } else {
84 $a = new object();
85 $a->grade = $grade->str_grade;
86 $a->max = grade_format_gradevalue($grade_item->grademax, $grade_item);
87 $grade->str_long_grade = get_string('gradelong', 'grades', $a);
88 }
b9f49659 89 }
90
91 // create html representation of feedback
92 if (is_null($grade->feedback)) {
93 $grade->str_feedback = '';
94 } else {
95 $grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
96 }
97
98 $item->grades[$userid] = $grade;
99 }
100 }
101
102 return $item;
103}
104
028553e7 105/**
106 * Returns the aggregated or calculated course grade for the given user(s).
107 * @public
108 * @param int $userid
109 * @param int $courseid optional id of course or array of ids, empty means all uses courses (returns array if not present)
110 * @return mixed grade info or grades array including item info, false if error
111 */
112function grade_get_course_grade($userid, $courseid_or_ids=null) {
113
114 if (!is_array($courseid_or_ids)) {
115 if (empty($courseid_or_ids)) {
116 if (!$courses = get_my_courses($userid, $sort='visible DESC,sortorder ASC', 'id')) {
117 return false;
118 }
119 $courseids = array_keys($courses);
120 return grade_get_course_grade($userid, $courseids);
121 }
122 if (!is_numeric($courseid_or_ids)) {
123 return false;
124 }
125 if (!$grades = grade_get_course_grade($userid, array($courseid_or_ids))) {
126 return false;
127 } else {
128 // only one grade - not array
129 $grade = reset($grades);
130 return $grade;
131 }
132 }
133
134 foreach ($courseid_or_ids as $courseid) {
135 $grade_item = grade_item::fetch_course_item($courseid);
136 $course_items[$grade_item->courseid] = $grade_item;
137 }
138
139 $grades = array();
140 foreach ($course_items as $grade_item) {
141 if ($grade_item->needsupdate) {
142 grade_regrade_final_grades($courseid);
143 }
144
145 $item = new object();
146 $item->scaleid = $grade_item->scaleid;
147 $item->name = $grade_item->get_name();
148 $item->grademin = $grade_item->grademin;
149 $item->grademax = $grade_item->grademax;
150 $item->gradepass = $grade_item->gradepass;
151 $item->locked = $grade_item->is_locked();
152 $item->hidden = $grade_item->is_hidden();
153
154 switch ($grade_item->gradetype) {
155 case GRADE_TYPE_NONE:
156 continue;
157
158 case GRADE_TYPE_VALUE:
159 $item->scaleid = 0;
160 break;
161
162 case GRADE_TYPE_TEXT:
163 $item->scaleid = 0;
164 $item->grademin = 0;
165 $item->grademax = 0;
166 $item->gradepass = 0;
167 break;
168 }
169 $grade_grade = new grade_grade(array('userid'=>$userid, 'itemid'=>$grade_item->id));
170 $grade_grade->grade_item =& $grade_item;
171
172 $grade = new object();
173 $grade->grade = $grade_grade->finalgrade;
174 $grade->locked = $grade_grade->is_locked();
175 $grade->hidden = $grade_grade->is_hidden();
176 $grade->overridden = $grade_grade->overridden;
177 $grade->feedback = $grade_grade->feedback;
178 $grade->feedbackformat = $grade_grade->feedbackformat;
179 $grade->usermodified = $grade_grade->usermodified;
180 $grade->dategraded = $grade_grade->get_dategraded();
181 $grade->item = $item;
182
183 // create text representation of grade
184 if ($grade_item->needsupdate) {
85a0a69f 185 $grade->grade = false;
186 $grade->str_grade = get_string('error');
187 $grade->str_long_grade = $grade->str_grade;
028553e7 188
189 } else if (is_null($grade->grade)) {
85a0a69f 190 $grade->str_grade = '-';
191 $grade->str_long_grade = $grade->str_grade;
028553e7 192
193 } else {
194 $grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
85a0a69f 195 if ($grade_item->gradetype == GRADE_TYPE_SCALE or $grade_item->get_displaytype() != GRADE_DISPLAY_TYPE_REAL) {
196 $grade->str_long_grade = $grade->str_grade;
197 } else {
198 $a = new object();
199 $a->grade = $grade->str_grade;
200 $a->max = grade_format_gradevalue($grade_item->grademax, $grade_item);
201 $grade->str_long_grade = get_string('gradelong', 'grades', $a);
202 }
028553e7 203 }
204
205 // create html representation of feedback
206 if (is_null($grade->feedback)) {
207 $grade->str_feedback = '';
208 } else {
209 $grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
210 }
211
212 $grades[$grade_item->courseid] = $grade;
213 }
214
215 return $grades;
216}
217
b9f49659 218/**
219 * Returns all grade items (including outcomes) or main item for a given activity identified by $cm object.
220 *
221 * @param object $cm A course module object (preferably with modname property)
222 * @return mixed - array of grade item instances (one if $only_main_item true), false if error or not found
223 */
224function grade_get_grade_items_for_activity($cm, $only_main_item=false) {
d24832f9 225 global $CFG, $DB;
b9f49659 226
227 if (!isset($cm->modname)) {
d24832f9 228 $params = array($cm->id);
229 $cm = $DB->get_record_sql("SELECT cm.*, m.name, md.name as modname
230 FROM {course_modules} cm,
231 {modules} md,
232 WHERE cm.id = ? AND md.id = cm.module", $params);
b9f49659 233 }
234
235
236 if (empty($cm) or empty($cm->instance) or empty($cm->course)) {
237 debugging("Incorrect cm parameter in grade_get_grade_items_for_activity()!");
238 return false;
239 }
240
241 if ($only_main_item) {
242 return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0));
243 } else {
244 return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
245 }
246}
247
248/**
249 * Returns whether or not user received grades in main grade item for given activity.
250 *
251 * @param object $cm
252 * @param int $userid
253 * @return bool True if graded false if user not graded yet
254 */
255function grade_is_user_graded_in_activity($cm, $userid) {
256
257 $grade_items = grade_get_grade_items_for_activity($cm, true);
258 if (empty($grade_items)) {
259 return false;
260 }
261
262 $grade_item = reset($grade_items);
263
264 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
265 return false;
266 }
267
268 if ($grade_item->needsupdate) {
269 // activity items should never fail to regrade
270 grade_regrade_final_grades($grade_item->courseid);
271 }
272
273 if (!$grade = $grade_item->get_final($userid)) {
274 return false;
275 }
276
277 if (is_null($grade->finalgrade)) {
278 return false;
279 }
280
281 return true;
282}
283
284/**
285 * Returns an array of activities (defined as $cm objects) which are gradeable from gradebook, outcomes are ignored.
286 *
287 * @param int $courseid If provided then restrict to one course.
288 * @param string $modulename If defined (could be 'forum', 'assignment' etc) then only that type are returned.
289 * @return array $cm objects
290 */
291function grade_get_gradable_activities($courseid, $modulename='') {
d24832f9 292 global $CFG, $DB;
b9f49659 293
294 if (empty($modulename)) {
d24832f9 295 if (!$modules = $DB->get_records('modules', array('visible' => '1'))) {
b9f49659 296 return false;
297 }
298 $result = array();
299 foreach ($modules as $module) {
300 if ($cms = grade_get_gradable_activities($courseid, $module->name)) {
301 $result = $result + $cms;
302 }
303 }
304 if (empty($result)) {
305 return false;
306 } else {
307 return $result;
308 }
309 }
310
d24832f9 311 $params = array($courseid, $modulename, GRADE_TYPE_NONE, $modulename);
b9f49659 312 $sql = "SELECT cm.*, m.name, md.name as modname
d24832f9 313 FROM {grade_items} gi, {course_modules} cm, {modules} md, {$modulename} m
314 WHERE gi.courseid = ? AND
b9f49659 315 gi.itemtype = 'mod' AND
d24832f9 316 gi.itemmodule = ? AND
b9f49659 317 gi.itemnumber = 0 AND
d24832f9 318 gi.gradetype != ? AND
b9f49659 319 gi.iteminstance = cm.instance AND
320 cm.instance = m.id AND
d24832f9 321 md.name = ? AND
b9f49659 322 md.id = cm.module";
323
d24832f9 324 return $DB->get_records_sql($sql, $params);
b9f49659 325}
d24832f9 326?>