MDL-11718 grader report now does not show column totals if user does not have view...
[moodle.git] / grade / report / user / lib.php
CommitLineData
f7998fbc 1<?php // $Id$
8ad36f4c 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
f7998fbc 25/**
26 * File in which the user_report class is defined.
27 * @package gradebook
28 */
29
30require_once($CFG->dirroot . '/grade/report/lib.php');
31require_once($CFG->libdir.'/tablelib.php');
32
33/**
34 * Class providing an API for the user report building and displaying.
35 * @uses grade_report
36 * @package gradebook
37 */
38class grade_report_user extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
44 var $user;
45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
50 var $table;
51
e0724506 52 /**
53 * Flat structure similar to grade tree
54 */
55 var $gseq;
56
f7998fbc 57 /**
58 * Constructor. Sets local copies of user preferences and initialises grade_tree.
59 * @param int $courseid
d30c4481 60 * @param object $gpr grade plugin return tracking object
f7998fbc 61 * @param string $context
62 * @param int $userid The id of the user
63 */
d30c4481 64 function grade_report_user($courseid, $gpr, $context, $userid) {
f7998fbc 65 global $CFG;
d30c4481 66 parent::grade_report($courseid, $gpr, $context);
f7998fbc 67
e0724506 68 $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
69
4faf5f99 70 // Grab the grade_tree for this course
e0724506 71 $this->gseq = new grade_seq($this->courseid, $switch);
4faf5f99 72
f7998fbc 73 // get the user (for full name)
74 $this->user = get_record('user', 'id', $userid);
75
76 // base url for sorting by first/last name
77 $this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&amp;userid='.$userid;
90d3960c 78 $this->pbarurl = $this->baseurl;
f7998fbc 79
e0724506 80 // always setup groups - no user preference here
81 $this->setup_groups();
f7998fbc 82
90d3960c 83 $this->setup_table();
f7998fbc 84 }
85
86 /**
87 * Prepares the headers and attributes of the flexitable.
88 */
89 function setup_table() {
02973505 90 global $CFG;
f7998fbc 91 /*
02973505 92 * Table has 5-6 columns
93 *| pic | itemname/description | grade (grade_final) | percentage | rank (optional) | feedback |
f7998fbc 94 */
95
96 // setting up table headers
02973505 97 if (!empty($CFG->grade_userreport_showrank)) {
98 // TODO: this is broken if hidden grades present!!
99 $tablecolumns = array('itemname', 'category', 'grade', 'percentage', 'rank', 'feedback');
100 $tableheaders = array($this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('category'), $this->get_lang_string('grade'),
101 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
102 $this->get_lang_string('feedback'));
103 } else {
104 $tablecolumns = array('itemname', 'category', 'grade', 'percentage', 'feedback');
105 $tableheaders = array($this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('category'), $this->get_lang_string('grade'),
106 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('feedback'));
107 }
f7998fbc 108
109 $this->table = new flexible_table('grade-report-user-'.$this->courseid);
110
111 $this->table->define_columns($tablecolumns);
112 $this->table->define_headers($tableheaders);
113 $this->table->define_baseurl($this->baseurl);
114
115 $this->table->set_attribute('cellspacing', '0');
116 $this->table->set_attribute('id', 'user-grade');
117 $this->table->set_attribute('class', 'boxaligncenter generaltable');
118
119 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
120 $this->table->set_control_variables(array(
121 TABLE_VAR_SORT => 'ssort',
122 TABLE_VAR_HIDE => 'shide',
123 TABLE_VAR_SHOW => 'sshow',
124 TABLE_VAR_IFIRST => 'sifirst',
125 TABLE_VAR_ILAST => 'silast',
126 TABLE_VAR_PAGE => 'spage'
127 ));
128
129 $this->table->setup();
130 }
131
132 function fill_table() {
133 global $CFG;
6ef84f6f 134 $numusers = $this->get_numusers(false); // total course users
6391ebe7 135 $items =& $this->gseq->items;
136 $grades = array();
f7998fbc 137
b89a70ce 138 $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->courseid));
f7998fbc 139
4c8893ed 140 // fetch or create all grades
6391ebe7 141 foreach ($items as $key=>$unused) {
4c8893ed 142 if (!$grade_grade = grade_grade::fetch(array('itemid'=>$items[$key]->id, 'userid'=>$this->user->id))) {
143 $grade_grade = new grade_grade();
144 $grade_grade->userid = $this->user->id;
145 $grade_grade->itemid = $items[$key]->id;
146 }
6391ebe7 147 $grades[$key] = $grade_grade;
4c8893ed 148 $grades[$key]->grade_item =& $items[$key];
6391ebe7 149 }
150
b89a70ce 151 if ($canviewhidden) {
d297269d 152 $altered = array();
153 $unknown = array();
b89a70ce 154 } else {
155 $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
d297269d 156 $altered = $hiding_affected['altered'];
157 $unknown = $hiding_affected['unknown'];
158 unset($hiding_affected);
b89a70ce 159 }
6391ebe7 160
d297269d 161 foreach ($items as $itemid=>$unused) {
162 $grade_item =& $items[$itemid];
163 $grade_grade =& $grades[$itemid];
164
02973505 165 if (empty($CFG->grade_userreport_showhiddenitems) and !$canviewhidden and $grade_item->is_hidden()) {
166 continue;
167 }
168
d297269d 169 if (in_array($itemid, $unknown)) {
170 $gradeval = null;
171 } else if (array_key_exists($itemid, $altered)) {
172 $gradeval = $altered[$itemid];
173 } else {
174 $gradeval = $grade_grade->finalgrade;
175 }
6391ebe7 176
177 $data = array();
f7998fbc 178
e0724506 179 /// prints grade item name
180 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
6391ebe7 181 $data[] = '<div class="catname">'.$grade_item->get_name().'</div>';
e0724506 182 } else {
6391ebe7 183 $data[] = '<div class="itemname">'.$this->get_module_link($grade_item->get_name(), $grade_item->itemmodule, $grade_item->iteminstance).'</div>';
e0724506 184 }
f7998fbc 185
e0724506 186 /// prints category
187 $cat = $grade_item->get_parent_category();
6391ebe7 188 $data[] = $cat->get_name();
4c8d9481 189
e0724506 190 /// prints the grade
e0724506 191 if ($grade_grade->is_excluded()) {
192 $excluded = get_string('excluded', 'grades').' ';
193 } else {
194 $excluded = '';
195 }
f7998fbc 196
b89a70ce 197 if ($grade_item->needsupdate) {
198 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
199
d297269d 200 } else if (!empty($CFG->grade_hiddenasdate) and !is_null($grade_grade->finalgrade) and !$canviewhidden and $grade_grade->is_hidden()
201 and !$grade_item->is_category_item() and !$grade_item->is_course_item()) {
202 // the problem here is that we do not have the time when grade value was modified, 'timemodified' is general modification date for grade_grades records
203 $data[] = $excluded . '<div class="gradeddate">'.get_string('gradedon', 'grades', userdate($grade_grade->timemodified, get_string('strftimedatetimeshort'))).'</div>';
6391ebe7 204
e0724506 205 } else {
d297269d 206 $data[] = $excluded . grade_format_gradevalue($gradeval, $grade_item, true);
e0724506 207 }
208
209 /// prints percentage
b89a70ce 210 if ($grade_item->needsupdate) {
211 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
23207a1a 212
6391ebe7 213 } else {
d297269d 214 $data[] = grade_format_gradevalue($gradeval, $grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE);
e0724506 215 }
6391ebe7 216
e0724506 217 /// prints rank
02973505 218 if (!empty($CFG->grade_userreport_showrank)) {
219 // TODO: this is broken if hidden grades present!!
220 if ($grade_item->needsupdate) {
221 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
222
223 } else if (is_null($gradeval)) {
224 // no grade, no rank
225 $data[] = '-';
226
227 } else {
228 /// find the number of users with a higher grade
229 $sql = "SELECT COUNT(DISTINCT(userid))
230 FROM {$CFG->prefix}grade_grades
231 WHERE finalgrade > {$grade_grade->finalgrade}
232 AND itemid = {$grade_item->id}";
233 $rank = count_records_sql($sql) + 1;
234
235 $data[] = "$rank/$numusers";
236 }
f7998fbc 237 }
238
e0724506 239 /// prints notes
6391ebe7 240 if (empty($grade_grade->feedback)) {
241 $data[] = '&nbsp;';
242
d297269d 243 } else if (!$canviewhidden and $grade_grade->is_hidden()) {
e0724506 244 $data[] = '&nbsp;';
6391ebe7 245
246 } else {
247 $data[] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
e0724506 248 }
6391ebe7 249
e0724506 250 $this->table->add_data($data);
f7998fbc 251 }
e0724506 252
253 return true;
f7998fbc 254 }
255
256 /**
257 * Prints or returns the HTML from the flexitable.
258 * @param bool $return Whether or not to return the data instead of printing it directly.
259 * @return string
260 */
261 function print_table($return=false) {
262 ob_start();
263 $this->table->print_html();
264 $html = ob_get_clean();
265 if ($return) {
266 return $html;
267 } else {
268 echo $html;
269 }
270 }
271
272 /**
273 * Processes the data sent by the form (grades and feedbacks).
274 * @var array $data
275 * @return bool Success or Failure (array of errors).
276 */
277 function process_data($data) {
278 }
279
f7998fbc 280}
281?>