Skip to content

Instantly share code, notes, and snippets.

@bmbrands
Created April 26, 2016 10:02
Show Gist options
  • Save bmbrands/c1eb21dfa7f491b1205ea0ca806b5493 to your computer and use it in GitHub Desktop.
Save bmbrands/c1eb21dfa7f491b1205ea0ca806b5493 to your computer and use it in GitHub Desktop.
Get a image for a course from the course summary files
<?php
require_once("config.php");
function course_image($courseid) {
global $DB, $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$courserecord = $DB->get_record('course', array('id' => $courseid));
$course = new course_in_list($courserecord);
foreach ($course->get_course_overviewfiles() as $file) {
$isimage = $file->is_valid_image();
$url = file_encode_url("$CFG->wwwroot/pluginfile.php",
'/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
$file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
if ($isimage) {
return html_writer::empty_tag('img',
array('src' => $url, 'alt' => 'Course Image '. $course->fullname, 'class' => 'courseimage'));
}
}
}
// Echo the course image for course with id number 77
echo course_image(77);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment