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 |
* Encapsules the behavior for creating a tool type from a cartridge URL
|
|
|
18 |
* in Moodle. Manages the UI while operations are occuring.
|
|
|
19 |
*
|
|
|
20 |
* See template: mod_lti/cartridge_registration_form
|
|
|
21 |
*
|
|
|
22 |
* @module mod_lti/cartridge_registration_form
|
|
|
23 |
* @copyright 2015 Ryan Wyllie <ryan@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', 'mod_lti/tool_type', 'mod_lti/events', 'mod_lti/keys', 'core/str'],
|
|
|
28 |
function($, ajax, notification, toolType, ltiEvents, KEYS, str) {
|
|
|
29 |
|
|
|
30 |
var SELECTORS = {
|
|
|
31 |
CARTRIDGE_URL: '#cartridge-url',
|
|
|
32 |
CONSUMER_KEY: '#registration-key',
|
|
|
33 |
SHARED_SECRET: '#registration-secret',
|
|
|
34 |
REGISTRATION_FORM: '#cartridge-registration-form',
|
|
|
35 |
REGISTRATION_SUBMIT_BUTTON: '#cartridge-registration-submit',
|
|
|
36 |
REGISTRATION_CANCEL_BUTTON: '#cartridge-registration-cancel',
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Return the URL the user entered for the cartridge.
|
|
|
41 |
*
|
|
|
42 |
* @method getCartridgeURL
|
|
|
43 |
* @private
|
|
|
44 |
* @return {String}
|
|
|
45 |
*/
|
|
|
46 |
var getCartridgeURL = function() {
|
|
|
47 |
return $(SELECTORS.REGISTRATION_FORM).attr('data-cartridge-url');
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Return the submit button element.
|
|
|
52 |
*
|
|
|
53 |
* @method getSubmitButton
|
|
|
54 |
* @private
|
|
|
55 |
* @return {JQuery} jQuery object
|
|
|
56 |
*/
|
|
|
57 |
var getSubmitButton = function() {
|
|
|
58 |
return $(SELECTORS.REGISTRATION_SUBMIT_BUTTON);
|
|
|
59 |
};
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Return the cancel button element.
|
|
|
63 |
*
|
|
|
64 |
* @method getCancelButton
|
|
|
65 |
* @private
|
|
|
66 |
* @return {JQuery} jQuery object
|
|
|
67 |
*/
|
|
|
68 |
var getCancelButton = function() {
|
|
|
69 |
return $(SELECTORS.REGISTRATION_CANCEL_BUTTON);
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Return the value that the user entered for the consumer key.
|
|
|
74 |
*
|
|
|
75 |
* @method getConsumerKey
|
|
|
76 |
* @private
|
|
|
77 |
* @return {String} the value entered for consumer key.
|
|
|
78 |
*/
|
|
|
79 |
var getConsumerKey = function() {
|
|
|
80 |
return $(SELECTORS.CONSUMER_KEY).val();
|
|
|
81 |
};
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Return the value that the user entered for the shared secret.
|
|
|
85 |
*
|
|
|
86 |
* @method getSharedSecret
|
|
|
87 |
* @private
|
|
|
88 |
* @return {String} the value entered for shared secret
|
|
|
89 |
*/
|
|
|
90 |
var getSharedSecret = function() {
|
|
|
91 |
return $(SELECTORS.SHARED_SECRET).val();
|
|
|
92 |
};
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Trigger a visual loading indicator.
|
|
|
96 |
*
|
|
|
97 |
* @method startLoading
|
|
|
98 |
* @private
|
|
|
99 |
*/
|
|
|
100 |
var startLoading = function() {
|
|
|
101 |
getSubmitButton().addClass('loading');
|
|
|
102 |
};
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Stop the visual loading indicator.
|
|
|
106 |
*
|
|
|
107 |
* @method stopLoading
|
|
|
108 |
* @private
|
|
|
109 |
*/
|
|
|
110 |
var stopLoading = function() {
|
|
|
111 |
getSubmitButton().removeClass('loading');
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Check if the page is currently loading.
|
|
|
116 |
*
|
|
|
117 |
* @method isLoading
|
|
|
118 |
* @private
|
|
|
119 |
* @return {Boolean}
|
|
|
120 |
*/
|
|
|
121 |
var isLoading = function() {
|
|
|
122 |
return getSubmitButton().hasClass('loading');
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Create a tool type from the cartridge URL that the user input. This will
|
|
|
127 |
* send an ajax request to the Moodle server to create the Type. The request will
|
|
|
128 |
* include the consumer key and secret, if any.
|
|
|
129 |
*
|
|
|
130 |
* On success the page will be re-rendered to take the user back to the original
|
|
|
131 |
* page with the list of tools and an alert notifying them of success.
|
|
|
132 |
*
|
|
|
133 |
* @method submitCartridgeURL
|
|
|
134 |
* @private
|
|
|
135 |
* @return {Promise} jQuery Deferred object
|
|
|
136 |
*/
|
|
|
137 |
var submitCartridgeURL = function() {
|
|
|
138 |
if (isLoading()) {
|
|
|
139 |
return false;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
var url = getCartridgeURL();
|
|
|
143 |
// No URL? Do nothing.
|
|
|
144 |
if (url === "") {
|
|
|
145 |
return false;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
startLoading();
|
|
|
149 |
var consumerKey = getConsumerKey();
|
|
|
150 |
var sharedSecret = getSharedSecret();
|
|
|
151 |
var promise = toolType.create({cartridgeurl: url, key: consumerKey, secret: sharedSecret});
|
|
|
152 |
|
|
|
153 |
promise.done(function() {
|
|
|
154 |
str.get_string('successfullycreatedtooltype', 'mod_lti').done(function(s) {
|
|
|
155 |
$(document).trigger(ltiEvents.NEW_TOOL_TYPE);
|
|
|
156 |
$(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
|
|
|
157 |
$(document).trigger(ltiEvents.REGISTRATION_FEEDBACK, {
|
|
|
158 |
message: s
|
|
|
159 |
});
|
|
|
160 |
}).fail(notification.exception);
|
|
|
161 |
}).fail(function() {
|
|
|
162 |
str.get_string('failedtocreatetooltype', 'mod_lti').done(function(s) {
|
|
|
163 |
$(document).trigger(ltiEvents.NEW_TOOL_TYPE);
|
|
|
164 |
$(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
|
|
|
165 |
$(document).trigger(ltiEvents.REGISTRATION_FEEDBACK, {
|
|
|
166 |
message: s,
|
|
|
167 |
error: true
|
|
|
168 |
});
|
|
|
169 |
}).fail(notification.exception);
|
|
|
170 |
}).always(function() {
|
|
|
171 |
stopLoading();
|
|
|
172 |
});
|
|
|
173 |
|
|
|
174 |
return promise;
|
|
|
175 |
};
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Sets up the listeners for user interaction on the page.
|
|
|
179 |
*
|
|
|
180 |
* @method registerEventListeners
|
|
|
181 |
* @private
|
|
|
182 |
*/
|
|
|
183 |
var registerEventListeners = function() {
|
|
|
184 |
var form = $(SELECTORS.REGISTRATION_FORM);
|
|
|
185 |
form.submit(function(e) {
|
|
|
186 |
e.preventDefault();
|
|
|
187 |
submitCartridgeURL();
|
|
|
188 |
});
|
|
|
189 |
|
|
|
190 |
var cancelButton = getCancelButton();
|
|
|
191 |
cancelButton.click(function(e) {
|
|
|
192 |
e.preventDefault();
|
|
|
193 |
$(document).trigger(ltiEvents.STOP_CARTRIDGE_REGISTRATION);
|
|
|
194 |
});
|
|
|
195 |
cancelButton.keypress(function(e) {
|
|
|
196 |
if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
|
|
|
197 |
if (e.keyCode == KEYS.ENTER || e.keyCode == KEYS.SPACE) {
|
|
|
198 |
e.preventDefault();
|
|
|
199 |
cancelButton.click();
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
});
|
|
|
203 |
};
|
|
|
204 |
|
|
|
205 |
return /** @alias module:mod_lti/cartridge_registration_form */ {
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Initialise this module.
|
|
|
209 |
*/
|
|
|
210 |
init: function() {
|
|
|
211 |
registerEventListeners();
|
|
|
212 |
}
|
|
|
213 |
};
|
|
|
214 |
});
|