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:
- 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.
- Should I be looking at permissions for the guest role, e.g. enabling them to view course content or something like that?
- Have i missed an obvious setting somewhere that allows pluginfile.php to return images to logged out users?
- 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.
Good luck hunting this down