function upload_file($mform, $options)
 {
     global $CFG, $USER, $DB, $OUTPUT;
     $mode = optional_param('mode', '', PARAM_ALPHA);
     $offset = optional_param('offset', 0, PARAM_INT);
     $returnurl = 'view.php?id=' . $this->cm->id;
     $submission = $this->get_submission($USER->id);
     if (!$this->can_upload_file($submission)) {
         $this->view_header(get_string('upload'));
         echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
         echo $OUTPUT->continue_button($returnurl);
         $this->view_footer();
         die;
     }
     if ($formdata = $mform->get_data()) {
         $fs = get_file_storage();
         $submission = $this->get_submission($USER->id, true);
         //create new submission if needed
         $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
         $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
         // Make sure all submitted PDFs are compatible with FPDI
         /** @var $files stored_file[] */
         if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, 'timemodified', false)) {
             foreach ($files as $file) {
                 if ($file->get_mimetype() == 'application/pdf') {
                     if (!MyPDFLib::ensure_pdf_compatible($file)) {
                         // Uses ghostscript to convert any PDFs > v1.4
                         throw new moodle_exception('invalidpdf', 'uploadpdf', $file->get_filename());
                     }
                 }
             }
         }
         $updates = new object();
         $updates->id = $submission->id;
         $updates->timemodified = time();
         if ($DB->update_record('assignment_submissions', $updates)) {
             add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a=' . $this->assignment->id, $this->assignment->id, $this->cm->id);
             $this->update_grade($submission);
             // send files to event system
             $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
             // Let Moodle know that assessable files were  uploaded (eg for plagiarism detection)
             $eventdata = new object();
             $eventdata->modulename = 'assignment';
             $eventdata->cmid = $this->cm->id;
             $eventdata->itemid = $submission->id;
             $eventdata->courseid = $this->course->id;
             $eventdata->userid = $USER->id;
             if ($files) {
                 $eventdata->files = $files;
             }
             events_trigger('assessable_file_uploaded', $eventdata);
         }
         $returnurl = new moodle_url('/mod/assignment/view.php', array('id' => $this->cm->id));
         redirect($returnurl);
     }
     $this->view_header(get_string('upload'));
     echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
     echo $OUTPUT->continue_button($returnurl);
     $this->view_footer();
     die;
 }