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