MDL-29400 Fixing unset object bug encountered by Rajesh during testing
[moodle.git] / mod / assignment / type / upload / 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  * Assignment upload type implementation
20  *
21  * @package   mod-assignment
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require_once(dirname(__FILE__).'/upload_form.php');
26 require_once($CFG->libdir . '/portfoliolib.php');
27 require_once($CFG->dirroot . '/mod/assignment/lib.php');
29 define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted'); // student thinks it is finished
30 define('ASSIGNMENT_STATUS_CLOSED', 'closed');       // teacher prevents more submissions
32 /**
33  * Extend the base assignment class for assignments where you upload a single file
34  *
35  */
36 class assignment_upload extends assignment_base {
38     function assignment_upload($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
39         parent::assignment_base($cmid, $assignment, $cm, $course);
40         $this->type = 'upload';
41     }
43     function view() {
44         global $USER, $OUTPUT;
46         require_capability('mod/assignment:view', $this->context);
47         $cansubmit = has_capability('mod/assignment:submit', $this->context);
49         add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
51         $this->view_header();
53         if ($this->assignment->timeavailable > time()
54           and !has_capability('mod/assignment:grade', $this->context)      // grading user can see it anytime
55           and $this->assignment->var3) {                                   // force hiding before available date
56             echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
57             print_string('notavailableyet', 'assignment');
58             echo $OUTPUT->box_end();
59         } else {
60             $this->view_intro();
61         }
63         $this->view_dates();
65         if (is_enrolled($this->context, $USER)) {
66             if ($submission = $this->get_submission($USER->id)) {
67                 $filecount = $this->count_user_files($submission->id);
68             } else {
69                 $filecount = 0;
70             }
71             if ($cansubmit or !empty($filecount)) { //if a user has submitted files using a previous role we should still show the files
72                 $this->view_feedback();
74                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
75                     echo $OUTPUT->heading(get_string('submission', 'assignment'), 3);
76                 } else {
77                     echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
78                 }
80                 if ($filecount and $submission) {
81                     echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
82                 } else {
83                     if (!$this->isopen() or $this->is_finalized($submission)) {
84                         echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
85                     } else {
86                         echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
87                     }
88                 }
90                 $this->view_upload_form();
92                 if ($this->notes_allowed()) {
93                     echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
94                     $this->view_notes();
95                 }
97                 $this->view_final_submission();
98             }
99         }
100         $this->view_footer();
101     }
104     function view_feedback($submission=NULL) {
105         global $USER, $CFG, $DB, $OUTPUT;
106         require_once($CFG->libdir.'/gradelib.php');
108         if (!$submission) { /// Get submission for this assignment
109             $submission = $this->get_submission($USER->id);
110         }
112         if (empty($submission->timemarked)) {   /// Nothing to show, so print nothing
113             return;
114         }
116         $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
117         $item = $grading_info->items[0];
118         $grade = $item->grades[$USER->id];
120         if ($grade->hidden or $grade->grade === false) { // hidden or error
121             return;
122         }
124         if ($grade->grade === null and empty($grade->str_feedback)) {   // No grade to show yet
125             if ($this->count_responsefiles($USER->id)) {   // but possibly response files are present
126                 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
127                 $responsefiles = $this->print_responsefiles($USER->id, true);
128                 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
129             }
130             return;
131         }
133         $graded_date = $grade->dategraded;
134         $graded_by   = $grade->usermodified;
136     /// We need the teacher info
137         if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
138             print_error('cannotfindteacher');
139         }
141     /// Print the feedback
142         echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
144         echo '<table cellspacing="0" class="feedback">';
146         echo '<tr>';
147         echo '<td class="left picture">';
148         echo $OUTPUT->user_picture($teacher);
149         echo '</td>';
150         echo '<td class="topic">';
151         echo '<div class="from">';
152         echo '<div class="fullname">'.fullname($teacher).'</div>';
153         echo '<div class="time">'.userdate($graded_date).'</div>';
154         echo '</div>';
155         echo '</td>';
156         echo '</tr>';
158         echo '<tr>';
159         echo '<td class="left side">&nbsp;</td>';
160         echo '<td class="content">';
161         if ($this->assignment->grade) {
162             echo '<div class="grade">';
163             echo get_string("grade").': '.$grade->str_long_grade;
164             echo '</div>';
165             echo '<div class="clearer"></div>';
166         }
168         echo '<div class="comment">';
169         echo $grade->str_feedback;
170         echo '</div>';
171         echo '</tr>';
173         echo '<tr>';
174         echo '<td class="left side">&nbsp;</td>';
175         echo '<td class="content">';
176         echo $this->print_responsefiles($USER->id, true);
177         echo '</tr>';
179         echo '</table>';
180     }
183     function view_upload_form() {
184         global $CFG, $USER, $OUTPUT;
186         $submission = $this->get_submission($USER->id);
188         if ($this->is_finalized($submission)) {
189             // no uploading
190             return;
191         }
193         if ($this->can_upload_file($submission)) {
194             $fs = get_file_storage();
195             // edit files in another page
196             if ($submission) {
197                 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
198                     $str = get_string('editthesefiles', 'assignment');
199                 } else {
200                     $str = get_string('uploadfiles', 'assignment');
201                 }
202             } else {
203                 $str = get_string('uploadfiles', 'assignment');
204             }
205             echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
206         }
207     }
209     function view_notes() {
210         global $USER, $OUTPUT;
212         if ($submission = $this->get_submission($USER->id)
213           and !empty($submission->data1)) {
214             echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML, array('overflowdiv'=>true)), 'generalbox boxaligncenter boxwidthwide');
215         } else {
216             echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
217         }
218         if ($this->can_update_notes($submission)) {
219             $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
220             echo '<div style="text-align:center">';
221             echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
222             echo '</div>';
223         }
224     }
226     function view_final_submission() {
227         global $CFG, $USER, $OUTPUT;
229         $submission = $this->get_submission($USER->id);
231         if ($this->isopen() and $this->can_finalize($submission)) {
232             //print final submit button
233             echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
234             echo '<div style="text-align:center">';
235             echo '<form method="post" action="upload.php">';
236             echo '<fieldset class="invisiblefieldset">';
237             echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
238             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
239             echo '<input type="hidden" name="action" value="finalize" />';
240             echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
241             echo '</fieldset>';
242             echo '</form>';
243             echo '</div>';
244         } else if (!$this->isopen()) {
245             echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
247         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
248             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
249                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
250             } else {
251                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
252             }
253         } else {
254             //no submission yet
255         }
256     }
259     /**
260      * Return true if var3 == hide description till available day
261      *
262      *@return boolean
263      */
264     function description_is_hidden() {
265         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
266     }
268     function print_student_answer($userid, $return=false){
269         global $CFG, $OUTPUT, $PAGE;
271         $submission = $this->get_submission($userid);
273         $output = '';
275         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
276             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
277         }
279         if ($this->notes_allowed() and !empty($submission->data1)) {
280             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
281             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
282             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
284             $output .= '&nbsp;';
285         }
288         $renderer = $PAGE->get_renderer('mod_assignment');
289         $output = $OUTPUT->box_start('files').$output;
290         $output .= $renderer->assignment_files($this->context, $submission->id);
291         $output .= $OUTPUT->box_end();
293         return $output;
294     }
297     /**
298      * Produces a list of links to the files uploaded by a user
299      *
300      * @param $userid int optional id of the user. If 0 then $USER->id is used.
301      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
302      * @return string optional
303      */
304     function print_user_files($userid=0, $return=false) {
305         global $CFG, $USER, $OUTPUT, $PAGE;
307         $mode    = optional_param('mode', '', PARAM_ALPHA);
308         $offset  = optional_param('offset', 0, PARAM_INT);
310         if (!$userid) {
311             if (!isloggedin()) {
312                 return '';
313             }
314             $userid = $USER->id;
315         }
317         $output = $OUTPUT->box_start('files');
319         $submission = $this->get_submission($userid);
321         // only during grading
322         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
323             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
324         }
326         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
328             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
329             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
331         }
333         if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
334             if ($this->can_unfinalize($submission)) {
335                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
336                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
337                 $output .=  $OUTPUT->help_icon('unfinalize', 'assignment');
339             } else if ($this->can_finalize($submission)) {
340                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
341                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
342             }
343         }
345         if ($submission) {
346             $renderer = $PAGE->get_renderer('mod_assignment');
347             $output .= $renderer->assignment_files($this->context, $submission->id);
348         }
349         $output .= $OUTPUT->box_end();
351         if ($return) {
352             return $output;
353         }
354         echo $output;
355     }
357     function submissions($mode) {
358         // redirects out of form to process (un)finalizing.
359         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
360         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
361         if ($unfinalize) {
362             $this->unfinalize('single');
363         } else if ($finalize) {
364             $this->finalize('single');
365         }
366         if ($unfinalize || $finalize) {
367             $mode = 'singlenosave';
368         }
369         parent::submissions($mode);
370     }
372     function process_feedback() {
373         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
374             return false;
375         }
376         $userid = required_param('userid', PARAM_INT);
377         $offset = required_param('offset', PARAM_INT);
378         $mform = $this->display_submission($offset, $userid, false);
379         parent::process_feedback($mform);
380     }
382     function print_responsefiles($userid, $return=false) {
383         global $CFG, $USER, $OUTPUT, $PAGE;
385         $mode    = optional_param('mode', '', PARAM_ALPHA);
386         $offset  = optional_param('offset', 0, PARAM_INT);
388         $output = $OUTPUT->box_start('responsefiles');
390         $candelete = $this->can_manage_responsefiles();
391         $strdelete   = get_string('delete');
393         $fs = get_file_storage();
394         $browser = get_file_browser();
396         if ($submission = $this->get_submission($userid)) {
397             $renderer = $PAGE->get_renderer('mod_assignment');
398             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
399         }
400         $output .= $OUTPUT->box_end();
402         if ($return) {
403             return $output;
404         }
405         echo $output;
406     }
409     /**
410      * Upload files
411      * upload_file function requires moodle form instance and file manager options
412      * @param object $mform
413      * @param array $options
414      */
415     function upload($mform = null, $filemanager_options = null) {
416         $action = required_param('action', PARAM_ALPHA);
417         switch ($action) {
418             case 'finalize':
419                 $this->finalize();
420                 break;
421             case 'finalizeclose':
422                 $this->finalizeclose();
423                 break;
424             case 'unfinalize':
425                 $this->unfinalize();
426                 break;
427             case 'uploadresponse':
428                 $this->upload_responsefile($mform, $filemanager_options);
429                 break;
430             case 'uploadfile':
431                 $this->upload_file($mform, $filemanager_options);
432             case 'savenotes':
433             case 'editnotes':
434                 $this->upload_notes();
435             default:
436                 print_error('unknowuploadaction', '', '', $action);
437         }
438     }
440     function upload_notes() {
441         global $CFG, $USER, $OUTPUT, $DB;
443         $action = required_param('action', PARAM_ALPHA);
445         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
447         $mform = new mod_assignment_upload_notes_form();
449         $defaults = new stdClass();
450         $defaults->id = $this->cm->id;
452         if ($submission = $this->get_submission($USER->id)) {
453             $defaults->text = clean_text($submission->data1);
454         } else {
455             $defaults->text = '';
456         }
458         $mform->set_data($defaults);
460         if ($mform->is_cancelled()) {
461             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
462             redirect($returnurl);
463         }
465         if (!$this->can_update_notes($submission)) {
466             $this->view_header(get_string('upload'));
467             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
468             echo $OUTPUT->continue_button($returnurl);
469             $this->view_footer();
470             die;
471         }
473         if ($data = $mform->get_data() and $action == 'savenotes') {
474             $submission = $this->get_submission($USER->id, true); // get or create submission
475             $updated = new stdClass();
476             $updated->id           = $submission->id;
477             $updated->timemodified = time();
478             $updated->data1        = $data->text;
480             $DB->update_record('assignment_submissions', $updated);
481             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
482             redirect($returnurl);
483             $submission = $this->get_submission($USER->id);
484             $this->update_grade($submission);
485         }
487         /// show notes edit form
488         $this->view_header(get_string('notes', 'assignment'));
490         echo $OUTPUT->heading(get_string('notes', 'assignment'));
492         $mform->display();
494         $this->view_footer();
495         die;
496     }
498     function upload_responsefile($mform, $options) {
499         global $CFG, $USER, $OUTPUT, $PAGE;
501         $userid = required_param('userid', PARAM_INT);
502         $mode   = required_param('mode', PARAM_ALPHA);
503         $offset = required_param('offset', PARAM_INT);
505         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
507         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
508             $fs = get_file_storage();
509             $submission = $this->get_submission($userid, true, true);
510             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
511                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
512                 redirect($returnurl->out(false));
513             }
514         }
515         $PAGE->set_title(get_string('upload'));
516         echo $OUTPUT->header();
517         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
518         echo $OUTPUT->continue_button($returnurl->out(true));
519         echo $OUTPUT->footer();
520         die;
521     }
523     function upload_file($mform, $options) {
524         global $CFG, $USER, $DB, $OUTPUT;
526         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
527         $submission = $this->get_submission($USER->id);
529         if (!$this->can_upload_file($submission)) {
530             $this->view_header(get_string('upload'));
531             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
532             echo $OUTPUT->continue_button($returnurl);
533             $this->view_footer();
534             die;
535         }
537         if ($formdata = $mform->get_data()) {
538             $fs = get_file_storage();
539             $submission = $this->get_submission($USER->id, true); //create new submission if needed
540             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
541             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
542             $updates = new stdClass();
543             $updates->id = $submission->id;
544             $updates->timemodified = time();
545             $DB->update_record('assignment_submissions', $updates);
546             add_to_log($this->course->id, 'assignment', 'upload',
547                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
548             $this->update_grade($submission);
549             if (!$this->drafts_tracked()) {
550                 $this->email_teachers($submission);
551             }
553             // send files to event system
554             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
555             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
556             $eventdata = new stdClass();
557             $eventdata->modulename   = 'assignment';
558             $eventdata->cmid         = $this->cm->id;
559             $eventdata->itemid       = $submission->id;
560             $eventdata->courseid     = $this->course->id;
561             $eventdata->userid       = $USER->id;
562             if ($files) {
563                 $eventdata->files        = $files;
564             }
565             events_trigger('assessable_file_uploaded', $eventdata);
566             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
567             redirect($returnurl);
568         }
570         $this->view_header(get_string('upload'));
571         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
572         echo $OUTPUT->continue_button($returnurl);
573         $this->view_footer();
574         die;
575     }
577     function send_file($filearea, $args) {
578         global $CFG, $DB, $USER;
579         require_once($CFG->libdir.'/filelib.php');
581         require_login($this->course, false, $this->cm);
583         if ($filearea === 'submission') {
584             $submissionid = (int)array_shift($args);
586             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
587                 return false;
588             }
590             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
591                 return false;
592             }
594             $relativepath = implode('/', $args);
595             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
597             $fs = get_file_storage();
598             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
599                 return false;
600             }
601             send_stored_file($file, 0, 0, true); // download MUST be forced - security!
603         } else if ($filearea === 'response') {
604             $submissionid = (int)array_shift($args);
606             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
607                 return false;
608             }
610             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
611                 return false;
612             }
614             $relativepath = implode('/', $args);
615             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
617             $fs = get_file_storage();
618             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
619                 return false;
620             }
621             send_stored_file($file, 0, 0, true);
622         }
624         return false;
625     }
627     function finalize($forcemode=null) {
628         global $USER, $DB, $OUTPUT;
629         $userid = optional_param('userid', $USER->id, PARAM_INT);
630         $offset = optional_param('offset', 0, PARAM_INT);
631         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
632         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
633         $submission = $this->get_submission($userid);
635         if ($forcemode!=null) {
636             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
637                 array('id'=>$this->cm->id,
638                     'userid'=>$userid,
639                     'mode'=>$forcemode,
640                     'offset'=>$offset
641                 ));
642         }
644         if (!$this->can_finalize($submission)) {
645             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
646         }
648         if ($forcemode==null) {
649             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
650                 $optionsno = array('id'=>$this->cm->id);
651                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
652                 $this->view_header(get_string('submitformarking', 'assignment'));
653                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
654                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
655                 $this->view_footer();
656                 die;
657             }
658         }
659         $updated = new stdClass();
660         $updated->id           = $submission->id;
661         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
662         $updated->timemodified = time();
664         $DB->update_record('assignment_submissions', $updated);
665         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
666                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
667         $submission = $this->get_submission($userid);
668         $this->update_grade($submission);
669         $this->email_teachers($submission);
671         // Trigger assessable_files_done event to show files are complete
672         $eventdata = new stdClass();
673         $eventdata->modulename   = 'assignment';
674         $eventdata->cmid         = $this->cm->id;
675         $eventdata->itemid       = $submission->id;
676         $eventdata->courseid     = $this->course->id;
677         $eventdata->userid       = $userid;
678         events_trigger('assessable_files_done', $eventdata);
680         if ($forcemode==null) {
681             redirect($returnurl->out(false));
682         }
683     }
685     function finalizeclose() {
686         global $DB;
688         $userid    = optional_param('userid', 0, PARAM_INT);
689         $mode      = required_param('mode', PARAM_ALPHA);
690         $offset    = required_param('offset', PARAM_INT);
691         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
693         // create but do not add student submission date
694         $submission = $this->get_submission($userid, true, true);
696         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
697             redirect($returnurl); // probably closed already
698         }
700         $updated = new stdClass();
701         $updated->id    = $submission->id;
702         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
704         $DB->update_record('assignment_submissions', $updated);
705         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
706                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
707         $submission = $this->get_submission($userid, false, true);
708         $this->update_grade($submission);
709         redirect($returnurl);
710     }
712     function unfinalize($forcemode=null) {
713         global $DB;
715         $userid = required_param('userid', PARAM_INT);
716         $mode   = required_param('mode', PARAM_ALPHA);
717         $offset = required_param('offset', PARAM_INT);
719         if ($forcemode!=null) {
720             $mode=$forcemode;
721         }
722         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
723         if (data_submitted()
724           and $submission = $this->get_submission($userid)
725           and $this->can_unfinalize($submission)
726           and confirm_sesskey()) {
728             $updated = new stdClass();
729             $updated->id = $submission->id;
730             $updated->data2 = '';
731             $DB->update_record('assignment_submissions', $updated);
732             //TODO: add unfinalize action to log
733             add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->cm->id.'&userid='.$userid.'&mode='.$mode.'&offset='.$offset, $this->assignment->id, $this->cm->id);
734             $submission = $this->get_submission($userid);
735             $this->update_grade($submission);
736         }
738         if ($forcemode==null) {
739             redirect($returnurl);
740         }
741     }
744     function delete() {
745         $action   = optional_param('action', '', PARAM_ALPHA);
747         switch ($action) {
748             case 'response':
749                 $this->delete_responsefile();
750                 break;
751             default:
752                 $this->delete_file();
753         }
754         die;
755     }
758     function delete_responsefile() {
759         global $CFG, $OUTPUT,$PAGE;
761         $file     = required_param('file', PARAM_FILE);
762         $userid   = required_param('userid', PARAM_INT);
763         $mode     = required_param('mode', PARAM_ALPHA);
764         $offset   = required_param('offset', PARAM_INT);
765         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
767         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
769         if (!$this->can_manage_responsefiles()) {
770            redirect($returnurl);
771         }
773         $urlreturn = 'submissions.php';
774         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
776         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
777             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
778             $PAGE->set_title(get_string('delete'));
779             echo $OUTPUT->header();
780             echo $OUTPUT->heading(get_string('delete'));
781             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
782             echo $OUTPUT->footer();
783             die;
784         }
786         if ($submission = $this->get_submission($userid)) {
787             $fs = get_file_storage();
788             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
789                 $file->delete();
790             }
791         }
792         redirect($returnurl);
793     }
796     function delete_file() {
797         global $CFG, $DB, $OUTPUT, $PAGE;
799         $file     = required_param('file', PARAM_FILE);
800         $userid   = required_param('userid', PARAM_INT);
801         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
802         $mode     = optional_param('mode', '', PARAM_ALPHA);
803         $offset   = optional_param('offset', 0, PARAM_INT);
805         require_login($this->course->id, false, $this->cm);
807         if (empty($mode)) {
808             $urlreturn = 'view.php';
809             $optionsreturn = array('id'=>$this->cm->id);
810             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
811         } else {
812             $urlreturn = 'submissions.php';
813             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
814             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
815         }
817         if (!$submission = $this->get_submission($userid) // incorrect submission
818           or !$this->can_delete_files($submission)) {     // can not delete
819             $this->view_header(get_string('delete'));
820             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
821             echo $OUTPUT->continue_button($returnurl);
822             $this->view_footer();
823             die;
824         }
826         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
827             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
828             if (empty($mode)) {
829                 $this->view_header(get_string('delete'));
830             } else {
831                 $PAGE->set_title(get_string('delete'));
832                 echo $OUTPUT->header();
833             }
834             echo $OUTPUT->heading(get_string('delete'));
835             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
836             if (empty($mode)) {
837                 $this->view_footer();
838             } else {
839                 echo $OUTPUT->footer();
840             }
841             die;
842         }
844         $fs = get_file_storage();
845         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
846             $file->delete();
847             $submission->timemodified = time();
848             $DB->update_record('assignment_submissions', $submission);
849             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
850                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
851             $this->update_grade($submission);
852         }
853         redirect($returnurl);
854     }
857     function can_upload_file($submission) {
858         global $USER;
860         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
861           and $this->isopen()                                                 // assignment not closed yet
862           and (empty($submission) or ($submission->userid == $USER->id))        // his/her own submission
863           and !$this->is_finalized($submission)) {                            // no uploading after final submission
864             return true;
865         } else {
866             return false;
867         }
868     }
870     function can_manage_responsefiles() {
871         if (has_capability('mod/assignment:grade', $this->context)) {
872             return true;
873         } else {
874             return false;
875         }
876     }
878     function can_delete_files($submission) {
879         global $USER;
881         if (has_capability('mod/assignment:grade', $this->context)) {
882             return true;
883         }
885         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
886           and $this->isopen()                                      // assignment not closed yet
887           and $this->assignment->resubmit                          // deleting allowed
888           and $USER->id == $submission->userid                     // his/her own submission
889           and !$this->is_finalized($submission)) {                 // no deleting after final submission
890             return true;
891         } else {
892             return false;
893         }
894     }
896     function drafts_tracked() {
897         return !empty($this->assignment->var4);
898     }
900     /**
901      * Returns submission status
902      * @param object $submission - may be empty
903      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
904      */
905     function is_finalized($submission) {
906         if (!$this->drafts_tracked()) {
907             return '';
909         } else if (empty($submission)) {
910             return '';
912         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
913             return $submission->data2;
915         } else {
916             return '';
917         }
918     }
920     function can_unfinalize($submission) {
921         if(is_bool($submission)) {
922             return false;
923         }
925         if (!$this->drafts_tracked()) {
926             return false;
927         }
929         if (has_capability('mod/assignment:grade', $this->context)
930           and $this->isopen()
931           and $this->is_finalized($submission)) {
932             return true;
933         } else {
934             return false;
935         }
936     }
938     function can_finalize($submission) {
939         global $USER;
941         if(is_bool($submission)) {
942             return false;
943         }
945         if (!$this->drafts_tracked()) {
946             return false;
947         }
949         if ($this->is_finalized($submission)) {
950             return false;
951         }
953         if (has_capability('mod/assignment:grade', $this->context)) {
954             return true;
956         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
957           and $this->isopen()                                                 // assignment not closed yet
958           and !empty($submission)                                             // submission must exist
959           and $submission->userid == $USER->id                                // his/her own submission
960           and ($this->count_user_files($submission->id)
961             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
963             return true;
964         } else {
965             return false;
966         }
967     }
969     function can_update_notes($submission) {
970         global $USER;
972         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
973           and $this->notes_allowed()                                          // notesd must be allowed
974           and $this->isopen()                                                 // assignment not closed yet
975           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
976           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
977             return true;
978         } else {
979             return false;
980         }
981     }
983     function notes_allowed() {
984         return (boolean)$this->assignment->var2;
985     }
987     function count_responsefiles($userid) {
988         if ($submission = $this->get_submission($userid)) {
989             $fs = get_file_storage();
990             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
991             return count($files);
992         } else {
993             return 0;
994         }
995     }
997     function setup_elements(&$mform) {
998         global $CFG, $COURSE;
1000         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1002         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1003         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1004         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1005         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1007         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1008         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1009         $mform->setDefault('resubmit', 1);
1011         $options = array();
1012         for($i = 1; $i <= 20; $i++) {
1013             $options[$i] = $i;
1014         }
1015         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1016         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1017         $mform->setDefault('var1', 3);
1019         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1020         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1021         $mform->setDefault('var2', 0);
1023         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1024         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1025         $mform->setDefault('var3', 0);
1027         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1028         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1029         $mform->setDefault('emailteachers', 0);
1031         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1032         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1033         $mform->setDefault('var4', 1);
1035         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
1036         plagiarism_get_form_elements_module($mform, $course_context);
1037     }
1039     function portfolio_exportable() {
1040         return true;
1041     }
1043     function extend_settings_navigation($node) {
1044         global $CFG, $USER, $OUTPUT;
1046         // get users submission if there is one
1047         $submission = $this->get_submission();
1048         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1049             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1050         } else {
1051             $editable = false;
1052         }
1054         // If the user has submitted something add some related links and data
1055         if (isset($submission->data2) AND $submission->data2 == 'submitted') {
1056             // Add a view link to the settings nav
1057             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1058             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1059             if (!empty($submission->timemodified)) {
1060                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1061                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1062                 $submittednode->add_class('note');
1063                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1064                     $submittednode->add_class('early');
1065                 } else {
1066                     $submittednode->add_class('late');
1067                 }
1068             }
1069         }
1071         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1072         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1073             $fs = get_file_storage();
1074             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1075                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1076                     $filenode = $node->add(get_string('submission', 'assignment'));
1077                 } else {
1078                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1079                 }
1080                 foreach ($files as $file) {
1081                     $filename = $file->get_filename();
1082                     $mimetype = $file->get_mimetype();
1083                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1084                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1085                 }
1086             }
1087         }
1089         // Show a notes link if they are enabled
1090         if ($this->notes_allowed()) {
1091             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1092             $node->add(get_string('notes', 'assignment'), $link);
1093         }
1094     }
1096     /**
1097      * creates a zip of all assignment submissions and sends a zip to the browser
1098      */
1099     public function download_submissions() {
1100         global $CFG,$DB;
1101         require_once($CFG->libdir.'/filelib.php');
1102         $submissions = $this->get_submissions('','');
1103         if (empty($submissions)) {
1104             print_error('errornosubmissions', 'assignment');
1105         }
1106         $filesforzipping = array();
1107         $fs = get_file_storage();
1109         $groupmode = groups_get_activity_groupmode($this->cm);
1110         $groupid = 0;   // All users
1111         $groupname = '';
1112         if ($groupmode) {
1113             $groupid = groups_get_activity_group($this->cm, true);
1114             $groupname = groups_get_group_name($groupid).'-';
1115         }
1116         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1117         foreach ($submissions as $submission) {
1118             $a_userid = $submission->userid; //get userid
1119             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1120                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1121                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1123                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1124                 foreach ($files as $file) {
1125                     //get files new name.
1126                     $fileext = strstr($file->get_filename(), '.');
1127                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
1128                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1129                     //save file name to array for zipping.
1130                     $filesforzipping[$fileforzipname] = $file;
1131                 }
1132             }
1133         } // end of foreach loop
1134         if ($zipfile = assignment_pack_files($filesforzipping)) {
1135             send_temp_file($zipfile, $filename); //send file and delete after sending.
1136         }
1137     }
1140 class mod_assignment_upload_notes_form extends moodleform {
1142     function get_data() {
1143         $data = parent::get_data();
1144         if ($data) {
1145             $data->format = $data->text['format'];
1146             $data->text = $data->text['text'];
1147         }
1148         return $data;
1149     }
1151     function set_data($data) {
1152         if (!isset($data->format)) {
1153             $data->format = FORMAT_HTML;
1154         }
1155         if (isset($data->text)) {
1156             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1157         }
1158         parent::set_data($data);
1159     }
1161     function definition() {
1162         $mform = $this->_form;
1164         // visible elements
1165         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1166         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1168         // hidden params
1169         $mform->addElement('hidden', 'id', 0);
1170         $mform->setType('id', PARAM_INT);
1171         $mform->addElement('hidden', 'action', 'savenotes');
1172         $mform->setType('action', PARAM_ALPHA);
1174         // buttons
1175         $this->add_action_buttons();
1176     }
1179 class mod_assignment_upload_response_form extends moodleform {
1180     function definition() {
1181         $mform = $this->_form;
1182         $instance = $this->_customdata;
1184         // visible elements
1185         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1187         // hidden params
1188         $mform->addElement('hidden', 'id', $instance->cm->id);
1189         $mform->setType('id', PARAM_INT);
1190         $mform->addElement('hidden', 'contextid', $instance->contextid);
1191         $mform->setType('contextid', PARAM_INT);
1192         $mform->addElement('hidden', 'action', 'uploadresponse');
1193         $mform->setType('action', PARAM_ALPHA);
1194         $mform->addElement('hidden', 'mode', $instance->mode);
1195         $mform->setType('mode', PARAM_ALPHA);
1196         $mform->addElement('hidden', 'offset', $instance->offset);
1197         $mform->setType('offset', PARAM_INT);
1198         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1199         $mform->setType('forcerefresh', PARAM_INT);
1200         $mform->addElement('hidden', 'userid', $instance->userid);
1201         $mform->setType('userid', PARAM_INT);
1203         // buttons
1204         $this->add_action_buttons(false, get_string('uploadthisfile'));
1205     }