MDL-63062 block_recentlyaccessedcourses: add block and styles
[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',
30 'core/notification'
31 ],
32 function(
33 $,
34 CoursesRepository,
35 Templates,
36 Notification
37 ) {
38
39 var SELECTORS = {
40 COURSES_VIEW: '[data-region="recentlyaccessedcourses-view"]',
41 COURSES_VIEW_CONTENT: '[data-region="recentlyaccessedcourses-view-content"]'
42 };
43
44 var NUM_COURSES_TOTAL = 10;
45
46 /**
47 * Get enrolled courses from backend.
48 *
49 * @method getRecentCourses
50 * @param {int} userid User from which the courses will be obtained
51 * @param {int} limit Only return this many results
52 * @return {array} Courses user has accessed
53 */
54 var getRecentCourses = function(userid, limit) {
55 return CoursesRepository.getLastAccessedCourses(userid, limit);
56 };
57
58 /**
59 * Render the dashboard courses.
60 *
61 * @method renderCourses
62 * @param {object} root The root element for the courses view.
63 * @param {array} courses containing array of returned courses.
64 * @return {promise} Resolved with HTML and JS strings
65 */
66 var renderCourses = function(root, courses) {
67 if (courses.length > 0) {
68 return Templates.render('block_recentlyaccessedcourses/view-cards', {
69 courses: courses
70 });
71 } else {
72 var nocoursesimgurl = root.attr('data-nocoursesimgurl');
73 return Templates.render('block_recentlyaccessedcourses/no-courses', {
74 nocoursesimgurl: nocoursesimgurl
75 });
76 }
77 };
78
79 /**
80 * Get and show the recent courses into the block.
81 *
82 * @param {int} userid User from which the courses will be obtained
83 * @param {object} root The root element for the recentlyaccessedcourses block.
84 */
85 var init = function(userid, root) {
86 root = $(root);
87 var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
88 var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);
89
90 var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);
91
92 coursesPromise.then(function(courses) {
93 var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);
94
95 pagedContentPromise.then(function(html, js) {
96 return Templates.replaceNodeContents(recentcoursesViewContent, html, js);
97 }).catch(Notification.exception);
98 return coursesPromise;
99 }).catch(Notification.exception);
100 };
101
102 return {
103 init: init
104 };
105 });