MDL-38254 Assignment Module: add avaialble message to all assignment types
[moodle.git] / mod / assignment / type / uploadsingle / assignment.class.php
CommitLineData
e7521559 1<?php
b0f2597e 2
ce2a1c4d
DC
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
b0f2597e 18/**
19 * Extend the base assignment class for assignments where you upload a single file
20 *
ce2a1c4d
DC
21 * @package mod-assignment
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b0f2597e 23 */
ce2a1c4d
DC
24require_once($CFG->dirroot.'/mod/assignment/lib.php');
25require_once(dirname(__FILE__).'/upload_form.php');
26
b0f2597e 27class assignment_uploadsingle extends assignment_base {
12dce253 28
9bf660b3 29
30 function print_student_answer($userid, $return=false){
d436d197 31 global $CFG, $USER, $OUTPUT;
12dce253 32
172dd12c 33 $fs = get_file_storage();
34 $browser = get_file_browser();
9bf660b3 35
591e205f 36 $output = '';
37
ce2a1c4d 38 if ($submission = $this->get_submission($userid)) {
64f93798
PS
39 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
40 foreach ($files as $file) {
41 $filename = $file->get_filename();
42 $found = true;
43 $mimetype = $file->get_mimetype();
44 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
559276b1 45 $output .= '<a href="'.$path.'" >'.$OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')).s($filename).'</a><br />';
bce59524
DM
46 $output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment));
47 $output .='<br/>';
64f93798 48 }
9bf660b3 49 }
50 }
51
52 $output = '<div class="files">'.$output.'</div>';
12dce253 53 return $output;
9bf660b3 54 }
b0f2597e 55
7bddd4b7 56 function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
57 parent::assignment_base($cmid, $assignment, $cm, $course);
0b5a80a1 58 $this->type = 'uploadsingle';
b0f2597e 59 }
60
73097f07 61 function view() {
12dce253 62
3a003ead 63 global $USER, $OUTPUT;
12dce253 64
01a67102 65 $context = context_module::instance($this->cm->id);
0468976c 66 require_capability('mod/assignment:view', $context);
12dce253 67
c125d80c 68 add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
69
73097f07 70 $this->view_header();
71
f77cfb73 72 $this->view_intro();
73097f07 73
f77cfb73 74 $this->view_dates();
73097f07 75
d992f53d 76 $filecount = false;
73097f07 77
d992f53d
MD
78 if ($submission = $this->get_submission($USER->id)) {
79 $filecount = $this->count_user_files($submission->id);
d8199f1d 80 if ($submission->timemarked) {
deba4dc3 81 $this->view_feedback($submission);
8704af63 82 }
83 if ($filecount) {
109f6d0f 84 echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
d8199f1d 85 }
86 }
87
2612e21b
RW
88 if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
89 if ($this->isopen()) {
90 $this->view_upload_form();
91 } else if ($this->assignment->timeavailable > time()) {
92 echo $OUTPUT->heading(get_string('futureaassignment','assignment'), 3);
93 }
70b2c772 94 }
73097f07 95
96 $this->view_footer();
97 }
98
7f408888
JPG
99 /**
100 * Display the response file to the student
101 *
102 * This default method prints the response file
103 *
104 * @param object $submission The submission object
105 */
106 function view_responsefile($submission) {
deba4dc3
RT
107 $fs = get_file_storage();
108 $noresponsefiles = $fs->is_area_empty($this->context->id, 'mod_assignment', 'response', $submission->id);
109 if (!$noresponsefiles) {
7f408888
JPG
110 echo '<tr>';
111 echo '<td class="left side">&nbsp;</td>';
112 echo '<td class="content">';
deba4dc3
RT
113 echo $this->print_responsefiles($submission->userid);
114 echo '</td></tr>';
7f408888
JPG
115 }
116 }
117
e7755fc6 118 function process_feedback($formdata=null) {
291ee134
AB
119 if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
120 return false;
38430d35 121 }
291ee134
AB
122 $userid = required_param('userid', PARAM_INT);
123 $offset = required_param('offset', PARAM_INT);
124 $mform = $this->display_submission($offset, $userid, false);
125 parent::process_feedback($mform);
8caba229
GGC
126 }
127
128 /**
129 * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
130 * This is necessary for simple file uploads where we need to check that the numfiles field is greater than zero to determine if a
131 * submission is complete.
132 *
133 * @param int $groupid (optional) If nonzero then count is restricted to this group
134 * @return int The number of submissions
135 */
136 function count_real_submissions($groupid=0) {
8caba229
GGC
137 global $DB;
138
139 // Grab the context assocated with our course module
01a67102 140 $context = context_module::instance($this->cm->id);
8caba229
GGC
141
142 // Get ids of users enrolled in the given course.
143 list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:view', $groupid);
144 $params['assignmentid'] = $this->cm->instance;
145
146 // Get ids of users enrolled in the given course.
147 return $DB->count_records_sql("SELECT COUNT('x')
148 FROM {assignment_submissions} s
149 LEFT JOIN {assignment} a ON a.id = s.assignment
150 INNER JOIN ($enroledsql) u ON u.id = s.userid
151 WHERE s.assignment = :assignmentid AND
152 s.numfiles > 0", $params);
153 }
38430d35 154
155 function print_responsefiles($userid, $return=false) {
17bf85de 156 global $OUTPUT, $PAGE;
38430d35 157
158 $output = $OUTPUT->box_start('responsefiles');
159
38430d35 160 if ($submission = $this->get_submission($userid)) {
161 $renderer = $PAGE->get_renderer('mod_assignment');
162 $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
38430d35 163 }
17bf85de 164 $output .= $OUTPUT->box_end();
38430d35 165
166 if ($return) {
167 return $output;
168 }
169 echo $output;
170 }
39790bd8 171
38430d35 172 function can_manage_responsefiles() {
173 if (has_capability('mod/assignment:grade', $this->context)) {
174 return true;
175 } else {
176 return false;
177 }
178 }
d8199f1d 179
73097f07 180 function view_upload_form() {
ce2a1c4d
DC
181 global $OUTPUT, $USER;
182 echo $OUTPUT->box_start('uploadbox');
183 $fs = get_file_storage();
184 // edit files in another page
185 if ($submission = $this->get_submission($USER->id)) {
186 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
187 $str = get_string('editthisfile', 'assignment');
188 } else {
189 $str = get_string('uploadafile', 'assignment');
190 }
191 } else {
192 $str = get_string('uploadafile', 'assignment');
193 }
38430d35 194 echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
ce2a1c4d 195 echo $OUTPUT->box_end();
73097f07 196 }
197
ce2a1c4d 198 function upload($mform) {
38430d35 199 $action = required_param('action', PARAM_ALPHA);
200 switch ($action) {
201 case 'uploadresponse':
202 $this->upload_responsefile($mform);
203 break;
204 case 'uploadfile':
205 $this->upload_file($mform);
206 }
207 }
208
209 function upload_file($mform) {
867aeead 210 global $CFG, $USER, $DB, $OUTPUT;
ce2a1c4d 211 $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
64f93798 212 if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
ce2a1c4d 213 redirect($viewurl);
64f93798 214 }
890c016a 215
15b3db12 216 $submission = $this->get_submission($USER->id);
3db4e011 217 $filecount = 0;
218 if ($submission) {
219 $filecount = $this->count_user_files($submission->id);
220 }
15b3db12 221 if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
38147229 222 if ($submission = $this->get_submission($USER->id)) {
39e11905 223 //TODO: change later to ">= 0", to prevent resubmission when graded 0
224 if (($submission->grade > 0) and !$this->assignment->resubmit) {
ce2a1c4d 225 redirect($viewurl, get_string('alreadygraded', 'assignment'));
38147229 226 }
73097f07 227 }
73097f07 228
ce2a1c4d 229 if ($formdata = $mform->get_data()) {
172dd12c 230 $fs = get_file_storage();
ce2a1c4d
DC
231 $submission = $this->get_submission($USER->id, true); //create new submission if needed
232 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
233
234 if ($newfilename = $mform->get_new_filename('assignment_file')) {
235 $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
236 $submission->id, '/', $newfilename);
237
39790bd8 238 $updates = new stdClass(); //just enough data for updating the submission
ce2a1c4d
DC
239 $updates->timemodified = time();
240 $updates->numfiles = 1;
241 $updates->id = $submission->id;
242 $DB->update_record('assignment_submissions', $updates);
243 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
244 $this->update_grade($submission);
245 $this->email_teachers($submission);
246
247 // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
39790bd8 248 $eventdata = new stdClass();
ce2a1c4d
DC
249 $eventdata->modulename = 'assignment';
250 $eventdata->cmid = $this->cm->id;
251 $eventdata->itemid = $submission->id;
252 $eventdata->courseid = $this->course->id;
253 $eventdata->userid = $USER->id;
4447223b
CT
254 $eventdata->file = $file; // This is depreceated - please use pathnamehashes instead!
255 $eventdata->pathnamehashes = array($file->get_pathnamehash());
ce2a1c4d 256 events_trigger('assessable_file_uploaded', $eventdata);
73097f07 257 }
ce2a1c4d
DC
258
259 redirect($viewurl, get_string('uploadedfile'));
172dd12c 260 } else {
ce2a1c4d 261 redirect($viewurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
73097f07 262 }
263 }
264
ce2a1c4d 265 redirect($viewurl);
73097f07 266 }
267
38430d35 268 function upload_responsefile($mform) {
269 global $CFG, $USER, $OUTPUT, $PAGE;
270
271 $userid = required_param('userid', PARAM_INT);
272 $mode = required_param('mode', PARAM_ALPHA);
273 $offset = required_param('offset', PARAM_INT);
274
275 $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
276
277 if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
278 $fs = get_file_storage();
279 $submission = $this->get_submission($userid, true); //create new submission if needed
280 $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
281
282 if ($newfilename = $mform->get_new_filename('assignment_file')) {
283 $file = $mform->save_stored_file('assignment_file', $this->context->id,
284 'mod_assignment', 'response',$submission->id, '/', $newfilename);
285 }
286 redirect($returnurl, get_string('uploadedfile'));
287 } else {
288 redirect($returnurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
289 }
290 }
291
436cfa9f 292 function setup_elements(&$mform) {
873750c2 293 global $CFG, $COURSE;
73097f07 294
436cfa9f 295 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
73097f07 296
e66841b9
DM
297 $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
298 $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
436cfa9f 299 $mform->setDefault('resubmit', 0);
300
e66841b9
DM
301 $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
302 $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
436cfa9f 303 $mform->setDefault('emailteachers', 0);
304
305 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
436cfa9f 306 $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
307 $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
bce59524 308
01a67102 309 $course_context = context_course::instance($COURSE->id);
67fbfe8b 310 plagiarism_get_form_elements_module($mform, $course_context, 'mod_assignment');
436cfa9f 311 }
b0f2597e 312
67a87e7d 313 function portfolio_exportable() {
314 return true;
315 }
316
b861b609 317 function send_file($filearea, $args, $forcedownload, array $options=array()) {
172dd12c 318 global $CFG, $DB, $USER;
319 require_once($CFG->libdir.'/filelib.php');
320
321 require_login($this->course, false, $this->cm);
322
289bcf73 323 if ($filearea !== 'submission' && $filearea !== 'response') {
64f93798
PS
324 return false;
325 }
172dd12c 326
64f93798 327 $submissionid = (int)array_shift($args);
172dd12c 328
64f93798 329 if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
172dd12c 330 return false;
331 }
332
64f93798
PS
333 if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
334 return false;
335 }
39790bd8 336
64f93798 337 $relativepath = implode('/', $args);
289bcf73 338 $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
64f93798
PS
339
340 $fs = get_file_storage();
341
342 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
172dd12c 343 return false;
344 }
345
261cbbac 346 send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
172dd12c 347 }
348
47c96a77 349 function extend_settings_navigation($node) {
350 global $CFG, $USER, $OUTPUT;
351
352 // get users submission if there is one
353 $submission = $this->get_submission();
64f93798 354 if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
47c96a77 355 $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
356 } else {
357 $editable = false;
358 }
359
d6d8c494 360 // If the user has submitted something add some related links and data
229278a7 361 if (isset($submission->numfiles) AND $submission->numfiles) {
47c96a77 362 // Add a view link to the settings nav
a6855934 363 $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
47c96a77 364 $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
365 if (!empty($submission->timemodified)) {
3406acde
SH
366 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
367 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
368 $submissionnode->add_class('note');
47c96a77 369 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
3406acde 370 $submissionnode->add_class('early');
47c96a77 371 } else {
3406acde 372 $submissionnode->add_class('late');
47c96a77 373 }
374 }
375 }
376
377 // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
87f702a0 378 if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
47c96a77 379 $fs = get_file_storage();
64f93798 380 if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
3406acde 381 $filenode = $node->add(get_string('submission', 'assignment'));
47c96a77 382 foreach ($files as $file) {
383 $filename = $file->get_filename();
384 $mimetype = $file->get_mimetype();
64f93798 385 $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
559276b1 386 $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file), ''));
47c96a77 387 }
388 }
389 }
390 }
b37ce778
DM
391
392 /**
393 * creates a zip of all assignment submissions and sends a zip to the browser
394 */
395 function download_submissions() {
396 global $CFG,$DB;
397 require_once($CFG->libdir.'/filelib.php');
398
399 $submissions = $this->get_submissions('','');
400 if (empty($submissions)) {
14f001a9 401 print_error('errornosubmissions', 'assignment');
b37ce778
DM
402 }
403 $filesforzipping = array();
b37ce778
DM
404 $fs = get_file_storage();
405
a211a40c 406 $groupmode = groups_get_activity_groupmode($this->cm);
b37ce778
DM
407 $groupid = 0; // All users
408 $groupname = '';
14f001a9 409 if ($groupmode) {
a211a40c
DM
410 $groupid = groups_get_activity_group($this->cm, true);
411 $groupname = groups_get_group_name($groupid).'-';
b37ce778
DM
412 }
413 $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
414 foreach ($submissions as $submission) {
415 $a_userid = $submission->userid; //get userid
416 if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
417 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
418 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
64f93798
PS
419
420 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
b37ce778
DM
421 foreach ($files as $file) {
422 //get files new name.
902eca42
DM
423 $fileext = strstr($file->get_filename(), '.');
424 $fileoriginal = str_replace($fileext, '', $file->get_filename());
425 $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
b37ce778 426 //save file name to array for zipping.
6db11242 427 $filesforzipping[$fileforzipname] = $file;
b37ce778
DM
428 }
429 }
430 } // End of foreach
431 if ($zipfile = assignment_pack_files($filesforzipping)) {
b37ce778
DM
432 send_temp_file($zipfile, $filename); //send file and delete after sending.
433 }
434 }
ad2d5eba
GGC
435
436 /**
437 * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
438 * table before a submission actually takes place. This function checks to see if the given submission has actually
439 * been submitted.
440 *
441 * @param stdClass $submission The submission we want to check for completion
442 * @return bool Indicates if the submission was found to be complete
443 */
444 public function is_submitted_with_required_data($submission) {
445 return ($submission->timemodified AND $submission->numfiles > 0);
446 }
b0f2597e 447}
448
38430d35 449class mod_assignment_uploadsingle_response_form extends moodleform {
450 function definition() {
451 $mform = $this->_form;
452 $instance = $this->_customdata;
453
454 // visible elements
455 $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
456
457 // hidden params
458 $mform->addElement('hidden', 'id', $instance->cm->id);
459 $mform->setType('id', PARAM_INT);
460 $mform->addElement('hidden', 'contextid', $instance->contextid);
461 $mform->setType('contextid', PARAM_INT);
462 $mform->addElement('hidden', 'action', 'uploadresponse');
463 $mform->setType('action', PARAM_ALPHA);
464 $mform->addElement('hidden', 'mode', $instance->mode);
465 $mform->setType('mode', PARAM_ALPHA);
466 $mform->addElement('hidden', 'offset', $instance->offset);
467 $mform->setType('offset', PARAM_INT);
468 $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
469 $mform->setType('forcerefresh', PARAM_INT);
470 $mform->addElement('hidden', 'userid', $instance->userid);
471 $mform->setType('userid', PARAM_INT);
472
473 // buttons
474 $this->add_action_buttons(false, get_string('uploadthisfile'));
475 }
8caba229 476}