image in block on dashboard doesn't display

image in block on dashboard doesn't display

by Simon Lewis -
Number of replies: 1

Hello,

(On Moodle 3.6) I've a simple image block (based on block_slider) that I put on the default dashboard and it works, when I reset the dashboard it doesn't display for other users. I know it's a context thing but can't work out what. Here's the code:

edit_form.php

$mform->addElement('filemanager', 'config_attachments', get_string('config_attachments', 'block_image'), null, array('subdirs' => 0, 'maxbytes' => 1000000, 'maxfiles' => 1, 'accepted_types' => array('.png', '.jpg', '.gif', '.jpeg')));
function set_data($defaults)
{ if (empty($entry->id)) {
$entry = new stdClass;
$entry->id = null;
}
$draftitemid = file_get_submitted_draft_itemid('config_attachments');
file_prepare_draft_area($draftitemid, $this->block->context->id, 'block_image', 'content', 0, array('subdirs' => true));
$entry->attachments = $draftitemid;
parent::set_data($defaults);
if ($data = parent::get_data()) {

file_save_draft_area_files($data->config_attachments, $this->block->context->id, 'block_image', 'content', 0, array('subdirs' => true)); } }


lib.php

function block_image_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options = array())
{
global $DB, $CFG;
if ($context->contextlevel != CONTEXT_BLOCK) {
send_file_not_found();
}
// If block is in course context, then check if user has capability to access course.
if ($context->get_course_context(false)) {
require_course_login($course);
} else if ($CFG->forcelogin) {
require_login();
} else {
// Get parent context and see if user have proper permission.
$parentcontext = $context->get_parent_context();
if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
// Check if category is visible and user can view this category.
$category = $DB->get_record('course_categories', array('id' => $parentcontext->instanceid), '*', MUST_EXIST);
if (!$category->visible) {
require_capability('moodle/category:viewhiddencategories', $parentcontext);
}
}
// At this point there is no way to check SYSTEM or USER context, so ignoring it.
}
if ($filearea !== 'content') {
send_file_not_found();
}
$fs = get_file_storage();
$filename = array_pop($args);
$filepath = $args ? '/' . implode('/', $args) . '/' : '/';
if (!$file = $fs->get_file($context->id, 'block_image', 'content', 0, $filepath, $filename) or $file->is_directory()) {
send_file_not_found();
}
if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
if ($parentcontext->contextlevel == CONTEXT_USER) {
// force download on all personal pages including /my/
//because we do not have reliable way to find out from where this is used
$forcedownload = true;
}
} else {
// weird, there should be parent context, better force dowload then
$forcedownload = true;
}
\core\session\manager::write_close();
send_stored_file($file, 60 * 60, 0, $forcedownload, $options);
}


block_image.php

class block_image extends block_base {
.... normal plugin stuff....
public function get_content() {
global $CFG, $USER, $DB, $PAGE, $OUTPUT;
require_once(__DIR__ . '/lib.php');
require_once($CFG->libdir . '/filelib.php');
$this->content = new stdClass;
$fs = get_file_storage();
$files = $fs->get_area_files($this->context->id, 'block_image', 'content');
if ($files) {
foreach ($files as $file) {
$filename = $file->get_filename();
$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $filename);
$imagelink = '<img src="' . $url . '" alt="' . $filename . '" />';
}
}
}


I know it's the context entered here '$files = $fs->get_area_files($this->context->id, 'block_image', 'content');' as if I hard-code the '$this->context->id' to match the context in the mdl_files then it works. Just not sure how to get it for each user's dashboard.

Any ideas?

thanks,

Simon



Average of ratings: -
In reply to Simon Lewis

Re: image in block on dashboard doesn't display

by Simon Lewis -

If anyone has the same problem in the future I have found a way around it. 

When configuring the block on the default dashboard, in the 'Where this block appears' section, I just set the 'Select pages' to 'Any page matching the above', with the page above being Dashboard.

Seems obvious now, there's probably a better way of doing it with the code (which I'd be interested to hear of), but this works for me at the moment.

Simon