MDL-13653 droplow not ignoring ungraded items un sum aggregation; merged from MOODLE_...
[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
7bddd4b7 12 function assignment_upload($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
13 parent::assignment_base($cmid, $assignment, $cm, $course);
0b5a80a1 14 $this->type = 'upload';
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">';
ad1e3409 101 print_user_picture($teacher, $this->course->id, $teacher->picture);
55b4d096 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 }
dc6cb74e 207
208
209 /*
210 * Return true if var3 == hide description till available day
211 *
212 *@return boolean
213 */
214 function description_is_hidden() {
215 return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
216 }
fcdcc372 217
55b4d096 218 function custom_feedbackform($submission, $return=false) {
219 global $CFG;
8ba4cbd2 220
55b4d096 221 $mode = optional_param('mode', '', PARAM_ALPHA);
222 $offset = optional_param('offset', 0, PARAM_INT);
223 $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
8ba4cbd2 224
55b4d096 225 $output = get_string('responsefiles', 'assignment').': ';
8ba4cbd2 226
55b4d096 227 $output .= '<form enctype="multipart/form-data" method="post" '.
228 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
2bf7e0b1 229 $output .= '<div>';
55b4d096 230 $output .= '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
231 $output .= '<input type="hidden" name="action" value="uploadresponse" />';
232 $output .= '<input type="hidden" name="mode" value="'.$mode.'" />';
233 $output .= '<input type="hidden" name="offset" value="'.$offset.'" />';
234 $output .= '<input type="hidden" name="userid" value="'.$submission->userid.'" />';
235 require_once($CFG->libdir.'/uploadlib.php');
236 $output .= upload_print_form_fragment(1,array('newfile'),null,false,null,0,0,true);
237 $output .= '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
2bf7e0b1 238 $output .= '</div>';
55b4d096 239 $output .= '</form>';
fcdcc372 240
55b4d096 241 if ($forcerefresh) {
242 $output .= $this->update_main_listing($submission);
243 }
fcdcc372 244
55b4d096 245 $responsefiles = $this->print_responsefiles($submission->userid, true);
246 if (!empty($responsefiles)) {
247 $output .= $responsefiles;
38ac07d2 248 }
55b4d096 249
250 if ($return) {
251 return $output;
38ac07d2 252 }
55b4d096 253 echo $output;
254 return;
255 }
fcdcc372 256
fcdcc372 257
55b4d096 258 function print_student_answer($userid, $return=false){
259 global $CFG;
fcdcc372 260
55b4d096 261 $filearea = $this->file_area_name($userid);
262 $submission = $this->get_submission($userid);
8ba4cbd2 263
55b4d096 264 $output = '';
8ba4cbd2 265
55b4d096 266 if ($basedir = $this->file_area($userid)) {
267 if (!$this->is_finalized($submission)) {
268 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
269 }
270
271 if ($this->notes_allowed() and !empty($submission->data1)) {
272 $output .= link_to_popup_window ('/mod/assignment/type/upload/notes.php?id='.$this->cm->id.'&amp;userid='.$userid,
273 'notes'.$userid, get_string('notes', 'assignment'), 500, 780, get_string('notes', 'assignment'), 'none', true, 'notesbutton'.$userid);
274 $output .= '&nbsp;';
275 }
276
277 if ($files = get_directory_list($basedir, 'responses')) {
278 foreach ($files as $key => $file) {
279 require_once($CFG->libdir.'/filelib.php');
280 $icon = mimeinfo('icon', $file);
281 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
a1c91f9a 282 $output .= '<a href="'.$ffurl.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>&nbsp;';
fcdcc372 283 }
fcdcc372 284 }
285 }
55b4d096 286 $output = '<div class="files">'.$output.'</div>';
287 $output .= '<br />';
8ba4cbd2 288
55b4d096 289 return $output;
fcdcc372 290 }
291
fcdcc372 292
55b4d096 293 /**
294 * Produces a list of links to the files uploaded by a user
295 *
296 * @param $userid int optional id of the user. If 0 then $USER->id is used.
297 * @param $return boolean optional defaults to false. If true the list is returned rather than printed
298 * @return string optional
299 */
300 function print_user_files($userid=0, $return=false) {
301 global $CFG, $USER;
302
303 $mode = optional_param('mode', '', PARAM_ALPHA);
304 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 305
55b4d096 306 if (!$userid) {
307 if (!isloggedin()) {
308 return '';
8ba4cbd2 309 }
55b4d096 310 $userid = $USER->id;
8ba4cbd2 311 }
312
55b4d096 313 $filearea = $this->file_area_name($userid);
8ba4cbd2 314
55b4d096 315 $output = '';
8ba4cbd2 316
55b4d096 317 if ($submission = $this->get_submission($userid)) {
8ba4cbd2 318
55b4d096 319 $candelete = $this->can_delete_files($submission);
320 $strdelete = get_string('delete');
8ba4cbd2 321
55b4d096 322 if (!$this->is_finalized($submission) and !empty($mode)) { // only during grading
323 $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
8ba4cbd2 324 }
8ba4cbd2 325
55b4d096 326 if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
fcdcc372 327
577d233d 328 $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
55b4d096 329 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
fcdcc372 330
55b4d096 331 }
fcdcc372 332
55b4d096 333 if ($basedir = $this->file_area($userid)) {
334 if ($files = get_directory_list($basedir, 'responses')) {
335 require_once($CFG->libdir.'/filelib.php');
336 foreach ($files as $key => $file) {
8ba4cbd2 337
55b4d096 338 $icon = mimeinfo('icon', $file);
8ba4cbd2 339
55b4d096 340 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
8ba4cbd2 341
8ba4cbd2 342
2bf7e0b1 343 $output .= '<a href="'.$ffurl.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.$file.'</a>';
fcdcc372 344
55b4d096 345 if ($candelete) {
346 $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 347
a1c91f9a 348 $output .= '<a href="'.$delurl.'">&nbsp;'
0d905d9f 349 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="" /></a> ';
8ba4cbd2 350 }
fcdcc372 351
55b4d096 352 $output .= '<br />';
6fc73d59 353 }
8ba4cbd2 354 }
55b4d096 355 }
ad1e3409 356 if (has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
357 if ($this->can_unfinalize($submission)) {
358 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
359 $output .= print_single_button('upload.php', $options, get_string('unfinalize', 'assignment'), 'post', '_self', true);
360 } else if ($this->can_finalize($submission)) {
361 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalize', 'mode'=>$mode, 'offset'=>$offset);
362 $output .= print_single_button('upload.php', $options, get_string('finalize', 'assignment'), 'post', '_self', true);
363 }
55b4d096 364 }
fcdcc372 365
55b4d096 366 $output = '<div class="files">'.$output.'</div>';
fcdcc372 367
55b4d096 368 }
8ba4cbd2 369
55b4d096 370 if ($return) {
371 return $output;
372 }
373 echo $output;
374 }
8ba4cbd2 375
55b4d096 376 function print_responsefiles($userid, $return=false) {
377 global $CFG, $USER;
8ba4cbd2 378
55b4d096 379 $mode = optional_param('mode', '', PARAM_ALPHA);
380 $offset = optional_param('offset', 0, PARAM_INT);
8ba4cbd2 381
55b4d096 382 $filearea = $this->file_area_name($userid).'/responses';
fcdcc372 383
55b4d096 384 $output = '';
fcdcc372 385
55b4d096 386 $candelete = $this->can_manage_responsefiles();
387 $strdelete = get_string('delete');
fcdcc372 388
55b4d096 389 if ($basedir = $this->file_area($userid)) {
390 $basedir .= '/responses';
fcdcc372 391
55b4d096 392 if ($files = get_directory_list($basedir)) {
393 require_once($CFG->libdir.'/filelib.php');
394 foreach ($files as $key => $file) {
fcdcc372 395
55b4d096 396 $icon = mimeinfo('icon', $file);
fcdcc372 397
55b4d096 398 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
fcdcc372 399
d9cf7323 400 $output .= '<a href="'.$ffurl.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>';
fcdcc372 401
55b4d096 402 if ($candelete) {
403 $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 404
a1c91f9a 405 $output .= '<a href="'.$delurl.'">&nbsp;'
0d905d9f 406 .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt=""/></a> ';
55b4d096 407 }
fcdcc372 408
55b4d096 409 $output .= '&nbsp;';
410 }
411 }
fcdcc372 412
fcdcc372 413
55b4d096 414 $output = '<div class="responsefiles">'.$output.'</div>';
415
8ba4cbd2 416 }
fcdcc372 417
55b4d096 418 if ($return) {
419 return $output;
8ba4cbd2 420 }
55b4d096 421 echo $output;
422 }
fcdcc372 423
fcdcc372 424
55b4d096 425 function upload() {
426 $action = required_param('action', PARAM_ALPHA);
fcdcc372 427
55b4d096 428 switch ($action) {
429 case 'finalize':
430 $this->finalize();
431 break;
432 case 'unfinalize':
433 $this->unfinalize();
434 break;
435 case 'uploadresponse':
436 $this->upload_responsefile();
437 break;
438 case 'uploadfile':
439 $this->upload_file();
440 case 'savenotes':
441 case 'editnotes':
442 $this->upload_notes();
443 default:
444 error('Error: Unknow upload action ('.$action.').');
8ba4cbd2 445 }
55b4d096 446 }
fcdcc372 447
55b4d096 448 function upload_notes() {
449 global $CFG, $USER;
fcdcc372 450
55b4d096 451 $action = required_param('action', PARAM_ALPHA);
fcdcc372 452
55b4d096 453 $returnurl = 'view.php?id='.$this->cm->id;
fcdcc372 454
1d284fbd 455 $mform = new mod_assignment_upload_notes_form();
6117edc7 456
457 $defaults = new object();
458 $defaults->id = $this->cm->id;
459
e38204d1 460 if ($submission = $this->get_submission($USER->id)) {
6117edc7 461 $defaults->text = $submission->data1;
e38204d1 462 } else {
6117edc7 463 $defaults->text = '';
e38204d1 464 }
465
beac4717 466 $mform->set_data($defaults);
6117edc7 467
5571b5f4 468 if ($mform->is_cancelled()) {
469 redirect('view.php?id='.$this->cm->id);
470 }
471
e38204d1 472 if (!$this->can_update_notes($submission)) {
473 $this->view_header(get_string('upload'));
474 notify(get_string('uploaderror', 'assignment'));
475 print_continue($returnurl);
476 $this->view_footer();
477 die;
478 }
479
beac4717 480 if ($data = $mform->get_data() and $action == 'savenotes') {
55b4d096 481 $submission = $this->get_submission($USER->id, true); // get or create submission
482 $updated = new object();
483 $updated->id = $submission->id;
484 $updated->timemodified = time();
6117edc7 485 $updated->data1 = $data->text;
fcdcc372 486
55b4d096 487 if (update_record('assignment_submissions', $updated)) {
488 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
489 redirect($returnurl);
490 } else {
491 $this->view_header(get_string('notes', 'assignment'));
492 notify(get_string('notesupdateerror', 'assignment'));
493 print_continue($returnurl);
494 $this->view_footer();
495 die;
496 }
497 }
fcdcc372 498
55b4d096 499 /// show notes edit form
55b4d096 500 $this->view_header(get_string('notes', 'assignment'));
6117edc7 501
a1c91f9a 502 print_heading(get_string('notes', 'assignment'), '');
6fc73d59 503
6117edc7 504 $mform->display();
8ba4cbd2 505
55b4d096 506 $this->view_footer();
507 die;
fcdcc372 508 }
509
55b4d096 510 function upload_responsefile() {
511 global $CFG;
8ba4cbd2 512
55b4d096 513 $userid = required_param('userid', PARAM_INT);
514 $mode = required_param('mode', PARAM_ALPHA);
515 $offset = required_param('offset', PARAM_INT);
fcdcc372 516
55b4d096 517 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 518
37aaf16c 519 if (data_submitted('nomatch') and $this->can_manage_responsefiles()) {
55b4d096 520 $dir = $this->file_area_name($userid).'/responses';
8d942514 521 check_dir_exists($CFG->dataroot.'/'.$dir, true, true);
8ba4cbd2 522
55b4d096 523 require_once($CFG->dirroot.'/lib/uploadlib.php');
524 $um = new upload_manager('newfile',false,true,$this->course,false,0,true);
525
526 if (!$um->process_file_uploads($dir)) {
527 print_header(get_string('upload'));
528 notify(get_string('uploaderror', 'assignment'));
529 echo $um->get_errors();
530 print_continue($returnurl);
531 print_footer('none');
532 die;
38ac07d2 533 }
fcdcc372 534 }
55b4d096 535 redirect($returnurl);
fcdcc372 536 }
537
55b4d096 538 function upload_file() {
fcdcc372 539 global $CFG, $USER;
8ba4cbd2 540
55b4d096 541 $mode = optional_param('mode', '', PARAM_ALPHA);
542 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 543
55b4d096 544 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 545
55b4d096 546 $filecount = $this->count_user_files($USER->id);
547 $submission = $this->get_submission($USER->id);
8ba4cbd2 548
55b4d096 549 if (!$this->can_upload_file($submission)) {
550 $this->view_header(get_string('upload'));
551 notify(get_string('uploaderror', 'assignment'));
552 print_continue($returnurl);
553 $this->view_footer();
554 die;
555 }
8ba4cbd2 556
55b4d096 557 $dir = $this->file_area_name($USER->id);
8d942514 558 check_dir_exists($CFG->dataroot.'/'.$dir, true, true); // better to create now so that student submissions do not block it later
fcdcc372 559
55b4d096 560 require_once($CFG->dirroot.'/lib/uploadlib.php');
561 $um = new upload_manager('newfile',false,true,$this->course,false,$this->assignment->maxbytes,true);
8ba4cbd2 562
55b4d096 563 if ($um->process_file_uploads($dir)) {
564 $submission = $this->get_submission($USER->id, true); //create new submission if needed
565 $updated = new object();
566 $updated->id = $submission->id;
567 $updated->timemodified = time();
568
569 if (update_record('assignment_submissions', $updated)) {
570 add_to_log($this->course->id, 'assignment', 'upload',
571 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
572 } else {
573 $new_filename = $um->get_new_filename();
574 $this->view_header(get_string('upload'));
575 notify(get_string('uploadnotregistered', 'assignment', $new_filename));
576 print_continue($returnurl);
577 $this->view_footer();
578 die;
fcdcc372 579 }
55b4d096 580 redirect('view.php?id='.$this->cm->id);
fcdcc372 581 }
55b4d096 582 $this->view_header(get_string('upload'));
583 notify(get_string('uploaderror', 'assignment'));
584 echo $um->get_errors();
585 print_continue($returnurl);
586 $this->view_footer();
587 die;
fcdcc372 588 }
589
55b4d096 590 function finalize() {
591 global $USER;
fcdcc372 592
ad1e3409 593 $userid = optional_param('userid', 0, PARAM_INT);
55b4d096 594
ad1e3409 595 if ($userid) {
596 $mode = required_param('mode', PARAM_ALPHA);
597 $offset = required_param('offset', PARAM_INT);
598 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
599 $confirm = true;
600 $submission = $this->get_submission($userid);
601
602 } else {
603 $confirm = optional_param('confirm', 0, PARAM_BOOL);
604 $returnurl = 'view.php?id='.$this->cm->id;
605 $submission = $this->get_submission($USER->id);
606 }
55b4d096 607
608 if (!$this->can_finalize($submission)) {
609 redirect($returnurl); // probably already graded, erdirect to assignment page, the reason should be obvious
610 }
611
37aaf16c 612 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 613 $optionsno = array('id'=>$this->cm->id);
614 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize');
615 $this->view_header(get_string('submitformarking', 'assignment'));
616 print_heading(get_string('submitformarking', 'assignment'));
617 notice_yesno(get_string('onceassignmentsent', 'assignment'), 'upload.php', 'view.php', $optionsyes, $optionsno, 'post', 'get');
618 $this->view_footer();
619 die;
620
621 } else {
622 $updated = new object();
ad1e3409 623 $updated->id = $submission->id;
624 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
55b4d096 625 $updated->timemodified = time();
626 if (update_record('assignment_submissions', $updated)) {
627 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finilize action to log
628 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
629 $this->email_teachers($submission);
630 } else {
631 $this->view_header(get_string('submitformarking', 'assignment'));
632 notify(get_string('finalizeerror', 'assignment'));
633 print_continue($returnurl);
634 $this->view_footer();
635 die;
fcdcc372 636 }
fcdcc372 637 }
55b4d096 638 redirect($returnurl);
639 }
fcdcc372 640
55b4d096 641 function unfinalize() {
8ba4cbd2 642
55b4d096 643 $userid = required_param('userid', PARAM_INT);
644 $mode = required_param('mode', PARAM_ALPHA);
645 $offset = required_param('offset', PARAM_INT);
fcdcc372 646
55b4d096 647 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
8ba4cbd2 648
37aaf16c 649 if (data_submitted('nomatch')
55b4d096 650 and $submission = $this->get_submission($userid)
651 and $this->can_unfinalize($submission)) {
fcdcc372 652
55b4d096 653 $updated = new object();
654 $updated->id = $submission->id;
655 $updated->data2 = '';
656 if (update_record('assignment_submissions', $updated)) {
657 //TODO: add unfinilize action to log
658 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
659 } else {
660 $this->view_header(get_string('submitformarking'));
661 notify(get_string('finalizeerror', 'assignment'));
662 print_continue($returnurl);
663 $this->view_footer();
664 die;
fcdcc372 665 }
666 }
55b4d096 667 redirect($returnurl);
668 }
fcdcc372 669
fcdcc372 670
55b4d096 671 function delete() {
672 $action = optional_param('action', '', PARAM_ALPHA);
fcdcc372 673
55b4d096 674 switch ($action) {
675 case 'response':
676 $this->delete_responsefile();
677 break;
678 default:
679 $this->delete_file();
fcdcc372 680 }
55b4d096 681 die;
682 }
fcdcc372 683
fcdcc372 684
55b4d096 685 function delete_responsefile() {
686 global $CFG;
8ba4cbd2 687
55b4d096 688 $file = required_param('file', PARAM_FILE);
689 $userid = required_param('userid', PARAM_INT);
690 $mode = required_param('mode', PARAM_ALPHA);
691 $offset = required_param('offset', PARAM_INT);
692 $confirm = optional_param('confirm', 0, PARAM_BOOL);
fcdcc372 693
55b4d096 694 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 695
55b4d096 696 if (!$this->can_manage_responsefiles()) {
697 redirect($returnurl);
698 }
fcdcc372 699
55b4d096 700 $urlreturn = 'submissions.php';
701 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
fcdcc372 702
37aaf16c 703 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 704 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset);
705 print_header(get_string('delete'));
706 print_heading(get_string('delete'));
707 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
708 print_footer('none');
709 die;
710 }
fcdcc372 711
55b4d096 712 $dir = $this->file_area_name($userid).'/responses';
713 $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
714 if (file_exists($filepath)) {
715 if (@unlink($filepath)) {
716 redirect($returnurl);
717 }
718 }
fcdcc372 719
55b4d096 720 // print delete error
721 print_header(get_string('delete'));
722 notify(get_string('deletefilefailed', 'assignment'));
723 print_continue($returnurl);
724 print_footer('none');
725 die;
fcdcc372 726
55b4d096 727 }
fcdcc372 728
fcdcc372 729
55b4d096 730 function delete_file() {
731 global $CFG;
fcdcc372 732
55b4d096 733 $file = required_param('file', PARAM_FILE);
734 $userid = required_param('userid', PARAM_INT);
735 $confirm = optional_param('confirm', 0, PARAM_BOOL);
736 $mode = optional_param('mode', '', PARAM_ALPHA);
737 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 738
55b4d096 739 require_login($this->course->id, false, $this->cm);
fcdcc372 740
55b4d096 741 if (empty($mode)) {
742 $urlreturn = 'view.php';
743 $optionsreturn = array('id'=>$this->cm->id);
744 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 745 } else {
55b4d096 746 $urlreturn = 'submissions.php';
747 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
748 $returnurl = "submissions.php?id={$this->cm->id}&amp;offset=$offset&amp;mode=$mode&amp;userid=$userid";
fcdcc372 749 }
750
55b4d096 751 if (!$submission = $this->get_submission($userid) // incorrect submission
752 or !$this->can_delete_files($submission)) { // can not delete
753 $this->view_header(get_string('delete'));
754 notify(get_string('cannotdeletefiles', 'assignment'));
755 print_continue($returnurl);
756 $this->view_footer();
757 die;
fcdcc372 758 }
55b4d096 759 $dir = $this->file_area_name($userid);
fcdcc372 760
37aaf16c 761 if (!data_submitted('nomatch') or !$confirm) {
55b4d096 762 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset);
763 if (empty($mode)) {
764 $this->view_header(get_string('delete'));
765 } else {
766 print_header(get_string('delete'));
fcdcc372 767 }
55b4d096 768 print_heading(get_string('delete'));
769 notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
770 if (empty($mode)) {
771 $this->view_footer();
772 } else {
773 print_footer('none');
fcdcc372 774 }
55b4d096 775 die;
fcdcc372 776 }
777
55b4d096 778 $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
779 if (file_exists($filepath)) {
780 if (@unlink($filepath)) {
781 $updated = new object();
782 $updated->id = $submission->id;
783 $updated->timemodified = time();
784 if (update_record('assignment_submissions', $updated)) {
785 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
786 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
787 }
788 redirect($returnurl);
789 }
fcdcc372 790 }
791
55b4d096 792 // print delete error
793 if (empty($mode)) {
794 $this->view_header(get_string('delete'));
795 } else {
796 print_header(get_string('delete'));
8ba4cbd2 797 }
55b4d096 798 notify(get_string('deletefilefailed', 'assignment'));
799 print_continue($returnurl);
800 if (empty($mode)) {
801 $this->view_footer();
802 } else {
803 print_footer('none');
8ba4cbd2 804 }
55b4d096 805 die;
806 }
8ba4cbd2 807
8ba4cbd2 808
55b4d096 809 function can_upload_file($submission) {
810 global $USER;
fcdcc372 811
55b4d096 812 if (has_capability('mod/assignment:submit', $this->context) // can submit
813 and $this->isopen() // assignment not closed yet
55b4d096 814 and (empty($submission) or $submission->userid == $USER->id) // his/her own submission
83774723 815 and $this->count_user_files($USER->id) < $this->assignment->var1 // file limit not reached
816 and !$this->is_finalized($submission)) { // no uploading after final submission
55b4d096 817 return true;
818 } else {
819 return false;
8ba4cbd2 820 }
55b4d096 821 }
fcdcc372 822
55b4d096 823 function can_manage_responsefiles() {
824 if (has_capability('mod/assignment:grade', $this->context)) {
825 return true;
826 } else {
827 return false;
828 }
829 }
fcdcc372 830
55b4d096 831 function can_delete_files($submission) {
832 global $USER;
fcdcc372 833
55b4d096 834 if (has_capability('mod/assignment:grade', $this->context)) {
835 return true;
fcdcc372 836 }
837
55b4d096 838 if (has_capability('mod/assignment:submit', $this->context)
839 and $this->isopen() // assignment not closed yet
55b4d096 840 and $this->assignment->resubmit // deleting allowed
841 and $USER->id == $submission->userid // his/her own submission
842 and !$this->is_finalized($submission)) { // no deleting after final submission
843 return true;
844 } else {
845 return false;
fcdcc372 846 }
55b4d096 847 }
fcdcc372 848
55b4d096 849 function is_finalized($submission) {
ad1e3409 850 if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED) {
55b4d096 851 return true;
852 } else {
853 return false;
fcdcc372 854 }
55b4d096 855 }
fcdcc372 856
55b4d096 857 function can_unfinalize($submission) {
858 if (has_capability('mod/assignment:grade', $this->context)
ad1e3409 859 and $this->isopen()
860 and $this->is_finalized($submission)) {
55b4d096 861 return true;
fcdcc372 862 } else {
55b4d096 863 return false;
fcdcc372 864 }
fcdcc372 865 }
866
55b4d096 867 function can_finalize($submission) {
868 global $USER;
8ba4cbd2 869
ad1e3409 870 if ($this->is_finalized($submission)) {
871 return false;
872 }
873
874 if (has_capability('mod/assignment:grade', $this->context)) {
875 return true;
876
877 } else if (has_capability('mod/assignment:submit', $this->context) // can submit
55b4d096 878 and $this->isopen() // assignment not closed yet
879 and !empty($submission) // submission must exist
55b4d096 880 and $submission->userid == $USER->id // his/her own submission
55b4d096 881 and ($this->count_user_files($USER->id)
ad1e3409 882 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
8ba4cbd2 883
55b4d096 884 return true;
8ba4cbd2 885 } else {
55b4d096 886 return false;
8ba4cbd2 887 }
55b4d096 888 }
8ba4cbd2 889
55b4d096 890 function can_update_notes($submission) {
891 global $USER;
8ba4cbd2 892
55b4d096 893 if (has_capability('mod/assignment:submit', $this->context)
ad1e3409 894 and $this->notes_allowed() // notesd must be allowed
55b4d096 895 and $this->isopen() // assignment not closed yet
55b4d096 896 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
897 and !$this->is_finalized($submission)) { // no updateingafter final submission
898 return true;
8ba4cbd2 899 } else {
55b4d096 900 return false;
8ba4cbd2 901 }
8ba4cbd2 902 }
903
55b4d096 904 function notes_allowed() {
905 return (boolean)$this->assignment->var2;
906 }
8ba4cbd2 907
55b4d096 908 /**
909 * Count the files uploaded by a given user
910 *
911 * @param $userid int The user id
912 * @return int
913 */
914 function count_user_files($userid) {
915 global $CFG;
8ba4cbd2 916
55b4d096 917 $filearea = $this->file_area_name($userid);
8ba4cbd2 918
55b4d096 919 if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
920 if ($files = get_directory_list($basedir, 'responses')) {
921 return count($files);
38ac07d2 922 }
fcdcc372 923 }
55b4d096 924 return 0;
925 }
8ba4cbd2 926
55b4d096 927 function count_responsefiles($userid) {
928 global $CFG;
8ba4cbd2 929
55b4d096 930 $filearea = $this->file_area_name($userid).'/responses';
8ba4cbd2 931
55b4d096 932 if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
933 $basedir .= '/responses';
934 if ($files = get_directory_list($basedir)) {
935 return count($files);
8ba4cbd2 936 }
fcdcc372 937 }
55b4d096 938 return 0;
38ac07d2 939 }
8ba4cbd2 940
436cfa9f 941 function setup_elements(&$mform) {
942 global $CFG, $COURSE;
943
944 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
945
946 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
436cfa9f 947 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
948 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
949 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
950
951 $mform->addElement('select', 'resubmit', get_string("allowdeleting", "assignment"), $ynoptions);
952 $mform->setHelpButton('resubmit', array('allowdeleting', get_string('allowdeleting', 'assignment'), 'assignment'));
953 $mform->setDefault('resubmit', 1);
954
955 $options = array();
956 for($i = 1; $i <= 20; $i++) {
957 $options[$i] = $i;
958 }
959 $mform->addElement('select', 'var1', get_string("allowmaxfiles", "assignment"), $options);
960 $mform->setHelpButton('var1', array('allowmaxfiles', get_string('allowmaxfiles', 'assignment'), 'assignment'));
961 $mform->setDefault('var1', 3);
962
963 $mform->addElement('select', 'var2', get_string("allownotes", "assignment"), $ynoptions);
964 $mform->setHelpButton('var2', array('allownotes', get_string('allownotes', 'assignment'), 'assignment'));
965 $mform->setDefault('var2', 0);
966
967 $mform->addElement('select', 'var3', get_string("hideintro", "assignment"), $ynoptions);
968 $mform->setHelpButton('var3', array('hideintro', get_string('hideintro', 'assignment'), 'assignment'));
969 $mform->setDefault('var3', 0);
970
971 $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
972 $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
973 $mform->setDefault('emailteachers', 0);
974
975
976 }
977
6117edc7 978}
979
f07b9627 980class mod_assignment_upload_notes_form extends moodleform {
6117edc7 981 function definition() {
982 $mform =& $this->_form;
983
984 // visible elements
985 $mform->addElement('htmleditor', 'text', get_string('notes', 'assignment'), array('cols'=>85, 'rows'=>30));
986 $mform->setType('text', PARAM_RAW); // to be cleaned before display
18a77361 987 $mform->setHelpButton('text', array('reading', 'writing'), false, 'editorhelpbutton');
55b4d096 988
6117edc7 989 // hidden params
990 $mform->addElement('hidden', 'id', 0);
991 $mform->setType('id', PARAM_INT);
992 $mform->addElement('hidden', 'action', 'savenotes');
993 $mform->setType('id', PARAM_ALPHA);
994
995 // buttons
a23f0aaf 996 $this->add_action_buttons();
6117edc7 997 }
fcdcc372 998}
8ba4cbd2 999
fcdcc372 1000?>