Showing embedded images from course to logged out user

Showing embedded images from course to logged out user

by Martin Greenaway -
Number of replies: 1

Hi there,

I'm having some difficulty working out what I'm doing wrong here, and any assistance would be hugely appreciated.  I'm trying to present course information from the first topic of a course to logged-out users.  The site is set to require users to log in in order to view courses, which is how I want it to stay, but it would be nice to be able to give users an overview of the course before requiring them to login.  Something that can be easily shared on Social Media, for instance.

So, to display the content of the first (overview) topic, which in our case is always section 1 in the course, I start by getting that section:

$topic = $DB->get_record('course_sections', array('course' => $courseid, 'section' => 1));

From this, I can use the ID, and the course context, to get the file (in theory).

$coursecontext = context_course::instance($courseid);
$summary = format_text($topic->summary);
$summaryWithImage = file_rewrite_pluginfile_urls($summary,
                                                 'pluginfile.php',
                                                 $coursecontext->id, 
                                                 'course', 
                                                 'section', 
                                                 $topic->id);

This gives me a properly formatted URL, and it seems to reference the correct file - it works fine when the user is logged into the system, but does not display the image when the user is logged out of the system.  Clicking on the image link takes me to the login page, so presumably pluginfile.php is refusing to serve the image to a logged out user.

My current questions, requiring more experienced Moodle brains than mine, are:

  1. Am I using the right context to get at this file (I presume I have to be, otherwise I'd not be able to retrieve the file URL and it wouldn't show correctly when the user is logged in)?  I've checked it against the record in mdl_files etc. and it all seems to be right.
  2. Should I be looking at permissions for the guest role, e.g. enabling them to view course content or something like that?
  3. Have i missed an obvious setting somewhere that allows pluginfile.php to return images to logged out users?
  4. Is there an easier way to do this?  At the moment, the workaround is to host the images externally but this is annoying and really shouldn't be necessary.

Any and all help gratefully appreciated.

Average of ratings: -
In reply to Martin Greenaway

Re: Showing embedded images from course to logged out user

by Niall Sheehan -
To test permissions of the images (I was curious) I switched to the Clean theme as it's not changing how Moodle handles things on the front page. I made sure that the Front Page settings had Display List of courses and list of categories enabled for non logged in users. In this scenario the course description and image are displayed on the front page, so I doubt it is a permissions issue.


Good luck hunting this down