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