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