2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the definition for the library class for file submission plugin
20 * This class provides all the functionality for the new assign module.
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
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');
37 * Library class for file submission plugin extending submission plugin base class
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
43 class assign_submission_file extends assign_submission_plugin {
46 * Get the name of the file submission plugin
49 public function get_name() {
50 return get_string('file', 'assignsubmission_file');
54 * Get file submission information from the database
56 * @param int $submissionid
59 private function get_file_submission($submissionid) {
61 return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid));
65 * Get the default setting for file submission plugin
67 * @param MoodleQuickForm $mform The form to add elements to
70 public function get_settings(MoodleQuickForm $mform) {
73 $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
74 $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
78 for ($i = 1; $i <= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {
82 $name = get_string('maxfilessubmission', 'assignsubmission_file');
83 $mform->addElement('select', 'assignsubmission_file_maxfiles', $name, $options);
84 $mform->addHelpButton('assignsubmission_file_maxfiles',
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,
92 get_config('assignsubmission_file', 'maxbytes'));
94 // Remove the option for 0 bytes.
97 if ($COURSE->maxbytes == 0) {
98 $choices = array(0=>get_string('siteuploadlimit', 'assignsubmission_file')) + $choices;
100 $choices = array(0=>get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')') + $choices;
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',
120 * Save the settings for file submission plugin
122 * @param stdClass $data
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);
132 * File format options
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);
146 * Add elements to submission form
148 * @param mixed $submission stdClass|null
149 * @param MoodleQuickForm $mform
150 * @param stdClass $data
153 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
155 if ($this->get_config('maxfilesubmissions') <= 0) {
159 $fileoptions = $this->get_file_options();
160 $submissionid = $submission ? $submission->id : 0;
162 $data = file_prepare_standard_filemanager($data,
165 $this->assignment->get_context(),
166 'assignsubmission_file',
167 ASSIGNSUBMISSION_FILE_FILEAREA,
169 $mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions);
174 * Count the number of files
176 * @param int $submissionid
177 * @param string $area
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',
190 return count($files);
194 * Save the files and trigger plagiarism plugin, if enabled,
195 * to scan the uploaded files via events trigger
197 * @param stdClass $submission
198 * @param stdClass $data
201 public function save(stdClass $submission, stdClass $data) {
204 $fileoptions = $this->get_file_options();
206 $data = file_postupdate_standard_filemanager($data,
209 $this->assignment->get_context(),
210 'assignsubmission_file',
211 ASSIGNSUBMISSION_FILE_FILEAREA,
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,
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;
237 $eventdata->files = $files;
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);
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;
258 * Produce a list of files suitable for export that represent this feedback or submission
260 * @param stdClass $submission The submission
261 * @return array - return an array of files indexed by filename
263 public function get_files(stdClass $submission, stdClass $user) {
265 $fs = get_file_storage();
267 $files = $fs->get_area_files($this->assignment->get_context()->id,
268 'assignsubmission_file',
269 ASSIGNSUBMISSION_FILE_FILEAREA,
274 foreach ($files as $file) {
275 $result[$file->get_filename()] = $file;
281 * Display the list of files in the submission status table
283 * @param stdClass $submission
284 * @param bool $showviewlink Set this to true if the list of files is long
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,
297 return get_string('countfiles', 'assignsubmission_file', $count);
302 * No full submission view - the summary contains the list of files and that is the whole submission
304 * @param stdClass $submission
307 public function view(stdClass $submission) {
308 return $this->assignment->render_area_files('assignsubmission_file',
309 ASSIGNSUBMISSION_FILE_FILEAREA,
316 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
319 * @param string $type
320 * @param int $version
321 * @return bool True if upgrade is possible
323 public function can_upgrade($type, $version) {
325 $uploadsingletype ='uploadsingle';
326 $uploadtype ='upload';
328 if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
336 * Upgrade the settings from the old assignment
337 * to the new plugin based one
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)
344 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
347 if ($oldassignment->assignmenttype == 'uploadsingle') {
348 $this->set_config('maxfilesubmissions', 1);
349 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
351 } else if ($oldassignment->assignmenttype == 'upload') {
352 $this->set_config('maxfilesubmissions', $oldassignment->var1);
353 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
355 // Advanced file upload uses a different setting to do the same thing.
356 $DB->set_field('assign',
358 $oldassignment->var4,
359 array('id'=>$this->assignment->get_instance()->id));
365 * Upgrade the submission from the old assignment to the new one
367 * @param context $oldcontext The context of the old assignment
368 * @param stdClass $oldassignment The data record for the old oldassignment
369 * @param stdClass $oldsubmission The data record for the old submission
370 * @param stdClass $submission The data record for the new submission
371 * @param string $log Record upgrade messages in the log
372 * @return bool true or false - false will trigger a rollback
374 public function upgrade(context $oldcontext,
375 stdClass $oldassignment,
376 stdClass $oldsubmission,
377 stdClass $submission,
381 $filesubmission = new stdClass();
383 $filesubmission->numfiles = $oldsubmission->numfiles;
384 $filesubmission->submission = $submission->id;
385 $filesubmission->assignment = $this->assignment->get_instance()->id;
387 if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
388 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
392 // Now copy the area files.
393 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
397 $this->assignment->get_context()->id,
398 'assignsubmission_file',
399 ASSIGNSUBMISSION_FILE_FILEAREA,
406 * The assignment has been deleted - cleanup
410 public function delete_instance() {
412 // Will throw exception on failure.
413 $DB->delete_records('assignsubmission_file',
414 array('assignment'=>$this->assignment->get_instance()->id));
420 * Formatting for log info
422 * @param stdClass $submission The submission
425 public function format_for_log(stdClass $submission) {
426 // Format the info for each submission plugin (will be added to log).
427 $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
429 return get_string('numfilesforlog', 'assignsubmission_file', $filecount);
433 * Return true if there are no submission files
434 * @param stdClass $submission
436 public function is_empty(stdClass $submission) {
437 return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;
441 * Get file areas returns a list of areas this plugin stores files
442 * @return array - An array of fileareas (keys) and descriptions (values)
444 public function get_file_areas() {
445 return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());