MDL-22893, assignment module should use filepicker and filemanager
[moodle.git] / mod / assignment / type / uploadsingle / assignment.class.php
1 <?php
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/>.
18 /**
19  * Extend the base assignment class for assignments where you upload a single file
20  *
21  * @package   mod-assignment
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 require_once($CFG->dirroot.'/mod/assignment/lib.php');
25 require_once(dirname(__FILE__).'/upload_form.php');
27 class assignment_uploadsingle extends assignment_base {
30     function print_student_answer($userid, $return=false){
31         global $CFG, $USER, $OUTPUT;
33         $fs = get_file_storage();
34         $browser = get_file_browser();
36         $output = '';
38         if ($submission = $this->get_submission($userid)) {
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);
45                     $output .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a><br />';
46                 }
47             }
48         }
50         $output = '<div class="files">'.$output.'</div>';
51         return $output;
52     }
54     function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
55         parent::assignment_base($cmid, $assignment, $cm, $course);
56         $this->type = 'uploadsingle';
57     }
59     function view() {
61         global $USER, $OUTPUT;
63         $context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
64         require_capability('mod/assignment:view', $context);
66         add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
68         $this->view_header();
70         $this->view_intro();
72         $this->view_dates();
74         $filecount = false;
76         if ($submission = $this->get_submission($USER->id)) {
77             $filecount = $this->count_user_files($submission->id);
78             if ($submission->timemarked) {
79                 $this->view_feedback();
80             }
81             if ($filecount) {
82                 echo $OUTPUT->box($this->print_user_files($submission->id, true), 'generalbox boxaligncenter');
83             }
84         }
86         if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
87             $this->view_upload_form();
88         }
90         $this->view_footer();
91     }
94     function view_upload_form() {
95         global $OUTPUT, $USER;
96         echo $OUTPUT->box_start('uploadbox');
97         $fs = get_file_storage();
98         // edit files in another page
99         if ($submission = $this->get_submission($USER->id)) {
100             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
101                 $str = get_string('editthisfile', 'assignment');
102             } else {
103                 $str = get_string('uploadafile', 'assignment');
104             }
105         } else {
106             $str = get_string('uploadafile', 'assignment');
107         }
108         echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id)), $str, 'get');
109         echo $OUTPUT->box_end();
110     }
113     function upload($mform) {
114         global $CFG, $USER, $DB, $OUTPUT;
115         $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
116         if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
117             redirect($viewurl);
118         }
120         $filecount = $this->count_user_files($USER->id);
121         $submission = $this->get_submission($USER->id);
122         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
123             if ($submission = $this->get_submission($USER->id)) {
124                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
125                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
126                     redirect($viewurl, get_string('alreadygraded', 'assignment'));
127                 }
128             }
130             if ($formdata = $mform->get_data()) {
131                 $fs = get_file_storage();
132                 $submission = $this->get_submission($USER->id, true); //create new submission if needed
133                 $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
135                 if ($newfilename = $mform->get_new_filename('assignment_file')) {
136                     $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
137                         $submission->id, '/', $newfilename);
139                     $updates = new object(); //just enough data for updating the submission
140                     $updates->timemodified = time();
141                     $updates->numfiles     = 1;
142                     $updates->id     = $submission->id;
143                     $DB->update_record('assignment_submissions', $updates);
144                     add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
145                     $this->update_grade($submission);
146                     $this->email_teachers($submission);
148                     // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
149                     $eventdata = new object();
150                     $eventdata->modulename   = 'assignment';
151                     $eventdata->cmid         = $this->cm->id;
152                     $eventdata->itemid       = $submission->id;
153                     $eventdata->courseid     = $this->course->id;
154                     $eventdata->userid       = $USER->id;
155                     $eventdata->file         = $file;
156                     events_trigger('assessable_file_uploaded', $eventdata);
157                 }
159                 redirect($viewurl, get_string('uploadedfile'));
160             } else {
161                 redirect($viewurl, get_string('uploaderror', 'assignment'));  //submitting not allowed!
162             }
163         }
165         redirect($viewurl);
166     }
168     function setup_elements(&$mform) {
169         global $CFG, $COURSE;
171         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
173         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
174         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
175         $mform->setDefault('resubmit', 0);
177         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
178         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
179         $mform->setDefault('emailteachers', 0);
181         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
182         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
183         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
184         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
185     }
187     function portfolio_exportable() {
188         return true;
189     }
191     function send_file($filearea, $args) {
192         global $CFG, $DB, $USER;
193         require_once($CFG->libdir.'/filelib.php');
195         require_login($this->course, false, $this->cm);
197         if ($filearea !== 'submission') {
198             return false;
199         }
201         $submissionid = (int)array_shift($args);
203         if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
204             return false;
205         }
207         if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
208             return false;
209         }
211         $relativepath = implode('/', $args);
212         $fullpath = '/'.$this->context->id.'/mod_assignment/submission/'.$submissionid.'/'.$relativepath;
214         $fs = get_file_storage();
216         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
217             return false;
218         }
220         send_stored_file($file, 0, 0, true); // download MUST be forced - security!
221     }
223     function extend_settings_navigation($node) {
224         global $CFG, $USER, $OUTPUT;
226         // get users submission if there is one
227         $submission = $this->get_submission();
228         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
229             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
230         } else {
231             $editable = false;
232         }
234         // If the user has submitted something add a bit more stuff
235         if ($submission) {
236             // Add a view link to the settings nav
237             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
238             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
239             if (!empty($submission->timemodified)) {
240                 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
241                 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
242                 $submissionnode->add_class('note');
243                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
244                     $submissionnode->add_class('early');
245                 } else {
246                     $submissionnode->add_class('late');
247                 }
248             }
249         }
251         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
252         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($USER->id)) {
253             $fs = get_file_storage();
254             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
255                 $filenode = $node->add(get_string('submission', 'assignment'));
256                 foreach ($files as $file) {
257                     $filename = $file->get_filename();
258                     $mimetype = $file->get_mimetype();
259                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
260                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype), ''));
261                 }
262             }
263         }
264     }
266     /**
267      * creates a zip of all assignment submissions and sends a zip to the browser
268      */
269     function download_submissions() {
270         global $CFG,$DB;
271         require_once($CFG->libdir.'/filelib.php');
273         $submissions = $this->get_submissions('','');
274         if (empty($submissions)) {
275             error("there are no submissions to download");
276         }
277         $filesforzipping = array();
278         $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
279         $fs = get_file_storage();
281         $groupmode = groupmode($this->course,$this->cm);
282         $groupid = 0;   // All users
283         $groupname = '';
284         if($groupmode) {
285             $group = get_current_group($this->course->id, true);
286             $groupid = $group->id;
287             $groupname = $group->name.'-';
288         }
289         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
290         foreach ($submissions as $submission) {
291             $a_userid = $submission->userid; //get userid
292             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
293                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
294                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
296                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
297                 foreach ($files as $file) {
298                     //get files new name.
299                     $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
300                     //save file name to array for zipping.
301                     $filesforzipping[$fileforzipname] = $file;
302                 }
303             }
304         } // End of foreach
305         if ($zipfile = assignment_pack_files($filesforzipping)) {
306             send_temp_file($zipfile, $filename); //send file and delete after sending.
307         }
308     }