Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * When returning to Moodle let the user select which course to add the resource to.
18
 *
19
 * @module     tool_moodlenet/select_page
20
 * @copyright  2020 Mathew May <mathew.solutions>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
define([
25
    'core/ajax',
26
    'core/templates',
27
    'tool_moodlenet/selectors',
28
    'core/notification'
29
], function(
30
    Ajax,
31
    Templates,
32
    Selectors,
33
    Notification
34
) {
35
    /**
36
     * @var {string} The id corresponding to the import.
37
     */
38
    var importId;
39
 
40
    /**
41
     * Set up the page.
42
     *
43
     * @method init
44
     * @param {string} importIdString the string ID of the import.
45
     */
46
    var init = function(importIdString) {
47
        importId = importIdString;
48
        var page = document.querySelector(Selectors.region.selectPage);
49
        registerListenerEvents(page);
50
        addCourses(page);
51
    };
52
 
53
    /**
54
     * Renders the 'no-courses' template.
55
     *
56
     * @param {HTMLElement} areaReplace the DOM node to replace.
57
     * @returns {Promise}
58
     */
59
    var renderNoCourses = function(areaReplace) {
60
        return Templates.renderPix('courses', 'tool_moodlenet').then(function(img) {
61
            return img;
62
        }).then(function(img) {
63
            var temp = document.createElement('div');
64
            temp.innerHTML = img.trim();
65
            return Templates.render('core_course/no-courses', {
66
                nocoursesimg: temp.firstChild.src
67
            });
68
        }).then(function(html, js) {
69
            Templates.replaceNodeContents(areaReplace, html, js);
70
            areaReplace.classList.add('mx-auto');
71
            areaReplace.classList.add('w-25');
72
            return;
73
        });
74
    };
75
 
76
    /**
77
     * Render the course cards for those supplied courses.
78
     *
79
     * @param {HTMLElement} areaReplace the DOM node to replace.
80
     * @param {Array<courses>} courses the courses to render.
81
     * @returns {Promise}
82
     */
83
    var renderCourses = function(areaReplace, courses) {
84
        return Templates.render('tool_moodlenet/view-cards', {
85
            courses: courses
86
        }).then(function(html, js) {
87
            Templates.replaceNodeContents(areaReplace, html, js);
88
            areaReplace.classList.remove('mx-auto');
89
            areaReplace.classList.remove('w-25');
90
            return;
91
        });
92
    };
93
 
94
    /**
95
     * For a given input, the page & what to replace fetch courses and manage icons too.
96
     *
97
     * @method searchCourses
98
     * @param {string} inputValue What to search for
99
     * @param {HTMLElement} page The whole page element for our page
100
     * @param {HTMLElement} areaReplace The Element to replace the contents of
101
     */
102
    var searchCourses = function(inputValue, page, areaReplace) {
103
        var searchIcon = page.querySelector(Selectors.region.searchIcon);
104
        var clearIcon = page.querySelector(Selectors.region.clearIcon);
105
 
106
        if (inputValue !== '') {
107
            searchIcon.classList.add('d-none');
108
            clearIcon.parentElement.classList.remove('d-none');
109
        } else {
110
            searchIcon.classList.remove('d-none');
111
            clearIcon.parentElement.classList.add('d-none');
112
        }
113
        var args = {
114
            searchvalue: inputValue,
115
        };
116
        Ajax.call([{
117
            methodname: 'tool_moodlenet_search_courses',
118
            args: args
119
        }])[0].then(function(result) {
120
            if (result.courses.length === 0) {
121
                return renderNoCourses(areaReplace);
122
            } else {
123
                // Add the importId to the course link
124
                result.courses.forEach(function(course) {
125
                    course.viewurl += '&id=' + importId;
126
                });
127
                return renderCourses(areaReplace, result.courses);
128
            }
129
        }).catch(Notification.exception);
130
    };
131
 
132
    /**
133
     * Add the event listeners to our page.
134
     *
135
     * @method registerListenerEvents
136
     * @param {HTMLElement} page The whole page element for our page
137
     */
138
    var registerListenerEvents = function(page) {
139
        var input = page.querySelector(Selectors.region.searchInput);
140
        var courseArea = page.querySelector(Selectors.region.courses);
141
        var clearIcon = page.querySelector(Selectors.region.clearIcon);
142
        clearIcon.addEventListener('click', function() {
143
            input.value = '';
144
            searchCourses('', page, courseArea);
145
        });
146
 
147
        input.addEventListener('input', debounce(function() {
148
            searchCourses(input.value, page, courseArea);
149
        }, 300));
150
    };
151
 
152
    /**
153
     * Fetch the courses to show the user. We use the same WS structure & template as the search for consistency.
154
     *
155
     * @method addCourses
156
     * @param {HTMLElement} page The whole page element for our course page
157
     */
158
    var addCourses = function(page) {
159
        var courseArea = page.querySelector(Selectors.region.courses);
160
        searchCourses('', page, courseArea);
161
    };
162
 
163
    /**
164
     * Define our own debounce function as Moodle 3.7 does not have it.
165
     *
166
     * @method debounce
167
     * @from underscore.js
168
     * @copyright 2009-2020 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
169
     * @licence MIT
170
     * @param {function} func The function we want to keep calling
171
     * @param {number} wait Our timeout
172
     * @param {boolean} immediate Do we want to apply the function immediately
173
     * @return {function}
174
     */
175
    var debounce = function(func, wait, immediate) {
176
        var timeout;
177
        return function() {
178
            var context = this;
179
            var args = arguments;
180
            var later = function() {
181
                timeout = null;
182
                if (!immediate) {
183
                    func.apply(context, args);
184
                }
185
            };
186
            var callNow = immediate && !timeout;
187
            clearTimeout(timeout);
188
            timeout = setTimeout(later, wait);
189
            if (callNow) {
190
                func.apply(context, args);
191
            }
192
        };
193
    };
194
    return {
195
        init: init,
196
    };
197
});