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
 * Controls all of the behaviour and interaction with a tool type card. These are
18
 * listed on the LTI tool type management page.
19
 *
20
 * See template: mod_lti/tool_proxy_card
21
 *
22
 * @module     mod_lti/tool_proxy_card_controller
23
 * @copyright  2016 John Okely <john@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @since      3.1
26
 */
27
define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/tool_proxy', 'mod_lti/events', 'mod_lti/keys',
28
        'core/str'],
29
        function($, ajax, notification, templates, toolProxy, ltiEvents, KEYS, str) {
30
 
31
    var SELECTORS = {
32
        DELETE_BUTTON: '.delete',
33
        CAPABILITIES_CONTAINER: '.capabilities-container',
34
        ACTIVATE_BUTTON: '.tool-card-footer a.activate',
35
    };
36
 
37
    // Timeout in seconds.
38
    var ANNOUNCEMENT_TIMEOUT = 2000;
39
 
40
    /**
41
     * Return the delete button element.
42
     *
43
     * @method getDeleteButton
44
     * @private
45
     * @param {JQuery} element jQuery object representing the tool card.
46
     * @return {JQuery} jQuery object
47
     */
48
    var getDeleteButton = function(element) {
49
        return element.find(SELECTORS.DELETE_BUTTON);
50
    };
51
 
52
    /**
53
     * Return the activate button for the type.
54
     *
55
     * @method getActivateButton
56
     * @private
57
     * @param {JQuery} element jQuery object representing the tool card.
58
     * @return {JQuery}  jQuery object
59
     */
60
    var getActivateButton = function(element) {
61
        return element.find(SELECTORS.ACTIVATE_BUTTON);
62
    };
63
 
64
    /**
65
     * Get the type id.
66
     *
67
     * @method getTypeId
68
     * @private
69
     * @param {JQuery} element jQuery object representing the tool card.
70
     * @return {String} Type ID
71
     */
72
    var getTypeId = function(element) {
73
        return element.attr('data-proxy-id');
74
    };
75
 
76
    /**
77
     * Stop any announcement currently visible on the card.
78
     *
79
     * @method clearAllAnnouncements
80
     * @private
81
     * @param {JQuery} element jQuery object representing the tool card.
82
     */
83
    var clearAllAnnouncements = function(element) {
84
        element.removeClass('announcement loading success fail capabilities');
85
    };
86
 
87
    /**
88
     * Show the loading announcement.
89
     *
90
     * @method startLoading
91
     * @private
92
     * @param {JQuery} element jQuery object representing the tool card.
93
     */
94
    var startLoading = function(element) {
95
        clearAllAnnouncements(element);
96
        element.addClass('announcement loading');
97
    };
98
 
99
    /**
100
     * Hide the loading announcement.
101
     *
102
     * @method stopLoading
103
     * @private
104
     * @param {JQuery} element jQuery object representing the tool card.
105
     */
106
    var stopLoading = function(element) {
107
        element.removeClass('announcement loading');
108
    };
109
 
110
    /**
111
     * Show the success announcement. The announcement is only
112
     * visible for 2 seconds.
113
     *
114
     * @method announceSuccess
115
     * @private
116
     * @param {JQuery} element jQuery object representing the tool card.
117
     * @return {Promise} jQuery Deferred object
118
     */
119
    var announceSuccess = function(element) {
120
        var promise = $.Deferred();
121
 
122
        clearAllAnnouncements(element);
123
        element.addClass('announcement success');
124
        setTimeout(function() {
125
            element.removeClass('announcement success');
126
            promise.resolve();
127
        }, ANNOUNCEMENT_TIMEOUT);
128
 
129
        return promise;
130
    };
131
 
132
    /**
133
     * Show the failure announcement. The announcement is only
134
     * visible for 2 seconds.
135
     *
136
     * @method announceFailure
137
     * @private
138
     * @param {JQuery} element jQuery object representing the tool card.
139
     * @return {Promise} jQuery Deferred object
140
     */
141
    var announceFailure = function(element) {
142
        var promise = $.Deferred();
143
 
144
        clearAllAnnouncements(element);
145
        element.addClass('announcement fail');
146
        setTimeout(function() {
147
                element.removeClass('announcement fail');
148
                promise.resolve();
149
            }, ANNOUNCEMENT_TIMEOUT);
150
 
151
        return promise;
152
    };
153
 
154
    /**
155
     * Delete the tool type from the Moodle server. Triggers a success
156
     * or failure announcement depending on the result.
157
     *
158
     * @method deleteType
159
     * @private
160
     * @param {JQuery} element jQuery object representing the tool card.
161
     * @return {Promise} jQuery Deferred object
162
     */
163
    var deleteType = function(element) {
164
        var promise = $.Deferred();
165
        var typeId = getTypeId(element);
166
        startLoading(element);
167
 
168
        if (typeId === "") {
169
            return $.Deferred().resolve();
170
        }
171
 
172
        str.get_strings([
173
                {
174
                    key: 'delete',
175
                    component: 'mod_lti'
176
                },
177
                {
178
                    key: 'delete_confirmation',
179
                    component: 'mod_lti'
180
                },
181
                {
182
                    key: 'delete',
183
                    component: 'mod_lti'
184
                },
185
                {
186
                    key: 'cancel',
187
                    component: 'core'
188
                },
189
            ])
190
            .done(function(strs) {
191
                    notification.confirm(strs[0], strs[1], strs[2], strs[3], function() {
192
                            toolProxy.delete(typeId)
193
                                .done(function() {
194
                                        stopLoading(element);
195
                                        announceSuccess(element)
196
                                            .done(function() {
197
                                                    element.remove();
198
                                                    promise.resolve();
199
                                                })
200
                                            .fail(notification.exception);
201
                                    })
202
                                .fail(function(error) {
203
                                        announceFailure(element);
204
                                        promise.reject(error);
205
                                    });
206
                    }, function() {
207
                            stopLoading(element);
208
                            promise.resolve();
209
                        });
210
                })
211
            .fail(function(error) {
212
                    stopLoading(element);
213
                    notification.exception(error);
214
                    promise.reject(error);
215
                });
216
 
217
        return promise;
218
    };
219
 
220
    /**
221
     * The user wishes to activate this tool so show them the capabilities that
222
     * they need to agree to or if there are none then set the tool type's state
223
     * to active.
224
     *
225
     * @method activateToolType
226
     * @private
227
     * @param {JQuery} element jQuery object representing the tool card.
228
     */
229
    var activateToolType = function(element) {
230
        var data = {proxyid: getTypeId(element)};
231
        $(document).trigger(ltiEvents.START_EXTERNAL_REGISTRATION, data);
232
    };
233
 
234
    /**
235
     * Sets up the listeners for user interaction on this tool type card.
236
     *
237
     * @method registerEventListeners
238
     * @private
239
     * @param {JQuery} element jQuery object representing the tool card.
240
     */
241
    var registerEventListeners = function(element) {
242
        var deleteButton = getDeleteButton(element);
243
        deleteButton.click(function(e) {
244
                e.preventDefault();
245
                deleteType(element);
246
            });
247
        deleteButton.keypress(function(e) {
248
                if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
249
                    if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
250
                        e.preventDefault();
251
                        deleteButton.click();
252
                    }
253
                }
254
            });
255
 
256
        var activateButton = getActivateButton(element);
257
        activateButton.click(function(e) {
258
                e.preventDefault();
259
                activateToolType(element);
260
            });
261
        activateButton.keypress(function(e) {
262
                if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
263
                    if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
264
                        e.preventDefault();
265
                        activateButton.click();
266
                    }
267
                }
268
            });
269
    };
270
 
271
    return /** @alias module:mod_lti/tool_card_controller */ {
272
 
273
        /**
274
         * Initialise this module.
275
         *
276
         * @param {JQuery} element jQuery object representing the tool card.
277
         */
278
        init: function(element) {
279
            registerEventListeners(element);
280
        }
281
    };
282
});