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