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