7f399e78fd8fd6cc6fb15a46897c147976a45a69
[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 = context_module::instance($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($submission);
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') && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
89             if ($this->isopen()) {
90                 $this->view_upload_form();
91             } else if ($this->assignment->timeavailable > time()) {
92                 echo $OUTPUT->heading(get_string('futureaassignment','assignment'), 3);
93             }
94         }
96         $this->view_footer();
97     }
99     /**
100      * Display the response file to the student
101      *
102      * This default method prints the response file
103      *
104      * @param object $submission The submission object
105      */
106     function view_responsefile($submission) {
107         $fs = get_file_storage();
108         $noresponsefiles = $fs->is_area_empty($this->context->id, 'mod_assignment', 'response', $submission->id);
109         if (!$noresponsefiles) {
110             echo '<tr>';
111             echo '<td class="left side">&nbsp;</td>';
112             echo '<td class="content">';
113             echo $this->print_responsefiles($submission->userid);
114             echo '</td></tr>';
115         }
116     }
118     function process_feedback($formdata=null) {
119         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
120             return false;
121         }
122         $userid = required_param('userid', PARAM_INT);
123         $offset = required_param('offset', PARAM_INT);
124         $mform = $this->display_submission($offset, $userid, false);
125         parent::process_feedback($mform);
126     }
128     /**
129      * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
130      * This is necessary for simple file uploads where we need to check that the numfiles field is greater than zero to determine if a
131      * submission is complete.
132      *
133      * @param  int $groupid (optional) If nonzero then count is restricted to this group
134      * @return int          The number of submissions
135      */
136     function count_real_submissions($groupid=0) {
137         global $DB;
139         // Grab the context assocated with our course module
140         $context = context_module::instance($this->cm->id);
142         // Get ids of users enrolled in the given course.
143         list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
144         $params['assignmentid'] = $this->cm->instance;
146         // Get ids of users enrolled in the given course.
147         return $DB->count_records_sql("SELECT COUNT('x')
148                                          FROM {assignment_submissions} s
149                                     LEFT JOIN {assignment} a ON a.id = s.assignment
150                                    INNER JOIN ($enroledsql) u ON u.id = s.userid
151                                         WHERE s.assignment = :assignmentid AND
152                                               s.numfiles > 0", $params);
153     }
155     function print_responsefiles($userid, $return=false) {
156         global $OUTPUT, $PAGE;
158         $output = $OUTPUT->box_start('responsefiles');
160         if ($submission = $this->get_submission($userid)) {
161             $renderer = $PAGE->get_renderer('mod_assignment');
162             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
163         }
164         $output .= $OUTPUT->box_end();
166         if ($return) {
167             return $output;
168         }
169         echo $output;
170     }
172     function can_manage_responsefiles() {
173         if (has_capability('mod/assignment:grade', $this->context)) {
174             return true;
175         } else {
176             return false;
177         }
178     }
180     function view_upload_form() {
181         global $OUTPUT, $USER;
182         echo $OUTPUT->box_start('uploadbox');
183         $fs = get_file_storage();
184         // edit files in another page
185         if ($submission = $this->get_submission($USER->id)) {
186             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
187                 $str = get_string('editthisfile', 'assignment');
188             } else {
189                 $str = get_string('uploadafile', 'assignment');
190             }
191         } else {
192             $str = get_string('uploadafile', 'assignment');
193         }
194         echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
195         echo $OUTPUT->box_end();
196     }
198     function upload($mform) {
199         $action = required_param('action', PARAM_ALPHA);
200         switch ($action) {
201             case 'uploadresponse':
202                 $this->upload_responsefile($mform);
203                 break;
204             case 'uploadfile':
205                 $this->upload_file($mform);
206         }
207     }
209     function upload_file($mform) {
210         global $CFG, $USER, $DB, $OUTPUT;
211         $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
212         if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
213             redirect($viewurl);
214         }
216         $submission = $this->get_submission($USER->id);
217         $filecount = 0;
218         if ($submission) {
219             $filecount = $this->count_user_files($submission->id);
220         }
221         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
222             if ($submission = $this->get_submission($USER->id)) {
223                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
224                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
225                     redirect($viewurl, get_string('alreadygraded', 'assignment'));
226                 }
227             }
229             if ($formdata = $mform->get_data()) {
230                 $fs = get_file_storage();
231                 $submission = $this->get_submission($USER->id, true); //create new submission if needed
232                 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
234                 if ($newfilename = $mform->get_new_filename('assignment_file')) {
235                     $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
236                         $submission->id, '/', $newfilename);
238                     $updates = new stdClass(); //just enough data for updating the submission
239                     $updates->timemodified = time();
240                     $updates->numfiles     = 1;
241                     $updates->id     = $submission->id;
242                     $DB->update_record('assignment_submissions', $updates);
243                     add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
244                     $this->update_grade($submission);
245                     $this->email_teachers($submission);
247                     // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
248                     $eventdata = new stdClass();
249                     $eventdata->modulename   = 'assignment';
250                     $eventdata->cmid         = $this->cm->id;
251                     $eventdata->itemid       = $submission->id;
252                     $eventdata->courseid     = $this->course->id;
253                     $eventdata->userid       = $USER->id;
254                     $eventdata->file         = $file; // This is depreceated - please use pathnamehashes instead!
255                     $eventdata->pathnamehashes = array($file->get_pathnamehash());
256                     events_trigger('assessable_file_uploaded', $eventdata);
257                 }
259                 redirect($viewurl, get_string('uploadedfile'));
260             } else {
261                 redirect($viewurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
262             }
263         }
265         redirect($viewurl);
266     }
268     function upload_responsefile($mform) {
269         global $CFG, $USER, $OUTPUT, $PAGE;
271         $userid = required_param('userid', PARAM_INT);
272         $mode   = required_param('mode', PARAM_ALPHA);
273         $offset = required_param('offset', PARAM_INT);
275         $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
277         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
278             $fs = get_file_storage();
279             $submission = $this->get_submission($userid, true); //create new submission if needed
280             $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
282             if ($newfilename = $mform->get_new_filename('assignment_file')) {
283                 $file = $mform->save_stored_file('assignment_file', $this->context->id,
284                         'mod_assignment', 'response',$submission->id, '/', $newfilename);
285             }
286             redirect($returnurl, get_string('uploadedfile'));
287         } else {
288             redirect($returnurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
289         }
290     }
292     function setup_elements(&$mform) {
293         global $CFG, $COURSE;
295         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
297         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
298         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
299         $mform->setDefault('resubmit', 0);
301         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
302         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
303         $mform->setDefault('emailteachers', 0);
305         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
306         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
307         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
309         $course_context = context_course::instance($COURSE->id);
310         plagiarism_get_form_elements_module($mform, $course_context, 'mod_assignment');
311     }
313     function portfolio_exportable() {
314         return true;
315     }
317     function send_file($filearea, $args, $forcedownload, array $options=array()) {
318         global $CFG, $DB, $USER;
319         require_once($CFG->libdir.'/filelib.php');
321         require_login($this->course, false, $this->cm);
323         if ($filearea !== 'submission' && $filearea !== 'response') {
324             return false;
325         }
327         $submissionid = (int)array_shift($args);
329         if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
330             return false;
331         }
333         if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
334             return false;
335         }
337         $relativepath = implode('/', $args);
338         $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
340         $fs = get_file_storage();
342         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
343             return false;
344         }
346         send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
347     }
349     function extend_settings_navigation($node) {
350         global $CFG, $USER, $OUTPUT;
352         // get users submission if there is one
353         $submission = $this->get_submission();
354         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
355             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
356         } else {
357             $editable = false;
358         }
360         // If the user has submitted something add some related links and data
361         if (isset($submission->numfiles) AND $submission->numfiles) {
362             // Add a view link to the settings nav
363             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
364             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
365             if (!empty($submission->timemodified)) {
366                 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
367                 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
368                 $submissionnode->add_class('note');
369                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
370                     $submissionnode->add_class('early');
371                 } else {
372                     $submissionnode->add_class('late');
373                 }
374             }
375         }
377         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
378         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
379             $fs = get_file_storage();
380             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
381                 $filenode = $node->add(get_string('submission', 'assignment'));
382                 foreach ($files as $file) {
383                     $filename = $file->get_filename();
384                     $mimetype = $file->get_mimetype();
385                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
386                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file), ''));
387                 }
388             }
389         }
390     }
392     /**
393      * creates a zip of all assignment submissions and sends a zip to the browser
394      */
395     function download_submissions() {
396         global $CFG,$DB;
397         require_once($CFG->libdir.'/filelib.php');
399         $submissions = $this->get_submissions('','');
400         if (empty($submissions)) {
401             print_error('errornosubmissions', 'assignment');
402         }
403         $filesforzipping = array();
404         $fs = get_file_storage();
406         $groupmode = groups_get_activity_groupmode($this->cm);
407         $groupid = 0;   // All users
408         $groupname = '';
409         if ($groupmode) {
410             $groupid = groups_get_activity_group($this->cm, true);
411             $groupname = groups_get_group_name($groupid).'-';
412         }
413         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
414         foreach ($submissions as $submission) {
415             $a_userid = $submission->userid; //get userid
416             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
417                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
418                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
420                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
421                 foreach ($files as $file) {
422                     //get files new name.
423                     $fileext = strstr($file->get_filename(), '.');
424                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
425                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
426                     //save file name to array for zipping.
427                     $filesforzipping[$fileforzipname] = $file;
428                 }
429             }
430         } // End of foreach
431         if ($zipfile = assignment_pack_files($filesforzipping)) {
432             send_temp_file($zipfile, $filename); //send file and delete after sending.
433         }
434     }
436     /**
437      * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
438      * table before a submission actually takes place. This function checks to see if the given submission has actually
439      * been submitted.
440      *
441      * @param  stdClass $submission The submission we want to check for completion
442      * @return bool                 Indicates if the submission was found to be complete
443      */
444     public function is_submitted_with_required_data($submission) {
445         return ($submission->timemodified AND $submission->numfiles > 0);
446     }
449 class mod_assignment_uploadsingle_response_form extends moodleform {
450     function definition() {
451         $mform = $this->_form;
452         $instance = $this->_customdata;
454         // visible elements
455         $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
457         // hidden params
458         $mform->addElement('hidden', 'id', $instance->cm->id);
459         $mform->setType('id', PARAM_INT);
460         $mform->addElement('hidden', 'contextid', $instance->contextid);
461         $mform->setType('contextid', PARAM_INT);
462         $mform->addElement('hidden', 'action', 'uploadresponse');
463         $mform->setType('action', PARAM_ALPHA);
464         $mform->addElement('hidden', 'mode', $instance->mode);
465         $mform->setType('mode', PARAM_ALPHA);
466         $mform->addElement('hidden', 'offset', $instance->offset);
467         $mform->setType('offset', PARAM_INT);
468         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
469         $mform->setType('forcerefresh', PARAM_INT);
470         $mform->addElement('hidden', 'userid', $instance->userid);
471         $mform->setType('userid', PARAM_INT);
473         // buttons
474         $this->add_action_buttons(false, get_string('uploadthisfile'));
475     }