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