diff --git a/repository/lib.php b/repository/lib.php index bb3c9a547d0..2a6fc41cc37 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -2416,9 +2416,10 @@ abstract class repository { $user_context = context_user::instance($USER->id); if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $filepath, $filename)) { if ($tempfile = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $newfilepath, $newfilename)) { + // Remember original file source field. + $source = @unserialize($file->get_source()); if ($tempfile->is_external_file()) { // New file is a reference. Check that existing file does not have any other files referencing to it - $source = @unserialize($file->get_source()); if (isset($source->original) && $fs->search_references_count($source->original)) { return (object)array('error' => get_string('errordoublereference', 'repository')); } @@ -2427,6 +2428,14 @@ abstract class repository { $file->delete(); // create new file $newfile = $fs->create_file_from_storedfile(array('filepath'=>$filepath, 'filename'=>$filename), $tempfile); + // Preserve original file location (stored in source field) for handling references + if (isset($source->original)) { + if (!($newfilesource = @unserialize($newfile->get_source()))) { + $newfilesource = new stdClass(); + } + $newfilesource->original = $source->original; + $newfile->set_source(serialize($newfilesource)); + } // remove temp file $tempfile->delete(); return true; diff --git a/repository/upload/lib.php b/repository/upload/lib.php index 47ad7a4be26..dbec79d3a6d 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -201,13 +201,14 @@ class repository_upload extends repository { $record->userid = $USER->id; if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) { + $existingfilename = $record->filename; + $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename); + $record->filename = $unused_filename; + $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']); if ($overwriteexisting) { - repository::delete_tempfile_from_draft($record->itemid, $record->filepath, $record->filename); + repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename, $record->filepath, $record->filename); + $record->filename = $existingfilename; } else { - $existingfilename = $record->filename; - $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename); - $record->filename = $unused_filename; - $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']); $event = array(); $event['event'] = 'fileexists'; $event['newfile'] = new stdClass; @@ -221,10 +222,10 @@ class repository_upload extends repository { $event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false); return $event; } + } else { + $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']); } - $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']); - return array( 'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false), 'id'=>$record->itemid,