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 /** Include eventslib.php */
28 require_once($CFG->libdir.'/eventslib.php');
30 defined('MOODLE_INTERNAL') || die();
32 * File areas for file submission assignment
34 define('ASSIGNSUBMISSION_FILE_MAXFILES', 20);
35 define('ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES', 5);
36 define('ASSIGNSUBMISSION_FILE_FILEAREA', 'submission_files');
39 * library class for file submission plugin extending submission plugin base class
41 * @package assignsubmission_file
42 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class assign_submission_file extends assign_submission_plugin {
48 * Get the name of the file submission plugin
51 public function get_name() {
52 return get_string('file', 'assignsubmission_file');
56 * Get file submission information from the database
58 * @param int $submissionid
61 private function get_file_submission($submissionid) {
63 return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid));
67 * Get the default setting for file submission plugin
69 * @param MoodleQuickForm $mform The form to add elements to
72 public function get_settings(MoodleQuickForm $mform) {
75 $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
76 $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
80 for($i = 1; $i <= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) {
84 $mform->addElement('select', 'assignsubmission_file_maxfiles', get_string('maxfilessubmission', 'assignsubmission_file'), $options);
85 $mform->addHelpButton('assignsubmission_file_maxfiles', 'maxfilessubmission', 'assignsubmission_file');
86 $mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions);
87 $mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'eq', 0);
89 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, get_config('assignsubmission_file', 'maxbytes'));
90 if ($COURSE->maxbytes == 0) {
91 $choices[0] = get_string('siteuploadlimit', 'assignsubmission_file');
93 $choices[0] = get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')';
95 $settings[] = array('type' => 'select',
96 'name' => 'maxsubmissionsizebytes',
97 'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
99 'default'=> $defaultmaxsubmissionsizebytes);
101 $mform->addElement('select', 'assignsubmission_file_maxsizebytes', get_string('maximumsubmissionsize', 'assignsubmission_file'), $choices);
102 $mform->addHelpButton('assignsubmission_file_maxsizebytes', 'maximumsubmissionsize', 'assignsubmission_file');
103 $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);
104 $mform->disabledIf('assignsubmission_file_maxsizebytes', 'assignsubmission_file_enabled', 'eq', 0);
108 * Save the settings for file submission plugin
110 * @param stdClass $data
113 public function save_settings(stdClass $data) {
114 $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
115 $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
120 * File format options
124 private function get_file_options() {
125 $fileoptions = array('subdirs'=>1,
126 'maxbytes'=>$this->get_config('maxsubmissionsizebytes'),
127 'maxfiles'=>$this->get_config('maxfilesubmissions'),
128 'accepted_types'=>'*',
129 'return_types'=>FILE_INTERNAL);
134 * Add elements to submission form
136 * @param mixed $submission stdClass|null
137 * @param MoodleQuickForm $mform
138 * @param stdClass $data
141 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
143 if ($this->get_config('maxfilesubmissions') <= 0) {
147 $fileoptions = $this->get_file_options();
148 $submissionid = $submission ? $submission->id : 0;
151 $data = file_prepare_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submissionid);
152 $mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions);
157 * Count the number of files
159 * @param int $submissionid
160 * @param string $area
163 private function count_files($submissionid, $area) {
165 $fs = get_file_storage();
166 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', $area, $submissionid, "id", false);
168 return count($files);
172 * Save the files and trigger plagiarism plugin, if enabled, to scan the uploaded files via events trigger
174 * @param stdClass $submission
175 * @param stdClass $data
178 public function save(stdClass $submission, stdClass $data) {
181 $fileoptions = $this->get_file_options();
184 $data = file_postupdate_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submission->id);
187 $filesubmission = $this->get_file_submission($submission->id);
189 //plagiarism code event trigger when files are uploaded
191 $fs = get_file_storage();
192 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submission->id, "id", false);
193 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
194 // send files to event system
195 // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
196 $eventdata = new stdClass();
197 $eventdata->modulename = 'assign';
198 $eventdata->cmid = $this->assignment->get_course_module()->id;
199 $eventdata->itemid = $submission->id;
200 $eventdata->courseid = $this->assignment->get_course()->id;
201 $eventdata->userid = $USER->id;
203 $eventdata->files = $files; // This is depreceated - please use pathnamehashes instead!
205 $eventdata->file = $files; // This is depreceated - please use pathnamehashes instead!
206 $eventdata->pathnamehashes = array_keys($files);
207 events_trigger('assessable_file_uploaded', $eventdata);
210 if ($filesubmission) {
211 $filesubmission->numfiles = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
212 return $DB->update_record('assignsubmission_file', $filesubmission);
214 $filesubmission = new stdClass();
215 $filesubmission->numfiles = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
216 $filesubmission->submission = $submission->id;
217 $filesubmission->assignment = $this->assignment->get_instance()->id;
218 return $DB->insert_record('assignsubmission_file', $filesubmission) > 0;
223 * Produce a list of files suitable for export that represent this feedback or submission
225 * @param stdClass $submission The submission
226 * @return array - return an array of files indexed by filename
228 public function get_files(stdClass $submission) {
230 $fs = get_file_storage();
232 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submission->id, "timemodified", false);
234 foreach ($files as $file) {
235 $result[$file->get_filename()] = $file;
241 * Display the list of files in the submission status table
243 * @param stdClass $submission
244 * @param bool $showviewlink Set this to true if the list of files is long
247 public function view_summary(stdClass $submission, $showviewlink) {
248 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
250 // show we show a link to view all files for this plugin?
251 $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES;
252 if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) {
253 return $this->assignment->render_area_files('assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submission->id);
255 return get_string('countfiles', 'assignsubmission_file', $count);
260 * No full submission view - the summary contains the list of files and that is the whole submission
262 * @param stdClass $submission
265 public function view(stdClass $submission) {
266 return $this->assignment->render_area_files('assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA, $submission->id);
272 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
275 * @param string $type
276 * @param int $version
277 * @return bool True if upgrade is possible
279 public function can_upgrade($type, $version) {
281 $uploadsingletype ='uploadsingle';
282 $uploadtype ='upload';
284 if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
292 * Upgrade the settings from the old assignment
293 * to the new plugin based one
295 * @param context $oldcontext - the old assignment context
296 * @param stdClass $oldassignment - the old assignment data record
297 * @param string $log record log events here
298 * @return bool Was it a success? (false will trigger rollback)
300 public function upgrade_settings(context $oldcontext,stdClass $oldassignment, & $log) {
301 if ($oldassignment->assignmenttype == 'uploadsingle') {
302 $this->set_config('maxfilesubmissions', 1);
303 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
307 $this->set_config('maxfilesubmissions', $oldassignment->var1);
308 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
317 * Upgrade the submission from the old assignment to the new one
319 * @param context $oldcontext The context of the old assignment
320 * @param stdClass $oldassignment The data record for the old oldassignment
321 * @param stdClass $oldsubmission The data record for the old submission
322 * @param stdClass $submission The data record for the new submission
323 * @param string $log Record upgrade messages in the log
324 * @return bool true or false - false will trigger a rollback
326 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
329 $filesubmission = new stdClass();
331 $filesubmission->numfiles = $oldsubmission->numfiles;
332 $filesubmission->submission = $submission->id;
333 $filesubmission->assignment = $this->assignment->get_instance()->id;
335 if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
336 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
343 // now copy the area files
344 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
349 $this->assignment->get_context()->id,
350 'assignsubmission_file',
351 ASSIGNSUBMISSION_FILE_FILEAREA,
362 * The assignment has been deleted - cleanup
366 public function delete_instance() {
368 // will throw exception on failure
369 $DB->delete_records('assignsubmission_file', array('assignment'=>$this->assignment->get_instance()->id));
375 * Formatting for log info
377 * @param stdClass $submission The submission
381 public function format_for_log(stdClass $submission) {
382 // format the info for each submission plugin add_to_log
383 $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA);
385 $fileloginfo .= ' the number of file(s) : ' . $filecount . " file(s).<br>";
391 * Return true if there are no submission files
392 * @param stdClass $submission
394 public function is_empty(stdClass $submission) {
395 return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0;
399 * Get file areas returns a list of areas this plugin stores files
400 * @return array - An array of fileareas (keys) and descriptions (values)
402 public function get_file_areas() {
403 return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name());