ANSI join ON clauses should contain only the join condition. Also,
[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
d30c4481 32 * @param object $gpr grade plugin return tracking object
f7998fbc 33 * @param string $context
34 * @param int $userid The id of the user
35 */
d30c4481 36 function grade_report_user($courseid, $gpr, $context, $userid) {
f7998fbc 37 global $CFG;
d30c4481 38 parent::grade_report($courseid, $gpr, $context);
f7998fbc 39
4faf5f99 40 // Grab the grade_tree for this course
41 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
42
f7998fbc 43 // get the user (for full name)
44 $this->user = get_record('user', 'id', $userid);
45
46 // base url for sorting by first/last name
47 $this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&amp;userid='.$userid;
90d3960c 48 $this->pbarurl = $this->baseurl;
f7998fbc 49
90d3960c 50 // Setup groups if requested
51 if ($this->get_pref('showgroups')) {
52 $this->setup_groups();
53 }
f7998fbc 54
90d3960c 55 $this->setup_table();
f7998fbc 56 }
57
58 /**
59 * Prepares the headers and attributes of the flexitable.
60 */
61 function setup_table() {
62 /*
63 * Table has 6 columns
64 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
65 */
66
67 // setting up table headers
4c8d9481 68 $tablecolumns = array('itemname', 'category', 'grade', 'percentage', 'rank', 'feedback');
69 $tableheaders = array($this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('category'), $this->get_lang_string('grade'),
f7998fbc 70 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
71 $this->get_lang_string('feedback'));
72
73 $this->table = new flexible_table('grade-report-user-'.$this->courseid);
74
75 $this->table->define_columns($tablecolumns);
76 $this->table->define_headers($tableheaders);
77 $this->table->define_baseurl($this->baseurl);
78
79 $this->table->set_attribute('cellspacing', '0');
80 $this->table->set_attribute('id', 'user-grade');
81 $this->table->set_attribute('class', 'boxaligncenter generaltable');
82
83 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
84 $this->table->set_control_variables(array(
85 TABLE_VAR_SORT => 'ssort',
86 TABLE_VAR_HIDE => 'shide',
87 TABLE_VAR_SHOW => 'sshow',
88 TABLE_VAR_IFIRST => 'sifirst',
89 TABLE_VAR_ILAST => 'silast',
90 TABLE_VAR_PAGE => 'spage'
91 ));
92
93 $this->table->setup();
94 }
95
96 function fill_table() {
97 global $CFG;
6ef84f6f 98 $numusers = $this->get_numusers(false); // total course users
f7998fbc 99
100 if ($all_grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
101 $grade_items = array();
102 foreach ($all_grade_items as $item) {
103 $grade_items[$item->sortorder] = $item;
104 }
105 unset($all_grade_items);
106 ksort($grade_items);
107
108 $total = $grade_items[1];
109 unset($grade_items[1]);
110 $grade_items[] = $total;
31a6c06c 111
f7998fbc 112 foreach ($grade_items as $grade_item) {
113
31a6c06c 114 $decimalpoints = $grade_item->get_decimals();
f7998fbc 115 $data = array();
116
23207a1a 117 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
f7998fbc 118
f7998fbc 119 // TODO: indicate items that "needsupdate" - missing final calculation
120
121 /// prints grade item name
122 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
123 $data[] = '<b>'.$grade_item->get_name().'</b>';
124 } else {
157ed1d1 125 $data[] = $this->get_module_link($grade_item->get_name(), $grade_item->itemmodule, $grade_item->iteminstance);;
f7998fbc 126 }
127
4c8d9481 128 /// prints category
129 $cat = $grade_item->get_parent_category();
130 $data[] = $cat->fullname;
131
132
f7998fbc 133 /// prints the grade
a7c3671a 134 $displaytype = $grade_item->get_displaytype();
f7998fbc 135
23207a1a 136 if ($grade_grade->is_excluded()) {
53a16616 137 $excluded = get_string('excluded', 'grades').' ';
23207a1a 138 } else {
139 $excluded = '';
140 }
141
a7c3671a 142 if ((int) $grade_grade->finalgrade < 1) {
143 $data[] = '-';
144 } elseif ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $grade_item->courseid))) {
145 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified));
146 } elseif ($grade_item->scaleid) {
147 if ($scale = get_record('scale', 'id', $grade_item->scaleid)) {
148 $scales = explode(",", $scale->scale);
149 // reindex because scale is off 1
150 $data[] = $excluded.$scales[$grade_grade->finalgrade-1];
151 }
3c334a7a 152 } else {
a7c3671a 153 $data[] = $excluded . grade_format_gradevalue($grade_grade->finalgrade, $grade_item, true, $displaytype, $decimalpoints);
154 }
155
f7998fbc 156 /// prints percentage
157
3c334a7a 158 if ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $grade_item->courseid))) {
6ef84f6f 159 if ((int) $grade_grade->finalgrade < 1) {
31a6c06c 160 $data[] = '-';
6ef84f6f 161 } else {
162 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified));
163 }
3c334a7a 164 } else {
165 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
166 // processing numeric grade
167 if ($grade_grade->finalgrade) {
168 $percentage = format_float(($grade_grade->finalgrade / $grade_item->grademax) * 100, $decimalpoints).'%';
169 } else {
170 $percentage = '-';
171 }
172
173 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
174 // processing scale grade
175 $scale = get_record('scale', 'id', $grade_item->scaleid);
176 $scalevals = explode(",", $scale->scale);
177 $percentage = format_float(($grade_grade->finalgrade) / count($scalevals) * 100, $decimalpoints).'%';
178
f7998fbc 179 } else {
3c334a7a 180 // text grade
f7998fbc 181 $percentage = '-';
182 }
183
3c334a7a 184 $data[] = $percentage;
f7998fbc 185 }
f7998fbc 186 /// prints rank
3ee5c201 187 if ($grade_grade->finalgrade) {
f7998fbc 188 /// find the number of users with a higher grade
189 $sql = "SELECT COUNT(DISTINCT(userid))
190 FROM {$CFG->prefix}grade_grades
3ee5c201 191 WHERE finalgrade > $grade_grade->finalgrade
f7998fbc 192 AND itemid = $grade_item->id";
193 $rank = count_records_sql($sql) + 1;
194
195 $data[] = "$rank/$numusers";
196 } else {
197 // no grade, no rank
198 $data[] = "-";
199 }
200
201 /// prints notes
3f2b0c8a 202 if (!empty($grade_grade->feedback)) {
203 $data[] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
f7998fbc 204 } else {
205 $data[] = '&nbsp;';
206 }
207 $this->table->add_data($data);
208 }
209
210 return true;
211 } else {
212 notify(get_string('nogradeitem', 'grades'));
213 return false;
214 }
215 }
216
217 /**
218 * Prints or returns the HTML from the flexitable.
219 * @param bool $return Whether or not to return the data instead of printing it directly.
220 * @return string
221 */
222 function print_table($return=false) {
223 ob_start();
224 $this->table->print_html();
225 $html = ob_get_clean();
226 if ($return) {
227 return $html;
228 } else {
229 echo $html;
230 }
231 }
232
233 /**
234 * Processes the data sent by the form (grades and feedbacks).
235 * @var array $data
236 * @return bool Success or Failure (array of errors).
237 */
238 function process_data($data) {
239 }
240
f7998fbc 241}
242?>