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)) {
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 if (has_capability('mod/assignment:submit', $this->context)) {
90 $this->view_upload_form();
91 debugging('yes mod/assignment:submit');
93 debugging('no mod/assignment:submit');
96 if ($this->notes_allowed()) {
97 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
101 $this->view_final_submission();
103 $this->view_footer();
107 function view_feedback($submission=NULL) {
108 global $USER, $CFG, $DB, $OUTPUT;
109 require_once($CFG->libdir.'/gradelib.php');
111 if (!$submission) { /// Get submission for this assignment
112 $submission = $this->get_submission($USER->id);
115 if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
116 if ($this->count_responsefiles($USER->id)) {
117 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
118 $responsefiles = $this->print_responsefiles($USER->id, true);
119 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
124 $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
125 $item = $grading_info->items[0];
126 $grade = $item->grades[$USER->id];
128 if ($grade->hidden or $grade->grade === false) { // hidden or error
132 if ($grade->grade === null and empty($grade->str_feedback)) { /// Nothing to show yet
136 $graded_date = $grade->dategraded;
137 $graded_by = $grade->usermodified;
139 /// We need the teacher info
140 if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
141 print_error('cannotfindteacher');
144 /// Print the feedback
145 echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
147 echo '<table cellspacing="0" class="feedback">';
150 echo '<td class="left picture">';
151 echo $OUTPUT->user_picture($teacher);
153 echo '<td class="topic">';
154 echo '<div class="from">';
155 echo '<div class="fullname">'.fullname($teacher).'</div>';
156 echo '<div class="time">'.userdate($graded_date).'</div>';
162 echo '<td class="left side"> </td>';
163 echo '<td class="content">';
164 if ($this->assignment->grade) {
165 echo '<div class="grade">';
166 echo get_string("grade").': '.$grade->str_long_grade;
168 echo '<div class="clearer"></div>';
171 echo '<div class="comment">';
172 echo $grade->str_feedback;
177 echo '<td class="left side"> </td>';
178 echo '<td class="content">';
179 echo $this->print_responsefiles($USER->id, true);
186 function view_upload_form() {
187 global $CFG, $USER, $OUTPUT;
189 $submission = $this->get_submission($USER->id);
191 if ($this->is_finalized($submission)) {
193 debugging('finalized');
197 if ($this->can_upload_file($submission)) {
198 debugging('can upload');
199 $fs = get_file_storage();
200 // edit files in another page
202 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
203 $str = get_string('editthesefiles', 'assignment');
205 $str = get_string('uploadfiles', 'assignment');
208 $str = get_string('uploadfiles', 'assignment');
210 echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
212 debugging('cant upload');
216 function view_notes() {
217 global $USER, $OUTPUT;
219 if ($submission = $this->get_submission($USER->id)
220 and !empty($submission->data1)) {
221 echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML), 'generalbox boxaligncenter boxwidthwide');
223 echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
225 if ($this->can_update_notes($submission)) {
226 $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
227 echo '<div style="text-align:center">';
228 echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
233 function view_final_submission() {
234 global $CFG, $USER, $OUTPUT;
236 $submission = $this->get_submission($USER->id);
238 if ($this->isopen() and $this->can_finalize($submission)) {
239 //print final submit button
240 echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
241 echo '<div style="text-align:center">';
242 echo '<form method="post" action="upload.php">';
243 echo '<fieldset class="invisiblefieldset">';
244 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
245 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
246 echo '<input type="hidden" name="action" value="finalize" />';
247 echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
251 } else if (!$this->isopen()) {
252 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
254 } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
255 if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
256 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
258 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
267 * Return true if var3 == hide description till available day
271 function description_is_hidden() {
272 return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
275 function print_student_answer($userid, $return=false){
276 global $CFG, $OUTPUT, $PAGE;
278 $submission = $this->get_submission($userid);
282 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
283 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
286 if ($this->notes_allowed() and !empty($submission->data1)) {
287 $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
288 $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
289 $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
295 $renderer = $PAGE->get_renderer('mod_assignment');
296 $output = $OUTPUT->box_start('files').$output;
297 $output .= $renderer->assignment_files($this->context, $submission->id);
298 $output .= $OUTPUT->box_end();
305 * Produces a list of links to the files uploaded by a user
307 * @param $userid int optional id of the user. If 0 then $USER->id is used.
308 * @param $return boolean optional defaults to false. If true the list is returned rather than printed
309 * @return string optional
311 function print_user_files($userid=0, $return=false) {
312 global $CFG, $USER, $OUTPUT, $PAGE;
314 $mode = optional_param('mode', '', PARAM_ALPHA);
315 $offset = optional_param('offset', 0, PARAM_INT);
324 $output = $OUTPUT->box_start('files');
326 $submission = $this->get_submission($userid);
328 // only during grading
329 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
330 $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
333 if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
335 $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&userid=$userid&offset=$offset&mode=single";
336 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
340 if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
341 if ($this->can_unfinalize($submission)) {
342 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
343 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
344 $output .= $OUTPUT->help_icon('unfinalize', 'assignment');
346 } else if ($this->can_finalize($submission)) {
347 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
348 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
353 $renderer = $PAGE->get_renderer('mod_assignment');
354 $output .= $renderer->assignment_files($this->context, $submission->id);
356 $output .= $OUTPUT->box_end();
364 function submissions($mode) {
365 // redirects out of form to process (un)finalizing.
366 $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
367 $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
369 $this->unfinalize('single');
370 } else if ($finalize) {
371 $this->finalize('single');
373 if ($unfinalize || $finalize) {
374 $mode = 'singlenosave';
376 parent::submissions($mode);
379 function process_feedback() {
380 if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
383 $userid = required_param('userid', PARAM_INT);
384 $offset = required_param('offset', PARAM_INT);
385 $mform = $this->display_submission($offset, $userid, false);
386 parent::process_feedback($mform);
389 function print_responsefiles($userid, $return=false) {
390 global $CFG, $USER, $OUTPUT, $PAGE;
392 $mode = optional_param('mode', '', PARAM_ALPHA);
393 $offset = optional_param('offset', 0, PARAM_INT);
395 $output = $OUTPUT->box_start('responsefiles');
397 $candelete = $this->can_manage_responsefiles();
398 $strdelete = get_string('delete');
400 $fs = get_file_storage();
401 $browser = get_file_browser();
403 if ($submission = $this->get_submission($userid)) {
404 $renderer = $PAGE->get_renderer('mod_assignment');
405 $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
407 $output .= $OUTPUT->box_end();
418 * upload_file function requires moodle form instance and file manager options
419 * @param object $mform
420 * @param array $options
422 function upload($mform = null, $filemanager_options = null) {
423 $action = required_param('action', PARAM_ALPHA);
428 case 'finalizeclose':
429 $this->finalizeclose();
434 case 'uploadresponse':
435 $this->upload_responsefile($mform, $filemanager_options);
438 $this->upload_file($mform, $filemanager_options);
441 $this->upload_notes();
443 print_error('unknowuploadaction', '', '', $action);
447 function upload_notes() {
448 global $CFG, $USER, $OUTPUT, $DB;
450 $action = required_param('action', PARAM_ALPHA);
452 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
454 $mform = new mod_assignment_upload_notes_form();
456 $defaults = new stdClass();
457 $defaults->id = $this->cm->id;
459 if ($submission = $this->get_submission($USER->id)) {
460 $defaults->text = clean_text($submission->data1);
462 $defaults->text = '';
465 $mform->set_data($defaults);
467 if ($mform->is_cancelled()) {
468 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
469 redirect($returnurl);
472 if (!$this->can_update_notes($submission)) {
473 $this->view_header(get_string('upload'));
474 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
475 echo $OUTPUT->continue_button($returnurl);
476 $this->view_footer();
480 if ($data = $mform->get_data() and $action == 'savenotes') {
481 $submission = $this->get_submission($USER->id, true); // get or create submission
482 $updated = new stdClass();
483 $updated->id = $submission->id;
484 $updated->timemodified = time();
485 $updated->data1 = $data->text;
487 $DB->update_record('assignment_submissions', $updated);
488 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
489 redirect($returnurl);
490 $submission = $this->get_submission($USER->id);
491 $this->update_grade($submission);
494 /// show notes edit form
495 $this->view_header(get_string('notes', 'assignment'));
497 echo $OUTPUT->heading(get_string('notes', 'assignment'));
501 $this->view_footer();
505 function upload_responsefile($mform, $options) {
506 global $CFG, $USER, $OUTPUT, $PAGE;
508 $userid = required_param('userid', PARAM_INT);
509 $mode = required_param('mode', PARAM_ALPHA);
510 $offset = required_param('offset', PARAM_INT);
512 $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
514 if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
515 $fs = get_file_storage();
516 $submission = $this->get_submission($userid, true, true);
517 if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
518 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
519 redirect($returnurl->out(false));
522 $PAGE->set_title(get_string('upload'));
523 echo $OUTPUT->header();
524 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
525 echo $OUTPUT->continue_button($returnurl->out(true));
526 echo $OUTPUT->footer();
530 function upload_file($mform, $options) {
531 global $CFG, $USER, $DB, $OUTPUT;
533 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
534 $submission = $this->get_submission($USER->id);
535 $filecount = $this->count_user_files($submission->id);
537 if (!$this->can_upload_file($submission)) {
538 $this->view_header(get_string('upload'));
539 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
540 echo $OUTPUT->continue_button($returnurl);
541 $this->view_footer();
545 if ($formdata = $mform->get_data()) {
546 $fs = get_file_storage();
547 $submission = $this->get_submission($USER->id, true); //create new submission if needed
548 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
549 $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
550 $updates = new stdClass();
551 $updates->id = $submission->id;
552 $updates->timemodified = time();
553 $DB->update_record('assignment_submissions', $updates);
554 add_to_log($this->course->id, 'assignment', 'upload',
555 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
556 $this->update_grade($submission);
557 if (!$this->drafts_tracked()) {
558 $this->email_teachers($submission);
561 // send files to event system
562 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
563 // Let Moodle know that assessable files were uploaded (eg for plagiarism detection)
564 $eventdata = new stdClass();
565 $eventdata->modulename = 'assignment';
566 $eventdata->cmid = $this->cm->id;
567 $eventdata->itemid = $submission->id;
568 $eventdata->courseid = $this->course->id;
569 $eventdata->userid = $USER->id;
571 $eventdata->files = $files;
573 events_trigger('assessable_file_uploaded', $eventdata);
574 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
575 redirect($returnurl);
578 $this->view_header(get_string('upload'));
579 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
580 echo $OUTPUT->continue_button($returnurl);
581 $this->view_footer();
585 function send_file($filearea, $args) {
586 global $CFG, $DB, $USER;
587 require_once($CFG->libdir.'/filelib.php');
589 require_login($this->course, false, $this->cm);
591 if ($filearea === 'submission') {
592 $submissionid = (int)array_shift($args);
594 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
598 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
602 $relativepath = implode('/', $args);
603 $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
605 $fs = get_file_storage();
606 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
609 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
611 } else if ($filearea === 'response') {
612 $submissionid = (int)array_shift($args);
614 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
618 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
622 $relativepath = implode('/', $args);
623 $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
625 $fs = get_file_storage();
626 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
629 send_stored_file($file, 0, 0, true);
635 function finalize($forcemode=null) {
636 global $USER, $DB, $OUTPUT;
637 $userid = optional_param('userid', $USER->id, PARAM_INT);
638 $offset = optional_param('offset', 0, PARAM_INT);
639 $confirm = optional_param('confirm', 0, PARAM_BOOL);
640 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
641 $submission = $this->get_submission($userid);
643 if ($forcemode!=null) {
644 $returnurl = new moodle_url('/mod/assignment/submissions.php',
645 array('id'=>$this->cm->id,
652 if (!$this->can_finalize($submission)) {
653 redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
656 if ($forcemode==null) {
657 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
658 $optionsno = array('id'=>$this->cm->id);
659 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
660 $this->view_header(get_string('submitformarking', 'assignment'));
661 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
662 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
663 $this->view_footer();
667 $updated = new stdClass();
668 $updated->id = $submission->id;
669 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
670 $updated->timemodified = time();
672 $DB->update_record('assignment_submissions', $updated);
673 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
674 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
675 $submission = $this->get_submission($userid);
676 $this->update_grade($submission);
677 $this->email_teachers($submission);
679 // Trigger assessable_files_done event to show files are complete
680 $eventdata = new stdClass();
681 $eventdata->modulename = 'assignment';
682 $eventdata->cmid = $this->cm->id;
683 $eventdata->itemid = $submission->id;
684 $eventdata->courseid = $this->course->id;
685 $eventdata->userid = $userid;
686 events_trigger('assessable_files_done', $eventdata);
688 if ($forcemode==null) {
689 redirect($returnurl->out(false));
693 function finalizeclose() {
696 $userid = optional_param('userid', 0, PARAM_INT);
697 $mode = required_param('mode', PARAM_ALPHA);
698 $offset = required_param('offset', PARAM_INT);
699 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
701 // create but do not add student submission date
702 $submission = $this->get_submission($userid, true, true);
704 if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
705 redirect($returnurl); // probably closed already
708 $updated = new stdClass();
709 $updated->id = $submission->id;
710 $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
712 $DB->update_record('assignment_submissions', $updated);
713 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
714 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
715 $submission = $this->get_submission($userid, false, true);
716 $this->update_grade($submission);
717 redirect($returnurl);
720 function unfinalize($forcemode=null) {
723 $userid = required_param('userid', PARAM_INT);
724 $mode = required_param('mode', PARAM_ALPHA);
725 $offset = required_param('offset', PARAM_INT);
727 if ($forcemode!=null) {
730 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
732 and $submission = $this->get_submission($userid)
733 and $this->can_unfinalize($submission)
734 and confirm_sesskey()) {
736 $updated = new stdClass();
737 $updated->id = $submission->id;
738 $updated->data2 = '';
739 $DB->update_record('assignment_submissions', $updated);
740 //TODO: add unfinalize action to log
741 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
742 $submission = $this->get_submission($userid);
743 $this->update_grade($submission);
746 if ($forcemode==null) {
747 redirect($returnurl);
753 $action = optional_param('action', '', PARAM_ALPHA);
757 $this->delete_responsefile();
760 $this->delete_file();
766 function delete_responsefile() {
767 global $CFG, $OUTPUT,$PAGE;
769 $file = required_param('file', PARAM_FILE);
770 $userid = required_param('userid', PARAM_INT);
771 $mode = required_param('mode', PARAM_ALPHA);
772 $offset = required_param('offset', PARAM_INT);
773 $confirm = optional_param('confirm', 0, PARAM_BOOL);
775 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
777 if (!$this->can_manage_responsefiles()) {
778 redirect($returnurl);
781 $urlreturn = 'submissions.php';
782 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
784 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
785 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
786 $PAGE->set_title(get_string('delete'));
787 echo $OUTPUT->header();
788 echo $OUTPUT->heading(get_string('delete'));
789 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
790 echo $OUTPUT->footer();
794 if ($submission = $this->get_submission($userid)) {
795 $fs = get_file_storage();
796 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
800 redirect($returnurl);
804 function delete_file() {
805 global $CFG, $DB, $OUTPUT, $PAGE;
807 $file = required_param('file', PARAM_FILE);
808 $userid = required_param('userid', PARAM_INT);
809 $confirm = optional_param('confirm', 0, PARAM_BOOL);
810 $mode = optional_param('mode', '', PARAM_ALPHA);
811 $offset = optional_param('offset', 0, PARAM_INT);
813 require_login($this->course->id, false, $this->cm);
816 $urlreturn = 'view.php';
817 $optionsreturn = array('id'=>$this->cm->id);
818 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
820 $urlreturn = 'submissions.php';
821 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
822 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
825 if (!$submission = $this->get_submission($userid) // incorrect submission
826 or !$this->can_delete_files($submission)) { // can not delete
827 $this->view_header(get_string('delete'));
828 echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
829 echo $OUTPUT->continue_button($returnurl);
830 $this->view_footer();
834 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
835 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
837 $this->view_header(get_string('delete'));
839 $PAGE->set_title(get_string('delete'));
840 echo $OUTPUT->header();
842 echo $OUTPUT->heading(get_string('delete'));
843 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
845 $this->view_footer();
847 echo $OUTPUT->footer();
852 $fs = get_file_storage();
853 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
855 $submission->timemodified = time();
856 $DB->update_record('assignment_submissions', $submission);
857 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
858 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
859 $this->update_grade($submission);
861 redirect($returnurl);
865 function can_upload_file($submission) {
868 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
869 and $this->isopen() // assignment not closed yet
870 and (empty($submission) or ($submission->userid == $USER->id)) // his/her own submission
871 and !$this->is_finalized($submission)) { // no uploading after final submission
878 function can_manage_responsefiles() {
879 if (has_capability('mod/assignment:grade', $this->context)) {
886 function can_delete_files($submission) {
889 if (has_capability('mod/assignment:grade', $this->context)) {
893 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
894 and $this->isopen() // assignment not closed yet
895 and $this->assignment->resubmit // deleting allowed
896 and $USER->id == $submission->userid // his/her own submission
897 and !$this->is_finalized($submission)) { // no deleting after final submission
904 function drafts_tracked() {
905 return !empty($this->assignment->var4);
909 * Returns submission status
910 * @param object $submission - may be empty
911 * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
913 function is_finalized($submission) {
914 if (!$this->drafts_tracked()) {
917 } else if (empty($submission)) {
920 } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
921 return $submission->data2;
928 function can_unfinalize($submission) {
929 if (!$this->drafts_tracked()) {
932 if (has_capability('mod/assignment:grade', $this->context)
934 and $this->is_finalized($submission)) {
941 function can_finalize($submission) {
943 if (!$this->drafts_tracked()) {
947 if ($this->is_finalized($submission)) {
951 if (has_capability('mod/assignment:grade', $this->context)) {
954 } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
955 and $this->isopen() // assignment not closed yet
956 and !empty($submission) // submission must exist
957 and $submission->userid == $USER->id // his/her own submission
958 and ($this->count_user_files($submission->id)
959 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
967 function can_update_notes($submission) {
970 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
971 and $this->notes_allowed() // notesd must be allowed
972 and $this->isopen() // assignment not closed yet
973 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
974 and !$this->is_finalized($submission)) { // no updateingafter final submission
981 function notes_allowed() {
982 return (boolean)$this->assignment->var2;
985 function count_responsefiles($userid) {
986 if ($submission = $this->get_submission($userid)) {
987 $fs = get_file_storage();
988 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
989 return count($files);
995 function setup_elements(&$mform) {
996 global $CFG, $COURSE;
998 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1000 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1001 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1002 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1003 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1005 $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1006 $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1007 $mform->setDefault('resubmit', 1);
1010 for($i = 1; $i <= 20; $i++) {
1013 $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1014 $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1015 $mform->setDefault('var1', 3);
1017 $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1018 $mform->addHelpButton('var2', 'allownotes', 'assignment');
1019 $mform->setDefault('var2', 0);
1021 $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1022 $mform->addHelpButton('var3', 'hideintro', 'assignment');
1023 $mform->setDefault('var3', 0);
1025 $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1026 $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1027 $mform->setDefault('emailteachers', 0);
1029 $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1030 $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1031 $mform->setDefault('var4', 1);
1033 $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
1034 plagiarism_get_form_elements_module($mform, $course_context);
1037 function portfolio_exportable() {
1041 function extend_settings_navigation($node) {
1042 global $CFG, $USER, $OUTPUT;
1044 // get users submission if there is one
1045 $submission = $this->get_submission();
1046 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1047 $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1052 // If the user has submitted something add a bit more stuff
1054 // Add a view link to the settings nav
1055 $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1056 $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1057 if (!empty($submission->timemodified)) {
1058 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1059 $submittednode->text = preg_replace('#([^,])\s#', '$1 ', $submittednode->text);
1060 $submittednode->add_class('note');
1061 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1062 $submittednode->add_class('early');
1064 $submittednode->add_class('late');
1069 // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1070 if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1071 $fs = get_file_storage();
1072 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1073 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1074 $filenode = $node->add(get_string('submission', 'assignment'));
1076 $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1078 foreach ($files as $file) {
1079 $filename = $file->get_filename();
1080 $mimetype = $file->get_mimetype();
1081 $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1082 $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
1087 // Show a notes link if they are enabled
1088 if ($this->notes_allowed()) {
1089 $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1090 $node->add(get_string('notes', 'assignment'), $link);
1095 * creates a zip of all assignment submissions and sends a zip to the browser
1097 public function download_submissions() {
1099 require_once($CFG->libdir.'/filelib.php');
1100 $submissions = $this->get_submissions('','');
1101 if (empty($submissions)) {
1102 print_error('errornosubmissions', 'assignment');
1104 $filesforzipping = array();
1105 $fs = get_file_storage();
1107 if (isset($this->cm->groupmode) && empty($this->course->groupmodeforce)) {
1108 $groupmode = $this->cm->groupmode;
1110 $groupmode = $this->course->groupmode;
1113 $groupid = 0; // All users
1116 $group = get_current_group($this->course->id, true);
1117 $groupid = $group->id;
1118 $groupname = $group->name.'-';
1120 $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1121 foreach ($submissions as $submission) {
1122 $a_userid = $submission->userid; //get userid
1123 if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1124 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1125 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1127 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1128 foreach ($files as $file) {
1129 //get files new name.
1130 $fileext = strstr($file->get_filename(), '.');
1131 $fileoriginal = str_replace($fileext, '', $file->get_filename());
1132 $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1133 //save file name to array for zipping.
1134 $filesforzipping[$fileforzipname] = $file;
1137 } // end of foreach loop
1138 if ($zipfile = assignment_pack_files($filesforzipping)) {
1139 send_temp_file($zipfile, $filename); //send file and delete after sending.
1144 class mod_assignment_upload_notes_form extends moodleform {
1146 function get_data() {
1147 $data = parent::get_data();
1149 $data->format = $data->text['format'];
1150 $data->text = $data->text['text'];
1155 function set_data($data) {
1156 if (!isset($data->format)) {
1157 $data->format = FORMAT_HTML;
1159 if (isset($data->text)) {
1160 $data->text = array('text'=>$data->text, 'format'=>$data->format);
1162 parent::set_data($data);
1165 function definition() {
1166 $mform = $this->_form;
1169 $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1170 $mform->setType('text', PARAM_RAW); // to be cleaned before display
1173 $mform->addElement('hidden', 'id', 0);
1174 $mform->setType('id', PARAM_INT);
1175 $mform->addElement('hidden', 'action', 'savenotes');
1176 $mform->setType('action', PARAM_ALPHA);
1179 $this->add_action_buttons();
1183 class mod_assignment_upload_response_form extends moodleform {
1184 function definition() {
1185 $mform = $this->_form;
1186 $instance = $this->_customdata;
1189 $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1192 $mform->addElement('hidden', 'id', $instance->cm->id);
1193 $mform->setType('id', PARAM_INT);
1194 $mform->addElement('hidden', 'contextid', $instance->contextid);
1195 $mform->setType('contextid', PARAM_INT);
1196 $mform->addElement('hidden', 'action', 'uploadresponse');
1197 $mform->setType('action', PARAM_ALPHA);
1198 $mform->addElement('hidden', 'mode', $instance->mode);
1199 $mform->setType('mode', PARAM_ALPHA);
1200 $mform->addElement('hidden', 'offset', $instance->offset);
1201 $mform->setType('offset', PARAM_INT);
1202 $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1203 $mform->setType('forcerefresh', PARAM_INT);
1204 $mform->addElement('hidden', 'userid', $instance->userid);
1205 $mform->setType('userid', PARAM_INT);
1208 $this->add_action_buttons(false, get_string('uploadthisfile'));