a2c2c8be819a7b2c3d00ba27196661ed7d59c75e
[moodle.git] / mod / assign / submission / file / locallib.php
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/>.
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  */
27 require_once($CFG->libdir.'/eventslib.php');
29 defined('MOODLE_INTERNAL') || die();
31 // File areas for file submission assignment.
32 define('ASSIGNSUBMISSION_FILE_MAXFILES', 20);
33 define('ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES', 5);
34 define('ASSIGNSUBMISSION_FILE_FILEAREA', 'submission_files');
36 /**
37  * Library class for file submission plugin extending submission plugin base class
38  *
39  * @package   assignsubmission_file
40  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
41  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42  */
43 class assign_submission_file extends assign_submission_plugin {
45     /**
46      * Get the name of the file submission plugin
47      * @return string
48      */
49     public function get_name() {
50         return get_string('file', 'assignsubmission_file');
51     }
53     /**
54      * Get file submission information from the database
55      *
56      * @param int $submissionid
57      * @return mixed
58      */
59     private function get_file_submission($submissionid) {
60         global $DB;
61         return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid));
62     }
64     /**
65      * Get the default setting for file submission plugin
66      *
67      * @param MoodleQuickForm $mform The form to add elements to
68      * @return void
69      */
70     public function get_settings(MoodleQuickForm $mform) {
71         global $CFG, $COURSE;
73         $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
74         $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
76         $settings = array();
77         $options = array();
78         for ($i = 1; $i <= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {
79             $options[$i] = $i;
80         }
82         $name = get_string('maxfilessubmission', 'assignsubmission_file');
83         $mform->addElement('select', 'assignsubmission_file_maxfiles', $name, $options);
84         $mform->addHelpButton('assignsubmission_file_maxfiles',
85                               'maxfilessubmission',
86                               'assignsubmission_file');
87         $mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions);
88         $mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'notchecked');
90         $choices = get_max_upload_sizes($CFG->maxbytes,
91                                         $COURSE->maxbytes,
92                                         get_config('assignsubmission_file', 'maxbytes'));
94         $settings[] = array('type' => 'select',
95                             'name' => 'maxsubmissionsizebytes',
96                             'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
97                             'options'=> $choices,
98                             'default'=> $defaultmaxsubmissionsizebytes);
100         $name = get_string('maximumsubmissionsize', 'assignsubmission_file');
101         $mform->addElement('select', 'assignsubmission_file_maxsizebytes', $name, $choices);
102         $mform->addHelpButton('assignsubmission_file_maxsizebytes',
103                               'maximumsubmissionsize',
104                               'assignsubmission_file');
105         $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);
106         $mform->disabledIf('assignsubmission_file_maxsizebytes',
107                            'assignsubmission_file_enabled',
108                            'notchecked');
109     }
111     /**
112      * Save the settings for file submission plugin
113      *
114      * @param stdClass $data
115      * @return bool
116      */
117     public function save_settings(stdClass $data) {
118         $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
119         $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
120         return true;
121     }
123     /**
124      * File format options
125      *
126      * @return array
127      */
128     private function get_file_options() {
129         $fileoptions = array('subdirs'=>1,
130                                 'maxbytes'=>$this->get_config('maxsubmissionsizebytes'),
131                                 'maxfiles'=>$this->get_config('maxfilesubmissions'),
132                                 'accepted_types'=>'*',
133                                 'return_types'=>FILE_INTERNAL);
134         return $fileoptions;
135     }
137     /**
138      * Add elements to submission form
139      *
140      * @param mixed $submission stdClass|null
141      * @param MoodleQuickForm $mform
142      * @param stdClass $data
143      * @return bool
144      */
145     public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
147         if ($this->get_config('maxfilesubmissions') <= 0) {
148             return false;
149         }
151         $fileoptions = $this->get_file_options();
152         $submissionid = $submission ? $submission->id : 0;
154         $data = file_prepare_standard_filemanager($data,
155                                                   'files',
156                                                   $fileoptions,
157                                                   $this->assignment->get_context(),
158                                                   'assignsubmission_file',
159                                                   ASSIGNSUBMISSION_FILE_FILEAREA,
160                                                   $submissionid);
161         $mform->addElement('filemanager', 'files_filemanager', html_writer::tag('span', $this->get_name(),
162             array('class' => 'accesshide')), null, $fileoptions);
164         return true;
165     }
167     /**
168      * Count the number of files
169      *
170      * @param int $submissionid
171      * @param string $area
172      * @return int
173      */
174     private function count_files($submissionid, $area) {
176         $fs = get_file_storage();
177         $files = $fs->get_area_files($this->assignment->get_context()->id,
178                                      'assignsubmission_file',
179                                      $area,
180                                      $submissionid,
181                                      'id',
182                                      false);
184         return count($files);
185     }
187     /**
188      * Save the files and trigger plagiarism plugin, if enabled,
189      * to scan the uploaded files via events trigger
190      *
191      * @param stdClass $submission
192      * @param stdClass $data
193      * @return bool
194      */
195     public function save(stdClass $submission, stdClass $data) {
196         global $USER, $DB;
198         $fileoptions = $this->get_file_options();
200         $data = file_postupdate_standard_filemanager($data,
201                                                      'files',
202                                                      $fileoptions,
203                                                      $this->assignment->get_context(),
204                                                      'assignsubmission_file',
205                                                      ASSIGNSUBMISSION_FILE_FILEAREA,
206                                                      $submission->id);
208         $filesubmission = $this->get_file_submission($submission->id);
210         // Plagiarism code event trigger when files are uploaded.
212         $fs = get_file_storage();
213         $files = $fs->get_area_files($this->assignment->get_context()->id,
214                                      'assignsubmission_file',
215                                      ASSIGNSUBMISSION_FILE_FILEAREA,
216                                      $submission->id,
217                                      'id',
218                                      false);
220         $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
222         $params = array(
223             'context' => context_module::instance($this->assignment->get_course_module()->id),
224             'objectid' => $submission->id,
225             'other' => array(
226                 'content' => '',
227                 'pathnamehashes' => array_keys($files)
228             )
229         );
230         $event = \assignsubmission_file\event\assessable_uploaded::create($params);
231         $event->set_legacy_files($files);
232         $event->trigger();
234         if ($filesubmission) {
235             $filesubmission->numfiles = $this->count_files($submission->id,
236                                                            ASSIGNSUBMISSION_FILE_FILEAREA);
237             return $DB->update_record('assignsubmission_file', $filesubmission);
238         } else {
239             $filesubmission = new stdClass();
240             $filesubmission->numfiles = $this->count_files($submission->id,
241                                                            ASSIGNSUBMISSION_FILE_FILEAREA);
242             $filesubmission->submission = $submission->id;
243             $filesubmission->assignment = $this->assignment->get_instance()->id;
244             return $DB->insert_record('assignsubmission_file', $filesubmission) > 0;
245         }
246     }
248     /**
249      * Produce a list of files suitable for export that represent this feedback or submission
250      *
251      * @param stdClass $submission The submission
252      * @param stdClass $user The user record - unused
253      * @return array - return an array of files indexed by filename
254      */
255     public function get_files(stdClass $submission, stdClass $user) {
256         $result = array();
257         $fs = get_file_storage();
259         $files = $fs->get_area_files($this->assignment->get_context()->id,
260                                      'assignsubmission_file',
261                                      ASSIGNSUBMISSION_FILE_FILEAREA,
262                                      $submission->id,
263                                      'timemodified',
264                                      false);
266         foreach ($files as $file) {
267             $result[$file->get_filename()] = $file;
268         }
269         return $result;
270     }
272     /**
273      * Display the list of files  in the submission status table
274      *
275      * @param stdClass $submission
276      * @param bool $showviewlink Set this to true if the list of files is long
277      * @return string
278      */
279     public function view_summary(stdClass $submission, & $showviewlink) {
280         $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
282         // Show we show a link to view all files for this plugin?
283         $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;
284         if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {
285             return $this->assignment->render_area_files('assignsubmission_file',
286                                                         ASSIGNSUBMISSION_FILE_FILEAREA,
287                                                         $submission->id);
288         } else {
289             return get_string('countfiles', 'assignsubmission_file', $count);
290         }
291     }
293     /**
294      * No full submission view - the summary contains the list of files and that is the whole submission
295      *
296      * @param stdClass $submission
297      * @return string
298      */
299     public function view(stdClass $submission) {
300         return $this->assignment->render_area_files('assignsubmission_file',
301                                                     ASSIGNSUBMISSION_FILE_FILEAREA,
302                                                     $submission->id);
303     }
307     /**
308      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
309      * and version.
310      *
311      * @param string $type
312      * @param int $version
313      * @return bool True if upgrade is possible
314      */
315     public function can_upgrade($type, $version) {
317         $uploadsingletype ='uploadsingle';
318         $uploadtype ='upload';
320         if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
321             return true;
322         }
323         return false;
324     }
327     /**
328      * Upgrade the settings from the old assignment
329      * to the new plugin based one
330      *
331      * @param context $oldcontext - the old assignment context
332      * @param stdClass $oldassignment - the old assignment data record
333      * @param string $log record log events here
334      * @return bool Was it a success? (false will trigger rollback)
335      */
336     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
337         global $DB;
339         if ($oldassignment->assignmenttype == 'uploadsingle') {
340             $this->set_config('maxfilesubmissions', 1);
341             $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
342             return true;
343         } else if ($oldassignment->assignmenttype == 'upload') {
344             $this->set_config('maxfilesubmissions', $oldassignment->var1);
345             $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
347             // Advanced file upload uses a different setting to do the same thing.
348             $DB->set_field('assign',
349                            'submissiondrafts',
350                            $oldassignment->var4,
351                            array('id'=>$this->assignment->get_instance()->id));
353             // Convert advanced file upload "hide description before due date" setting.
354             $alwaysshow = 0;
355             if (!$oldassignment->var3) {
356                 $alwaysshow = 1;
357             }
358             $DB->set_field('assign',
359                            'alwaysshowdescription',
360                            $alwaysshow,
361                            array('id'=>$this->assignment->get_instance()->id));
362             return true;
363         }
364     }
366     /**
367      * Upgrade the submission from the old assignment to the new one
368      *
369      * @param context $oldcontext The context of the old assignment
370      * @param stdClass $oldassignment The data record for the old oldassignment
371      * @param stdClass $oldsubmission The data record for the old submission
372      * @param stdClass $submission The data record for the new submission
373      * @param string $log Record upgrade messages in the log
374      * @return bool true or false - false will trigger a rollback
375      */
376     public function upgrade(context $oldcontext,
377                             stdClass $oldassignment,
378                             stdClass $oldsubmission,
379                             stdClass $submission,
380                             & $log) {
381         global $DB;
383         $filesubmission = new stdClass();
385         $filesubmission->numfiles = $oldsubmission->numfiles;
386         $filesubmission->submission = $submission->id;
387         $filesubmission->assignment = $this->assignment->get_instance()->id;
389         if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
390             $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
391             return false;
392         }
394         // Now copy the area files.
395         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
396                                                         'mod_assignment',
397                                                         'submission',
398                                                         $oldsubmission->id,
399                                                         $this->assignment->get_context()->id,
400                                                         'assignsubmission_file',
401                                                         ASSIGNSUBMISSION_FILE_FILEAREA,
402                                                         $submission->id);
404         return true;
405     }
407     /**
408      * The assignment has been deleted - cleanup
409      *
410      * @return bool
411      */
412     public function delete_instance() {
413         global $DB;
414         // Will throw exception on failure.
415         $DB->delete_records('assignsubmission_file',
416                             array('assignment'=>$this->assignment->get_instance()->id));
418         return true;
419     }
421     /**
422      * Formatting for log info
423      *
424      * @param stdClass $submission The submission
425      * @return string
426      */
427     public function format_for_log(stdClass $submission) {
428         // Format the info for each submission plugin (will be added to log).
429         $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
431         return get_string('numfilesforlog', 'assignsubmission_file', $filecount);
432     }
434     /**
435      * Return true if there are no submission files
436      * @param stdClass $submission
437      */
438     public function is_empty(stdClass $submission) {
439         return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;
440     }
442     /**
443      * Get file areas returns a list of areas this plugin stores files
444      * @return array - An array of fileareas (keys) and descriptions (values)
445      */
446     public function get_file_areas() {
447         return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());
448     }
450     /**
451      * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
452      * on the last submission.
453      * @param stdClass $sourcesubmission
454      * @param stdClass $destsubmission
455      */
456     public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
457         global $DB;
459         // Copy the files across.
460         $contextid = $this->assignment->get_context()->id;
461         $fs = get_file_storage();
462         $files = $fs->get_area_files($contextid,
463                                      'assignsubmission_file',
464                                      ASSIGNSUBMISSION_FILE_FILEAREA,
465                                      $sourcesubmission->id,
466                                      'id',
467                                      false);
468         foreach ($files as $file) {
469             $fieldupdates = array('itemid' => $destsubmission->id);
470             $fs->create_file_from_storedfile($fieldupdates, $file);
471         }
473         // Copy the assignsubmission_file record.
474         if ($filesubmission = $this->get_file_submission($sourcesubmission->id)) {
475             unset($filesubmission->id);
476             $filesubmission->submission = $destsubmission->id;
477             $DB->insert_record('assignsubmission_file', $filesubmission);
478         }
479         return true;
480     }
482     /**
483      * Return a description of external params suitable for uploading a file submission from a webservice.
484      *
485      * @return external_description|null
486      */
487     public function get_external_parameters() {
488         return array(
489             'files_filemanager' => new external_value(
490                 PARAM_INT,
491                 'The id of a draft area containing files for this submission.'
492             )
493         );
494     }