assignment MDL-22609 notice when no submission yet in single upload grading. - fixed.
[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)) {
370 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
16d8bacb 371 $output .= '<form></form>'; //@TODO: see bug MDL-22893 this is a temporary workaround for a broken - form nesting isn't valid!
5c2ed7e2 372 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('unfinalize', 'assignment'));
39bc9fbb 373 } else if ($this->can_finalize($submission)) {
374 $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
16d8bacb 375 $output .= '<form></form>'; //@TODO: see bug MDL-22893 this is a temporary workaround for a broken - form nesting isn't valid!
5c2ed7e2 376 $output .= $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('finalize', 'assignment'));
55b4d096 377 }
55b4d096 378 }
8ba4cbd2 379
ce2a1c4d 380 $renderer = $PAGE->get_renderer('mod_assignment');
ce2a1c4d
DC
381 $output .= $renderer->assignment_files($this->context, $submission->id);
382 $output .= $OUTPUT->box_end();
39bc9fbb 383
55b4d096 384 if ($return) {
385 return $output;
386 }
387 echo $output;
388 }
8ba4cbd2 389
55b4d096 390 function print_responsefiles($userid, $return=false) {
ce2a1c4d 391 global $CFG, $USER, $OUTPUT, $PAGE;
8ba4cbd2 392
55b4d096 393 $mode = optional_param('mode', '', PARAM_ALPHA);
394 $offset = optional_param('offset', 0, PARAM_INT);
8ba4cbd2 395
e525b6f1 396 $output = $OUTPUT->box_start('responsefiles');
fcdcc372 397
55b4d096 398 $candelete = $this->can_manage_responsefiles();
399 $strdelete = get_string('delete');
fcdcc372 400
172dd12c 401 $fs = get_file_storage();
402 $browser = get_file_browser();
fcdcc372 403
64f93798 404 if ($submission = $this->get_submission($userid)) {
ce2a1c4d 405 $renderer = $PAGE->get_renderer('mod_assignment');
be534597 406 $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
ce2a1c4d 407 $output .= $OUTPUT->box_end();
8ba4cbd2 408 }
fcdcc372 409
55b4d096 410 if ($return) {
411 return $output;
8ba4cbd2 412 }
55b4d096 413 echo $output;
414 }
fcdcc372 415
fcdcc372 416
ce2a1c4d
DC
417 /**
418 * Upload files
419 * upload_file function requires moodle form instance and file manager options
420 * @param object $mform
421 * @param array $options
422 */
423 function upload($mform = null, $filemanager_options = null) {
55b4d096 424 $action = required_param('action', PARAM_ALPHA);
55b4d096 425 switch ($action) {
426 case 'finalize':
427 $this->finalize();
428 break;
39bc9fbb 429 case 'finalizeclose':
03076be4 430 $this->finalizeclose();
39bc9fbb 431 break;
55b4d096 432 case 'unfinalize':
433 $this->unfinalize();
434 break;
435 case 'uploadresponse':
6ad4b78f 436 $this->upload_responsefile($mform, $filemanager_options);
55b4d096 437 break;
438 case 'uploadfile':
ce2a1c4d 439 $this->upload_file($mform, $filemanager_options);
55b4d096 440 case 'savenotes':
441 case 'editnotes':
442 $this->upload_notes();
443 default:
a939f681 444 print_error('unknowuploadaction', '', '', $action);
8ba4cbd2 445 }
55b4d096 446 }
fcdcc372 447
55b4d096 448 function upload_notes() {
c561f44c 449 global $CFG, $USER, $OUTPUT, $DB;
fcdcc372 450
55b4d096 451 $action = required_param('action', PARAM_ALPHA);
fcdcc372 452
ce2a1c4d 453 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
fcdcc372 454
1d284fbd 455 $mform = new mod_assignment_upload_notes_form();
6117edc7 456
457 $defaults = new object();
458 $defaults->id = $this->cm->id;
459
e38204d1 460 if ($submission = $this->get_submission($USER->id)) {
cc27235e 461 $defaults->text = clean_text($submission->data1);
e38204d1 462 } else {
6117edc7 463 $defaults->text = '';
e38204d1 464 }
465
beac4717 466 $mform->set_data($defaults);
6117edc7 467
5571b5f4 468 if ($mform->is_cancelled()) {
ce2a1c4d
DC
469 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
470 redirect($returnurl);
5571b5f4 471 }
472
e38204d1 473 if (!$this->can_update_notes($submission)) {
474 $this->view_header(get_string('upload'));
3a003ead 475 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
476 echo $OUTPUT->continue_button($returnurl);
e38204d1 477 $this->view_footer();
478 die;
479 }
480
294ce987 481 if ($data = $mform->get_data() and $action == 'savenotes') {
55b4d096 482 $submission = $this->get_submission($USER->id, true); // get or create submission
483 $updated = new object();
484 $updated->id = $submission->id;
485 $updated->timemodified = time();
6117edc7 486 $updated->data1 = $data->text;
fcdcc372 487
74d7d735 488 if ($DB->update_record('assignment_submissions', $updated)) {
55b4d096 489 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
490 redirect($returnurl);
12dce253 491 $submission = $this->get_submission($USER->id);
492 $this->update_grade($submission);
493
55b4d096 494 } else {
495 $this->view_header(get_string('notes', 'assignment'));
3a003ead 496 echo $OUTPUT->notification(get_string('notesupdateerror', 'assignment'));
497 echo $OUTPUT->continue_button($returnurl);
55b4d096 498 $this->view_footer();
499 die;
500 }
501 }
fcdcc372 502
55b4d096 503 /// show notes edit form
55b4d096 504 $this->view_header(get_string('notes', 'assignment'));
6117edc7 505
867aeead 506 echo $OUTPUT->heading(get_string('notes', 'assignment'));
6fc73d59 507
6117edc7 508 $mform->display();
8ba4cbd2 509
55b4d096 510 $this->view_footer();
511 die;
fcdcc372 512 }
513
6ad4b78f 514 function upload_responsefile($mform, $options) {
83a7f058 515 global $CFG, $USER, $OUTPUT, $PAGE;
8ba4cbd2 516
55b4d096 517 $userid = required_param('userid', PARAM_INT);
518 $mode = required_param('mode', PARAM_ALPHA);
519 $offset = required_param('offset', PARAM_INT);
fcdcc372 520
e1d4da05 521 $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
6ad4b78f 522
523 if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
172dd12c 524 $fs = get_file_storage();
6ad4b78f 525 $submission = $this->get_submission($userid, true, true);
526 if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
527 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
528 redirect($returnurl->out(false));
38ac07d2 529 }
fcdcc372 530 }
83a7f058 531 $PAGE->set_title(get_string('upload'));
532 echo $OUTPUT->header();
3a003ead 533 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
be534597 534 echo $OUTPUT->continue_button($returnurl->out(true));
256d2850 535 echo $OUTPUT->footer();
172dd12c 536 die;
fcdcc372 537 }
538
ce2a1c4d 539 function upload_file($mform, $options) {
3a003ead 540 global $CFG, $USER, $DB, $OUTPUT;
8ba4cbd2 541
ce2a1c4d 542 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
55b4d096 543 $submission = $this->get_submission($USER->id);
87f702a0 544 $filecount = $this->count_user_files($submission->id);
8ba4cbd2 545
55b4d096 546 if (!$this->can_upload_file($submission)) {
547 $this->view_header(get_string('upload'));
3a003ead 548 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
549 echo $OUTPUT->continue_button($returnurl);
55b4d096 550 $this->view_footer();
551 die;
552 }
8ba4cbd2 553
ce2a1c4d 554 if ($formdata = $mform->get_data()) {
172dd12c 555 $fs = get_file_storage();
ce2a1c4d
DC
556 $submission = $this->get_submission($USER->id, true); //create new submission if needed
557 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
558 $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
559 $updates = new object();
560 $updates->id = $submission->id;
561 $updates->timemodified = time();
562 if ($DB->update_record('assignment_submissions', $updates)) {
563 add_to_log($this->course->id, 'assignment', 'upload',
564 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
565 $this->update_grade($submission);
566 if (!$this->drafts_tracked()) {
567 $this->email_teachers($submission);
91d8d9a0 568 }
ce2a1c4d
DC
569
570 // send files to event system
571 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
572 // Let Moodle know that assessable files were uploaded (eg for plagiarism detection)
573 $eventdata = new object();
574 $eventdata->modulename = 'assignment';
575 $eventdata->cmid = $this->cm->id;
576 $eventdata->itemid = $submission->id;
577 $eventdata->courseid = $this->course->id;
578 $eventdata->userid = $USER->id;
579 if ($files) {
580 $eventdata->files = $files;
581 }
582 events_trigger('assessable_file_uploaded', $eventdata);
fcdcc372 583 }
ce2a1c4d
DC
584 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
585 redirect($returnurl);
fcdcc372 586 }
172dd12c 587
55b4d096 588 $this->view_header(get_string('upload'));
3a003ead 589 echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
590 echo $OUTPUT->continue_button($returnurl);
55b4d096 591 $this->view_footer();
592 die;
fcdcc372 593 }
594
172dd12c 595 function send_file($filearea, $args) {
596 global $CFG, $DB, $USER;
597 require_once($CFG->libdir.'/filelib.php');
598
599 require_login($this->course, false, $this->cm);
600
64f93798
PS
601 if ($filearea === 'submission') {
602 $submissionid = (int)array_shift($args);
172dd12c 603
64f93798
PS
604 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
605 return false;
606 }
172dd12c 607
64f93798
PS
608 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
609 return false;
610 }
611
612 $relativepath = implode('/', $args);
613 $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
172dd12c 614
64f93798
PS
615 $fs = get_file_storage();
616 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
172dd12c 617 return false;
618 }
64f93798
PS
619 send_stored_file($file, 0, 0, true); // download MUST be forced - security!
620
621 } else if ($filearea === 'response') {
622 $submissionid = (int)array_shift($args);
172dd12c 623
64f93798 624 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
172dd12c 625 return false;
626 }
627
64f93798
PS
628 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
629 return false;
630 }
631
632 $relativepath = implode('/', $args);
633 $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
634
635 $fs = get_file_storage();
636 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
637 return false;
638 }
639 send_stored_file($file, 0, 0, true);
172dd12c 640 }
641
64f93798 642 return false;
172dd12c 643 }
644
03076be4 645 function finalize() {
867aeead 646 global $USER, $DB, $OUTPUT;
fcdcc372 647
03076be4 648 $confirm = optional_param('confirm', 0, PARAM_BOOL);
ce2a1c4d 649 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
03076be4 650 $submission = $this->get_submission($USER->id);
55b4d096 651
652 if (!$this->can_finalize($submission)) {
39bc9fbb 653 redirect($returnurl); // probably already graded, redirect to assignment page, the reason should be obvious
55b4d096 654 }
655
a19dffc0 656 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
55b4d096 657 $optionsno = array('id'=>$this->cm->id);
a19dffc0 658 $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
55b4d096 659 $this->view_header(get_string('submitformarking', 'assignment'));
867aeead 660 echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
75d47fa9 661 echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
55b4d096 662 $this->view_footer();
663 die;
664
03076be4 665 }
666 $updated = new object();
667 $updated->id = $submission->id;
668 $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
669 $updated->timemodified = time();
670
74d7d735 671 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 672 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
673 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 674 $submission = $this->get_submission($USER->id);
675 $this->update_grade($submission);
03076be4 676 $this->email_teachers($submission);
55b4d096 677 } else {
03076be4 678 $this->view_header(get_string('submitformarking', 'assignment'));
3a003ead 679 echo $OUTPUT->notification(get_string('finalizeerror', 'assignment'));
680 echo $OUTPUT->continue_button($returnurl);
03076be4 681 $this->view_footer();
682 die;
683 }
dcffbe54
MD
684
685 // Trigger assessable_files_done event to show files are complete
1c5517a5 686 $eventdata = new object();
dcffbe54
MD
687 $eventdata->modulename = 'assignment';
688 $eventdata->cmid = $this->cm->id;
689 $eventdata->itemid = $submission->id;
690 $eventdata->courseid = $this->course->id;
691 $eventdata->userid = $USER->id;
692 events_trigger('assessable_files_done', $eventdata);
1c5517a5 693
03076be4 694 redirect($returnurl);
695 }
39bc9fbb 696
03076be4 697 function finalizeclose() {
74d7d735 698 global $DB;
699
03076be4 700 $userid = optional_param('userid', 0, PARAM_INT);
701 $mode = required_param('mode', PARAM_ALPHA);
702 $offset = required_param('offset', PARAM_INT);
ce2a1c4d 703 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
03076be4 704
12dce253 705 // create but do not add student submission date
03076be4 706 $submission = $this->get_submission($userid, true, true);
707
a19dffc0 708 if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
03076be4 709 redirect($returnurl); // probably closed already
710 }
711
712 $updated = new object();
713 $updated->id = $submission->id;
714 $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
715
74d7d735 716 if ($DB->update_record('assignment_submissions', $updated)) {
03076be4 717 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
718 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 719 $submission = $this->get_submission($userid, false, true);
720 $this->update_grade($submission);
fcdcc372 721 }
55b4d096 722 redirect($returnurl);
723 }
fcdcc372 724
55b4d096 725 function unfinalize() {
74d7d735 726 global $DB;
8ba4cbd2 727
55b4d096 728 $userid = required_param('userid', PARAM_INT);
729 $mode = required_param('mode', PARAM_ALPHA);
730 $offset = required_param('offset', PARAM_INT);
fcdcc372 731
55b4d096 732 $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
74d7d735 733 if (data_submitted()
55b4d096 734 and $submission = $this->get_submission($userid)
a19dffc0
PS
735 and $this->can_unfinalize($submission)
736 and confirm_sesskey()) {
fcdcc372 737
55b4d096 738 $updated = new object();
739 $updated->id = $submission->id;
740 $updated->data2 = '';
74d7d735 741 if ($DB->update_record('assignment_submissions', $updated)) {
39bc9fbb 742 //TODO: add unfinalize action to log
55b4d096 743 add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
12dce253 744 $submission = $this->get_submission($userid);
745 $this->update_grade($submission);
55b4d096 746 } else {
0de80bcf 747 $this->view_header(get_string('submitformarking', 'assignment'));
3a003ead 748 echo $OUTPUT->notification(get_string('unfinalizeerror', 'assignment'));
749 echo $OUTPUT->continue_button($returnurl);
55b4d096 750 $this->view_footer();
751 die;
fcdcc372 752 }
753 }
55b4d096 754 redirect($returnurl);
755 }
fcdcc372 756
fcdcc372 757
55b4d096 758 function delete() {
759 $action = optional_param('action', '', PARAM_ALPHA);
fcdcc372 760
55b4d096 761 switch ($action) {
762 case 'response':
763 $this->delete_responsefile();
764 break;
765 default:
766 $this->delete_file();
fcdcc372 767 }
55b4d096 768 die;
769 }
fcdcc372 770
fcdcc372 771
55b4d096 772 function delete_responsefile() {
83a7f058 773 global $CFG, $OUTPUT,$PAGE;
8ba4cbd2 774
55b4d096 775 $file = required_param('file', PARAM_FILE);
776 $userid = required_param('userid', PARAM_INT);
777 $mode = required_param('mode', PARAM_ALPHA);
778 $offset = required_param('offset', PARAM_INT);
779 $confirm = optional_param('confirm', 0, PARAM_BOOL);
fcdcc372 780
ce2a1c4d 781 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
fcdcc372 782
55b4d096 783 if (!$this->can_manage_responsefiles()) {
784 redirect($returnurl);
785 }
fcdcc372 786
55b4d096 787 $urlreturn = 'submissions.php';
788 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
fcdcc372 789
a19dffc0
PS
790 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
791 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
83a7f058 792 $PAGE->set_title(get_string('delete'));
793 echo $OUTPUT->header();
867aeead 794 echo $OUTPUT->heading(get_string('delete'));
75d47fa9 795 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
256d2850 796 echo $OUTPUT->footer();
55b4d096 797 die;
798 }
fcdcc372 799
64f93798
PS
800 if ($submission = $this->get_submission($userid)) {
801 $fs = get_file_storage();
802 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
803 $file->delete();
55b4d096 804 }
805 }
64f93798 806 redirect($returnurl);
55b4d096 807 }
fcdcc372 808
fcdcc372 809
55b4d096 810 function delete_file() {
55c7042d 811 global $CFG, $DB, $OUTPUT, $PAGE;
fcdcc372 812
55b4d096 813 $file = required_param('file', PARAM_FILE);
814 $userid = required_param('userid', PARAM_INT);
815 $confirm = optional_param('confirm', 0, PARAM_BOOL);
816 $mode = optional_param('mode', '', PARAM_ALPHA);
817 $offset = optional_param('offset', 0, PARAM_INT);
fcdcc372 818
55b4d096 819 require_login($this->course->id, false, $this->cm);
fcdcc372 820
55b4d096 821 if (empty($mode)) {
822 $urlreturn = 'view.php';
823 $optionsreturn = array('id'=>$this->cm->id);
ce2a1c4d 824 $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
8ba4cbd2 825 } else {
55b4d096 826 $urlreturn = 'submissions.php';
827 $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
ce2a1c4d 828 $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
fcdcc372 829 }
830
55b4d096 831 if (!$submission = $this->get_submission($userid) // incorrect submission
832 or !$this->can_delete_files($submission)) { // can not delete
833 $this->view_header(get_string('delete'));
3a003ead 834 echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
835 echo $OUTPUT->continue_button($returnurl);
55b4d096 836 $this->view_footer();
837 die;
fcdcc372 838 }
839
a19dffc0 840 if (!data_submitted() or !$confirm or !confirm_sesskey()) {
64f93798 841 $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
55b4d096 842 if (empty($mode)) {
843 $this->view_header(get_string('delete'));
844 } else {
55c7042d 845 $PAGE->set_title(get_string('delete'));
846 echo $OUTPUT->header();
fcdcc372 847 }
867aeead 848 echo $OUTPUT->heading(get_string('delete'));
75d47fa9 849 echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
55b4d096 850 if (empty($mode)) {
851 $this->view_footer();
852 } else {
256d2850 853 echo $OUTPUT->footer();
fcdcc372 854 }
55b4d096 855 die;
fcdcc372 856 }
857
172dd12c 858 $fs = get_file_storage();
64f93798
PS
859 if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
860 $file->delete();
861 $submission->timemodified = time();
862 $DB->update_record('assignment_submissions', $submission);
863 add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
864 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
865 $this->update_grade($submission);
8ba4cbd2 866 }
64f93798 867 redirect($returnurl);
55b4d096 868 }
8ba4cbd2 869
8ba4cbd2 870
55b4d096 871 function can_upload_file($submission) {
872 global $USER;
fcdcc372 873
64f93798 874 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 875 and $this->isopen() // assignment not closed yet
55b4d096 876 and (empty($submission) or $submission->userid == $USER->id) // his/her own submission
87f702a0 877 and $this->count_user_files($submission->id) < $this->assignment->var1 // file limit not reached
83774723 878 and !$this->is_finalized($submission)) { // no uploading after final submission
55b4d096 879 return true;
880 } else {
881 return false;
8ba4cbd2 882 }
55b4d096 883 }
fcdcc372 884
55b4d096 885 function can_manage_responsefiles() {
886 if (has_capability('mod/assignment:grade', $this->context)) {
887 return true;
888 } else {
889 return false;
890 }
891 }
fcdcc372 892
55b4d096 893 function can_delete_files($submission) {
894 global $USER;
fcdcc372 895
55b4d096 896 if (has_capability('mod/assignment:grade', $this->context)) {
897 return true;
fcdcc372 898 }
899
64f93798 900 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 901 and $this->isopen() // assignment not closed yet
55b4d096 902 and $this->assignment->resubmit // deleting allowed
903 and $USER->id == $submission->userid // his/her own submission
904 and !$this->is_finalized($submission)) { // no deleting after final submission
905 return true;
906 } else {
907 return false;
fcdcc372 908 }
55b4d096 909 }
fcdcc372 910
42f50aec 911 function drafts_tracked() {
912 return !empty($this->assignment->var4);
913 }
914
39bc9fbb 915 /**
916 * Returns submission status
917 * @param object $submission - may be empty
918 * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
919 */
55b4d096 920 function is_finalized($submission) {
42f50aec 921 if (!$this->drafts_tracked()) {
922 return '';
923
924 } else if (empty($submission)) {
39bc9fbb 925 return '';
926
927 } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
928 return $submission->data2;
929
55b4d096 930 } else {
39bc9fbb 931 return '';
fcdcc372 932 }
55b4d096 933 }
fcdcc372 934
55b4d096 935 function can_unfinalize($submission) {
42f50aec 936 if (!$this->drafts_tracked()) {
937 return false;
938 }
55b4d096 939 if (has_capability('mod/assignment:grade', $this->context)
ad1e3409 940 and $this->isopen()
941 and $this->is_finalized($submission)) {
55b4d096 942 return true;
fcdcc372 943 } else {
55b4d096 944 return false;
fcdcc372 945 }
fcdcc372 946 }
947
55b4d096 948 function can_finalize($submission) {
949 global $USER;
42f50aec 950 if (!$this->drafts_tracked()) {
951 return false;
952 }
8ba4cbd2 953
4454447d
PS
954 if ($this->is_finalized($submission)) {
955 return false;
956 }
ad1e3409 957
958 if (has_capability('mod/assignment:grade', $this->context)) {
959 return true;
960
64f93798 961 } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
55b4d096 962 and $this->isopen() // assignment not closed yet
963 and !empty($submission) // submission must exist
55b4d096 964 and $submission->userid == $USER->id // his/her own submission
87f702a0 965 and ($this->count_user_files($submission->id)
ad1e3409 966 or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
8ba4cbd2 967
55b4d096 968 return true;
8ba4cbd2 969 } else {
55b4d096 970 return false;
8ba4cbd2 971 }
55b4d096 972 }
8ba4cbd2 973
55b4d096 974 function can_update_notes($submission) {
975 global $USER;
8ba4cbd2 976
64f93798 977 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
ad1e3409 978 and $this->notes_allowed() // notesd must be allowed
55b4d096 979 and $this->isopen() // assignment not closed yet
55b4d096 980 and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
981 and !$this->is_finalized($submission)) { // no updateingafter final submission
982 return true;
8ba4cbd2 983 } else {
55b4d096 984 return false;
8ba4cbd2 985 }
8ba4cbd2 986 }
987
55b4d096 988 function notes_allowed() {
989 return (boolean)$this->assignment->var2;
990 }
8ba4cbd2 991
55b4d096 992 function count_responsefiles($userid) {
64f93798
PS
993 if ($submission = $this->get_submission($userid)) {
994 $fs = get_file_storage();
995 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
996 return count($files);
997 } else {
998 return 0;
999 }
38ac07d2 1000 }
8ba4cbd2 1001
436cfa9f 1002 function setup_elements(&$mform) {
675be221 1003 global $CFG, $COURSE;
436cfa9f 1004
1005 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1006
1007 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
436cfa9f 1008 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1009 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1010 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1011
e66841b9
DM
1012 $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
1013 $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
436cfa9f 1014 $mform->setDefault('resubmit', 1);
1015
1016 $options = array();
1017 for($i = 1; $i <= 20; $i++) {
1018 $options[$i] = $i;
1019 }
e66841b9
DM
1020 $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
1021 $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
436cfa9f 1022 $mform->setDefault('var1', 3);
1023
e66841b9
DM
1024 $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
1025 $mform->addHelpButton('var2', 'allownotes', 'assignment');
436cfa9f 1026 $mform->setDefault('var2', 0);
1027
e66841b9
DM
1028 $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
1029 $mform->addHelpButton('var3', 'hideintro', 'assignment');
436cfa9f 1030 $mform->setDefault('var3', 0);
1031
e66841b9
DM
1032 $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
1033 $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
436cfa9f 1034 $mform->setDefault('emailteachers', 0);
1035
e66841b9
DM
1036 $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
1037 $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
26087ac5 1038 $mform->setDefault('var4', 1);
436cfa9f 1039
1040 }
1041
67a87e7d 1042 function portfolio_exportable() {
1043 return true;
1044 }
1045
47c96a77 1046 function extend_settings_navigation($node) {
1047 global $CFG, $USER, $OUTPUT;
1048
1049 // get users submission if there is one
1050 $submission = $this->get_submission();
64f93798 1051 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
47c96a77 1052 $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
1053 } else {
1054 $editable = false;
1055 }
1056
1057 // If the user has submitted something add a bit more stuff
1058 if ($submission) {
1059 // Add a view link to the settings nav
a6855934 1060 $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
47c96a77 1061 $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
1062 if (!empty($submission->timemodified)) {
3406acde
SH
1063 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
1064 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
1065 $submittednode->add_class('note');
47c96a77 1066 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
3406acde 1067 $submittednode->add_class('early');
47c96a77 1068 } else {
3406acde 1069 $submittednode->add_class('late');
47c96a77 1070 }
1071 }
1072 }
1073
1074 // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
87f702a0 1075 if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
47c96a77 1076 $fs = get_file_storage();
64f93798 1077 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
47c96a77 1078 if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
3406acde 1079 $filenode = $node->add(get_string('submission', 'assignment'));
47c96a77 1080 } else {
3406acde 1081 $filenode = $node->add(get_string('submissiondraft', 'assignment'));
47c96a77 1082 }
1083 foreach ($files as $file) {
1084 $filename = $file->get_filename();
1085 $mimetype = $file->get_mimetype();
64f93798 1086 $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
3406acde 1087 $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
47c96a77 1088 }
1089 }
1090 }
1091
1092 // Show a notes link if they are enabled
1093 if ($this->notes_allowed()) {
a6855934 1094 $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
47c96a77 1095 $node->add(get_string('notes', 'assignment'), $link);
1096 }
1097 }
b37ce778
DM
1098
1099 /**
1100 * creates a zip of all assignment submissions and sends a zip to the browser
1101 */
1102 public function download_submissions() {
1103 global $CFG,$DB;
1104 require_once($CFG->libdir.'/filelib.php');
b37ce778
DM
1105 $submissions = $this->get_submissions('','');
1106 if (empty($submissions)) {
1107 error("there are no submissions to download");
1108 }
1109 $filesforzipping = array();
1110 $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
b37ce778
DM
1111 $fs = get_file_storage();
1112
1113 $groupmode = groupmode($this->course,$this->cm);
1114 $groupid = 0; // All users
1115 $groupname = '';
1116 if($groupmode) {
1117 $group = get_current_group($this->course->id, true);
1118 $groupid = $group->id;
1119 $groupname = $group->name.'-';
1120 }
1121 $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
1122 foreach ($submissions as $submission) {
1123 $a_userid = $submission->userid; //get userid
1124 if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
1125 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
1126 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
64f93798
PS
1127
1128 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
b37ce778
DM
1129 foreach ($files as $file) {
1130 //get files new name.
1131 $fileforzipname = $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
b37ce778 1132 //save file name to array for zipping.
6db11242 1133 $filesforzipping[$fileforzipname] = $file;
b37ce778
DM
1134 }
1135 }
1136 } // end of foreach loop
1137 if ($zipfile = assignment_pack_files($filesforzipping)) {
b37ce778
DM
1138 send_temp_file($zipfile, $filename); //send file and delete after sending.
1139 }
1140 }
6117edc7 1141}
1142
f07b9627 1143class mod_assignment_upload_notes_form extends moodleform {
c561f44c
SH
1144
1145 function get_data() {
1146 $data = parent::get_data();
1147 if ($data) {
1148 $data->format = $data->text['format'];
1149 $data->text = $data->text['text'];
1150 }
1151 return $data;
1152 }
1153
1154 function set_data($data) {
1155 if (!isset($data->format)) {
1156 $data->format = FORMAT_HTML;
1157 }
1158 if (isset($data->text)) {
1159 $data->text = array('text'=>$data->text, 'format'=>$data->format);
1160 }
1161 parent::set_data($data);
1162 }
1163
6117edc7 1164 function definition() {
172dd12c 1165 $mform = $this->_form;
6117edc7 1166
1167 // visible elements
c561f44c 1168 $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
6117edc7 1169 $mform->setType('text', PARAM_RAW); // to be cleaned before display
55b4d096 1170
6117edc7 1171 // hidden params
1172 $mform->addElement('hidden', 'id', 0);
1173 $mform->setType('id', PARAM_INT);
1174 $mform->addElement('hidden', 'action', 'savenotes');
172dd12c 1175 $mform->setType('action', PARAM_ALPHA);
6117edc7 1176
1177 // buttons
a23f0aaf 1178 $this->add_action_buttons();
6117edc7 1179 }
fcdcc372 1180}
8ba4cbd2 1181
172dd12c 1182class mod_assignment_upload_response_form extends moodleform {
1183 function definition() {
1184 $mform = $this->_form;
1185 $instance = $this->_customdata;
1186
1187 // visible elements
6ad4b78f 1188 $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
172dd12c 1189
1190 // hidden params
1191 $mform->addElement('hidden', 'id', $instance->cm->id);
1192 $mform->setType('id', PARAM_INT);
6ad4b78f 1193 $mform->addElement('hidden', 'contextid', $instance->contextid);
1194 $mform->setType('contextid', PARAM_INT);
172dd12c 1195 $mform->addElement('hidden', 'action', 'uploadresponse');
1196 $mform->setType('action', PARAM_ALPHA);
6ad4b78f 1197 $mform->addElement('hidden', 'mode', $instance->mode);
172dd12c 1198 $mform->setType('mode', PARAM_ALPHA);
6ad4b78f 1199 $mform->addElement('hidden', 'offset', $instance->offset);
172dd12c 1200 $mform->setType('offset', PARAM_INT);
6ad4b78f 1201 $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
172dd12c 1202 $mform->setType('forcerefresh', PARAM_INT);
6ad4b78f 1203 $mform->addElement('hidden', 'userid', $instance->userid);
172dd12c 1204 $mform->setType('userid', PARAM_INT);
1205
1206 // buttons
1207 $this->add_action_buttons(false, get_string('uploadthisfile'));
1208 }
1209}
1210
67a87e7d 1211
1212
e7521559 1213