MDL-9633 Added report class for user report
[moodle.git] / grade / report / user / lib.php
CommitLineData
f7998fbc 1<?php // $Id$
2/**
3 * File in which the user_report class is defined.
4 * @package gradebook
5 */
6
7require_once($CFG->dirroot . '/grade/report/lib.php');
8require_once($CFG->libdir.'/tablelib.php');
9
10/**
11 * Class providing an API for the user report building and displaying.
12 * @uses grade_report
13 * @package gradebook
14 */
15class grade_report_user extends grade_report {
16
17 /**
18 * The user.
19 * @var object $user
20 */
21 var $user;
22
23 /**
24 * A flexitable to hold the data.
25 * @var object $table
26 */
27 var $table;
28
29 /**
30 * Constructor. Sets local copies of user preferences and initialises grade_tree.
31 * @param int $courseid
32 * @param string $context
33 * @param int $userid The id of the user
34 */
35 function grade_report_user($courseid, $context, $userid) {
36 global $CFG;
37 parent::grade_report($courseid, $context);
38
39 // get the user (for full name)
40 $this->user = get_record('user', 'id', $userid);
41
42 // base url for sorting by first/last name
43 $this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&amp;userid='.$userid;
44
45 $this->setup_table();
46
47 }
48
49 /**
50 * Prepares the headers and attributes of the flexitable.
51 */
52 function setup_table() {
53 /*
54 * Table has 6 columns
55 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
56 */
57
58 // setting up table headers
59 $tablecolumns = array('itempic', 'itemname', 'grade', 'percentage', 'rank', 'feedback');
60 $tableheaders = array('', $this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('grade'),
61 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
62 $this->get_lang_string('feedback'));
63
64 $this->table = new flexible_table('grade-report-user-'.$this->courseid);
65
66 $this->table->define_columns($tablecolumns);
67 $this->table->define_headers($tableheaders);
68 $this->table->define_baseurl($this->baseurl);
69
70 $this->table->set_attribute('cellspacing', '0');
71 $this->table->set_attribute('id', 'user-grade');
72 $this->table->set_attribute('class', 'boxaligncenter generaltable');
73
74 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
75 $this->table->set_control_variables(array(
76 TABLE_VAR_SORT => 'ssort',
77 TABLE_VAR_HIDE => 'shide',
78 TABLE_VAR_SHOW => 'sshow',
79 TABLE_VAR_IFIRST => 'sifirst',
80 TABLE_VAR_ILAST => 'silast',
81 TABLE_VAR_PAGE => 'spage'
82 ));
83
84 $this->table->setup();
85 }
86
87 function fill_table() {
88 global $CFG;
89
90 if ($all_grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
91 $grade_items = array();
92 foreach ($all_grade_items as $item) {
93 $grade_items[$item->sortorder] = $item;
94 }
95 unset($all_grade_items);
96 ksort($grade_items);
97
98 $total = $grade_items[1];
99 unset($grade_items[1]);
100 $grade_items[] = $total;
101
102 foreach ($grade_items as $grade_item) {
103
104 $data = array();
105
106 $params->itemid = $grade_item->id;
107 $params->userid = $this->user->id;
108 $grade_grades = new grade_grades($params);
109 $grade_text = $grade_grades->load_text();
110
111 /// prints mod icon if available
112 if ($grade_item->itemtype == 'mod') {
113 $iconpath = $CFG->dirroot.'/mod/'.$grade_item->itemmodule.'/icon.gif';
114 $icon = $CFG->wwwroot.'/mod/'.$grade_item->itemmodule.'/icon.gif';
115 if (file_exists($iconpath)) {
116 $data[] = '<img src = "'.$icon.'" alt="'.$grade_item->itemname.'" class="activityicon"/>';
117 }
118 } else {
119 $data[] = '';
120 }
121
122 // TODO: indicate items that "needsupdate" - missing final calculation
123
124 /// prints grade item name
125 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
126 $data[] = '<b>'.$grade_item->get_name().'</b>';
127 } else {
128 $data[] = $grade_item->get_name();
129 }
130
131 /// prints the grade
132
133 if ($grade_item->scaleid) {
134 // using scales
135 if ($scale = get_record('scale', 'id', $grade_item->scaleid)) {
136 $scales = explode(",", $scale->scale);
137 // reindex because scale is off 1
138 // invalid grade if gradeval < 1
139 if ((int) $grade_grades->finalgrade < 1) {
140 $data[] = '-';
141 } else {
142 $data[] = $scales[$grade_grades->finalgrade-1];
143 }
144 }
145 } else {
146 // normal grade, or text, just display
147 $data[] = $this->get_grade_clean($grade_grades->finalgrade);
148 }
149
150 /// prints percentage
151
152 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
153 // processing numeric grade
154 if ($grade_grades->finalgrade) {
155 $percentage = $this->get_grade_clean(($grade_grades->finalgrade / $grade_item->grademax) * 100).'%';
156 } else {
157 $percentage = '-';
158 }
159
160 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
161 // processing scale grade
162 $scale = get_record('scale', 'id', $grade_item->scaleid);
163 $scalevals = explode(",", $scale->scale);
164 $percentage = $this->get_grade_clean(($grade_grades->finalgrade) / count($scalevals) * 100).'%';
165
166 } else {
167 // text grade
168 $percentage = '-';
169 }
170
171 $data[] = $percentage;
172
173 /// prints rank
174 if ($grade_grades->finalgrade) {
175 /// find the number of users with a higher grade
176 $sql = "SELECT COUNT(DISTINCT(userid))
177 FROM {$CFG->prefix}grade_grades
178 WHERE finalgrade > $grade_grades->finalgrade
179 AND itemid = $grade_item->id";
180 $rank = count_records_sql($sql) + 1;
181
182 $data[] = "$rank/$numusers";
183 } else {
184 // no grade, no rank
185 $data[] = "-";
186 }
187
188 /// prints notes
189 if (!empty($grade_text->feedback)) {
190 $data[] = $grade_text->feedback;
191 } else {
192 $data[] = '&nbsp;';
193 }
194 $this->table->add_data($data);
195 }
196
197 return true;
198 } else {
199 notify(get_string('nogradeitem', 'grades'));
200 return false;
201 }
202 }
203
204 /**
205 * Prints or returns the HTML from the flexitable.
206 * @param bool $return Whether or not to return the data instead of printing it directly.
207 * @return string
208 */
209 function print_table($return=false) {
210 ob_start();
211 $this->table->print_html();
212 $html = ob_get_clean();
213 if ($return) {
214 return $html;
215 } else {
216 echo $html;
217 }
218 }
219
220 /**
221 * Processes the data sent by the form (grades and feedbacks).
222 * @var array $data
223 * @return bool Success or Failure (array of errors).
224 */
225 function process_data($data) {
226 }
227
228 /**
229 * Fetches and returns a count of all the users that will be shows on this page.
230 * @return int Count of users
231 */
232 function get_numusers() {
233 global $CFG;
234 return count(get_role_users(@implode(',', $CFG->gradebookroles), $this->context));
235 }
236
237}
238?>