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