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 the settings page in the message drawer.
|
|
|
18 |
*
|
|
|
19 |
* @module core_message/message_drawer_view_settings
|
|
|
20 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(
|
|
|
24 |
[
|
|
|
25 |
'jquery',
|
|
|
26 |
'core/notification',
|
|
|
27 |
'core/str',
|
|
|
28 |
'core/pubsub',
|
|
|
29 |
'core/templates',
|
|
|
30 |
'core_message/message_repository',
|
|
|
31 |
'core/custom_interaction_events',
|
|
|
32 |
'core_message/message_drawer_events'
|
|
|
33 |
],
|
|
|
34 |
function(
|
|
|
35 |
$,
|
|
|
36 |
Notification,
|
|
|
37 |
Str,
|
|
|
38 |
PubSub,
|
|
|
39 |
Templates,
|
|
|
40 |
Repository,
|
|
|
41 |
CustomEvents,
|
|
|
42 |
MessageDrawerEvents
|
|
|
43 |
) {
|
|
|
44 |
|
|
|
45 |
var SELECTORS = {
|
|
|
46 |
CHECKBOX: 'input[type="checkbox"]',
|
|
|
47 |
SETTINGS: '[data-region="settings"]',
|
|
|
48 |
PRIVACY_PREFERENCE: '[data-preference="blocknoncontacts"] input[type="radio"]',
|
|
|
49 |
NOTIFICATIONS_PREFERENCE: '[data-preference="notifications"] input[type="checkbox"]',
|
|
|
50 |
ENTER_TO_SEND_PREFERENCE: '[data-preference="entertosend"] input[type="checkbox"]',
|
|
|
51 |
NOTIFICATION_PREFERENCES_CONTAINER: '[data-region="notification-preference-container"]',
|
|
|
52 |
CONTENT_CONTAINER: '[data-region="content-container"]',
|
|
|
53 |
PLACEHOLDER_CONTAINER: '[data-region="placeholder-container"]'
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
var TEMPLATES = {
|
|
|
57 |
NOTIFICATION_PREFERENCES: 'core_message/message_drawer_view_settings_body_content_notification_preferences'
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
var NOTIFICATION_PREFERENCES_KEY = 'message_provider_moodle_instantmessage';
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Select the correct radio button in the DOM for the privacy preference.
|
|
|
64 |
*
|
|
|
65 |
* @param {Object} body The settings body element.
|
|
|
66 |
* @param {Number} value Which radio button should be set
|
|
|
67 |
*/
|
|
|
68 |
var setPrivacyPreference = function(body, value) {
|
|
|
69 |
var inputs = body.find(SELECTORS.PRIVACY_PREFERENCE);
|
|
|
70 |
inputs.each(function(index, input) {
|
|
|
71 |
input = $(input);
|
|
|
72 |
if (input.val() == value) {
|
|
|
73 |
input.prop('checked', true);
|
|
|
74 |
} else {
|
|
|
75 |
input.prop('checked', false);
|
|
|
76 |
}
|
|
|
77 |
});
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Set the "enter to send" checkbox to the correct value in the DOM.
|
|
|
82 |
*
|
|
|
83 |
* @param {Object} body The settings body element.
|
|
|
84 |
* @param {Bool} value Whether enter to send is enabled or disabled.
|
|
|
85 |
*/
|
|
|
86 |
var setEnterToSend = function(body, value) {
|
|
|
87 |
var checkbox = body.find(SELECTORS.ENTER_TO_SEND_PREFERENCE);
|
|
|
88 |
|
|
|
89 |
if (value) {
|
|
|
90 |
checkbox.prop('checked', true);
|
|
|
91 |
} else {
|
|
|
92 |
checkbox.prop('checked', false);
|
|
|
93 |
}
|
|
|
94 |
};
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Send a request to the server to save the given preferences. Also publish
|
|
|
98 |
* a preferences updated event for the rest of the message drawer to
|
|
|
99 |
* subscribe to.
|
|
|
100 |
*
|
|
|
101 |
* @param {Number} loggedInUserId The logged in user id.
|
|
|
102 |
* @param {Array} preferences The preferences to set.
|
|
|
103 |
* @return {Object} jQuery promise
|
|
|
104 |
*/
|
|
|
105 |
var savePreferences = function(loggedInUserId, preferences) {
|
|
|
106 |
return Repository.savePreferences(loggedInUserId, preferences)
|
|
|
107 |
.then(function() {
|
|
|
108 |
PubSub.publish(MessageDrawerEvents.PREFERENCES_UPDATED, preferences);
|
|
|
109 |
return;
|
|
|
110 |
})
|
|
|
111 |
.catch(Notification.exception);
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Create all of the event listeners for the message preferences page.
|
|
|
116 |
*
|
|
|
117 |
* @method registerEventListeners
|
|
|
118 |
* @param {Object} body The settings body element.
|
|
|
119 |
* @param {Number} loggedInUserId The logged in user id.
|
|
|
120 |
*/
|
|
|
121 |
var registerEventListeners = function(body, loggedInUserId) {
|
|
|
122 |
var settingsContainer = body.find(SELECTORS.SETTINGS);
|
|
|
123 |
|
|
|
124 |
CustomEvents.define(settingsContainer, [
|
|
|
125 |
CustomEvents.events.activate
|
|
|
126 |
]);
|
|
|
127 |
|
|
|
128 |
settingsContainer.on(CustomEvents.events.activate, SELECTORS.NOTIFICATIONS_PREFERENCE, function(e) {
|
|
|
129 |
var container = $(e.target).closest(SELECTORS.NOTIFICATION_PREFERENCES_CONTAINER);
|
|
|
130 |
var checkboxes = container.find(SELECTORS.CHECKBOX);
|
|
|
131 |
if (!checkboxes.length) {
|
|
|
132 |
return;
|
|
|
133 |
}
|
|
|
134 |
// The preference value is all of the enabled processors, comma separated, so let's
|
|
|
135 |
// see which ones are enabled.
|
|
|
136 |
var values = checkboxes.toArray().reduce(function(carry, checkbox) {
|
|
|
137 |
checkbox = $(checkbox);
|
|
|
138 |
if (checkbox.prop('checked')) {
|
|
|
139 |
carry.push(checkbox.attr('data-name'));
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return carry;
|
|
|
143 |
}, []);
|
|
|
144 |
var newValue = values.length ? values.join(',') : 'none';
|
|
|
145 |
var preferences = [
|
|
|
146 |
{
|
|
|
147 |
type: 'message_provider_moodle_instantmessage_enabled',
|
|
|
148 |
value: newValue
|
|
|
149 |
}
|
|
|
150 |
];
|
|
|
151 |
|
|
|
152 |
savePreferences(loggedInUserId, preferences);
|
|
|
153 |
});
|
|
|
154 |
|
|
|
155 |
settingsContainer.on('change', SELECTORS.PRIVACY_PREFERENCE, function(e) {
|
|
|
156 |
var newValue = $(e.target).val();
|
|
|
157 |
var preferences = [
|
|
|
158 |
{
|
|
|
159 |
type: 'message_blocknoncontacts',
|
|
|
160 |
value: newValue
|
|
|
161 |
}
|
|
|
162 |
];
|
|
|
163 |
|
|
|
164 |
savePreferences(loggedInUserId, preferences);
|
|
|
165 |
});
|
|
|
166 |
|
|
|
167 |
settingsContainer.on(CustomEvents.events.activate, SELECTORS.ENTER_TO_SEND_PREFERENCE, function(e) {
|
|
|
168 |
var newValue = $(e.target).prop('checked');
|
|
|
169 |
var preferences = [
|
|
|
170 |
{
|
|
|
171 |
type: 'message_entertosend',
|
|
|
172 |
value: newValue
|
|
|
173 |
}
|
|
|
174 |
];
|
|
|
175 |
|
|
|
176 |
savePreferences(loggedInUserId, preferences);
|
|
|
177 |
});
|
|
|
178 |
};
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Initialise the module by loading the user's messaging preferences from the server and
|
|
|
182 |
* rendering them in the settings page.
|
|
|
183 |
*
|
|
|
184 |
* Moodle may have many (or no) message processors enabled to notify the user when they
|
|
|
185 |
* receive messages. We need to dynamically build the settings page based on which processors
|
|
|
186 |
* are configured for the user.
|
|
|
187 |
*
|
|
|
188 |
* @param {Object} body The settings body element.
|
|
|
189 |
* @param {Number} loggedInUserId The logged in user id.
|
|
|
190 |
*/
|
|
|
191 |
var init = function(body, loggedInUserId) {
|
|
|
192 |
// Load the message preferences from the server.
|
|
|
193 |
Repository.getUserMessagePreferences(loggedInUserId)
|
|
|
194 |
.then(function(response) {
|
|
|
195 |
// Set the values of the stright forward preferences.
|
|
|
196 |
setPrivacyPreference(body, response.blocknoncontacts);
|
|
|
197 |
setEnterToSend(body, response.entertosend);
|
|
|
198 |
|
|
|
199 |
// Parse the list of other preferences into a more usable format.
|
|
|
200 |
var notificationProcessors = [];
|
|
|
201 |
if (response.preferences.components.length) {
|
|
|
202 |
response.preferences.components.forEach(function(component) {
|
|
|
203 |
if (component.notifications.length) {
|
|
|
204 |
// Filter down to just the notification processors that work on instant
|
|
|
205 |
// messaging. We don't care about another other ones.
|
|
|
206 |
var notificationPreferences = component.notifications.filter(function(notification) {
|
|
|
207 |
return notification.preferencekey == NOTIFICATION_PREFERENCES_KEY;
|
|
|
208 |
});
|
|
|
209 |
|
|
|
210 |
if (notificationPreferences.length) {
|
|
|
211 |
// Messaging only has one config at the moment which is for notifications
|
|
|
212 |
// on personal messages.
|
|
|
213 |
var configuration = component.notifications[0];
|
|
|
214 |
notificationProcessors = configuration.processors.map(function(processor) {
|
|
|
215 |
// Consider the the processor enabled if either preference is set. This is
|
|
|
216 |
// for backwards compatibility. Going forward they will be treated as one
|
|
|
217 |
// setting.
|
|
|
218 |
var checked = processor.enabled;
|
|
|
219 |
return {
|
|
|
220 |
displayname: processor.displayname,
|
|
|
221 |
name: processor.name,
|
|
|
222 |
checked: checked,
|
|
|
223 |
// The admin can force processors to be enabled at a site level so
|
|
|
224 |
// we need to check if this processor has been locked by the admin.
|
|
|
225 |
locked: processor.locked,
|
|
|
226 |
lockedmessage: processor.lockedmessage || null,
|
|
|
227 |
};
|
|
|
228 |
});
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
});
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
var container = body.find(SELECTORS.NOTIFICATION_PREFERENCES_CONTAINER);
|
|
|
235 |
if (notificationProcessors.length) {
|
|
|
236 |
// We have processors (i.e. email, mobile, jabber) to show.
|
|
|
237 |
container.removeClass('hidden');
|
|
|
238 |
// Render the processor options.
|
|
|
239 |
return Templates.render(TEMPLATES.NOTIFICATION_PREFERENCES, {processors: notificationProcessors})
|
|
|
240 |
.then(function(html) {
|
|
|
241 |
container.append(html);
|
|
|
242 |
return html;
|
|
|
243 |
});
|
|
|
244 |
} else {
|
|
|
245 |
return true;
|
|
|
246 |
}
|
|
|
247 |
})
|
|
|
248 |
.then(function() {
|
|
|
249 |
// We're done loading so hide the loading placeholder and show the settings.
|
|
|
250 |
body.find(SELECTORS.CONTENT_CONTAINER).removeClass('hidden');
|
|
|
251 |
body.find(SELECTORS.PLACEHOLDER_CONTAINER).addClass('hidden');
|
|
|
252 |
// Register the event listers for if the user wants to change the preferences.
|
|
|
253 |
registerEventListeners(body, loggedInUserId);
|
|
|
254 |
return;
|
|
|
255 |
})
|
|
|
256 |
.catch(Notification.exception);
|
|
|
257 |
};
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* Initialise the settings page by adding event listeners to
|
|
|
261 |
* the checkboxes.
|
|
|
262 |
*
|
|
|
263 |
* @param {string} namespace The route namespace.
|
|
|
264 |
* @param {Object} header The settings header element.
|
|
|
265 |
* @param {Object} body The settings body element.
|
|
|
266 |
* @param {Object} footer The footer body element.
|
|
|
267 |
* @param {Number} loggedInUserId The logged in user id.
|
|
|
268 |
* @return {Object} jQuery promise
|
|
|
269 |
*/
|
|
|
270 |
var show = function(namespace, header, body, footer, loggedInUserId) {
|
|
|
271 |
if (!body.attr('data-init')) {
|
|
|
272 |
init(body, loggedInUserId);
|
|
|
273 |
body.attr('data-init', true);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
return $.Deferred().resolve().promise();
|
|
|
277 |
};
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* String describing this page used for aria-labels.
|
|
|
281 |
*
|
|
|
282 |
* @return {Object} jQuery promise
|
|
|
283 |
*/
|
|
|
284 |
var description = function() {
|
|
|
285 |
return Str.get_string('messagedrawerviewsettings', 'core_message');
|
|
|
286 |
};
|
|
|
287 |
|
|
|
288 |
return {
|
|
|
289 |
show: show,
|
|
|
290 |
description: description,
|
|
|
291 |
};
|
|
|
292 |
});
|