assignment MDL-24373 removed file count check to display button to manage assignment...
[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)) {
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             if (has_capability('mod/assignment:submit', $this->context)) {
90                 $this->view_upload_form();
91                 debugging('yes mod/assignment:submit');
92             } else {
93                 debugging('no mod/assignment:submit');
94             }
96             if ($this->notes_allowed()) {
97                 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
98                 $this->view_notes();
99             }
101             $this->view_final_submission();
102         }
103         $this->view_footer();
104     }
107     function view_feedback($submission=NULL) {
108         global $USER, $CFG, $DB, $OUTPUT;
109         require_once($CFG->libdir.'/gradelib.php');
111         if (!$submission) { /// Get submission for this assignment
112             $submission = $this->get_submission($USER->id);
113         }
115         if (empty($submission->timemarked)) {   /// Nothing to show, so print nothing
116             if ($this->count_responsefiles($USER->id)) {
117                 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
118                 $responsefiles = $this->print_responsefiles($USER->id, true);
119                 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
120             }
121             return;
122         }
124         $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
125         $item = $grading_info->items[0];
126         $grade = $item->grades[$USER->id];
128         if ($grade->hidden or $grade->grade === false) { // hidden or error
129             return;
130         }
132         if ($grade->grade === null and empty($grade->str_feedback)) {   /// Nothing to show yet
133             return;
134         }
136         $graded_date = $grade->dategraded;
137         $graded_by   = $grade->usermodified;
139     /// We need the teacher info
140         if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
141             print_error('cannotfindteacher');
142         }
144     /// Print the feedback
145         echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
147         echo '<table cellspacing="0" class="feedback">';
149         echo '<tr>';
150         echo '<td class="left picture">';
151         echo $OUTPUT->user_picture($teacher);
152         echo '</td>';
153         echo '<td class="topic">';
154         echo '<div class="from">';
155         echo '<div class="fullname">'.fullname($teacher).'</div>';
156         echo '<div class="time">'.userdate($graded_date).'</div>';
157         echo '</div>';
158         echo '</td>';
159         echo '</tr>';
161         echo '<tr>';
162         echo '<td class="left side">&nbsp;</td>';
163         echo '<td class="content">';
164         if ($this->assignment->grade) {
165             echo '<div class="grade">';
166             echo get_string("grade").': '.$grade->str_long_grade;
167             echo '</div>';
168             echo '<div class="clearer"></div>';
169         }
171         echo '<div class="comment">';
172         echo $grade->str_feedback;
173         echo '</div>';
174         echo '</tr>';
176         echo '<tr>';
177         echo '<td class="left side">&nbsp;</td>';
178         echo '<td class="content">';
179         echo $this->print_responsefiles($USER->id, true);
180         echo '</tr>';
182         echo '</table>';
183     }
186     function view_upload_form() {
187         global $CFG, $USER, $OUTPUT;
189         $submission = $this->get_submission($USER->id);
191         if ($this->is_finalized($submission)) {
192             // no uploading
193             debugging('finalized');
194             return;
195         }
197         if ($this->can_upload_file($submission)) {
198             debugging('can upload');
199             $fs = get_file_storage();
200             // edit files in another page
201             if ($submission) {
202                 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
203                     $str = get_string('editthesefiles', 'assignment');
204                 } else {
205                     $str = get_string('uploadfiles', 'assignment');
206                 }
207             } else {
208                 $str = get_string('uploadfiles', 'assignment');
209             }
210             echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
211         } else {
212             debugging('cant upload');
213         }
214     }
216     function view_notes() {
217         global $USER, $OUTPUT;
219         if ($submission = $this->get_submission($USER->id)
220           and !empty($submission->data1)) {
221             echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML), 'generalbox boxaligncenter boxwidthwide');
222         } else {
223             echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
224         }
225         if ($this->can_update_notes($submission)) {
226             $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
227             echo '<div style="text-align:center">';
228             echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
229             echo '</div>';
230         }
231     }
233     function view_final_submission() {
234         global $CFG, $USER, $OUTPUT;
236         $submission = $this->get_submission($USER->id);
238         if ($this->isopen() and $this->can_finalize($submission)) {
239             //print final submit button
240             echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
241             echo '<div style="text-align:center">';
242             echo '<form method="post" action="upload.php">';
243             echo '<fieldset class="invisiblefieldset">';
244             echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
245             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
246             echo '<input type="hidden" name="action" value="finalize" />';
247             echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
248             echo '</fieldset>';
249             echo '</form>';
250             echo '</div>';
251         } else if (!$this->isopen()) {
252             echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
254         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
255             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
256                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
257             } else {
258                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
259             }
260         } else {
261             //no submission yet
262         }
263     }
266     /**
267      * Return true if var3 == hide description till available day
268      *
269      *@return boolean
270      */
271     function description_is_hidden() {
272         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
273     }
275     function print_student_answer($userid, $return=false){
276         global $CFG, $OUTPUT, $PAGE;
278         $submission = $this->get_submission($userid);
280         $output = '';
282         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
283             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
284         }
286         if ($this->notes_allowed() and !empty($submission->data1)) {
287             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
288             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
289             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
291             $output .= '&nbsp;';
292         }
295         $renderer = $PAGE->get_renderer('mod_assignment');
296         $output = $OUTPUT->box_start('files').$output;
297         $output .= $renderer->assignment_files($this->context, $submission->id);
298         $output .= $OUTPUT->box_end();
300         return $output;
301     }
304     /**
305      * Produces a list of links to the files uploaded by a user
306      *
307      * @param $userid int optional id of the user. If 0 then $USER->id is used.
308      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
309      * @return string optional
310      */
311     function print_user_files($userid=0, $return=false) {
312         global $CFG, $USER, $OUTPUT, $PAGE;
314         $mode    = optional_param('mode', '', PARAM_ALPHA);
315         $offset  = optional_param('offset', 0, PARAM_INT);
317         if (!$userid) {
318             if (!isloggedin()) {
319                 return '';
320             }
321             $userid = $USER->id;
322         }
324         $output = $OUTPUT->box_start('files');
326         $submission = $this->get_submission($userid);
328         // only during grading
329         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
330             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
331         }
333         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
335             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
336             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
338         }
340         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
341             if ($this->can_unfinalize($submission)) {
342                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
343                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
344                 $output .=  $OUTPUT->help_icon('unfinalize', 'assignment');
346             } else if ($this->can_finalize($submission)) {
347                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
348                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
349             }
350         }
352         if ($submission) {
353             $renderer = $PAGE->get_renderer('mod_assignment');
354             $output .= $renderer->assignment_files($this->context, $submission->id);
355         }
356         $output .= $OUTPUT->box_end();
358         if ($return) {
359             return $output;
360         }
361         echo $output;
362     }
364     function submissions($mode) {
365         // redirects out of form to process (un)finalizing.
366         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
367         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
368         if ($unfinalize) {
369             $this->unfinalize('single');
370         } else if ($finalize) {
371             $this->finalize('single');
372         }
373         if ($unfinalize || $finalize) {
374             $mode = 'singlenosave';
375         }
376         parent::submissions($mode);
377     }
379     function process_feedback() {
380         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
381             return false;
382         }
383         $userid = required_param('userid', PARAM_INT);
384         $offset = required_param('offset', PARAM_INT);
385         $mform = $this->display_submission($offset, $userid, false);
386         parent::process_feedback($mform);
387     }
389     function print_responsefiles($userid, $return=false) {
390         global $CFG, $USER, $OUTPUT, $PAGE;
392         $mode    = optional_param('mode', '', PARAM_ALPHA);
393         $offset  = optional_param('offset', 0, PARAM_INT);
395         $output = $OUTPUT->box_start('responsefiles');
397         $candelete = $this->can_manage_responsefiles();
398         $strdelete   = get_string('delete');
400         $fs = get_file_storage();
401         $browser = get_file_browser();
403         if ($submission = $this->get_submission($userid)) {
404             $renderer = $PAGE->get_renderer('mod_assignment');
405             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
406         }
407         $output .= $OUTPUT->box_end();
409         if ($return) {
410             return $output;
411         }
412         echo $output;
413     }
416     /**
417      * Upload files
418      * upload_file function requires moodle form instance and file manager options
419      * @param object $mform
420      * @param array $options
421      */
422     function upload($mform = null, $filemanager_options = null) {
423         $action = required_param('action', PARAM_ALPHA);
424         switch ($action) {
425             case 'finalize':
426                 $this->finalize();
427                 break;
428             case 'finalizeclose':
429                 $this->finalizeclose();
430                 break;
431             case 'unfinalize':
432                 $this->unfinalize();
433                 break;
434             case 'uploadresponse':
435                 $this->upload_responsefile($mform, $filemanager_options);
436                 break;
437             case 'uploadfile':
438                 $this->upload_file($mform, $filemanager_options);
439             case 'savenotes':
440             case 'editnotes':
441                 $this->upload_notes();
442             default:
443                 print_error('unknowuploadaction', '', '', $action);
444         }
445     }
447     function upload_notes() {
448         global $CFG, $USER, $OUTPUT, $DB;
450         $action = required_param('action', PARAM_ALPHA);
452         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
454         $mform = new mod_assignment_upload_notes_form();
456         $defaults = new stdClass();
457         $defaults->id = $this->cm->id;
459         if ($submission = $this->get_submission($USER->id)) {
460             $defaults->text = clean_text($submission->data1);
461         } else {
462             $defaults->text = '';
463         }
465         $mform->set_data($defaults);
467         if ($mform->is_cancelled()) {
468             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
469             redirect($returnurl);
470         }
472         if (!$this->can_update_notes($submission)) {
473             $this->view_header(get_string('upload'));
474             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
475             echo $OUTPUT->continue_button($returnurl);
476             $this->view_footer();
477             die;
478         }
480         if ($data = $mform->get_data() and $action == 'savenotes') {
481             $submission = $this->get_submission($USER->id, true); // get or create submission
482             $updated = new stdClass();
483             $updated->id           = $submission->id;
484             $updated->timemodified = time();
485             $updated->data1        = $data->text;
487             $DB->update_record('assignment_submissions', $updated);
488             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
489             redirect($returnurl);
490             $submission = $this->get_submission($USER->id);
491             $this->update_grade($submission);
492         }
494         /// show notes edit form
495         $this->view_header(get_string('notes', 'assignment'));
497         echo $OUTPUT->heading(get_string('notes', 'assignment'));
499         $mform->display();
501         $this->view_footer();
502         die;
503     }
505     function upload_responsefile($mform, $options) {
506         global $CFG, $USER, $OUTPUT, $PAGE;
508         $userid = required_param('userid', PARAM_INT);
509         $mode   = required_param('mode', PARAM_ALPHA);
510         $offset = required_param('offset', PARAM_INT);
512         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
514         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
515             $fs = get_file_storage();
516             $submission = $this->get_submission($userid, true, true);
517             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
518                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
519                 redirect($returnurl->out(false));
520             }
521         }
522         $PAGE->set_title(get_string('upload'));
523         echo $OUTPUT->header();
524         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
525         echo $OUTPUT->continue_button($returnurl->out(true));
526         echo $OUTPUT->footer();
527         die;
528     }
530     function upload_file($mform, $options) {
531         global $CFG, $USER, $DB, $OUTPUT;
533         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
534         $submission = $this->get_submission($USER->id);
535         $filecount = $this->count_user_files($submission->id);
537         if (!$this->can_upload_file($submission)) {
538             $this->view_header(get_string('upload'));
539             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
540             echo $OUTPUT->continue_button($returnurl);
541             $this->view_footer();
542             die;
543         }
545         if ($formdata = $mform->get_data()) {
546             $fs = get_file_storage();
547             $submission = $this->get_submission($USER->id, true); //create new submission if needed
548             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
549             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
550             $updates = new stdClass();
551             $updates->id = $submission->id;
552             $updates->timemodified = time();
553             $DB->update_record('assignment_submissions', $updates);
554             add_to_log($this->course->id, 'assignment', 'upload',
555                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
556             $this->update_grade($submission);
557             if (!$this->drafts_tracked()) {
558                 $this->email_teachers($submission);
559             }
561             // send files to event system
562             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
563             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
564             $eventdata = new stdClass();
565             $eventdata->modulename   = 'assignment';
566             $eventdata->cmid         = $this->cm->id;
567             $eventdata->itemid       = $submission->id;
568             $eventdata->courseid     = $this->course->id;
569             $eventdata->userid       = $USER->id;
570             if ($files) {
571                 $eventdata->files        = $files;
572             }
573             events_trigger('assessable_file_uploaded', $eventdata);
574             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
575             redirect($returnurl);
576         }
578         $this->view_header(get_string('upload'));
579         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
580         echo $OUTPUT->continue_button($returnurl);
581         $this->view_footer();
582         die;
583     }
585     function send_file($filearea, $args) {
586         global $CFG, $DB, $USER;
587         require_once($CFG->libdir.'/filelib.php');
589         require_login($this->course, false, $this->cm);
591         if ($filearea === 'submission') {
592             $submissionid = (int)array_shift($args);
594             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
595                 return false;
596             }
598             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
599                 return false;
600             }
602             $relativepath = implode('/', $args);
603             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
605             $fs = get_file_storage();
606             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
607                 return false;
608             }
609             send_stored_file($file, 0, 0, true); // download MUST be forced - security!
611         } else if ($filearea === 'response') {
612             $submissionid = (int)array_shift($args);
614             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
615                 return false;
616             }
618             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
619                 return false;
620             }
622             $relativepath = implode('/', $args);
623             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
625             $fs = get_file_storage();
626             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
627                 return false;
628             }
629             send_stored_file($file, 0, 0, true);
630         }
632         return false;
633     }
635     function finalize($forcemode=null) {
636         global $USER, $DB, $OUTPUT;
637         $userid = optional_param('userid', $USER->id, PARAM_INT);
638         $offset = optional_param('offset', 0, PARAM_INT);
639         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
640         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
641         $submission = $this->get_submission($userid);
643         if ($forcemode!=null) {
644             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
645                 array('id'=>$this->cm->id,
646                     'userid'=>$userid,
647                     'mode'=>$forcemode,
648                     'offset'=>$offset
649                 ));
650         }
652         if (!$this->can_finalize($submission)) {
653             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
654         }
656         if ($forcemode==null) {
657             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
658                 $optionsno = array('id'=>$this->cm->id);
659                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
660                 $this->view_header(get_string('submitformarking', 'assignment'));
661                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
662                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
663                 $this->view_footer();
664                 die;
665             }
666         }
667         $updated = new stdClass();
668         $updated->id           = $submission->id;
669         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
670         $updated->timemodified = time();
672         $DB->update_record('assignment_submissions', $updated);
673         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
674                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
675         $submission = $this->get_submission($userid);
676         $this->update_grade($submission);
677         $this->email_teachers($submission);
679         // Trigger assessable_files_done event to show files are complete
680         $eventdata = new stdClass();
681         $eventdata->modulename   = 'assignment';
682         $eventdata->cmid         = $this->cm->id;
683         $eventdata->itemid       = $submission->id;
684         $eventdata->courseid     = $this->course->id;
685         $eventdata->userid       = $userid;
686         events_trigger('assessable_files_done', $eventdata);
688         if ($forcemode==null) {
689             redirect($returnurl->out(false));
690         }
691     }
693     function finalizeclose() {
694         global $DB;
696         $userid    = optional_param('userid', 0, PARAM_INT);
697         $mode      = required_param('mode', PARAM_ALPHA);
698         $offset    = required_param('offset', PARAM_INT);
699         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
701         // create but do not add student submission date
702         $submission = $this->get_submission($userid, true, true);
704         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
705             redirect($returnurl); // probably closed already
706         }
708         $updated = new stdClass();
709         $updated->id    = $submission->id;
710         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
712         $DB->update_record('assignment_submissions', $updated);
713         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
714                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
715         $submission = $this->get_submission($userid, false, true);
716         $this->update_grade($submission);
717         redirect($returnurl);
718     }
720     function unfinalize($forcemode=null) {
721         global $DB;
723         $userid = required_param('userid', PARAM_INT);
724         $mode   = required_param('mode', PARAM_ALPHA);
725         $offset = required_param('offset', PARAM_INT);
727         if ($forcemode!=null) {
728             $mode=$forcemode;
729         }
730         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
731         if (data_submitted()
732           and $submission = $this->get_submission($userid)
733           and $this->can_unfinalize($submission)
734           and confirm_sesskey()) {
736             $updated = new stdClass();
737             $updated->id = $submission->id;
738             $updated->data2 = '';
739             $DB->update_record('assignment_submissions', $updated);
740             //TODO: add unfinalize action to log
741             add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
742             $submission = $this->get_submission($userid);
743             $this->update_grade($submission);
744         }
746         if ($forcemode==null) {
747             redirect($returnurl);
748         }
749     }
752     function delete() {
753         $action   = optional_param('action', '', PARAM_ALPHA);
755         switch ($action) {
756             case 'response':
757                 $this->delete_responsefile();
758                 break;
759             default:
760                 $this->delete_file();
761         }
762         die;
763     }
766     function delete_responsefile() {
767         global $CFG, $OUTPUT,$PAGE;
769         $file     = required_param('file', PARAM_FILE);
770         $userid   = required_param('userid', PARAM_INT);
771         $mode     = required_param('mode', PARAM_ALPHA);
772         $offset   = required_param('offset', PARAM_INT);
773         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
775         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
777         if (!$this->can_manage_responsefiles()) {
778            redirect($returnurl);
779         }
781         $urlreturn = 'submissions.php';
782         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
784         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
785             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
786             $PAGE->set_title(get_string('delete'));
787             echo $OUTPUT->header();
788             echo $OUTPUT->heading(get_string('delete'));
789             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
790             echo $OUTPUT->footer();
791             die;
792         }
794         if ($submission = $this->get_submission($userid)) {
795             $fs = get_file_storage();
796             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
797                 $file->delete();
798             }
799         }
800         redirect($returnurl);
801     }
804     function delete_file() {
805         global $CFG, $DB, $OUTPUT, $PAGE;
807         $file     = required_param('file', PARAM_FILE);
808         $userid   = required_param('userid', PARAM_INT);
809         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
810         $mode     = optional_param('mode', '', PARAM_ALPHA);
811         $offset   = optional_param('offset', 0, PARAM_INT);
813         require_login($this->course->id, false, $this->cm);
815         if (empty($mode)) {
816             $urlreturn = 'view.php';
817             $optionsreturn = array('id'=>$this->cm->id);
818             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
819         } else {
820             $urlreturn = 'submissions.php';
821             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
822             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
823         }
825         if (!$submission = $this->get_submission($userid) // incorrect submission
826           or !$this->can_delete_files($submission)) {     // can not delete
827             $this->view_header(get_string('delete'));
828             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
829             echo $OUTPUT->continue_button($returnurl);
830             $this->view_footer();
831             die;
832         }
834         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
835             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
836             if (empty($mode)) {
837                 $this->view_header(get_string('delete'));
838             } else {
839                 $PAGE->set_title(get_string('delete'));
840                 echo $OUTPUT->header();
841             }
842             echo $OUTPUT->heading(get_string('delete'));
843             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
844             if (empty($mode)) {
845                 $this->view_footer();
846             } else {
847                 echo $OUTPUT->footer();
848             }
849             die;
850         }
852         $fs = get_file_storage();
853         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
854             $file->delete();
855             $submission->timemodified = time();
856             $DB->update_record('assignment_submissions', $submission);
857             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
858                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
859             $this->update_grade($submission);
860         }
861         redirect($returnurl);
862     }
865     function can_upload_file($submission) {
866         global $USER;
868         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
869           and $this->isopen()                                                 // assignment not closed yet
870           and (empty($submission) or ($submission->userid == $USER->id))        // his/her own submission
871           and !$this->is_finalized($submission)) {                            // no uploading after final submission
872             return true;
873         } else {
874             return false;
875         }
876     }
878     function can_manage_responsefiles() {
879         if (has_capability('mod/assignment:grade', $this->context)) {
880             return true;
881         } else {
882             return false;
883         }
884     }
886     function can_delete_files($submission) {
887         global $USER;
889         if (has_capability('mod/assignment:grade', $this->context)) {
890             return true;
891         }
893         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
894           and $this->isopen()                                      // assignment not closed yet
895           and $this->assignment->resubmit                          // deleting allowed
896           and $USER->id == $submission->userid                     // his/her own submission
897           and !$this->is_finalized($submission)) {                 // no deleting after final submission
898             return true;
899         } else {
900             return false;
901         }
902     }
904     function drafts_tracked() {
905         return !empty($this->assignment->var4);
906     }
908     /**
909      * Returns submission status
910      * @param object $submission - may be empty
911      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
912      */
913     function is_finalized($submission) {
914         if (!$this->drafts_tracked()) {
915             return '';
917         } else if (empty($submission)) {
918             return '';
920         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
921             return $submission->data2;
923         } else {
924             return '';
925         }
926     }
928     function can_unfinalize($submission) {
929         if (!$this->drafts_tracked()) {
930             return false;
931         }
932         if (has_capability('mod/assignment:grade', $this->context)
933           and $this->isopen()
934           and $this->is_finalized($submission)) {
935             return true;
936         } else {
937             return false;
938         }
939     }
941     function can_finalize($submission) {
942         global $USER;
943         if (!$this->drafts_tracked()) {
944             return false;
945         }
947         if ($this->is_finalized($submission)) {
948             return false;
949         }
951         if (has_capability('mod/assignment:grade', $this->context)) {
952             return true;
954         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
955           and $this->isopen()                                                 // assignment not closed yet
956           and !empty($submission)                                             // submission must exist
957           and $submission->userid == $USER->id                                // his/her own submission
958           and ($this->count_user_files($submission->id)
959             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
961             return true;
962         } else {
963             return false;
964         }
965     }
967     function can_update_notes($submission) {
968         global $USER;
970         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
971           and $this->notes_allowed()                                          // notesd must be allowed
972           and $this->isopen()                                                 // assignment not closed yet
973           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
974           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
975             return true;
976         } else {
977             return false;
978         }
979     }
981     function notes_allowed() {
982         return (boolean)$this->assignment->var2;
983     }
985     function count_responsefiles($userid) {
986         if ($submission = $this->get_submission($userid)) {
987             $fs = get_file_storage();
988             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
989             return count($files);
990         } else {
991             return 0;
992         }
993     }
995     function setup_elements(&$mform) {
996         global $CFG, $COURSE;
998         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1000         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1001         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1002         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1003         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1005         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1006         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1007         $mform->setDefault('resubmit', 1);
1009         $options = array();
1010         for($i = 1; $i <= 20; $i++) {
1011             $options[$i] = $i;
1012         }
1013         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1014         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1015         $mform->setDefault('var1', 3);
1017         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1018         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1019         $mform->setDefault('var2', 0);
1021         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1022         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1023         $mform->setDefault('var3', 0);
1025         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1026         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1027         $mform->setDefault('emailteachers', 0);
1029         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1030         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1031         $mform->setDefault('var4', 1);
1033         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
1034         plagiarism_get_form_elements_module($mform, $course_context);
1035     }
1037     function portfolio_exportable() {
1038         return true;
1039     }
1041     function extend_settings_navigation($node) {
1042         global $CFG, $USER, $OUTPUT;
1044         // get users submission if there is one
1045         $submission = $this->get_submission();
1046         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1047             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1048         } else {
1049             $editable = false;
1050         }
1052         // If the user has submitted something add a bit more stuff
1053         if ($submission) {
1054             // Add a view link to the settings nav
1055             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1056             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1057             if (!empty($submission->timemodified)) {
1058                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1059                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1060                 $submittednode->add_class('note');
1061                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1062                     $submittednode->add_class('early');
1063                 } else {
1064                     $submittednode->add_class('late');
1065                 }
1066             }
1067         }
1069         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1070         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1071             $fs = get_file_storage();
1072             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1073                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1074                     $filenode = $node->add(get_string('submission', 'assignment'));
1075                 } else {
1076                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1077                 }
1078                 foreach ($files as $file) {
1079                     $filename = $file->get_filename();
1080                     $mimetype = $file->get_mimetype();
1081                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1082                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1083                 }
1084             }
1085         }
1087         // Show a notes link if they are enabled
1088         if ($this->notes_allowed()) {
1089             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1090             $node->add(get_string('notes', 'assignment'), $link);
1091         }
1092     }
1094     /**
1095      * creates a zip of all assignment submissions and sends a zip to the browser
1096      */
1097     public function download_submissions() {
1098         global $CFG,$DB;
1099         require_once($CFG->libdir.'/filelib.php');
1100         $submissions = $this->get_submissions('','');
1101         if (empty($submissions)) {
1102             print_error('errornosubmissions', 'assignment');
1103         }
1104         $filesforzipping = array();
1105         $fs = get_file_storage();
1107         if (isset($this->cm->groupmode) && empty($this->course->groupmodeforce)) {
1108             $groupmode = $this->cm->groupmode;
1109         } else {
1110             $groupmode = $this->course->groupmode;
1111         }
1112         
1113         $groupid = 0;   // All users
1114         $groupname = '';
1115         if ($groupmode) {
1116             $group = get_current_group($this->course->id, true);
1117             $groupid = $group->id;
1118             $groupname = $group->name.'-';
1119         }
1120         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1121         foreach ($submissions as $submission) {
1122             $a_userid = $submission->userid; //get userid
1123             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1124                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1125                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1127                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1128                 foreach ($files as $file) {
1129                     //get files new name.
1130                     $fileext = strstr($file->get_filename(), '.');
1131                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
1132                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1133                     //save file name to array for zipping.
1134                     $filesforzipping[$fileforzipname] = $file;
1135                 }
1136             }
1137         } // end of foreach loop
1138         if ($zipfile = assignment_pack_files($filesforzipping)) {
1139             send_temp_file($zipfile, $filename); //send file and delete after sending.
1140         }
1141     }
1144 class mod_assignment_upload_notes_form extends moodleform {
1146     function get_data() {
1147         $data = parent::get_data();
1148         if ($data) {
1149             $data->format = $data->text['format'];
1150             $data->text = $data->text['text'];
1151         }
1152         return $data;
1153     }
1155     function set_data($data) {
1156         if (!isset($data->format)) {
1157             $data->format = FORMAT_HTML;
1158         }
1159         if (isset($data->text)) {
1160             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1161         }
1162         parent::set_data($data);
1163     }
1165     function definition() {
1166         $mform = $this->_form;
1168         // visible elements
1169         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1170         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1172         // hidden params
1173         $mform->addElement('hidden', 'id', 0);
1174         $mform->setType('id', PARAM_INT);
1175         $mform->addElement('hidden', 'action', 'savenotes');
1176         $mform->setType('action', PARAM_ALPHA);
1178         // buttons
1179         $this->add_action_buttons();
1180     }
1183 class mod_assignment_upload_response_form extends moodleform {
1184     function definition() {
1185         $mform = $this->_form;
1186         $instance = $this->_customdata;
1188         // visible elements
1189         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1191         // hidden params
1192         $mform->addElement('hidden', 'id', $instance->cm->id);
1193         $mform->setType('id', PARAM_INT);
1194         $mform->addElement('hidden', 'contextid', $instance->contextid);
1195         $mform->setType('contextid', PARAM_INT);
1196         $mform->addElement('hidden', 'action', 'uploadresponse');
1197         $mform->setType('action', PARAM_ALPHA);
1198         $mform->addElement('hidden', 'mode', $instance->mode);
1199         $mform->setType('mode', PARAM_ALPHA);
1200         $mform->addElement('hidden', 'offset', $instance->offset);
1201         $mform->setType('offset', PARAM_INT);
1202         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1203         $mform->setType('forcerefresh', PARAM_INT);
1204         $mform->addElement('hidden', 'userid', $instance->userid);
1205         $mform->setType('userid', PARAM_INT);
1207         // buttons
1208         $this->add_action_buttons(false, get_string('uploadthisfile'));
1209     }