MDL-39913 filetypes: mimetypes remediation
[moodle.git] / mod / assign / submission / file / locallib.php
CommitLineData
bbd0e548
DW
1<?php
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/**
18 * This file contains the definition for the library class for file submission plugin
19 *
20 * This class provides all the functionality for the new assign module.
21 *
22 * @package assignsubmission_file
23 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
bbd0e548
DW
27require_once($CFG->libdir.'/eventslib.php');
28
29defined('MOODLE_INTERNAL') || die();
e5403f8c
DW
30
31// File areas for file submission assignment.
8ab7bb9f
DW
32define('ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES', 5);
33define('ASSIGNSUBMISSION_FILE_FILEAREA', 'submission_files');
bbd0e548
DW
34
35/**
e5403f8c 36 * Library class for file submission plugin extending submission plugin base class
bbd0e548
DW
37 *
38 * @package assignsubmission_file
39 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class assign_submission_file extends assign_submission_plugin {
43
44 /**
45 * Get the name of the file submission plugin
46 * @return string
47 */
48 public function get_name() {
49 return get_string('file', 'assignsubmission_file');
50 }
51
52 /**
53 * Get file submission information from the database
54 *
55 * @param int $submissionid
56 * @return mixed
57 */
58 private function get_file_submission($submissionid) {
59 global $DB;
60 return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid));
61 }
62
63 /**
64 * Get the default setting for file submission plugin
65 *
66 * @param MoodleQuickForm $mform The form to add elements to
67 * @return void
68 */
69 public function get_settings(MoodleQuickForm $mform) {
70 global $CFG, $COURSE;
71
72 $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
73 $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
74
75 $settings = array();
76 $options = array();
35e7ad6a 77 for ($i = 1; $i <= get_config('assignsubmission_file', 'maxfiles'); $i++) {
bbd0e548
DW
78 $options[$i] = $i;
79 }
80
e5403f8c
DW
81 $name = get_string('maxfilessubmission', 'assignsubmission_file');
82 $mform->addElement('select', 'assignsubmission_file_maxfiles', $name, $options);
83 $mform->addHelpButton('assignsubmission_file_maxfiles',
84 'maxfilessubmission',
85 'assignsubmission_file');
bbd0e548 86 $mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions);
b0da618b 87 $mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'notchecked');
bbd0e548 88
e5403f8c
DW
89 $choices = get_max_upload_sizes($CFG->maxbytes,
90 $COURSE->maxbytes,
91 get_config('assignsubmission_file', 'maxbytes'));
6d976f3f 92
bbd0e548
DW
93 $settings[] = array('type' => 'select',
94 'name' => 'maxsubmissionsizebytes',
95 'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
96 'options'=> $choices,
97 'default'=> $defaultmaxsubmissionsizebytes);
98
e5403f8c
DW
99 $name = get_string('maximumsubmissionsize', 'assignsubmission_file');
100 $mform->addElement('select', 'assignsubmission_file_maxsizebytes', $name, $choices);
101 $mform->addHelpButton('assignsubmission_file_maxsizebytes',
102 'maximumsubmissionsize',
103 'assignsubmission_file');
bbd0e548 104 $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);
e5403f8c
DW
105 $mform->disabledIf('assignsubmission_file_maxsizebytes',
106 'assignsubmission_file_enabled',
b0da618b 107 'notchecked');
bbd0e548
DW
108 }
109
110 /**
111 * Save the settings for file submission plugin
112 *
113 * @param stdClass $data
114 * @return bool
115 */
116 public function save_settings(stdClass $data) {
117 $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
118 $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
119 return true;
120 }
121
122 /**
123 * File format options
124 *
125 * @return array
126 */
127 private function get_file_options() {
151b0f94
DW
128 $fileoptions = array('subdirs' => 1,
129 'maxbytes' => $this->get_config('maxsubmissionsizebytes'),
130 'maxfiles' => $this->get_config('maxfilesubmissions'),
131 'accepted_types' => '*',
132 'return_types' => (FILE_INTERNAL | FILE_CONTROLLED_LINK));
6ba716c1
DB
133 if ($fileoptions['maxbytes'] == 0) {
134 // Use module default.
135 $fileoptions['maxbytes'] = get_config('assignsubmission_file', 'maxbytes');
136 }
bbd0e548
DW
137 return $fileoptions;
138 }
139
140 /**
141 * Add elements to submission form
142 *
143 * @param mixed $submission stdClass|null
144 * @param MoodleQuickForm $mform
145 * @param stdClass $data
146 * @return bool
147 */
148 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
149
150 if ($this->get_config('maxfilesubmissions') <= 0) {
151 return false;
152 }
153
154 $fileoptions = $this->get_file_options();
155 $submissionid = $submission ? $submission->id : 0;
156
e5403f8c
DW
157 $data = file_prepare_standard_filemanager($data,
158 'files',
159 $fileoptions,
160 $this->assignment->get_context(),
161 'assignsubmission_file',
162 ASSIGNSUBMISSION_FILE_FILEAREA,
163 $submissionid);
3a5c3b3c 164 $mform->addElement('filemanager', 'files_filemanager', $this->get_name(), null, $fileoptions);
7e5be074 165
bbd0e548
DW
166 return true;
167 }
168
169 /**
170 * Count the number of files
171 *
172 * @param int $submissionid
173 * @param string $area
174 * @return int
175 */
176 private function count_files($submissionid, $area) {
bbd0e548 177 $fs = get_file_storage();
e5403f8c
DW
178 $files = $fs->get_area_files($this->assignment->get_context()->id,
179 'assignsubmission_file',
180 $area,
181 $submissionid,
182 'id',
183 false);
bbd0e548
DW
184
185 return count($files);
186 }
187
188 /**
e5403f8c
DW
189 * Save the files and trigger plagiarism plugin, if enabled,
190 * to scan the uploaded files via events trigger
bbd0e548
DW
191 *
192 * @param stdClass $submission
193 * @param stdClass $data
194 * @return bool
195 */
196 public function save(stdClass $submission, stdClass $data) {
197 global $USER, $DB;
198
199 $fileoptions = $this->get_file_options();
200
e5403f8c
DW
201 $data = file_postupdate_standard_filemanager($data,
202 'files',
203 $fileoptions,
204 $this->assignment->get_context(),
205 'assignsubmission_file',
206 ASSIGNSUBMISSION_FILE_FILEAREA,
207 $submission->id);
bbd0e548
DW
208
209 $filesubmission = $this->get_file_submission($submission->id);
210
e5403f8c 211 // Plagiarism code event trigger when files are uploaded.
bbd0e548
DW
212
213 $fs = get_file_storage();
e5403f8c
DW
214 $files = $fs->get_area_files($this->assignment->get_context()->id,
215 'assignsubmission_file',
216 ASSIGNSUBMISSION_FILE_FILEAREA,
217 $submission->id,
218 'id',
219 false);
220
8ab7bb9f 221 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
e5403f8c 222
e3d2d064
FM
223 $params = array(
224 'context' => context_module::instance($this->assignment->get_course_module()->id),
d7f05ad4 225 'courseid' => $this->assignment->get_course()->id,
e3d2d064
FM
226 'objectid' => $submission->id,
227 'other' => array(
228 'content' => '',
229 'pathnamehashes' => array_keys($files)
230 )
231 );
5cfa441b 232 if (!empty($submission->userid) && ($submission->userid != $USER->id)) {
f5928e78 233 $params['relateduserid'] = $submission->userid;
5cfa441b 234 }
e3d2d064
FM
235 $event = \assignsubmission_file\event\assessable_uploaded::create($params);
236 $event->set_legacy_files($files);
237 $event->trigger();
bbd0e548 238
d7f05ad4
AG
239 $groupname = null;
240 $groupid = 0;
241 // Get the group name as other fields are not transcribed in the logs and this information is important.
242 if (empty($submission->userid) && !empty($submission->groupid)) {
243 $groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), '*', MUST_EXIST);
244 $groupid = $submission->groupid;
245 } else {
246 $params['relateduserid'] = $submission->userid;
247 }
248
249 // Unset the objectid and other field from params for use in submission events.
250 unset($params['objectid']);
251 unset($params['other']);
252 $params['other'] = array(
253 'submissionid' => $submission->id,
254 'submissionattempt' => $submission->attemptnumber,
255 'submissionstatus' => $submission->status,
256 'filesubmissioncount' => $count,
257 'groupid' => $groupid,
258 'groupname' => $groupname
259 );
260
bbd0e548 261 if ($filesubmission) {
e5403f8c
DW
262 $filesubmission->numfiles = $this->count_files($submission->id,
263 ASSIGNSUBMISSION_FILE_FILEAREA);
d7f05ad4
AG
264 $updatestatus = $DB->update_record('assignsubmission_file', $filesubmission);
265 $params['objectid'] = $filesubmission->id;
266
267 $event = \assignsubmission_file\event\submission_updated::create($params);
4781ff2e 268 $event->set_assign($this->assignment);
d7f05ad4
AG
269 $event->trigger();
270 return $updatestatus;
bbd0e548
DW
271 } else {
272 $filesubmission = new stdClass();
e5403f8c
DW
273 $filesubmission->numfiles = $this->count_files($submission->id,
274 ASSIGNSUBMISSION_FILE_FILEAREA);
bbd0e548
DW
275 $filesubmission->submission = $submission->id;
276 $filesubmission->assignment = $this->assignment->get_instance()->id;
d7f05ad4
AG
277 $filesubmission->id = $DB->insert_record('assignsubmission_file', $filesubmission);
278 $params['objectid'] = $filesubmission->id;
279
280 $event = \assignsubmission_file\event\submission_created::create($params);
4781ff2e 281 $event->set_assign($this->assignment);
d7f05ad4
AG
282 $event->trigger();
283 return $filesubmission->id > 0;
bbd0e548
DW
284 }
285 }
286
287 /**
288 * Produce a list of files suitable for export that represent this feedback or submission
289 *
290 * @param stdClass $submission The submission
1561a37c 291 * @param stdClass $user The user record - unused
bbd0e548
DW
292 * @return array - return an array of files indexed by filename
293 */
2406815b 294 public function get_files(stdClass $submission, stdClass $user) {
bbd0e548
DW
295 $result = array();
296 $fs = get_file_storage();
297
e5403f8c
DW
298 $files = $fs->get_area_files($this->assignment->get_context()->id,
299 'assignsubmission_file',
300 ASSIGNSUBMISSION_FILE_FILEAREA,
301 $submission->id,
302 'timemodified',
303 false);
bbd0e548
DW
304
305 foreach ($files as $file) {
1b467cb8
SB
306 // Do we return the full folder path or just the file name?
307 if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
308 $result[$file->get_filename()] = $file;
309 } else {
310 $result[$file->get_filepath().$file->get_filename()] = $file;
311 }
bbd0e548
DW
312 }
313 return $result;
314 }
315
316 /**
317 * Display the list of files in the submission status table
318 *
319 * @param stdClass $submission
320 * @param bool $showviewlink Set this to true if the list of files is long
321 * @return string
322 */
7ee1548a 323 public function view_summary(stdClass $submission, & $showviewlink) {
8ab7bb9f 324 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
bbd0e548 325
e5403f8c 326 // Show we show a link to view all files for this plugin?
8ab7bb9f
DW
327 $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;
328 if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {
e5403f8c
DW
329 return $this->assignment->render_area_files('assignsubmission_file',
330 ASSIGNSUBMISSION_FILE_FILEAREA,
331 $submission->id);
bbd0e548
DW
332 } else {
333 return get_string('countfiles', 'assignsubmission_file', $count);
334 }
335 }
336
337 /**
338 * No full submission view - the summary contains the list of files and that is the whole submission
339 *
340 * @param stdClass $submission
341 * @return string
342 */
343 public function view(stdClass $submission) {
e5403f8c
DW
344 return $this->assignment->render_area_files('assignsubmission_file',
345 ASSIGNSUBMISSION_FILE_FILEAREA,
346 $submission->id);
bbd0e548
DW
347 }
348
349
350
351 /**
352 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
353 * and version.
354 *
355 * @param string $type
356 * @param int $version
357 * @return bool True if upgrade is possible
358 */
359 public function can_upgrade($type, $version) {
360
361 $uploadsingletype ='uploadsingle';
362 $uploadtype ='upload';
363
364 if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
365 return true;
366 }
367 return false;
368 }
369
370
371 /**
372 * Upgrade the settings from the old assignment
373 * to the new plugin based one
374 *
375 * @param context $oldcontext - the old assignment context
376 * @param stdClass $oldassignment - the old assignment data record
377 * @param string $log record log events here
378 * @return bool Was it a success? (false will trigger rollback)
379 */
e5403f8c 380 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
01d29b39
DW
381 global $DB;
382
bbd0e548
DW
383 if ($oldassignment->assignmenttype == 'uploadsingle') {
384 $this->set_config('maxfilesubmissions', 1);
385 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
386 return true;
01d29b39 387 } else if ($oldassignment->assignmenttype == 'upload') {
bbd0e548
DW
388 $this->set_config('maxfilesubmissions', $oldassignment->var1);
389 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
01d29b39
DW
390
391 // Advanced file upload uses a different setting to do the same thing.
392 $DB->set_field('assign',
393 'submissiondrafts',
394 $oldassignment->var4,
395 array('id'=>$this->assignment->get_instance()->id));
f531ca72
DW
396
397 // Convert advanced file upload "hide description before due date" setting.
398 $alwaysshow = 0;
399 if (!$oldassignment->var3) {
400 $alwaysshow = 1;
401 }
402 $DB->set_field('assign',
403 'alwaysshowdescription',
404 $alwaysshow,
405 array('id'=>$this->assignment->get_instance()->id));
bbd0e548
DW
406 return true;
407 }
bbd0e548
DW
408 }
409
410 /**
411 * Upgrade the submission from the old assignment to the new one
412 *
413 * @param context $oldcontext The context of the old assignment
414 * @param stdClass $oldassignment The data record for the old oldassignment
415 * @param stdClass $oldsubmission The data record for the old submission
416 * @param stdClass $submission The data record for the new submission
417 * @param string $log Record upgrade messages in the log
418 * @return bool true or false - false will trigger a rollback
419 */
e5403f8c
DW
420 public function upgrade(context $oldcontext,
421 stdClass $oldassignment,
422 stdClass $oldsubmission,
423 stdClass $submission,
424 & $log) {
bbd0e548
DW
425 global $DB;
426
427 $filesubmission = new stdClass();
428
429 $filesubmission->numfiles = $oldsubmission->numfiles;
430 $filesubmission->submission = $submission->id;
431 $filesubmission->assignment = $this->assignment->get_instance()->id;
432
433 if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
434 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
435 return false;
436 }
437
e5403f8c 438 // Now copy the area files.
bbd0e548
DW
439 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
440 'mod_assignment',
441 'submission',
442 $oldsubmission->id,
bbd0e548
DW
443 $this->assignment->get_context()->id,
444 'assignsubmission_file',
8ab7bb9f 445 ASSIGNSUBMISSION_FILE_FILEAREA,
bbd0e548
DW
446 $submission->id);
447
bbd0e548
DW
448 return true;
449 }
450
451 /**
452 * The assignment has been deleted - cleanup
453 *
454 * @return bool
455 */
456 public function delete_instance() {
457 global $DB;
e5403f8c
DW
458 // Will throw exception on failure.
459 $DB->delete_records('assignsubmission_file',
460 array('assignment'=>$this->assignment->get_instance()->id));
bbd0e548
DW
461
462 return true;
463 }
464
465 /**
466 * Formatting for log info
467 *
468 * @param stdClass $submission The submission
bbd0e548
DW
469 * @return string
470 */
471 public function format_for_log(stdClass $submission) {
e5403f8c 472 // Format the info for each submission plugin (will be added to log).
8ab7bb9f 473 $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
bbd0e548 474
e5403f8c 475 return get_string('numfilesforlog', 'assignsubmission_file', $filecount);
bbd0e548
DW
476 }
477
478 /**
479 * Return true if there are no submission files
480 * @param stdClass $submission
481 */
482 public function is_empty(stdClass $submission) {
8ab7bb9f 483 return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;
bbd0e548
DW
484 }
485
c89d23ee
CB
486 /**
487 * Determine if a submission is empty
488 *
489 * This is distinct from is_empty in that it is intended to be used to
490 * determine if a submission made before saving is empty.
491 *
492 * @param stdClass $data The submission data
493 * @return bool
494 */
495 public function submission_is_empty(stdClass $data) {
496 $files = file_get_drafarea_files($data->files_filemanager);
497 return count($files->list) == 0;
498 }
499
bbd0e548
DW
500 /**
501 * Get file areas returns a list of areas this plugin stores files
502 * @return array - An array of fileareas (keys) and descriptions (values)
503 */
504 public function get_file_areas() {
8ab7bb9f 505 return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());
bbd0e548
DW
506 }
507
df211804
DW
508 /**
509 * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
510 * on the last submission.
511 * @param stdClass $sourcesubmission
512 * @param stdClass $destsubmission
513 */
514 public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
515 global $DB;
516
517 // Copy the files across.
518 $contextid = $this->assignment->get_context()->id;
519 $fs = get_file_storage();
520 $files = $fs->get_area_files($contextid,
521 'assignsubmission_file',
522 ASSIGNSUBMISSION_FILE_FILEAREA,
523 $sourcesubmission->id,
524 'id',
525 false);
526 foreach ($files as $file) {
527 $fieldupdates = array('itemid' => $destsubmission->id);
528 $fs->create_file_from_storedfile($fieldupdates, $file);
529 }
530
531 // Copy the assignsubmission_file record.
532 if ($filesubmission = $this->get_file_submission($sourcesubmission->id)) {
533 unset($filesubmission->id);
534 $filesubmission->submission = $destsubmission->id;
535 $DB->insert_record('assignsubmission_file', $filesubmission);
536 }
537 return true;
538 }
05a6445a
DW
539
540 /**
541 * Return a description of external params suitable for uploading a file submission from a webservice.
542 *
543 * @return external_description|null
544 */
545 public function get_external_parameters() {
539af602
DW
546 return array(
547 'files_filemanager' => new external_value(
548 PARAM_INT,
69115a5a
JL
549 'The id of a draft area containing files for this submission.',
550 VALUE_OPTIONAL
539af602
DW
551 )
552 );
05a6445a 553 }
30cdddb0
JL
554
555 /**
556 * Return the plugin configs for external functions.
557 *
558 * @return array the list of settings
559 * @since Moodle 3.2
560 */
561 public function get_config_for_external() {
562 global $CFG;
563
564 $configs = $this->get_config();
565
566 // Get a size in bytes.
567 if ($configs->maxsubmissionsizebytes == 0) {
568 $configs->maxsubmissionsizebytes = get_max_upload_file_size($CFG->maxbytes, $this->assignment->get_course()->maxbytes,
569 get_config('assignsubmission_file', 'maxbytes'));
570 }
571 return (array) $configs;
572 }
bbd0e548 573}