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