MDL-20904, upload function now upload to user private only, remove file_stroage code...
[moodle.git] / mod / assignment / type / upload / assignment.class.php
CommitLineData
e7521559 1<?php
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() {
867aeead 20 global $USER, $OUTPUT;
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
3a003ead 31 echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
29889a2a 32 print_string('notavailableyet', 'assignment');
3a003ead 33 echo $OUTPUT->box_end();
29889a2a 34 } else {
35 $this->view_intro();
36 }
37
55b4d096 38 $this->view_dates();
fcdcc372 39
64f93798
PS
40 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
41 if ($submission = $this->get_submission($USER->id)) {
42 $filecount = $this->count_user_files($submission->id);
43 } else {
44 $filecount = 0;
45 }
6fc73d59 46
55b4d096 47 $this->view_feedback();
6fc73d59 48
42f50aec 49 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
867aeead 50 echo $OUTPUT->heading(get_string('submission', 'assignment'), 3);
55b4d096 51 } else {
867aeead 52 echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
fcdcc372 53 }
fcdcc372 54
55b4d096 55 if ($filecount and $submission) {
610b5fde 56 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
55b4d096 57 } else {
39bc9fbb 58 if (!$this->isopen() or $this->is_finalized($submission)) {
15b45bdc 59 echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
55b4d096 60 } else {
15b45bdc 61 echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
55b4d096 62 }
63 }
6fc73d59 64
8ba4cbd2 65 $this->view_upload_form();
55b4d096 66
67 if ($this->notes_allowed()) {
867aeead 68 echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
55b4d096 69 $this->view_notes();
70 }
71
72 $this->view_final_submission();
8ba4cbd2 73 }
74 $this->view_footer();
fcdcc372 75 }
76
fcdcc372 77
55b4d096 78 function view_feedback($submission=NULL) {
867aeead 79 global $USER, $CFG, $DB, $OUTPUT;
12dce253 80 require_once($CFG->libdir.'/gradelib.php');
8ba4cbd2 81
55b4d096 82 if (!$submission) { /// Get submission for this assignment
83 $submission = $this->get_submission($USER->id);
84 }
8ba4cbd2 85
55b4d096 86 if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
87 if ($this->count_responsefiles($USER->id)) {
867aeead 88 echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
55b4d096 89 $responsefiles = $this->print_responsefiles($USER->id, true);
3a003ead 90 echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
55b4d096 91 }
92 return;
93 }
fcdcc372 94
12dce253 95 $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
96 $item = $grading_info->items[0];
97 $grade = $item->grades[$USER->id];
98
99 if ($grade->hidden or $grade->grade === false) { // hidden or error
100 return;
101 }
102
103 if ($grade->grade === null and empty($grade->str_feedback)) { /// Nothing to show yet
104 return;
105 }
106
107 $graded_date = $grade->dategraded;
108 $graded_by = $grade->usermodified;
109
55b4d096 110 /// We need the teacher info
74d7d735 111 if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
a939f681 112 print_error('cannotfindteacher');
55b4d096 113 }
fcdcc372 114
55b4d096 115 /// Print the feedback
867aeead 116 echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
8ba4cbd2 117
55b4d096 118 echo '<table cellspacing="0" class="feedback">';
fcdcc372 119
55b4d096 120 echo '<tr>';
121 echo '<td class="left picture">';
812dbaf7 122 echo $OUTPUT->user_picture($teacher);
55b4d096 123 echo '</td>';
124 echo '<td class="topic">';
125 echo '<div class="from">';
126 echo '<div class="fullname">'.fullname($teacher).'</div>';
12dce253 127 echo '<div class="time">'.userdate($graded_date).'</div>';
55b4d096 128 echo '</div>';
129 echo '</td>';
130 echo '</tr>';
fcdcc372 131
55b4d096 132 echo '<tr>';
133 echo '<td class="left side">&nbsp;</td>';
134 echo '<td class="content">';
135 if ($this->assignment->grade) {
136 echo '<div class="grade">';
12dce253 137 echo get_string("grade").': '.$grade->str_long_grade;
55b4d096 138 echo '</div>';
139 echo '<div class="clearer"></div>';
140 }
8ba4cbd2 141
55b4d096 142 echo '<div class="comment">';
12dce253 143 echo $grade->str_feedback;
55b4d096 144 echo '</div>';
145 echo '</tr>';
8ba4cbd2 146
55b4d096 147 echo '<tr>';
148 echo '<td class="left side">&nbsp;</td>';
149 echo '<td class="content">';
150 echo $this->print_responsefiles($USER->id, true);
151 echo '</tr>';
8ba4cbd2 152
55b4d096 153 echo '</table>';
fcdcc372 154 }
155
fcdcc372 156
55b4d096 157 function view_upload_form() {
158 global $CFG, $USER;
fcdcc372 159
8ba4cbd2 160 $submission = $this->get_submission($USER->id);
fcdcc372 161
55b4d096 162 if ($this->is_finalized($submission)) {
163 // no uploading
164 return;
165 }
8ba4cbd2 166
55b4d096 167 if ($this->can_upload_file($submission)) {
172dd12c 168 $mform = new mod_assignment_upload_file_form('upload.php', $this);
a6e1750a
PM
169 echo "<div class=\"uploadbox\">";
170 $mform->display();
171 echo "</div>";
fcdcc372 172 }
8ba4cbd2 173
55b4d096 174 }
8ba4cbd2 175
55b4d096 176 function view_notes() {
3a003ead 177 global $USER, $OUTPUT;
55b4d096 178
179 if ($submission = $this->get_submission($USER->id)
180 and !empty($submission->data1)) {
3a003ead 181 echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML), 'generalbox boxaligncenter boxwidthwide');
55b4d096 182 } else {
3a003ead 183 echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
55b4d096 184 }
185 if ($this->can_update_notes($submission)) {
186 $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
d9cb14b8 187 echo '<div style="text-align:center">';
5c2ed7e2 188 echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
d9cb14b8 189 echo '</div>';
55b4d096 190 }
fcdcc372 191 }
192
55b4d096 193 function view_final_submission() {
867aeead 194 global $CFG, $USER, $OUTPUT;
fcdcc372 195
8ba4cbd2 196 $submission = $this->get_submission($USER->id);
fcdcc372 197
39bc9fbb 198 if ($this->isopen() and $this->can_finalize($submission)) {
55b4d096 199 //print final submit button
867aeead 200 echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
d9cb14b8 201 echo '<div style="text-align:center">';
55b4d096 202 echo '<form method="post" action="upload.php">';
d9cb14b8 203 echo '<fieldset class="invisiblefieldset">';
55b4d096 204 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
a19dffc0 205 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
55b4d096 206 echo '<input type="hidden" name="action" value="finalize" />';
207 echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
d9cb14b8 208 echo '</fieldset>';
55b4d096 209 echo '</form>';
d9cb14b8 210 echo '</div>';
39bc9fbb 211 } else if (!$this->isopen()) {
867aeead 212 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
39bc9fbb 213
42f50aec 214 } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
39bc9fbb 215 if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
867aeead 216 echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
39bc9fbb 217 } else {
867aeead 218 echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
39bc9fbb 219 }
55b4d096 220 } else {
221 //no submission yet
222 }
223 }
39bc9fbb 224
225
226 /**
dc6cb74e 227 * Return true if var3 == hide description till available day
39bc9fbb 228 *
dc6cb74e 229 *@return boolean
39bc9fbb 230 */
dc6cb74e 231 function description_is_hidden() {
232 return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
233 }
fcdcc372 234
55b4d096 235 function custom_feedbackform($submission, $return=false) {
236 global $CFG;
8ba4cbd2 237
55b4d096 238 $mode = optional_param('mode', '', PARAM_ALPHA);
239 $offset = optional_param('offset', 0, PARAM_INT);
240 $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
8ba4cbd2 241
172dd12c 242 $mform = new mod_assignment_upload_response_form("$CFG->wwwroot/mod/assignment/upload.php", $this);
243
244 $mform->set_data(array('id'=>$this->cm->id, 'offset'=>$offset, 'forcerefresh'=>$forcerefresh, 'userid'=>$submission->userid, 'mode'=>$mode));
245
55b4d096 246 $output = get_string('responsefiles', 'assignment').': ';
8ba4cbd2 247
172dd12c 248 ob_start();
249 $mform->display();
250 $output = ob_get_clean();
fcdcc372 251
55b4d096 252 if ($forcerefresh) {
253 $output .= $this->update_main_listing($submission);
254 }
fcdcc372 255
55b4d096 256 $responsefiles = $this->print_responsefiles($submission->userid, true);
257 if (!empty($responsefiles)) {
258 $output .= $responsefiles;
38ac07d2 259 }
55b4d096 260
261 if ($return) {
262 return $output;
38ac07d2 263 }
55b4d096 264 echo $output;
265 return;
266 }
fcdcc372 267
fcdcc372 268
55b4d096 269 function print_student_answer($userid, $return=false){
d436d197 270 global $CFG, $OUTPUT;
fcdcc372 271
55b4d096 272 $submission = $this->get_submission($userid);
8ba4cbd2 273
55b4d096 274 $output = '';
8ba4cbd2 275
172dd12c 276 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
277 $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
278 }
55b4d096 279
172dd12c 280 if ($this->notes_allowed() and !empty($submission->data1)) {
3ea5951e 281 $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
9bf16314
PS
282 $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
283 $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
e7521559 284
172dd12c 285 $output .= '&nbsp;';
286 }
55b4d096 287
172dd12c 288 $fs = get_file_storage();
289 $browser = get_file_browser();
290
64f93798 291 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
172dd12c 292
293 foreach ($files as $file) {
294 $filename = $file->get_filename();
295 $found = true;
296 $mimetype = $file->get_mimetype();
64f93798 297 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
b5d0cafc 298 $output .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a>&nbsp;';
fcdcc372 299 }
172dd12c 300
fcdcc372 301 }
55b4d096 302 $output = '<div class="files">'.$output.'</div>';
303 $output .= '<br />';
8ba4cbd2 304
55b4d096 305 return $output;
fcdcc372 306 }
307
fcdcc372 308
55b4d096 309 /**
310 * Produces a list of links to the files uploaded by a user
311 *
312 * @param $userid int optional id of the user. If 0 then $USER->id is used.
313 * @param $return boolean optional defaults to false. If true the list is returned rather than printed
314 * @return string optional
315 */
316 function print_user_files($userid=0, $return=false) {
f2a1963c 317 global $CFG, $USER, $OUTPUT;
55b4d096 318
319 $mode = optional_param('mode', '', PARAM_ALPHA);
320 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 321
55b4d096 322 if (!$userid) {
323 if (!isloggedin()) {
324 return '';
8ba4cbd2 325 }
55b4d096 326 $userid = $USER->id;
8ba4cbd2 327 }
328
55b4d096 329 $output = '';
8ba4cbd2 330
39bc9fbb 331 $submission = $this->get_submission($userid);
8ba4cbd2 332
39bc9fbb 333 $candelete = $this->can_delete_files($submission);
334 $strdelete = get_string('delete');
8ba4cbd2 335
42f50aec 336 if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) { // only during grading
39bc9fbb 337 $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
338 }
fcdcc372 339
39bc9fbb 340 if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
fcdcc372 341
39bc9fbb 342 $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
343 $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
fcdcc372 344
39bc9fbb 345 }
8ba4cbd2 346
172dd12c 347 $fs = get_file_storage();
348 $browser = get_file_browser();
349
64f93798 350 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
0d06b6fd 351 $button = new portfolio_add_button();
172dd12c 352 foreach ($files as $file) {
353 $filename = $file->get_filename();
354 $mimetype = $file->get_mimetype();
64f93798 355 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
b5d0cafc 356 $output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';
617ac19b 357
172dd12c 358 if ($candelete) {
d992f53d 359 $delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;submissionid={$submission->id}&amp;mode=$mode&amp;offset=$offset";
617ac19b 360
172dd12c 361 $output .= '<a href="'.$delurl.'">&nbsp;'
b5d0cafc 362 .'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="" /></a> ';
8ba4cbd2 363 }
617ac19b 364
728e1931 365 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
5fb29115 366 $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php');
887160c7 367 $button->set_format_by_file($file);
0d06b6fd 368 $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
67a87e7d 369 }
172dd12c 370 $output .= '<br />';
371 }
0d06b6fd 372 if (count($files) > 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) {
5fb29115 373 $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php');
59dd457e 374 $button->reset_formats(); // reset what we set before, since it's multi-file
0d06b6fd 375 $output .= $button->to_html();
55b4d096 376 }
39bc9fbb 377 }
03076be4 378
379 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 380 if ($this->can_unfinalize($submission)) {
381 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
5c2ed7e2 382 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('unfinalize', 'assignment'));
39bc9fbb 383 } else if ($this->can_finalize($submission)) {
384 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
5c2ed7e2 385 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('finalize', 'assignment'));
55b4d096 386 }
55b4d096 387 }
8ba4cbd2 388
39bc9fbb 389 $output = '<div class="files">'.$output.'</div>';
390
55b4d096 391 if ($return) {
392 return $output;
393 }
394 echo $output;
395 }
8ba4cbd2 396
55b4d096 397 function print_responsefiles($userid, $return=false) {
f2a1963c 398 global $CFG, $USER, $OUTPUT;
8ba4cbd2 399
55b4d096 400 $mode = optional_param('mode', '', PARAM_ALPHA);
401 $offset = optional_param('offset', 0, PARAM_INT);
8ba4cbd2 402
55b4d096 403 $output = '';
fcdcc372 404
55b4d096 405 $candelete = $this->can_manage_responsefiles();
406 $strdelete = get_string('delete');
fcdcc372 407
172dd12c 408 $fs = get_file_storage();
409 $browser = get_file_browser();
fcdcc372 410
64f93798
PS
411 if ($submission = $this->get_submission($userid)) {
412 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "timemodified", false)) {
413 foreach ($files as $file) {
414 $filename = $file->get_filename();
415 $found = true;
416 $mimetype = $file->get_mimetype();
417 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/response/'.$submission->id.'/'.$filename);
fcdcc372 418
64f93798 419 $output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.$filename.'</a>';
fcdcc372 420
64f93798
PS
421 if ($candelete) {
422 $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 423
64f93798
PS
424 $output .= '<a href="'.$delurl.'">&nbsp;'
425 .'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt=""/></a> ';
426 }
427
428 $output .= '&nbsp;';
55b4d096 429 }
fcdcc372 430
64f93798 431 $output = '<div class="responsefiles">'.$output.'</div>';
172dd12c 432 }
8ba4cbd2 433 }
fcdcc372 434
55b4d096 435 if ($return) {
436 return $output;
8ba4cbd2 437 }
55b4d096 438 echo $output;
439 }
fcdcc372 440
fcdcc372 441
55b4d096 442 function upload() {
443 $action = required_param('action', PARAM_ALPHA);
fcdcc372 444
55b4d096 445 switch ($action) {
446 case 'finalize':
447 $this->finalize();
448 break;
39bc9fbb 449 case 'finalizeclose':
03076be4 450 $this->finalizeclose();
39bc9fbb 451 break;
55b4d096 452 case 'unfinalize':
453 $this->unfinalize();
454 break;
455 case 'uploadresponse':
456 $this->upload_responsefile();
457 break;
458 case 'uploadfile':
459 $this->upload_file();
460 case 'savenotes':
461 case 'editnotes':
462 $this->upload_notes();
463 default:
a939f681 464 print_error('unknowuploadaction', '', '', $action);
8ba4cbd2 465 }
55b4d096 466 }
fcdcc372 467
55b4d096 468 function upload_notes() {
c561f44c 469 global $CFG, $USER, $OUTPUT, $DB;
fcdcc372 470
55b4d096 471 $action = required_param('action', PARAM_ALPHA);
fcdcc372 472
55b4d096 473 $returnurl = 'view.php?id='.$this->cm->id;
fcdcc372 474
1d284fbd 475 $mform = new mod_assignment_upload_notes_form();
6117edc7 476
477 $defaults = new object();
478 $defaults->id = $this->cm->id;
479
e38204d1 480 if ($submission = $this->get_submission($USER->id)) {
cc27235e 481 $defaults->text = clean_text($submission->data1);
e38204d1 482 } else {
6117edc7 483 $defaults->text = '';
e38204d1 484 }
485
beac4717 486 $mform->set_data($defaults);
6117edc7 487
5571b5f4 488 if ($mform->is_cancelled()) {
489 redirect('view.php?id='.$this->cm->id);
490 }
491
e38204d1 492 if (!$this->can_update_notes($submission)) {
493 $this->view_header(get_string('upload'));
3a003ead 494 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
495 echo $OUTPUT->continue_button($returnurl);
e38204d1 496 $this->view_footer();
497 die;
498 }
499
294ce987 500 if ($data = $mform->get_data() and $action == 'savenotes') {
55b4d096 501 $submission = $this->get_submission($USER->id, true); // get or create submission
502 $updated = new object();
503 $updated->id = $submission->id;
504 $updated->timemodified = time();
6117edc7 505 $updated->data1 = $data->text;
fcdcc372 506
74d7d735 507 if ($DB->update_record('assignment_submissions', $updated)) {
55b4d096 508 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
509 redirect($returnurl);
12dce253 510 $submission = $this->get_submission($USER->id);
511 $this->update_grade($submission);
512
55b4d096 513 } else {
514 $this->view_header(get_string('notes', 'assignment'));
3a003ead 515 echo $OUTPUT->notification(get_string('notesupdateerror', 'assignment'));
516 echo $OUTPUT->continue_button($returnurl);
55b4d096 517 $this->view_footer();
518 die;
519 }
520 }
fcdcc372 521
55b4d096 522 /// show notes edit form
55b4d096 523 $this->view_header(get_string('notes', 'assignment'));
6117edc7 524
867aeead 525 echo $OUTPUT->heading(get_string('notes', 'assignment'));
6fc73d59 526
6117edc7 527 $mform->display();
8ba4cbd2 528
55b4d096 529 $this->view_footer();
530 die;
fcdcc372 531 }
532
55b4d096 533 function upload_responsefile() {
83a7f058 534 global $CFG, $USER, $OUTPUT, $PAGE;
8ba4cbd2 535
55b4d096 536 $userid = required_param('userid', PARAM_INT);
537 $mode = required_param('mode', PARAM_ALPHA);
538 $offset = required_param('offset', PARAM_INT);
fcdcc372 539
55b4d096 540 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
fcdcc372 541
172dd12c 542 $mform = new mod_assignment_upload_response_form(null, $this);
543 if ($mform->get_data() and $this->can_manage_responsefiles()) {
544 $fs = get_file_storage();
545 $filename = $mform->get_new_filename('newfile');
546 if ($filename !== false) {
64f93798
PS
547 $submission = $this->get_submission($userid, true, true);
548 if (!$fs->file_exists($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $filename)) {
549 if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'response', $submission->id, '/', $filename, false, $USER->id)) {
172dd12c 550 redirect($returnurl);
551 }
552 }
38ac07d2 553 }
fcdcc372 554 }
83a7f058 555 $PAGE->set_title(get_string('upload'));
556 echo $OUTPUT->header();
3a003ead 557 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
558 echo $OUTPUT->continue_button($returnurl);
256d2850 559 echo $OUTPUT->footer();
172dd12c 560 die;
fcdcc372 561 }
562
55b4d096 563 function upload_file() {
3a003ead 564 global $CFG, $USER, $DB, $OUTPUT;
8ba4cbd2 565
55b4d096 566 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 567
55b4d096 568 $filecount = $this->count_user_files($USER->id);
569 $submission = $this->get_submission($USER->id);
8ba4cbd2 570
55b4d096 571 if (!$this->can_upload_file($submission)) {
572 $this->view_header(get_string('upload'));
3a003ead 573 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
574 echo $OUTPUT->continue_button($returnurl);
55b4d096 575 $this->view_footer();
576 die;
577 }
8ba4cbd2 578
172dd12c 579 $mform = new mod_assignment_upload_file_form('upload.php', $this);
580 if ($mform->get_data()) {
581 $fs = get_file_storage();
582 $filename = $mform->get_new_filename('newfile');
583 if ($filename !== false) {
172dd12c 584 $submission = $this->get_submission($USER->id, true); //create new submission if needed
64f93798
PS
585 if (!$fs->file_exists($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename)) {
586 if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename, false, $USER->id)) {
d992f53d
MD
587 $updates = new object();
588 $updates->id = $submission->id;
589 $updates->timemodified = time();
590 if ($DB->update_record('assignment_submissions', $updates)) {
172dd12c 591 add_to_log($this->course->id, 'assignment', 'upload',
592 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
593 $this->update_grade($submission);
594 if (!$this->drafts_tracked()) {
595 $this->email_teachers($submission);
596 }
dcffbe54
MD
597
598 // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
1c5517a5 599 $eventdata = new object();
dcffbe54
MD
600 $eventdata->modulename = 'assignment';
601 $eventdata->cmid = $this->cm->id;
602 $eventdata->itemid = $submission->id;
603 $eventdata->courseid = $this->course->id;
604 $eventdata->userid = $USER->id;
605 $eventdata->file = $file;
606 events_trigger('assessable_file_uploaded', $eventdata);
1c5517a5 607
172dd12c 608 redirect('view.php?id='.$this->cm->id);
609 } else {
610 $file->delete();
611 }
612 }
91d8d9a0 613 }
fcdcc372 614 }
615 }
172dd12c 616
55b4d096 617 $this->view_header(get_string('upload'));
3a003ead 618 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
619 echo $OUTPUT->continue_button($returnurl);
55b4d096 620 $this->view_footer();
621 die;
fcdcc372 622 }
623
172dd12c 624 function send_file($filearea, $args) {
625 global $CFG, $DB, $USER;
626 require_once($CFG->libdir.'/filelib.php');
627
628 require_login($this->course, false, $this->cm);
629
64f93798
PS
630 if ($filearea === 'submission') {
631 $submissionid = (int)array_shift($args);
172dd12c 632
64f93798
PS
633 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
634 return false;
635 }
172dd12c 636
64f93798
PS
637 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
638 return false;
639 }
640
641 $relativepath = implode('/', $args);
642 $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
172dd12c 643
64f93798
PS
644 $fs = get_file_storage();
645 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
172dd12c 646 return false;
647 }
64f93798
PS
648 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
649
650 } else if ($filearea === 'response') {
651 $submissionid = (int)array_shift($args);
172dd12c 652
64f93798 653 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
172dd12c 654 return false;
655 }
656
64f93798
PS
657 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
658 return false;
659 }
660
661 $relativepath = implode('/', $args);
662 $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
663
664 $fs = get_file_storage();
665 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
666 return false;
667 }
668 send_stored_file($file, 0, 0, true);
172dd12c 669 }
670
64f93798 671 return false;
172dd12c 672 }
673
03076be4 674 function finalize() {
867aeead 675 global $USER, $DB, $OUTPUT;
fcdcc372 676
03076be4 677 $confirm = optional_param('confirm', 0, PARAM_BOOL);
678 $returnurl = 'view.php?id='.$this->cm->id;
679 $submission = $this->get_submission($USER->id);
55b4d096 680
681 if (!$this->can_finalize($submission)) {
39bc9fbb 682 redirect($returnurl); // probably already graded, redirect to assignment page, the reason should be obvious
55b4d096 683 }
684
a19dffc0 685 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
55b4d096 686 $optionsno = array('id'=>$this->cm->id);
a19dffc0 687 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
55b4d096 688 $this->view_header(get_string('submitformarking', 'assignment'));
867aeead 689 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
75d47fa9 690 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
55b4d096 691 $this->view_footer();
692 die;
693
03076be4 694 }
695 $updated = new object();
696 $updated->id = $submission->id;
697 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
698 $updated->timemodified = time();
699
74d7d735 700 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 701 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
702 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 703 $submission = $this->get_submission($USER->id);
704 $this->update_grade($submission);
03076be4 705 $this->email_teachers($submission);
55b4d096 706 } else {
03076be4 707 $this->view_header(get_string('submitformarking', 'assignment'));
3a003ead 708 echo $OUTPUT->notification(get_string('finalizeerror', 'assignment'));
709 echo $OUTPUT->continue_button($returnurl);
03076be4 710 $this->view_footer();
711 die;
712 }
dcffbe54
MD
713
714 // Trigger assessable_files_done event to show files are complete
1c5517a5 715 $eventdata = new object();
dcffbe54
MD
716 $eventdata->modulename = 'assignment';
717 $eventdata->cmid = $this->cm->id;
718 $eventdata->itemid = $submission->id;
719 $eventdata->courseid = $this->course->id;
720 $eventdata->userid = $USER->id;
721 events_trigger('assessable_files_done', $eventdata);
1c5517a5 722
03076be4 723 redirect($returnurl);
724 }
39bc9fbb 725
03076be4 726 function finalizeclose() {
74d7d735 727 global $DB;
728
03076be4 729 $userid = optional_param('userid', 0, PARAM_INT);
730 $mode = required_param('mode', PARAM_ALPHA);
731 $offset = required_param('offset', PARAM_INT);
732 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
733
12dce253 734 // create but do not add student submission date
03076be4 735 $submission = $this->get_submission($userid, true, true);
736
a19dffc0 737 if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
03076be4 738 redirect($returnurl); // probably closed already
739 }
740
741 $updated = new object();
742 $updated->id = $submission->id;
743 $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
744
74d7d735 745 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 746 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
747 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 748 $submission = $this->get_submission($userid, false, true);
749 $this->update_grade($submission);
fcdcc372 750 }
55b4d096 751 redirect($returnurl);
752 }
fcdcc372 753
55b4d096 754 function unfinalize() {
74d7d735 755 global $DB;
8ba4cbd2 756
55b4d096 757 $userid = required_param('userid', PARAM_INT);
758 $mode = required_param('mode', PARAM_ALPHA);
759 $offset = required_param('offset', PARAM_INT);
fcdcc372 760
55b4d096 761 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
8ba4cbd2 762
74d7d735 763 if (data_submitted()
55b4d096 764 and $submission = $this->get_submission($userid)
a19dffc0
PS
765 and $this->can_unfinalize($submission)
766 and confirm_sesskey()) {
fcdcc372 767
55b4d096 768 $updated = new object();
769 $updated->id = $submission->id;
770 $updated->data2 = '';
74d7d735 771 if ($DB->update_record('assignment_submissions', $updated)) {
39bc9fbb 772 //TODO: add unfinalize action to log
55b4d096 773 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 774 $submission = $this->get_submission($userid);
775 $this->update_grade($submission);
55b4d096 776 } else {
0de80bcf 777 $this->view_header(get_string('submitformarking', 'assignment'));
3a003ead 778 echo $OUTPUT->notification(get_string('unfinalizeerror', 'assignment'));
779 echo $OUTPUT->continue_button($returnurl);
55b4d096 780 $this->view_footer();
781 die;
fcdcc372 782 }
783 }
55b4d096 784 redirect($returnurl);
785 }
fcdcc372 786
fcdcc372 787
55b4d096 788 function delete() {
789 $action = optional_param('action', '', PARAM_ALPHA);
fcdcc372 790
55b4d096 791 switch ($action) {
792 case 'response':
793 $this->delete_responsefile();
794 break;
795 default:
796 $this->delete_file();
fcdcc372 797 }
55b4d096 798 die;
799 }
fcdcc372 800
fcdcc372 801
55b4d096 802 function delete_responsefile() {
83a7f058 803 global $CFG, $OUTPUT,$PAGE;
8ba4cbd2 804
55b4d096 805 $file = required_param('file', PARAM_FILE);
806 $userid = required_param('userid', PARAM_INT);
807 $mode = required_param('mode', PARAM_ALPHA);
808 $offset = required_param('offset', PARAM_INT);
809 $confirm = optional_param('confirm', 0, PARAM_BOOL);
fcdcc372 810
75d47fa9 811 $returnurl = "submissions.php?id={$this->cm->id}&userid=$userid&mode=$mode&offset=$offset";
fcdcc372 812
55b4d096 813 if (!$this->can_manage_responsefiles()) {
814 redirect($returnurl);
815 }
fcdcc372 816
55b4d096 817 $urlreturn = 'submissions.php';
818 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
fcdcc372 819
a19dffc0
PS
820 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
821 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
83a7f058 822 $PAGE->set_title(get_string('delete'));
823 echo $OUTPUT->header();
867aeead 824 echo $OUTPUT->heading(get_string('delete'));
75d47fa9 825 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
256d2850 826 echo $OUTPUT->footer();
55b4d096 827 die;
828 }
fcdcc372 829
64f93798
PS
830 if ($submission = $this->get_submission($userid)) {
831 $fs = get_file_storage();
832 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
833 $file->delete();
55b4d096 834 }
835 }
64f93798 836 redirect($returnurl);
55b4d096 837 }
fcdcc372 838
fcdcc372 839
55b4d096 840 function delete_file() {
55c7042d 841 global $CFG, $DB, $OUTPUT, $PAGE;
fcdcc372 842
55b4d096 843 $file = required_param('file', PARAM_FILE);
844 $userid = required_param('userid', PARAM_INT);
845 $confirm = optional_param('confirm', 0, PARAM_BOOL);
846 $mode = optional_param('mode', '', PARAM_ALPHA);
847 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 848
55b4d096 849 require_login($this->course->id, false, $this->cm);
fcdcc372 850
55b4d096 851 if (empty($mode)) {
852 $urlreturn = 'view.php';
853 $optionsreturn = array('id'=>$this->cm->id);
854 $returnurl = 'view.php?id='.$this->cm->id;
8ba4cbd2 855 } else {
55b4d096 856 $urlreturn = 'submissions.php';
857 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
75d47fa9 858 $returnurl = "submissions.php?id={$this->cm->id}&offset=$offset&mode=$mode&userid=$userid";
fcdcc372 859 }
860
55b4d096 861 if (!$submission = $this->get_submission($userid) // incorrect submission
862 or !$this->can_delete_files($submission)) { // can not delete
863 $this->view_header(get_string('delete'));
3a003ead 864 echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
865 echo $OUTPUT->continue_button($returnurl);
55b4d096 866 $this->view_footer();
867 die;
fcdcc372 868 }
869
a19dffc0 870 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
64f93798 871 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
55b4d096 872 if (empty($mode)) {
873 $this->view_header(get_string('delete'));
874 } else {
55c7042d 875 $PAGE->set_title(get_string('delete'));
876 echo $OUTPUT->header();
fcdcc372 877 }
867aeead 878 echo $OUTPUT->heading(get_string('delete'));
75d47fa9 879 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
55b4d096 880 if (empty($mode)) {
881 $this->view_footer();
882 } else {
256d2850 883 echo $OUTPUT->footer();
fcdcc372 884 }
55b4d096 885 die;
fcdcc372 886 }
887
172dd12c 888 $fs = get_file_storage();
64f93798
PS
889 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
890 $file->delete();
891 $submission->timemodified = time();
892 $DB->update_record('assignment_submissions', $submission);
893 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
894 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
895 $this->update_grade($submission);
8ba4cbd2 896 }
64f93798 897 redirect($returnurl);
55b4d096 898 }
8ba4cbd2 899
8ba4cbd2 900
55b4d096 901 function can_upload_file($submission) {
902 global $USER;
fcdcc372 903
64f93798 904 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 905 and $this->isopen() // assignment not closed yet
55b4d096 906 and (empty($submission) or $submission->userid == $USER->id) // his/her own submission
83774723 907 and $this->count_user_files($USER->id) < $this->assignment->var1 // file limit not reached
908 and !$this->is_finalized($submission)) { // no uploading after final submission
55b4d096 909 return true;
910 } else {
911 return false;
8ba4cbd2 912 }
55b4d096 913 }
fcdcc372 914
55b4d096 915 function can_manage_responsefiles() {
916 if (has_capability('mod/assignment:grade', $this->context)) {
917 return true;
918 } else {
919 return false;
920 }
921 }
fcdcc372 922
55b4d096 923 function can_delete_files($submission) {
924 global $USER;
fcdcc372 925
55b4d096 926 if (has_capability('mod/assignment:grade', $this->context)) {
927 return true;
fcdcc372 928 }
929
64f93798 930 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 931 and $this->isopen() // assignment not closed yet
55b4d096 932 and $this->assignment->resubmit // deleting allowed
933 and $USER->id == $submission->userid // his/her own submission
934 and !$this->is_finalized($submission)) { // no deleting after final submission
935 return true;
936 } else {
937 return false;
fcdcc372 938 }
55b4d096 939 }
fcdcc372 940
42f50aec 941 function drafts_tracked() {
942 return !empty($this->assignment->var4);
943 }
944
39bc9fbb 945 /**
946 * Returns submission status
947 * @param object $submission - may be empty
948 * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
949 */
55b4d096 950 function is_finalized($submission) {
42f50aec 951 if (!$this->drafts_tracked()) {
952 return '';
953
954 } else if (empty($submission)) {
39bc9fbb 955 return '';
956
957 } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
958 return $submission->data2;
959
55b4d096 960 } else {
39bc9fbb 961 return '';
fcdcc372 962 }
55b4d096 963 }
fcdcc372 964
55b4d096 965 function can_unfinalize($submission) {
42f50aec 966 if (!$this->drafts_tracked()) {
967 return false;
968 }
55b4d096 969 if (has_capability('mod/assignment:grade', $this->context)
ad1e3409 970 and $this->isopen()
971 and $this->is_finalized($submission)) {
55b4d096 972 return true;
fcdcc372 973 } else {
55b4d096 974 return false;
fcdcc372 975 }
fcdcc372 976 }
977
55b4d096 978 function can_finalize($submission) {
979 global $USER;
42f50aec 980 if (!$this->drafts_tracked()) {
981 return false;
982 }
8ba4cbd2 983
4454447d
PS
984 if ($this->is_finalized($submission)) {
985 return false;
986 }
ad1e3409 987
988 if (has_capability('mod/assignment:grade', $this->context)) {
989 return true;
990
64f93798 991 } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 992 and $this->isopen() // assignment not closed yet
993 and !empty($submission) // submission must exist
55b4d096 994 and $submission->userid == $USER->id // his/her own submission
55b4d096 995 and ($this->count_user_files($USER->id)
ad1e3409 996 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
8ba4cbd2 997
55b4d096 998 return true;
8ba4cbd2 999 } else {
55b4d096 1000 return false;
8ba4cbd2 1001 }
55b4d096 1002 }
8ba4cbd2 1003
55b4d096 1004 function can_update_notes($submission) {
1005 global $USER;
8ba4cbd2 1006
64f93798 1007 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
ad1e3409 1008 and $this->notes_allowed() // notesd must be allowed
55b4d096 1009 and $this->isopen() // assignment not closed yet
55b4d096 1010 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
1011 and !$this->is_finalized($submission)) { // no updateingafter final submission
1012 return true;
8ba4cbd2 1013 } else {
55b4d096 1014 return false;
8ba4cbd2 1015 }
8ba4cbd2 1016 }
1017
55b4d096 1018 function notes_allowed() {
1019 return (boolean)$this->assignment->var2;
1020 }
8ba4cbd2 1021
55b4d096 1022 function count_responsefiles($userid) {
64f93798
PS
1023 if ($submission = $this->get_submission($userid)) {
1024 $fs = get_file_storage();
1025 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
1026 return count($files);
1027 } else {
1028 return 0;
1029 }
38ac07d2 1030 }
8ba4cbd2 1031
436cfa9f 1032 function setup_elements(&$mform) {
675be221 1033 global $CFG, $COURSE;
436cfa9f 1034
1035 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1036
1037 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
436cfa9f 1038 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1039 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1040 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1041
e66841b9
DM
1042 $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1043 $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
436cfa9f 1044 $mform->setDefault('resubmit', 1);
1045
1046 $options = array();
1047 for($i = 1; $i <= 20; $i++) {
1048 $options[$i] = $i;
1049 }
e66841b9
DM
1050 $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1051 $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
436cfa9f 1052 $mform->setDefault('var1', 3);
1053
e66841b9
DM
1054 $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1055 $mform->addHelpButton('var2', 'allownotes', 'assignment');
436cfa9f 1056 $mform->setDefault('var2', 0);
1057
e66841b9
DM
1058 $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1059 $mform->addHelpButton('var3', 'hideintro', 'assignment');
436cfa9f 1060 $mform->setDefault('var3', 0);
1061
e66841b9
DM
1062 $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1063 $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
436cfa9f 1064 $mform->setDefault('emailteachers', 0);
1065
e66841b9
DM
1066 $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1067 $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
26087ac5 1068 $mform->setDefault('var4', 1);
436cfa9f 1069
1070 }
1071
67a87e7d 1072 function portfolio_exportable() {
1073 return true;
1074 }
1075
47c96a77 1076 function extend_settings_navigation($node) {
1077 global $CFG, $USER, $OUTPUT;
1078
1079 // get users submission if there is one
1080 $submission = $this->get_submission();
64f93798 1081 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
47c96a77 1082 $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1083 } else {
1084 $editable = false;
1085 }
1086
1087 // If the user has submitted something add a bit more stuff
1088 if ($submission) {
1089 // Add a view link to the settings nav
a6855934 1090 $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
47c96a77 1091 $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1092 if (!empty($submission->timemodified)) {
3406acde
SH
1093 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1094 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1095 $submittednode->add_class('note');
47c96a77 1096 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
3406acde 1097 $submittednode->add_class('early');
47c96a77 1098 } else {
3406acde 1099 $submittednode->add_class('late');
47c96a77 1100 }
1101 }
1102 }
1103
1104 // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
64f93798 1105 if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($USER->id)) {
47c96a77 1106 $fs = get_file_storage();
64f93798 1107 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
47c96a77 1108 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
3406acde 1109 $filenode = $node->add(get_string('submission', 'assignment'));
47c96a77 1110 } else {
3406acde 1111 $filenode = $node->add(get_string('submissiondraft', 'assignment'));
47c96a77 1112 }
1113 foreach ($files as $file) {
1114 $filename = $file->get_filename();
1115 $mimetype = $file->get_mimetype();
64f93798 1116 $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
3406acde 1117 $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
47c96a77 1118 }
1119 }
1120 }
1121
1122 // Show a notes link if they are enabled
1123 if ($this->notes_allowed()) {
a6855934 1124 $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
47c96a77 1125 $node->add(get_string('notes', 'assignment'), $link);
1126 }
1127 }
b37ce778
DM
1128
1129 /**
1130 * creates a zip of all assignment submissions and sends a zip to the browser
1131 */
1132 public function download_submissions() {
1133 global $CFG,$DB;
1134 require_once($CFG->libdir.'/filelib.php');
b37ce778
DM
1135 $submissions = $this->get_submissions('','');
1136 if (empty($submissions)) {
1137 error("there are no submissions to download");
1138 }
1139 $filesforzipping = array();
1140 $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
b37ce778
DM
1141 $fs = get_file_storage();
1142
1143 $groupmode = groupmode($this->course,$this->cm);
1144 $groupid = 0; // All users
1145 $groupname = '';
1146 if($groupmode) {
1147 $group = get_current_group($this->course->id, true);
1148 $groupid = $group->id;
1149 $groupname = $group->name.'-';
1150 }
1151 $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1152 foreach ($submissions as $submission) {
1153 $a_userid = $submission->userid; //get userid
1154 if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1155 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1156 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
64f93798
PS
1157
1158 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
b37ce778
DM
1159 foreach ($files as $file) {
1160 //get files new name.
1161 $fileforzipname = $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
b37ce778 1162 //save file name to array for zipping.
6db11242 1163 $filesforzipping[$fileforzipname] = $file;
b37ce778
DM
1164 }
1165 }
1166 } // end of foreach loop
1167 if ($zipfile = assignment_pack_files($filesforzipping)) {
b37ce778
DM
1168 send_temp_file($zipfile, $filename); //send file and delete after sending.
1169 }
1170 }
6117edc7 1171}
1172
f07b9627 1173class mod_assignment_upload_notes_form extends moodleform {
c561f44c
SH
1174
1175 function get_data() {
1176 $data = parent::get_data();
1177 if ($data) {
1178 $data->format = $data->text['format'];
1179 $data->text = $data->text['text'];
1180 }
1181 return $data;
1182 }
1183
1184 function set_data($data) {
1185 if (!isset($data->format)) {
1186 $data->format = FORMAT_HTML;
1187 }
1188 if (isset($data->text)) {
1189 $data->text = array('text'=>$data->text, 'format'=>$data->format);
1190 }
1191 parent::set_data($data);
1192 }
1193
6117edc7 1194 function definition() {
172dd12c 1195 $mform = $this->_form;
6117edc7 1196
1197 // visible elements
c561f44c 1198 $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
6117edc7 1199 $mform->setType('text', PARAM_RAW); // to be cleaned before display
55b4d096 1200
6117edc7 1201 // hidden params
1202 $mform->addElement('hidden', 'id', 0);
1203 $mform->setType('id', PARAM_INT);
1204 $mform->addElement('hidden', 'action', 'savenotes');
172dd12c 1205 $mform->setType('action', PARAM_ALPHA);
6117edc7 1206
1207 // buttons
a23f0aaf 1208 $this->add_action_buttons();
6117edc7 1209 }
fcdcc372 1210}
8ba4cbd2 1211
172dd12c 1212class mod_assignment_upload_response_form extends moodleform {
1213 function definition() {
1214 $mform = $this->_form;
1215 $instance = $this->_customdata;
1216
1217 // visible elements
1218 $mform->addElement('file', 'newfile', get_string('uploadafile'));
1219
1220 // hidden params
1221 $mform->addElement('hidden', 'id', $instance->cm->id);
1222 $mform->setType('id', PARAM_INT);
1223 $mform->addElement('hidden', 'action', 'uploadresponse');
1224 $mform->setType('action', PARAM_ALPHA);
1225 $mform->addElement('hidden', 'mode');
1226 $mform->setType('mode', PARAM_ALPHA);
1227 $mform->addElement('hidden', 'offset');
1228 $mform->setType('offset', PARAM_INT);
1229 $mform->addElement('hidden', 'forcerefresh');
1230 $mform->setType('forcerefresh', PARAM_INT);
1231 $mform->addElement('hidden', 'userid');
1232 $mform->setType('userid', PARAM_INT);
1233
1234 // buttons
1235 $this->add_action_buttons(false, get_string('uploadthisfile'));
1236 }
1237}
1238
67a87e7d 1239
1240
e7521559 1241