Skip to content

Commit

Permalink
Add category id parameter to course cards embed code, replace depreca…
Browse files Browse the repository at this point in the history
…ted function call (file_encode_url), make compatible with mobile app (no support for background images on in-line styles).
  • Loading branch information
andrewhancox committed Oct 12, 2020
1 parent 3471283 commit cd13bd2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [2.0.5] dev-2020-10-12
### Added
- categoryid parameter for {coursecards categoryid=x} tag (ALPHA).

## [2.0.4] dev-2020-10-09
### Added
- New {ifingroup id|idnumber}{/ifingroup} tags.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ FilterCodes are meant to be entered as regular text in the Moodle WYSIWYG editor
* {coursecompletiondate} : Course completion date. If not completed, will display "Not completed". Will also detect if completion is not enabled.
* {courseprogress}: (ALPHA) Displays course progress status in words. Only works within a course.
* {courseprogressbar}: (ALPHA) Displays course progress status as a status bar. Only works within a course.
* {coursecards categoryid=x}: (ALPHA) Display available courses as cards. Optional categoryid parameter to specify one or more category IDs.
* {course_fields}: Displays the custom course fields. NOTE: Respects a custom course field's Visible To setting.
* {course_field_shortname} : Display's custom course field. Replace "shortname" with the shortname of a custom course field all in lowercase. NOTE: Respects a custom course field's Visible To setting.
* {courseimage} : Display's the course image.
Expand Down Expand Up @@ -569,7 +570,7 @@ Create a Page on your Moodle site, preferably in a course, so that those tags wo
* Completion date [{coursecompletiondate}]: {coursecompletiondate}
* Course progress (ALPHA) [{courseprogress}]: {courseprogress}
* Course progress bar (ALPHA) [{courseprogressbar}]: {courseprogressbar}
* Course cards (ALPHA) [{coursecards}]: {coursecards}
* Course cards (ALPHA) [{coursecards categoryid=1}]: If miscellaneous category still exists, it will be displayed: {coursecards categoryid=1}
* Category cards (ALPHA) [{categorycards}]: {categorycards}
* Total courses [{coursecount}]: {coursecount}
* Institution [{institution}]: {institution}
Expand Down
122 changes: 69 additions & 53 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,9 @@ function ($matches) {
$coursefiles = $course->get_course_overviewfiles();
foreach ($coursefiles as $file) {
if ($isimage = $file->is_valid_image()) {
$imgurl = file_encode_url("/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component()
. '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename() , !$isimage);
$imgurl = new moodle_url($imgurl);
$imgurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(), null, $file->get_filepath(),
$file->get_filename())->out(false);
break;
}
}
Expand Down Expand Up @@ -870,62 +870,78 @@ function ($matches) {
}
}

if (stripos($text, '{coursecards}') !== false) {
if (stripos($text, '{coursecards') !== false) { //Only do the expensive regex if it's worth it.
global $CFG, $OUTPUT;
$matches = [];

$chelper = new coursecat_helper();
$chelper->set_show_courses(20)->set_courses_display_options(array(
'recursive' => true,
'limit' => $CFG->frontpagecourselimit,
'viewmoreurl' => new moodle_url('/course/index.php'),
'viewmoretext' => new lang_string('fulllistofcourses')
));

$chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
$courses = core_course_category::get(0)->get_courses($chelper->get_courses_display_options());

$rcourseids = array_keys($courses);

$header = '<div class="card-deck mr-0">';
$footer = '</div>';
$content = '';
if (count($rcourseids) > 0) {
foreach ($rcourseids as $courseid) {
$course = get_course($courseid);

// Load image from course image. If none, generate a course image based on the course ID.
$context = context_course::instance($courseid);
if ($course instanceof stdClass) {
$course = new \core_course_list_element($course);
}
$coursefiles = $course->get_course_overviewfiles();
$imgurl = '';
foreach ($coursefiles as $file) {
if ($isimage = $file->is_valid_image()) {
$imgurl = file_encode_url("/pluginfile.php", '/' . $file->get_contextid() . '/'
. $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath()
. $file->get_filename(), !$isimage);
$imgurl = new moodle_url($imgurl);
break;
preg_match_all('#{coursecards(| categoryid=([0-9])*)}#', $text, $matches);
$results = array_combine($matches[0], $matches[2]);

foreach ($results as $placeholder => $categoryid) {
if (empty($categoryid)) {
$categoryid = 0;
}

try {
$core_course_category = core_course_category::get($categoryid);
} catch (moodle_exception $ex) {
$replace['/'.$placeholder.'/i'] = '';
continue;
}

$chelper = new coursecat_helper();
$chelper->set_show_courses(20)->set_courses_display_options(array(
'recursive' => true,
'limit' => $CFG->frontpagecourselimit,
'viewmoreurl' => new moodle_url('/course/index.php'),
'viewmoretext' => new lang_string('fulllistofcourses')
));

$chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
$courses = $core_course_category->get_courses($chelper->get_courses_display_options());

$rcourseids = array_keys($courses);

$header = '<div class="card-deck mr-0">';
$footer = '</div>';
$content = '';
if (count($rcourseids) > 0) {
foreach ($rcourseids as $courseid) {
$course = get_course($courseid);

// Load image from course image. If none, generate a course image based on the course ID.
$context = context_course::instance($courseid);
if ($course instanceof stdClass) {
$course = new \core_course_list_element($course);
}
$coursefiles = $course->get_course_overviewfiles();
$imgurl = '';
foreach ($coursefiles as $file) {
if ($isimage = $file->is_valid_image()) {
$imgurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
$file->get_filearea(), null, $file->get_filepath(),
$file->get_filename())->out(false);
break;
}
}
if (empty($imgurl)) {
$imgurl = $OUTPUT->get_generated_image_for_id($courseid);
}
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid ));
$content .= '<div class="filtercodes_coursecards card shadow mr-4 mb-4 ml-1" style="min-width: 300px;max-width: 300px;height: 300px;">
<div class="card-img-top" style="max-width: 300px;height: 250px;">
<a href="' . $courseurl . '" class="text-normal h-100">
<img style="max-width: 100%;max-height: 200px;" src="' . $imgurl .'">
</a>
</div>
<div class="card-title pt-1 pr-3 pb-1 pl-3 m-0" style="height: 50px;">
<a href="' . $courseurl . '">' . $course->get_formatted_name() . '</a>
</div>
</div>';
}
if (empty($imgurl)) {
$imgurl = $OUTPUT->get_generated_image_for_id($courseid);
}
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid ));
$content .= '
<div class="card shadow mr-4 mb-4 ml-1" style="min-width:300px;max-width:300px;">
<a href="' . $courseurl . '" class="text-normal h-100">
<div class="card-img-top" style="background-image:url(' . $imgurl
. ');height:100px;max-width:300px;padding-top:50%;background-size:cover;'
. 'background-repeat:no-repeat;background-position:center;"></div>
<div class="card-title pt-1 pr-3 pb-1 pl-3 m-0">' . $course->get_formatted_name() . '</div>
</a>
</div>
';
}
$replace['/'.$placeholder.'/i'] = !empty($header) ? $header . $content . $footer : '';
}
$replace['/\{coursecards\}/i'] = !empty($header) ? $header . $content . $footer : '';
}
}

Expand Down
8 changes: 8 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
div.filtercodes_coursecards div.card-img-top {
text-align: center;
align-content: center;
}

div.filtercodes_coursecards div.card-img-top img {
padding-top: 10px;
}

0 comments on commit cd13bd2

Please sign in to comment.