1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Javascript to initialise the Recently accessed courses block.
19 * @module block_recentlyaccessedcourses/main.js
20 * @package block_recentlyaccessedcourses
21 * @copyright 2018 Victor Deniz <victor@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 'core_course/repository',
44 COURSES_VIEW: '[data-region="recentlyaccessedcourses-view"]',
45 COURSES_VIEW_CONTENT: '[data-region="recentlyaccessedcourses-view-content"]'
48 var NUM_COURSES_TOTAL = 10;
51 * Get enrolled courses from backend.
53 * @method getRecentCourses
54 * @param {int} userid User from which the courses will be obtained
55 * @param {int} limit Only return this many results
56 * @return {array} Courses user has accessed
58 var getRecentCourses = function(userid, limit) {
59 return CoursesRepository.getLastAccessedCourses(userid, limit);
63 * Render the dashboard courses.
65 * @method renderCourses
66 * @param {object} root The root element for the courses view.
67 * @param {array} courses containing array of returned courses.
68 * @return {promise} Resolved with HTML and JS strings
70 var renderCourses = function(root, courses) {
71 if (courses.length > 0) {
72 return Templates.render('core_course/view-cards', {
76 var nocoursesimgurl = root.attr('data-nocoursesimg');
77 return Templates.render('core_course/no-courses', {
78 nocoursesimg: nocoursesimgurl
84 * Fetch user's recently accessed courses and reload the content of the block.
86 * @param {int} userid User whose courses will be shown
87 * @param {object} root The root element for the recentlyaccessedcourses view.
88 * @returns {promise} The updated content for the block.
90 var reloadContent = function(userid, root) {
92 var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
93 var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);
95 var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);
97 return coursesPromise.then(function(courses) {
98 var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);
100 pagedContentPromise.then(function(html, js) {
101 return Templates.replaceNodeContents(recentcoursesViewContent, html, js);
102 }).catch(Notification.exception);
103 return coursesPromise;
104 }).catch(Notification.exception);
108 * Register event listeners for the block.
110 * @param {int} userid User whose courses will be shown
111 * @param {object} root The root element for the recentlyaccessedcourses block.
113 var registerEventListeners = function(userid, root) {
114 PubSub.subscribe(CourseEvents.favourited, function() {
115 reloadContent(userid, root);
118 PubSub.subscribe(CourseEvents.unfavorited, function() {
119 reloadContent(userid, root);
124 * Get and show the recent courses into the block.
126 * @param {int} userid User from which the courses will be obtained
127 * @param {object} root The root element for the recentlyaccessedcourses block.
129 var init = function(userid, root) {
132 registerEventListeners(userid, root);
133 reloadContent(userid, root);