-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocallib.php
More file actions
445 lines (387 loc) · 14.2 KB
/
locallib.php
File metadata and controls
445 lines (387 loc) · 14.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?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/>.
/**
* This file contains the definition for the library class for ILSP submission plugin.
*
* @package assignsubmission_ilsp
* @copyright 2019 Lancaster University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/user/profile/lib.php");
require_once($CFG->dirroot . '/mod/assign/submission/ilsp/renderable.php');
require_once($CFG->dirroot . '/mod/assign/submission/ilsp/lib.php');
require_once($CFG->dirroot . '/user/lib.php');
/**
* ILSP assignment submission plugin class definition.
*/
class assign_submission_ilsp extends assign_submission_plugin {
/**
* @var array Coversheet cache.
*/
protected $coversheets = [];
/**
* @var string
*/
private string $htmlid;
/**
* Get the name of the ilsp submission plugin
* @return string
*/
public function get_name() {
return get_string('ilsp', 'assignsubmission_ilsp');
}
/**
* Get the settings for ilsp submission plugin
*
* @param MoodleQuickForm $mform The form to add elements to
* @return void
*/
public function get_settings(MoodleQuickForm $mform) {
global $CFG, $COURSE;
}
/**
* Save the settings for ilsp submission plugin
*
* @param stdClass $data
* @return bool
*/
public function save_settings(stdClass $data) {
return true;
}
/**
* Add form elements for settings
*
* @param mixed $submission can be null
* @param MoodleQuickForm $mform
* @param stdClass $data
* @return true if elements were added to the form
*/
public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
return true;
}
/**
* Editor format options
*
* @return array
*/
private function get_edit_options() {
$editoroptions = [];
return $editoroptions;
}
/**
* Save data to the database and trigger plagiarism plugin,
* if enabled, to scan the uploaded content via events trigger
*
* @param stdClass $submission
* @param stdClass $data
* @return bool
*/
public function save(stdClass $submission, stdClass $data) {
return true;
}
/**
* Return a list of the text fields that can be imported/exported by this plugin.
* This appears on the grading worksheet.
*
* @return array An array of field names and descriptions. (name=>description, ...)
*/
public function get_editor_fields() {
return ['ilsp' => get_string('pluginname', 'assignsubmission_ilsp')];
}
/**
* Load the submission object from its id.
*
* @param int $submissionid The id of the submission we want
* @return stdClass The submission
*/
protected function get_submission($submissionid) {
global $DB;
$params = ['assignment' => $this->assignment->get_instance()->id, 'id' => $submissionid];
return $DB->get_record('assign_submission', $params, '*', MUST_EXIST);
}
/**
* Get the saved text content from the editor.
* This appears on the grading worksheet.
*
* @param string $name
* @param int $submissionid
* @return string
*/
public function get_editor_text($name, $submissionid) {
// Get the submission back so we can get the user.
$submission = $this->get_submission($submissionid);
$user = user_get_users_by_id([$submission->userid])[$submission->userid];
// Get the valid ilsps and map to their names.
$ilsps = $this->get_ilsp($user);
$ilsps = array_map(function ($ilsps) {
return $ilsps['name'];
}, $ilsps);
return implode(', ', $ilsps);
}
/**
* Get the content format for the editor
*
* @param string $name
* @param int $submissionid
* @return int
*/
public function get_editor_format($name, $submissionid) {
return 0;
}
/**
* Returns an array of links to the given list of named files.
*
* @param array $files Files keyed by display name
* @return array Links to download the files
*/
protected function link_files($files) {
$links = [];
foreach ($files as $name => $file) {
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(),
$file->get_itemid(), $file->get_filepath(), $file->get_filename());
$links[] = html_writer::link($url, $name);
}
return $links;
}
/**
* Display ilsp word count in the submission status table
*
* @param stdClass $submission
* @param bool $showviewlink - If the summary has been truncated set this to true
* @return string
*/
public function view_summary(stdClass $submission, &$showviewlink) {
global $PAGE;
// Get users to get ilsps.
$user = user_get_users_by_id([$submission->userid])[$submission->userid];
$ilsps = $this->get_ilsp($user);
if (empty($ilsps)) {
return '';
}
$html = '';
// Put a header on the grading panel page.
if ($PAGE->pagetype == 'mod-assign-gradingpanel') {
$html .= html_writer::tag('h3', get_string('ilsps', 'assignsubmission_ilsp'));
$html .= html_writer::tag('p', get_string('assignsubmission_ilsp_description', 'assignsubmission_ilsp'));
}
$ilspfiles = new ilsp_files($ilsps);
$this->htmlid = html_writer::random_id('assign_ilsp');
$html .= html_writer::start_div('', ['id' => $this->htmlid]);
$html .= html_writer::alist($this->link_files($ilspfiles->files));
$html .= html_writer::end_div();
return $html;
}
/**
* Generate the ilsps for this user.
*
* @param $user stdClass Current user
* @return array|void
* @throws coding_exception
* @throws dml_exception
*/
protected function get_ilsp($user) {
$this->generate_coversheets();
$ilsps = $this->coversheets;
if (empty($profiledyslexia = get_config('assignsubmission_ilsp', 'dyslexia_profile')) ||
empty($profiledyscalculia = get_config('assignsubmission_ilsp', 'dyscalculia_profile')) ||
empty($profiledeafness = get_config('assignsubmission_ilsp', 'deafness_profile')) ||
empty($profilevisualimpairment = get_config('assignsubmission_ilsp', 'visualimpairment_profile'))) {
return;
}
// Get the extra profile fields.
profile_load_data($user);
// Set the enabled flags and use them to trim the ilsp array to only return enabled ones.
$ilsps['ilsp_coversheets_dyslexia']['enabled'] = !empty($user->{'profile_field_'.$profiledyslexia});
$ilsps['ilsp_coversheets_dyscalculia']['enabled'] = !empty($user->{'profile_field_'.$profiledyscalculia});
$ilsps['ilsp_coversheets_deafness']['enabled'] = !empty($user->{'profile_field_'.$profiledeafness});
$ilsps['ilsp_coversheets_visualimpairment']['enabled'] = !empty($user->{'profile_field_'.$profilevisualimpairment});
foreach ($ilsps as $type => $value) {
if (!$value['enabled'] || !isset($value['itemid'])) {
unset($ilsps[$type]);
}
}
return $ilsps;
}
/**
* Generate the generic coversheet information.
*
* @throws coding_exception
* @throws dml_exception
*/
protected function generate_coversheets() {
if (count($this->coversheets) == 0) {
// Get the config and set up the basic parts of the coversheet array.
if (empty($namedyslexia = get_config('assignsubmission_ilsp', 'dyslexia_name')) ||
empty($namedyscalculia = get_config('assignsubmission_ilsp', 'dyscalculia_name')) ||
empty($namedeafness = get_config('assignsubmission_ilsp', 'deafness_name')) ||
empty($namevisualimpairment = get_config('assignsubmission_ilsp', 'visualimpairment_name'))) {
return;
}
$ilsps['ilsp_coversheets_dyslexia']['itemid'] = ILSP_COVERSHEETS_DYSLEXIA;
$ilsps['ilsp_coversheets_dyscalculia']['itemid'] = ILSP_COVERSHEETS_DYSCALCULIA;
$ilsps['ilsp_coversheets_deafness']['itemid'] = ILSP_COVERSHEETS_DEAFNESS;
$ilsps['ilsp_coversheets_visualimpairment']['itemid'] = ILSP_COVERSHEETS_VISUALIMPAIRMENT;
$ilsps['ilsp_coversheets_dyslexia']['name'] = $namedyslexia;
$ilsps['ilsp_coversheets_dyscalculia']['name'] = $namedyscalculia;
$ilsps['ilsp_coversheets_deafness']['name'] = $namedeafness;
$ilsps['ilsp_coversheets_visualimpairment']['name'] = $namevisualimpairment;
// Load the coversheet files from the file store.
$fs = get_file_storage();
$syscontext = context_system::instance();
foreach ($ilsps as $key => $ilsp) {
$files = $fs->get_area_files($syscontext->id,
'assignsubmission_ilsp',
'ilsp_coversheets',
$ilsp['itemid'],
'timemodified',
false);
if (!count($files)) {
unset($ilsps[$key]);
continue;
}
$ilsps[$key]['file'] = array_shift($files);
}
$this->coversheets = $ilsps;
}
}
/**
* Produce a list of files suitable for export that represent this submission.
*
* @param stdClass $submission - For this is the submission data
* @param stdClass $user - This is the user record for this submission
* @return array - return an array of files indexed by filename
*/
public function get_files(stdClass $submission, stdClass $user) {
global $DB;
$ilsps = $this->get_ilsp($user);
// Iterate over the enabled ilsps and return the appropriate coversheet files.
$result = [];
foreach ($ilsps as $ilsp) {
$file = $ilsp['file'];
// Do we return the full folder path or just the file name?
if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
$result[$file->get_filename()] = $file;
} else {
$result[$file->get_filepath().$file->get_filename()] = $file;
}
}
return $result;
}
/**
* Display the saved text content from the editor in the view table
*
* @param stdClass $submission
* @return string
*/
public function view(stdClass $submission) {
return '';
}
/**
* Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
*
* @param string $type old assignment subtype
* @param int $version old assignment version
* @return bool True if upgrade is possible
*/
public function can_upgrade($type, $version) {
return false;
}
/**
* Upgrade the settings from the old assignment to the new plugin based one
*
* @param context $oldcontext - the database for the old assignment context
* @param stdClass $oldassignment - the database for the old assignment instance
* @param string $log record log events here
* @return bool Was it a success?
*/
public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
return true;
}
/**
* The assignment has been deleted - cleanup
*
* @return bool
*/
public function delete_instance() {
return true;
}
/**
* No text is set for this plugin
*
* @param stdClass $submission
* @return bool
*/
public function is_empty(stdClass $submission) {
return true;
}
/**
* If true, the plugin will appear on the module settings page and can be
* enabled/disabled per assignment instance.
*
* @return bool
*/
public function is_configurable() {
return false;
}
/**
* The ILSP plugin has no submission component so should not be counted
* when determining whether to show the edit submission link.
*
* @return bool
*/
public function allow_submissions() {
return false;
}
/**
* Allows hiding this plugin from the submission/feedback screen if it is not enabled.
*
* @return bool - if false - this plugin will not accept submissions / feedback
*/
public function is_enabled() {
if ($this->assignment->has_instance() && $this->assignment->get_instance()->teamsubmission) {
return false;
}
return true;
}
/**
* Determine if a submission is empty
*
* This is distinct from is_empty in that it is intended to be used to
* determine if a submission made before saving is empty.
*
* @param stdClass $data The submission data
* @return bool
*/
public function submission_is_empty(stdClass $data) {
return false;
}
/**
* Return the plugin configs for external functions.
*
* @return array the list of settings
*/
public function get_config_for_external() {
return (array) $this->get_config();
}
/**
* Get file areas returns a list of areas this plugin stores files
* @return array - An array of fileareas (keys) and descriptions (values)
*/
public function get_file_areas() {
return ['ilsp_coversheets' => $this->get_name()];
}
}