Whoops fixed some typos :-)
[moodle.git] / mod / assignment / type / upload / assignment.class.php
CommitLineData
fcdcc372 1<?php // $Id$
6117edc7 2require_once($CFG->libdir.'/formslib.php');
fcdcc372 3
55b4d096 4define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted');
5
fcdcc372 6/**
55b4d096 7 * Extend the base assignment class for assignments where you upload a single file
fcdcc372 8 *
9 */
10class assignment_upload extends assignment_base {
fcdcc372 11
12 function assignment_upload($cmid=0) {
13 parent::assignment_base($cmid);
8ba4cbd2 14
fcdcc372 15 }
16
fcdcc372 17 function view() {
8ba4cbd2 18 global $USER;
fcdcc372 19
55b4d096 20 require_capability('mod/assignment:view', $this->context);
fcdcc372 21
55b4d096 22 add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
23
24 $this->view_header();
29889a2a 25
26 if ($this->assignment->timeavailable > time()
8c408f8e 27 and !has_capability('mod/assignment:grade', $this->context) // grading user can see it anytime
28 and $this->assignment->var3) { // force hiding before available date
32776fef 29 print_simple_box_start('center', '', '', 0, 'generalbox', 'intro');
29889a2a 30 print_string('notavailableyet', 'assignment');
31 print_simple_box_end();
32 } else {
33 $this->view_intro();
34 }
35
55b4d096 36 $this->view_dates();
fcdcc372 37
55b4d096 38 if (has_capability('mod/assignment:submit', $this->context)) {
39 $filecount = $this->count_user_files($USER->id);
40 $submission = $this->get_submission($USER->id);
6fc73d59 41
55b4d096 42 $this->view_feedback();
6fc73d59 43
55b4d096 44 if ($this->is_finalized($submission)) {
a1c91f9a 45 print_heading(get_string('submission', 'assignment'), '', 3);
55b4d096 46 } else {
a1c91f9a 47 print_heading(get_string('submissiondraft', 'assignment'), '', 3);
fcdcc372 48 }
fcdcc372 49
55b4d096 50 if ($filecount and $submission) {
51 print_simple_box($this->print_user_files($USER->id, true), 'center');
52 } else {
53 if ($this->is_finalized($submission)) {
54 print_simple_box(get_string('nofiles', 'assignment'), 'center');
55 } else {
56 print_simple_box(get_string('nofilesyet', 'assignment'), 'center');
57 }
58 }
6fc73d59 59
8ba4cbd2 60 $this->view_upload_form();
55b4d096 61
62 if ($this->notes_allowed()) {
a1c91f9a 63 print_heading(get_string('notes', 'assignment'), '', 3);
55b4d096 64 $this->view_notes();
65 }
66
67 $this->view_final_submission();
8ba4cbd2 68 }
69 $this->view_footer();
fcdcc372 70 }
71
fcdcc372 72
55b4d096 73 function view_feedback($submission=NULL) {
74 global $USER;
8ba4cbd2 75
55b4d096 76 if (!$submission) { /// Get submission for this assignment
77 $submission = $this->get_submission($USER->id);
78 }
8ba4cbd2 79
55b4d096 80 if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
81 if ($this->count_responsefiles($USER->id)) {
82 print_heading(get_string('responsefiles', 'assignment', $this->course->teacher), '', 3);
83 $responsefiles = $this->print_responsefiles($USER->id, true);
84 print_simple_box($responsefiles, 'center');
85 }
86 return;
87 }
fcdcc372 88
55b4d096 89 /// We need the teacher info
90 if (! $teacher = get_record('user', 'id', $submission->teacher)) {
91 error('Could not find the teacher');
92 }
fcdcc372 93
55b4d096 94 /// Print the feedback
95 print_heading(get_string('submissionfeedback', 'assignment'), '', 3);
8ba4cbd2 96
55b4d096 97 echo '<table cellspacing="0" class="feedback">';
fcdcc372 98
55b4d096 99 echo '<tr>';
100 echo '<td class="left picture">';
101 print_user_picture($teacher->id, $this->course->id, $teacher->picture);
102 echo '</td>';
103 echo '<td class="topic">';
104 echo '<div class="from">';
105 echo '<div class="fullname">'.fullname($teacher).'</div>';
106 echo '<div class="time">'.userdate($submission->timemarked).'</div>';
107 echo '</div>';
108 echo '</td>';
109 echo '</tr>';
fcdcc372 110
55b4d096 111 echo '<tr>';
112 echo '<td class="left side">&nbsp;</td>';
113 echo '<td class="content">';
114 if ($this->assignment->grade) {
115 echo '<div class="grade">';
116 echo get_string("grade").': '.$this->display_grade($submission->grade);
117 echo '</div>';
118 echo '<div class="clearer"></div>';
119 }
8ba4cbd2 120
55b4d096 121 echo '<div class="comment">';
122 echo format_text($submission->submissioncomment, $submission->format);
123 echo '</div>';
124 echo '</tr>';
8ba4cbd2 125
55b4d096 126 echo '<tr>';
127 echo '<td class="left side">&nbsp;</td>';
128 echo '<td class="content">';
129 echo $this->print_responsefiles($USER->id, true);
130 echo '</tr>';
8ba4cbd2 131
55b4d096 132 echo '</table>';
fcdcc372 133 }
134
fcdcc372 135
55b4d096 136 function view_upload_form() {
137 global $CFG, $USER;
fcdcc372 138
8ba4cbd2 139 $submission = $this->get_submission($USER->id);
fcdcc372 140
55b4d096 141 $struploadafile = get_string('uploadafile');
142 $strmaxsize = get_string('maxsize', '', display_size($this->assignment->maxbytes));
fcdcc372 143
55b4d096 144 if ($this->is_finalized($submission)) {
145 // no uploading
146 return;
147 }
8ba4cbd2 148
55b4d096 149 if ($this->can_upload_file($submission)) {
d9cb14b8 150 echo '<div style="text-align:center">';
55b4d096 151 echo '<form enctype="multipart/form-data" method="post" action="upload.php">';
d9cb14b8 152 echo '<fieldset class="invisiblefieldset">';
55b4d096 153 echo "<p>$struploadafile ($strmaxsize)</p>";
154 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
155 echo '<input type="hidden" name="action" value="uploadfile" />';
156 require_once($CFG->libdir.'/uploadlib.php');
157 upload_print_form_fragment(1,array('newfile'),null,false,null,0,$this->assignment->maxbytes,false);
158 echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
d9cb14b8 159 echo '</fieldset>';
55b4d096 160 echo '</form>';
d9cb14b8 161 echo '</div>';
55b4d096 162 echo '<br />';
fcdcc372 163 }
8ba4cbd2 164
55b4d096 165 }
8ba4cbd2 166
55b4d096 167 function view_notes() {
168 global $USER;
169
170 if ($submission = $this->get_submission($USER->id)
171 and !empty($submission->data1)) {
172 print_simple_box(format_text($submission->data1, FORMAT_HTML), 'center', '630px');
173 } else {
174 print_simple_box(get_string('notesempty', 'assignment'), 'center');
175 }
176 if ($this->can_update_notes($submission)) {
177 $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
d9cb14b8 178 echo '<div style="text-align:center">';
55b4d096 179 print_single_button('upload.php', $options, get_string('edit'), 'post', '_self', false);
d9cb14b8 180 echo '</div>';
55b4d096 181 }
fcdcc372 182 }
183
55b4d096 184 function view_final_submission() {
fcdcc372 185 global $CFG, $USER;
186
8ba4cbd2 187 $submission = $this->get_submission($USER->id);
fcdcc372 188
55b4d096 189 if ($this->can_finalize($submission)) {
190 //print final submit button
191 print_heading(get_string('submitformarking','assignment'), '', 3);
d9cb14b8 192 echo '<div style="text-align:center">';
55b4d096 193 echo '<form method="post" action="upload.php">';
d9cb14b8 194 echo '<fieldset class="invisiblefieldset">';
55b4d096 195 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
196 echo '<input type="hidden" name="action" value="finalize" />';
197 echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
d9cb14b8 198 echo '</fieldset>';
55b4d096 199 echo '</form>';
d9cb14b8 200 echo '</div>';
55b4d096 201 } else if ($this->is_finalized($submission)) {
202 print_heading(get_string('submitedformarking','assignment'), '', 3);
203 } else {
204 //no submission yet
205 }
206 }
fcdcc372 207
55b4d096 208 function custom_feedbackform($submission, $return=false) {
209 global $CFG;
8ba4cbd2 210
55b4d096 211 $mode = optional_param('mode', '', PARAM_ALPHA);
212 $offset = optional_param('offset', 0, PARAM_INT);
213 $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
8ba4cbd2 214
55b4d096 215 $output = get_string('responsefiles', 'assignment').': ';
8ba4cbd2 216
55b4d096 217 $output .= '<form enctype="multipart/form-data" method="post" '.
218 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
219 $output .= '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
220 $output .= '<input type="hidden" name="action" value="uploadresponse" />';
221 $output .= '<input type="hidden" name="mode" value="'.$mode.'" />';
222 $output .= '<input type="hidden" name="offset" value="'.$offset.'" />';
223 $output .= '<input type="hidden" name="userid" value="'.$submission->userid.'" />';
224 require_once($CFG->libdir.'/uploadlib.php');
225 $output .= upload_print_form_fragment(1,array('newfile'),null,false,null,0,0,true);
226 $output .= '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
227 $output .= '</form>';
fcdcc372 228
55b4d096 229 if ($forcerefresh) {
230 $output .= $this->update_main_listing($submission);
231 }
fcdcc372 232
55b4d096 233 $responsefiles = $this->print_responsefiles($submission->userid, true);
234 if (!empty($responsefiles)) {
235 $output .= $responsefiles;
38ac07d2 236 }
55b4d096 237
238 if ($return) {
239 return $output;
38ac07d2 240 }
55b4d096 241 echo $output;
242 return;
243 }
fcdcc372 244
fcdcc372 245
55b4d096 246 function print_student_answer($userid, $return=false){
247 global $CFG;
fcdcc372 248
55b4d096 249 $filearea = $this->file_area_name($userid);
250 $submission = $this->get_submission($userid);
8ba4cbd2 251
55b4d096 252 $output = '';
8ba4cbd2 253
55b4d096 254 if ($basedir = $this->file_area($userid)) {
255 if (!$this->is_finalized($submission)) {
256 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
257 }
258
259 if ($this->notes_allowed() and !empty($submission->data1)) {
260 $output .= link_to_popup_window ('/mod/assignment/type/upload/notes.php?id='.$this->cm->id.'&amp;userid='.$userid,
261 'notes'.$userid, get_string('notes', 'assignment'), 500, 780, get_string('notes', 'assignment'), 'none', true, 'notesbutton'.$userid);
262 $output .= '&nbsp;';
263 }
264
265 if ($files = get_directory_list($basedir, 'responses')) {
266 foreach ($files as $key => $file) {
267 require_once($CFG->libdir.'/filelib.php');
268 $icon = mimeinfo('icon', $file);
269 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
a1c91f9a 270 $output .= '<a href="'.$ffurl.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>&nbsp;';
fcdcc372 271 }
fcdcc372 272 }
273 }
55b4d096 274 $output = '<div class="files">'.$output.'</div>';
275 $output .= '<br />';
8ba4cbd2 276
55b4d096 277 return $output;
fcdcc372 278 }
279
fcdcc372 280
55b4d096 281 /**
282 * Produces a list of links to the files uploaded by a user
283 *
284 * @param $userid int optional id of the user. If 0 then $USER->id is used.
285 * @param $return boolean optional defaults to false. If true the list is returned rather than printed
286 * @return string optional
287 */
288 function print_user_files($userid=0, $return=false) {
289 global $CFG, $USER;
290
291 $mode = optional_param('mode', '', PARAM_ALPHA);
292 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 293
55b4d096 294 if (!$userid) {
295 if (!isloggedin()) {
296 return '';
8ba4cbd2 297 }
55b4d096 298 $userid = $USER->id;
8ba4cbd2 299 }
300
55b4d096 301 $filearea = $this->file_area_name($userid);
8ba4cbd2 302
55b4d096 303 $output = '';
8ba4cbd2 304
55b4d096 305 if ($submission = $this->get_submission($userid)) {
8ba4cbd2 306
55b4d096 307 $candelete = $this->can_delete_files($submission);
308 $strdelete = get_string('delete');
8ba4cbd2 309
55b4d096 310 if (!$this->is_finalized($submission) and !empty($mode)) { // only during grading
311 $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
8ba4cbd2 312 }
8ba4cbd2 313
55b4d096 314 if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
315 $offset = required_param('offset', PARAM_INT);
fcdcc372 316
55b4d096 317 $npurl = "type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
318 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
fcdcc372 319
55b4d096 320 }
fcdcc372 321
55b4d096 322 if ($basedir = $this->file_area($userid)) {
323 if ($files = get_directory_list($basedir, 'responses')) {
324 require_once($CFG->libdir.'/filelib.php');
325 foreach ($files as $key => $file) {
8ba4cbd2 326
55b4d096 327 $icon = mimeinfo('icon', $file);
8ba4cbd2 328
55b4d096 329 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
8ba4cbd2 330
8ba4cbd2 331
0d905d9f 332 $output .= '<a href="'.$ffurl.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.$file.'</a>';
fcdcc372 333
55b4d096 334 if ($candelete) {
335 $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=$file&amp;userid={$submission->userid}&amp;mode=$mode&amp;offset=$offset";
8ba4cbd2 336
a1c91f9a 337 $output .= '<a href="'.$delurl.'">&nbsp;'
0d905d9f 338 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="" /></a> ';
8ba4cbd2 339 }
fcdcc372 340
55b4d096 341 $output .= '<br />';
6fc73d59 342 }
8ba4cbd2 343 }
55b4d096 344 }
345 if (has_capability('mod/assignment:grade', $this->context)
346 and $this->can_unfinalize($submission)
347 and $mode != '') { // we do not want it on view.php page
348 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
349 $output .= print_single_button('upload.php', $options, get_string('unfinalize', 'assignment'), 'post', '_self', true);
350 }
fcdcc372 351
55b4d096 352 $output = '<div class="files">'.$output.'</div>';
fcdcc372 353
55b4d096 354 }
8ba4cbd2 355
55b4d096 356 if ($return) {
357 return $output;
358 }
359 echo $output;
360 }
8ba4cbd2 361
55b4d096 362 function print_responsefiles($userid, $return=false) {
363 global $CFG, $USER;
8ba4cbd2 364
55b4d096 365 $mode = optional_param('mode', '', PARAM_ALPHA);
366 $offset = optional_param('offset', 0, PARAM_INT);
8ba4cbd2 367
55b4d096 368 $filearea = $this->file_area_name($userid).'/responses';
fcdcc372 369
55b4d096 370 $output = '';
fcdcc372 371
55b4d096 372 $candelete = $this->can_manage_responsefiles();
373 $strdelete = get_string('delete');
fcdcc372 374
55b4d096 375 if ($basedir = $this->file_area($userid)) {
376 $basedir .= '/responses';
fcdcc372 377
55b4d096 378 if ($files = get_directory_list($basedir)) {
379 require_once($CFG->libdir.'/filelib.php');
380 foreach ($files as $key => $file) {
fcdcc372 381
55b4d096 382 $icon = mimeinfo('icon', $file);
fcdcc372 383
55b4d096 384 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
fcdcc372 385
a1c91f9a 386 $output .= '<a href="'.$ffurl.'" ><img class="align" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>';
fcdcc372 387
55b4d096 388 if ($candelete) {
389 $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=$file&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;action=response";
fcdcc372 390
a1c91f9a 391 $output .= '<a href="'.$delurl.'">&nbsp;'
0d905d9f 392 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt=""/></a> ';
55b4d096 393 }
fcdcc372 394
55b4d096 395 $output .= '&nbsp;';
396 }
397 }
fcdcc372 398
fcdcc372 399
55b4d096 400 $output = '<div class="responsefiles">'.$output.'</div>';
401
8ba4cbd2 402 }
fcdcc372 403
55b4d096 404 if ($return) {
405 return $output;
8ba4cbd2 406 }
55b4d096 407 echo $output;
408 }
fcdcc372 409
fcdcc372 410
55b4d096 411 function upload() {
412 $action = required_param('action', PARAM_ALPHA);
fcdcc372 413
55b4d096 414 switch ($action) {
415 case 'finalize':
416 $this->finalize();
417 break;
418 case 'unfinalize':
419 $this->unfinalize();
420 break;
421 case 'uploadresponse':
422 $this->upload_responsefile();
423 break;
424 case 'uploadfile':
425 $this->upload_file();
426 case 'savenotes':
427 case 'editnotes':
428 $this->upload_notes();
429 default:
430 error('Error: Unknow upload action ('.$action.').');
8ba4cbd2 431 }
55b4d096 432 }
fcdcc372 433
55b4d096 434 function upload_notes() {
435 global $CFG, $USER;
fcdcc372 436
55b4d096 437 $action = required_param('action', PARAM_ALPHA);
fcdcc372 438
55b4d096 439 $returnurl = 'view.php?id='.$this->cm->id;
fcdcc372 440
1d284fbd 441 $mform = new mod_assignment_upload_notes_form();
6117edc7 442
443 $defaults = new object();
444 $defaults->id = $this->cm->id;
445
e38204d1 446 if ($submission = $this->get_submission($USER->id)) {
6117edc7 447 $defaults->text = $submission->data1;
e38204d1 448 } else {
6117edc7 449 $defaults->text = '';
e38204d1 450 }
451
6117edc7 452 $mform->set_defaults($defaults);
453
5571b5f4 454 if ($mform->is_cancelled()) {
455 redirect('view.php?id='.$this->cm->id);
456 }
457
e38204d1 458 if (!$this->can_update_notes($submission)) {
459 $this->view_header(get_string('upload'));
460 notify(get_string('uploaderror', 'assignment'));
461 print_continue($returnurl);
462 $this->view_footer();
463 die;
464 }
465
6117edc7 466 if ($data = $mform->data_submitted() and $action == 'savenotes') {
55b4d096 467 $submission = $this->get_submission($USER->id, true); // get or create submission
468 $updated = new object();
469 $updated->id = $submission->id;
470 $updated->timemodified = time();
6117edc7 471 $updated->data1 = $data->text;
fcdcc372 472
55b4d096 473 if (update_record('assignment_submissions', $updated)) {
474 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
475 redirect($returnurl);
476 } else {
477 $this->view_header(get_string('notes', 'assignment'));
478 notify(get_string('notesupdateerror', 'assignment'));
479 print_continue($returnurl);
480 $this->view_footer();
481 die;
482 }
483 }
fcdcc372 484
55b4d096 485 /// show notes edit form
55b4d096 486 $this->view_header(get_string('notes', 'assignment'));
6117edc7 487
a1c91f9a 488 print_heading(get_string('notes', 'assignment'), '');
6fc73d59 489
6117edc7 490 $mform->display();
8ba4cbd2 491
55b4d096 492 $this->view_footer();
493 die;
fcdcc372 494 }
495
55b4d096 496 function upload_responsefile() {
497 global $CFG;
8ba4cbd2 498
55b4d096 499 $userid = required_param('userid', PARAM_INT);
500 $mode = required_param('mode', PARAM_ALPHA);
501 $offset = required_param('offset', PARAM_INT);
fcdcc372 502
55b4d096 503 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 504
37aaf16c 505 if (data_submitted('nomatch') and $this->can_manage_responsefiles()) {
55b4d096 506 $dir = $this->file_area_name($userid).'/responses';
8d942514 507 check_dir_exists($CFG->dataroot.'/'.$dir, true, true);
8ba4cbd2 508
55b4d096 509 require_once($CFG->dirroot.'/lib/uploadlib.php');
510 $um = new upload_manager('newfile',false,true,$this->course,false,0,true);
511
512 if (!$um->process_file_uploads($dir)) {
513 print_header(get_string('upload'));
514 notify(get_string('uploaderror', 'assignment'));
515 echo $um->get_errors();
516 print_continue($returnurl);
517 print_footer('none');
518 die;
38ac07d2 519 }
fcdcc372 520 }
55b4d096 521 redirect($returnurl);
fcdcc372 522 }
523
55b4d096 524 function upload_file() {
fcdcc372 525 global $CFG, $USER;
8ba4cbd2 526
55b4d096 527 $mode = optional_param('mode', '', PARAM_ALPHA);
528 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 529
55b4d096 530 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 531
55b4d096 532 $filecount = $this->count_user_files($USER->id);
533 $submission = $this->get_submission($USER->id);
8ba4cbd2 534
55b4d096 535 if (!$this->can_upload_file($submission)) {
536 $this->view_header(get_string('upload'));
537 notify(get_string('uploaderror', 'assignment'));
538 print_continue($returnurl);
539 $this->view_footer();
540 die;
541 }
8ba4cbd2 542
55b4d096 543 $dir = $this->file_area_name($USER->id);
8d942514 544 check_dir_exists($CFG->dataroot.'/'.$dir, true, true); // better to create now so that student submissions do not block it later
fcdcc372 545
55b4d096 546 require_once($CFG->dirroot.'/lib/uploadlib.php');
547 $um = new upload_manager('newfile',false,true,$this->course,false,$this->assignment->maxbytes,true);
8ba4cbd2 548
55b4d096 549 if ($um->process_file_uploads($dir)) {
550 $submission = $this->get_submission($USER->id, true); //create new submission if needed
551 $updated = new object();
552 $updated->id = $submission->id;
553 $updated->timemodified = time();
554
555 if (update_record('assignment_submissions', $updated)) {
556 add_to_log($this->course->id, 'assignment', 'upload',
557 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
558 } else {
559 $new_filename = $um->get_new_filename();
560 $this->view_header(get_string('upload'));
561 notify(get_string('uploadnotregistered', 'assignment', $new_filename));
562 print_continue($returnurl);
563 $this->view_footer();
564 die;
fcdcc372 565 }
55b4d096 566 redirect('view.php?id='.$this->cm->id);
fcdcc372 567 }
55b4d096 568 $this->view_header(get_string('upload'));
569 notify(get_string('uploaderror', 'assignment'));
570 echo $um->get_errors();
571 print_continue($returnurl);
572 $this->view_footer();
573 die;
fcdcc372 574 }
575
55b4d096 576 function finalize() {
577 global $USER;
fcdcc372 578
55b4d096 579 $confirm = optional_param('confirm', 0, PARAM_BOOL);
580
581 $returnurl = 'view.php?id='.$this->cm->id;
582 $submission = $this->get_submission($USER->id);
583
584 if (!$this->can_finalize($submission)) {
585 redirect($returnurl); // probably already graded, erdirect to assignment page, the reason should be obvious
586 }
587
37aaf16c 588 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 589 $optionsno = array('id'=>$this->cm->id);
590 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize');
591 $this->view_header(get_string('submitformarking', 'assignment'));
592 print_heading(get_string('submitformarking', 'assignment'));
593 notice_yesno(get_string('onceassignmentsent', 'assignment'), 'upload.php', 'view.php', $optionsyes, $optionsno, 'post', 'get');
594 $this->view_footer();
595 die;
596
597 } else {
598 $updated = new object();
599 $updated->id = $submission->id;
600 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
601 $updated->timemodified = time();
602 if (update_record('assignment_submissions', $updated)) {
603 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finilize action to log
604 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
605 $this->email_teachers($submission);
606 } else {
607 $this->view_header(get_string('submitformarking', 'assignment'));
608 notify(get_string('finalizeerror', 'assignment'));
609 print_continue($returnurl);
610 $this->view_footer();
611 die;
fcdcc372 612 }
fcdcc372 613 }
55b4d096 614 redirect($returnurl);
615 }
fcdcc372 616
55b4d096 617 function unfinalize() {
8ba4cbd2 618
55b4d096 619 $userid = required_param('userid', PARAM_INT);
620 $mode = required_param('mode', PARAM_ALPHA);
621 $offset = required_param('offset', PARAM_INT);
fcdcc372 622
55b4d096 623 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
8ba4cbd2 624
37aaf16c 625 if (data_submitted('nomatch')
55b4d096 626 and $submission = $this->get_submission($userid)
627 and $this->can_unfinalize($submission)) {
fcdcc372 628
55b4d096 629 $updated = new object();
630 $updated->id = $submission->id;
631 $updated->data2 = '';
632 if (update_record('assignment_submissions', $updated)) {
633 //TODO: add unfinilize action to log
634 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
635 } else {
636 $this->view_header(get_string('submitformarking'));
637 notify(get_string('finalizeerror', 'assignment'));
638 print_continue($returnurl);
639 $this->view_footer();
640 die;
fcdcc372 641 }
642 }
55b4d096 643 redirect($returnurl);
644 }
fcdcc372 645
fcdcc372 646
55b4d096 647 function delete() {
648 $action = optional_param('action', '', PARAM_ALPHA);
fcdcc372 649
55b4d096 650 switch ($action) {
651 case 'response':
652 $this->delete_responsefile();
653 break;
654 default:
655 $this->delete_file();
fcdcc372 656 }
55b4d096 657 die;
658 }
fcdcc372 659
fcdcc372 660
55b4d096 661 function delete_responsefile() {
662 global $CFG;
8ba4cbd2 663
55b4d096 664 $file = required_param('file', PARAM_FILE);
665 $userid = required_param('userid', PARAM_INT);
666 $mode = required_param('mode', PARAM_ALPHA);
667 $offset = required_param('offset', PARAM_INT);
668 $confirm = optional_param('confirm', 0, PARAM_BOOL);
fcdcc372 669
55b4d096 670 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 671
55b4d096 672 if (!$this->can_manage_responsefiles()) {
673 redirect($returnurl);
674 }
fcdcc372 675
55b4d096 676 $urlreturn = 'submissions.php';
677 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
fcdcc372 678
37aaf16c 679 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 680 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset);
681 print_header(get_string('delete'));
682 print_heading(get_string('delete'));
683 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
684 print_footer('none');
685 die;
686 }
fcdcc372 687
55b4d096 688 $dir = $this->file_area_name($userid).'/responses';
689 $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
690 if (file_exists($filepath)) {
691 if (@unlink($filepath)) {
692 redirect($returnurl);
693 }
694 }
fcdcc372 695
55b4d096 696 // print delete error
697 print_header(get_string('delete'));
698 notify(get_string('deletefilefailed', 'assignment'));
699 print_continue($returnurl);
700 print_footer('none');
701 die;
fcdcc372 702
55b4d096 703 }
fcdcc372 704
fcdcc372 705
55b4d096 706 function delete_file() {
707 global $CFG;
fcdcc372 708
55b4d096 709 $file = required_param('file', PARAM_FILE);
710 $userid = required_param('userid', PARAM_INT);
711 $confirm = optional_param('confirm', 0, PARAM_BOOL);
712 $mode = optional_param('mode', '', PARAM_ALPHA);
713 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 714
55b4d096 715 require_login($this->course->id, false, $this->cm);
fcdcc372 716
55b4d096 717 if (empty($mode)) {
718 $urlreturn = 'view.php';
719 $optionsreturn = array('id'=>$this->cm->id);
720 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 721 } else {
55b4d096 722 $urlreturn = 'submissions.php';
723 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
724 $returnurl = "submissions.php?id={$this->cm->id}&amp;offset=$offset&amp;mode=$mode&amp;userid=$userid";
fcdcc372 725 }
726
55b4d096 727 if (!$submission = $this->get_submission($userid) // incorrect submission
728 or !$this->can_delete_files($submission)) { // can not delete
729 $this->view_header(get_string('delete'));
730 notify(get_string('cannotdeletefiles', 'assignment'));
731 print_continue($returnurl);
732 $this->view_footer();
733 die;
fcdcc372 734 }
55b4d096 735 $dir = $this->file_area_name($userid);
fcdcc372 736
37aaf16c 737 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 738 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset);
739 if (empty($mode)) {
740 $this->view_header(get_string('delete'));
741 } else {
742 print_header(get_string('delete'));
fcdcc372 743 }
55b4d096 744 print_heading(get_string('delete'));
745 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
746 if (empty($mode)) {
747 $this->view_footer();
748 } else {
749 print_footer('none');
fcdcc372 750 }
55b4d096 751 die;
fcdcc372 752 }
753
55b4d096 754 $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
755 if (file_exists($filepath)) {
756 if (@unlink($filepath)) {
757 $updated = new object();
758 $updated->id = $submission->id;
759 $updated->timemodified = time();
760 if (update_record('assignment_submissions', $updated)) {
761 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
762 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
763 }
764 redirect($returnurl);
765 }
fcdcc372 766 }
767
55b4d096 768 // print delete error
769 if (empty($mode)) {
770 $this->view_header(get_string('delete'));
771 } else {
772 print_header(get_string('delete'));
8ba4cbd2 773 }
55b4d096 774 notify(get_string('deletefilefailed', 'assignment'));
775 print_continue($returnurl);
776 if (empty($mode)) {
777 $this->view_footer();
778 } else {
779 print_footer('none');
8ba4cbd2 780 }
55b4d096 781 die;
782 }
8ba4cbd2 783
8ba4cbd2 784
55b4d096 785 function can_upload_file($submission) {
786 global $USER;
fcdcc372 787
55b4d096 788 if (has_capability('mod/assignment:submit', $this->context) // can submit
789 and $this->isopen() // assignment not closed yet
790 and (empty($submission) or $submission->grade == -1) // not graded
791 and (empty($submission) or $submission->userid == $USER->id) // his/her own submission
792 and $this->count_user_files($USER->id) < $this->assignment->var1) { // file limit not reached
793 return true;
794 } else {
795 return false;
8ba4cbd2 796 }
55b4d096 797 }
fcdcc372 798
55b4d096 799 function can_manage_responsefiles() {
800 if (has_capability('mod/assignment:grade', $this->context)) {
801 return true;
802 } else {
803 return false;
804 }
805 }
fcdcc372 806
55b4d096 807 function can_delete_files($submission) {
808 global $USER;
fcdcc372 809
55b4d096 810 if (has_capability('mod/assignment:grade', $this->context)) {
811 return true;
fcdcc372 812 }
813
55b4d096 814 if (has_capability('mod/assignment:submit', $this->context)
815 and $this->isopen() // assignment not closed yet
816 and (!empty($submission) and $submission->grade == -1) // not graded
817 and $this->assignment->resubmit // deleting allowed
818 and $USER->id == $submission->userid // his/her own submission
819 and !$this->is_finalized($submission)) { // no deleting after final submission
820 return true;
821 } else {
822 return false;
fcdcc372 823 }
55b4d096 824 }
fcdcc372 825
55b4d096 826 function is_finalized($submission) {
827 if (!empty($submission)
828 and $submission->data2 == ASSIGNMENT_STATUS_SUBMITTED) {
829 return true;
830 } else {
831 return false;
fcdcc372 832 }
55b4d096 833 }
fcdcc372 834
55b4d096 835 function can_unfinalize($submission) {
836 if (has_capability('mod/assignment:grade', $this->context)
837 and !empty($submission)
838 and $this->is_finalized($submission)
839 and $submission->grade == -1) {
840 return true;
fcdcc372 841 } else {
55b4d096 842 return false;
fcdcc372 843 }
fcdcc372 844 }
845
55b4d096 846 function can_finalize($submission) {
847 global $USER;
8ba4cbd2 848
55b4d096 849 if (has_capability('mod/assignment:submit', $this->context) // can submit
850 and $this->isopen() // assignment not closed yet
851 and !empty($submission) // submission must exist
852 and $submission->data2 != ASSIGNMENT_STATUS_SUBMITTED // not graded
853 and $submission->userid == $USER->id // his/her own submission
854 and $submission->grade == -1 // no reason to finalize already graded submission
855 and ($this->count_user_files($USER->id)
856 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
8ba4cbd2 857
55b4d096 858 return true;
8ba4cbd2 859 } else {
55b4d096 860 return false;
8ba4cbd2 861 }
55b4d096 862 }
8ba4cbd2 863
55b4d096 864 function can_update_notes($submission) {
865 global $USER;
8ba4cbd2 866
55b4d096 867 if (has_capability('mod/assignment:submit', $this->context)
868 and $this->notes_allowed() // notesd must be allowed
869 and $this->isopen() // assignment not closed yet
870 and (empty($submission) or $submission->grade == -1) // not graded
871 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
872 and !$this->is_finalized($submission)) { // no updateingafter final submission
873 return true;
8ba4cbd2 874 } else {
55b4d096 875 return false;
8ba4cbd2 876 }
8ba4cbd2 877 }
878
55b4d096 879 function notes_allowed() {
880 return (boolean)$this->assignment->var2;
881 }
8ba4cbd2 882
55b4d096 883 /**
884 * Count the files uploaded by a given user
885 *
886 * @param $userid int The user id
887 * @return int
888 */
889 function count_user_files($userid) {
890 global $CFG;
8ba4cbd2 891
55b4d096 892 $filearea = $this->file_area_name($userid);
8ba4cbd2 893
55b4d096 894 if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
895 if ($files = get_directory_list($basedir, 'responses')) {
896 return count($files);
38ac07d2 897 }
fcdcc372 898 }
55b4d096 899 return 0;
900 }
8ba4cbd2 901
55b4d096 902 function count_responsefiles($userid) {
903 global $CFG;
8ba4cbd2 904
55b4d096 905 $filearea = $this->file_area_name($userid).'/responses';
8ba4cbd2 906
55b4d096 907 if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
908 $basedir .= '/responses';
909 if ($files = get_directory_list($basedir)) {
910 return count($files);
8ba4cbd2 911 }
fcdcc372 912 }
55b4d096 913 return 0;
38ac07d2 914 }
8ba4cbd2 915
6117edc7 916}
917
f07b9627 918class mod_assignment_upload_notes_form extends moodleform {
6117edc7 919 function definition() {
920 $mform =& $this->_form;
921
922 // visible elements
923 $mform->addElement('htmleditor', 'text', get_string('notes', 'assignment'), array('cols'=>85, 'rows'=>30));
924 $mform->setType('text', PARAM_RAW); // to be cleaned before display
18a77361 925 $mform->setHelpButton('text', array('reading', 'writing'), false, 'editorhelpbutton');
55b4d096 926
6117edc7 927 // hidden params
928 $mform->addElement('hidden', 'id', 0);
929 $mform->setType('id', PARAM_INT);
930 $mform->addElement('hidden', 'action', 'savenotes');
931 $mform->setType('id', PARAM_ALPHA);
932
933 // buttons
a23f0aaf 934 $this->add_action_buttons();
6117edc7 935 }
fcdcc372 936}
8ba4cbd2 937
fcdcc372 938?>