$favourites = array_filter($favourites, function($favourite) { $course = get_course($favourite->itemid); $coursecontext = \context_course::instance($favourite->itemid); if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { return false; } return true; }); $formattedcourses = array_map(function($favourite) use ($renderer) { $course = get_course($favourite->itemid); $context = \context_course::instance($favourite->itemid); $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => true]); return $exporter->export($renderer); }, $favourites);