assignment MDL-22852 changed url to moodle url.
[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);
48         add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
50         $this->view_header();
52         if ($this->assignment->timeavailable > time()
53           and !has_capability('mod/assignment:grade', $this->context)      // grading user can see it anytime
54           and $this->assignment->var3) {                                   // force hiding before available date
55             echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
56             print_string('notavailableyet', 'assignment');
57             echo $OUTPUT->box_end();
58         } else {
59             $this->view_intro();
60         }
62         $this->view_dates();
64         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
65             if ($submission = $this->get_submission($USER->id)) {
66                 $filecount = $this->count_user_files($submission->id);
67             } else {
68                 $filecount = 0;
69             }
71             $this->view_feedback();
73             if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
74                 echo $OUTPUT->heading(get_string('submission', 'assignment'), 3);
75             } else {
76                 echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
77             }
79             if ($filecount and $submission) {
80                 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
81             } else {
82                 if (!$this->isopen() or $this->is_finalized($submission)) {
83                     echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
84                 } else {
85                     echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
86                 }
87             }
89             $this->view_upload_form();
91             if ($this->notes_allowed()) {
92                 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
93                 $this->view_notes();
94             }
96             $this->view_final_submission();
97         }
98         $this->view_footer();
99     }
102     function view_feedback($submission=NULL) {
103         global $USER, $CFG, $DB, $OUTPUT;
104         require_once($CFG->libdir.'/gradelib.php');
106         if (!$submission) { /// Get submission for this assignment
107             $submission = $this->get_submission($USER->id);
108         }
110         if (empty($submission->timemarked)) {   /// Nothing to show, so print nothing
111             if ($this->count_responsefiles($USER->id)) {
112                 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
113                 $responsefiles = $this->print_responsefiles($USER->id, true);
114                 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
115             }
116             return;
117         }
119         $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
120         $item = $grading_info->items[0];
121         $grade = $item->grades[$USER->id];
123         if ($grade->hidden or $grade->grade === false) { // hidden or error
124             return;
125         }
127         if ($grade->grade === null and empty($grade->str_feedback)) {   /// Nothing to show yet
128             return;
129         }
131         $graded_date = $grade->dategraded;
132         $graded_by   = $grade->usermodified;
134     /// We need the teacher info
135         if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
136             print_error('cannotfindteacher');
137         }
139     /// Print the feedback
140         echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
142         echo '<table cellspacing="0" class="feedback">';
144         echo '<tr>';
145         echo '<td class="left picture">';
146         echo $OUTPUT->user_picture($teacher);
147         echo '</td>';
148         echo '<td class="topic">';
149         echo '<div class="from">';
150         echo '<div class="fullname">'.fullname($teacher).'</div>';
151         echo '<div class="time">'.userdate($graded_date).'</div>';
152         echo '</div>';
153         echo '</td>';
154         echo '</tr>';
156         echo '<tr>';
157         echo '<td class="left side">&nbsp;</td>';
158         echo '<td class="content">';
159         if ($this->assignment->grade) {
160             echo '<div class="grade">';
161             echo get_string("grade").': '.$grade->str_long_grade;
162             echo '</div>';
163             echo '<div class="clearer"></div>';
164         }
166         echo '<div class="comment">';
167         echo $grade->str_feedback;
168         echo '</div>';
169         echo '</tr>';
171         echo '<tr>';
172         echo '<td class="left side">&nbsp;</td>';
173         echo '<td class="content">';
174         echo $this->print_responsefiles($USER->id, true);
175         echo '</tr>';
177         echo '</table>';
178     }
181     function view_upload_form() {
182         global $CFG, $USER, $OUTPUT;
184         $submission = $this->get_submission($USER->id);
186         if ($this->is_finalized($submission)) {
187             // no uploading
188             return;
189         }
191         if ($this->can_upload_file($submission)) {
192             $fs = get_file_storage();
193             // edit files in another page
194             if ($submission) {
195                 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
196                     $str = get_string('editthesefiles', 'assignment');
197                 } else {
198                     $str = get_string('uploadfiles', 'assignment');
199                 }
200             } else {
201                 $str = get_string('uploadfiles', 'assignment');
202             }
203             echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
204         }
206     }
208     function view_notes() {
209         global $USER, $OUTPUT;
211         if ($submission = $this->get_submission($USER->id)
212           and !empty($submission->data1)) {
213             echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML), 'generalbox boxaligncenter boxwidthwide');
214         } else {
215             echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
216         }
217         if ($this->can_update_notes($submission)) {
218             $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
219             echo '<div style="text-align:center">';
220             echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
221             echo '</div>';
222         }
223     }
225     function view_final_submission() {
226         global $CFG, $USER, $OUTPUT;
228         $submission = $this->get_submission($USER->id);
230         if ($this->isopen() and $this->can_finalize($submission)) {
231             //print final submit button
232             echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
233             echo '<div style="text-align:center">';
234             echo '<form method="post" action="upload.php">';
235             echo '<fieldset class="invisiblefieldset">';
236             echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
237             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
238             echo '<input type="hidden" name="action" value="finalize" />';
239             echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
240             echo '</fieldset>';
241             echo '</form>';
242             echo '</div>';
243         } else if (!$this->isopen()) {
244             echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
246         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
247             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
248                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
249             } else {
250                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
251             }
252         } else {
253             //no submission yet
254         }
255     }
258     /**
259      * Return true if var3 == hide description till available day
260      *
261      *@return boolean
262      */
263     function description_is_hidden() {
264         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
265     }
267     function custom_feedbackform($submission, $return=false) {
268         global $CFG, $OUTPUT;
270         $mode         = optional_param('mode', '', PARAM_ALPHA);
271         $offset       = optional_param('offset', 0, PARAM_INT);
272         $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
274         if ($this->count_responsefiles($submission->userid) > 0) {
275             $str = get_string('editthesefiles', 'assignment');
276         } else {
277             $str = get_string('uploadfiles', 'assignment');
278         }
280         $output = get_string('responsefiles', 'assignment').': ';
282         $output .= $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php',
283                     array('contextid'=>$this->context->id,'id'=>$this->cm->id, 'offset'=>$offset,
284                           'forcerefresh'=>$forcerefresh, 'userid'=>$submission->userid, 'mode'=>$mode)), $str, 'get');
286         if ($forcerefresh) {
287             $output .= $this->update_main_listing($submission);
288         }
290         $responsefiles = $this->print_responsefiles($submission->userid, true);
291         if (!empty($responsefiles)) {
292             $output .= $responsefiles;
293         }
295         if ($return) {
296             return $output;
297         }
298         echo $output;
299         return;
300     }
303     function print_student_answer($userid, $return=false){
304         global $CFG, $OUTPUT, $PAGE;
306         $submission = $this->get_submission($userid);
308         $output = '';
310         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
311             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
312         }
314         if ($this->notes_allowed() and !empty($submission->data1)) {
315             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
316             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
317             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
319             $output .= '&nbsp;';
320         }
323         $renderer = $PAGE->get_renderer('mod_assignment');
324         $output = $OUTPUT->box_start('files').$output;
325         $output .= $renderer->assignment_files($this->context, $submission->id);
326         $output .= $OUTPUT->box_end();
328         return $output;
329     }
332     /**
333      * Produces a list of links to the files uploaded by a user
334      *
335      * @param $userid int optional id of the user. If 0 then $USER->id is used.
336      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
337      * @return string optional
338      */
339     function print_user_files($userid=0, $return=false) {
340         global $CFG, $USER, $OUTPUT, $PAGE;
342         $mode    = optional_param('mode', '', PARAM_ALPHA);
343         $offset  = optional_param('offset', 0, PARAM_INT);
345         if (!$userid) {
346             if (!isloggedin()) {
347                 return '';
348             }
349             $userid = $USER->id;
350         }
352         $output = $OUTPUT->box_start('files');
354         $submission = $this->get_submission($userid);
356         // only during grading
357         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
358             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
359         }
361         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
363             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
364             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
366         }
368         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
369             if ($this->can_unfinalize($submission)) {
370                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
371                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
373             } else if ($this->can_finalize($submission)) {
374                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
375                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
376             }
377         }
379         if ($submission) {
380             $renderer = $PAGE->get_renderer('mod_assignment');
381             $output .= $renderer->assignment_files($this->context, $submission->id);
382         }
383         $output .= $OUTPUT->box_end();
385         if ($return) {
386             return $output;
387         }
388         echo $output;
389     }
391     function submissions($mode) {
392         // redirects out of form to process (un)finalizing.
393         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
394         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
395         if ($unfinalize) {
396             $this->unfinalize('single');
397         } else if ($finalize) {
398             $this->finalize('single');
399         }
400         parent::submissions($mode);
401     }
403     function print_responsefiles($userid, $return=false) {
404         global $CFG, $USER, $OUTPUT, $PAGE;
406         $mode    = optional_param('mode', '', PARAM_ALPHA);
407         $offset  = optional_param('offset', 0, PARAM_INT);
409         $output = $OUTPUT->box_start('responsefiles');
411         $candelete = $this->can_manage_responsefiles();
412         $strdelete   = get_string('delete');
414         $fs = get_file_storage();
415         $browser = get_file_browser();
417         if ($submission = $this->get_submission($userid)) {
418             $renderer = $PAGE->get_renderer('mod_assignment');
419             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
420         }
421         $output .= $OUTPUT->box_end();
423         if ($return) {
424             return $output;
425         }
426         echo $output;
427     }
430     /**
431      * Upload files
432      * upload_file function requires moodle form instance and file manager options
433      * @param object $mform
434      * @param array $options
435      */
436     function upload($mform = null, $filemanager_options = null) {
437         $action = required_param('action', PARAM_ALPHA);
438         switch ($action) {
439             case 'finalize':
440                 $this->finalize();
441                 break;
442             case 'finalizeclose':
443                 $this->finalizeclose();
444                 break;
445             case 'unfinalize':
446                 $this->unfinalize();
447                 break;
448             case 'uploadresponse':
449                 $this->upload_responsefile($mform, $filemanager_options);
450                 break;
451             case 'uploadfile':
452                 $this->upload_file($mform, $filemanager_options);
453             case 'savenotes':
454             case 'editnotes':
455                 $this->upload_notes();
456             default:
457                 print_error('unknowuploadaction', '', '', $action);
458         }
459     }
461     function upload_notes() {
462         global $CFG, $USER, $OUTPUT, $DB;
464         $action = required_param('action', PARAM_ALPHA);
466         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
468         $mform = new mod_assignment_upload_notes_form();
470         $defaults = new object();
471         $defaults->id = $this->cm->id;
473         if ($submission = $this->get_submission($USER->id)) {
474             $defaults->text = clean_text($submission->data1);
475         } else {
476             $defaults->text = '';
477         }
479         $mform->set_data($defaults);
481         if ($mform->is_cancelled()) {
482             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
483             redirect($returnurl);
484         }
486         if (!$this->can_update_notes($submission)) {
487             $this->view_header(get_string('upload'));
488             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
489             echo $OUTPUT->continue_button($returnurl);
490             $this->view_footer();
491             die;
492         }
494         if ($data = $mform->get_data() and $action == 'savenotes') {
495             $submission = $this->get_submission($USER->id, true); // get or create submission
496             $updated = new object();
497             $updated->id           = $submission->id;
498             $updated->timemodified = time();
499             $updated->data1        = $data->text;
501             if ($DB->update_record('assignment_submissions', $updated)) {
502                 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
503                 redirect($returnurl);
504                 $submission = $this->get_submission($USER->id);
505                 $this->update_grade($submission);
507             } else {
508                 $this->view_header(get_string('notes', 'assignment'));
509                 echo $OUTPUT->notification(get_string('notesupdateerror', 'assignment'));
510                 echo $OUTPUT->continue_button($returnurl);
511                 $this->view_footer();
512                 die;
513             }
514         }
516         /// show notes edit form
517         $this->view_header(get_string('notes', 'assignment'));
519         echo $OUTPUT->heading(get_string('notes', 'assignment'));
521         $mform->display();
523         $this->view_footer();
524         die;
525     }
527     function upload_responsefile($mform, $options) {
528         global $CFG, $USER, $OUTPUT, $PAGE;
530         $userid = required_param('userid', PARAM_INT);
531         $mode   = required_param('mode', PARAM_ALPHA);
532         $offset = required_param('offset', PARAM_INT);
534         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
536         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
537             $fs = get_file_storage();
538             $submission = $this->get_submission($userid, true, true);
539             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
540                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
541                 redirect($returnurl->out(false));
542             }
543         }
544         $PAGE->set_title(get_string('upload'));
545         echo $OUTPUT->header();
546         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
547         echo $OUTPUT->continue_button($returnurl->out(true));
548         echo $OUTPUT->footer();
549         die;
550     }
552     function upload_file($mform, $options) {
553         global $CFG, $USER, $DB, $OUTPUT;
555         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
556         $submission = $this->get_submission($USER->id);
557         $filecount = $this->count_user_files($submission->id);
559         if (!$this->can_upload_file($submission)) {
560             $this->view_header(get_string('upload'));
561             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
562             echo $OUTPUT->continue_button($returnurl);
563             $this->view_footer();
564             die;
565         }
567         if ($formdata = $mform->get_data()) {
568             $fs = get_file_storage();
569             $submission = $this->get_submission($USER->id, true); //create new submission if needed
570             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
571             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
572             $updates = new object();
573             $updates->id = $submission->id;
574             $updates->timemodified = time();
575             if ($DB->update_record('assignment_submissions', $updates)) {
576                 add_to_log($this->course->id, 'assignment', 'upload',
577                         'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
578                 $this->update_grade($submission);
579                 if (!$this->drafts_tracked()) {
580                     $this->email_teachers($submission);
581                 }
583                 // send files to event system
584                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
585                 // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
586                 $eventdata = new object();
587                 $eventdata->modulename   = 'assignment';
588                 $eventdata->cmid         = $this->cm->id;
589                 $eventdata->itemid       = $submission->id;
590                 $eventdata->courseid     = $this->course->id;
591                 $eventdata->userid       = $USER->id;
592                 if ($files) {
593                     $eventdata->files        = $files;
594                 }
595                 events_trigger('assessable_file_uploaded', $eventdata);
596             }
597             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
598             redirect($returnurl);
599         }
601         $this->view_header(get_string('upload'));
602         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
603         echo $OUTPUT->continue_button($returnurl);
604         $this->view_footer();
605         die;
606     }
608     function send_file($filearea, $args) {
609         global $CFG, $DB, $USER;
610         require_once($CFG->libdir.'/filelib.php');
612         require_login($this->course, false, $this->cm);
614         if ($filearea === 'submission') {
615             $submissionid = (int)array_shift($args);
617             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
618                 return false;
619             }
621             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
622                 return false;
623             }
625             $relativepath = implode('/', $args);
626             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
628             $fs = get_file_storage();
629             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
630                 return false;
631             }
632             send_stored_file($file, 0, 0, true); // download MUST be forced - security!
634         } else if ($filearea === 'response') {
635             $submissionid = (int)array_shift($args);
637             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
638                 return false;
639             }
641             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
642                 return false;
643             }
645             $relativepath = implode('/', $args);
646             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
648             $fs = get_file_storage();
649             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
650                 return false;
651             }
652             send_stored_file($file, 0, 0, true);
653         }
655         return false;
656     }
658     function finalize($forcemode=null) {
659         global $USER, $DB, $OUTPUT;
660         $userid = optional_param('userid', $USER->id, PARAM_INT);
661         $offset = optional_param('offset', 0, PARAM_INT);
662         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
663         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
664         $submission = $this->get_submission($userid);
666         if ($forcemode!=null) {
667             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
668                 array('id'=>$this->cm->id,
669                     'userid'=>$userid,
670                     'mode'=>$forcemode,
671                     'offset'=>$offset
672                 ));
673         }
675         if (!$this->can_finalize($submission)) {
676             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
677         }
679         if ($forcemode==null) {
680             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
681                 $optionsno = array('id'=>$this->cm->id);
682                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
683                 $this->view_header(get_string('submitformarking', 'assignment'));
684                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
685                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
686                 $this->view_footer();
687                 die;
688             }
689         }
690         $updated = new object();
691         $updated->id           = $submission->id;
692         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
693         $updated->timemodified = time();
695         if ($DB->update_record('assignment_submissions', $updated)) {
696             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
697                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
698             $submission = $this->get_submission($userid);
699             $this->update_grade($submission);
700             $this->email_teachers($submission);
701         } else {
702             $this->view_header(get_string('submitformarking', 'assignment'));
703             echo $OUTPUT->notification(get_string('finalizeerror', 'assignment'));
704             echo $OUTPUT->continue_button($returnurl);
705             $this->view_footer();
706             die;
707         }
709         // Trigger assessable_files_done event to show files are complete
710         $eventdata = new object();
711         $eventdata->modulename   = 'assignment';
712         $eventdata->cmid         = $this->cm->id;
713         $eventdata->itemid       = $submission->id;
714         $eventdata->courseid     = $this->course->id;
715         $eventdata->userid       = $userid;
716         events_trigger('assessable_files_done', $eventdata);
718         redirect($returnurl->out(false));
719     }
721     function finalizeclose() {
722         global $DB;
724         $userid    = optional_param('userid', 0, PARAM_INT);
725         $mode      = required_param('mode', PARAM_ALPHA);
726         $offset    = required_param('offset', PARAM_INT);
727         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
729         // create but do not add student submission date
730         $submission = $this->get_submission($userid, true, true);
732         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
733             redirect($returnurl); // probably closed already
734         }
736         $updated = new object();
737         $updated->id    = $submission->id;
738         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
740         if ($DB->update_record('assignment_submissions', $updated)) {
741             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
742                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
743             $submission = $this->get_submission($userid, false, true);
744             $this->update_grade($submission);
745         }
746         redirect($returnurl);
747     }
749     function unfinalize($forcemode=null) {
750         global $DB;
752         $userid = required_param('userid', PARAM_INT);
753         $mode   = required_param('mode', PARAM_ALPHA);
754         $offset = required_param('offset', PARAM_INT);
756         if ($forcemode!=null) {
757             $mode=$forcemode;
758         }
759         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
760         if (data_submitted()
761           and $submission = $this->get_submission($userid)
762           and $this->can_unfinalize($submission)
763           and confirm_sesskey()) {
765             $updated = new object();
766             $updated->id = $submission->id;
767             $updated->data2 = '';
768             if ($DB->update_record('assignment_submissions', $updated)) {
769                 //TODO: add unfinalize action to log
770                 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
771                 $submission = $this->get_submission($userid);
772                 $this->update_grade($submission);
773             } else {
774                 $this->view_header(get_string('submitformarking', 'assignment'));
775                 echo $OUTPUT->notification(get_string('unfinalizeerror', 'assignment'));
776                 echo $OUTPUT->continue_button($returnurl);
777                 $this->view_footer();
778                 die;
779             }
780         }
781         redirect($returnurl);
782     }
785     function delete() {
786         $action   = optional_param('action', '', PARAM_ALPHA);
788         switch ($action) {
789             case 'response':
790                 $this->delete_responsefile();
791                 break;
792             default:
793                 $this->delete_file();
794         }
795         die;
796     }
799     function delete_responsefile() {
800         global $CFG, $OUTPUT,$PAGE;
802         $file     = required_param('file', PARAM_FILE);
803         $userid   = required_param('userid', PARAM_INT);
804         $mode     = required_param('mode', PARAM_ALPHA);
805         $offset   = required_param('offset', PARAM_INT);
806         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
808         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
810         if (!$this->can_manage_responsefiles()) {
811            redirect($returnurl);
812         }
814         $urlreturn = 'submissions.php';
815         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
817         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
818             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
819             $PAGE->set_title(get_string('delete'));
820             echo $OUTPUT->header();
821             echo $OUTPUT->heading(get_string('delete'));
822             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
823             echo $OUTPUT->footer();
824             die;
825         }
827         if ($submission = $this->get_submission($userid)) {
828             $fs = get_file_storage();
829             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
830                 $file->delete();
831             }
832         }
833         redirect($returnurl);
834     }
837     function delete_file() {
838         global $CFG, $DB, $OUTPUT, $PAGE;
840         $file     = required_param('file', PARAM_FILE);
841         $userid   = required_param('userid', PARAM_INT);
842         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
843         $mode     = optional_param('mode', '', PARAM_ALPHA);
844         $offset   = optional_param('offset', 0, PARAM_INT);
846         require_login($this->course->id, false, $this->cm);
848         if (empty($mode)) {
849             $urlreturn = 'view.php';
850             $optionsreturn = array('id'=>$this->cm->id);
851             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
852         } else {
853             $urlreturn = 'submissions.php';
854             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
855             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
856         }
858         if (!$submission = $this->get_submission($userid) // incorrect submission
859           or !$this->can_delete_files($submission)) {     // can not delete
860             $this->view_header(get_string('delete'));
861             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
862             echo $OUTPUT->continue_button($returnurl);
863             $this->view_footer();
864             die;
865         }
867         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
868             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
869             if (empty($mode)) {
870                 $this->view_header(get_string('delete'));
871             } else {
872                 $PAGE->set_title(get_string('delete'));
873                 echo $OUTPUT->header();
874             }
875             echo $OUTPUT->heading(get_string('delete'));
876             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
877             if (empty($mode)) {
878                 $this->view_footer();
879             } else {
880                 echo $OUTPUT->footer();
881             }
882             die;
883         }
885         $fs = get_file_storage();
886         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
887             $file->delete();
888             $submission->timemodified = time();
889             $DB->update_record('assignment_submissions', $submission);
890             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
891                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
892             $this->update_grade($submission);
893         }
894         redirect($returnurl);
895     }
898     function can_upload_file($submission) {
899         global $USER;
901         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
902           and $this->isopen()                                                 // assignment not closed yet
903           and (empty($submission) or ($submission->userid == $USER->id        // his/her own submission
904             and $this->count_user_files($submission->id) <= $this->assignment->var1))    // file limit not exceeded
905           and !$this->is_finalized($submission)) {                            // no uploading after final submission
906             return true;
907         } else {
908             return false;
909         }
910     }
912     function can_manage_responsefiles() {
913         if (has_capability('mod/assignment:grade', $this->context)) {
914             return true;
915         } else {
916             return false;
917         }
918     }
920     function can_delete_files($submission) {
921         global $USER;
923         if (has_capability('mod/assignment:grade', $this->context)) {
924             return true;
925         }
927         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
928           and $this->isopen()                                      // assignment not closed yet
929           and $this->assignment->resubmit                          // deleting allowed
930           and $USER->id == $submission->userid                     // his/her own submission
931           and !$this->is_finalized($submission)) {                 // no deleting after final submission
932             return true;
933         } else {
934             return false;
935         }
936     }
938     function drafts_tracked() {
939         return !empty($this->assignment->var4);
940     }
942     /**
943      * Returns submission status
944      * @param object $submission - may be empty
945      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
946      */
947     function is_finalized($submission) {
948         if (!$this->drafts_tracked()) {
949             return '';
951         } else if (empty($submission)) {
952             return '';
954         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
955             return $submission->data2;
957         } else {
958             return '';
959         }
960     }
962     function can_unfinalize($submission) {
963         if (!$this->drafts_tracked()) {
964             return false;
965         }
966         if (has_capability('mod/assignment:grade', $this->context)
967           and $this->isopen()
968           and $this->is_finalized($submission)) {
969             return true;
970         } else {
971             return false;
972         }
973     }
975     function can_finalize($submission) {
976         global $USER;
977         if (!$this->drafts_tracked()) {
978             return false;
979         }
981         if ($this->is_finalized($submission)) {
982             return false;
983         }
985         if (has_capability('mod/assignment:grade', $this->context)) {
986             return true;
988         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
989           and $this->isopen()                                                 // assignment not closed yet
990           and !empty($submission)                                             // submission must exist
991           and $submission->userid == $USER->id                                // his/her own submission
992           and ($this->count_user_files($submission->id)
993             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
995             return true;
996         } else {
997             return false;
998         }
999     }
1001     function can_update_notes($submission) {
1002         global $USER;
1004         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
1005           and $this->notes_allowed()                                          // notesd must be allowed
1006           and $this->isopen()                                                 // assignment not closed yet
1007           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
1008           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
1009             return true;
1010         } else {
1011             return false;
1012         }
1013     }
1015     function notes_allowed() {
1016         return (boolean)$this->assignment->var2;
1017     }
1019     function count_responsefiles($userid) {
1020         if ($submission = $this->get_submission($userid)) {
1021             $fs = get_file_storage();
1022             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
1023             return count($files);
1024         } else {
1025             return 0;
1026         }
1027     }
1029     function setup_elements(&$mform) {
1030         global $CFG, $COURSE;
1032         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1034         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1035         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1036         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1037         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1039         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1040         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1041         $mform->setDefault('resubmit', 1);
1043         $options = array();
1044         for($i = 1; $i <= 20; $i++) {
1045             $options[$i] = $i;
1046         }
1047         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1048         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1049         $mform->setDefault('var1', 3);
1051         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1052         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1053         $mform->setDefault('var2', 0);
1055         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1056         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1057         $mform->setDefault('var3', 0);
1059         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1060         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1061         $mform->setDefault('emailteachers', 0);
1063         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1064         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1065         $mform->setDefault('var4', 1);
1067     }
1069     function portfolio_exportable() {
1070         return true;
1071     }
1073     function extend_settings_navigation($node) {
1074         global $CFG, $USER, $OUTPUT;
1076         // get users submission if there is one
1077         $submission = $this->get_submission();
1078         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1079             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1080         } else {
1081             $editable = false;
1082         }
1084         // If the user has submitted something add a bit more stuff
1085         if ($submission) {
1086             // Add a view link to the settings nav
1087             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1088             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1089             if (!empty($submission->timemodified)) {
1090                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1091                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1092                 $submittednode->add_class('note');
1093                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1094                     $submittednode->add_class('early');
1095                 } else {
1096                     $submittednode->add_class('late');
1097                 }
1098             }
1099         }
1101         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1102         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1103             $fs = get_file_storage();
1104             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1105                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1106                     $filenode = $node->add(get_string('submission', 'assignment'));
1107                 } else {
1108                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1109                 }
1110                 foreach ($files as $file) {
1111                     $filename = $file->get_filename();
1112                     $mimetype = $file->get_mimetype();
1113                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1114                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1115                 }
1116             }
1117         }
1119         // Show a notes link if they are enabled
1120         if ($this->notes_allowed()) {
1121             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1122             $node->add(get_string('notes', 'assignment'), $link);
1123         }
1124     }
1126     /**
1127      * creates a zip of all assignment submissions and sends a zip to the browser
1128      */
1129     public function download_submissions() {
1130         global $CFG,$DB;
1131         require_once($CFG->libdir.'/filelib.php');
1132         $submissions = $this->get_submissions('','');
1133         if (empty($submissions)) {
1134             error("there are no submissions to download");
1135         }
1136         $filesforzipping = array();
1137         $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
1138         $fs = get_file_storage();
1140         $groupmode = groupmode($this->course,$this->cm);
1141         $groupid = 0;   // All users
1142         $groupname = '';
1143         if($groupmode) {
1144             $group = get_current_group($this->course->id, true);
1145             $groupid = $group->id;
1146             $groupname = $group->name.'-';
1147         }
1148         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1149         foreach ($submissions as $submission) {
1150             $a_userid = $submission->userid; //get userid
1151             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1152                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1153                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1155                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1156                 foreach ($files as $file) {
1157                     //get files new name.
1158                     $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
1159                     //save file name to array for zipping.
1160                     $filesforzipping[$fileforzipname] = $file;
1161                 }
1162             }
1163         } // end of foreach loop
1164         if ($zipfile = assignment_pack_files($filesforzipping)) {
1165             send_temp_file($zipfile, $filename); //send file and delete after sending.
1166         }
1167     }
1170 class mod_assignment_upload_notes_form extends moodleform {
1172     function get_data() {
1173         $data = parent::get_data();
1174         if ($data) {
1175             $data->format = $data->text['format'];
1176             $data->text = $data->text['text'];
1177         }
1178         return $data;
1179     }
1181     function set_data($data) {
1182         if (!isset($data->format)) {
1183             $data->format = FORMAT_HTML;
1184         }
1185         if (isset($data->text)) {
1186             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1187         }
1188         parent::set_data($data);
1189     }
1191     function definition() {
1192         $mform = $this->_form;
1194         // visible elements
1195         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1196         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1198         // hidden params
1199         $mform->addElement('hidden', 'id', 0);
1200         $mform->setType('id', PARAM_INT);
1201         $mform->addElement('hidden', 'action', 'savenotes');
1202         $mform->setType('action', PARAM_ALPHA);
1204         // buttons
1205         $this->add_action_buttons();
1206     }
1209 class mod_assignment_upload_response_form extends moodleform {
1210     function definition() {
1211         $mform = $this->_form;
1212         $instance = $this->_customdata;
1214         // visible elements
1215         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1217         // hidden params
1218         $mform->addElement('hidden', 'id', $instance->cm->id);
1219         $mform->setType('id', PARAM_INT);
1220         $mform->addElement('hidden', 'contextid', $instance->contextid);
1221         $mform->setType('contextid', PARAM_INT);
1222         $mform->addElement('hidden', 'action', 'uploadresponse');
1223         $mform->setType('action', PARAM_ALPHA);
1224         $mform->addElement('hidden', 'mode', $instance->mode);
1225         $mform->setType('mode', PARAM_ALPHA);
1226         $mform->addElement('hidden', 'offset', $instance->offset);
1227         $mform->setType('offset', PARAM_INT);
1228         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1229         $mform->setType('forcerefresh', PARAM_INT);
1230         $mform->addElement('hidden', 'userid', $instance->userid);
1231         $mform->setType('userid', PARAM_INT);
1233         // buttons
1234         $this->add_action_buttons(false, get_string('uploadthisfile'));
1235     }