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