Merge branch 'MDL-37030-master' of git://github.com/damyon/moodle
[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', 'eq', 0);
90         $choices = get_max_upload_sizes($CFG->maxbytes,
91                                         $COURSE->maxbytes,
92                                         get_config('assignsubmission_file', 'maxbytes'));
94         // Remove the option for 0 bytes.
95         unset($choices[0]);
97         if ($COURSE->maxbytes == 0) {
98             $choices = array(0=>get_string('siteuploadlimit', 'assignsubmission_file')) + $choices;
99         } else {
100             $choices = array(0=>get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')') + $choices;
101         }
102         $settings[] = array('type' => 'select',
103                             'name' => 'maxsubmissionsizebytes',
104                             'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
105                             'options'=> $choices,
106                             'default'=> $defaultmaxsubmissionsizebytes);
108         $name = get_string('maximumsubmissionsize', 'assignsubmission_file');
109         $mform->addElement('select', 'assignsubmission_file_maxsizebytes', $name, $choices);
110         $mform->addHelpButton('assignsubmission_file_maxsizebytes',
111                               'maximumsubmissionsize',
112                               'assignsubmission_file');
113         $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);
114         $mform->disabledIf('assignsubmission_file_maxsizebytes',
115                            'assignsubmission_file_enabled',
116                            'eq', 0);
117     }
119     /**
120      * Save the settings for file submission plugin
121      *
122      * @param stdClass $data
123      * @return bool
124      */
125     public function save_settings(stdClass $data) {
126         $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
127         $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
128         return true;
129     }
131     /**
132      * File format options
133      *
134      * @return array
135      */
136     private function get_file_options() {
137         $fileoptions = array('subdirs'=>1,
138                                 'maxbytes'=>$this->get_config('maxsubmissionsizebytes'),
139                                 'maxfiles'=>$this->get_config('maxfilesubmissions'),
140                                 'accepted_types'=>'*',
141                                 'return_types'=>FILE_INTERNAL);
142         return $fileoptions;
143     }
145     /**
146      * Add elements to submission form
147      *
148      * @param mixed $submission stdClass|null
149      * @param MoodleQuickForm $mform
150      * @param stdClass $data
151      * @return bool
152      */
153     public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
155         if ($this->get_config('maxfilesubmissions') <= 0) {
156             return false;
157         }
159         $fileoptions = $this->get_file_options();
160         $submissionid = $submission ? $submission->id : 0;
162         $data = file_prepare_standard_filemanager($data,
163                                                   'files',
164                                                   $fileoptions,
165                                                   $this->assignment->get_context(),
166                                                   'assignsubmission_file',
167                                                   ASSIGNSUBMISSION_FILE_FILEAREA,
168                                                   $submissionid);
169         $mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions);
170         return true;
171     }
173     /**
174      * Count the number of files
175      *
176      * @param int $submissionid
177      * @param string $area
178      * @return int
179      */
180     private function count_files($submissionid, $area) {
182         $fs = get_file_storage();
183         $files = $fs->get_area_files($this->assignment->get_context()->id,
184                                      'assignsubmission_file',
185                                      $area,
186                                      $submissionid,
187                                      'id',
188                                      false);
190         return count($files);
191     }
193     /**
194      * Save the files and trigger plagiarism plugin, if enabled,
195      * to scan the uploaded files via events trigger
196      *
197      * @param stdClass $submission
198      * @param stdClass $data
199      * @return bool
200      */
201     public function save(stdClass $submission, stdClass $data) {
202         global $USER, $DB;
204         $fileoptions = $this->get_file_options();
206         $data = file_postupdate_standard_filemanager($data,
207                                                      'files',
208                                                      $fileoptions,
209                                                      $this->assignment->get_context(),
210                                                      'assignsubmission_file',
211                                                      ASSIGNSUBMISSION_FILE_FILEAREA,
212                                                      $submission->id);
214         $filesubmission = $this->get_file_submission($submission->id);
216         // Plagiarism code event trigger when files are uploaded.
218         $fs = get_file_storage();
219         $files = $fs->get_area_files($this->assignment->get_context()->id,
220                                      'assignsubmission_file',
221                                      ASSIGNSUBMISSION_FILE_FILEAREA,
222                                      $submission->id,
223                                      'id',
224                                      false);
226         $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
228         // Send files to event system.
229         // This lets Moodle know that an assessable file was uploaded (eg for plagiarism detection).
230         $eventdata = new stdClass();
231         $eventdata->modulename = 'assign';
232         $eventdata->cmid = $this->assignment->get_course_module()->id;
233         $eventdata->itemid = $submission->id;
234         $eventdata->courseid = $this->assignment->get_course()->id;
235         $eventdata->userid = $USER->id;
236         if ($count > 1) {
237             $eventdata->files = $files;
238         }
239         $eventdata->file = $files;
240         $eventdata->pathnamehashes = array_keys($files);
241         events_trigger('assessable_file_uploaded', $eventdata);
243         if ($filesubmission) {
244             $filesubmission->numfiles = $this->count_files($submission->id,
245                                                            ASSIGNSUBMISSION_FILE_FILEAREA);
246             return $DB->update_record('assignsubmission_file', $filesubmission);
247         } else {
248             $filesubmission = new stdClass();
249             $filesubmission->numfiles = $this->count_files($submission->id,
250                                                            ASSIGNSUBMISSION_FILE_FILEAREA);
251             $filesubmission->submission = $submission->id;
252             $filesubmission->assignment = $this->assignment->get_instance()->id;
253             return $DB->insert_record('assignsubmission_file', $filesubmission) > 0;
254         }
255     }
257     /**
258      * Produce a list of files suitable for export that represent this feedback or submission
259      *
260      * @param stdClass $submission The submission
261      * @return array - return an array of files indexed by filename
262      */
263     public function get_files(stdClass $submission, stdClass $user) {
264         $result = array();
265         $fs = get_file_storage();
267         $files = $fs->get_area_files($this->assignment->get_context()->id,
268                                      'assignsubmission_file',
269                                      ASSIGNSUBMISSION_FILE_FILEAREA,
270                                      $submission->id,
271                                      'timemodified',
272                                      false);
274         foreach ($files as $file) {
275             $result[$file->get_filename()] = $file;
276         }
277         return $result;
278     }
280     /**
281      * Display the list of files  in the submission status table
282      *
283      * @param stdClass $submission
284      * @param bool $showviewlink Set this to true if the list of files is long
285      * @return string
286      */
287     public function view_summary(stdClass $submission, & $showviewlink) {
288         $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
290         // Show we show a link to view all files for this plugin?
291         $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;
292         if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {
293             return $this->assignment->render_area_files('assignsubmission_file',
294                                                         ASSIGNSUBMISSION_FILE_FILEAREA,
295                                                         $submission->id);
296         } else {
297             return get_string('countfiles', 'assignsubmission_file', $count);
298         }
299     }
301     /**
302      * No full submission view - the summary contains the list of files and that is the whole submission
303      *
304      * @param stdClass $submission
305      * @return string
306      */
307     public function view(stdClass $submission) {
308         return $this->assignment->render_area_files('assignsubmission_file',
309                                                     ASSIGNSUBMISSION_FILE_FILEAREA,
310                                                     $submission->id);
311     }
315     /**
316      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
317      * and version.
318      *
319      * @param string $type
320      * @param int $version
321      * @return bool True if upgrade is possible
322      */
323     public function can_upgrade($type, $version) {
325         $uploadsingletype ='uploadsingle';
326         $uploadtype ='upload';
328         if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
329             return true;
330         }
331         return false;
332     }
335     /**
336      * Upgrade the settings from the old assignment
337      * to the new plugin based one
338      *
339      * @param context $oldcontext - the old assignment context
340      * @param stdClass $oldassignment - the old assignment data record
341      * @param string $log record log events here
342      * @return bool Was it a success? (false will trigger rollback)
343      */
344     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
345         if ($oldassignment->assignmenttype == 'uploadsingle') {
346             $this->set_config('maxfilesubmissions', 1);
347             $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
348             return true;
349         } else {
350             $this->set_config('maxfilesubmissions', $oldassignment->var1);
351             $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
352             return true;
353         }
354     }
356     /**
357      * Upgrade the submission from the old assignment to the new one
358      *
359      * @param context $oldcontext The context of the old assignment
360      * @param stdClass $oldassignment The data record for the old oldassignment
361      * @param stdClass $oldsubmission The data record for the old submission
362      * @param stdClass $submission The data record for the new submission
363      * @param string $log Record upgrade messages in the log
364      * @return bool true or false - false will trigger a rollback
365      */
366     public function upgrade(context $oldcontext,
367                             stdClass $oldassignment,
368                             stdClass $oldsubmission,
369                             stdClass $submission,
370                             & $log) {
371         global $DB;
373         $filesubmission = new stdClass();
375         $filesubmission->numfiles = $oldsubmission->numfiles;
376         $filesubmission->submission = $submission->id;
377         $filesubmission->assignment = $this->assignment->get_instance()->id;
379         if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
380             $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
381             return false;
382         }
384         // Now copy the area files.
385         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
386                                                         'mod_assignment',
387                                                         'submission',
388                                                         $oldsubmission->id,
389                                                         $this->assignment->get_context()->id,
390                                                         'assignsubmission_file',
391                                                         ASSIGNSUBMISSION_FILE_FILEAREA,
392                                                         $submission->id);
394         return true;
395     }
397     /**
398      * The assignment has been deleted - cleanup
399      *
400      * @return bool
401      */
402     public function delete_instance() {
403         global $DB;
404         // Will throw exception on failure.
405         $DB->delete_records('assignsubmission_file',
406                             array('assignment'=>$this->assignment->get_instance()->id));
408         return true;
409     }
411     /**
412      * Formatting for log info
413      *
414      * @param stdClass $submission The submission
415      * @return string
416      */
417     public function format_for_log(stdClass $submission) {
418         // Format the info for each submission plugin (will be added to log).
419         $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
421         return get_string('numfilesforlog', 'assignsubmission_file', $filecount);
422     }
424     /**
425      * Return true if there are no submission files
426      * @param stdClass $submission
427      */
428     public function is_empty(stdClass $submission) {
429         return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;
430     }
432     /**
433      * Get file areas returns a list of areas this plugin stores files
434      * @return array - An array of fileareas (keys) and descriptions (values)
435      */
436     public function get_file_areas() {
437         return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());
438     }