0

I'm trying to customise Moodle - 4.O according to my requirements. I have installed My programs plugin so that we can create a program and add some courses into it. The thing is I want to display all the courses in the program as cards. The Course card has to contain Course image, course name, number of learners etc.... I was unable to find where moodle stores these course images in database, so that I can fetch them and display in the card. Is there any other way to display course image in card?

I checked in every table for that course image, it seems like they are not storing anywhere, but in my courses page,card view, they were displaying the card with images of the course. I checked the my courses code, but didn't get how they are rendering the course images.

1 Answers1

0

The image location is stored in the mdl_files table, but you will need the context id for the course

eg: (PostgreSQL)

SELECT c.id AS courseid,
    c.fullname AS coursename,
    'moodledatafolder/filedir/' || SUBSTRING(f.contenthash,1,2) || '/' || SUBSTRING(f.contenthash,3,2) || '/' || f.contenthash AS imagelocation
FROM mdl_course c
JOIN mdl_context ctx ON ctx.instanceid = c.id AND ctx.contextlevel = 50 /* CONTEXT_COURSE */
JOIN mdl_files f ON f.contextid = ctx.id AND f.component = 'course'
    AND f.filearea = 'overviewfiles' AND f.mimetype LIKE 'image/%'
WHERE c.id = 1

But that's in the backend, the images are not delivered directly to the browser. For the frontend you will need to use a Moodle function to get the image url

eg:

// Use a course object or if you haven't one already use:
$course = get_course($courseid);

$courseimageurl = \core_course\external\course_summary_exporter::get_course_image($course);

// $courseimageurl can return a false if there is no image
if (!$courseimageurl) {
    // Use an alternative
}
Russell England
  • 9,436
  • 1
  • 27
  • 41