Accessing a picture uploaded through a filepicker in global plugin's settings on the view page

Re: Accessing a picture uploaded through a filepicker in global plugin's settings on the view page

by Artem Kobylin -
Number of replies: 0

Hello Davo

Thank you for your reply. Doing further research I came across the local plugin "Filemanager", development of that you have supported. I got through the plugin's code and found the local_filemanager_pluginfile() function there, which manages the access to plugin files. I think, I got the idea of the concept, and it worked, when I implemented it in my code.
I am writing here the changes I made and hope that this topic will help others.

Thanks again Davo for your help smile.

I attached a screenshot of mdl_files with new records with the right component.

I made following changes in the code:

--- settings.php:

$setting = new admin_setting_configstoredfile(
        'mod_facetoface/facetoface_email_signature_img',
        get_string('setting:emailsignatureimg', 'facetoface'),
        get_string('setting:emailsignatureimg_caption', 'facetoface'),
        'signature_image',
        0,
        ['maxfiles' => 1, 'accepted_types' => ['.jpg', '.png', '.svg']]
);
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);

--- lib.php:

function mod_facetoface_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $DB;

if ($context->contextlevel != CONTEXT_SYSTEM) {
return false;
}
    // No require_login(), because users getting a email notification
    // are not logged in 
    if ($filearea != 'signature_image') {
return false;
}

$itemid = (int)array_shift($args);

if ($itemid != 0) {
return false;
}

$fs = get_file_storage();

$filename = array_pop($args);
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/'.implode('/', $args).'/';
}

$file = $fs->get_file($context->id, 'mod_facetoface', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false;
}

// finally send the file
send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
}
--- test.php:

require_once(dirname(__FILE__, 3) . '/config.php');

global $OUTPUT;
$systemcontext = context_system::instance();
$fs = get_file_storage();
if ($files = $fs->get_area_files($systemcontext->id, 'mod_facetoface', 'signature_image')) {
foreach ($files as $file) {
if ($file->is_directory()) continue;

$fileurl = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);
$img = html_writer::img($fileurl, 'test');
echo $img;
}
}


Attachment mdl_files_new_records.png
Attachment Output_in_browser_html1.png
Attachment Output_in_the_browser1.png