MDL-48273 singleview: add load_user method and other group validations
[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
57fac09a
DW
74 /**
75 * Get the description of this page
76 * @return string
77 */
7bd4a361
ZD
78 public function description() {
79 return get_string('users');
80 }
81
57fac09a
DW
82 /**
83 * Convert this list of items into an options list
84 *
85 * @return array
86 */
7bd4a361 87 public function options() {
57fac09a
DW
88 $options = array();
89 foreach ($this->items as $userid => $user) {
90 $options[$userid] = fullname($user);
91 }
0a55da57 92
0a55da57 93 return $options;
7bd4a361
ZD
94 }
95
57fac09a
DW
96 /**
97 * Return the type of the things in this list.
98 * @return string
99 */
7bd4a361
ZD
100 public function item_type() {
101 return 'user';
102 }
103
57fac09a
DW
104 /**
105 * Get the original settings for this item
106 * @return array
107 */
7bd4a361
ZD
108 public function original_definition() {
109 $def = array('finalgrade', 'feedback');
110
5b257154 111 $def[] = 'override';
7bd4a361
ZD
112
113 $def[] = 'exclude';
114
115 return $def;
116 }
117
57fac09a
DW
118 /**
119 * Init this page
120 *
121 * @param bool $selfitemisempty True if we have not selected a user.
122 */
65c134f9 123 public function init($selfitemisempty = false) {
8ec7b088 124
aac66bef
SL
125 $this->items = $this->load_users();
126 $this->totalitemcount = count($this->items);
7bd4a361 127
65c134f9 128 if ($selfitemisempty) {
7bd4a361
ZD
129 return;
130 }
131
7bd4a361
ZD
132 $params = array(
133 'id' => $this->itemid,
134 'courseid' => $this->courseid
135 );
136
137 $this->item = grade_item::fetch($params);
57fac09a
DW
138 if (!self::filter($this->item)) {
139 $this->items = array();
140 $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview'));
7bd4a361
ZD
141 }
142
65c134f9 143 $this->requiresextra = !$this->item->is_manual_item();
7bd4a361
ZD
144
145 $this->setup_structure();
146
147 $this->set_definition($this->original_definition());
148 $this->set_headers($this->original_headers());
149 }
150
57fac09a
DW
151 /**
152 * Get the table headers
153 *
154 * @return array
155 */
7bd4a361 156 public function original_headers() {
8ec7b088 157 return array(
65c134f9 158 '', // For filter icon.
7bd4a361
ZD
159 get_string('firstname') . ' (' . get_string('alternatename') . ') ' . get_string('lastname'),
160 get_string('range', 'grades'),
161 get_string('grade', 'grades'),
8ec7b088
ZD
162 get_string('feedback', 'grades'),
163 $this->make_toggle_links('override'),
164 $this->make_toggle_links('exclude')
7bd4a361 165 );
7bd4a361
ZD
166 }
167
57fac09a
DW
168 /**
169 * Format a row in the table
170 *
171 * @param user $item
172 * @return string
173 */
7bd4a361
ZD
174 public function format_line($item) {
175 global $OUTPUT;
176
177 $grade = $this->fetch_grade_or_default($this->item, $item->id);
178
7bd4a361
ZD
179 $lockicon = '';
180
65c134f9
ZD
181 $lockedgrade = $lockedgradeitem = 0;
182 if (!empty($grade->locked)) {
183 $lockedgrade = 1;
184 }
185 if (!empty($grade->grade_item->locked)) {
186 $lockedgradeitem = 1;
187 }
188 // Check both grade and grade item.
57fac09a 189 if ( $lockedgrade || $lockedgradeitem ) {
7bd4a361 190 $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked') . ' ';
57fac09a 191 }
7bd4a361
ZD
192
193 if (!empty($item->alternatename)) {
194 $fullname = $lockicon . $item->alternatename . ' (' . $item->firstname . ') ' . $item->lastname;
195 } else {
196 $fullname = $lockicon . fullname($item);
197 }
198
199 $item->imagealt = $fullname;
508f23f1 200 $url = new moodle_url("/user/view.php", array('id' => $item->id, 'course' => $this->courseid));
65c134f9 201 $iconstring = get_string('filtergrades', 'gradereport_singleview', $fullname);
7bd3932e 202 $grade->label = $fullname;
7bd4a361 203
65c134f9
ZD
204 $line = array(
205 $OUTPUT->action_icon($this->format_link('user', $item->id), new pix_icon('t/editstring', $iconstring)),
43cd76e8 206 $OUTPUT->user_picture($item, array('visibletoscreenreaders' => false)) .
92c61fee 207 html_writer::link($url, $fullname),
7bd4a361
ZD
208 $this->item_range()
209 );
43cd76e8
JC
210 $lineclasses = array(
211 "action",
212 "user",
213 "range"
214 );
215 $outputline = array();
216 $i = 0;
217 foreach ($line as $key => $value) {
218 $cell = new \html_table_cell($value);
219 if ($isheader = $i == 1) {
220 $cell->header = $isheader;
221 $cell->scope = "row";
222 }
223 if (array_key_exists($key, $lineclasses)) {
224 $cell->attributes['class'] = $lineclasses[$key];
225 }
226 $outputline[] = $cell;
227 $i++;
228 }
7bd4a361 229
43cd76e8 230 return $this->format_definition($outputline, $grade);
7bd4a361
ZD
231 }
232
57fac09a
DW
233 /**
234 * Get the range ui element for this grade_item
235 *
236 * @return element;
237 */
7bd4a361
ZD
238 public function item_range() {
239 if (empty($this->range)) {
57fac09a 240 $this->range = new range($this->item);
7bd4a361
ZD
241 }
242
243 return $this->range;
244 }
245
57fac09a
DW
246 /**
247 * Does this page require paging?
248 *
249 * @return bool
250 */
7bd4a361 251 public function supports_paging() {
65c134f9 252 return $this->requirespaging;
7bd4a361
ZD
253 }
254
57fac09a
DW
255 /**
256 * Get the pager for this page.
257 *
258 * @return string
259 */
7bd4a361
ZD
260 public function pager() {
261 global $OUTPUT;
262
263 return $OUTPUT->paging_bar(
8ec7b088 264 $this->totalitemcount, $this->page, $this->perpage,
d124a60b 265 new moodle_url('/grade/report/singleview/index.php', array(
7bd4a361
ZD
266 'perpage' => $this->perpage,
267 'id' => $this->courseid,
aac66bef 268 'group' => $this->groupid,
7bd4a361
ZD
269 'itemid' => $this->itemid,
270 'item' => 'grade'
271 ))
272 );
273 }
274
57fac09a
DW
275 /**
276 * Get the heading for this page.
277 *
278 * @return string
279 */
7bd4a361
ZD
280 public function heading() {
281 return $this->item->get_name();
282 }
57fac09a 283
43cd76e8
JC
284 /**
285 * Get the summary for this table.
286 *
287 * @return string
288 */
289 public function summary() {
290 return get_string('summarygrade', 'gradereport_singleview');
291 }
292
57fac09a
DW
293 /**
294 * Process the data from the form.
295 *
296 * @param array $data
297 * @return array of warnings
298 */
299 public function process($data) {
300 $bulk = new bulk_insert($this->item);
301 // Bulk insert messages the data to be passed in
302 // ie: for all grades of empty grades apply the specified value.
303 if ($bulk->is_applied($data)) {
304 $filter = $bulk->get_type($data);
305 $insertvalue = $bulk->get_insert_value($data);
306 // Appropriately massage data that may not exist.
307 if ($this->supports_paging()) {
308 $gradeitem = grade_item::fetch(array(
309 'courseid' => $this->courseid,
310 'id' => $this->item->id
311 ));
312
313 $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
314
315 foreach ($this->items as $itemid => $item) {
316 $field = "finalgrade_{$gradeitem->id}_{$itemid}";
317 if (isset($data->$field)) {
318 continue;
319 }
320
321 $grade = grade_grade::fetch(array(
322 'itemid' => $gradeitem->id,
323 'userid' => $itemid
324 ));
325
326 $data->$field = empty($grade) ? $null : $grade->finalgrade;
327 $data->{"old$field"} = $data->$field;
4fb6b62c
ZD
328
329 preg_match('/_(\d+)_(\d+)/', $field, $oldoverride);
330 $oldoverride = 'oldoverride' . $oldoverride[0];
331 if (empty($data->$oldoverride)) {
332 $data->$field = (!isset($grade->rawgrade)) ? $null : $grade->rawgrade;
333 }
57fac09a
DW
334 }
335 }
336
337 foreach ($data as $varname => $value) {
50e30bd4
ZD
338 if (preg_match('/override_(\d+)_(\d+)/', $varname, $matches)) {
339 $data->$matches[0] = '1';
340 }
57fac09a
DW
341 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
342 continue;
343 }
344
345 $gradeitem = grade_item::fetch(array(
346 'courseid' => $this->courseid,
347 'id' => $matches[1]
348 ));
349
350 $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
351
352 $empties = (trim($value) === '' or ($isscale and $value == -1));
353
354 if ($filter == 'all' or $empties) {
355 $data->$varname = ($isscale and empty($insertvalue)) ?
356 -1 : $insertvalue;
357 }
358 }
359 }
360
361 return parent::process($data);
362 }
363
7bd4a361 364}