'jquery',
'core_course/repository',
'core/templates',
- 'core/notification'
+ 'core/notification',
+ 'core/pubsub',
+ 'core_course/events'
],
function(
$,
CoursesRepository,
Templates,
- Notification
+ Notification,
+ PubSub,
+ CourseEvents
) {
var SELECTORS = {
};
/**
- * Get and show the recent courses into the block.
+ * Fetch user's recently accessed courses and reload the content of the block.
*
- * @param {int} userid User from which the courses will be obtained
- * @param {object} root The root element for the recentlyaccessedcourses block.
+ * @param {int} userid User whose courses will be shown
+ * @param {object} root The root element for the recentlyaccessedcourses view.
+ * @returns {promise} The updated content for the block.
*/
- var init = function(userid, root) {
- root = $(root);
+ var reloadContent = function(userid, root) {
+
var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);
var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);
- coursesPromise.then(function(courses) {
+ return coursesPromise.then(function(courses) {
var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);
pagedContentPromise.then(function(html, js) {
}).catch(Notification.exception);
};
+ /**
+ * Register event listeners for the block.
+ *
+ * @param {int} userid User whose courses will be shown
+ * @param {object} root The root element for the recentlyaccessedcourses block.
+ */
+ var registerEventListeners = function(userid, root) {
+ PubSub.subscribe(CourseEvents.favourited, function() {
+ reloadContent(userid, root);
+ });
+
+ PubSub.subscribe(CourseEvents.unfavorited, function() {
+ reloadContent(userid, root);
+ });
+ };
+
+ /**
+ * Get and show the recent courses into the block.
+ *
+ * @param {int} userid User from which the courses will be obtained
+ * @param {object} root The root element for the recentlyaccessedcourses block.
+ */
+ var init = function(userid, root) {
+ root = $(root);
+
+ registerEventListeners(userid, root);
+ reloadContent(userid, root);
+ };
+
return {
init: init
};
"name": "Assignment due 1",
"viewurl": "https://moodlesite/course/view.php?id=2",
"courseimageurl": "https://moodlesite/pluginfile/123/course/overviewfiles/123.jpg",
- "fullname": "course 3"
+ "fullname": "course 3",
+ "isfavourite": true
}
]
}
<div class="card-deck dashboard-card-deck" role="list">
{{#courses}}
- <a href="{{viewurl}}" title="{{fullname}}" class="card dashboard-card" role="list-item">
- <div class="card-img-top dashboard-card-img" style='background-image: url("{{{courseimage}}}");'>
- </div>
- <div class="card-body pr-1 course-info-container">
- <div class="d-flex">
- <div class="card-title d-inline-block text-truncate">
- {{{fullname}}}
+ <div class="card dashboard-card" role="listitem">
+ <a href="{{viewurl}}" title="{{fullname}}">
+ <div class="card-img-top dashboard-card-img" style='background-image: url("{{{courseimage}}}");'>
+ <span class="sr-only">{{#str}}aria:courseimage, core_course{{/str}}</span>
+ {{>core_course/favouriteicon }}
+ </div>
+ <div class="card-body pr-1 course-info-container">
+ <div class="d-flex">
+ <div class="card-title d-inline-block text-truncate">
+ {{{fullname}}}
+ </div>
</div>
</div>
- </div>
- </a>
+ </a>
+ </div>
{{/courses}}
</div>
\ No newline at end of file
$recentcourses = array_map(function($course) use ($renderer) {
context_helper::preload_from_record($course);
$context = context_course::instance($course->id);
- $exporter = new course_summary_exporter($course, ['context' => $context]);
+ $isfavourite = !empty($course->component);
+ $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => $isfavourite]);
return $exporter->export($renderer);
}, $courses);
}
$basefields = array('id', 'idnumber', 'summary', 'summaryformat', 'startdate', 'enddate', 'category',
- 'shortname', 'fullname', 'userid', 'timeaccess');
+ 'shortname', 'fullname', 'timeaccess', 'component');
$sort = trim($sort);
if (empty($sort)) {
$sql = "SELECT $ctxfields, $coursefields
FROM {course} c
- JOIN {context} ctx ON ctx.contextlevel = :contextlevel
+ JOIN {context} ctx
+ ON ctx.contextlevel = :contextlevel
AND ctx.instanceid = c.id
- JOIN {user_lastaccess} ul ON ul.courseid = c.id
+ JOIN {user_lastaccess} ul
+ ON ul.courseid = c.id
+ LEFT JOIN {favourite} f
+ ON f.component = 'core_course'
+ AND f.itemtype = 'courses'
+ AND f.userid = ul.userid
+ AND f.itemid = ul.courseid
WHERE ul.userid = :userid
$orderby";
$params = ['userid' => $userid, 'contextlevel' => CONTEXT_COURSE];