Really Struggling with file_rewrite_pluginfile_urls() - Moodle 3.9

Really Struggling with file_rewrite_pluginfile_urls() - Moodle 3.9

by Dave Emsley -
Number of replies: 1
Hi All,

I am writing a local plugin which will get the course summary from get_course() and place it on the page.

mymoodle.com/local/my_plugin/?courseid=3 produces:

<img src="http://mymoodle.com/pluginfile.php/1/course/summary/0/half_coffee.jpg" alt="Half a cup of coffee" class="img-responsive atto_image_button_text-bottom" width="237" height="122">

which does not produce an image.  The files table has:

I'm unsure how and why the code doesn't work if I either get the contextid from the page (returns a 1) or use the context ID in the files table above.  I'm also unsure of the item id and how I should be obtaining that.

My code is:

function local_pluginname_get_course_blurb($courseid) {
    $course = get_course($courseid);
    //parameters ($text, $itemid should be obtained dynamically by sql - this is just an example)
    $text =  $course->summary;
    $file = "pluginfile.php";
    $component = "course";
    $filearea = "summary";
    $contextid = 1;
    $itemid = 0;
    $course_blurb = file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid);
    return $course_blurb;   
}


Cheers

Dave


Average of ratings: Useful (1)
In reply to Dave Emsley

Re: Really Struggling with file_rewrite_pluginfile_urls() - Moodle 3.9

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The most likely explanation is a problem with your local_pluginname_pluginfile() function inside local/pluginname/lib.php - if you have xdebug installed + a suitable IDE, I suggest you put a break point in that function and step through to determine why it is not returning the file when requested by the browser (if you don't have xdebug, you may have to resort to echo/var_dump statements, but that is a lot more fiddly to do).

Edit: just reread and realised that you are serving files with component 'course', and filearea 'summary', so it will not be attempting to look for the local_pluginname_pluginfile() function - instead it will be serving the file directly from filelib.php. I suggest you search that file for the line:
if ($filearea === 'summary' || $filearea === 'overviewfiles') {
and start debugging from there.

I can guarantee, however, that a contextid of 1 will not be able to return files stored in a contextid of 27 (as shown in your screenshot). You can get the correct context from:
$context = context_course::instance($courseid);
$contextid = $context->id;

Average of ratings: Useful (1)