MDL-34466 mod_assignment Replace all instances of get_context_instance() with respect...
[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);
47         $cansubmit = has_capability('mod/assignment:submit', $this->context);
49         add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
51         $this->view_header();
53         if ($this->assignment->timeavailable > time()
54           and !has_capability('mod/assignment:grade', $this->context)      // grading user can see it anytime
55           and $this->assignment->var3) {                                   // force hiding before available date
56             echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
57             print_string('notavailableyet', 'assignment');
58             echo $OUTPUT->box_end();
59         } else {
60             $this->view_intro();
61         }
63         $this->view_dates();
65         if (is_enrolled($this->context, $USER)) {
66             if ($submission = $this->get_submission($USER->id)) {
67                 $filecount = $this->count_user_files($submission->id);
68             } else {
69                 $filecount = 0;
70             }
71             if ($cansubmit or !empty($filecount)) { //if a user has submitted files using a previous role we should still show the files
72                 $this->view_feedback();
74                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
75                     echo $OUTPUT->heading(get_string('submission', 'assignment'), 3);
76                 } else {
77                     echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
78                 }
80                 if ($filecount and $submission) {
81                     echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
82                 } else {
83                     if (!$this->isopen() or $this->is_finalized($submission)) {
84                         echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
85                     } else {
86                         echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
87                     }
88                 }
90                 $this->view_upload_form();
92                 if ($this->notes_allowed()) {
93                     echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
94                     $this->view_notes();
95                 }
97                 $this->view_final_submission();
98             }
99         }
100         $this->view_footer();
101     }
104     function view_feedback($submission=NULL) {
105         global $USER, $CFG, $DB, $OUTPUT, $PAGE;
106         require_once($CFG->libdir.'/gradelib.php');
107         require_once("$CFG->dirroot/grade/grading/lib.php");
109         if (!$submission) { /// Get submission for this assignment
110             $userid = $USER->id;
111             $submission = $this->get_submission($userid);
112         } else {
113             $userid = $submission->userid;
114         }
116         // Check the user can submit
117         $canviewfeedback = ($userid == $USER->id && has_capability('mod/assignment:submit', $this->context, $USER->id, false));
118         // If not then check if the user still has the view cap and has a previous submission
119         $canviewfeedback = $canviewfeedback || (!empty($submission) && $submission->userid == $USER->id && has_capability('mod/assignment:view', $this->context));
120         // Or if user can grade (is a teacher or admin)
121         $canviewfeedback = $canviewfeedback || has_capability('mod/assignment:grade', $this->context);
123         if (!$canviewfeedback) {
124             // can not view or submit assignments -> no feedback
125             return;
126         }
128         $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $userid);
129         $item = $grading_info->items[0];
130         $grade = $item->grades[$userid];
132         if ($grade->hidden or $grade->grade === false) { // hidden or error
133             return;
134         }
136         if ($grade->grade === null and empty($grade->str_feedback)) {   // No grade to show yet
137             if ($this->count_responsefiles($userid)) {   // but possibly response files are present
138                 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
139                 $responsefiles = $this->print_responsefiles($userid, true);
140                 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
141             }
142             return;
143         }
145         $graded_date = $grade->dategraded;
146         $graded_by   = $grade->usermodified;
148     /// We need the teacher info
149         if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
150             print_error('cannotfindteacher');
151         }
153     /// Print the feedback
154         echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
156         echo '<table cellspacing="0" class="feedback">';
158         echo '<tr>';
159         echo '<td class="left picture">';
160         echo $OUTPUT->user_picture($teacher);
161         echo '</td>';
162         echo '<td class="topic">';
163         echo '<div class="from">';
164         echo '<div class="fullname">'.fullname($teacher).'</div>';
165         echo '<div class="time">'.userdate($graded_date).'</div>';
166         echo '</div>';
167         echo '</td>';
168         echo '</tr>';
170         echo '<tr>';
171         echo '<td class="left side">&nbsp;</td>';
172         echo '<td class="content">';
173         $gradestr = '<div class="grade">'. get_string("grade").': '.$grade->str_long_grade. '</div>';
174         if (!empty($submission) && $controller = get_grading_manager($this->context, 'mod_assignment', 'submission')->get_active_controller()) {
175             $controller->set_grade_range(make_grades_menu($this->assignment->grade));
176             echo $controller->render_grade($PAGE, $submission->id, $item, $gradestr, has_capability('mod/assignment:grade', $this->context));
177         } else {
178             echo $gradestr;
179         }
180         echo '<div class="clearer"></div>';
182         echo '<div class="comment">';
183         echo $grade->str_feedback;
184         echo '</div>';
185         echo '</tr>';
187         echo '<tr>';
188         echo '<td class="left side">&nbsp;</td>';
189         echo '<td class="content">';
190         echo $this->print_responsefiles($userid, true);
191         echo '</tr>';
193         echo '</table>';
194     }
197     function view_upload_form() {
198         global $CFG, $USER, $OUTPUT;
200         $submission = $this->get_submission($USER->id);
202         if ($this->is_finalized($submission)) {
203             // no uploading
204             return;
205         }
207         if ($this->can_upload_file($submission)) {
208             $fs = get_file_storage();
209             // edit files in another page
210             if ($submission) {
211                 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
212                     $str = get_string('editthesefiles', 'assignment');
213                 } else {
214                     $str = get_string('uploadfiles', 'assignment');
215                 }
216             } else {
217                 $str = get_string('uploadfiles', 'assignment');
218             }
219             echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
220         }
221     }
223     function view_notes() {
224         global $USER, $OUTPUT;
226         if ($submission = $this->get_submission($USER->id)
227           and !empty($submission->data1)) {
228             echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML, array('overflowdiv'=>true)), 'generalbox boxaligncenter boxwidthwide');
229         } else {
230             echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
231         }
232         if ($this->can_update_notes($submission)) {
233             $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
234             echo '<div style="text-align:center">';
235             echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
236             echo '</div>';
237         }
238     }
240     function view_final_submission() {
241         global $CFG, $USER, $OUTPUT;
243         $submission = $this->get_submission($USER->id);
245         if ($this->isopen() and $this->can_finalize($submission)) {
246             //print final submit button
247             echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
248             echo '<div style="text-align:center">';
249             echo '<form method="post" action="upload.php">';
250             echo '<fieldset class="invisiblefieldset">';
251             echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
252             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
253             echo '<input type="hidden" name="action" value="finalize" />';
254             echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
255             echo '</fieldset>';
256             echo '</form>';
257             echo '</div>';
258         } else if (!$this->isopen()) {
259             echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
261         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
262             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
263                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
264             } else {
265                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
266             }
267         } else {
268             //no submission yet
269         }
270     }
273     /**
274      * Return true if var3 == hide description till available day
275      *
276      *@return boolean
277      */
278     function description_is_hidden() {
279         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
280     }
282     function print_student_answer($userid, $return=false){
283         global $CFG, $OUTPUT, $PAGE;
285         $submission = $this->get_submission($userid);
287         $output = '';
289         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
290             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
291         }
293         if ($this->notes_allowed() and !empty($submission->data1)) {
294             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
295             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
296             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
298             $output .= '&nbsp;';
299         }
302         $renderer = $PAGE->get_renderer('mod_assignment');
303         $output = $OUTPUT->box_start('files').$output;
304         $output .= $renderer->assignment_files($this->context, $submission->id);
305         $output .= $OUTPUT->box_end();
307         return $output;
308     }
311     /**
312      * Produces a list of links to the files uploaded by a user
313      *
314      * @param $userid int optional id of the user. If 0 then $USER->id is used.
315      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
316      * @return string optional
317      */
318     function print_user_files($userid=0, $return=false) {
319         global $CFG, $USER, $OUTPUT, $PAGE;
321         $mode    = optional_param('mode', '', PARAM_ALPHA);
322         $offset  = optional_param('offset', 0, PARAM_INT);
324         if (!$userid) {
325             if (!isloggedin()) {
326                 return '';
327             }
328             $userid = $USER->id;
329         }
331         $output = $OUTPUT->box_start('files');
333         $submission = $this->get_submission($userid);
335         // only during grading
336         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
337             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
338         }
340         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
342             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
343             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
345         }
347         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
348             if ($this->can_unfinalize($submission)) {
349                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
350                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
351                 $output .=  $OUTPUT->help_icon('unfinalize', 'assignment');
353             } else if ($this->can_finalize($submission)) {
354                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
355                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
356             }
357         }
359         if ($submission) {
360             $renderer = $PAGE->get_renderer('mod_assignment');
361             $output .= $renderer->assignment_files($this->context, $submission->id);
362         }
363         $output .= $OUTPUT->box_end();
365         if ($return) {
366             return $output;
367         }
368         echo $output;
369     }
371     function submissions($mode) {
372         // redirects out of form to process (un)finalizing.
373         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
374         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
375         if ($unfinalize) {
376             $this->unfinalize('single');
377         } else if ($finalize) {
378             $this->finalize('single');
379         }
380         if ($unfinalize || $finalize) {
381             $mode = 'singlenosave';
382         }
383         parent::submissions($mode);
384     }
386     function process_feedback($formdata=null) {
387         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
388             return false;
389         }
390         $userid = required_param('userid', PARAM_INT);
391         $offset = required_param('offset', PARAM_INT);
392         $mform = $this->display_submission($offset, $userid, false);
393         parent::process_feedback($mform);
394     }
396     /**
397      * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
398      * This is necessary for tracked advanced file uploads where we need to check that the data2 field is equal to ASSIGNMENT_STATUS_SUBMITTED
399      * to determine if a submission is complete.
400      *
401      * @param  int $groupid (optional) If nonzero then count is restricted to this group
402      * @return int          The number of submissions
403      */
404     function count_real_submissions($groupid=0) {
405         global $DB;
407         // Grab the context assocated with our course module
408         $context = context_module::instance($this->cm->id);
410         // Get ids of users enrolled in the given course.
411         list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
412         $params['assignmentid'] = $this->cm->instance;
414         $query = '';
415         if ($this->drafts_tracked() and $this->isopen()) {
416             $query = ' AND ' . $DB->sql_compare_text('s.data2') . " = '"  . ASSIGNMENT_STATUS_SUBMITTED . "'";
417         } else {
418             // Count on submissions with files actually uploaded
419             $query = " AND s.numfiles > 0";
420         }
421         return $DB->count_records_sql("SELECT COUNT('x')
422                                          FROM {assignment_submissions} s
423                                     LEFT JOIN {assignment} a ON a.id = s.assignment
424                                    INNER JOIN ($enroledsql) u ON u.id = s.userid
425                                         WHERE s.assignment = :assignmentid" .
426                                               $query, $params);
427     }
429     function print_responsefiles($userid, $return=false) {
430         global $CFG, $USER, $OUTPUT, $PAGE;
432         $mode    = optional_param('mode', '', PARAM_ALPHA);
433         $offset  = optional_param('offset', 0, PARAM_INT);
435         $output = $OUTPUT->box_start('responsefiles');
437         $candelete = $this->can_manage_responsefiles();
438         $strdelete   = get_string('delete');
440         $fs = get_file_storage();
441         $browser = get_file_browser();
443         if ($submission = $this->get_submission($userid)) {
444             $renderer = $PAGE->get_renderer('mod_assignment');
445             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
446         }
447         $output .= $OUTPUT->box_end();
449         if ($return) {
450             return $output;
451         }
452         echo $output;
453     }
455     /**
456      * Upload files
457      * upload_file function requires moodle form instance and file manager options
458      * @param object $mform
459      * @param array $options
460      */
461     function upload($mform = null, $filemanager_options = null) {
462         $action = required_param('action', PARAM_ALPHA);
463         switch ($action) {
464             case 'finalize':
465                 $this->finalize();
466                 break;
467             case 'finalizeclose':
468                 $this->finalizeclose();
469                 break;
470             case 'unfinalize':
471                 $this->unfinalize();
472                 break;
473             case 'uploadresponse':
474                 $this->upload_responsefile($mform, $filemanager_options);
475                 break;
476             case 'uploadfile':
477                 $this->upload_file($mform, $filemanager_options);
478             case 'savenotes':
479             case 'editnotes':
480                 $this->upload_notes();
481             default:
482                 print_error('unknowuploadaction', '', '', $action);
483         }
484     }
486     function upload_notes() {
487         global $CFG, $USER, $OUTPUT, $DB;
489         $action = required_param('action', PARAM_ALPHA);
491         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
493         $mform = new mod_assignment_upload_notes_form();
495         $defaults = new stdClass();
496         $defaults->id = $this->cm->id;
498         if ($submission = $this->get_submission($USER->id)) {
499             $defaults->text = clean_text($submission->data1);
500         } else {
501             $defaults->text = '';
502         }
504         $mform->set_data($defaults);
506         if ($mform->is_cancelled()) {
507             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
508             redirect($returnurl);
509         }
511         if (!$this->can_update_notes($submission)) {
512             $this->view_header(get_string('upload'));
513             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
514             echo $OUTPUT->continue_button($returnurl);
515             $this->view_footer();
516             die;
517         }
519         if ($data = $mform->get_data() and $action == 'savenotes') {
520             $submission = $this->get_submission($USER->id, true); // get or create submission
521             $updated = new stdClass();
522             $updated->id           = $submission->id;
523             $updated->timemodified = time();
524             $updated->data1        = $data->text;
526             $DB->update_record('assignment_submissions', $updated);
527             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
528             redirect($returnurl);
529             $submission = $this->get_submission($USER->id);
530             $this->update_grade($submission);
531         }
533         /// show notes edit form
534         $this->view_header(get_string('notes', 'assignment'));
536         echo $OUTPUT->heading(get_string('notes', 'assignment'));
538         $mform->display();
540         $this->view_footer();
541         die;
542     }
544     function upload_responsefile($mform, $options) {
545         global $CFG, $USER, $OUTPUT, $PAGE;
547         $userid = required_param('userid', PARAM_INT);
548         $mode   = required_param('mode', PARAM_ALPHA);
549         $offset = required_param('offset', PARAM_INT);
551         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
553         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
554             $fs = get_file_storage();
555             $submission = $this->get_submission($userid, true, true);
556             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
557                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
558                 redirect($returnurl->out(false));
559             }
560         }
561         $PAGE->set_title(get_string('upload'));
562         echo $OUTPUT->header();
563         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
564         echo $OUTPUT->continue_button($returnurl->out(true));
565         echo $OUTPUT->footer();
566         die;
567     }
569     function upload_file($mform, $options) {
570         global $CFG, $USER, $DB, $OUTPUT;
572         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
573         $submission = $this->get_submission($USER->id);
575         if (!$this->can_upload_file($submission)) {
576             $this->view_header(get_string('upload'));
577             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
578             echo $OUTPUT->continue_button($returnurl);
579             $this->view_footer();
580             die;
581         }
583         if ($formdata = $mform->get_data()) {
584             $fs = get_file_storage();
585             $submission = $this->get_submission($USER->id, true); //create new submission if needed
586             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
587             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
588             $updates = new stdClass();
589             $updates->id = $submission->id;
590             $updates->numfiles = count($fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, 'sortorder', false));
591             $updates->timemodified = time();
592             $DB->update_record('assignment_submissions', $updates);
593             add_to_log($this->course->id, 'assignment', 'upload',
594                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
595             $this->update_grade($submission);
596             if (!$this->drafts_tracked()) {
597                 $this->email_teachers($submission);
598             }
600             // send files to event system
601             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
602             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
603             $eventdata = new stdClass();
604             $eventdata->modulename   = 'assignment';
605             $eventdata->cmid         = $this->cm->id;
606             $eventdata->itemid       = $submission->id;
607             $eventdata->courseid     = $this->course->id;
608             $eventdata->userid       = $USER->id;
609             if ($files) {
610                 $eventdata->files        = $files; // This is depreceated - please use pathnamehashes instead!
611             }
612             $eventdata->pathnamehashes = array_keys($files);
613             events_trigger('assessable_file_uploaded', $eventdata);
614             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
615             redirect($returnurl);
616         }
618         $this->view_header(get_string('upload'));
619         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
620         echo $OUTPUT->continue_button($returnurl);
621         $this->view_footer();
622         die;
623     }
625     function send_file($filearea, $args, $forcedownload, array $options=array()) {
626         global $CFG, $DB, $USER;
627         require_once($CFG->libdir.'/filelib.php');
629         require_login($this->course, false, $this->cm);
631         if ($filearea === 'submission') {
632             $submissionid = (int)array_shift($args);
634             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
635                 return false;
636             }
638             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
639                 return false;
640             }
642             $relativepath = implode('/', $args);
643             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
645             $fs = get_file_storage();
646             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
647                 return false;
648             }
650             send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
652         } else if ($filearea === 'response') {
653             $submissionid = (int)array_shift($args);
655             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
656                 return false;
657             }
659             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
660                 return false;
661             }
663             $relativepath = implode('/', $args);
664             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
666             $fs = get_file_storage();
667             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
668                 return false;
669             }
670             send_stored_file($file, 0, 0, true, $options);
671         }
673         return false;
674     }
676     function finalize($forcemode=null) {
677         global $USER, $DB, $OUTPUT;
678         $userid = optional_param('userid', $USER->id, PARAM_INT);
679         $offset = optional_param('offset', 0, PARAM_INT);
680         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
681         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
682         $submission = $this->get_submission($userid);
684         if ($forcemode!=null) {
685             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
686                 array('id'=>$this->cm->id,
687                     'userid'=>$userid,
688                     'mode'=>$forcemode,
689                     'offset'=>$offset
690                 ));
691         }
693         if (!$this->can_finalize($submission)) {
694             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
695         }
697         if ($forcemode==null) {
698             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
699                 $optionsno = array('id'=>$this->cm->id);
700                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
701                 $this->view_header(get_string('submitformarking', 'assignment'));
702                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
703                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
704                 $this->view_footer();
705                 die;
706             }
707         }
708         $updated = new stdClass();
709         $updated->id           = $submission->id;
710         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
711         $updated->timemodified = time();
713         $DB->update_record('assignment_submissions', $updated);
714         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
715                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
716         $submission = $this->get_submission($userid);
717         $this->update_grade($submission);
718         $this->email_teachers($submission);
720         // Trigger assessable_files_done event to show files are complete
721         $eventdata = new stdClass();
722         $eventdata->modulename   = 'assignment';
723         $eventdata->cmid         = $this->cm->id;
724         $eventdata->itemid       = $submission->id;
725         $eventdata->courseid     = $this->course->id;
726         $eventdata->userid       = $userid;
727         events_trigger('assessable_files_done', $eventdata);
729         if ($forcemode==null) {
730             redirect($returnurl->out(false));
731         }
732     }
734     function finalizeclose() {
735         global $DB;
737         $userid    = optional_param('userid', 0, PARAM_INT);
738         $mode      = required_param('mode', PARAM_ALPHA);
739         $offset    = required_param('offset', PARAM_INT);
740         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
742         // create but do not add student submission date
743         $submission = $this->get_submission($userid, true, true);
745         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
746             redirect($returnurl); // probably closed already
747         }
749         $updated = new stdClass();
750         $updated->id    = $submission->id;
751         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
753         $DB->update_record('assignment_submissions', $updated);
754         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
755                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
756         $submission = $this->get_submission($userid, false, true);
757         $this->update_grade($submission);
758         redirect($returnurl);
759     }
761     function unfinalize($forcemode=null) {
762         global $DB;
764         $userid = required_param('userid', PARAM_INT);
765         $mode   = required_param('mode', PARAM_ALPHA);
766         $offset = required_param('offset', PARAM_INT);
768         if ($forcemode!=null) {
769             $mode=$forcemode;
770         }
771         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
772         if (data_submitted()
773           and $submission = $this->get_submission($userid)
774           and $this->can_unfinalize($submission)
775           and confirm_sesskey()) {
777             $updated = new stdClass();
778             $updated->id = $submission->id;
779             $updated->data2 = '';
780             $DB->update_record('assignment_submissions', $updated);
781             //TODO: add unfinalize action to log
782             add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->cm->id.'&userid='.$userid.'&mode='.$mode.'&offset='.$offset, $this->assignment->id, $this->cm->id);
783             $submission = $this->get_submission($userid);
784             $this->update_grade($submission);
785         }
787         if ($forcemode==null) {
788             redirect($returnurl);
789         }
790     }
793     function delete() {
794         $action   = optional_param('action', '', PARAM_ALPHA);
796         switch ($action) {
797             case 'response':
798                 $this->delete_responsefile();
799                 break;
800             default:
801                 $this->delete_file();
802         }
803         die;
804     }
807     function delete_responsefile() {
808         global $CFG, $OUTPUT,$PAGE;
810         $file     = required_param('file', PARAM_FILE);
811         $userid   = required_param('userid', PARAM_INT);
812         $mode     = required_param('mode', PARAM_ALPHA);
813         $offset   = required_param('offset', PARAM_INT);
814         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
816         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
818         if (!$this->can_manage_responsefiles()) {
819            redirect($returnurl);
820         }
822         $urlreturn = 'submissions.php';
823         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
825         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
826             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
827             $PAGE->set_title(get_string('delete'));
828             echo $OUTPUT->header();
829             echo $OUTPUT->heading(get_string('delete'));
830             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
831             echo $OUTPUT->footer();
832             die;
833         }
835         if ($submission = $this->get_submission($userid)) {
836             $fs = get_file_storage();
837             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
838                 $file->delete();
839             }
840         }
841         redirect($returnurl);
842     }
845     function delete_file() {
846         global $CFG, $DB, $OUTPUT, $PAGE;
848         $file     = required_param('file', PARAM_FILE);
849         $userid   = required_param('userid', PARAM_INT);
850         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
851         $mode     = optional_param('mode', '', PARAM_ALPHA);
852         $offset   = optional_param('offset', 0, PARAM_INT);
854         require_login($this->course, false, $this->cm);
856         if (empty($mode)) {
857             $urlreturn = 'view.php';
858             $optionsreturn = array('id'=>$this->cm->id);
859             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
860         } else {
861             $urlreturn = 'submissions.php';
862             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
863             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
864         }
866         if (!$submission = $this->get_submission($userid) // incorrect submission
867           or !$this->can_delete_files($submission)) {     // can not delete
868             $this->view_header(get_string('delete'));
869             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
870             echo $OUTPUT->continue_button($returnurl);
871             $this->view_footer();
872             die;
873         }
875         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
876             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
877             if (empty($mode)) {
878                 $this->view_header(get_string('delete'));
879             } else {
880                 $PAGE->set_title(get_string('delete'));
881                 echo $OUTPUT->header();
882             }
883             echo $OUTPUT->heading(get_string('delete'));
884             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
885             if (empty($mode)) {
886                 $this->view_footer();
887             } else {
888                 echo $OUTPUT->footer();
889             }
890             die;
891         }
893         $fs = get_file_storage();
894         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
895             $file->delete();
896             $submission->timemodified = time();
897             $DB->update_record('assignment_submissions', $submission);
898             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
899                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
900             $this->update_grade($submission);
901         }
902         redirect($returnurl);
903     }
906     function can_upload_file($submission) {
907         global $USER;
909         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
910           and $this->isopen()                                                 // assignment not closed yet
911           and (empty($submission) or ($submission->userid == $USER->id))        // his/her own submission
912           and !$this->is_finalized($submission)) {                            // no uploading after final submission
913             return true;
914         } else {
915             return false;
916         }
917     }
919     function can_manage_responsefiles() {
920         if (has_capability('mod/assignment:grade', $this->context)) {
921             return true;
922         } else {
923             return false;
924         }
925     }
927     function can_delete_files($submission) {
928         global $USER;
930         if (has_capability('mod/assignment:grade', $this->context)) {
931             return true;
932         }
934         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
935           and $this->isopen()                                      // assignment not closed yet
936           and $this->assignment->resubmit                          // deleting allowed
937           and $USER->id == $submission->userid                     // his/her own submission
938           and !$this->is_finalized($submission)) {                 // no deleting after final submission
939             return true;
940         } else {
941             return false;
942         }
943     }
945     function drafts_tracked() {
946         return !empty($this->assignment->var4);
947     }
949     /**
950      * Returns submission status
951      * @param object $submission - may be empty
952      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
953      */
954     function is_finalized($submission) {
955         if (!$this->drafts_tracked()) {
956             return '';
958         } else if (empty($submission)) {
959             return '';
961         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
962             return $submission->data2;
964         } else {
965             return '';
966         }
967     }
969     function can_unfinalize($submission) {
970         if(is_bool($submission)) {
971             return false;
972         }
974         if (!$this->drafts_tracked()) {
975             return false;
976         }
978         if (has_capability('mod/assignment:grade', $this->context)
979           and $this->isopen()
980           and $this->is_finalized($submission)) {
981             return true;
982         } else {
983             return false;
984         }
985     }
987     function can_finalize($submission) {
988         global $USER;
990         if(is_bool($submission)) {
991             return false;
992         }
994         if (!$this->drafts_tracked()) {
995             return false;
996         }
998         if ($this->is_finalized($submission)) {
999             return false;
1000         }
1002         if (has_capability('mod/assignment:grade', $this->context)) {
1003             return true;
1005         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
1006           and $this->isopen()                                                 // assignment not closed yet
1007           and !empty($submission)                                             // submission must exist
1008           and $submission->userid == $USER->id                                // his/her own submission
1009           and ($this->count_user_files($submission->id)
1010             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
1012             return true;
1013         } else {
1014             return false;
1015         }
1016     }
1018     function can_update_notes($submission) {
1019         global $USER;
1021         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
1022           and $this->notes_allowed()                                          // notesd must be allowed
1023           and $this->isopen()                                                 // assignment not closed yet
1024           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
1025           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
1026             return true;
1027         } else {
1028             return false;
1029         }
1030     }
1032     function notes_allowed() {
1033         return (boolean)$this->assignment->var2;
1034     }
1036     function count_responsefiles($userid) {
1037         if ($submission = $this->get_submission($userid)) {
1038             $fs = get_file_storage();
1039             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
1040             return count($files);
1041         } else {
1042             return 0;
1043         }
1044     }
1046     function setup_elements(&$mform) {
1047         global $CFG, $COURSE;
1049         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1051         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1052         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1053         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1054         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1056         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1057         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1058         $mform->setDefault('resubmit', 1);
1060         $options = array();
1061         for($i = 1; $i <= 20; $i++) {
1062             $options[$i] = $i;
1063         }
1064         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1065         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1066         $mform->setDefault('var1', 3);
1068         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1069         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1070         $mform->setDefault('var2', 0);
1072         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1073         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1074         $mform->setDefault('var3', 0);
1076         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1077         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1078         $mform->setDefault('emailteachers', 0);
1080         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1081         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1082         $mform->setDefault('var4', 1);
1084         $course_context = context_course::instance($COURSE->id);
1085         plagiarism_get_form_elements_module($mform, $course_context);
1086     }
1088     function portfolio_exportable() {
1089         return true;
1090     }
1092     function extend_settings_navigation($node) {
1093         global $CFG, $USER, $OUTPUT;
1095         // get users submission if there is one
1096         $submission = $this->get_submission();
1097         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1098             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1099         } else {
1100             $editable = false;
1101         }
1103         // If the user has submitted something add some related links and data
1104         if (isset($submission->data2) AND $submission->data2 == 'submitted') {
1105             // Add a view link to the settings nav
1106             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1107             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1108             if (!empty($submission->timemodified)) {
1109                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1110                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1111                 $submittednode->add_class('note');
1112                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1113                     $submittednode->add_class('early');
1114                 } else {
1115                     $submittednode->add_class('late');
1116                 }
1117             }
1118         }
1120         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1121         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1122             $fs = get_file_storage();
1123             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1124                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1125                     $filenode = $node->add(get_string('submission', 'assignment'));
1126                 } else {
1127                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1128                 }
1129                 foreach ($files as $file) {
1130                     $filename = $file->get_filename();
1131                     $mimetype = $file->get_mimetype();
1132                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1133                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file),''));
1134                 }
1135             }
1136         }
1138         // Show a notes link if they are enabled
1139         if ($this->notes_allowed()) {
1140             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1141             $node->add(get_string('notes', 'assignment'), $link);
1142         }
1143     }
1145     /**
1146      * creates a zip of all assignment submissions and sends a zip to the browser
1147      */
1148     public function download_submissions() {
1149         global $CFG,$DB;
1150         require_once($CFG->libdir.'/filelib.php');
1151         $submissions = $this->get_submissions('','');
1152         if (empty($submissions)) {
1153             print_error('errornosubmissions', 'assignment');
1154         }
1155         $filesforzipping = array();
1156         $fs = get_file_storage();
1158         $groupmode = groups_get_activity_groupmode($this->cm);
1159         $groupid = 0;   // All users
1160         $groupname = '';
1161         if ($groupmode) {
1162             $groupid = groups_get_activity_group($this->cm, true);
1163             $groupname = groups_get_group_name($groupid).'-';
1164         }
1165         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1166         foreach ($submissions as $submission) {
1167             $a_userid = $submission->userid; //get userid
1168             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1169                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1170                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1172                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1173                 foreach ($files as $file) {
1174                     //get files new name.
1175                     $fileext = strstr($file->get_filename(), '.');
1176                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
1177                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1178                     //save file name to array for zipping.
1179                     $filesforzipping[$fileforzipname] = $file;
1180                 }
1181             }
1182         } // end of foreach loop
1183         if ($zipfile = assignment_pack_files($filesforzipping)) {
1184             send_temp_file($zipfile, $filename); //send file and delete after sending.
1185         }
1186     }
1188     /**
1189      * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
1190      * table before a submission actually takes place. This function checks to see if the given submission has actually
1191      * been submitted.
1192      *
1193      * @param  stdClass $submission The submission we want to check for completion
1194      * @return bool                 Indicates if the submission was found to be complete
1195      */
1196     public function is_submitted_with_required_data($submission) {
1197         return ($submission->timemodified AND $submission->data2);
1198     }
1201 class mod_assignment_upload_notes_form extends moodleform {
1203     function get_data() {
1204         $data = parent::get_data();
1205         if ($data) {
1206             $data->format = $data->text['format'];
1207             $data->text = $data->text['text'];
1208         }
1209         return $data;
1210     }
1212     function set_data($data) {
1213         if (!isset($data->format)) {
1214             $data->format = FORMAT_HTML;
1215         }
1216         if (isset($data->text)) {
1217             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1218         }
1219         parent::set_data($data);
1220     }
1222     function definition() {
1223         $mform = $this->_form;
1225         // visible elements
1226         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1227         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1229         // hidden params
1230         $mform->addElement('hidden', 'id', 0);
1231         $mform->setType('id', PARAM_INT);
1232         $mform->addElement('hidden', 'action', 'savenotes');
1233         $mform->setType('action', PARAM_ALPHA);
1235         // buttons
1236         $this->add_action_buttons();
1237     }
1240 class mod_assignment_upload_response_form extends moodleform {
1241     function definition() {
1242         $mform = $this->_form;
1243         $instance = $this->_customdata;
1245         // visible elements
1246         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1248         // hidden params
1249         $mform->addElement('hidden', 'id', $instance->cm->id);
1250         $mform->setType('id', PARAM_INT);
1251         $mform->addElement('hidden', 'contextid', $instance->contextid);
1252         $mform->setType('contextid', PARAM_INT);
1253         $mform->addElement('hidden', 'action', 'uploadresponse');
1254         $mform->setType('action', PARAM_ALPHA);
1255         $mform->addElement('hidden', 'mode', $instance->mode);
1256         $mform->setType('mode', PARAM_ALPHA);
1257         $mform->addElement('hidden', 'offset', $instance->offset);
1258         $mform->setType('offset', PARAM_INT);
1259         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1260         $mform->setType('forcerefresh', PARAM_INT);
1261         $mform->addElement('hidden', 'userid', $instance->userid);
1262         $mform->setType('userid', PARAM_INT);
1264         // buttons
1265         $this->add_action_buttons(false, get_string('uploadthisfile'));
1266     }