3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Assignment upload type implementation
21 * @package mod-assignment
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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
33 * Extend the base assignment class for assignments where you upload a single file
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';
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);
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();
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);
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);
76 echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
79 if ($filecount and $submission) {
80 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
82 if (!$this->isopen() or $this->is_finalized($submission)) {
83 echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
85 echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
89 $this->view_upload_form();
91 if ($this->notes_allowed()) {
92 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
96 $this->view_final_submission();
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);
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');
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
127 if ($grade->grade === null and empty($grade->str_feedback)) { /// Nothing to show yet
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');
139 /// Print the feedback
140 echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
142 echo '<table cellspacing="0" class="feedback">';
145 echo '<td class="left picture">';
146 echo $OUTPUT->user_picture($teacher);
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>';
157 echo '<td class="left side"> </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;
163 echo '<div class="clearer"></div>';
166 echo '<div class="comment">';
167 echo $grade->str_feedback;
172 echo '<td class="left side"> </td>';
173 echo '<td class="content">';
174 echo $this->print_responsefiles($USER->id, true);
181 function view_upload_form() {
182 global $CFG, $USER, $OUTPUT;
184 $submission = $this->get_submission($USER->id);
186 if ($this->is_finalized($submission)) {
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');
198 $str = get_string('uploadfiles', 'assignment');
201 $str = get_string('uploadfiles', 'assignment');
203 echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id)), $str, 'get');
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');
215 echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
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'));
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').'" />';
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);
250 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
259 * Return true if var3 == hide description till available day
263 function description_is_hidden() {
264 return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
267 function custom_feedbackform($submission, $return=false) {
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').': ';
282 $output = ob_get_clean();
285 $output .= $this->update_main_listing($submission);
288 $responsefiles = $this->print_responsefiles($submission->userid, true);
289 if (!empty($responsefiles)) {
290 $output .= $responsefiles;
301 function print_student_answer($userid, $return=false){
302 global $CFG, $OUTPUT, $PAGE;
304 $submission = $this->get_submission($userid);
308 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
309 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
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')));
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();
330 * Produces a list of links to the files uploaded by a user
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
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);
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 />';
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}&userid=$userid&offset=$offset&mode=single";
361 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
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'));
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();
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);
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();
416 * upload_file function requires moodle form instance and file manager options
417 * @param object $mform
418 * @param array $options
420 function upload($mform = null, $filemanager_options = null) {
421 $action = required_param('action', PARAM_ALPHA);
427 case 'finalizeclose':
428 $this->finalizeclose();
433 case 'uploadresponse':
434 $this->upload_responsefile();
437 $this->upload_file($mform, $filemanager_options);
440 $this->upload_notes();
442 print_error('unknowuploadaction', '', '', $action);
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);
461 $defaults->text = '';
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);
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();
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);
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();
501 /// show notes edit form
502 $this->view_header(get_string('notes', 'assignment'));
504 echo $OUTPUT->heading(get_string('notes', 'assignment'));
508 $this->view_footer();
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}&userid=$userid&mode=$mode&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);
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();
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();
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);
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;
583 $eventdata->files = $files;
585 events_trigger('assessable_file_uploaded', $eventdata);
587 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
588 redirect($returnurl);
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();
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))) {
611 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
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()) {
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))) {
631 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
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()) {
642 send_stored_file($file, 0, 0, true);
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
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();
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);
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();
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);
700 function finalizeclose() {
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
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);
725 redirect($returnurl);
728 function unfinalize() {
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}&userid=$userid&mode=$mode&offset=$offset&forcerefresh=1";
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);
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();
758 redirect($returnurl);
763 $action = optional_param('action', '', PARAM_ALPHA);
767 $this->delete_responsefile();
770 $this->delete_file();
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);
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();
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)) {
810 redirect($returnurl);
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);
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));
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));
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();
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());
847 $this->view_header(get_string('delete'));
849 $PAGE->set_title(get_string('delete'));
850 echo $OUTPUT->header();
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));
855 $this->view_footer();
857 echo $OUTPUT->footer();
862 $fs = get_file_storage();
863 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
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);
871 redirect($returnurl);
875 function can_upload_file($submission) {
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
889 function can_manage_responsefiles() {
890 if (has_capability('mod/assignment:grade', $this->context)) {
897 function can_delete_files($submission) {
900 if (has_capability('mod/assignment:grade', $this->context)) {
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
915 function drafts_tracked() {
916 return !empty($this->assignment->var4);
920 * Returns submission status
921 * @param object $submission - may be empty
922 * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
924 function is_finalized($submission) {
925 if (!$this->drafts_tracked()) {
928 } else if (empty($submission)) {
931 } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
932 return $submission->data2;
939 function can_unfinalize($submission) {
940 if (!$this->drafts_tracked()) {
943 if (has_capability('mod/assignment:grade', $this->context)
945 and $this->is_finalized($submission)) {
952 function can_finalize($submission) {
954 if (!$this->drafts_tracked()) {
958 if ($this->is_finalized($submission)) {
962 if (has_capability('mod/assignment:grade', $this->context)) {
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
978 function can_update_notes($submission) {
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
992 function notes_allowed() {
993 return (boolean)$this->assignment->var2;
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);
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);
1021 for($i = 1; $i <= 20; $i++) {
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);
1046 function portfolio_exportable() {
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);
1061 // If the user has submitted something add a bit more stuff
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 ', $submittednode->text);
1069 $submittednode->add_class('note');
1070 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1071 $submittednode->add_class('early');
1073 $submittednode->add_class('late');
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'));
1085 $filenode = $node->add(get_string('submissiondraft', 'assignment'));
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),''));
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);
1104 * creates a zip of all assignment submissions and sends a zip to the browser
1106 public function download_submissions() {
1108 require_once($CFG->libdir.'/filelib.php');
1109 $submissions = $this->get_submissions('','');
1110 if (empty($submissions)) {
1111 error("there are no submissions to download");
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
1121 $group = get_current_group($this->course->id, true);
1122 $groupid = $group->id;
1123 $groupname = $group->name.'-';
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;
1140 } // end of foreach loop
1141 if ($zipfile = assignment_pack_files($filesforzipping)) {
1142 send_temp_file($zipfile, $filename); //send file and delete after sending.
1147 class mod_assignment_upload_notes_form extends moodleform {
1149 function get_data() {
1150 $data = parent::get_data();
1152 $data->format = $data->text['format'];
1153 $data->text = $data->text['text'];
1158 function set_data($data) {
1159 if (!isset($data->format)) {
1160 $data->format = FORMAT_HTML;
1162 if (isset($data->text)) {
1163 $data->text = array('text'=>$data->text, 'format'=>$data->format);
1165 parent::set_data($data);
1168 function definition() {
1169 $mform = $this->_form;
1172 $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1173 $mform->setType('text', PARAM_RAW); // to be cleaned before display
1176 $mform->addElement('hidden', 'id', 0);
1177 $mform->setType('id', PARAM_INT);
1178 $mform->addElement('hidden', 'action', 'savenotes');
1179 $mform->setType('action', PARAM_ALPHA);
1182 $this->add_action_buttons();
1186 class mod_assignment_upload_response_form extends moodleform {
1187 function definition() {
1188 $mform = $this->_form;
1189 $instance = $this->_customdata;
1192 $mform->addElement('file', 'newfile', get_string('uploadafile'));
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);
1209 $this->add_action_buttons(false, get_string('uploadthisfile'));