0e033f6e |
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 starred courses block. |
18 | * |
19 | * @copyright 2018 Simey Lameze <simey@moodle.com> |
20 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
21 | */ |
22 | |
23 | define( |
24 | [ |
25 | 'jquery', |
26 | 'core/notification', |
27 | 'block_starredcourses/repository', |
07fdb5a0 |
28 | 'core/pubsub', |
29 | 'core/templates', |
30 | 'core_course/events' |
0e033f6e |
31 | ], |
32 | function( |
33 | $, |
34 | Notification, |
35 | Repository, |
07fdb5a0 |
36 | PubSub, |
37 | Templates, |
38 | CourseEvents |
0e033f6e |
39 | ) { |
40 | |
41 | var SELECTORS = { |
d5d7699b |
42 | STARRED_COURSES_REGION_VIEW: '[data-region="starred-courses-view"]', |
0e033f6e |
43 | STARRED_COURSES_REGION: '[data-region="starred-courses-view-content"]' |
44 | }; |
45 | |
0e033f6e |
46 | /** |
47 | * Render the starred courses. |
48 | * |
49 | * @method renderCourses |
50 | * @param {object} root The root element for the starred view. |
51 | * @param {array} courses containing array of returned courses. |
bdde1601 |
52 | * @returns {promise} Resolved with HTML and JS strings |
0e033f6e |
53 | */ |
54 | var renderCourses = function(root, courses) { |
55 | if (courses.length > 0) { |
a29135f8 |
56 | return Templates.render('core_course/view-cards', { |
0e033f6e |
57 | courses: courses |
58 | }); |
59 | } else { |
bdde1601 |
60 | var nocoursesimg = root.find(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg'); |
a29135f8 |
61 | return Templates.render('core_course/no-courses', { |
0e033f6e |
62 | nocoursesimg: nocoursesimg |
63 | }); |
64 | } |
65 | }; |
66 | |
67 | /** |
d5d7699b |
68 | * Fetch user's starred courses and reload the content of the block. |
0e033f6e |
69 | * |
d5d7699b |
70 | * @param {object} root The root element for the starred view. |
bdde1601 |
71 | * @returns {promise} The updated content for the block. |
0e033f6e |
72 | */ |
8730c061 |
73 | var reloadContent = function(root) { |
d5d7699b |
74 | var content = root.find(SELECTORS.STARRED_COURSES_REGION); |
0e033f6e |
75 | |
8730c061 |
76 | var args = { |
0ec60261 |
77 | limit: 0, |
8730c061 |
78 | offset: 0, |
79 | }; |
0e033f6e |
80 | |
8730c061 |
81 | return Repository.getStarredCourses(args) |
82 | .then(function(courses) { |
83 | return renderCourses(root, courses); |
d5d7699b |
84 | }).then(function(html, js) { |
85 | return Templates.replaceNodeContents(content, html, js); |
86 | }).catch(Notification.exception); |
87 | }; |
88 | |
89 | /** |
90 | * Register event listeners for the block. |
91 | * |
92 | * @param {object} root The calendar root element |
d5d7699b |
93 | */ |
8730c061 |
94 | var registerEventListeners = function(root) { |
07fdb5a0 |
95 | PubSub.subscribe(CourseEvents.favourited, function() { |
8730c061 |
96 | reloadContent(root); |
0e033f6e |
97 | }); |
d5d7699b |
98 | |
07fdb5a0 |
99 | PubSub.subscribe(CourseEvents.unfavorited, function() { |
8730c061 |
100 | reloadContent(root); |
d5d7699b |
101 | }); |
102 | }; |
103 | |
104 | /** |
105 | * Initialise all of the modules for the starred courses block. |
106 | * |
107 | * @param {object} root The root element for the block. |
108 | */ |
109 | var init = function(root) { |
110 | root = $(root); |
d5d7699b |
111 | |
8730c061 |
112 | registerEventListeners(root); |
113 | reloadContent(root); |
0e033f6e |
114 | }; |
115 | |
116 | return { |
117 | init: init |
118 | }; |
119 | }); |