MDL-15758 - remove dependence on 'userid' callback parameter in assignment portfolio...
[moodle.git] / mod / assignment / type / upload / assignment.class.php
CommitLineData
fcdcc372 1<?php // $Id$
67a87e7d 2require_once($CFG->libdir . '/portfoliolib.php');
3require_once($CFG->dirroot . '/mod/assignment/lib.php');
fcdcc372 4
39bc9fbb 5define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted'); // student thinks it is finished
6define('ASSIGNMENT_STATUS_CLOSED', 'closed'); // teacher prevents more submissions
55b4d096 7
fcdcc372 8/**
55b4d096 9 * Extend the base assignment class for assignments where you upload a single file
fcdcc372 10 *
11 */
12class assignment_upload extends assignment_base {
fcdcc372 13
7bddd4b7 14 function assignment_upload($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
15 parent::assignment_base($cmid, $assignment, $cm, $course);
0b5a80a1 16 $this->type = 'upload';
fcdcc372 17 }
18
fcdcc372 19 function view() {
8ba4cbd2 20 global $USER;
fcdcc372 21
55b4d096 22 require_capability('mod/assignment:view', $this->context);
fcdcc372 23
55b4d096 24 add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
25
26 $this->view_header();
29889a2a 27
28 if ($this->assignment->timeavailable > time()
8c408f8e 29 and !has_capability('mod/assignment:grade', $this->context) // grading user can see it anytime
30 and $this->assignment->var3) { // force hiding before available date
32776fef 31 print_simple_box_start('center', '', '', 0, 'generalbox', 'intro');
29889a2a 32 print_string('notavailableyet', 'assignment');
33 print_simple_box_end();
34 } else {
35 $this->view_intro();
36 }
37
55b4d096 38 $this->view_dates();
fcdcc372 39
55b4d096 40 if (has_capability('mod/assignment:submit', $this->context)) {
41 $filecount = $this->count_user_files($USER->id);
42 $submission = $this->get_submission($USER->id);
6fc73d59 43
55b4d096 44 $this->view_feedback();
6fc73d59 45
42f50aec 46 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
a1c91f9a 47 print_heading(get_string('submission', 'assignment'), '', 3);
55b4d096 48 } else {
a1c91f9a 49 print_heading(get_string('submissiondraft', 'assignment'), '', 3);
fcdcc372 50 }
fcdcc372 51
55b4d096 52 if ($filecount and $submission) {
53 print_simple_box($this->print_user_files($USER->id, true), 'center');
54 } else {
39bc9fbb 55 if (!$this->isopen() or $this->is_finalized($submission)) {
55b4d096 56 print_simple_box(get_string('nofiles', 'assignment'), 'center');
57 } else {
58 print_simple_box(get_string('nofilesyet', 'assignment'), 'center');
59 }
60 }
6fc73d59 61
8ba4cbd2 62 $this->view_upload_form();
55b4d096 63
64 if ($this->notes_allowed()) {
a1c91f9a 65 print_heading(get_string('notes', 'assignment'), '', 3);
55b4d096 66 $this->view_notes();
67 }
68
69 $this->view_final_submission();
8ba4cbd2 70 }
71 $this->view_footer();
fcdcc372 72 }
73
fcdcc372 74
55b4d096 75 function view_feedback($submission=NULL) {
74d7d735 76 global $USER, $CFG, $DB;
12dce253 77 require_once($CFG->libdir.'/gradelib.php');
8ba4cbd2 78
55b4d096 79 if (!$submission) { /// Get submission for this assignment
80 $submission = $this->get_submission($USER->id);
81 }
8ba4cbd2 82
55b4d096 83 if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
84 if ($this->count_responsefiles($USER->id)) {
85 print_heading(get_string('responsefiles', 'assignment', $this->course->teacher), '', 3);
86 $responsefiles = $this->print_responsefiles($USER->id, true);
87 print_simple_box($responsefiles, 'center');
88 }
89 return;
90 }
fcdcc372 91
12dce253 92 $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
93 $item = $grading_info->items[0];
94 $grade = $item->grades[$USER->id];
95
96 if ($grade->hidden or $grade->grade === false) { // hidden or error
97 return;
98 }
99
100 if ($grade->grade === null and empty($grade->str_feedback)) { /// Nothing to show yet
101 return;
102 }
103
104 $graded_date = $grade->dategraded;
105 $graded_by = $grade->usermodified;
106
55b4d096 107 /// We need the teacher info
74d7d735 108 if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
a939f681 109 print_error('cannotfindteacher');
55b4d096 110 }
fcdcc372 111
55b4d096 112 /// Print the feedback
113 print_heading(get_string('submissionfeedback', 'assignment'), '', 3);
8ba4cbd2 114
55b4d096 115 echo '<table cellspacing="0" class="feedback">';
fcdcc372 116
55b4d096 117 echo '<tr>';
118 echo '<td class="left picture">';
ad1e3409 119 print_user_picture($teacher, $this->course->id, $teacher->picture);
55b4d096 120 echo '</td>';
121 echo '<td class="topic">';
122 echo '<div class="from">';
123 echo '<div class="fullname">'.fullname($teacher).'</div>';
12dce253 124 echo '<div class="time">'.userdate($graded_date).'</div>';
55b4d096 125 echo '</div>';
126 echo '</td>';
127 echo '</tr>';
fcdcc372 128
55b4d096 129 echo '<tr>';
130 echo '<td class="left side">&nbsp;</td>';
131 echo '<td class="content">';
132 if ($this->assignment->grade) {
133 echo '<div class="grade">';
12dce253 134 echo get_string("grade").': '.$grade->str_long_grade;
55b4d096 135 echo '</div>';
136 echo '<div class="clearer"></div>';
137 }
8ba4cbd2 138
55b4d096 139 echo '<div class="comment">';
12dce253 140 echo $grade->str_feedback;
55b4d096 141 echo '</div>';
142 echo '</tr>';
8ba4cbd2 143
55b4d096 144 echo '<tr>';
145 echo '<td class="left side">&nbsp;</td>';
146 echo '<td class="content">';
147 echo $this->print_responsefiles($USER->id, true);
148 echo '</tr>';
8ba4cbd2 149
55b4d096 150 echo '</table>';
fcdcc372 151 }
152
fcdcc372 153
55b4d096 154 function view_upload_form() {
155 global $CFG, $USER;
fcdcc372 156
8ba4cbd2 157 $submission = $this->get_submission($USER->id);
fcdcc372 158
55b4d096 159 if ($this->is_finalized($submission)) {
160 // no uploading
161 return;
162 }
8ba4cbd2 163
55b4d096 164 if ($this->can_upload_file($submission)) {
172dd12c 165 $mform = new mod_assignment_upload_file_form('upload.php', $this);
166 $mform->display();
fcdcc372 167 }
8ba4cbd2 168
55b4d096 169 }
8ba4cbd2 170
55b4d096 171 function view_notes() {
172 global $USER;
173
174 if ($submission = $this->get_submission($USER->id)
175 and !empty($submission->data1)) {
176 print_simple_box(format_text($submission->data1, FORMAT_HTML), 'center', '630px');
177 } else {
178 print_simple_box(get_string('notesempty', 'assignment'), 'center');
179 }
180 if ($this->can_update_notes($submission)) {
181 $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
d9cb14b8 182 echo '<div style="text-align:center">';
55b4d096 183 print_single_button('upload.php', $options, get_string('edit'), 'post', '_self', false);
d9cb14b8 184 echo '</div>';
55b4d096 185 }
fcdcc372 186 }
187
55b4d096 188 function view_final_submission() {
fcdcc372 189 global $CFG, $USER;
190
8ba4cbd2 191 $submission = $this->get_submission($USER->id);
fcdcc372 192
39bc9fbb 193 if ($this->isopen() and $this->can_finalize($submission)) {
55b4d096 194 //print final submit button
195 print_heading(get_string('submitformarking','assignment'), '', 3);
d9cb14b8 196 echo '<div style="text-align:center">';
55b4d096 197 echo '<form method="post" action="upload.php">';
d9cb14b8 198 echo '<fieldset class="invisiblefieldset">';
55b4d096 199 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
200 echo '<input type="hidden" name="action" value="finalize" />';
201 echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
d9cb14b8 202 echo '</fieldset>';
55b4d096 203 echo '</form>';
d9cb14b8 204 echo '</div>';
39bc9fbb 205 } else if (!$this->isopen()) {
206 print_heading(get_string('nomoresubmissions','assignment'), '', 3);
207
42f50aec 208 } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
39bc9fbb 209 if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
210 print_heading(get_string('submitedformarking','assignment'), '', 3);
211 } else {
212 print_heading(get_string('nomoresubmissions','assignment'), '', 3);
213 }
55b4d096 214 } else {
215 //no submission yet
216 }
217 }
39bc9fbb 218
219
220 /**
dc6cb74e 221 * Return true if var3 == hide description till available day
39bc9fbb 222 *
dc6cb74e 223 *@return boolean
39bc9fbb 224 */
dc6cb74e 225 function description_is_hidden() {
226 return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
227 }
fcdcc372 228
55b4d096 229 function custom_feedbackform($submission, $return=false) {
230 global $CFG;
8ba4cbd2 231
55b4d096 232 $mode = optional_param('mode', '', PARAM_ALPHA);
233 $offset = optional_param('offset', 0, PARAM_INT);
234 $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
8ba4cbd2 235
172dd12c 236 $mform = new mod_assignment_upload_response_form("$CFG->wwwroot/mod/assignment/upload.php", $this);
237
238 $mform->set_data(array('id'=>$this->cm->id, 'offset'=>$offset, 'forcerefresh'=>$forcerefresh, 'userid'=>$submission->userid, 'mode'=>$mode));
239
55b4d096 240 $output = get_string('responsefiles', 'assignment').': ';
8ba4cbd2 241
172dd12c 242 ob_start();
243 $mform->display();
244 $output = ob_get_clean();
fcdcc372 245
55b4d096 246 if ($forcerefresh) {
247 $output .= $this->update_main_listing($submission);
248 }
fcdcc372 249
55b4d096 250 $responsefiles = $this->print_responsefiles($submission->userid, true);
251 if (!empty($responsefiles)) {
252 $output .= $responsefiles;
38ac07d2 253 }
55b4d096 254
255 if ($return) {
256 return $output;
38ac07d2 257 }
55b4d096 258 echo $output;
259 return;
260 }
fcdcc372 261
fcdcc372 262
55b4d096 263 function print_student_answer($userid, $return=false){
264 global $CFG;
fcdcc372 265
55b4d096 266 $submission = $this->get_submission($userid);
8ba4cbd2 267
55b4d096 268 $output = '';
8ba4cbd2 269
172dd12c 270 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
271 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
272 }
55b4d096 273
172dd12c 274 if ($this->notes_allowed() and !empty($submission->data1)) {
275 $output .= link_to_popup_window ('/mod/assignment/type/upload/notes.php?id='.$this->cm->id.'&amp;userid='.$userid,
276 'notes'.$userid, get_string('notes', 'assignment'), 500, 780, get_string('notes', 'assignment'), 'none', true, 'notesbutton'.$userid);
277 $output .= '&nbsp;';
278 }
55b4d096 279
172dd12c 280 $fs = get_file_storage();
281 $browser = get_file_browser();
282
283 if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
284
285 foreach ($files as $file) {
286 $filename = $file->get_filename();
287 $found = true;
288 $mimetype = $file->get_mimetype();
289 $icon = mimeinfo_from_type('icon', $mimetype);
290 $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
291 $output .= '<a href="'.$path.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.s($filename).'</a>&nbsp;';
292
fcdcc372 293 }
172dd12c 294
fcdcc372 295 }
55b4d096 296 $output = '<div class="files">'.$output.'</div>';
297 $output .= '<br />';
8ba4cbd2 298
55b4d096 299 return $output;
fcdcc372 300 }
301
fcdcc372 302
55b4d096 303 /**
304 * Produces a list of links to the files uploaded by a user
305 *
306 * @param $userid int optional id of the user. If 0 then $USER->id is used.
307 * @param $return boolean optional defaults to false. If true the list is returned rather than printed
308 * @return string optional
309 */
310 function print_user_files($userid=0, $return=false) {
311 global $CFG, $USER;
312
313 $mode = optional_param('mode', '', PARAM_ALPHA);
314 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 315
55b4d096 316 if (!$userid) {
317 if (!isloggedin()) {
318 return '';
8ba4cbd2 319 }
55b4d096 320 $userid = $USER->id;
8ba4cbd2 321 }
322
55b4d096 323 $output = '';
8ba4cbd2 324
39bc9fbb 325 $submission = $this->get_submission($userid);
8ba4cbd2 326
39bc9fbb 327 $candelete = $this->can_delete_files($submission);
328 $strdelete = get_string('delete');
8ba4cbd2 329
42f50aec 330 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) { // only during grading
39bc9fbb 331 $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
332 }
fcdcc372 333
39bc9fbb 334 if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
fcdcc372 335
39bc9fbb 336 $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
337 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
fcdcc372 338
39bc9fbb 339 }
8ba4cbd2 340
172dd12c 341 $fs = get_file_storage();
342 $browser = get_file_browser();
343
344 if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
345 $p = array(
172dd12c 346 'assignmentid' => $this->cm->id,
347 );
348 foreach ($files as $file) {
349 $filename = $file->get_filename();
350 $mimetype = $file->get_mimetype();
351 $icon = mimeinfo_from_type('icon', $mimetype);
352 $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
353 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.s($filename).'</a>';
354
355 if ($candelete) {
356 $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid={$submission->userid}&amp;mode=$mode&amp;offset=$offset";
357
358 $output .= '<a href="'.$delurl.'">&nbsp;'
359 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="" /></a> ';
8ba4cbd2 360 }
172dd12c 361
728e1931 362 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
899ffdfb 363 $p['file'] = $file->get_id();
866d543f 364 $output .= portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', PORTFOLIO_ADD_ICON_LINK, null, true);
67a87e7d 365 }
172dd12c 366 $output .= '<br />';
367 }
728e1931 368 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
172dd12c 369 unset($p['file']);// for all files
866d543f 370 $output .= '<br />' . portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', null, null, true);
55b4d096 371 }
39bc9fbb 372 }
03076be4 373
374 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
39bc9fbb 375 if ($this->can_unfinalize($submission)) {
376 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
377 $output .= print_single_button('upload.php', $options, get_string('unfinalize', 'assignment'), 'post', '_self', true);
378 } else if ($this->can_finalize($submission)) {
379 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
380 $output .= print_single_button('upload.php', $options, get_string('finalize', 'assignment'), 'post', '_self', true);
55b4d096 381 }
55b4d096 382 }
8ba4cbd2 383
39bc9fbb 384 $output = '<div class="files">'.$output.'</div>';
385
55b4d096 386 if ($return) {
387 return $output;
388 }
389 echo $output;
390 }
8ba4cbd2 391
55b4d096 392 function print_responsefiles($userid, $return=false) {
393 global $CFG, $USER;
8ba4cbd2 394
55b4d096 395 $mode = optional_param('mode', '', PARAM_ALPHA);
396 $offset = optional_param('offset', 0, PARAM_INT);
8ba4cbd2 397
55b4d096 398 $output = '';
fcdcc372 399
55b4d096 400 $candelete = $this->can_manage_responsefiles();
401 $strdelete = get_string('delete');
fcdcc372 402
172dd12c 403 $fs = get_file_storage();
404 $browser = get_file_browser();
fcdcc372 405
172dd12c 406 if ($files = $fs->get_area_files($this->context->id, 'assignment_response', $userid, "timemodified", false)) {
407 foreach ($files as $file) {
408 $filename = $file->get_filename();
409 $found = true;
410 $mimetype = $file->get_mimetype();
411 $icon = mimeinfo_from_type('icon', $mimetype);
412 $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_response/'.$userid.'/'.$filename);
fcdcc372 413
172dd12c 414 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$filename.'</a>';
fcdcc372 415
172dd12c 416 if ($candelete) {
417 $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;action=response";
fcdcc372 418
172dd12c 419 $output .= '<a href="'.$delurl.'">&nbsp;'
420 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt=""/></a> ';
55b4d096 421 }
fcdcc372 422
172dd12c 423 $output .= '&nbsp;';
424 }
fcdcc372 425
55b4d096 426 $output = '<div class="responsefiles">'.$output.'</div>';
8ba4cbd2 427 }
fcdcc372 428
55b4d096 429 if ($return) {
430 return $output;
8ba4cbd2 431 }
55b4d096 432 echo $output;
433 }
fcdcc372 434
fcdcc372 435
55b4d096 436 function upload() {
437 $action = required_param('action', PARAM_ALPHA);
fcdcc372 438
55b4d096 439 switch ($action) {
440 case 'finalize':
441 $this->finalize();
442 break;
39bc9fbb 443 case 'finalizeclose':
03076be4 444 $this->finalizeclose();
39bc9fbb 445 break;
55b4d096 446 case 'unfinalize':
447 $this->unfinalize();
448 break;
449 case 'uploadresponse':
450 $this->upload_responsefile();
451 break;
452 case 'uploadfile':
453 $this->upload_file();
454 case 'savenotes':
455 case 'editnotes':
456 $this->upload_notes();
457 default:
a939f681 458 print_error('unknowuploadaction', '', '', $action);
8ba4cbd2 459 }
55b4d096 460 }
fcdcc372 461
55b4d096 462 function upload_notes() {
463 global $CFG, $USER;
fcdcc372 464
55b4d096 465 $action = required_param('action', PARAM_ALPHA);
fcdcc372 466
55b4d096 467 $returnurl = 'view.php?id='.$this->cm->id;
fcdcc372 468
1d284fbd 469 $mform = new mod_assignment_upload_notes_form();
6117edc7 470
471 $defaults = new object();
472 $defaults->id = $this->cm->id;
473
e38204d1 474 if ($submission = $this->get_submission($USER->id)) {
6117edc7 475 $defaults->text = $submission->data1;
e38204d1 476 } else {
6117edc7 477 $defaults->text = '';
e38204d1 478 }
479
beac4717 480 $mform->set_data($defaults);
6117edc7 481
5571b5f4 482 if ($mform->is_cancelled()) {
483 redirect('view.php?id='.$this->cm->id);
484 }
485
e38204d1 486 if (!$this->can_update_notes($submission)) {
487 $this->view_header(get_string('upload'));
488 notify(get_string('uploaderror', 'assignment'));
489 print_continue($returnurl);
490 $this->view_footer();
491 die;
492 }
493
294ce987 494 if ($data = $mform->get_data() and $action == 'savenotes') {
55b4d096 495 $submission = $this->get_submission($USER->id, true); // get or create submission
496 $updated = new object();
497 $updated->id = $submission->id;
498 $updated->timemodified = time();
6117edc7 499 $updated->data1 = $data->text;
fcdcc372 500
74d7d735 501 if ($DB->update_record('assignment_submissions', $updated)) {
55b4d096 502 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
503 redirect($returnurl);
12dce253 504 $submission = $this->get_submission($USER->id);
505 $this->update_grade($submission);
506
55b4d096 507 } else {
508 $this->view_header(get_string('notes', 'assignment'));
509 notify(get_string('notesupdateerror', 'assignment'));
510 print_continue($returnurl);
511 $this->view_footer();
512 die;
513 }
514 }
fcdcc372 515
55b4d096 516 /// show notes edit form
55b4d096 517 $this->view_header(get_string('notes', 'assignment'));
6117edc7 518
a1c91f9a 519 print_heading(get_string('notes', 'assignment'), '');
6fc73d59 520
6117edc7 521 $mform->display();
8ba4cbd2 522
55b4d096 523 $this->view_footer();
524 die;
fcdcc372 525 }
526
55b4d096 527 function upload_responsefile() {
172dd12c 528 global $CFG, $USER;
8ba4cbd2 529
55b4d096 530 $userid = required_param('userid', PARAM_INT);
531 $mode = required_param('mode', PARAM_ALPHA);
532 $offset = required_param('offset', PARAM_INT);
fcdcc372 533
55b4d096 534 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 535
172dd12c 536 $mform = new mod_assignment_upload_response_form(null, $this);
537 if ($mform->get_data() and $this->can_manage_responsefiles()) {
538 $fs = get_file_storage();
539 $filename = $mform->get_new_filename('newfile');
540 if ($filename !== false) {
541 if (!$fs->file_exists($this->context->id, 'assignment_response', $userid, '/', $filename)) {
542 if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_response', $userid, '/', $filename, false, $USER->id)) {
543 redirect($returnurl);
544 }
545 }
38ac07d2 546 }
fcdcc372 547 }
172dd12c 548 print_header(get_string('upload'));
549 notify(get_string('uploaderror', 'assignment'));
550 print_continue($returnurl);
551 print_footer('none');
552 die;
fcdcc372 553 }
554
55b4d096 555 function upload_file() {
74d7d735 556 global $CFG, $USER, $DB;
8ba4cbd2 557
55b4d096 558 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 559
55b4d096 560 $filecount = $this->count_user_files($USER->id);
561 $submission = $this->get_submission($USER->id);
8ba4cbd2 562
55b4d096 563 if (!$this->can_upload_file($submission)) {
564 $this->view_header(get_string('upload'));
565 notify(get_string('uploaderror', 'assignment'));
566 print_continue($returnurl);
567 $this->view_footer();
568 die;
569 }
8ba4cbd2 570
172dd12c 571 $mform = new mod_assignment_upload_file_form('upload.php', $this);
572 if ($mform->get_data()) {
573 $fs = get_file_storage();
574 $filename = $mform->get_new_filename('newfile');
575 if ($filename !== false) {
576 if (!$fs->file_exists($this->context->id, 'assignment_submission', $USER->id, '/', $filename)) {
577 if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) {
578 $submission = $this->get_submission($USER->id, true); //create new submission if needed
579 $submission->timemodified = time();
580 if ($DB->update_record('assignment_submissions', $submission)) {
581 add_to_log($this->course->id, 'assignment', 'upload',
582 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
583 $this->update_grade($submission);
584 if (!$this->drafts_tracked()) {
585 $this->email_teachers($submission);
586 }
587 redirect('view.php?id='.$this->cm->id);
588 } else {
589 $file->delete();
590 }
591 }
91d8d9a0 592 }
fcdcc372 593 }
594 }
172dd12c 595
55b4d096 596 $this->view_header(get_string('upload'));
597 notify(get_string('uploaderror', 'assignment'));
55b4d096 598 print_continue($returnurl);
599 $this->view_footer();
600 die;
fcdcc372 601 }
602
172dd12c 603 function send_file($filearea, $args) {
604 global $CFG, $DB, $USER;
605 require_once($CFG->libdir.'/filelib.php');
606
607 require_login($this->course, false, $this->cm);
608
609 $userid = (int)array_shift($args);
610 $relativepath = '/'.implode('/', $args);
611 $fullpath = $this->context->id.$filearea.$userid.$relativepath;
612
613 $fs = get_file_storage();
614
615 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
616 return false;
617 }
618
619 if ($filearea === 'assignment_submission') {
620 if ($USER->id != $userid and !has_capability('mod/assignment:grade', $this->context)) {
621 return false;
622 }
623
624 } else if ($filearea === 'assignment_response') {
625 if ($USER->id != $userid and !has_capability('mod/assignment:grade', $this->context)) {
626 return false;
627 }
628
629 } else {
630 return false;
631 }
632
43c60e88 633 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
172dd12c 634 }
635
03076be4 636 function finalize() {
74d7d735 637 global $USER, $DB;
fcdcc372 638
03076be4 639 $confirm = optional_param('confirm', 0, PARAM_BOOL);
640 $returnurl = 'view.php?id='.$this->cm->id;
641 $submission = $this->get_submission($USER->id);
55b4d096 642
643 if (!$this->can_finalize($submission)) {
39bc9fbb 644 redirect($returnurl); // probably already graded, redirect to assignment page, the reason should be obvious
55b4d096 645 }
646
03076be4 647 if (!data_submitted() or !$confirm) {
55b4d096 648 $optionsno = array('id'=>$this->cm->id);
649 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize');
650 $this->view_header(get_string('submitformarking', 'assignment'));
651 print_heading(get_string('submitformarking', 'assignment'));
652 notice_yesno(get_string('onceassignmentsent', 'assignment'), 'upload.php', 'view.php', $optionsyes, $optionsno, 'post', 'get');
653 $this->view_footer();
654 die;
655
03076be4 656 }
657 $updated = new object();
658 $updated->id = $submission->id;
659 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
660 $updated->timemodified = time();
661
74d7d735 662 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 663 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
664 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 665 $submission = $this->get_submission($USER->id);
666 $this->update_grade($submission);
03076be4 667 $this->email_teachers($submission);
55b4d096 668 } else {
03076be4 669 $this->view_header(get_string('submitformarking', 'assignment'));
670 notify(get_string('finalizeerror', 'assignment'));
671 print_continue($returnurl);
672 $this->view_footer();
673 die;
674 }
675 redirect($returnurl);
676 }
39bc9fbb 677
03076be4 678 function finalizeclose() {
74d7d735 679 global $DB;
680
03076be4 681 $userid = optional_param('userid', 0, PARAM_INT);
682 $mode = required_param('mode', PARAM_ALPHA);
683 $offset = required_param('offset', PARAM_INT);
684 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
685
12dce253 686 // create but do not add student submission date
03076be4 687 $submission = $this->get_submission($userid, true, true);
688
689 if (!data_submitted() or !$this->can_finalize($submission)) {
690 redirect($returnurl); // probably closed already
691 }
692
693 $updated = new object();
694 $updated->id = $submission->id;
695 $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
696
74d7d735 697 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 698 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
699 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 700 $submission = $this->get_submission($userid, false, true);
701 $this->update_grade($submission);
fcdcc372 702 }
55b4d096 703 redirect($returnurl);
704 }
fcdcc372 705
55b4d096 706 function unfinalize() {
74d7d735 707 global $DB;
8ba4cbd2 708
55b4d096 709 $userid = required_param('userid', PARAM_INT);
710 $mode = required_param('mode', PARAM_ALPHA);
711 $offset = required_param('offset', PARAM_INT);
fcdcc372 712
55b4d096 713 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
8ba4cbd2 714
74d7d735 715 if (data_submitted()
55b4d096 716 and $submission = $this->get_submission($userid)
717 and $this->can_unfinalize($submission)) {
fcdcc372 718
55b4d096 719 $updated = new object();
720 $updated->id = $submission->id;
721 $updated->data2 = '';
74d7d735 722 if ($DB->update_record('assignment_submissions', $updated)) {
39bc9fbb 723 //TODO: add unfinalize action to log
55b4d096 724 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 725 $submission = $this->get_submission($userid);
726 $this->update_grade($submission);
55b4d096 727 } else {
0de80bcf 728 $this->view_header(get_string('submitformarking', 'assignment'));
729 notify(get_string('unfinalizeerror', 'assignment'));
55b4d096 730 print_continue($returnurl);
731 $this->view_footer();
732 die;
fcdcc372 733 }
734 }
55b4d096 735 redirect($returnurl);
736 }
fcdcc372 737
fcdcc372 738
55b4d096 739 function delete() {
740 $action = optional_param('action', '', PARAM_ALPHA);
fcdcc372 741
55b4d096 742 switch ($action) {
743 case 'response':
744 $this->delete_responsefile();
745 break;
746 default:
747 $this->delete_file();
fcdcc372 748 }
55b4d096 749 die;
750 }
fcdcc372 751
fcdcc372 752
55b4d096 753 function delete_responsefile() {
754 global $CFG;
8ba4cbd2 755
55b4d096 756 $file = required_param('file', PARAM_FILE);
757 $userid = required_param('userid', PARAM_INT);
758 $mode = required_param('mode', PARAM_ALPHA);
759 $offset = required_param('offset', PARAM_INT);
760 $confirm = optional_param('confirm', 0, PARAM_BOOL);
fcdcc372 761
55b4d096 762 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 763
55b4d096 764 if (!$this->can_manage_responsefiles()) {
765 redirect($returnurl);
766 }
fcdcc372 767
55b4d096 768 $urlreturn = 'submissions.php';
769 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
fcdcc372 770
74d7d735 771 if (!data_submitted() or !$confirm) {
55b4d096 772 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset);
773 print_header(get_string('delete'));
774 print_heading(get_string('delete'));
775 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
776 print_footer('none');
777 die;
778 }
fcdcc372 779
172dd12c 780 $fs = get_file_storage();
781 if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) {
782 if ($file->delete()) {
55b4d096 783 redirect($returnurl);
784 }
785 }
fcdcc372 786
55b4d096 787 // print delete error
788 print_header(get_string('delete'));
789 notify(get_string('deletefilefailed', 'assignment'));
790 print_continue($returnurl);
791 print_footer('none');
792 die;
fcdcc372 793
55b4d096 794 }
fcdcc372 795
fcdcc372 796
55b4d096 797 function delete_file() {
74d7d735 798 global $CFG, $DB;
fcdcc372 799
55b4d096 800 $file = required_param('file', PARAM_FILE);
801 $userid = required_param('userid', PARAM_INT);
802 $confirm = optional_param('confirm', 0, PARAM_BOOL);
803 $mode = optional_param('mode', '', PARAM_ALPHA);
804 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 805
55b4d096 806 require_login($this->course->id, false, $this->cm);
fcdcc372 807
55b4d096 808 if (empty($mode)) {
809 $urlreturn = 'view.php';
810 $optionsreturn = array('id'=>$this->cm->id);
811 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 812 } else {
55b4d096 813 $urlreturn = 'submissions.php';
814 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
815 $returnurl = "submissions.php?id={$this->cm->id}&amp;offset=$offset&amp;mode=$mode&amp;userid=$userid";
fcdcc372 816 }
817
55b4d096 818 if (!$submission = $this->get_submission($userid) // incorrect submission
819 or !$this->can_delete_files($submission)) { // can not delete
820 $this->view_header(get_string('delete'));
821 notify(get_string('cannotdeletefiles', 'assignment'));
822 print_continue($returnurl);
823 $this->view_footer();
824 die;
fcdcc372 825 }
826
74d7d735 827 if (!data_submitted() or !$confirm) {
55b4d096 828 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset);
829 if (empty($mode)) {
830 $this->view_header(get_string('delete'));
831 } else {
832 print_header(get_string('delete'));
fcdcc372 833 }
55b4d096 834 print_heading(get_string('delete'));
835 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
836 if (empty($mode)) {
837 $this->view_footer();
838 } else {
839 print_footer('none');
fcdcc372 840 }
55b4d096 841 die;
fcdcc372 842 }
843
172dd12c 844 $fs = get_file_storage();
845 if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) {
846 if ($file->delete()) {
847 $submission->timemodified = time();
848 if ($DB->update_record('assignment_submissions', $submission)) {
55b4d096 849 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
850 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 851 $this->update_grade($submission);
55b4d096 852 }
853 redirect($returnurl);
854 }
fcdcc372 855 }
856
55b4d096 857 // print delete error
858 if (empty($mode)) {
859 $this->view_header(get_string('delete'));
860 } else {
861 print_header(get_string('delete'));
8ba4cbd2 862 }
55b4d096 863 notify(get_string('deletefilefailed', 'assignment'));
864 print_continue($returnurl);
865 if (empty($mode)) {
866 $this->view_footer();
867 } else {
868 print_footer('none');
8ba4cbd2 869 }
55b4d096 870 die;
871 }
8ba4cbd2 872
8ba4cbd2 873
55b4d096 874 function can_upload_file($submission) {
875 global $USER;
fcdcc372 876
55b4d096 877 if (has_capability('mod/assignment:submit', $this->context) // can submit
878 and $this->isopen() // assignment not closed yet
55b4d096 879 and (empty($submission) or $submission->userid == $USER->id) // his/her own submission
83774723 880 and $this->count_user_files($USER->id) < $this->assignment->var1 // file limit not reached
881 and !$this->is_finalized($submission)) { // no uploading after final submission
55b4d096 882 return true;
883 } else {
884 return false;
8ba4cbd2 885 }
55b4d096 886 }
fcdcc372 887
55b4d096 888 function can_manage_responsefiles() {
889 if (has_capability('mod/assignment:grade', $this->context)) {
890 return true;
891 } else {
892 return false;
893 }
894 }
fcdcc372 895
55b4d096 896 function can_delete_files($submission) {
897 global $USER;
fcdcc372 898
55b4d096 899 if (has_capability('mod/assignment:grade', $this->context)) {
900 return true;
fcdcc372 901 }
902
55b4d096 903 if (has_capability('mod/assignment:submit', $this->context)
904 and $this->isopen() // assignment not closed yet
55b4d096 905 and $this->assignment->resubmit // deleting allowed
906 and $USER->id == $submission->userid // his/her own submission
907 and !$this->is_finalized($submission)) { // no deleting after final submission
908 return true;
909 } else {
910 return false;
fcdcc372 911 }
55b4d096 912 }
fcdcc372 913
42f50aec 914 function drafts_tracked() {
915 return !empty($this->assignment->var4);
916 }
917
39bc9fbb 918 /**
919 * Returns submission status
920 * @param object $submission - may be empty
921 * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
922 */
55b4d096 923 function is_finalized($submission) {
42f50aec 924 if (!$this->drafts_tracked()) {
925 return '';
926
927 } else if (empty($submission)) {
39bc9fbb 928 return '';
929
930 } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
931 return $submission->data2;
932
55b4d096 933 } else {
39bc9fbb 934 return '';
fcdcc372 935 }
55b4d096 936 }
fcdcc372 937
55b4d096 938 function can_unfinalize($submission) {
42f50aec 939 if (!$this->drafts_tracked()) {
940 return false;
941 }
55b4d096 942 if (has_capability('mod/assignment:grade', $this->context)
ad1e3409 943 and $this->isopen()
944 and $this->is_finalized($submission)) {
55b4d096 945 return true;
fcdcc372 946 } else {
55b4d096 947 return false;
fcdcc372 948 }
fcdcc372 949 }
950
55b4d096 951 function can_finalize($submission) {
952 global $USER;
42f50aec 953 if (!$this->drafts_tracked()) {
954 return false;
955 }
8ba4cbd2 956
ad1e3409 957 if ($this->is_finalized($submission)) {
958 return false;
959 }
960
961 if (has_capability('mod/assignment:grade', $this->context)) {
962 return true;
963
964 } else if (has_capability('mod/assignment:submit', $this->context) // can submit
55b4d096 965 and $this->isopen() // assignment not closed yet
966 and !empty($submission) // submission must exist
55b4d096 967 and $submission->userid == $USER->id // his/her own submission
55b4d096 968 and ($this->count_user_files($USER->id)
ad1e3409 969 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
8ba4cbd2 970
55b4d096 971 return true;
8ba4cbd2 972 } else {
55b4d096 973 return false;
8ba4cbd2 974 }
55b4d096 975 }
8ba4cbd2 976
55b4d096 977 function can_update_notes($submission) {
978 global $USER;
8ba4cbd2 979
55b4d096 980 if (has_capability('mod/assignment:submit', $this->context)
ad1e3409 981 and $this->notes_allowed() // notesd must be allowed
55b4d096 982 and $this->isopen() // assignment not closed yet
55b4d096 983 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
984 and !$this->is_finalized($submission)) { // no updateingafter final submission
985 return true;
8ba4cbd2 986 } else {
55b4d096 987 return false;
8ba4cbd2 988 }
8ba4cbd2 989 }
990
55b4d096 991 function notes_allowed() {
992 return (boolean)$this->assignment->var2;
993 }
8ba4cbd2 994
55b4d096 995 function count_responsefiles($userid) {
172dd12c 996 $fs = get_file_storage();
997 $files = $fs->get_area_files($this->context->id, 'assignment_response', $userid, "id", false);
998 return count($files);
38ac07d2 999 }
8ba4cbd2 1000
436cfa9f 1001 function setup_elements(&$mform) {
1002 global $CFG, $COURSE;
1003
1004 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1005
1006 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
436cfa9f 1007 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1008 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1009 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1010
1011 $mform->addElement('select', 'resubmit', get_string("allowdeleting", "assignment"), $ynoptions);
1012 $mform->setHelpButton('resubmit', array('allowdeleting', get_string('allowdeleting', 'assignment'), 'assignment'));
1013 $mform->setDefault('resubmit', 1);
1014
1015 $options = array();
1016 for($i = 1; $i <= 20; $i++) {
1017 $options[$i] = $i;
1018 }
1019 $mform->addElement('select', 'var1', get_string("allowmaxfiles", "assignment"), $options);
1020 $mform->setHelpButton('var1', array('allowmaxfiles', get_string('allowmaxfiles', 'assignment'), 'assignment'));
1021 $mform->setDefault('var1', 3);
1022
1023 $mform->addElement('select', 'var2', get_string("allownotes", "assignment"), $ynoptions);
1024 $mform->setHelpButton('var2', array('allownotes', get_string('allownotes', 'assignment'), 'assignment'));
1025 $mform->setDefault('var2', 0);
1026
1027 $mform->addElement('select', 'var3', get_string("hideintro", "assignment"), $ynoptions);
1028 $mform->setHelpButton('var3', array('hideintro', get_string('hideintro', 'assignment'), 'assignment'));
1029 $mform->setDefault('var3', 0);
1030
1031 $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
1032 $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
1033 $mform->setDefault('emailteachers', 0);
1034
42f50aec 1035 $mform->addElement('select', 'var4', get_string("trackdrafts", "assignment"), $ynoptions);
1036 $mform->setHelpButton('var4', array('trackdrafts', get_string('trackdrafts', 'assignment'), 'assignment'));
1037 $mform->setDefault('trackdrafts', 1);
436cfa9f 1038
1039 }
1040
67a87e7d 1041 function portfolio_exportable() {
1042 return true;
1043 }
1044
6117edc7 1045}
1046
f07b9627 1047class mod_assignment_upload_notes_form extends moodleform {
6117edc7 1048 function definition() {
172dd12c 1049 $mform = $this->_form;
6117edc7 1050
1051 // visible elements
1052 $mform->addElement('htmleditor', 'text', get_string('notes', 'assignment'), array('cols'=>85, 'rows'=>30));
1053 $mform->setType('text', PARAM_RAW); // to be cleaned before display
18a77361 1054 $mform->setHelpButton('text', array('reading', 'writing'), false, 'editorhelpbutton');
55b4d096 1055
6117edc7 1056 // hidden params
1057 $mform->addElement('hidden', 'id', 0);
1058 $mform->setType('id', PARAM_INT);
1059 $mform->addElement('hidden', 'action', 'savenotes');
172dd12c 1060 $mform->setType('action', PARAM_ALPHA);
6117edc7 1061
1062 // buttons
a23f0aaf 1063 $this->add_action_buttons();
6117edc7 1064 }
fcdcc372 1065}
8ba4cbd2 1066
172dd12c 1067class mod_assignment_upload_response_form extends moodleform {
1068 function definition() {
1069 $mform = $this->_form;
1070 $instance = $this->_customdata;
1071
1072 // visible elements
1073 $mform->addElement('file', 'newfile', get_string('uploadafile'));
1074
1075 // hidden params
1076 $mform->addElement('hidden', 'id', $instance->cm->id);
1077 $mform->setType('id', PARAM_INT);
1078 $mform->addElement('hidden', 'action', 'uploadresponse');
1079 $mform->setType('action', PARAM_ALPHA);
1080 $mform->addElement('hidden', 'mode');
1081 $mform->setType('mode', PARAM_ALPHA);
1082 $mform->addElement('hidden', 'offset');
1083 $mform->setType('offset', PARAM_INT);
1084 $mform->addElement('hidden', 'forcerefresh');
1085 $mform->setType('forcerefresh', PARAM_INT);
1086 $mform->addElement('hidden', 'userid');
1087 $mform->setType('userid', PARAM_INT);
1088
1089 // buttons
1090 $this->add_action_buttons(false, get_string('uploadthisfile'));
1091 }
1092}
1093
67a87e7d 1094
1095
fcdcc372 1096?>