$record->contextid = $usercontext->id; $record->timecreated = $now; $record->timemodified = $now; $record->userid = $USER->id; $record->sortorder = 0; // Check that user has permission to access this file if (!$repo->file_is_accessible($source)) { throw new file_exception('storedfilecannotread'); } // If file is already a reference, set $source = file source, $repo = file repository // note that in this case user may not have permission to access the source file directly // so no file_browser/file_info can be used below 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); } } // {@link repository::build_source_field()} $sourcefield = $repo->get_file_source_info($source); $record->source = $repo::build_source_field($sourcefield); if ($usefilereference) { $reference = $repo->get_file_reference($source); // get reference life time from repo $record->referencelifetime = $repo->get_reference_file_lifetime($reference);
if ($repo->has_moodle_files()) { $file = repository::get_moodle_file($source); 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) { 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. $unused_filename = repository::get_unused_filename($itemid, $saveas_path, $saveas_filename); $record->filename = $unused_filename; // Create a file copy using unused filename. $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference); $event = array(); $event['event'] = 'fileexists'; $event['newfile'] = new stdClass(); $event['newfile']->filepath = $saveas_path; $event['newfile']->filename = $unused_filename;
case 'download': // Check that user has permission to access this file if (!$repo->file_is_accessible($fileurl)) { print_error('storedfilecannotread'); } $record = new stdClass(); $reference = $repo->get_file_reference($fileurl); $sourcefield = $repo->get_file_source_info($fileurl); $record->source = repository::build_source_field($sourcefield); // If file is already a reference, set $fileurl = file source, $repo = file repository // note that in this case user may not have permission to access the source file directly // so no file_browser/file_info can be used below if ($repo->has_moodle_files()) { $file = repository::get_moodle_file($fileurl); if ($file && $file->is_external_file()) { $sourcefield = $file->get_source(); // remember the original source $record->source = $repo::build_source_field($sourcefield); $reference = $file->get_reference(); $repo_id = $file->get_repository_id(); $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions); } } $record->filepath = $savepath; $record->filename = $filename; $record->component = 'user'; $record->filearea = 'draft'; $record->itemid = $itemid; $record->license = '';