Skip to content

Commit

Permalink
MDL-63062 block_recentlyaccessedcourses: add starred icon
Browse files Browse the repository at this point in the history
  • Loading branch information
vmdef committed Nov 5, 2018
1 parent 41f6129 commit 4f6680a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
2 changes: 1 addition & 1 deletion blocks/recentlyaccessedcourses/amd/build/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 42 additions & 8 deletions blocks/recentlyaccessedcourses/amd/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ define(
'jquery',
'core_course/repository',
'core/templates',
'core/notification'
'core/notification',
'core/pubsub',
'core_course/events'
],
function(
$,
CoursesRepository,
Templates,
Notification
Notification,
PubSub,
CourseEvents
) {

var SELECTORS = {
Expand Down Expand Up @@ -77,19 +81,20 @@ define(
};

/**
* 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) {
Expand All @@ -99,6 +104,35 @@ define(
}).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
};
Expand Down
25 changes: 15 additions & 10 deletions blocks/recentlyaccessedcourses/templates/view-cards.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,29 @@
"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>
2 changes: 1 addition & 1 deletion course/amd/build/repository.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3951,7 +3951,8 @@ public static function get_recent_courses(int $userid = 0, int $limit = 0, int $
$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);

Expand Down
13 changes: 10 additions & 3 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4548,7 +4548,7 @@ function course_get_recent_courses(int $userid = null, int $limit = 0, int $offs
}

$basefields = array('id', 'idnumber', 'summary', 'summaryformat', 'startdate', 'enddate', 'category',
'shortname', 'fullname', 'userid', 'timeaccess');
'shortname', 'fullname', 'timeaccess', 'component');

$sort = trim($sort);
if (empty($sort)) {
Expand All @@ -4571,9 +4571,16 @@ function course_get_recent_courses(int $userid = null, int $limit = 0, int $offs

$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];
Expand Down

0 comments on commit 4f6680a

Please sign in to comment.