Skip to content

Commit

Permalink
Add ability to filter coursecards by categoryid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhancox committed Sep 23, 2020
1 parent d87a45d commit 1e4b56d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- New {userdescription} tag.
- New {categorycards} tag (ALPHA}.
- New {coursecards} tag (ALPHA).
- New {coursecards_categoryCATID} tag (ALPHA).
- New {courseprogress} tag (ALPHA).
- New {courseprogressbar} tag (ALPHA).
- New {-} tag (soft hyphen)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Moodle metadata filters
* {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}: (ALPHA) Display available courses as cards. Has only been tested on Front Page.
* {coursecards_categoryID}: (ALPHA) Display available courses from category ID as cards. Has only been tested on Front Page.
* {categorycards}: (ALPHA) Display top level categories as cards. Has only been tested on Front Page.
* {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.
Expand Down
105 changes: 61 additions & 44 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,62 +852,79 @@ 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(|_category([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);
}
}
if (empty($imgurl)) {
$imgurl = $OUTPUT->get_generated_image_for_id($courseid);
}
$courseurl = new moodle_url('/course/view.php', array('id' => $courseid ));
$content .= '
$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;
}
}
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>
. ');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

0 comments on commit 1e4b56d

Please sign in to comment.