MDL-51709 gradebook: Name display in single view.
[moodle.git] / grade / report / singleview / classes / local / screen / grade.php
CommitLineData
7bd4a361 1<?php
508f23f1
ZD
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
57fac09a 18 * The screen with a list of users.
508f23f1 19 *
65c134f9 20 * @package gradereport_singleview
508f23f1
ZD
21 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
57fac09a
DW
25namespace gradereport_singleview\local\screen;
26
27use gradereport_singleview\local\ui\range;
1b2c1ccb
ZD
28use gradereport_singleview\local\ui\bulk_insert;
29use grade_grade;
57fac09a
DW
30use grade_item;
31use moodle_url;
32use pix_icon;
33use html_writer;
34use gradereport_singleview;
35
36defined('MOODLE_INTERNAL') || die;
37
38/**
39 * The screen with a list of users.
40 *
41 * @package gradereport_singleview
42 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class grade extends tablelike implements selectable_items, filterable_items {
8ec7b088 46
57fac09a 47 /** @var int $totalitemcount Used for paging */
8ec7b088 48 private $totalitemcount = 0;
7bd4a361 49
57fac09a 50 /** @var bool $requiresextra True if this is a manual grade item */
65c134f9 51 private $requiresextra = false;
7bd4a361 52
57fac09a 53 /** @var bool $requirepaging True if there are more users than our limit. */
8ec7b088 54 private $requirespaging = true;
7bd4a361 55
57fac09a
DW
56 /**
57 * True if $CFG->grade_overridecat is true
58 *
59 * @return bool
60 */
65c134f9 61 public static function allowcategories() {
57fac09a 62 return get_config('moodle', 'grade_overridecat');
7bd4a361
ZD
63 }
64
57fac09a
DW
65 /**
66 * Filter the list excluding category items (if required)?
67 * @param grade_item $item The grade item.
68 */
7bd4a361 69 public static function filter($item) {
57fac09a
DW
70 return get_config('moodle', 'grade_overridecat') ||
71 !($item->is_course_item() || $item->is_category_item());
7bd4a361
ZD
72 }
73
2a6cdc51
DW
74 /**
75 * Get the label for the select box that chooses items for this page.
76 * @return string
77 */
78 public function select_label() {
79 return get_string('selectuser', 'gradereport_singleview');
80 }
81
57fac09a
DW
82 /**
83 * Get the description of this page
84 * @return string
85 */
7bd4a361
ZD
86 public function description() {
87 return get_string('users');
88 }
89
57fac09a
DW
90 /**
91 * Convert this list of items into an options list
92 *
93 * @return array
94 */
7bd4a361 95 public function options() {
57fac09a
DW
96 $options = array();
97 foreach ($this->items as $userid => $user) {
98 $options[$userid] = fullname($user);
99 }
0a55da57 100
0a55da57 101 return $options;
7bd4a361
ZD
102 }
103
57fac09a
DW
104 /**
105 * Return the type of the things in this list.
106 * @return string
107 */
7bd4a361
ZD
108 public function item_type() {
109 return 'user';
110 }
111
57fac09a
DW
112 /**
113 * Get the original settings for this item
114 * @return array
115 */
7bd4a361
ZD
116 public function original_definition() {
117 $def = array('finalgrade', 'feedback');
118
5b257154 119 $def[] = 'override';
7bd4a361
ZD
120
121 $def[] = 'exclude';
122
123 return $def;
124 }
125
57fac09a
DW
126 /**
127 * Init this page
128 *
129 * @param bool $selfitemisempty True if we have not selected a user.
130 */
65c134f9 131 public function init($selfitemisempty = false) {
8ec7b088 132
aac66bef
SL
133 $this->items = $this->load_users();
134 $this->totalitemcount = count($this->items);
7bd4a361 135
65c134f9 136 if ($selfitemisempty) {
7bd4a361
ZD
137 return;
138 }
139
7bd4a361
ZD
140 $params = array(
141 'id' => $this->itemid,
142 'courseid' => $this->courseid
143 );
144
145 $this->item = grade_item::fetch($params);
57fac09a
DW
146 if (!self::filter($this->item)) {
147 $this->items = array();
148 $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview'));
7bd4a361
ZD
149 }
150
65c134f9 151 $this->requiresextra = !$this->item->is_manual_item();
7bd4a361
ZD
152
153 $this->setup_structure();
154
155 $this->set_definition($this->original_definition());
156 $this->set_headers($this->original_headers());
157 }
158
57fac09a
DW
159 /**
160 * Get the table headers
161 *
162 * @return array
163 */
7bd4a361 164 public function original_headers() {
8ec7b088 165 return array(
65c134f9 166 '', // For filter icon.
4830f551 167 get_string('fullnameuser', 'core'),
7bd4a361
ZD
168 get_string('range', 'grades'),
169 get_string('grade', 'grades'),
8ec7b088
ZD
170 get_string('feedback', 'grades'),
171 $this->make_toggle_links('override'),
172 $this->make_toggle_links('exclude')
7bd4a361 173 );
7bd4a361
ZD
174 }
175
57fac09a
DW
176 /**
177 * Format a row in the table
178 *
179 * @param user $item
180 * @return string
181 */
7bd4a361
ZD
182 public function format_line($item) {
183 global $OUTPUT;
184
185 $grade = $this->fetch_grade_or_default($this->item, $item->id);
186
7bd4a361
ZD
187 $lockicon = '';
188
65c134f9
ZD
189 $lockedgrade = $lockedgradeitem = 0;
190 if (!empty($grade->locked)) {
191 $lockedgrade = 1;
192 }
193 if (!empty($grade->grade_item->locked)) {
194 $lockedgradeitem = 1;
195 }
196 // Check both grade and grade item.
57fac09a 197 if ( $lockedgrade || $lockedgradeitem ) {
7bd4a361 198 $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked') . ' ';
57fac09a 199 }
7bd4a361 200
4830f551
LB
201 if (has_capability('moodle/site:viewfullnames', \context_course::instance($this->courseid))) {
202 $fullname = $lockicon . fullname($item, true);
7bd4a361
ZD
203 } else {
204 $fullname = $lockicon . fullname($item);
205 }
206
207 $item->imagealt = $fullname;
508f23f1 208 $url = new moodle_url("/user/view.php", array('id' => $item->id, 'course' => $this->courseid));
65c134f9 209 $iconstring = get_string('filtergrades', 'gradereport_singleview', $fullname);
7bd3932e 210 $grade->label = $fullname;
8a944856 211 $userpic = $OUTPUT->user_picture($item, ['link' => false, 'visibletoscreenreaders' => false]);
7bd4a361 212
65c134f9 213 $line = array(
4e9b0c42
SR
214 $OUTPUT->action_icon($this->format_link('user', $item->id), new pix_icon('t/editstring', ''), null,
215 ['title' => $iconstring, 'aria-label' => $iconstring]),
8a944856 216 html_writer::link($url, $userpic . $fullname),
7bd4a361
ZD
217 $this->item_range()
218 );
43cd76e8
JC
219 $lineclasses = array(
220 "action",
221 "user",
222 "range"
223 );
224 $outputline = array();
225 $i = 0;
226 foreach ($line as $key => $value) {
227 $cell = new \html_table_cell($value);
228 if ($isheader = $i == 1) {
229 $cell->header = $isheader;
230 $cell->scope = "row";
231 }
232 if (array_key_exists($key, $lineclasses)) {
233 $cell->attributes['class'] = $lineclasses[$key];
234 }
235 $outputline[] = $cell;
236 $i++;
237 }
7bd4a361 238
43cd76e8 239 return $this->format_definition($outputline, $grade);
7bd4a361
ZD
240 }
241
57fac09a
DW
242 /**
243 * Get the range ui element for this grade_item
244 *
245 * @return element;
246 */
7bd4a361
ZD
247 public function item_range() {
248 if (empty($this->range)) {
57fac09a 249 $this->range = new range($this->item);
7bd4a361
ZD
250 }
251
252 return $this->range;
253 }
254
57fac09a
DW
255 /**
256 * Does this page require paging?
257 *
258 * @return bool
259 */
7bd4a361 260 public function supports_paging() {
65c134f9 261 return $this->requirespaging;
7bd4a361
ZD
262 }
263
57fac09a
DW
264 /**
265 * Get the pager for this page.
266 *
267 * @return string
268 */
7bd4a361
ZD
269 public function pager() {
270 global $OUTPUT;
271
272 return $OUTPUT->paging_bar(
8ec7b088 273 $this->totalitemcount, $this->page, $this->perpage,
d124a60b 274 new moodle_url('/grade/report/singleview/index.php', array(
7bd4a361
ZD
275 'perpage' => $this->perpage,
276 'id' => $this->courseid,
aac66bef 277 'group' => $this->groupid,
7bd4a361
ZD
278 'itemid' => $this->itemid,
279 'item' => 'grade'
280 ))
281 );
282 }
283
57fac09a
DW
284 /**
285 * Get the heading for this page.
286 *
287 * @return string
288 */
7bd4a361 289 public function heading() {
2a6cdc51 290 return get_string('gradeitem', 'gradereport_singleview', $this->item->get_name());
7bd4a361 291 }
57fac09a 292
43cd76e8
JC
293 /**
294 * Get the summary for this table.
295 *
296 * @return string
297 */
298 public function summary() {
299 return get_string('summarygrade', 'gradereport_singleview');
300 }
301
57fac09a
DW
302 /**
303 * Process the data from the form.
304 *
305 * @param array $data
306 * @return array of warnings
307 */
308 public function process($data) {
309 $bulk = new bulk_insert($this->item);
310 // Bulk insert messages the data to be passed in
311 // ie: for all grades of empty grades apply the specified value.
312 if ($bulk->is_applied($data)) {
313 $filter = $bulk->get_type($data);
314 $insertvalue = $bulk->get_insert_value($data);
315 // Appropriately massage data that may not exist.
316 if ($this->supports_paging()) {
317 $gradeitem = grade_item::fetch(array(
318 'courseid' => $this->courseid,
319 'id' => $this->item->id
320 ));
321
322 $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
323
324 foreach ($this->items as $itemid => $item) {
325 $field = "finalgrade_{$gradeitem->id}_{$itemid}";
326 if (isset($data->$field)) {
327 continue;
328 }
329
330 $grade = grade_grade::fetch(array(
331 'itemid' => $gradeitem->id,
332 'userid' => $itemid
333 ));
334
335 $data->$field = empty($grade) ? $null : $grade->finalgrade;
336 $data->{"old$field"} = $data->$field;
337 }
338 }
339
340 foreach ($data as $varname => $value) {
bd1537b6 341 if (preg_match('/^oldoverride_(\d+)_(\d+)/', $varname, $matches)) {
81e32515
AG
342 // If we've selected overriding all grades.
343 if ($filter == 'all') {
bd1537b6
DC
344 $override = "override_{$matches[1]}_{$matches[2]}";
345 $data->$override = '1';
346 }
50e30bd4 347 }
57fac09a
DW
348 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
349 continue;
350 }
351
352 $gradeitem = grade_item::fetch(array(
353 'courseid' => $this->courseid,
354 'id' => $matches[1]
355 ));
356
357 $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
358
359 $empties = (trim($value) === '' or ($isscale and $value == -1));
360
361 if ($filter == 'all' or $empties) {
362 $data->$varname = ($isscale and empty($insertvalue)) ?
363 -1 : $insertvalue;
364 }
365 }
366 }
367
368 return parent::process($data);
369 }
370
7bd4a361 371}