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