MDL-12822 removing some unused grade functions (obsoleted by format grade function...
[moodle.git] / grade / querylib.php
CommitLineData
b9f49659 1<?php //$Id$
2
3/**
4 * Returns the aggregated or calculated course grade for the given user(s).
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();
19 $item->itemnumber = $grade_item->itemnumber;
20 $item->scaleid = $grade_item->scaleid;
21 $item->name = $grade_item->get_name();
22 $item->grademin = $grade_item->grademin;
23 $item->grademax = $grade_item->grademax;
24 $item->gradepass = $grade_item->gradepass;
25 $item->locked = $grade_item->is_locked();
26 $item->hidden = $grade_item->is_hidden();
27 $item->grades = array();
28
29 switch ($grade_item->gradetype) {
30 case GRADE_TYPE_NONE:
31 continue;
32
33 case GRADE_TYPE_VALUE:
34 $item->scaleid = 0;
35 break;
36
37 case GRADE_TYPE_TEXT:
38 $item->scaleid = 0;
39 $item->grademin = 0;
40 $item->grademax = 0;
41 $item->gradepass = 0;
42 break;
43 }
44
45 if (empty($userid_or_ids)) {
46 $userids = array();
47
48 } else if (is_array($userid_or_ids)) {
49 $userids = $userid_or_ids;
50
51 } else {
52 $userids = array($userid_or_ids);
53 }
54
55 if ($userids) {
56 $grade_grades = grade_grade::fetch_users_grades($grade_item, $userids, true);
57 foreach ($userids as $userid) {
58 $grade_grades[$userid]->grade_item =& $grade_item;
59
60 $grade = new object();
61 $grade->grade = $grade_grades[$userid]->finalgrade;
62 $grade->locked = $grade_grades[$userid]->is_locked();
63 $grade->hidden = $grade_grades[$userid]->is_hidden();
64 $grade->overridden = $grade_grades[$userid]->overridden;
65 $grade->feedback = $grade_grades[$userid]->feedback;
66 $grade->feedbackformat = $grade_grades[$userid]->feedbackformat;
67 $grade->usermodified = $grade_grades[$userid]->usermodified;
68 $grade->datesubmitted = $grade_grades[$userid]->get_datesubmitted();
69 $grade->dategraded = $grade_grades[$userid]->get_dategraded();
70
71 // create text representation of grade
72 if ($grade_item->needsupdate) {
73 $grade->grade = false;
74 $grade->str_grade = get_string('error');
75
76 } else if (is_null($grade->grade)) {
77 $grade->str_grade = '-';
78
79 } else {
80 $grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
81 }
82
83 // create html representation of feedback
84 if (is_null($grade->feedback)) {
85 $grade->str_feedback = '';
86 } else {
87 $grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
88 }
89
90 $item->grades[$userid] = $grade;
91 }
92 }
93
94 return $item;
95}
96
97/**
98 * Returns all grade items (including outcomes) or main item for a given activity identified by $cm object.
99 *
100 * @param object $cm A course module object (preferably with modname property)
101 * @return mixed - array of grade item instances (one if $only_main_item true), false if error or not found
102 */
103function grade_get_grade_items_for_activity($cm, $only_main_item=false) {
104 global $CFG;
105
106 if (!isset($cm->modname)) {
107 $cm = get_record_sql("SELECT cm.*, m.name, md.name as modname
108 FROM {$CFG->prefix}course_modules cm,
109 {$CFG->prefix}modules md,
110 WHERE cm.id = {$cm->id} AND md.id = cm.module");
111 }
112
113
114 if (empty($cm) or empty($cm->instance) or empty($cm->course)) {
115 debugging("Incorrect cm parameter in grade_get_grade_items_for_activity()!");
116 return false;
117 }
118
119 if ($only_main_item) {
120 return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0));
121 } else {
122 return grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
123 }
124}
125
126/**
127 * Returns whether or not user received grades in main grade item for given activity.
128 *
129 * @param object $cm
130 * @param int $userid
131 * @return bool True if graded false if user not graded yet
132 */
133function grade_is_user_graded_in_activity($cm, $userid) {
134
135 $grade_items = grade_get_grade_items_for_activity($cm, true);
136 if (empty($grade_items)) {
137 return false;
138 }
139
140 $grade_item = reset($grade_items);
141
142 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
143 return false;
144 }
145
146 if ($grade_item->needsupdate) {
147 // activity items should never fail to regrade
148 grade_regrade_final_grades($grade_item->courseid);
149 }
150
151 if (!$grade = $grade_item->get_final($userid)) {
152 return false;
153 }
154
155 if (is_null($grade->finalgrade)) {
156 return false;
157 }
158
159 return true;
160}
161
162/**
163 * Returns an array of activities (defined as $cm objects) which are gradeable from gradebook, outcomes are ignored.
164 *
165 * @param int $courseid If provided then restrict to one course.
166 * @param string $modulename If defined (could be 'forum', 'assignment' etc) then only that type are returned.
167 * @return array $cm objects
168 */
169function grade_get_gradable_activities($courseid, $modulename='') {
170 global $CFG;
171
172 if (empty($modulename)) {
173 if (!$modules = get_records('modules', 'visible', '1')) {
174 return false;
175 }
176 $result = array();
177 foreach ($modules as $module) {
178 if ($cms = grade_get_gradable_activities($courseid, $module->name)) {
179 $result = $result + $cms;
180 }
181 }
182 if (empty($result)) {
183 return false;
184 } else {
185 return $result;
186 }
187 }
188
189 $sql = "SELECT cm.*, m.name, md.name as modname
190 FROM {$CFG->prefix}grade_items gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules md, {$CFG->prefix}$modulename m
191 WHERE gi.courseid = $courseid AND
192 gi.itemtype = 'mod' AND
193 gi.itemmodule = '$modulename' AND
194 gi.itemnumber = 0 AND
195 gi.gradetype != ".GRADE_TYPE_NONE." AND
196 gi.iteminstance = cm.instance AND
197 cm.instance = m.id AND
198 md.name = '$modulename' AND
199 md.id = cm.module";
200
201 return get_records_sql($sql);
202}
203?>