MDL-31515 Allow for subsequent files to overwrite old files in database activity...
authorAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Mon, 6 Feb 2012 15:18:54 +0000 (15:18 +0000)
committerAndrew Robert Nicols <andrew.nicols@luns.net.uk>
Mon, 6 Feb 2012 15:19:11 +0000 (15:19 +0000)
mod/data/field/file/field.class.php

index bfea754..9e9df3d 100644 (file)
@@ -167,27 +167,29 @@ class data_field_file extends data_field_base {
         $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
 
         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
-        $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value);
+        $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC');
 
         if (count($files)<2) {
             // no file
         } else {
-            $count = 0;
             foreach ($files as $draftfile) {
-                $file_record = array('contextid'=>$this->context->id, 'component'=>'mod_data', 'filearea'=>'content', 'itemid'=>$content->id, 'filepath'=>'/');
                 if (!$draftfile->is_directory()) {
-                    $file_record['filename'] = $draftfile->get_filename();
+                    $file_record = array(
+                        'contextid' => $this->context->id,
+                        'component' => 'mod_data',
+                        'filearea' => 'content',
+                        'itemid' => $content->id,
+                        'filepath' => '/',
+                        'filename' => $draftfile->get_filename(),
+                    );
 
-                    $content->content = $draftfile->get_filename();
+                    $content->content = $file_record['filename'];
 
                     $fs->create_file_from_storedfile($file_record, $draftfile);
                     $DB->update_record('data_content', $content);
 
-                    if ($count > 0) {
-                        break;
-                    } else {
-                        $count++;
-                    }
+                    // Break from the loop now to avoid overwriting the uploaded file record
+                    break;
                 }
             }
         }