From e4b3b1c79b2e8bb6ecc3631e13c0441505792631 Mon Sep 17 00:00:00 2001 From: Stefan Topfstedt Date: Wed, 16 Aug 2023 10:52:30 -0700 Subject: [PATCH] MDL-65363 block_starredcourses: only list enroled courses Filter out any starred courses that the logged-in user is not enroled in. --- blocks/starredcourses/classes/external.php | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/blocks/starredcourses/classes/external.php b/blocks/starredcourses/classes/external.php index d1ace2cd0f3..d83b32f8d9c 100644 --- a/blocks/starredcourses/classes/external.php +++ b/blocks/starredcourses/classes/external.php @@ -82,14 +82,41 @@ class block_starredcourses_external extends core_course_external { // Get the favourites, by type, for the user. $favourites = $userservice->find_favourites_by_type('core_course', 'courses', $offset, $limit); + $favouritecourseids = []; + if ($favourites) { + $favouritecourseids = array_map( + function($favourite) { + return $favourite->itemid; + }, $favourites); + } + + // Get all courses that the current user is enroled in, restricted down to favourites. + $filteredcourses = []; + if ($favouritecourseids) { + $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, null, + COURSE_DB_QUERY_LIMIT, $favouritecourseids); + list($filteredcourses, $processedcount) = course_filter_courses_by_favourites( + $courses, + $favouritecourseids, + 0 + ); + } + // Grab the course ids. + $filteredcourseids = array_column($filteredcourses, 'id'); + + // Filter out any favourites that are not in the list of enroled courses. + $filteredfavourites = array_filter($favourites, function($favourite) use ($filteredcourseids) { + return in_array($favourite->itemid, $filteredcourseids); + }); + // Sort the favourites getting last added first. - usort($favourites, function($a, $b) { + usort($filteredfavourites, function($a, $b) { if ($a->timemodified == $b->timemodified) return 0; return ($a->timemodified > $b->timemodified) ? -1 : 1; }); $formattedcourses = array(); - foreach ($favourites as $favourite) { + foreach ($filteredfavourites as $favourite) { $course = get_course($favourite->itemid); $context = \context_course::instance($favourite->itemid); $canviewhiddencourses = has_capability('moodle/course:viewhiddencourses', $context);