MDL-11658 shared rdef definition - reduced memory usage in cron
[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() {
90 /*
91 * Table has 6 columns
92 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
93 */
94
95 // setting up table headers
4c8d9481 96 $tablecolumns = array('itemname', 'category', 'grade', 'percentage', 'rank', 'feedback');
97 $tableheaders = array($this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('category'), $this->get_lang_string('grade'),
f7998fbc 98 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
99 $this->get_lang_string('feedback'));
100
101 $this->table = new flexible_table('grade-report-user-'.$this->courseid);
102
103 $this->table->define_columns($tablecolumns);
104 $this->table->define_headers($tableheaders);
105 $this->table->define_baseurl($this->baseurl);
106
107 $this->table->set_attribute('cellspacing', '0');
108 $this->table->set_attribute('id', 'user-grade');
109 $this->table->set_attribute('class', 'boxaligncenter generaltable');
110
111 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
112 $this->table->set_control_variables(array(
113 TABLE_VAR_SORT => 'ssort',
114 TABLE_VAR_HIDE => 'shide',
115 TABLE_VAR_SHOW => 'sshow',
116 TABLE_VAR_IFIRST => 'sifirst',
117 TABLE_VAR_ILAST => 'silast',
118 TABLE_VAR_PAGE => 'spage'
119 ));
120
121 $this->table->setup();
122 }
123
124 function fill_table() {
125 global $CFG;
6ef84f6f 126 $numusers = $this->get_numusers(false); // total course users
f7998fbc 127
e0724506 128 foreach ($this->gseq->items as $element) {
129 $grade_item = $element['object'];
130 $decimalpoints = $grade_item->get_decimals();
131 $data = array();
f7998fbc 132
e0724506 133 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
134 $grade_grade->grade_item =& $grade_item;
f7998fbc 135
e0724506 136 // TODO: indicate items that "needsupdate" - missing final calculation
f7998fbc 137
e0724506 138 /// prints grade item name
139 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
140 $data[] = '<b>'.$grade_item->get_name().'</b>';
141 } else {
142 $data[] = $this->get_module_link($grade_item->get_name(), $grade_item->itemmodule, $grade_item->iteminstance);;
143 }
f7998fbc 144
e0724506 145 /// prints category
146 $cat = $grade_item->get_parent_category();
147 $data[] = $cat->fullname;
f7998fbc 148
4c8d9481 149
e0724506 150 /// prints the grade
151 $displaytype = $grade_item->get_displaytype();
4c8d9481 152
e0724506 153 if ($grade_grade->is_excluded()) {
154 $excluded = get_string('excluded', 'grades').' ';
155 } else {
156 $excluded = '';
157 }
f7998fbc 158
e0724506 159 if ((int) $grade_grade->finalgrade < 1) {
160 $data[] = '-';
161 } elseif ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $grade_item->courseid))) {
162 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified));
163 } elseif ($grade_item->scaleid) {
164 if ($scale = get_record('scale', 'id', $grade_item->scaleid)) {
165 $scales = explode(",", $scale->scale);
166 // reindex because scale is off 1
167 $data[] = $excluded.$scales[$grade_grade->finalgrade-1];
23207a1a 168 }
e0724506 169 } else {
170 $data[] = $excluded . grade_format_gradevalue($grade_grade->finalgrade, $grade_item, true, $displaytype, $decimalpoints);
171 }
172
173 /// prints percentage
23207a1a 174
e0724506 175 if ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $grade_item->courseid))) {
a7c3671a 176 if ((int) $grade_grade->finalgrade < 1) {
177 $data[] = '-';
3c334a7a 178 } else {
e0724506 179 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified));
180 }
181 } else {
182 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
183 // processing numeric grade
184 if ($grade_grade->finalgrade) {
185 $percentage = format_float(($grade_grade->finalgrade / $grade_item->grademax) * 100, $decimalpoints).'%';
f7998fbc 186 } else {
187 $percentage = '-';
188 }
189
e0724506 190 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
191 // processing scale grade
192 $scale = get_record('scale', 'id', $grade_item->scaleid);
193 $scalevals = explode(",", $scale->scale);
194 $percentage = format_float(($grade_grade->finalgrade) / count($scalevals) * 100, $decimalpoints).'%';
f7998fbc 195
f7998fbc 196 } else {
e0724506 197 // text grade
198 $percentage = '-';
f7998fbc 199 }
e0724506 200
201 $data[] = $percentage;
202 }
203 /// prints rank
204 if ($grade_grade->finalgrade) {
205 /// find the number of users with a higher grade
206 $sql = "SELECT COUNT(DISTINCT(userid))
207 FROM {$CFG->prefix}grade_grades
208 WHERE finalgrade > $grade_grade->finalgrade
209 AND itemid = $grade_item->id";
210 $rank = count_records_sql($sql) + 1;
211
212 $data[] = "$rank/$numusers";
213 } else {
214 // no grade, no rank
215 $data[] = "-";
f7998fbc 216 }
217
e0724506 218 /// prints notes
219 if (!empty($grade_grade->feedback)) {
220 $data[] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
221 } else {
222 $data[] = '&nbsp;';
223 }
224 $this->table->add_data($data);
f7998fbc 225 }
e0724506 226
227 return true;
f7998fbc 228 }
229
230 /**
231 * Prints or returns the HTML from the flexitable.
232 * @param bool $return Whether or not to return the data instead of printing it directly.
233 * @return string
234 */
235 function print_table($return=false) {
236 ob_start();
237 $this->table->print_html();
238 $html = ob_get_clean();
239 if ($return) {
240 return $html;
241 } else {
242 echo $html;
243 }
244 }
245
246 /**
247 * Processes the data sent by the form (grades and feedbacks).
248 * @var array $data
249 * @return bool Success or Failure (array of errors).
250 */
251 function process_data($data) {
252 }
253
f7998fbc 254}
255?>