# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/mod/assignment/db/upgrade.php
--- moodle/mod/assignment/db/upgrade.php Base (1.33)
+++ moodle/mod/assignment/db/upgrade.php Locally Modified (Based On 1.33)
@@ -68,15 +68,18 @@
                 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
 
                 // migrate submitted files first
-                $path = $basepath;
+                $newpath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->id/";
+                if (!file_exists($newpath)) {
+                    mkdir($newpath);
+                }
                 $filearea = 'assignment_submission';
-                $items = new DirectoryIterator($path);
+                $items = new DirectoryIterator($basepath);
                 foreach ($items as $item) {
                     if (!$item->isFile()) {
                         continue;
                     }
                     if (!$item->isReadable()) {
-                        echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
+                        echo $OUTPUT->notification(" File not readable, skipping: ".$basepath.$item->getFilename());
                         continue;
                     }
                     $filename = clean_param($item->getFilename(), PARAM_FILE);
@@ -84,19 +87,20 @@
                         continue;
                     }
                     if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
-                        $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
-                        if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
-                            unlink($path.$item->getFilename());
+                        $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
+                        if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
+                            unlink($basepath.$item->getFilename());
                         }
                     }
                 }
                 unset($items); //release file handles
 
                 // migrate teacher response files
-                $path = $basepath.'responses/';
-                if (file_exists($path)) {
+                $basepath = $basepath.'responses/';
+                $newpath = $newpath.'responses/';
+                if (file_exists($basepath)) {
                     $filearea = 'assignment_response';
-                    $items = new DirectoryIterator($path);
+                    $items = new DirectoryIterator($basepath);
                     foreach ($items as $item) {
                         if (!$item->isFile()) {
                             continue;
@@ -106,13 +110,13 @@
                             continue;
                         }
                         if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
-                            $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename,
+                            $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
                                                  'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
                             if ($submission->teacher) {
                                 $file_record['userid'] = $submission->teacher;
                             }
-                            if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
-                                unlink($path.$item->getFilename());
+                            if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
+                                unlink($basepath.$item->getFilename());
                             }
                         }
                     }
Index: moodle/mod/assignment/type/upload/assignment.class.php
--- moodle/mod/assignment/type/upload/assignment.class.php Base (1.109)
+++ moodle/mod/assignment/type/upload/assignment.class.php Locally Modified (Based On 1.109)
@@ -38,8 +38,8 @@
         $this->view_dates();
 
         if (has_capability('mod/assignment:submit', $this->context)) {
-            $filecount = $this->count_user_files($USER->id);
             $submission = $this->get_submission($USER->id);
+            $filecount = $this->count_user_files($submission->id);
 
             $this->view_feedback();
 
@@ -344,16 +344,16 @@
         $fs = get_file_storage();
         $browser = get_file_browser();
 
-        if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
+        if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $submission->id, "timemodified", false)) {
             $button = new portfolio_add_button();
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 $mimetype = $file->get_mimetype();
-                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
+                $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$submission->id.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';
 
                 if ($candelete) {
-                    $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid={$submission->userid}&amp;mode=$mode&amp;offset=$offset";
+                    $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;submissionid={$submission->id}&amp;mode=$mode&amp;offset=$offset";
 
                     $output .= '<a href="'.$delurl.'">&nbsp;'
                               .'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="" /></a> ';
@@ -575,11 +575,13 @@
             $fs = get_file_storage();
             $filename = $mform->get_new_filename('newfile');
             if ($filename !== false) {
-                if (!$fs->file_exists($this->context->id, 'assignment_submission', $USER->id, '/', $filename)) {
-                    if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) {
                         $submission = $this->get_submission($USER->id, true); //create new submission if needed
-                        $submission->timemodified = time();
-                        if ($DB->update_record('assignment_submissions', $submission)) {
+                if (!$fs->file_exists($this->context->id, 'assignment_submission', $submission->id, '/', $filename)) {
+                    if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $submission->id, '/', $filename, false, $USER->id)) {
+                        $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);
@@ -823,6 +825,7 @@
 
         $file     = required_param('file', PARAM_FILE);
         $userid   = required_param('userid', PARAM_INT);
+        $submissionid   = required_param('submissionid', PARAM_INT);
         $confirm  = optional_param('confirm', 0, PARAM_BOOL);
         $mode     = optional_param('mode', '', PARAM_ALPHA);
         $offset   = optional_param('offset', 0, PARAM_INT);
@@ -849,7 +852,7 @@
         }
 
         if (!data_submitted() or !$confirm or !confirm_sesskey()) {
-            $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
+            $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'submissionid'=>$submissionid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
             if (empty($mode)) {
                 $this->view_header(get_string('delete'));
             } else {
@@ -867,7 +870,7 @@
         }
 
         $fs = get_file_storage();
-        if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) {
+        if ($file = $fs->get_file($this->context->id, 'assignment_submission', $submissionid, '/', $file)) {
             if ($file->delete()) {
                 $submission->timemodified = time();
                 if ($DB->update_record('assignment_submissions', $submission)) {
Index: moodle/mod/assignment/type/uploadsingle/assignment.class.php
--- moodle/mod/assignment/type/uploadsingle/assignment.class.php Base (1.63)
+++ moodle/mod/assignment/type/uploadsingle/assignment.class.php Locally Modified (Based On 1.63)
@@ -50,14 +50,15 @@
 
         $this->view_dates();
 
-        $filecount = $this->count_user_files($USER->id);
+        $filecount = false;
 
-        if ($submission = $this->get_submission()) {
+        if ($submission = $this->get_submission($USER->id)) {
+            $filecount = $this->count_user_files($submission->id);
             if ($submission->timemarked) {
                 $this->view_feedback();
             }
             if ($filecount) {
-                echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
+                echo $OUTPUT->box($this->print_user_files($submission->id, true), 'generalbox boxaligncenter');
             }
         }
 
@@ -99,12 +100,18 @@
                 $fs = get_file_storage();
                 $filename = $mform->get_new_filename('newfile');
                 if ($filename !== false) {
-                    $fs->delete_area_files($this->context->id, 'assignment_submission', $USER->id);
-                    if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) {
+                    $fs->delete_area_files($this->context->id, 'assignment_submission', $submission->id);
+
+                    if (empty($submission->id)) {
                         $submission = $this->get_submission($USER->id, true); //create new submission if needed
-                        $submission->timemodified = time();
-                        $submission->numfiles     = 1;
-                        if ($DB->update_record('assignment_submissions', $submission)) {
+                    }
+
+                    if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $submission->id, '/', $filename, false, $USER->id)) {
+                        $updates = new object(); //just enough data for updating the submission
+                        $updates->timemodified = time();
+                        $updates->numfiles     = 1;
+                        $updates->id     = $submission->id;
+                        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);
