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