MDL-20904, upload function now upload to user private only, remove file_stroage code...
[moodle.git] / mod / assignment / type / uploadsingle / assignment.class.php
1 <?php
3 /**
4  * Extend the base assignment class for assignments where you upload a single file
5  *
6  */
7 class assignment_uploadsingle extends assignment_base {
10     function print_student_answer($userid, $return=false){
11         global $CFG, $USER, $OUTPUT;
13         $fs = get_file_storage();
14         $browser = get_file_browser();
16         $output = '';
18         if ($submission = $this->get_submission($USER->id)) {
19             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
20                 foreach ($files as $file) {
21                     $filename = $file->get_filename();
22                     $found = true;
23                     $mimetype = $file->get_mimetype();
24                     $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
25                     $output .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a><br />';
26                 }
27             }
28         }
30         $output = '<div class="files">'.$output.'</div>';
31         return $output;
32     }
34     function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
35         parent::assignment_base($cmid, $assignment, $cm, $course);
36         $this->type = 'uploadsingle';
37     }
39     function view() {
41         global $USER, $OUTPUT;
43         $context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
44         require_capability('mod/assignment:view', $context);
46         add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
48         $this->view_header();
50         $this->view_intro();
52         $this->view_dates();
54         $filecount = false;
56         if ($submission = $this->get_submission($USER->id)) {
57             $filecount = $this->count_user_files($submission->id);
58             if ($submission->timemarked) {
59                 $this->view_feedback();
60             }
61             if ($filecount) {
62                 echo $OUTPUT->box($this->print_user_files($submission->id, true), 'generalbox boxaligncenter');
63             }
64         }
66         if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
67             $this->view_upload_form();
68         }
70         $this->view_footer();
71     }
74     function view_upload_form() {
75         $mform = new mod_assignment_upload_file_form('upload.php', $this);
76                 echo "<div class=\"uploadbox\">";
77         $mform->display();
78         echo "</div>";
79     }
82     function upload() {
83         global $CFG, $USER, $DB, $OUTPUT;
85         if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
86             redirect('view.php?id='.$this->cm->id);
87         }
89         $filecount = $this->count_user_files($USER->id);
90         $submission = $this->get_submission($USER->id);
91         if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
92             if ($submission = $this->get_submission($USER->id)) {
93                 //TODO: change later to ">= 0", to prevent resubmission when graded 0
94                 if (($submission->grade > 0) and !$this->assignment->resubmit) {
95                     redirect('view.php?id='.$this->cm->id, get_string('alreadygraded', 'assignment'));
96                 }
97             }
99             $mform = new mod_assignment_upload_file_form('upload.php', $this);
100             if ($mform->get_data()) {
101                 $fs = get_file_storage();
102                 $filename = $mform->get_new_filename('newfile');
103                 if ($filename !== false) {
104                     $submission = $this->get_submission($USER->id, true); //create new submission if needed
105                     $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
107                     if ($file = $mform->save_stored_file('newfile', $this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename, false, $USER->id)) {
108                         $updates = new object(); //just enough data for updating the submission
109                         $updates->timemodified = time();
110                         $updates->numfiles     = 1;
111                         $updates->id     = $submission->id;
112                         $DB->update_record('assignment_submissions', $updates);
113                         add_to_log($this->course->id, 'assignment', 'upload',
114                                 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
115                         $this->update_grade($submission);
116                         $this->email_teachers($submission);
118                         // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
119                         $eventdata = new object();
120                         $eventdata->modulename   = 'assignment';
121                         $eventdata->cmid         = $this->cm->id;
122                         $eventdata->itemid       = $submission->id;
123                         $eventdata->courseid     = $this->course->id;
124                         $eventdata->userid       = $USER->id;
125                         $eventdata->file         = $file;
126                         events_trigger('assessable_file_uploaded', $eventdata);
128                         redirect('view.php?id='.$this->cm->id, get_string('uploadedfile'));
129                     }
130                 }
131             } else {
132                 redirect('view.php?id='.$this->cm->id, get_string('uploaderror', 'assignment'));  //submitting not allowed!
133             }
134         }
136         redirect('view.php?id='.$this->cm->id);
137     }
139     function setup_elements(&$mform) {
140         global $CFG, $COURSE;
142         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
144         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
145         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
146         $mform->setDefault('resubmit', 0);
148         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
149         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
150         $mform->setDefault('emailteachers', 0);
152         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
153         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
154         $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
155         $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
157     }
159     function portfolio_exportable() {
160         return true;
161     }
163     function send_file($filearea, $args) {
164         global $CFG, $DB, $USER;
165         require_once($CFG->libdir.'/filelib.php');
167         require_login($this->course, false, $this->cm);
169         if ($filearea !== 'submission') {
170             return false;
171         }
173         $submissionid = (int)array_shift($args);
175         if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
176             return false;
177         }
179         if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
180             return false;
181         }
183         $relativepath = implode('/', $args);
184         $fullpath = "/$this->context->id/mod_assignment/submission/$submissionid/$relativepath";
186         $fs = get_file_storage();
188         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
189             return false;
190         }
192         send_stored_file($file, 0, 0, true); // download MUST be forced - security!
193     }
195     function extend_settings_navigation($node) {
196         global $CFG, $USER, $OUTPUT;
198         // get users submission if there is one
199         $submission = $this->get_submission();
200         if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
201             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
202         } else {
203             $editable = false;
204         }
206         // If the user has submitted something add a bit more stuff
207         if ($submission) {
208             // Add a view link to the settings nav
209             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
210             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
211             if (!empty($submission->timemodified)) {
212                 $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
213                 $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
214                 $submissionnode->add_class('note');
215                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
216                     $submissionnode->add_class('early');
217                 } else {
218                     $submissionnode->add_class('late');
219                 }
220             }
221         }
223         // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
224         if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($USER->id)) {
225             $fs = get_file_storage();
226             if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
227                 $filenode = $node->add(get_string('submission', 'assignment'));
228                 foreach ($files as $file) {
229                     $filename = $file->get_filename();
230                     $mimetype = $file->get_mimetype();
231                     $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
232                     $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype), ''));
233                 }
234             }
235         }
236     }
238     /**
239      * creates a zip of all assignment submissions and sends a zip to the browser
240      */
241     function download_submissions() {
242         global $CFG,$DB;
243         require_once($CFG->libdir.'/filelib.php');
245         $submissions = $this->get_submissions('','');
246         if (empty($submissions)) {
247             error("there are no submissions to download");
248         }
249         $filesforzipping = array();
250         $filenewname = clean_filename($this->assignment->name); //create prefix of individual files
251         $fs = get_file_storage();
253         $groupmode = groupmode($this->course,$this->cm);
254         $groupid = 0;   // All users
255         $groupname = '';
256         if($groupmode) {
257             $group = get_current_group($this->course->id, true);
258             $groupid = $group->id;
259             $groupname = $group->name.'-';
260         }
261         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
262         foreach ($submissions as $submission) {
263             $a_userid = $submission->userid; //get userid
264             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
265                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
266                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
268                 $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
269                 foreach ($files as $file) {
270                     //get files new name.
271                     $fileforzipname =  $a_user->username . "_" . $filenewname . "_" . $file->get_filename();
272                     //save file name to array for zipping.
273                     $filesforzipping[$fileforzipname] = $file;
274                 }
275             }
276         } // End of foreach
277         if ($zipfile = assignment_pack_files($filesforzipping)) {
278             send_temp_file($zipfile, $filename); //send file and delete after sending.
279         }
280     }