Merge branch 'master_MDL-63800' of https://github.com/danmarsden/moodle
[moodle.git] / blocks / recentlyaccessedcourses / amd / src / main.js
CommitLineData
41f61293
VDF
1// This file is part of Moodle - http://moodle.org/
2//
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.
7//
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.
12//
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/>.
15
16/**
17 * Javascript to initialise the Recently accessed courses block.
18 *
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
23 */
24
25define(
26 [
27 'jquery',
28 'core_course/repository',
29 'core/templates',
4f6680a1
VDF
30 'core/notification',
31 'core/pubsub',
32 'core_course/events'
41f61293
VDF
33 ],
34 function(
35 $,
36 CoursesRepository,
37 Templates,
4f6680a1
VDF
38 Notification,
39 PubSub,
40 CourseEvents
41f61293
VDF
41 ) {
42
43 var SELECTORS = {
44 COURSES_VIEW: '[data-region="recentlyaccessedcourses-view"]',
45 COURSES_VIEW_CONTENT: '[data-region="recentlyaccessedcourses-view-content"]'
46 };
47
48 var NUM_COURSES_TOTAL = 10;
49
50 /**
51 * Get enrolled courses from backend.
52 *
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
57 */
58 var getRecentCourses = function(userid, limit) {
59 return CoursesRepository.getLastAccessedCourses(userid, limit);
60 };
61
62 /**
63 * Render the dashboard courses.
64 *
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
69 */
70 var renderCourses = function(root, courses) {
71 if (courses.length > 0) {
72 return Templates.render('block_recentlyaccessedcourses/view-cards', {
73 courses: courses
74 });
75 } else {
76 var nocoursesimgurl = root.attr('data-nocoursesimgurl');
77 return Templates.render('block_recentlyaccessedcourses/no-courses', {
78 nocoursesimgurl: nocoursesimgurl
79 });
80 }
81 };
82
83 /**
4f6680a1 84 * Fetch user's recently accessed courses and reload the content of the block.
41f61293 85 *
4f6680a1
VDF
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.
41f61293 89 */
4f6680a1
VDF
90 var reloadContent = function(userid, root) {
91
41f61293
VDF
92 var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
93 var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);
94
95 var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);
96
4f6680a1 97 return coursesPromise.then(function(courses) {
41f61293
VDF
98 var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);
99
100 pagedContentPromise.then(function(html, js) {
101 return Templates.replaceNodeContents(recentcoursesViewContent, html, js);
102 }).catch(Notification.exception);
103 return coursesPromise;
104 }).catch(Notification.exception);
105 };
106
4f6680a1
VDF
107 /**
108 * Register event listeners for the block.
109 *
110 * @param {int} userid User whose courses will be shown
111 * @param {object} root The root element for the recentlyaccessedcourses block.
112 */
113 var registerEventListeners = function(userid, root) {
114 PubSub.subscribe(CourseEvents.favourited, function() {
115 reloadContent(userid, root);
116 });
117
118 PubSub.subscribe(CourseEvents.unfavorited, function() {
119 reloadContent(userid, root);
120 });
121 };
122
123 /**
124 * Get and show the recent courses into the block.
125 *
126 * @param {int} userid User from which the courses will be obtained
127 * @param {object} root The root element for the recentlyaccessedcourses block.
128 */
129 var init = function(userid, root) {
130 root = $(root);
131
132 registerEventListeners(userid, root);
133 reloadContent(userid, root);
134 };
135
41f61293
VDF
136 return {
137 init: init
138 };
139 });