MDL-11167, admin blocks page should not show blocks not placed on course pages
[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
6391ebe7 127 $items =& $this->gseq->items;
128 $grades = array();
f7998fbc 129
b89a70ce 130 $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->courseid));
f7998fbc 131
6391ebe7 132 foreach ($items as $key=>$unused) {
133 $grade_item =& $items[$key];
e0724506 134 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
6391ebe7 135 $grades[$key] = $grade_grade;
136 $grades[$key]->grade_item =& $grade_item;
137 }
138
b89a70ce 139 if ($canviewhidden) {
140 $hiding_affected = array();
141 } else {
142 $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
143 }
6391ebe7 144
145 foreach ($items as $key=>$unused) {
146 $grade_item =& $items[$key];
147 $grade_grade =& $grades[$key];
148
149 $data = array();
f7998fbc 150
e0724506 151 /// prints grade item name
152 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
6391ebe7 153 $data[] = '<div class="catname">'.$grade_item->get_name().'</div>';
e0724506 154 } else {
6391ebe7 155 $data[] = '<div class="itemname">'.$this->get_module_link($grade_item->get_name(), $grade_item->itemmodule, $grade_item->iteminstance).'</div>';
e0724506 156 }
f7998fbc 157
e0724506 158 /// prints category
159 $cat = $grade_item->get_parent_category();
6391ebe7 160 $data[] = $cat->get_name();
4c8d9481 161
e0724506 162 /// prints the grade
e0724506 163 if ($grade_grade->is_excluded()) {
164 $excluded = get_string('excluded', 'grades').' ';
165 } else {
166 $excluded = '';
167 }
f7998fbc 168
b89a70ce 169 if ($grade_item->needsupdate) {
170 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
171
172 } else if (is_null($grade_grade->finalgrade)) {
6391ebe7 173 $data[] = $excluded . '-';
174
b89a70ce 175 } else if (!$canviewhidden and ($grade_grade->is_hidden() or in_array($grade_item->id, $hiding_affected))) {
6391ebe7 176 // TODO: optinally do not show anything for hidden grades
177 // $data[] = '-';
178 if ($grade_grade->is_hidden()) {
179 $data[] = $excluded . '<div class="gradeddate">'.get_string('gradedon', 'grades', userdate($grade_grade->timemodified, get_string('strftimedatetimeshort'))).'</div>';
180 } else {
181 $data[] = $excluded . '-';
23207a1a 182 }
6391ebe7 183
e0724506 184 } else {
6391ebe7 185 $data[] = $excluded . grade_format_gradevalue($grade_grade->finalgrade, $grade_item, true);
e0724506 186 }
187
188 /// prints percentage
b89a70ce 189 if ($grade_item->needsupdate) {
190 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
23207a1a 191
b89a70ce 192 } else if (is_null($grade_grade->finalgrade)) {
6391ebe7 193 $data[] = '-';
f7998fbc 194
b89a70ce 195 } else if (!$canviewhidden and ($grade_grade->is_hidden() or in_array($grade_item->id, $hiding_affected))) {
6391ebe7 196 $data[] = '-';
e0724506 197
6391ebe7 198 } else {
199 $data[] = grade_format_gradevalue($grade_grade->finalgrade, $grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE);
e0724506 200 }
6391ebe7 201
e0724506 202 /// prints rank
b89a70ce 203 if ($grade_item->needsupdate) {
204 $data[] = '<span class="gradingerror">'.get_string('error').'</span>';
205
206 } else if (is_null($grade_grade->finalgrade)) {
6391ebe7 207 // no grade, no rank
208 $data[] = '-';
209
b89a70ce 210 } else if (!$canviewhidden and ($grade_grade->is_hidden() or in_array($grade_item->id, $hiding_affected))) {
6391ebe7 211 $data[] = '-';
212
213 } else {
e0724506 214 /// find the number of users with a higher grade
215 $sql = "SELECT COUNT(DISTINCT(userid))
6391ebe7 216 FROM {$CFG->prefix}grade_grades
217 WHERE finalgrade > {$grade_grade->finalgrade}
218 AND itemid = {$grade_item->id}";
e0724506 219 $rank = count_records_sql($sql) + 1;
220
221 $data[] = "$rank/$numusers";
f7998fbc 222 }
223
e0724506 224 /// prints notes
6391ebe7 225 if (empty($grade_grade->feedback)) {
226 $data[] = '&nbsp;';
227
b89a70ce 228 } else if (!$canviewhidden and ($grade_grade->is_hidden() or in_array($grade_item->id, $hiding_affected))) {
e0724506 229 $data[] = '&nbsp;';
6391ebe7 230
231 } else {
232 $data[] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
e0724506 233 }
6391ebe7 234
e0724506 235 $this->table->add_data($data);
f7998fbc 236 }
e0724506 237
238 return true;
f7998fbc 239 }
240
241 /**
242 * Prints or returns the HTML from the flexitable.
243 * @param bool $return Whether or not to return the data instead of printing it directly.
244 * @return string
245 */
246 function print_table($return=false) {
247 ob_start();
248 $this->table->print_html();
249 $html = ob_get_clean();
250 if ($return) {
251 return $html;
252 } else {
253 echo $html;
254 }
255 }
256
257 /**
258 * Processes the data sent by the form (grades and feedbacks).
259 * @var array $data
260 * @return bool Success or Failure (array of errors).
261 */
262 function process_data($data) {
263 }
264
f7998fbc 265}
266?>