Merge branch 'wip-MDL-33452-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Dan Poltawski 2012-06-13 15:03:07 +08:00
commit 3aad3a5e64
2 changed files with 36 additions and 0 deletions

View File

@ -644,6 +644,32 @@ abstract class repository {
}
}
/**
* Parses the 'source' returned by moodle repositories and returns an instance of stored_file
*
* @param string $source
* @return stored_file|null
*/
public static function get_moodle_file($source) {
$params = unserialize(base64_decode($source));
if (empty($params) || !is_array($params)) {
return null;
}
foreach (array('contextid', 'itemid', 'filename', 'filepath', 'component') as $key) {
if (!array_key_exists($key, $params)) {
return null;
}
}
$contextid = clean_param($params['contextid'], PARAM_INT);
$component = clean_param($params['component'], PARAM_COMPONENT);
$filearea = clean_param($params['filearea'], PARAM_AREA);
$itemid = clean_param($params['itemid'], PARAM_INT);
$filepath = clean_param($params['filepath'], PARAM_PATH);
$filename = clean_param($params['filename'], PARAM_FILE);
$fs = get_file_storage();
return $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename);
}
/**
* This function is used to copy a moodle file to draft area
*

View File

@ -231,6 +231,16 @@ switch ($action) {
$record->timemodified = $now;
$record->userid = $USER->id;
// If file is already a reference, set $source = file source, $repo = file repository
if ($repo->has_moodle_files()) {
$file = repository::get_moodle_file($source);
if ($file && $file->is_external_file()) {
$source = $file->get_reference();
$repo_id = $file->get_repository_id();
$repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
}
}
if ($usefilereference) {
$reference = $repo->get_file_reference($source);
// get reference life time from repo