facc70f5b12856c06ff37a818cb97a16a306057f
[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 = $this->get_submission($USER->id)) {
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)), $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;
270         $mode         = optional_param('mode', '', PARAM_ALPHA);
271         $offset       = optional_param('offset', 0, PARAM_INT);
272         $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
274         $mform = new mod_assignment_upload_response_form("$CFG->wwwroot/mod/assignment/upload.php", $this);
276         $mform->set_data(array('id'=>$this->cm->id, 'offset'=>$offset, 'forcerefresh'=>$forcerefresh, 'userid'=>$submission->userid, 'mode'=>$mode));
278         $output = get_string('responsefiles', 'assignment').': ';
280         ob_start();
281         $mform->display();
282         $output = ob_get_clean();
284         if ($forcerefresh) {
285             $output .= $this->update_main_listing($submission);
286         }
288         $responsefiles = $this->print_responsefiles($submission->userid, true);
289         if (!empty($responsefiles)) {
290             $output .= $responsefiles;
291         }
293         if ($return) {
294             return $output;
295         }
296         echo $output;
297         return;
298     }
301     function print_student_answer($userid, $return=false){
302         global $CFG, $OUTPUT, $PAGE;
304         $submission = $this->get_submission($userid);
306         $output = '';
308         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
309             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
310         }
312         if ($this->notes_allowed() and !empty($submission->data1)) {
313             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
314             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
315             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
317             $output .= '&nbsp;';
318         }
320         $renderer = $PAGE->get_renderer('mod_assignment');
321         $output = $OUTPUT->box_start('files').$output;
322         $output .= $renderer->assignment_files($this->context, $submission->id);
323         $output .= $OUTPUT->box_end();
325         return $output;
326     }
329     /**
330      * Produces a list of links to the files uploaded by a user
331      *
332      * @param $userid int optional id of the user. If 0 then $USER->id is used.
333      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
334      * @return string optional
335      */
336     function print_user_files($userid=0, $return=false) {
337         global $CFG, $USER, $OUTPUT, $PAGE;
339         $mode    = optional_param('mode', '', PARAM_ALPHA);
340         $offset  = optional_param('offset', 0, PARAM_INT);
342         if (!$userid) {
343             if (!isloggedin()) {
344                 return '';
345             }
346             $userid = $USER->id;
347         }
349         $output = '';
351         $submission = $this->get_submission($userid);
353         // only during grading
354         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
355             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
356         }
358         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
360             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
361             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
363         }
365         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
366             if ($this->can_unfinalize($submission)) {
367                 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
368                 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('unfinalize', 'assignment'));
369             } else if ($this->can_finalize($submission)) {
370                 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
371                 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('finalize', 'assignment'));
372             }
373         }
375         $renderer = $PAGE->get_renderer('mod_assignment');
376         $output = $OUTPUT->box_start('files').$output;
377         $output .= $renderer->assignment_files($this->context, $submission->id);
378         $output .= $OUTPUT->box_end();
380         if ($return) {
381             return $output;
382         }
383         echo $output;
384     }
386     function print_responsefiles($userid, $return=false) {
387         global $CFG, $USER, $OUTPUT, $PAGE;
389         $mode    = optional_param('mode', '', PARAM_ALPHA);
390         $offset  = optional_param('offset', 0, PARAM_INT);
392         $output = '';
394         $candelete = $this->can_manage_responsefiles();
395         $strdelete   = get_string('delete');
397         $fs = get_file_storage();
398         $browser = get_file_browser();
400         if ($submission = $this->get_submission($userid)) {
401             $renderer = $PAGE->get_renderer('mod_assignment');
402             $output = $OUTPUT->box_start('responsefiles').$output;
403             $output .= $renderer->assignment_files($this->context, $submission->id);
404             $output .= $OUTPUT->box_end();
405         }
407         if ($return) {
408             return $output;
409         }
410         echo $output;
411     }
414     /**
415      * Upload files
416      * upload_file function requires moodle form instance and file manager options
417      * @param object $mform
418      * @param array $options
419      */
420     function upload($mform = null, $filemanager_options = null) {
421         $action = required_param('action', PARAM_ALPHA);
423         switch ($action) {
424             case 'finalize':
425                 $this->finalize();
426                 break;
427             case 'finalizeclose':
428                 $this->finalizeclose();
429                 break;
430             case 'unfinalize':
431                 $this->unfinalize();
432                 break;
433             case 'uploadresponse':
434                 $this->upload_responsefile();
435                 break;
436             case 'uploadfile':
437                 $this->upload_file($mform, $filemanager_options);
438             case 'savenotes':
439             case 'editnotes':
440                 $this->upload_notes();
441             default:
442                 print_error('unknowuploadaction', '', '', $action);
443         }
444     }
446     function upload_notes() {
447         global $CFG, $USER, $OUTPUT, $DB;
449         $action = required_param('action', PARAM_ALPHA);
451         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
453         $mform = new mod_assignment_upload_notes_form();
455         $defaults = new object();
456         $defaults->id = $this->cm->id;
458         if ($submission = $this->get_submission($USER->id)) {
459             $defaults->text = clean_text($submission->data1);
460         } else {
461             $defaults->text = '';
462         }
464         $mform->set_data($defaults);
466         if ($mform->is_cancelled()) {
467             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
468             redirect($returnurl);
469         }
471         if (!$this->can_update_notes($submission)) {
472             $this->view_header(get_string('upload'));
473             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
474             echo $OUTPUT->continue_button($returnurl);
475             $this->view_footer();
476             die;
477         }
479         if ($data = $mform->get_data() and $action == 'savenotes') {
480             $submission = $this->get_submission($USER->id, true); // get or create submission
481             $updated = new object();
482             $updated->id           = $submission->id;
483             $updated->timemodified = time();
484             $updated->data1        = $data->text;
486             if ($DB->update_record('assignment_submissions', $updated)) {
487                 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
488                 redirect($returnurl);
489                 $submission = $this->get_submission($USER->id);
490                 $this->update_grade($submission);
492             } else {
493                 $this->view_header(get_string('notes', 'assignment'));
494                 echo $OUTPUT->notification(get_string('notesupdateerror', 'assignment'));
495                 echo $OUTPUT->continue_button($returnurl);
496                 $this->view_footer();
497                 die;
498             }
499         }
501         /// show notes edit form
502         $this->view_header(get_string('notes', 'assignment'));
504         echo $OUTPUT->heading(get_string('notes', 'assignment'));
506         $mform->display();
508         $this->view_footer();
509         die;
510     }
512     function upload_responsefile() {
513         global $CFG, $USER, $OUTPUT, $PAGE;
515         $userid = required_param('userid', PARAM_INT);
516         $mode   = required_param('mode', PARAM_ALPHA);
517         $offset = required_param('offset', PARAM_INT);
519         $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
521         $mform = new mod_assignment_upload_response_form(null, $this);
522         if ($mform->get_data() and $this->can_manage_responsefiles()) {
523             $fs = get_file_storage();
524             $filename = $mform->get_new_filename('newfile');
525             if ($filename !== false) {
526                 $submission = $this->get_submission($userid, true, true);
527                 if (!$fs->file_exists($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $filename)) {
528                     if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'response', $submission->id, '/', $filename, false, $USER->id)) {
529                         redirect($returnurl);
530                     }
531                 }
532             }
533         }
534         $PAGE->set_title(get_string('upload'));
535         echo $OUTPUT->header();
536         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
537         echo $OUTPUT->continue_button($returnurl);
538         echo $OUTPUT->footer();
539         die;
540     }
542     function upload_file($mform, $options) {
543         global $CFG, $USER, $DB, $OUTPUT;
545         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
546         $filecount = $this->count_user_files($USER->id);
547         $submission = $this->get_submission($USER->id);
549         if (!$this->can_upload_file($submission)) {
550             $this->view_header(get_string('upload'));
551             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
552             echo $OUTPUT->continue_button($returnurl);
553             $this->view_footer();
554             die;
555         }
557         if ($formdata = $mform->get_data()) {
558             $fs = get_file_storage();
559             $submission = $this->get_submission($USER->id, true); //create new submission if needed
560             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
561             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
562             $updates = new object();
563             $updates->id = $submission->id;
564             $updates->timemodified = time();
565             if ($DB->update_record('assignment_submissions', $updates)) {
566                 add_to_log($this->course->id, 'assignment', 'upload',
567                         'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
568                 $this->update_grade($submission);
569                 if (!$this->drafts_tracked()) {
570                     $this->email_teachers($submission);
571                 }
573                 // send files to event system
574                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
575                 // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
576                 $eventdata = new object();
577                 $eventdata->modulename   = 'assignment';
578                 $eventdata->cmid         = $this->cm->id;
579                 $eventdata->itemid       = $submission->id;
580                 $eventdata->courseid     = $this->course->id;
581                 $eventdata->userid       = $USER->id;
582                 if ($files) {
583                     $eventdata->files        = $files;
584                 }
585                 events_trigger('assessable_file_uploaded', $eventdata);
586             }
587             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
588             redirect($returnurl);
589         }
591         $this->view_header(get_string('upload'));
592         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
593         echo $OUTPUT->continue_button($returnurl);
594         $this->view_footer();
595         die;
596     }
598     function send_file($filearea, $args) {
599         global $CFG, $DB, $USER;
600         require_once($CFG->libdir.'/filelib.php');
602         require_login($this->course, false, $this->cm);
604         if ($filearea === 'submission') {
605             $submissionid = (int)array_shift($args);
607             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
608                 return false;
609             }
611             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
612                 return false;
613             }
615             $relativepath = implode('/', $args);
616             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
618             $fs = get_file_storage();
619             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
620                 return false;
621             }
622             send_stored_file($file, 0, 0, true); // download MUST be forced - security!
624         } else if ($filearea === 'response') {
625             $submissionid = (int)array_shift($args);
627             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
628                 return false;
629             }
631             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
632                 return false;
633             }
635             $relativepath = implode('/', $args);
636             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
638             $fs = get_file_storage();
639             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
640                 return false;
641             }
642             send_stored_file($file, 0, 0, true);
643         }
645         return false;
646     }
648     function finalize() {
649         global $USER, $DB, $OUTPUT;
651         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
652         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
653         $submission = $this->get_submission($USER->id);
655         if (!$this->can_finalize($submission)) {
656             redirect($returnurl); // probably already graded, redirect to assignment page, the reason should be obvious
657         }
659         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
660             $optionsno = array('id'=>$this->cm->id);
661             $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
662             $this->view_header(get_string('submitformarking', 'assignment'));
663             echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
664             echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
665             $this->view_footer();
666             die;
668         }
669         $updated = new object();
670         $updated->id           = $submission->id;
671         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
672         $updated->timemodified = time();
674         if ($DB->update_record('assignment_submissions', $updated)) {
675             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
676                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
677             $submission = $this->get_submission($USER->id);
678             $this->update_grade($submission);
679             $this->email_teachers($submission);
680         } else {
681             $this->view_header(get_string('submitformarking', 'assignment'));
682             echo $OUTPUT->notification(get_string('finalizeerror', 'assignment'));
683             echo $OUTPUT->continue_button($returnurl);
684             $this->view_footer();
685             die;
686         }
688         // Trigger assessable_files_done event to show files are complete
689         $eventdata = new object();
690         $eventdata->modulename   = 'assignment';
691         $eventdata->cmid         = $this->cm->id;
692         $eventdata->itemid       = $submission->id;
693         $eventdata->courseid     = $this->course->id;
694         $eventdata->userid       = $USER->id;
695         events_trigger('assessable_files_done', $eventdata);
697         redirect($returnurl);
698     }
700     function finalizeclose() {
701         global $DB;
703         $userid    = optional_param('userid', 0, PARAM_INT);
704         $mode      = required_param('mode', PARAM_ALPHA);
705         $offset    = required_param('offset', PARAM_INT);
706         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
708         // create but do not add student submission date
709         $submission = $this->get_submission($userid, true, true);
711         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
712             redirect($returnurl); // probably closed already
713         }
715         $updated = new object();
716         $updated->id    = $submission->id;
717         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
719         if ($DB->update_record('assignment_submissions', $updated)) {
720             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
721                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
722             $submission = $this->get_submission($userid, false, true);
723             $this->update_grade($submission);
724         }
725         redirect($returnurl);
726     }
728     function unfinalize() {
729         global $DB;
731         $userid = required_param('userid', PARAM_INT);
732         $mode   = required_param('mode', PARAM_ALPHA);
733         $offset = required_param('offset', PARAM_INT);
735         $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
737         if (data_submitted()
738           and $submission = $this->get_submission($userid)
739           and $this->can_unfinalize($submission)
740           and confirm_sesskey()) {
742             $updated = new object();
743             $updated->id = $submission->id;
744             $updated->data2 = '';
745             if ($DB->update_record('assignment_submissions', $updated)) {
746                 //TODO: add unfinalize action to log
747                 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
748                 $submission = $this->get_submission($userid);
749                 $this->update_grade($submission);
750             } else {
751                 $this->view_header(get_string('submitformarking', 'assignment'));
752                 echo $OUTPUT->notification(get_string('unfinalizeerror', 'assignment'));
753                 echo $OUTPUT->continue_button($returnurl);
754                 $this->view_footer();
755                 die;
756             }
757         }
758         redirect($returnurl);
759     }
762     function delete() {
763         $action   = optional_param('action', '', PARAM_ALPHA);
765         switch ($action) {
766             case 'response':
767                 $this->delete_responsefile();
768                 break;
769             default:
770                 $this->delete_file();
771         }
772         die;
773     }
776     function delete_responsefile() {
777         global $CFG, $OUTPUT,$PAGE;
779         $file     = required_param('file', PARAM_FILE);
780         $userid   = required_param('userid', PARAM_INT);
781         $mode     = required_param('mode', PARAM_ALPHA);
782         $offset   = required_param('offset', PARAM_INT);
783         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
785         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
787         if (!$this->can_manage_responsefiles()) {
788            redirect($returnurl);
789         }
791         $urlreturn = 'submissions.php';
792         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
794         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
795             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
796             $PAGE->set_title(get_string('delete'));
797             echo $OUTPUT->header();
798             echo $OUTPUT->heading(get_string('delete'));
799             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
800             echo $OUTPUT->footer();
801             die;
802         }
804         if ($submission = $this->get_submission($userid)) {
805             $fs = get_file_storage();
806             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
807                 $file->delete();
808             }
809         }
810         redirect($returnurl);
811     }
814     function delete_file() {
815         global $CFG, $DB, $OUTPUT, $PAGE;
817         $file     = required_param('file', PARAM_FILE);
818         $userid   = required_param('userid', PARAM_INT);
819         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
820         $mode     = optional_param('mode', '', PARAM_ALPHA);
821         $offset   = optional_param('offset', 0, PARAM_INT);
823         require_login($this->course->id, false, $this->cm);
825         if (empty($mode)) {
826             $urlreturn = 'view.php';
827             $optionsreturn = array('id'=>$this->cm->id);
828             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
829         } else {
830             $urlreturn = 'submissions.php';
831             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
832             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
833         }
835         if (!$submission = $this->get_submission($userid) // incorrect submission
836           or !$this->can_delete_files($submission)) {     // can not delete
837             $this->view_header(get_string('delete'));
838             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
839             echo $OUTPUT->continue_button($returnurl);
840             $this->view_footer();
841             die;
842         }
844         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
845             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
846             if (empty($mode)) {
847                 $this->view_header(get_string('delete'));
848             } else {
849                 $PAGE->set_title(get_string('delete'));
850                 echo $OUTPUT->header();
851             }
852             echo $OUTPUT->heading(get_string('delete'));
853             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
854             if (empty($mode)) {
855                 $this->view_footer();
856             } else {
857                 echo $OUTPUT->footer();
858             }
859             die;
860         }
862         $fs = get_file_storage();
863         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
864             $file->delete();
865             $submission->timemodified = time();
866             $DB->update_record('assignment_submissions', $submission);
867             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
868                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
869             $this->update_grade($submission);
870         }
871         redirect($returnurl);
872     }
875     function can_upload_file($submission) {
876         global $USER;
878         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
879           and $this->isopen()                                                 // assignment not closed yet
880           and (empty($submission) or $submission->userid == $USER->id)        // his/her own submission
881           and $this->count_user_files($USER->id) < $this->assignment->var1    // file limit not reached
882           and !$this->is_finalized($submission)) {                            // no uploading after final submission
883             return true;
884         } else {
885             return false;
886         }
887     }
889     function can_manage_responsefiles() {
890         if (has_capability('mod/assignment:grade', $this->context)) {
891             return true;
892         } else {
893             return false;
894         }
895     }
897     function can_delete_files($submission) {
898         global $USER;
900         if (has_capability('mod/assignment:grade', $this->context)) {
901             return true;
902         }
904         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
905           and $this->isopen()                                      // assignment not closed yet
906           and $this->assignment->resubmit                          // deleting allowed
907           and $USER->id == $submission->userid                     // his/her own submission
908           and !$this->is_finalized($submission)) {                 // no deleting after final submission
909             return true;
910         } else {
911             return false;
912         }
913     }
915     function drafts_tracked() {
916         return !empty($this->assignment->var4);
917     }
919     /**
920      * Returns submission status
921      * @param object $submission - may be empty
922      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
923      */
924     function is_finalized($submission) {
925         if (!$this->drafts_tracked()) {
926             return '';
928         } else if (empty($submission)) {
929             return '';
931         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
932             return $submission->data2;
934         } else {
935             return '';
936         }
937     }
939     function can_unfinalize($submission) {
940         if (!$this->drafts_tracked()) {
941             return false;
942         }
943         if (has_capability('mod/assignment:grade', $this->context)
944           and $this->isopen()
945           and $this->is_finalized($submission)) {
946             return true;
947         } else {
948             return false;
949         }
950     }
952     function can_finalize($submission) {
953         global $USER;
954         if (!$this->drafts_tracked()) {
955             return false;
956         }
958         if ($this->is_finalized($submission)) {
959             return false;
960         }
962         if (has_capability('mod/assignment:grade', $this->context)) {
963             return true;
965         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
966           and $this->isopen()                                                 // assignment not closed yet
967           and !empty($submission)                                             // submission must exist
968           and $submission->userid == $USER->id                                // his/her own submission
969           and ($this->count_user_files($USER->id)
970             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
972             return true;
973         } else {
974             return false;
975         }
976     }
978     function can_update_notes($submission) {
979         global $USER;
981         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
982           and $this->notes_allowed()                                          // notesd must be allowed
983           and $this->isopen()                                                 // assignment not closed yet
984           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
985           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
986             return true;
987         } else {
988             return false;
989         }
990     }
992     function notes_allowed() {
993         return (boolean)$this->assignment->var2;
994     }
996     function count_responsefiles($userid) {
997         if ($submission = $this->get_submission($userid)) {
998             $fs = get_file_storage();
999             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
1000             return count($files);
1001         } else {
1002             return 0;
1003         }
1004     }
1006     function setup_elements(&$mform) {
1007         global $CFG, $COURSE;
1009         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1011         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1012         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1013         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1014         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1016         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1017         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1018         $mform->setDefault('resubmit', 1);
1020         $options = array();
1021         for($i = 1; $i <= 20; $i++) {
1022             $options[$i] = $i;
1023         }
1024         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1025         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1026         $mform->setDefault('var1', 3);
1028         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1029         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1030         $mform->setDefault('var2', 0);
1032         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1033         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1034         $mform->setDefault('var3', 0);
1036         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1037         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1038         $mform->setDefault('emailteachers', 0);
1040         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1041         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1042         $mform->setDefault('var4', 1);
1044     }
1046     function portfolio_exportable() {
1047         return true;
1048     }
1050     function extend_settings_navigation($node) {
1051         global $CFG, $USER, $OUTPUT;
1053         // get users submission if there is one
1054         $submission = $this->get_submission();
1055         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1056             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1057         } else {
1058             $editable = false;
1059         }
1061         // If the user has submitted something add a bit more stuff
1062         if ($submission) {
1063             // Add a view link to the settings nav
1064             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1065             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1066             if (!empty($submission->timemodified)) {
1067                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1068                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1069                 $submittednode->add_class('note');
1070                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1071                     $submittednode->add_class('early');
1072                 } else {
1073                     $submittednode->add_class('late');
1074                 }
1075             }
1076         }
1078         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1079         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($USER->id)) {
1080             $fs = get_file_storage();
1081             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1082                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1083                     $filenode = $node->add(get_string('submission', 'assignment'));
1084                 } else {
1085                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1086                 }
1087                 foreach ($files as $file) {
1088                     $filename = $file->get_filename();
1089                     $mimetype = $file->get_mimetype();
1090                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1091                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1092                 }
1093             }
1094         }
1096         // Show a notes link if they are enabled
1097         if ($this->notes_allowed()) {
1098             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1099             $node->add(get_string('notes', 'assignment'), $link);
1100         }
1101     }
1103     /**
1104      * creates a zip of all assignment submissions and sends a zip to the browser
1105      */
1106     public function download_submissions() {
1107         global $CFG,$DB;
1108         require_once($CFG->libdir.'/filelib.php');
1109         $submissions = $this->get_submissions('','');
1110         if (empty($submissions)) {
1111             error("there are no submissions to download");
1112         }
1113         $filesforzipping = array();
1114         $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
1115         $fs = get_file_storage();
1117         $groupmode = groupmode($this->course,$this->cm);
1118         $groupid = 0;   // All users
1119         $groupname = '';
1120         if($groupmode) {
1121             $group = get_current_group($this->course->id, true);
1122             $groupid = $group->id;
1123             $groupname = $group->name.'-';
1124         }
1125         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1126         foreach ($submissions as $submission) {
1127             $a_userid = $submission->userid; //get userid
1128             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1129                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1130                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1132                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1133                 foreach ($files as $file) {
1134                     //get files new name.
1135                     $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
1136                     //save file name to array for zipping.
1137                     $filesforzipping[$fileforzipname] = $file;
1138                 }
1139             }
1140         } // end of foreach loop
1141         if ($zipfile = assignment_pack_files($filesforzipping)) {
1142             send_temp_file($zipfile, $filename); //send file and delete after sending.
1143         }
1144     }
1147 class mod_assignment_upload_notes_form extends moodleform {
1149     function get_data() {
1150         $data = parent::get_data();
1151         if ($data) {
1152             $data->format = $data->text['format'];
1153             $data->text = $data->text['text'];
1154         }
1155         return $data;
1156     }
1158     function set_data($data) {
1159         if (!isset($data->format)) {
1160             $data->format = FORMAT_HTML;
1161         }
1162         if (isset($data->text)) {
1163             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1164         }
1165         parent::set_data($data);
1166     }
1168     function definition() {
1169         $mform = $this->_form;
1171         // visible elements
1172         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1173         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1175         // hidden params
1176         $mform->addElement('hidden', 'id', 0);
1177         $mform->setType('id', PARAM_INT);
1178         $mform->addElement('hidden', 'action', 'savenotes');
1179         $mform->setType('action', PARAM_ALPHA);
1181         // buttons
1182         $this->add_action_buttons();
1183     }
1186 class mod_assignment_upload_response_form extends moodleform {
1187     function definition() {
1188         $mform = $this->_form;
1189         $instance = $this->_customdata;
1191         // visible elements
1192         $mform->addElement('file', 'newfile', get_string('uploadafile'));
1194         // hidden params
1195         $mform->addElement('hidden', 'id', $instance->cm->id);
1196         $mform->setType('id', PARAM_INT);
1197         $mform->addElement('hidden', 'action', 'uploadresponse');
1198         $mform->setType('action', PARAM_ALPHA);
1199         $mform->addElement('hidden', 'mode');
1200         $mform->setType('mode', PARAM_ALPHA);
1201         $mform->addElement('hidden', 'offset');
1202         $mform->setType('offset', PARAM_INT);
1203         $mform->addElement('hidden', 'forcerefresh');
1204         $mform->setType('forcerefresh', PARAM_INT);
1205         $mform->addElement('hidden', 'userid');
1206         $mform->setType('userid', PARAM_INT);
1208         // buttons
1209         $this->add_action_buttons(false, get_string('uploadthisfile'));
1210     }