Permalink
Browse files

MDL-34290 Auto synchronise newly created references when possible

  • Loading branch information...
1 parent a7f53f0 commit 29ea4e9c56e47280753502de8bc5620642736672 @marinaglancy marinaglancy committed Aug 9, 2012
Showing with 30 additions and 8 deletions.
  1. +23 −6 lib/filestorage/file_storage.php
  2. +7 −2 repository/repository_ajax.php
@@ -1266,22 +1266,39 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc
$transaction = $DB->start_delegated_transaction();
try {
- $filerecord->referencefileid = $this->get_or_create_referencefileid($repositoryid, $reference,
- $filerecord->referencelastsync, $filerecord->referencelifetime);
+ $filerecord->referencefileid = $this->get_or_create_referencefileid($repositoryid, $reference);
} catch (Exception $e) {
throw new file_reference_exception($repositoryid, $reference, null, null, $e->getMessage());
}
- // External file doesn't have content in moodle.
- // So we create an empty file for it.
- list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null);
+ if (isset($filerecord->contenthash) && $this->content_exists($filerecord->contenthash)) {
+ // there was specified the contenthash for a file already stored in moodle filepool
+ if (empty($filerecord->filesize)) {
+ $filepathname = $this->path_from_hash($filerecord->contenthash) . '/' . $filerecord->contenthash;
+ $filerecord->filesize = filesize($filepathname);
+ } else {
+ $filerecord->filesize = clean_param($filerecord->filesize, PARAM_INT);
+ }
+ } else {
+ // atempt to get the result of last synchronisation for this reference
+ $lastcontent = $DB->get_record('files', array('referencefileid' => $filerecord->referencefileid),
+ 'id, contenthash, filesize', IGNORE_MULTIPLE);
+ if ($lastcontent) {
+ $filerecord->contenthash = $lastcontent->contenthash;
+ $filerecord->filesize = $lastcontent->filesize;
+ } else {
+ // External file doesn't have content in moodle.
+ // So we create an empty file for it.
+ list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null);
+ }
+ }
$filerecord->pathnamehash = $this->get_pathname_hash($filerecord->contextid, $filerecord->component, $filerecord->filearea, $filerecord->itemid, $filerecord->filepath, $filerecord->filename);
try {
$filerecord->id = $DB->insert_record('files', $filerecord);
} catch (dml_exception $e) {
- if ($newfile) {
+ if (!empty($newfile)) {
$this->deleted_file_cleanup($filerecord->contenthash);
}
throw new stored_file_creation_exception($filerecord->contextid, $filerecord->component, $filerecord->filearea, $filerecord->itemid,
@@ -234,15 +234,20 @@
if ($file && $file->is_external_file()) {
$sourcefield = $file->get_source(); // remember the original source
$record->source = $repo::build_source_field($sourcefield);
+ $record->contenthash = $file->get_contenthash();
+ $record->filesize = $file->get_filesize();
$reference = $file->get_reference();
$repo_id = $file->get_repository_id();
$repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
}
}
if ($usefilereference) {
- // get reference life time from repo
- $record->referencelifetime = $repo->get_reference_file_lifetime($reference);
+ if ($repo->has_moodle_files()) {
+ $sourcefile = repository::get_moodle_file($reference);
+ $record->contenthash = $sourcefile->get_contenthash();
+ $record->filesize = $sourcefile->get_filesize();
+ }
// Check if file exists.
if (repository::draftfile_exists($itemid, $saveas_path, $saveas_filename)) {
// File name being used, rename it.

0 comments on commit 29ea4e9

Please sign in to comment.