mirrored from git://git.moodle.org/moodle.git
/
screen.php
365 lines (302 loc) · 9.76 KB
/
screen.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
363
364
365
<?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/>.
/**
* Abstract class used as a base for the 3 screens.
*
* @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 context_course;
use moodle_url;
use html_writer;
use grade_structure;
use grade_grade;
use grade_item;
use stdClass;
defined('MOODLE_INTERNAL') || die;
/**
* Abstract class used as a base for the 3 screens.
*
* @package gradereport_singleview
* @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class screen {
/** @var int $courseid The id of the course */
protected $courseid;
/** @var int $itemid Either a user id or a grade_item id */
protected $itemid;
/** @var int $groupid The currently set groupid (if set) */
protected $groupid;
/** @var course_context $context The course context */
protected $context;
/** @var int $page The page number */
protected $page;
/** @var int $perpage Results per page */
protected $perpage;
/** @var array $items List of items on the page, they could be users or grade_items */
protected $items;
/**
* Constructor
*
* @param int $courseid The course id
* @param int $itemid The item id
* @param int $groupid The group id
*/
public function __construct($courseid, $itemid, $groupid = null) {
global $DB;
$this->courseid = $courseid;
$this->itemid = $itemid;
$this->groupid = $groupid;
$this->context = context_course::instance($this->courseid);
$this->course = $DB->get_record('course', array('id' => $courseid));
$this->page = optional_param('page', 0, PARAM_INT);
$this->perpage = optional_param('perpage', 100, PARAM_INT);
if ($this->perpage > 100) {
$this->perpage = 100;
}
$this->init(empty($itemid));
}
/**
* Cache the grade_structure class
*/
public function setup_structure() {
$this->structure = new grade_structure();
$this->structure->modinfo = get_fast_modinfo($this->course);
}
/**
* Create a nice link from a thing (user or grade_item).
*
* @param string $screen
* @param int $itemid
* @param bool $display Should we wrap this in an anchor ?
* @return string The link
*/
public function format_link($screen, $itemid, $display = null) {
$url = new moodle_url('/grade/report/singleview/index.php', array(
'id' => $this->courseid,
'item' => $screen,
'itemid' => $itemid,
'group' => $this->groupid,
));
if ($display) {
return html_writer::link($url, $display);
} else {
return $url;
}
}
/**
* Get the grade_grade
*
* @param grade_item $item The grade_item
* @param int $userid The user id
* @return grade_grade
*/
public function fetch_grade_or_default($item, $userid) {
$grade = grade_grade::fetch(array(
'itemid' => $item->id, 'userid' => $userid
));
if (!$grade) {
$default = new stdClass;
$default->userid = $userid;
$default->itemid = $item->id;
$default->feedback = '';
$grade = new grade_grade($default, false);
}
$grade->grade_item = $item;
return $grade;
}
/**
* Make the HTML element that toggles all the checkboxes on or off.
*
* @param string $key A unique key for this control - inserted in the classes.
* @return string
*/
public function make_toggle($key) {
$attrs = array('href' => '#');
$all = html_writer::tag('a', get_string('all'), $attrs + array(
'class' => 'include all ' . $key
));
$none = html_writer::tag('a', get_string('none'), $attrs + array(
'class' => 'include none ' . $key
));
return html_writer::tag('span', "$all / $none", array(
'class' => 'inclusion_links'
));
}
/**
* Make a toggle link with some text before it.
*
* @param string $key A unique key for this control - inserted in the classes.
* @return string
*/
public function make_toggle_links($key) {
return get_string($key, 'gradereport_singleview') . ' ' .
$this->make_toggle($key);
}
/**
* Get the default heading for the screen.
*
* @return string
*/
public function heading() {
return get_string('pluginname', 'gradereport_singleview');
}
/**
* Override this to init the screen.
*
* @param boolean $selfitemisempty True if no item has been selected yet.
*/
public abstract function init($selfitemisempty = false);
/**
* Get the type of items in the list.
*
* @return string
*/
public abstract function item_type();
/**
* Get the entire screen as a string.
*
* @return string
*/
public abstract function html();
/**
* Does this screen support paging?
*
* @return bool
*/
public function supports_paging() {
return true;
}
/**
* Default pager
*
* @return string
*/
public function pager() {
return '';
}
/**
* Initialise the js for this screen.
*/
public function js() {
global $PAGE;
$module = array(
'name' => 'gradereport_singleview',
'fullpath' => '/grade/report/singleview/js/singleview.js',
'requires' => array('base', 'dom', 'event', 'event-simulate', 'io-base')
);
$PAGE->requires->js_init_call('M.gradereport_singleview.init', array(), false, $module);
}
/**
* Process the data from a form submission.
*
* @param array $data
* @return array of warnings
*/
public function process($data) {
$warnings = array();
$fields = $this->definition();
// Avoiding execution timeouts when updating
// a large amount of grades.
$progress = 0;
$progressbar = new \core\progress\display_if_slow();
$progressbar->start_html();
$progressbar->start_progress(get_string('savegrades', 'gradereport_singleview'), count((array) $data) - 1);
$changecount = array();
foreach ($data as $varname => $throw) {
$progressbar->progress($progress);
$progress++;
if (preg_match("/(\w+)_(\d+)_(\d+)/", $varname, $matches)) {
$itemid = $matches[2];
$userid = $matches[3];
} else {
continue;
}
$gradeitem = grade_item::fetch(array(
'id' => $itemid, 'courseid' => $this->courseid
));
if (preg_match('/^old[oe]{1}/', $varname)) {
$elementname = preg_replace('/^old/', '', $varname);
if (!isset($data->$elementname)) {
// Decrease the progress because we've increased the
// size of the array we are iterating through.
$progress--;
$data->$elementname = false;
}
}
if (!in_array($matches[1], $fields)) {
continue;
}
if (!$gradeitem) {
continue;
}
$grade = $this->fetch_grade_or_default($gradeitem, $userid);
$classname = '\\gradereport_singleview\\local\\ui\\' . $matches[1];
$element = new $classname($grade);
$name = $element->get_name();
$oldname = "old$name";
$posted = $data->$name;
$format = $element->determine_format();
if ($format->is_textbox() and trim($data->$name) === '') {
$data->$name = null;
}
// Same value; skip.
if (isset($data->$oldname) && $data->$oldname == $posted) {
continue;
}
$msg = $element->set($posted);
// Optional type.
if (!empty($msg)) {
$warnings[] = $msg;
}
if (preg_match('/_(\d+)_(\d+)/', $varname, $matchelement)) {
$changecount[$matchelement[0]] = 1;
}
}
// Some post-processing.
$eventdata = new stdClass;
$eventdata->warnings = $warnings;
$eventdata->post_data = $data;
$eventdata->instance = $this;
$eventdata->changecount = $changecount;
$progressbar->end_html();
return $eventdata;
}
/**
* By default there are no options.
* @return array
*/
public function options() {
return array();
}
/**
* Should we show the group selector?
* @return bool
*/
public function display_group_selector() {
return true;
}
/**
* Should we show the next prev selector?
* @return bool
*/
public function supports_next_prev() {
return true;
}
}