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 preference for an individual notification type on the
|
|
|
18 |
* message preference page.
|
|
|
19 |
*
|
|
|
20 |
* @module core_message/notification_preference
|
|
|
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', 'core/ajax', 'core/notification', 'core_message/notification_processor'],
|
|
|
25 |
function($, Ajax, Notification, NotificationProcessor) {
|
|
|
26 |
|
|
|
27 |
const SELECTORS = {
|
|
|
28 |
PROCESSOR: '[data-processor-name]',
|
|
|
29 |
STATE_INPUTS: '[data-state] input',
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Constructor for the Preference.
|
|
|
34 |
*
|
|
|
35 |
* @class
|
|
|
36 |
* @param {object} element jQuery object root element of the preference
|
|
|
37 |
* @param {int} userId The current user id
|
|
|
38 |
*/
|
|
|
39 |
const NotificationPreference = function(element, userId) {
|
|
|
40 |
this.root = $(element);
|
|
|
41 |
this.userId = userId;
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Get the unique prefix key that identifies this user preference.
|
|
|
46 |
*
|
|
|
47 |
* @method getPreferenceKey
|
|
|
48 |
* @return {string}
|
|
|
49 |
*/
|
|
|
50 |
NotificationPreference.prototype.getPreferenceKey = function() {
|
|
|
51 |
return this.root.attr('data-preference-key');
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Get the unique key for the enabled preference.
|
|
|
56 |
*
|
|
|
57 |
* @method getEnabledPreferenceKey
|
|
|
58 |
* @return {string}
|
|
|
59 |
*/
|
|
|
60 |
NotificationPreference.prototype.getEnabledPreferenceKey = function() {
|
|
|
61 |
return this.getPreferenceKey() + '_enabled';
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Get the list of Processors available for this preference.
|
|
|
66 |
*
|
|
|
67 |
* @method getProcessors
|
|
|
68 |
* @return {array}
|
|
|
69 |
*/
|
|
|
70 |
NotificationPreference.prototype.getProcessors = function() {
|
|
|
71 |
return this.root.find(SELECTORS.PROCESSOR).map(function(index, element) {
|
|
|
72 |
return new NotificationProcessor($(element));
|
|
|
73 |
});
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Flag the preference as loading.
|
|
|
78 |
*
|
|
|
79 |
* @method startLoading
|
|
|
80 |
*/
|
|
|
81 |
NotificationPreference.prototype.startLoading = function() {
|
|
|
82 |
this.root.addClass('loading');
|
|
|
83 |
this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', true);
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Remove the loading flag for this preference.
|
|
|
88 |
*
|
|
|
89 |
* @method stopLoading
|
|
|
90 |
*/
|
|
|
91 |
NotificationPreference.prototype.stopLoading = function() {
|
|
|
92 |
this.root.removeClass('loading');
|
|
|
93 |
this.root.find(SELECTORS.STATE_INPUTS).prop('disabled', false);
|
|
|
94 |
};
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Check if the preference is loading.
|
|
|
98 |
*
|
|
|
99 |
* @method isLoading
|
|
|
100 |
* @return {Boolean}
|
|
|
101 |
*/
|
|
|
102 |
NotificationPreference.prototype.isLoading = function() {
|
|
|
103 |
return this.root.hasClass('loading');
|
|
|
104 |
};
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Persist the current state of the processors for this preference.
|
|
|
108 |
*
|
|
|
109 |
* @method save
|
|
|
110 |
* @return {object} jQuery promise
|
|
|
111 |
*/
|
|
|
112 |
NotificationPreference.prototype.save = function() {
|
|
|
113 |
if (this.isLoading()) {
|
|
|
114 |
return $.Deferred().resolve();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
this.startLoading();
|
|
|
118 |
|
|
|
119 |
let enabledValue = '';
|
|
|
120 |
|
|
|
121 |
this.getProcessors().each(function(index, processor) {
|
|
|
122 |
if (processor.isEnabled()) {
|
|
|
123 |
if (enabledValue === '') {
|
|
|
124 |
enabledValue = processor.getName();
|
|
|
125 |
} else {
|
|
|
126 |
enabledValue += ',' + processor.getName();
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
});
|
|
|
130 |
|
|
|
131 |
if (enabledValue === '') {
|
|
|
132 |
enabledValue = 'none';
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
const args = {
|
|
|
136 |
userid: this.userId,
|
|
|
137 |
preferences: [
|
|
|
138 |
{
|
|
|
139 |
type: this.getEnabledPreferenceKey(),
|
|
|
140 |
value: enabledValue,
|
|
|
141 |
}
|
|
|
142 |
],
|
|
|
143 |
};
|
|
|
144 |
|
|
|
145 |
const request = {
|
|
|
146 |
methodname: 'core_user_update_user_preferences',
|
|
|
147 |
args: args,
|
|
|
148 |
};
|
|
|
149 |
|
|
|
150 |
return Ajax.call([request])[0]
|
|
|
151 |
.fail(Notification.exception)
|
|
|
152 |
.always(function() {
|
|
|
153 |
this.stopLoading();
|
|
|
154 |
}.bind(this));
|
|
|
155 |
};
|
|
|
156 |
|
|
|
157 |
return NotificationPreference;
|
|
|
158 |
});
|