4 * Returns the aggregated or calculated course grade(s) in given course.
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
10 function grade_get_course_grades($courseid, $userid_or_ids=null) {
12 $grade_item = grade_item
::fetch_course_item($courseid);
14 if ($grade_item->needsupdate
) {
15 grade_regrade_final_grades($courseid);
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();
28 switch ($grade_item->gradetype
) {
32 case GRADE_TYPE_VALUE
:
44 if (empty($userid_or_ids)) {
47 } else if (is_array($userid_or_ids)) {
48 $userids = $userid_or_ids;
51 $userids = array($userid_or_ids);
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;
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
;
67 $grade->dategraded
= $grade_grades[$userid]->get_dategraded();
69 // create text representation of grade
70 if ($grade_item->needsupdate
) {
71 $grade->grade
= false;
72 $grade->str_grade
= get_string('error');
73 $grade->str_long_grade
= $grade->str_grade
;
75 } else if (is_null($grade->grade
)) {
76 $grade->str_grade
= '-';
77 $grade->str_long_grade
= $grade->str_grade
;
80 $grade->str_grade
= grade_format_gradevalue($grade->grade
, $grade_item);
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
;
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);
91 // create html representation of feedback
92 if (is_null($grade->feedback
)) {
93 $grade->str_feedback
= '';
95 $grade->str_feedback
= format_text($grade->feedback
, $grade->feedbackformat
);
98 $item->grades
[$userid] = $grade;
106 * Returns the aggregated or calculated course grade for the given user(s).
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
112 function grade_get_course_grade($userid, $courseid_or_ids=null) {
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')) {
119 $courseids = array_keys($courses);
120 return grade_get_course_grade($userid, $courseids);
122 if (!is_numeric($courseid_or_ids)) {
125 if (!$grades = grade_get_course_grade($userid, array($courseid_or_ids))) {
128 // only one grade - not array
129 $grade = reset($grades);
134 foreach ($courseid_or_ids as $courseid) {
135 $grade_item = grade_item
::fetch_course_item($courseid);
136 $course_items[$grade_item->courseid
] = $grade_item;
140 foreach ($course_items as $grade_item) {
141 if ($grade_item->needsupdate
) {
142 grade_regrade_final_grades($courseid);
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();
154 switch ($grade_item->gradetype
) {
155 case GRADE_TYPE_NONE
:
158 case GRADE_TYPE_VALUE
:
162 case GRADE_TYPE_TEXT
:
166 $item->gradepass
= 0;
169 $grade_grade = new grade_grade(array('userid'=>$userid, 'itemid'=>$grade_item->id
));
170 $grade_grade->grade_item
=& $grade_item;
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;
183 // create text representation of grade
184 if ($grade_item->needsupdate
) {
185 $grade->grade
= false;
186 $grade->str_grade
= get_string('error');
187 $grade->str_long_grade
= $grade->str_grade
;
189 } else if (is_null($grade->grade
)) {
190 $grade->str_grade
= '-';
191 $grade->str_long_grade
= $grade->str_grade
;
194 $grade->str_grade
= grade_format_gradevalue($grade->grade
, $grade_item);
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
;
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);
205 // create html representation of feedback
206 if (is_null($grade->feedback
)) {
207 $grade->str_feedback
= '';
209 $grade->str_feedback
= format_text($grade->feedback
, $grade->feedbackformat
);
212 $grades[$grade_item->courseid
] = $grade;
219 * Returns all grade items (including outcomes) or main item for a given activity identified by $cm object.
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
224 function grade_get_grade_items_for_activity($cm, $only_main_item=false) {
227 if (!isset($cm->modname
)) {
228 $cm = get_record_sql("SELECT cm.*, m.name, md.name as modname
229 FROM {$CFG->prefix}course_modules cm,
230 {$CFG->prefix}modules md,
231 WHERE cm.id = {$cm->id} AND md.id = cm.module");
235 if (empty($cm) or empty($cm->instance
) or empty($cm->course
)) {
236 debugging("Incorrect cm parameter in grade_get_grade_items_for_activity()!");
240 if ($only_main_item) {
241 return grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname
, 'iteminstance'=>$cm->instance
, 'courseid'=>$cm->course
, 'itemnumber'=>0));
243 return grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname
, 'iteminstance'=>$cm->instance
, 'courseid'=>$cm->course
));
248 * Returns whether or not user received grades in main grade item for given activity.
252 * @return bool True if graded false if user not graded yet
254 function grade_is_user_graded_in_activity($cm, $userid) {
256 $grade_items = grade_get_grade_items_for_activity($cm, true);
257 if (empty($grade_items)) {
261 $grade_item = reset($grade_items);
263 if ($grade_item->gradetype
== GRADE_TYPE_NONE
) {
267 if ($grade_item->needsupdate
) {
268 // activity items should never fail to regrade
269 grade_regrade_final_grades($grade_item->courseid
);
272 if (!$grade = $grade_item->get_final($userid)) {
276 if (is_null($grade->finalgrade
)) {
284 * Returns an array of activities (defined as $cm objects) which are gradeable from gradebook, outcomes are ignored.
286 * @param int $courseid If provided then restrict to one course.
287 * @param string $modulename If defined (could be 'forum', 'assignment' etc) then only that type are returned.
288 * @return array $cm objects
290 function grade_get_gradable_activities($courseid, $modulename='') {
293 if (empty($modulename)) {
294 if (!$modules = get_records('modules', 'visible', '1')) {
298 foreach ($modules as $module) {
299 if ($cms = grade_get_gradable_activities($courseid, $module->name
)) {
300 $result = $result +
$cms;
303 if (empty($result)) {
310 $sql = "SELECT cm.*, m.name, md.name as modname
311 FROM {$CFG->prefix}grade_items gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules md, {$CFG->prefix}$modulename m
312 WHERE gi.courseid = $courseid AND
313 gi.itemtype = 'mod' AND
314 gi.itemmodule = '$modulename' AND
315 gi.itemnumber = 0 AND
316 gi.gradetype != ".GRADE_TYPE_NONE
." AND
317 gi.iteminstance = cm.instance AND
318 cm.instance = m.id AND
319 md.name = '$modulename' AND
322 return get_records_sql($sql);