Hi
I need help please. I can recite everything on the filemanager found on Moodle by heart, (and on google), but I can't get it to work.
I used the filemanager to get a user to upload a file. It is part of a block I am developing. That seems to be successful.
The following code creates a link so the file can be downloaded.
$fs = get_file_storage();
$files = $fs->get_area_files( $context->id, 'block_extensions', 'evidencefile', $itemid, '', false);
foreach ($files as $file) {
// If the record is a directory, skip.
if ($file->is_directory() and $file->get_filename() == '.') {
continue;
}
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), false);
$link = html_writer::link($url,$file->get_filename());
$filename = $file->get_filename();
}
And finally the plugin file:
function block_extensions_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = []) {
if ($context->contextlevel != CONTEXT_COURSE) {
send_file_not_found();
}
if ($filearea !== 'evidencefile') {
send_file_not_found();
}
require_course_login($course, true, $cm);
$itemid = array_shift($args); // The first item in the $args array.
$filename = array_pop($args); // The last item in the $args array.
if (!$args) {
$filepath = '/'; // $args is empty => the path is '/'
} else {
$filepath = '/' . implode('/', $args) . '/'; // $args contains elements of the filepath
}
// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'block_extensions', $filearea, $itemid, $filepath, $filename);
if (!$file) {
send_file_not_found();
}
send_stored_file($file, 0, 0, $forcedownload, $options);
}
But, I get the Exception - Object of class stored_file could not be converted to string error, when I click on the file link.
Please help.