Retrieving a Course Image URL in Moodle Using PHP

Tejaksha K
3 min readMar 7, 2023

--

Moodle is a popular open-source learning management system used by many educational institutions and organizations worldwide. One of the key features of Moodle is its ability to manage courses and display relevant course information to users. This often includes displaying an image for each course, which can help to provide a visual representation of the course content.

If you’re working with Moodle’s PHP code, you may need to retrieve the image URL for a course programmatically. Luckily, Moodle provides a convenient way to do this using the \core_course\external\course_summary_exporter class.

Here’s an example code snippet that demonstrates how to use this class to retrieve a course’s image URL:

$course = get_course($courseid);
$imageUrl = \core_course\external\course_summary_exporter::get_course_image($course);

In this code, the $courseid variable should contain the ID of the course you want to retrieve the image URL for. The get_course() function is used to retrieve the course object for the specified ID. This object is then passed as a parameter to the get_course_image() method of the course_summary_exporter class.

The get_course_image() method returns the URL of the course image, which can then be used to display the image on the course page or in other areas of your Moodle site.

It’s worth noting that this code assumes that the Moodle core files are loaded and that the \core_course\external\course_summary_exporter class is available. If you're working in a custom Moodle plugin or theme, you may need to include these files or namespaces manually.

In Moodle, there are other methods available for retrieving course images in addition to the \core_course\external\course_summary_exporter::get_course_image() method. Here are a few of them:

  1. Using the coursecat::get() method

This method retrieves a course category object by ID and includes the $withcontents parameter to indicate whether or not to retrieve the course objects as well. Once the category object is retrieved, you can then loop through its course objects and retrieve the course images as follows:

$category = coursecat::get($categoryid, true);
foreach ($category->get_courses() as $course) {
$imageUrl = $course->get_course_image_url();
// Do something with the image URL
}

2. Using the course_enrolment_manager::get_course() method

This method retrieves a course object by ID and includes the $withcontents parameter to indicate whether or not to retrieve the course contents as well. Once the course object is retrieved, you can then retrieve the course image as follows:

$course = course_enrolment_manager::get_course($courseid, true);
$imageUrl = $course->get_course_image_url();
// Do something with the image URL

3. Using the course_format_class::get_course_image_url() method

This method retrieves the image URL for a course using the course’s format class. To use this method, you’ll first need to retrieve the course’s format class using the course_get_format() function:

$course = get_course($courseid);
$format = course_get_format($course);
$imageUrl = $format->get_course_image_url();
// Do something with the image URL

Note that this method only works if the course has a format class that implements the course_format_class interface.

Overall, there are multiple methods available for retrieving course images in Moodle. The best method to use will depend on the specific use case and the available information about the course.

You’re welcome! I’m glad I could help you with my blog post. Don’t hesitate to reach out if you have any other questions or need further assistance. Keep up the great work! ❤️

This method retrieves a course object by ID and includes the $withcontents parameter to indicate whether or not to retrieve the course contents as well. Once the course object is retrieved, you can then retrieve the course image as follows:

--

--

Tejaksha K
Tejaksha K

Written by Tejaksha K

I'm a Full Stack Developer & Cloud Expert with experience in Google Cloud Platform & AWS.

No responses yet