Accessing Course Images in Moodle: Explained and Demonstrated

Accessing Course Images in Moodle: Explained and Demonstrated

In Moodle LMS, there are multiple way to access the images uploaded inside the course.
Here in this video we will see two different methods of getting the image in form of URL.

Accessing the URL using the Moodle built-in function

$params = array();
$params = array('id' => $course_id);
$course = $DB->get_record('course', $params, '*', MUST_EXIST);

// here is how you can get the course overview files...
$course = new core_course_list_element($course);
foreach ($course->get_course_overviewfiles() as $file) {
    if ($file->is_valid_image()) {
        $imagepath = '/' . $file->get_contextid() .
                '/' . $file->get_component() .
                '/' . $file->get_filearea() .
                $file->get_filepath() .
                $file->get_filename();
        $imageurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $imagepath,
                false);
        echo  $imageurl.'<br>';        
        break;
    }
}



Manual/custom way to get the course image path


// Manual method
$context = context_course::instance($course->id, MUST_EXIST);
$files11 = $DB->get_record_sql("select * from {files} where filename!='.' AND contextid = ?
AND  component = ? AND filearea=?", array($context->id, 'course', 'overviewfiles'));

$component = $files11->component;
$filearea = $files11->filearea;
$filename =  $files11->filename;
$itemid = $context->id;
$fullpath = $CFG->wwwroot."/pluginfile.php/$context->id/$component/$filearea/$filename" ;
echo $fullpath ;


Below is the complete code that include the both ways to access the image

<?php

require_once('../config.php');
require_once($CFG->dirroot . '/course/classes/list_element.php');
global $CFG, $DB, $USER, $OUTPUT;
$course_id = optional_param('id', 0, PARAM_INT);
$course_id = 2;

echo $OUTPUT->header();
 
$params = array();
$params = array('id' => $course_id);
$course = $DB->get_record('course', $params, '*', MUST_EXIST);

// here is how you can get the course overview files...
$course = new core_course_list_element($course);
foreach ($course->get_course_overviewfiles() as $file) {
    if ($file->is_valid_image()) {
        $imagepath = '/' . $file->get_contextid() .
                '/' . $file->get_component() .
                '/' . $file->get_filearea() .
                $file->get_filepath() .
                $file->get_filename();
        $imageurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $imagepath,
                false);
        echo  $imageurl.'<br>';        
        break;
    }
}


// Manual method
$context = context_course::instance($course->id, MUST_EXIST);
$files11 = $DB->get_record_sql("select * from {files} where filename!='.' AND contextid = ?
AND  component = ? AND filearea=?", array($context->id, 'course', 'overviewfiles'));

$component = $files11->component;
$filearea = $files11->filearea;
$filename =  $files11->filename;
$itemid = $context->id;
$fullpath = $CFG->wwwroot."/pluginfile.php/$context->id/$component/$filearea/$filename" ;
echo $fullpath ;

echo $OUTPUT->footer();


You will get the detailed explanation in this video clip





Thank you for being here!

Post a Comment

Previous Post Next Post