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 preferences for the list of notification types on the
|
|
|
18 |
* message preference page
|
|
|
19 |
*
|
|
|
20 |
* @module core_message/preferences_notifications_list_controller
|
|
|
21 |
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
define(['jquery',
|
|
|
25 |
'core/ajax',
|
|
|
26 |
'core/notification',
|
|
|
27 |
'core/custom_interaction_events',
|
|
|
28 |
'core_message/notification_preference',
|
|
|
29 |
'core_message/notification_processor_settings',
|
|
|
30 |
],
|
|
|
31 |
function(
|
|
|
32 |
$,
|
|
|
33 |
Ajax,
|
|
|
34 |
Notification,
|
|
|
35 |
CustomEvents,
|
|
|
36 |
NotificationPreference,
|
|
|
37 |
NotificationProcessorSettings,
|
|
|
38 |
) {
|
|
|
39 |
|
|
|
40 |
var SELECTORS = {
|
|
|
41 |
DISABLE_NOTIFICATIONS: '[data-region="disable-notification-container"] [data-disable-notifications]',
|
|
|
42 |
DISABLE_NOTIFICATIONS_CONTAINER: '[data-region="disable-notification-container"]',
|
|
|
43 |
PREFERENCE: '.preference-state',
|
|
|
44 |
PREFERENCE_ROW: '[data-region="preference-row"]',
|
|
|
45 |
PREFERENCE_INPUT: '.preference-state input',
|
|
|
46 |
PROCESSOR_SETTING: '[data-processor-setting]',
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Constructor for the PreferencesController.
|
|
|
51 |
*
|
|
|
52 |
* @class
|
|
|
53 |
* @param {object} element jQuery object root element of the preference
|
|
|
54 |
*/
|
|
|
55 |
var PreferencesController = function(element) {
|
|
|
56 |
this.root = $(element);
|
|
|
57 |
this.userId = this.root.attr('data-user-id');
|
|
|
58 |
|
|
|
59 |
this.registerEventListeners();
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Check if the preferences are all disabled.
|
|
|
64 |
*
|
|
|
65 |
* @method isDisabled
|
|
|
66 |
* @return {bool}
|
|
|
67 |
*/
|
|
|
68 |
PreferencesController.prototype.isDisabled = function() {
|
|
|
69 |
return this.root.hasClass('disabled');
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Disable all of the preferences.
|
|
|
74 |
*
|
|
|
75 |
* @method setDisabled
|
|
|
76 |
*/
|
|
|
77 |
PreferencesController.prototype.setDisabled = function() {
|
|
|
78 |
this.root.addClass('disabled');
|
|
|
79 |
this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', true);
|
|
|
80 |
};
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Enable all of the preferences.
|
|
|
84 |
*
|
|
|
85 |
* @method setEnabled
|
|
|
86 |
*/
|
|
|
87 |
PreferencesController.prototype.setEnabled = function() {
|
|
|
88 |
this.root.removeClass('disabled');
|
|
|
89 |
this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
|
|
|
90 |
};
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Update the disable all notifications user property in the DOM and
|
|
|
94 |
* send a request to update on the server.
|
|
|
95 |
*
|
|
|
96 |
* @method toggleDisableAllStatus
|
|
|
97 |
* @return {Promise}
|
|
|
98 |
*/
|
|
|
99 |
PreferencesController.prototype.toggleDisableAllStatus = function() {
|
|
|
100 |
var checkbox = $(SELECTORS.DISABLE_NOTIFICATIONS);
|
|
|
101 |
var container = $(SELECTORS.DISABLE_NOTIFICATIONS_CONTAINER);
|
|
|
102 |
var ischecked = checkbox.prop('checked');
|
|
|
103 |
|
|
|
104 |
if (container.hasClass('loading')) {
|
|
|
105 |
return $.Deferred().resolve();
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
container.addClass('loading');
|
|
|
109 |
|
|
|
110 |
var request = {
|
|
|
111 |
methodname: 'core_user_update_user_preferences',
|
|
|
112 |
args: {
|
|
|
113 |
userid: this.userId,
|
|
|
114 |
emailstop: ischecked ? 1 : 0,
|
|
|
115 |
}
|
|
|
116 |
};
|
|
|
117 |
|
|
|
118 |
return Ajax.call([request])[0]
|
|
|
119 |
.done(function() {
|
|
|
120 |
if (ischecked) {
|
|
|
121 |
this.setDisabled();
|
|
|
122 |
} else {
|
|
|
123 |
this.setEnabled();
|
|
|
124 |
}
|
|
|
125 |
}.bind(this))
|
|
|
126 |
.always(function() {
|
|
|
127 |
container.removeClass('loading');
|
|
|
128 |
})
|
|
|
129 |
.fail(Notification.exception);
|
|
|
130 |
};
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Set up all of the event listeners for the PreferencesController.
|
|
|
134 |
*
|
|
|
135 |
* @method registerEventListeners
|
|
|
136 |
*/
|
|
|
137 |
PreferencesController.prototype.registerEventListeners = function() {
|
|
|
138 |
var disabledNotificationsElement = $(SELECTORS.DISABLE_NOTIFICATIONS);
|
|
|
139 |
|
|
|
140 |
CustomEvents.define(this.root, [
|
|
|
141 |
CustomEvents.events.activate,
|
|
|
142 |
]);
|
|
|
143 |
|
|
|
144 |
this.root.on('change', function(e) {
|
|
|
145 |
if (!this.isDisabled()) {
|
|
|
146 |
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
|
|
|
147 |
var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW);
|
|
|
148 |
var preference = new NotificationPreference(preferenceRow, this.userId);
|
|
|
149 |
|
|
|
150 |
preferenceElement.addClass('loading');
|
|
|
151 |
preference.save().always(function() {
|
|
|
152 |
preferenceElement.removeClass('loading');
|
|
|
153 |
});
|
|
|
154 |
}
|
|
|
155 |
}.bind(this));
|
|
|
156 |
|
|
|
157 |
var eventFormPromise = NotificationProcessorSettings.create({});
|
|
|
158 |
|
|
|
159 |
this.root.on(CustomEvents.events.activate, SELECTORS.PROCESSOR_SETTING, function(e, data) {
|
|
|
160 |
var element = $(e.target).closest(SELECTORS.PROCESSOR_SETTING);
|
|
|
161 |
|
|
|
162 |
data.originalEvent.preventDefault();
|
|
|
163 |
|
|
|
164 |
eventFormPromise.then(function(modal) {
|
|
|
165 |
// Configure modal with element settings.
|
|
|
166 |
modal.setUserId($(element).attr('data-user-id'));
|
|
|
167 |
modal.setName($(element).attr('data-name'));
|
|
|
168 |
modal.setContextId($(element).attr('data-context-id'));
|
|
|
169 |
modal.setElement(element);
|
|
|
170 |
modal.show();
|
|
|
171 |
|
|
|
172 |
e.stopImmediatePropagation();
|
|
|
173 |
return;
|
|
|
174 |
}).catch(Notification.exception);
|
|
|
175 |
});
|
|
|
176 |
|
|
|
177 |
CustomEvents.define(disabledNotificationsElement, [
|
|
|
178 |
CustomEvents.events.activate
|
|
|
179 |
]);
|
|
|
180 |
|
|
|
181 |
disabledNotificationsElement.on(CustomEvents.events.activate, function() {
|
|
|
182 |
this.toggleDisableAllStatus();
|
|
|
183 |
}.bind(this));
|
|
|
184 |
};
|
|
|
185 |
|
|
|
186 |
return PreferencesController;
|
|
|
187 |
});
|