080d2010f13b0b25ac6bfba306c583f6784f09c6
[moodle.git] / mod / assignment / type / uploadsingle / assignment.class.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Extend the base assignment class for assignments where you upload a single file
20  *
21  * @package   mod-assignment
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 require_once($CFG->dirroot.'/mod/assignment/lib.php');
25 require_once(dirname(__FILE__).'/upload_form.php');
27 class assignment_uploadsingle extends assignment_base {
30     function print_student_answer($userid, $return=false){
31         global $CFG, $USER, $OUTPUT;
33         $fs = get_file_storage();
34         $browser = get_file_browser();
36         $output = '';
38         if ($submission = $this->get_submission($userid)) {
39             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
40                 foreach ($files as $file) {
41                     $filename = $file->get_filename();
42                     $found = true;
43                     $mimetype = $file->get_mimetype();
44                     $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
45                     $output .= '<a href="'.$path.'" >'.$OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')).s($filename).'</a><br />';
46                     $output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment));
47                     $output .='<br/>';
48                 }
49             }
50         }
52         $output = '<div class="files">'.$output.'</div>';
53         return $output;
54     }
56     function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
57         parent::assignment_base($cmid, $assignment, $cm, $course);
58         $this->type = 'uploadsingle';
59     }
61     function view() {
63         global $USER, $OUTPUT;
65         $context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
66         require_capability('mod/assignment:view', $context);
68         add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
70         $this->view_header();
72         $this->view_intro();
74         $this->view_dates();
76         $filecount = false;
78         if ($submission = $this->get_submission($USER->id)) {
79             $filecount = $this->count_user_files($submission->id);
80             if ($submission->timemarked) {
81                 $this->view_feedback();
82             }
83             if ($filecount) {
84                 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
85             }
86         }
88         if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
89             $this->view_upload_form();
90         }
92         $this->view_footer();
93     }
95     function process_feedback($formdata=null) {
96         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
97             return false;
98         }
99         $userid = required_param('userid', PARAM_INT);
100         $offset = required_param('offset', PARAM_INT);
101         $mform = $this->display_submission($offset, $userid, false);
102         parent::process_feedback($mform);
103     }
105     /**
106      * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
107      * This is necessary for simple file uploads where we need to check that the numfiles field is greater than zero to determine if a
108      * submission is complete.
109      *
110      * @param  int $groupid (optional) If nonzero then count is restricted to this group
111      * @return int          The number of submissions
112      */
113     function count_real_submissions($groupid=0) {
114         global $DB;
116         // Grab the context assocated with our course module
117         $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
119         // Get ids of users enrolled in the given course.
120         list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
121         $params['assignmentid'] = $this->cm->instance;
123         // Get ids of users enrolled in the given course.
124         return $DB->count_records_sql("SELECT COUNT('x')
125                                          FROM {assignment_submissions} s
126                                     LEFT JOIN {assignment} a ON a.id = s.assignment
127                                    INNER JOIN ($enroledsql) u ON u.id = s.userid
128                                         WHERE s.assignment = :assignmentid AND
129                                               s.numfiles > 0", $params);
130     }
132     function print_responsefiles($userid, $return=false) {
133         global $CFG, $USER, $OUTPUT, $PAGE;
135         $mode    = optional_param('mode', '', PARAM_ALPHA);
136         $offset  = optional_param('offset', 0, PARAM_INT);
138         $output = $OUTPUT->box_start('responsefiles');
140         $candelete = $this->can_manage_responsefiles();
141         $strdelete   = get_string('delete');
143         $fs = get_file_storage();
144         $browser = get_file_browser();
146         if ($submission = $this->get_submission($userid)) {
147             $renderer = $PAGE->get_renderer('mod_assignment');
148             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
149             $output .= $OUTPUT->box_end();
150         }
152         if ($return) {
153             return $output;
154         }
155         echo $output;
156     }
158     function can_manage_responsefiles() {
159         if (has_capability('mod/assignment:grade', $this->context)) {
160             return true;
161         } else {
162             return false;
163         }
164     }
166     function view_upload_form() {
167         global $OUTPUT, $USER;
168         echo $OUTPUT->box_start('uploadbox');
169         $fs = get_file_storage();
170         // edit files in another page
171         if ($submission = $this->get_submission($USER->id)) {
172             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
173                 $str = get_string('editthisfile', 'assignment');
174             } else {
175                 $str = get_string('uploadafile', 'assignment');
176             }
177         } else {
178             $str = get_string('uploadafile', 'assignment');
179         }
180         echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
181         echo $OUTPUT->box_end();
182     }
184     function upload($mform) {
185         $action = required_param('action', PARAM_ALPHA);
186         switch ($action) {
187             case 'uploadresponse':
188                 $this->upload_responsefile($mform);
189                 break;
190             case 'uploadfile':
191                 $this->upload_file($mform);
192         }
193     }
195     function upload_file($mform) {
196         global $CFG, $USER, $DB, $OUTPUT;
197         $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
198         if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
199             redirect($viewurl);
200         }
202         $submission = $this->get_submission($USER->id);
203         $filecount = 0;
204         if ($submission) {
205             $filecount = $this->count_user_files($submission->id);
206         }
207         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
208             if ($submission = $this->get_submission($USER->id)) {
209                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
210                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
211                     redirect($viewurl, get_string('alreadygraded', 'assignment'));
212                 }
213             }
215             if ($formdata = $mform->get_data()) {
216                 $fs = get_file_storage();
217                 $submission = $this->get_submission($USER->id, true); //create new submission if needed
218                 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
220                 if ($newfilename = $mform->get_new_filename('assignment_file')) {
221                     $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
222                         $submission->id, '/', $newfilename);
224                     $updates = new stdClass(); //just enough data for updating the submission
225                     $updates->timemodified = time();
226                     $updates->numfiles     = 1;
227                     $updates->id     = $submission->id;
228                     $DB->update_record('assignment_submissions', $updates);
229                     add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
230                     $this->update_grade($submission);
231                     $this->email_teachers($submission);
233                     // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
234                     $eventdata = new stdClass();
235                     $eventdata->modulename   = 'assignment';
236                     $eventdata->cmid         = $this->cm->id;
237                     $eventdata->itemid       = $submission->id;
238                     $eventdata->courseid     = $this->course->id;
239                     $eventdata->userid       = $USER->id;
240                     $eventdata->file         = $file; // This is depreceated - please use pathnamehashes instead!
241                     $eventdata->pathnamehashes = array($file->get_pathnamehash());
242                     events_trigger('assessable_file_uploaded', $eventdata);
243                 }
245                 redirect($viewurl, get_string('uploadedfile'));
246             } else {
247                 redirect($viewurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
248             }
249         }
251         redirect($viewurl);
252     }
254     function upload_responsefile($mform) {
255         global $CFG, $USER, $OUTPUT, $PAGE;
257         $userid = required_param('userid', PARAM_INT);
258         $mode   = required_param('mode', PARAM_ALPHA);
259         $offset = required_param('offset', PARAM_INT);
261         $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
263         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
264             $fs = get_file_storage();
265             $submission = $this->get_submission($userid, true); //create new submission if needed
266             $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
268             if ($newfilename = $mform->get_new_filename('assignment_file')) {
269                 $file = $mform->save_stored_file('assignment_file', $this->context->id,
270                         'mod_assignment', 'response',$submission->id, '/', $newfilename);
271             }
272             redirect($returnurl, get_string('uploadedfile'));
273         } else {
274             redirect($returnurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
275         }
276     }
278     function setup_elements(&$mform) {
279         global $CFG, $COURSE;
281         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
283         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
284         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
285         $mform->setDefault('resubmit', 0);
287         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
288         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
289         $mform->setDefault('emailteachers', 0);
291         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
292         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
293         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
294         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
296         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
297         plagiarism_get_form_elements_module($mform, $course_context);
298     }
300     function portfolio_exportable() {
301         return true;
302     }
304     function send_file($filearea, $args, $forcedownload, array $options=array()) {
305         global $CFG, $DB, $USER;
306         require_once($CFG->libdir.'/filelib.php');
308         require_login($this->course, false, $this->cm);
310         if ($filearea !== 'submission' && $filearea !== 'response') {
311             return false;
312         }
314         $submissionid = (int)array_shift($args);
316         if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
317             return false;
318         }
320         if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
321             return false;
322         }
324         $relativepath = implode('/', $args);
325         $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
327         $fs = get_file_storage();
329         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
330             return false;
331         }
333         send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
334     }
336     function extend_settings_navigation($node) {
337         global $CFG, $USER, $OUTPUT;
339         // get users submission if there is one
340         $submission = $this->get_submission();
341         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
342             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
343         } else {
344             $editable = false;
345         }
347         // If the user has submitted something add some related links and data
348         if (isset($submission->numfiles) AND $submission->numfiles) {
349             // Add a view link to the settings nav
350             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
351             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
352             if (!empty($submission->timemodified)) {
353                 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
354                 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
355                 $submissionnode->add_class('note');
356                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
357                     $submissionnode->add_class('early');
358                 } else {
359                     $submissionnode->add_class('late');
360                 }
361             }
362         }
364         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
365         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
366             $fs = get_file_storage();
367             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
368                 $filenode = $node->add(get_string('submission', 'assignment'));
369                 foreach ($files as $file) {
370                     $filename = $file->get_filename();
371                     $mimetype = $file->get_mimetype();
372                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
373                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file), ''));
374                 }
375             }
376         }
377     }
379     /**
380      * creates a zip of all assignment submissions and sends a zip to the browser
381      */
382     function download_submissions() {
383         global $CFG,$DB;
384         require_once($CFG->libdir.'/filelib.php');
386         $submissions = $this->get_submissions('','');
387         if (empty($submissions)) {
388             print_error('errornosubmissions', 'assignment');
389         }
390         $filesforzipping = array();
391         $fs = get_file_storage();
393         $groupmode = groups_get_activity_groupmode($this->cm);
394         $groupid = 0;   // All users
395         $groupname = '';
396         if ($groupmode) {
397             $groupid = groups_get_activity_group($this->cm, true);
398             $groupname = groups_get_group_name($groupid).'-';
399         }
400         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
401         foreach ($submissions as $submission) {
402             $a_userid = $submission->userid; //get userid
403             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
404                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
405                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
407                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
408                 foreach ($files as $file) {
409                     //get files new name.
410                     $fileext = strstr($file->get_filename(), '.');
411                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
412                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
413                     //save file name to array for zipping.
414                     $filesforzipping[$fileforzipname] = $file;
415                 }
416             }
417         } // End of foreach
418         if ($zipfile = assignment_pack_files($filesforzipping)) {
419             send_temp_file($zipfile, $filename); //send file and delete after sending.
420         }
421     }
423     /**
424      * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
425      * table before a submission actually takes place. This function checks to see if the given submission has actually
426      * been submitted.
427      *
428      * @param  stdClass $submission The submission we want to check for completion
429      * @return bool                 Indicates if the submission was found to be complete
430      */
431     public function is_submitted_with_required_data($submission) {
432         return ($submission->timemodified AND $submission->numfiles > 0);
433     }
436 class mod_assignment_uploadsingle_response_form extends moodleform {
437     function definition() {
438         $mform = $this->_form;
439         $instance = $this->_customdata;
441         // visible elements
442         $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
444         // hidden params
445         $mform->addElement('hidden', 'id', $instance->cm->id);
446         $mform->setType('id', PARAM_INT);
447         $mform->addElement('hidden', 'contextid', $instance->contextid);
448         $mform->setType('contextid', PARAM_INT);
449         $mform->addElement('hidden', 'action', 'uploadresponse');
450         $mform->setType('action', PARAM_ALPHA);
451         $mform->addElement('hidden', 'mode', $instance->mode);
452         $mform->setType('mode', PARAM_ALPHA);
453         $mform->addElement('hidden', 'offset', $instance->offset);
454         $mform->setType('offset', PARAM_INT);
455         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
456         $mform->setType('forcerefresh', PARAM_INT);
457         $mform->addElement('hidden', 'userid', $instance->userid);
458         $mform->setType('userid', PARAM_INT);
460         // buttons
461         $this->add_action_buttons(false, get_string('uploadthisfile'));
462     }