MDL-29400 Fixing unset object bug encountered by Rajesh during testing
[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.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.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     }
96     function process_feedback() {
97         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
98             return false;
99         }
100         $userid = required_param('userid', PARAM_INT);
101         $offset = required_param('offset', PARAM_INT);
102         $mform = $this->display_submission($offset, $userid, false);
103         parent::process_feedback($mform);
104         }
106     function print_responsefiles($userid, $return=false) {
107         global $CFG, $USER, $OUTPUT, $PAGE;
109         $mode    = optional_param('mode', '', PARAM_ALPHA);
110         $offset  = optional_param('offset', 0, PARAM_INT);
112         $output = $OUTPUT->box_start('responsefiles');
114         $candelete = $this->can_manage_responsefiles();
115         $strdelete   = get_string('delete');
117         $fs = get_file_storage();
118         $browser = get_file_browser();
120         if ($submission = $this->get_submission($userid)) {
121             $renderer = $PAGE->get_renderer('mod_assignment');
122             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
123             $output .= $OUTPUT->box_end();
124         }
126         if ($return) {
127             return $output;
128         }
129         echo $output;
130     }
132     function can_manage_responsefiles() {
133         if (has_capability('mod/assignment:grade', $this->context)) {
134             return true;
135         } else {
136             return false;
137         }
138     }
140     function view_upload_form() {
141         global $OUTPUT, $USER;
142         echo $OUTPUT->box_start('uploadbox');
143         $fs = get_file_storage();
144         // edit files in another page
145         if ($submission = $this->get_submission($USER->id)) {
146             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
147                 $str = get_string('editthisfile', 'assignment');
148             } else {
149                 $str = get_string('uploadafile', 'assignment');
150             }
151         } else {
152             $str = get_string('uploadafile', 'assignment');
153         }
154         echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
155         echo $OUTPUT->box_end();
156     }
159     function upload($mform) {
160         $action = required_param('action', PARAM_ALPHA);
161         switch ($action) {
162             case 'uploadresponse':
163                 $this->upload_responsefile($mform);
164                 break;
165             case 'uploadfile':
166                 $this->upload_file($mform);
167         }
168     }
170     function upload_file($mform) {
171         global $CFG, $USER, $DB, $OUTPUT;
172         $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
173         if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
174             redirect($viewurl);
175         }
177         $submission = $this->get_submission($USER->id);
178         $filecount = 0;
179         if ($submission) {
180             $filecount = $this->count_user_files($submission->id);
181         }
182         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
183             if ($submission = $this->get_submission($USER->id)) {
184                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
185                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
186                     redirect($viewurl, get_string('alreadygraded', 'assignment'));
187                 }
188             }
190             if ($formdata = $mform->get_data()) {
191                 $fs = get_file_storage();
192                 $submission = $this->get_submission($USER->id, true); //create new submission if needed
193                 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
195                 if ($newfilename = $mform->get_new_filename('assignment_file')) {
196                     $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
197                         $submission->id, '/', $newfilename);
199                     $updates = new stdClass(); //just enough data for updating the submission
200                     $updates->timemodified = time();
201                     $updates->numfiles     = 1;
202                     $updates->id     = $submission->id;
203                     $DB->update_record('assignment_submissions', $updates);
204                     add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
205                     $this->update_grade($submission);
206                     $this->email_teachers($submission);
208                     // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
209                     $eventdata = new stdClass();
210                     $eventdata->modulename   = 'assignment';
211                     $eventdata->cmid         = $this->cm->id;
212                     $eventdata->itemid       = $submission->id;
213                     $eventdata->courseid     = $this->course->id;
214                     $eventdata->userid       = $USER->id;
215                     $eventdata->file         = $file;
216                     events_trigger('assessable_file_uploaded', $eventdata);
217                 }
219                 redirect($viewurl, get_string('uploadedfile'));
220             } else {
221                 redirect($viewurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
222             }
223         }
225         redirect($viewurl);
226     }
228     function upload_responsefile($mform) {
229         global $CFG, $USER, $OUTPUT, $PAGE;
231         $userid = required_param('userid', PARAM_INT);
232         $mode   = required_param('mode', PARAM_ALPHA);
233         $offset = required_param('offset', PARAM_INT);
235         $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
237         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
238             $fs = get_file_storage();
239             $submission = $this->get_submission($userid, true); //create new submission if needed
240             $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
242             if ($newfilename = $mform->get_new_filename('assignment_file')) {
243                 $file = $mform->save_stored_file('assignment_file', $this->context->id,
244                         'mod_assignment', 'response',$submission->id, '/', $newfilename);
245             }
246             redirect($returnurl, get_string('uploadedfile'));
247         } else {
248             redirect($returnurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
249         }
250     }
252     function setup_elements(&$mform) {
253         global $CFG, $COURSE;
255         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
257         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
258         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
259         $mform->setDefault('resubmit', 0);
261         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
262         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
263         $mform->setDefault('emailteachers', 0);
265         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
266         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
267         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
268         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
270         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
271         plagiarism_get_form_elements_module($mform, $course_context);
272     }
274     function portfolio_exportable() {
275         return true;
276     }
278     function send_file($filearea, $args) {
279         global $CFG, $DB, $USER;
280         require_once($CFG->libdir.'/filelib.php');
282         require_login($this->course, false, $this->cm);
284         if ($filearea !== 'submission' && $filearea !== 'response') {
285             return false;
286         }
288         $submissionid = (int)array_shift($args);
290         if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
291             return false;
292         }
294         if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
295             return false;
296         }
298         $relativepath = implode('/', $args);
299         $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
301         $fs = get_file_storage();
303         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
304             return false;
305         }
307         send_stored_file($file, 0, 0, true); // download MUST be forced - security!
308     }
310     function extend_settings_navigation($node) {
311         global $CFG, $USER, $OUTPUT;
313         // get users submission if there is one
314         $submission = $this->get_submission();
315         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
316             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
317         } else {
318             $editable = false;
319         }
321         // If the user has submitted something add some related links and data
322         if (isset($submission->numfiles) AND $submission->numfiles) {
323             // Add a view link to the settings nav
324             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
325             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
326             if (!empty($submission->timemodified)) {
327                 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
328                 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
329                 $submissionnode->add_class('note');
330                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
331                     $submissionnode->add_class('early');
332                 } else {
333                     $submissionnode->add_class('late');
334                 }
335             }
336         }
338         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
339         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
340             $fs = get_file_storage();
341             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
342                 $filenode = $node->add(get_string('submission', 'assignment'));
343                 foreach ($files as $file) {
344                     $filename = $file->get_filename();
345                     $mimetype = $file->get_mimetype();
346                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
347                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype), ''));
348                 }
349             }
350         }
351     }
353     /**
354      * creates a zip of all assignment submissions and sends a zip to the browser
355      */
356     function download_submissions() {
357         global $CFG,$DB;
358         require_once($CFG->libdir.'/filelib.php');
360         $submissions = $this->get_submissions('','');
361         if (empty($submissions)) {
362             print_error('errornosubmissions', 'assignment');
363         }
364         $filesforzipping = array();
365         $fs = get_file_storage();
367         $groupmode = groups_get_activity_groupmode($this->cm);
368         $groupid = 0;   // All users
369         $groupname = '';
370         if ($groupmode) {
371             $groupid = groups_get_activity_group($this->cm, true);
372             $groupname = groups_get_group_name($groupid).'-';
373         }
374         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
375         foreach ($submissions as $submission) {
376             $a_userid = $submission->userid; //get userid
377             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
378                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
379                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
381                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
382                 foreach ($files as $file) {
383                     //get files new name.
384                     $fileext = strstr($file->get_filename(), '.');
385                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
386                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
387                     //save file name to array for zipping.
388                     $filesforzipping[$fileforzipname] = $file;
389                 }
390             }
391         } // End of foreach
392         if ($zipfile = assignment_pack_files($filesforzipping)) {
393             send_temp_file($zipfile, $filename); //send file and delete after sending.
394         }
395     }
398 class mod_assignment_uploadsingle_response_form extends moodleform {
399     function definition() {
400         $mform = $this->_form;
401         $instance = $this->_customdata;
403         // visible elements
404         $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
406         // hidden params
407         $mform->addElement('hidden', 'id', $instance->cm->id);
408         $mform->setType('id', PARAM_INT);
409         $mform->addElement('hidden', 'contextid', $instance->contextid);
410         $mform->setType('contextid', PARAM_INT);
411         $mform->addElement('hidden', 'action', 'uploadresponse');
412         $mform->setType('action', PARAM_ALPHA);
413         $mform->addElement('hidden', 'mode', $instance->mode);
414         $mform->setType('mode', PARAM_ALPHA);
415         $mform->addElement('hidden', 'offset', $instance->offset);
416         $mform->setType('offset', PARAM_INT);
417         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
418         $mform->setType('forcerefresh', PARAM_INT);
419         $mform->addElement('hidden', 'userid', $instance->userid);
420         $mform->setType('userid', PARAM_INT);
422         // buttons
423         $this->add_action_buttons(false, get_string('uploadthisfile'));
424     }