Merge branch 'MDL-30481_m23' of git://github.com/rwijaya/moodle into MOODLE_23_STABLE
[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             if ($this->assignment->timeavailable < time()) {
260                 echo $OUTPUT->heading(get_string('closedassignment','assignment'), 3);
261             } else {
262                 echo $OUTPUT->heading(get_string('futureaassignment','assignment'), 3);
263             }
264         } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
265             if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
266                 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
267             } else {
268                 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
269             }
270         } else {
271             //no submission yet
272         }
273     }
276     /**
277      * Return true if var3 == hide description till available day
278      *
279      *@return boolean
280      */
281     function description_is_hidden() {
282         return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
283     }
285     function print_student_answer($userid, $return=false){
286         global $CFG, $OUTPUT, $PAGE;
288         $submission = $this->get_submission($userid);
290         $output = '';
292         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
293             $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
294         }
296         if ($this->notes_allowed() and !empty($submission->data1)) {
297             $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
298             $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
299             $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
301             $output .= '&nbsp;';
302         }
305         $renderer = $PAGE->get_renderer('mod_assignment');
306         $output = $OUTPUT->box_start('files').$output;
307         $output .= $renderer->assignment_files($this->context, $submission->id);
308         $output .= $OUTPUT->box_end();
310         return $output;
311     }
314     /**
315      * Produces a list of links to the files uploaded by a user
316      *
317      * @param $userid int optional id of the user. If 0 then $USER->id is used.
318      * @param $return boolean optional defaults to false. If true the list is returned rather than printed
319      * @return string optional
320      */
321     function print_user_files($userid=0, $return=false) {
322         global $CFG, $USER, $OUTPUT, $PAGE;
324         $mode    = optional_param('mode', '', PARAM_ALPHA);
325         $offset  = optional_param('offset', 0, PARAM_INT);
327         if (!$userid) {
328             if (!isloggedin()) {
329                 return '';
330             }
331             $userid = $USER->id;
332         }
334         $output = $OUTPUT->box_start('files');
336         $submission = $this->get_submission($userid);
338         // only during grading
339         if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
340             $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
341         }
343         if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
345             $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
346             $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
348         }
350         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
351             if ($this->can_unfinalize($submission)) {
352                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
353                 $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
354                 $output .=  $OUTPUT->help_icon('unfinalize', 'assignment');
356             } else if ($this->can_finalize($submission)) {
357                 //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
358                 $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
359             }
360         }
362         if ($submission) {
363             $renderer = $PAGE->get_renderer('mod_assignment');
364             $output .= $renderer->assignment_files($this->context, $submission->id);
365         }
366         $output .= $OUTPUT->box_end();
368         if ($return) {
369             return $output;
370         }
371         echo $output;
372     }
374     function submissions($mode) {
375         // redirects out of form to process (un)finalizing.
376         $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
377         $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
378         if ($unfinalize) {
379             $this->unfinalize('single');
380         } else if ($finalize) {
381             $this->finalize('single');
382         }
383         if ($unfinalize || $finalize) {
384             $mode = 'singlenosave';
385         }
386         parent::submissions($mode);
387     }
389     function process_feedback($formdata=null) {
390         if (!$feedback = data_submitted() or !confirm_sesskey()) {      // No incoming data?
391             return false;
392         }
393         $userid = required_param('userid', PARAM_INT);
394         $offset = required_param('offset', PARAM_INT);
395         $mform = $this->display_submission($offset, $userid, false);
396         parent::process_feedback($mform);
397     }
399     /**
400      * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
401      * This is necessary for tracked advanced file uploads where we need to check that the data2 field is equal to ASSIGNMENT_STATUS_SUBMITTED
402      * to determine if a submission is complete.
403      *
404      * @param  int $groupid (optional) If nonzero then count is restricted to this group
405      * @return int          The number of submissions
406      */
407     function count_real_submissions($groupid=0) {
408         global $DB;
410         // Grab the context assocated with our course module
411         $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
413         // Get ids of users enrolled in the given course.
414         list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
415         $params['assignmentid'] = $this->cm->instance;
417         $query = '';
418         if ($this->drafts_tracked() and $this->isopen()) {
419             $query = ' AND ' . $DB->sql_compare_text('s.data2') . " = '"  . ASSIGNMENT_STATUS_SUBMITTED . "'";
420         } else {
421             // Count on submissions with files actually uploaded
422             $query = " AND s.numfiles > 0";
423         }
424         return $DB->count_records_sql("SELECT COUNT('x')
425                                          FROM {assignment_submissions} s
426                                     LEFT JOIN {assignment} a ON a.id = s.assignment
427                                    INNER JOIN ($enroledsql) u ON u.id = s.userid
428                                         WHERE s.assignment = :assignmentid" .
429                                               $query, $params);
430     }
432     function print_responsefiles($userid, $return=false) {
433         global $CFG, $USER, $OUTPUT, $PAGE;
435         $mode    = optional_param('mode', '', PARAM_ALPHA);
436         $offset  = optional_param('offset', 0, PARAM_INT);
438         $output = $OUTPUT->box_start('responsefiles');
440         $candelete = $this->can_manage_responsefiles();
441         $strdelete   = get_string('delete');
443         $fs = get_file_storage();
444         $browser = get_file_browser();
446         if ($submission = $this->get_submission($userid)) {
447             $renderer = $PAGE->get_renderer('mod_assignment');
448             $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
449         }
450         $output .= $OUTPUT->box_end();
452         if ($return) {
453             return $output;
454         }
455         echo $output;
456     }
458     /**
459      * Upload files
460      * upload_file function requires moodle form instance and file manager options
461      * @param object $mform
462      * @param array $options
463      */
464     function upload($mform = null, $filemanager_options = null) {
465         $action = required_param('action', PARAM_ALPHA);
466         switch ($action) {
467             case 'finalize':
468                 $this->finalize();
469                 break;
470             case 'finalizeclose':
471                 $this->finalizeclose();
472                 break;
473             case 'unfinalize':
474                 $this->unfinalize();
475                 break;
476             case 'uploadresponse':
477                 $this->upload_responsefile($mform, $filemanager_options);
478                 break;
479             case 'uploadfile':
480                 $this->upload_file($mform, $filemanager_options);
481             case 'savenotes':
482             case 'editnotes':
483                 $this->upload_notes();
484             default:
485                 print_error('unknowuploadaction', '', '', $action);
486         }
487     }
489     function upload_notes() {
490         global $CFG, $USER, $OUTPUT, $DB;
492         $action = required_param('action', PARAM_ALPHA);
494         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
496         $mform = new mod_assignment_upload_notes_form();
498         $defaults = new stdClass();
499         $defaults->id = $this->cm->id;
501         if ($submission = $this->get_submission($USER->id)) {
502             $defaults->text = clean_text($submission->data1);
503         } else {
504             $defaults->text = '';
505         }
507         $mform->set_data($defaults);
509         if ($mform->is_cancelled()) {
510             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
511             redirect($returnurl);
512         }
514         if (!$this->can_update_notes($submission)) {
515             $this->view_header(get_string('upload'));
516             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
517             echo $OUTPUT->continue_button($returnurl);
518             $this->view_footer();
519             die;
520         }
522         if ($data = $mform->get_data() and $action == 'savenotes') {
523             $submission = $this->get_submission($USER->id, true); // get or create submission
524             $updated = new stdClass();
525             $updated->id           = $submission->id;
526             $updated->timemodified = time();
527             $updated->data1        = $data->text;
529             $DB->update_record('assignment_submissions', $updated);
530             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
531             redirect($returnurl);
532             $submission = $this->get_submission($USER->id);
533             $this->update_grade($submission);
534         }
536         /// show notes edit form
537         $this->view_header(get_string('notes', 'assignment'));
539         echo $OUTPUT->heading(get_string('notes', 'assignment'));
541         $mform->display();
543         $this->view_footer();
544         die;
545     }
547     function upload_responsefile($mform, $options) {
548         global $CFG, $USER, $OUTPUT, $PAGE;
550         $userid = required_param('userid', PARAM_INT);
551         $mode   = required_param('mode', PARAM_ALPHA);
552         $offset = required_param('offset', PARAM_INT);
554         $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
556         if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
557             $fs = get_file_storage();
558             $submission = $this->get_submission($userid, true, true);
559             if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
560                 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
561                 redirect($returnurl->out(false));
562             }
563         }
564         $PAGE->set_title(get_string('upload'));
565         echo $OUTPUT->header();
566         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
567         echo $OUTPUT->continue_button($returnurl->out(true));
568         echo $OUTPUT->footer();
569         die;
570     }
572     function upload_file($mform, $options) {
573         global $CFG, $USER, $DB, $OUTPUT;
575         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
576         $submission = $this->get_submission($USER->id);
578         if (!$this->can_upload_file($submission)) {
579             $this->view_header(get_string('upload'));
580             echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
581             echo $OUTPUT->continue_button($returnurl);
582             $this->view_footer();
583             die;
584         }
586         if ($formdata = $mform->get_data()) {
587             $fs = get_file_storage();
588             $submission = $this->get_submission($USER->id, true); //create new submission if needed
589             $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
590             $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
591             $updates = new stdClass();
592             $updates->id = $submission->id;
593             $updates->numfiles = count($fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, 'sortorder', false));
594             $updates->timemodified = time();
595             $DB->update_record('assignment_submissions', $updates);
596             add_to_log($this->course->id, 'assignment', 'upload',
597                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
598             $this->update_grade($submission);
599             if (!$this->drafts_tracked()) {
600                 $this->email_teachers($submission);
601             }
603             // send files to event system
604             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
605             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
606             $eventdata = new stdClass();
607             $eventdata->modulename   = 'assignment';
608             $eventdata->cmid         = $this->cm->id;
609             $eventdata->itemid       = $submission->id;
610             $eventdata->courseid     = $this->course->id;
611             $eventdata->userid       = $USER->id;
612             if ($files) {
613                 $eventdata->files        = $files; // This is depreceated - please use pathnamehashes instead!
614             }
615             $eventdata->pathnamehashes = array_keys($files);
616             events_trigger('assessable_file_uploaded', $eventdata);
617             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
618             redirect($returnurl);
619         }
621         $this->view_header(get_string('upload'));
622         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
623         echo $OUTPUT->continue_button($returnurl);
624         $this->view_footer();
625         die;
626     }
628     function send_file($filearea, $args, $forcedownload, array $options=array()) {
629         global $CFG, $DB, $USER;
630         require_once($CFG->libdir.'/filelib.php');
632         require_login($this->course, false, $this->cm);
634         if ($filearea === 'submission') {
635             $submissionid = (int)array_shift($args);
637             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
638                 return false;
639             }
641             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
642                 return false;
643             }
645             $relativepath = implode('/', $args);
646             $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
648             $fs = get_file_storage();
649             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
650                 return false;
651             }
653             send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
655         } else if ($filearea === 'response') {
656             $submissionid = (int)array_shift($args);
658             if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
659                 return false;
660             }
662             if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
663                 return false;
664             }
666             $relativepath = implode('/', $args);
667             $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
669             $fs = get_file_storage();
670             if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
671                 return false;
672             }
673             send_stored_file($file, 0, 0, true, $options);
674         }
676         return false;
677     }
679     function finalize($forcemode=null) {
680         global $USER, $DB, $OUTPUT;
681         $userid = optional_param('userid', $USER->id, PARAM_INT);
682         $offset = optional_param('offset', 0, PARAM_INT);
683         $confirm    = optional_param('confirm', 0, PARAM_BOOL);
684         $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
685         $submission = $this->get_submission($userid);
687         if ($forcemode!=null) {
688             $returnurl  = new moodle_url('/mod/assignment/submissions.php',
689                 array('id'=>$this->cm->id,
690                     'userid'=>$userid,
691                     'mode'=>$forcemode,
692                     'offset'=>$offset
693                 ));
694         }
696         if (!$this->can_finalize($submission)) {
697             redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
698         }
700         if ($forcemode==null) {
701             if (!data_submitted() or !$confirm or !confirm_sesskey()) {
702                 $optionsno = array('id'=>$this->cm->id);
703                 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
704                 $this->view_header(get_string('submitformarking', 'assignment'));
705                 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
706                 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
707                 $this->view_footer();
708                 die;
709             }
710         }
711         $updated = new stdClass();
712         $updated->id           = $submission->id;
713         $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
714         $updated->timemodified = time();
716         $DB->update_record('assignment_submissions', $updated);
717         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
718                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
719         $submission = $this->get_submission($userid);
720         $this->update_grade($submission);
721         $this->email_teachers($submission);
723         // Trigger assessable_files_done event to show files are complete
724         $eventdata = new stdClass();
725         $eventdata->modulename   = 'assignment';
726         $eventdata->cmid         = $this->cm->id;
727         $eventdata->itemid       = $submission->id;
728         $eventdata->courseid     = $this->course->id;
729         $eventdata->userid       = $userid;
730         events_trigger('assessable_files_done', $eventdata);
732         if ($forcemode==null) {
733             redirect($returnurl->out(false));
734         }
735     }
737     function finalizeclose() {
738         global $DB;
740         $userid    = optional_param('userid', 0, PARAM_INT);
741         $mode      = required_param('mode', PARAM_ALPHA);
742         $offset    = required_param('offset', PARAM_INT);
743         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
745         // create but do not add student submission date
746         $submission = $this->get_submission($userid, true, true);
748         if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
749             redirect($returnurl); // probably closed already
750         }
752         $updated = new stdClass();
753         $updated->id    = $submission->id;
754         $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
756         $DB->update_record('assignment_submissions', $updated);
757         add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
758                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
759         $submission = $this->get_submission($userid, false, true);
760         $this->update_grade($submission);
761         redirect($returnurl);
762     }
764     function unfinalize($forcemode=null) {
765         global $DB;
767         $userid = required_param('userid', PARAM_INT);
768         $mode   = required_param('mode', PARAM_ALPHA);
769         $offset = required_param('offset', PARAM_INT);
771         if ($forcemode!=null) {
772             $mode=$forcemode;
773         }
774         $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
775         if (data_submitted()
776           and $submission = $this->get_submission($userid)
777           and $this->can_unfinalize($submission)
778           and confirm_sesskey()) {
780             $updated = new stdClass();
781             $updated->id = $submission->id;
782             $updated->data2 = '';
783             $DB->update_record('assignment_submissions', $updated);
784             //TODO: add unfinalize action to log
785             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);
786             $submission = $this->get_submission($userid);
787             $this->update_grade($submission);
788         }
790         if ($forcemode==null) {
791             redirect($returnurl);
792         }
793     }
796     function delete() {
797         $action   = optional_param('action', '', PARAM_ALPHA);
799         switch ($action) {
800             case 'response':
801                 $this->delete_responsefile();
802                 break;
803             default:
804                 $this->delete_file();
805         }
806         die;
807     }
810     function delete_responsefile() {
811         global $CFG, $OUTPUT,$PAGE;
813         $file     = required_param('file', PARAM_FILE);
814         $userid   = required_param('userid', PARAM_INT);
815         $mode     = required_param('mode', PARAM_ALPHA);
816         $offset   = required_param('offset', PARAM_INT);
817         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
819         $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
821         if (!$this->can_manage_responsefiles()) {
822            redirect($returnurl);
823         }
825         $urlreturn = 'submissions.php';
826         $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
828         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
829             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
830             $PAGE->set_title(get_string('delete'));
831             echo $OUTPUT->header();
832             echo $OUTPUT->heading(get_string('delete'));
833             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
834             echo $OUTPUT->footer();
835             die;
836         }
838         if ($submission = $this->get_submission($userid)) {
839             $fs = get_file_storage();
840             if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
841                 $file->delete();
842             }
843         }
844         redirect($returnurl);
845     }
848     function delete_file() {
849         global $CFG, $DB, $OUTPUT, $PAGE;
851         $file     = required_param('file', PARAM_FILE);
852         $userid   = required_param('userid', PARAM_INT);
853         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
854         $mode     = optional_param('mode', '', PARAM_ALPHA);
855         $offset   = optional_param('offset', 0, PARAM_INT);
857         require_login($this->course, false, $this->cm);
859         if (empty($mode)) {
860             $urlreturn = 'view.php';
861             $optionsreturn = array('id'=>$this->cm->id);
862             $returnurl  = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
863         } else {
864             $urlreturn = 'submissions.php';
865             $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
866             $returnurl  = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
867         }
869         if (!$submission = $this->get_submission($userid) // incorrect submission
870           or !$this->can_delete_files($submission)) {     // can not delete
871             $this->view_header(get_string('delete'));
872             echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
873             echo $OUTPUT->continue_button($returnurl);
874             $this->view_footer();
875             die;
876         }
878         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
879             $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
880             if (empty($mode)) {
881                 $this->view_header(get_string('delete'));
882             } else {
883                 $PAGE->set_title(get_string('delete'));
884                 echo $OUTPUT->header();
885             }
886             echo $OUTPUT->heading(get_string('delete'));
887             echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
888             if (empty($mode)) {
889                 $this->view_footer();
890             } else {
891                 echo $OUTPUT->footer();
892             }
893             die;
894         }
896         $fs = get_file_storage();
897         if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
898             $file->delete();
899             $submission->timemodified = time();
900             $DB->update_record('assignment_submissions', $submission);
901             add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
902                     'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
903             $this->update_grade($submission);
904         }
905         redirect($returnurl);
906     }
909     function can_upload_file($submission) {
910         global $USER;
912         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
913           and $this->isopen()                                                 // assignment not closed yet
914           and (empty($submission) or ($submission->userid == $USER->id))        // his/her own submission
915           and !$this->is_finalized($submission)) {                            // no uploading after final submission
916             return true;
917         } else {
918             return false;
919         }
920     }
922     function can_manage_responsefiles() {
923         if (has_capability('mod/assignment:grade', $this->context)) {
924             return true;
925         } else {
926             return false;
927         }
928     }
930     function can_delete_files($submission) {
931         global $USER;
933         if (has_capability('mod/assignment:grade', $this->context)) {
934             return true;
935         }
937         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
938           and $this->isopen()                                      // assignment not closed yet
939           and $this->assignment->resubmit                          // deleting allowed
940           and $USER->id == $submission->userid                     // his/her own submission
941           and !$this->is_finalized($submission)) {                 // no deleting after final submission
942             return true;
943         } else {
944             return false;
945         }
946     }
948     function drafts_tracked() {
949         return !empty($this->assignment->var4);
950     }
952     /**
953      * Returns submission status
954      * @param object $submission - may be empty
955      * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
956      */
957     function is_finalized($submission) {
958         if (!$this->drafts_tracked()) {
959             return '';
961         } else if (empty($submission)) {
962             return '';
964         } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
965             return $submission->data2;
967         } else {
968             return '';
969         }
970     }
972     function can_unfinalize($submission) {
973         if(is_bool($submission)) {
974             return false;
975         }
977         if (!$this->drafts_tracked()) {
978             return false;
979         }
981         if (has_capability('mod/assignment:grade', $this->context)
982           and $this->isopen()
983           and $this->is_finalized($submission)) {
984             return true;
985         } else {
986             return false;
987         }
988     }
990     function can_finalize($submission) {
991         global $USER;
993         if(is_bool($submission)) {
994             return false;
995         }
997         if (!$this->drafts_tracked()) {
998             return false;
999         }
1001         if ($this->is_finalized($submission)) {
1002             return false;
1003         }
1005         if (has_capability('mod/assignment:grade', $this->context)) {
1006             return true;
1008         } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
1009           and $this->isopen()                                                 // assignment not closed yet
1010           and !empty($submission)                                             // submission must exist
1011           and $submission->userid == $USER->id                                // his/her own submission
1012           and ($this->count_user_files($submission->id)
1013             or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
1015             return true;
1016         } else {
1017             return false;
1018         }
1019     }
1021     function can_update_notes($submission) {
1022         global $USER;
1024         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
1025           and $this->notes_allowed()                                          // notesd must be allowed
1026           and $this->isopen()                                                 // assignment not closed yet
1027           and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
1028           and !$this->is_finalized($submission)) {                            // no updateingafter final submission
1029             return true;
1030         } else {
1031             return false;
1032         }
1033     }
1035     function notes_allowed() {
1036         return (boolean)$this->assignment->var2;
1037     }
1039     function count_responsefiles($userid) {
1040         if ($submission = $this->get_submission($userid)) {
1041             $fs = get_file_storage();
1042             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
1043             return count($files);
1044         } else {
1045             return 0;
1046         }
1047     }
1049     function setup_elements(&$mform) {
1050         global $CFG, $COURSE;
1052         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1054         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1055         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1056         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1057         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1059         $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1060         $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
1061         $mform->setDefault('resubmit', 1);
1063         $options = array();
1064         for($i = 1; $i <= 20; $i++) {
1065             $options[$i] = $i;
1066         }
1067         $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1068         $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
1069         $mform->setDefault('var1', 3);
1071         $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1072         $mform->addHelpButton('var2', 'allownotes', 'assignment');
1073         $mform->setDefault('var2', 0);
1075         $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1076         $mform->addHelpButton('var3', 'hideintro', 'assignment');
1077         $mform->setDefault('var3', 0);
1079         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1080         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
1081         $mform->setDefault('emailteachers', 0);
1083         $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1084         $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
1085         $mform->setDefault('var4', 1);
1087         $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
1088         plagiarism_get_form_elements_module($mform, $course_context);
1089     }
1091     function portfolio_exportable() {
1092         return true;
1093     }
1095     function extend_settings_navigation($node) {
1096         global $CFG, $USER, $OUTPUT;
1098         // get users submission if there is one
1099         $submission = $this->get_submission();
1100         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
1101             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1102         } else {
1103             $editable = false;
1104         }
1106         // If the user has submitted something add some related links and data
1107         if (isset($submission->data2) AND $submission->data2 == 'submitted') {
1108             // Add a view link to the settings nav
1109             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
1110             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1111             if (!empty($submission->timemodified)) {
1112                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1113                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1114                 $submittednode->add_class('note');
1115                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
1116                     $submittednode->add_class('early');
1117                 } else {
1118                     $submittednode->add_class('late');
1119                 }
1120             }
1121         }
1123         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
1124         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
1125             $fs = get_file_storage();
1126             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
1127                 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
1128                     $filenode = $node->add(get_string('submission', 'assignment'));
1129                 } else {
1130                     $filenode = $node->add(get_string('submissiondraft', 'assignment'));
1131                 }
1132                 foreach ($files as $file) {
1133                     $filename = $file->get_filename();
1134                     $mimetype = $file->get_mimetype();
1135                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
1136                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file),''));
1137                 }
1138             }
1139         }
1141         // Show a notes link if they are enabled
1142         if ($this->notes_allowed()) {
1143             $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
1144             $node->add(get_string('notes', 'assignment'), $link);
1145         }
1146     }
1148     /**
1149      * creates a zip of all assignment submissions and sends a zip to the browser
1150      */
1151     public function download_submissions() {
1152         global $CFG,$DB;
1153         require_once($CFG->libdir.'/filelib.php');
1154         $submissions = $this->get_submissions('','');
1155         if (empty($submissions)) {
1156             print_error('errornosubmissions', 'assignment', new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id)));
1157         }
1158         $filesforzipping = array();
1159         $fs = get_file_storage();
1161         $groupmode = groups_get_activity_groupmode($this->cm);
1162         $groupid = 0;   // All users
1163         $groupname = '';
1164         if ($groupmode) {
1165             $groupid = groups_get_activity_group($this->cm, true);
1166             $groupname = groups_get_group_name($groupid).'-';
1167         }
1168         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1169         foreach ($submissions as $submission) {
1170             // If assignment is open and submission is not finalized and marking button enabled then don't add it to zip.
1171             $submissionstatus = $this->is_finalized($submission);
1172             if ($this->isopen() && empty($submissionstatus) && !empty($this->assignment->var4)) {
1173                 continue;
1174             }
1175             $a_userid = $submission->userid; //get userid
1176             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1177                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1178                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
1180                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
1181                 foreach ($files as $file) {
1182                     //get files new name.
1183                     $fileext = strstr($file->get_filename(), '.');
1184                     $fileoriginal = str_replace($fileext, '', $file->get_filename());
1185                     $fileforzipname =  clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
1186                     //save file name to array for zipping.
1187                     $filesforzipping[$fileforzipname] = $file;
1188                 }
1189             }
1190         } // end of foreach loop
1192         // Throw error if no files are added.
1193         if (empty($filesforzipping)) {
1194             print_error('errornosubmissions', 'assignment', new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id)));
1195         }
1197         if ($zipfile = assignment_pack_files($filesforzipping)) {
1198             send_temp_file($zipfile, $filename); //send file and delete after sending.
1199         }
1200     }
1202     /**
1203      * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
1204      * table before a submission actually takes place. This function checks to see if the given submission has actually
1205      * been submitted.
1206      *
1207      * @param  stdClass $submission The submission we want to check for completion
1208      * @return bool                 Indicates if the submission was found to be complete
1209      */
1210     public function is_submitted_with_required_data($submission) {
1211         if ($this->drafts_tracked()) {
1212             $submitted = $submission->timemodified > 0 &&
1213                          $submission->data2 == ASSIGNMENT_STATUS_SUBMITTED;
1214         } else {
1215             $submitted = $submission->numfiles > 0;
1216         }
1217         return $submitted;
1218     }
1221 class mod_assignment_upload_notes_form extends moodleform {
1223     function get_data() {
1224         $data = parent::get_data();
1225         if ($data) {
1226             $data->format = $data->text['format'];
1227             $data->text = $data->text['text'];
1228         }
1229         return $data;
1230     }
1232     function set_data($data) {
1233         if (!isset($data->format)) {
1234             $data->format = FORMAT_HTML;
1235         }
1236         if (isset($data->text)) {
1237             $data->text = array('text'=>$data->text, 'format'=>$data->format);
1238         }
1239         parent::set_data($data);
1240     }
1242     function definition() {
1243         $mform = $this->_form;
1245         // visible elements
1246         $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
1247         $mform->setType('text', PARAM_RAW); // to be cleaned before display
1249         // hidden params
1250         $mform->addElement('hidden', 'id', 0);
1251         $mform->setType('id', PARAM_INT);
1252         $mform->addElement('hidden', 'action', 'savenotes');
1253         $mform->setType('action', PARAM_ALPHA);
1255         // buttons
1256         $this->add_action_buttons();
1257     }
1260 class mod_assignment_upload_response_form extends moodleform {
1261     function definition() {
1262         $mform = $this->_form;
1263         $instance = $this->_customdata;
1265         // visible elements
1266         $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
1268         // hidden params
1269         $mform->addElement('hidden', 'id', $instance->cm->id);
1270         $mform->setType('id', PARAM_INT);
1271         $mform->addElement('hidden', 'contextid', $instance->contextid);
1272         $mform->setType('contextid', PARAM_INT);
1273         $mform->addElement('hidden', 'action', 'uploadresponse');
1274         $mform->setType('action', PARAM_ALPHA);
1275         $mform->addElement('hidden', 'mode', $instance->mode);
1276         $mform->setType('mode', PARAM_ALPHA);
1277         $mform->addElement('hidden', 'offset', $instance->offset);
1278         $mform->setType('offset', PARAM_INT);
1279         $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
1280         $mform->setType('forcerefresh', PARAM_INT);
1281         $mform->addElement('hidden', 'userid', $instance->userid);
1282         $mform->setType('userid', PARAM_INT);
1284         // buttons
1285         $this->add_action_buttons(false, get_string('uploadthisfile'));
1286     }