Assignment MDL-24948 fix depreceated calls to get group info
[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             }
93             if ($this->notes_allowed()) {
94                 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
95                 $this->view_notes();
96             }
98             $this->view_final_submission();
99         }
100         $this->view_footer();
101     }
104     function view_feedback($submission=NULL) {
105         global $USER, $CFG, $DB, $OUTPUT;
106         require_once($CFG->libdir.'/gradelib.php');
108         if (!$submission) { /// Get submission for this assignment
109             $submission = $this->get_submission($USER->id);
110         }
112         if (empty($submission->timemarked)) {   /// Nothing to show, so print nothing
113             if ($this->count_responsefiles($USER->id)) {
114                 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
115                 $responsefiles = $this->print_responsefiles($USER->id, true);
116                 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
117             }
118             return;
119         }
121         $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
122         $item = $grading_info->items[0];
123         $grade = $item->grades[$USER->id];
125         if ($grade->hidden or $grade->grade === false) { // hidden or error
126             return;
127         }
129         if ($grade->grade === null and empty($grade->str_feedback)) {   /// Nothing to show yet
130             return;
131         }
133         $graded_date = $grade->dategraded;
134         $graded_by   = $grade->usermodified;
136     /// We need the teacher info
137         if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
138             print_error('cannotfindteacher');
139         }
141     /// Print the feedback
142         echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
144         echo '<table cellspacing="0" class="feedback">';
146         echo '<tr>';
147         echo '<td class="left picture">';
148         echo $OUTPUT->user_picture($teacher);
149         echo '</td>';
150         echo '<td class="topic">';
151         echo '<div class="from">';
152         echo '<div class="fullname">'.fullname($teacher).'</div>';
153         echo '<div class="time">'.userdate($graded_date).'</div>';
154         echo '</div>';
155         echo '</td>';
156         echo '</tr>';
158         echo '<tr>';
159         echo '<td class="left side">&nbsp;</td>';
160         echo '<td class="content">';
161         if ($this->assignment->grade) {
162             echo '<div class="grade">';
163             echo get_string("grade").': '.$grade->str_long_grade;
164             echo '</div>';
165             echo '<div class="clearer"></div>';
166         }
168         echo '<div class="comment">';
169         echo $grade->str_feedback;
170         echo '</div>';
171         echo '</tr>';
173         echo '<tr>';
174         echo '<td class="left side">&nbsp;</td>';
175         echo '<td class="content">';
176         echo $this->print_responsefiles($USER->id, true);
177         echo '</tr>';
179         echo '</table>';
180     }
183     function view_upload_form() {
184         global $CFG, $USER, $OUTPUT;
186         $submission = $this->get_submission($USER->id);
188         if ($this->is_finalized($submission)) {
189             // no uploading
190             return;
191         }
193         if ($this->can_upload_file($submission)) {
194             $fs = get_file_storage();
195             // edit files in another page
196             if ($submission) {
197                 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
198                     $str = get_string('editthesefiles', 'assignment');
199                 } else {
200                     $str = get_string('uploadfiles', 'assignment');
201                 }
202             } else {
203                 $str = get_string('uploadfiles', 'assignment');
204             }
205             echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
206         }
207     }
209     function view_notes() {
210         global $USER, $OUTPUT;
212         if ($submission = $this->get_submission($USER->id)
213           and !empty($submission->data1)) {
214             echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML), 'generalbox boxaligncenter boxwidthwide');
215         } else {
216             echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
217         }
218         if ($this->can_update_notes($submission)) {
219             $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
220             echo '<div style="text-align:center">';
221             echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
222             echo '</div>';
223         }
224     }
226     function view_final_submission() {
227         global $CFG, $USER, $OUTPUT;
229         $submission = $this->get_submission($USER->id);
231         if ($this->isopen() and $this->can_finalize($submission)) {
232             //print final submit button
233             echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
234             echo '<div style="text-align:center">';
235             echo '<form method="post" action="upload.php">';
236             echo '<fieldset class="invisiblefieldset">';
237             echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
238             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
239             echo '<input type="hidden" name="action" value="finalize" />';
240             echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
241             echo '</fieldset>';
242             echo '</form>';
243             echo '</div>';
244         } else if (!$this->isopen()) {
245             echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
247         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
248             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
249                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
250             } else {
251                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
252             }
253         } else {
254             //no submission yet
255         }
256     }
259     /**
260      * Return true if var3 == hide description till available day
261      *
262      *@return boolean
263      */
264     function description_is_hidden() {
265         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
266     }
268     function print_student_answer($userid, $return=false){
269         global $CFG, $OUTPUT, $PAGE;
271         $submission = $this->get_submission($userid);
273         $output = '';
275         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
276             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
277         }
279         if ($this->notes_allowed() and !empty($submission->data1)) {
280             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
281             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
282             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
284             $output .= '&nbsp;';
285         }
288         $renderer = $PAGE->get_renderer('mod_assignment');
289         $output = $OUTPUT->box_start('files').$output;
290         $output .= $renderer->assignment_files($this->context, $submission->id);
291         $output .= $OUTPUT->box_end();
293         return $output;
294     }
297     /**
298      * Produces a list of links to the files uploaded by a user
299      *
300      * @param $userid int optional id of the user. If 0 then $USER->id is used.
301      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
302      * @return string optional
303      */
304     function print_user_files($userid=0, $return=false) {
305         global $CFG, $USER, $OUTPUT, $PAGE;
307         $mode    = optional_param('mode', '', PARAM_ALPHA);
308         $offset  = optional_param('offset', 0, PARAM_INT);
310         if (!$userid) {
311             if (!isloggedin()) {
312                 return '';
313             }
314             $userid = $USER->id;
315         }
317         $output = $OUTPUT->box_start('files');
319         $submission = $this->get_submission($userid);
321         // only during grading
322         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
323             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
324         }
326         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
328             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
329             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
331         }
333         if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
334             if ($this->can_unfinalize($submission)) {
335                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
336                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
337                 $output .=  $OUTPUT->help_icon('unfinalize', 'assignment');
339             } else if ($this->can_finalize($submission)) {
340                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
341                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
342             }
343         }
345         if ($submission) {
346             $renderer = $PAGE->get_renderer('mod_assignment');
347             $output .= $renderer->assignment_files($this->context, $submission->id);
348         }
349         $output .= $OUTPUT->box_end();
351         if ($return) {
352             return $output;
353         }
354         echo $output;
355     }
357     function submissions($mode) {
358         // redirects out of form to process (un)finalizing.
359         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
360         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
361         if ($unfinalize) {
362             $this->unfinalize('single');
363         } else if ($finalize) {
364             $this->finalize('single');
365         }
366         if ($unfinalize || $finalize) {
367             $mode = 'singlenosave';
368         }
369         parent::submissions($mode);
370     }
372     function process_feedback() {
373         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
374             return false;
375         }
376         $userid = required_param('userid', PARAM_INT);
377         $offset = required_param('offset', PARAM_INT);
378         $mform = $this->display_submission($offset, $userid, false);
379         parent::process_feedback($mform);
380     }
382     function print_responsefiles($userid, $return=false) {
383         global $CFG, $USER, $OUTPUT, $PAGE;
385         $mode    = optional_param('mode', '', PARAM_ALPHA);
386         $offset  = optional_param('offset', 0, PARAM_INT);
388         $output = $OUTPUT->box_start('responsefiles');
390         $candelete = $this->can_manage_responsefiles();
391         $strdelete   = get_string('delete');
393         $fs = get_file_storage();
394         $browser = get_file_browser();
396         if ($submission = $this->get_submission($userid)) {
397             $renderer = $PAGE->get_renderer('mod_assignment');
398             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
399         }
400         $output .= $OUTPUT->box_end();
402         if ($return) {
403             return $output;
404         }
405         echo $output;
406     }
409     /**
410      * Upload files
411      * upload_file function requires moodle form instance and file manager options
412      * @param object $mform
413      * @param array $options
414      */
415     function upload($mform = null, $filemanager_options = null) {
416         $action = required_param('action', PARAM_ALPHA);
417         switch ($action) {
418             case 'finalize':
419                 $this->finalize();
420                 break;
421             case 'finalizeclose':
422                 $this->finalizeclose();
423                 break;
424             case 'unfinalize':
425                 $this->unfinalize();
426                 break;
427             case 'uploadresponse':
428                 $this->upload_responsefile($mform, $filemanager_options);
429                 break;
430             case 'uploadfile':
431                 $this->upload_file($mform, $filemanager_options);
432             case 'savenotes':
433             case 'editnotes':
434                 $this->upload_notes();
435             default:
436                 print_error('unknowuploadaction', '', '', $action);
437         }
438     }
440     function upload_notes() {
441         global $CFG, $USER, $OUTPUT, $DB;
443         $action = required_param('action', PARAM_ALPHA);
445         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
447         $mform = new mod_assignment_upload_notes_form();
449         $defaults = new stdClass();
450         $defaults->id = $this->cm->id;
452         if ($submission = $this->get_submission($USER->id)) {
453             $defaults->text = clean_text($submission->data1);
454         } else {
455             $defaults->text = '';
456         }
458         $mform->set_data($defaults);
460         if ($mform->is_cancelled()) {
461             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
462             redirect($returnurl);
463         }
465         if (!$this->can_update_notes($submission)) {
466             $this->view_header(get_string('upload'));
467             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
468             echo $OUTPUT->continue_button($returnurl);
469             $this->view_footer();
470             die;
471         }
473         if ($data = $mform->get_data() and $action == 'savenotes') {
474             $submission = $this->get_submission($USER->id, true); // get or create submission
475             $updated = new stdClass();
476             $updated->id           = $submission->id;
477             $updated->timemodified = time();
478             $updated->data1        = $data->text;
480             $DB->update_record('assignment_submissions', $updated);
481             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
482             redirect($returnurl);
483             $submission = $this->get_submission($USER->id);
484             $this->update_grade($submission);
485         }
487         /// show notes edit form
488         $this->view_header(get_string('notes', 'assignment'));
490         echo $OUTPUT->heading(get_string('notes', 'assignment'));
492         $mform->display();
494         $this->view_footer();
495         die;
496     }
498     function upload_responsefile($mform, $options) {
499         global $CFG, $USER, $OUTPUT, $PAGE;
501         $userid = required_param('userid', PARAM_INT);
502         $mode   = required_param('mode', PARAM_ALPHA);
503         $offset = required_param('offset', PARAM_INT);
505         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
507         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
508             $fs = get_file_storage();
509             $submission = $this->get_submission($userid, true, true);
510             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
511                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
512                 redirect($returnurl->out(false));
513             }
514         }
515         $PAGE->set_title(get_string('upload'));
516         echo $OUTPUT->header();
517         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
518         echo $OUTPUT->continue_button($returnurl->out(true));
519         echo $OUTPUT->footer();
520         die;
521     }
523     function upload_file($mform, $options) {
524         global $CFG, $USER, $DB, $OUTPUT;
526         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
527         $submission = $this->get_submission($USER->id);
528         $filecount = $this->count_user_files($submission->id);
530         if (!$this->can_upload_file($submission)) {
531             $this->view_header(get_string('upload'));
532             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
533             echo $OUTPUT->continue_button($returnurl);
534             $this->view_footer();
535             die;
536         }
538         if ($formdata = $mform->get_data()) {
539             $fs = get_file_storage();
540             $submission = $this->get_submission($USER->id, true); //create new submission if needed
541             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
542             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
543             $updates = new stdClass();
544             $updates->id = $submission->id;
545             $updates->timemodified = time();
546             $DB->update_record('assignment_submissions', $updates);
547             add_to_log($this->course->id, 'assignment', 'upload',
548                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
549             $this->update_grade($submission);
550             if (!$this->drafts_tracked()) {
551                 $this->email_teachers($submission);
552             }
554             // send files to event system
555             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
556             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
557             $eventdata = new stdClass();
558             $eventdata->modulename   = 'assignment';
559             $eventdata->cmid         = $this->cm->id;
560             $eventdata->itemid       = $submission->id;
561             $eventdata->courseid     = $this->course->id;
562             $eventdata->userid       = $USER->id;
563             if ($files) {
564                 $eventdata->files        = $files;
565             }
566             events_trigger('assessable_file_uploaded', $eventdata);
567             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
568             redirect($returnurl);
569         }
571         $this->view_header(get_string('upload'));
572         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
573         echo $OUTPUT->continue_button($returnurl);
574         $this->view_footer();
575         die;
576     }
578     function send_file($filearea, $args) {
579         global $CFG, $DB, $USER;
580         require_once($CFG->libdir.'/filelib.php');
582         require_login($this->course, false, $this->cm);
584         if ($filearea === 'submission') {
585             $submissionid = (int)array_shift($args);
587             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
588                 return false;
589             }
591             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
592                 return false;
593             }
595             $relativepath = implode('/', $args);
596             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
598             $fs = get_file_storage();
599             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
600                 return false;
601             }
602             send_stored_file($file, 0, 0, true); // download MUST be forced - security!
604         } else if ($filearea === 'response') {
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/response/$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);
623         }
625         return false;
626     }
628     function finalize($forcemode=null) {
629         global $USER, $DB, $OUTPUT;
630         $userid = optional_param('userid', $USER->id, PARAM_INT);
631         $offset = optional_param('offset', 0, PARAM_INT);
632         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
633         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
634         $submission = $this->get_submission($userid);
636         if ($forcemode!=null) {
637             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
638                 array('id'=>$this->cm->id,
639                     'userid'=>$userid,
640                     'mode'=>$forcemode,
641                     'offset'=>$offset
642                 ));
643         }
645         if (!$this->can_finalize($submission)) {
646             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
647         }
649         if ($forcemode==null) {
650             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
651                 $optionsno = array('id'=>$this->cm->id);
652                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
653                 $this->view_header(get_string('submitformarking', 'assignment'));
654                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
655                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
656                 $this->view_footer();
657                 die;
658             }
659         }
660         $updated = new stdClass();
661         $updated->id           = $submission->id;
662         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
663         $updated->timemodified = time();
665         $DB->update_record('assignment_submissions', $updated);
666         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
667                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
668         $submission = $this->get_submission($userid);
669         $this->update_grade($submission);
670         $this->email_teachers($submission);
672         // Trigger assessable_files_done event to show files are complete
673         $eventdata = new stdClass();
674         $eventdata->modulename   = 'assignment';
675         $eventdata->cmid         = $this->cm->id;
676         $eventdata->itemid       = $submission->id;
677         $eventdata->courseid     = $this->course->id;
678         $eventdata->userid       = $userid;
679         events_trigger('assessable_files_done', $eventdata);
681         if ($forcemode==null) {
682             redirect($returnurl->out(false));
683         }
684     }
686     function finalizeclose() {
687         global $DB;
689         $userid    = optional_param('userid', 0, PARAM_INT);
690         $mode      = required_param('mode', PARAM_ALPHA);
691         $offset    = required_param('offset', PARAM_INT);
692         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
694         // create but do not add student submission date
695         $submission = $this->get_submission($userid, true, true);
697         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
698             redirect($returnurl); // probably closed already
699         }
701         $updated = new stdClass();
702         $updated->id    = $submission->id;
703         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
705         $DB->update_record('assignment_submissions', $updated);
706         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
707                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
708         $submission = $this->get_submission($userid, false, true);
709         $this->update_grade($submission);
710         redirect($returnurl);
711     }
713     function unfinalize($forcemode=null) {
714         global $DB;
716         $userid = required_param('userid', PARAM_INT);
717         $mode   = required_param('mode', PARAM_ALPHA);
718         $offset = required_param('offset', PARAM_INT);
720         if ($forcemode!=null) {
721             $mode=$forcemode;
722         }
723         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
724         if (data_submitted()
725           and $submission = $this->get_submission($userid)
726           and $this->can_unfinalize($submission)
727           and confirm_sesskey()) {
729             $updated = new stdClass();
730             $updated->id = $submission->id;
731             $updated->data2 = '';
732             $DB->update_record('assignment_submissions', $updated);
733             //TODO: add unfinalize action to log
734             add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
735             $submission = $this->get_submission($userid);
736             $this->update_grade($submission);
737         }
739         if ($forcemode==null) {
740             redirect($returnurl);
741         }
742     }
745     function delete() {
746         $action   = optional_param('action', '', PARAM_ALPHA);
748         switch ($action) {
749             case 'response':
750                 $this->delete_responsefile();
751                 break;
752             default:
753                 $this->delete_file();
754         }
755         die;
756     }
759     function delete_responsefile() {
760         global $CFG, $OUTPUT,$PAGE;
762         $file     = required_param('file', PARAM_FILE);
763         $userid   = required_param('userid', PARAM_INT);
764         $mode     = required_param('mode', PARAM_ALPHA);
765         $offset   = required_param('offset', PARAM_INT);
766         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
768         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
770         if (!$this->can_manage_responsefiles()) {
771            redirect($returnurl);
772         }
774         $urlreturn = 'submissions.php';
775         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
777         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
778             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
779             $PAGE->set_title(get_string('delete'));
780             echo $OUTPUT->header();
781             echo $OUTPUT->heading(get_string('delete'));
782             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
783             echo $OUTPUT->footer();
784             die;
785         }
787         if ($submission = $this->get_submission($userid)) {
788             $fs = get_file_storage();
789             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
790                 $file->delete();
791             }
792         }
793         redirect($returnurl);
794     }
797     function delete_file() {
798         global $CFG, $DB, $OUTPUT, $PAGE;
800         $file     = required_param('file', PARAM_FILE);
801         $userid   = required_param('userid', PARAM_INT);
802         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
803         $mode     = optional_param('mode', '', PARAM_ALPHA);
804         $offset   = optional_param('offset', 0, PARAM_INT);
806         require_login($this->course->id, false, $this->cm);
808         if (empty($mode)) {
809             $urlreturn = 'view.php';
810             $optionsreturn = array('id'=>$this->cm->id);
811             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
812         } else {
813             $urlreturn = 'submissions.php';
814             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
815             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
816         }
818         if (!$submission = $this->get_submission($userid) // incorrect submission
819           or !$this->can_delete_files($submission)) {     // can not delete
820             $this->view_header(get_string('delete'));
821             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
822             echo $OUTPUT->continue_button($returnurl);
823             $this->view_footer();
824             die;
825         }
827         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
828             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
829             if (empty($mode)) {
830                 $this->view_header(get_string('delete'));
831             } else {
832                 $PAGE->set_title(get_string('delete'));
833                 echo $OUTPUT->header();
834             }
835             echo $OUTPUT->heading(get_string('delete'));
836             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
837             if (empty($mode)) {
838                 $this->view_footer();
839             } else {
840                 echo $OUTPUT->footer();
841             }
842             die;
843         }
845         $fs = get_file_storage();
846         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
847             $file->delete();
848             $submission->timemodified = time();
849             $DB->update_record('assignment_submissions', $submission);
850             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
851                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
852             $this->update_grade($submission);
853         }
854         redirect($returnurl);
855     }
858     function can_upload_file($submission) {
859         global $USER;
861         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
862           and $this->isopen()                                                 // assignment not closed yet
863           and (empty($submission) or ($submission->userid == $USER->id))        // his/her own submission
864           and !$this->is_finalized($submission)) {                            // no uploading after final submission
865             return true;
866         } else {
867             return false;
868         }
869     }
871     function can_manage_responsefiles() {
872         if (has_capability('mod/assignment:grade', $this->context)) {
873             return true;
874         } else {
875             return false;
876         }
877     }
879     function can_delete_files($submission) {
880         global $USER;
882         if (has_capability('mod/assignment:grade', $this->context)) {
883             return true;
884         }
886         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
887           and $this->isopen()                                      // assignment not closed yet
888           and $this->assignment->resubmit                          // deleting allowed
889           and $USER->id == $submission->userid                     // his/her own submission
890           and !$this->is_finalized($submission)) {                 // no deleting after final submission
891             return true;
892         } else {
893             return false;
894         }
895     }
897     function drafts_tracked() {
898         return !empty($this->assignment->var4);
899     }
901     /**
902      * Returns submission status
903      * @param object $submission - may be empty
904      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
905      */
906     function is_finalized($submission) {
907         if (!$this->drafts_tracked()) {
908             return '';
910         } else if (empty($submission)) {
911             return '';
913         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
914             return $submission->data2;
916         } else {
917             return '';
918         }
919     }
921     function can_unfinalize($submission) {
922         if (!$this->drafts_tracked()) {
923             return false;
924         }
925         if (has_capability('mod/assignment:grade', $this->context)
926           and $this->isopen()
927           and $this->is_finalized($submission)) {
928             return true;
929         } else {
930             return false;
931         }
932     }
934     function can_finalize($submission) {
935         global $USER;
936         if (!$this->drafts_tracked()) {
937             return false;
938         }
940         if ($this->is_finalized($submission)) {
941             return false;
942         }
944         if (has_capability('mod/assignment:grade', $this->context)) {
945             return true;
947         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
948           and $this->isopen()                                                 // assignment not closed yet
949           and !empty($submission)                                             // submission must exist
950           and $submission->userid == $USER->id                                // his/her own submission
951           and ($this->count_user_files($submission->id)
952             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
954             return true;
955         } else {
956             return false;
957         }
958     }
960     function can_update_notes($submission) {
961         global $USER;
963         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
964           and $this->notes_allowed()                                          // notesd must be allowed
965           and $this->isopen()                                                 // assignment not closed yet
966           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
967           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
968             return true;
969         } else {
970             return false;
971         }
972     }
974     function notes_allowed() {
975         return (boolean)$this->assignment->var2;
976     }
978     function count_responsefiles($userid) {
979         if ($submission = $this->get_submission($userid)) {
980             $fs = get_file_storage();
981             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
982             return count($files);
983         } else {
984             return 0;
985         }
986     }
988     function setup_elements(&$mform) {
989         global $CFG, $COURSE;
991         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
993         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
994         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
995         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
996         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
998         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
999         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1000         $mform->setDefault('resubmit', 1);
1002         $options = array();
1003         for($i = 1; $i <= 20; $i++) {
1004             $options[$i] = $i;
1005         }
1006         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1007         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1008         $mform->setDefault('var1', 3);
1010         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1011         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1012         $mform->setDefault('var2', 0);
1014         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1015         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1016         $mform->setDefault('var3', 0);
1018         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1019         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1020         $mform->setDefault('emailteachers', 0);
1022         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1023         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1024         $mform->setDefault('var4', 1);
1026         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
1027         plagiarism_get_form_elements_module($mform, $course_context);
1028     }
1030     function portfolio_exportable() {
1031         return true;
1032     }
1034     function extend_settings_navigation($node) {
1035         global $CFG, $USER, $OUTPUT;
1037         // get users submission if there is one
1038         $submission = $this->get_submission();
1039         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1040             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1041         } else {
1042             $editable = false;
1043         }
1045         // If the user has submitted something add a bit more stuff
1046         if ($submission) {
1047             // Add a view link to the settings nav
1048             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1049             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1050             if (!empty($submission->timemodified)) {
1051                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1052                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1053                 $submittednode->add_class('note');
1054                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1055                     $submittednode->add_class('early');
1056                 } else {
1057                     $submittednode->add_class('late');
1058                 }
1059             }
1060         }
1062         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1063         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1064             $fs = get_file_storage();
1065             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1066                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1067                     $filenode = $node->add(get_string('submission', 'assignment'));
1068                 } else {
1069                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1070                 }
1071                 foreach ($files as $file) {
1072                     $filename = $file->get_filename();
1073                     $mimetype = $file->get_mimetype();
1074                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1075                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1076                 }
1077             }
1078         }
1080         // Show a notes link if they are enabled
1081         if ($this->notes_allowed()) {
1082             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1083             $node->add(get_string('notes', 'assignment'), $link);
1084         }
1085     }
1087     /**
1088      * creates a zip of all assignment submissions and sends a zip to the browser
1089      */
1090     public function download_submissions() {
1091         global $CFG,$DB;
1092         require_once($CFG->libdir.'/filelib.php');
1093         $submissions = $this->get_submissions('','');
1094         if (empty($submissions)) {
1095             print_error('errornosubmissions', 'assignment');
1096         }
1097         $filesforzipping = array();
1098         $fs = get_file_storage();
1100         $groupmode = groups_get_activity_groupmode($this->cm);
1101         $groupid = 0;   // All users
1102         $groupname = '';
1103         if ($groupmode) {
1104             $groupid = groups_get_activity_group($this->cm, true);
1105             $groupname = groups_get_group_name($groupid).'-';
1106         }
1107         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1108         foreach ($submissions as $submission) {
1109             $a_userid = $submission->userid; //get userid
1110             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1111                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1112                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1114                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1115                 foreach ($files as $file) {
1116                     //get files new name.
1117                     $fileext = strstr($file->get_filename(), '.');
1118                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
1119                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1120                     //save file name to array for zipping.
1121                     $filesforzipping[$fileforzipname] = $file;
1122                 }
1123             }
1124         } // end of foreach loop
1125         if ($zipfile = assignment_pack_files($filesforzipping)) {
1126             send_temp_file($zipfile, $filename); //send file and delete after sending.
1127         }
1128     }
1131 class mod_assignment_upload_notes_form extends moodleform {
1133     function get_data() {
1134         $data = parent::get_data();
1135         if ($data) {
1136             $data->format = $data->text['format'];
1137             $data->text = $data->text['text'];
1138         }
1139         return $data;
1140     }
1142     function set_data($data) {
1143         if (!isset($data->format)) {
1144             $data->format = FORMAT_HTML;
1145         }
1146         if (isset($data->text)) {
1147             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1148         }
1149         parent::set_data($data);
1150     }
1152     function definition() {
1153         $mform = $this->_form;
1155         // visible elements
1156         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1157         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1159         // hidden params
1160         $mform->addElement('hidden', 'id', 0);
1161         $mform->setType('id', PARAM_INT);
1162         $mform->addElement('hidden', 'action', 'savenotes');
1163         $mform->setType('action', PARAM_ALPHA);
1165         // buttons
1166         $this->add_action_buttons();
1167     }
1170 class mod_assignment_upload_response_form extends moodleform {
1171     function definition() {
1172         $mform = $this->_form;
1173         $instance = $this->_customdata;
1175         // visible elements
1176         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1178         // hidden params
1179         $mform->addElement('hidden', 'id', $instance->cm->id);
1180         $mform->setType('id', PARAM_INT);
1181         $mform->addElement('hidden', 'contextid', $instance->contextid);
1182         $mform->setType('contextid', PARAM_INT);
1183         $mform->addElement('hidden', 'action', 'uploadresponse');
1184         $mform->setType('action', PARAM_ALPHA);
1185         $mform->addElement('hidden', 'mode', $instance->mode);
1186         $mform->setType('mode', PARAM_ALPHA);
1187         $mform->addElement('hidden', 'offset', $instance->offset);
1188         $mform->setType('offset', PARAM_INT);
1189         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1190         $mform->setType('forcerefresh', PARAM_INT);
1191         $mform->addElement('hidden', 'userid', $instance->userid);
1192         $mform->setType('userid', PARAM_INT);
1194         // buttons
1195         $this->add_action_buttons(false, get_string('uploadthisfile'));
1196     }