Skip to content

Commit

Permalink
MDL-34290 Auto synchronise newly created references when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Aug 29, 2012
1 parent f4fe646 commit 437f5dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
29 changes: 23 additions & 6 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions repository/repository_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 437f5dc

Please sign in to comment.