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 |
* Message users.
|
|
|
18 |
*
|
|
|
19 |
* @module report_insights/message_users
|
|
|
20 |
* @copyright 2019 David Monllao
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/str', 'core/log', 'core/modal_save_cancel', 'core/modal_events', 'core/templates',
|
|
|
24 |
'core/notification', 'core/ajax'],
|
|
|
25 |
function($, Str, Log, ModalSaveCancel, ModalEvents, Templates, Notification, Ajax) {
|
|
|
26 |
|
|
|
27 |
var SELECTORS = {
|
|
|
28 |
BULKACTIONSELECT: "#formactionid"
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Constructor.
|
|
|
33 |
*
|
|
|
34 |
* @param {String} rootNode
|
|
|
35 |
* @param {String} actionName
|
|
|
36 |
*/
|
|
|
37 |
var MessageUsers = function(rootNode, actionName) {
|
|
|
38 |
this.actionName = actionName;
|
|
|
39 |
this.attachEventListeners(rootNode);
|
|
|
40 |
};
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @var {String} actionName
|
|
|
44 |
* @private
|
|
|
45 |
*/
|
|
|
46 |
MessageUsers.prototype.actionName = null;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @var {Modal} modal
|
|
|
50 |
* @private
|
|
|
51 |
*/
|
|
|
52 |
MessageUsers.prototype.modal = null;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Attach the event listener to the send message bulk action.
|
|
|
56 |
* @param {String} rootNode
|
|
|
57 |
*/
|
|
|
58 |
MessageUsers.prototype.attachEventListeners = function(rootNode) {
|
|
|
59 |
$(rootNode + ' button[data-bulk-sendmessage]').on('click', function(e) {
|
|
|
60 |
e.preventDefault();
|
|
|
61 |
var cTarget = $(e.currentTarget);
|
|
|
62 |
|
|
|
63 |
// Using an associative array in case there is more than 1 prediction for the same user.
|
|
|
64 |
var users = {};
|
|
|
65 |
var predictionToUserMapping = cTarget.data('prediction-to-user-id');
|
|
|
66 |
|
|
|
67 |
var checkedSelector = '.insights-list input[data-togglegroup^="insight-bulk-action"][data-toggle="slave"]:checked';
|
|
|
68 |
$(checkedSelector).each(function(index, value) {
|
|
|
69 |
var predictionId = $(value).closest('tr[data-prediction-id]').data('prediction-id');
|
|
|
70 |
|
|
|
71 |
if (typeof predictionToUserMapping[predictionId] === 'undefined') {
|
|
|
72 |
Log.error('Unknown user for prediction ' + predictionId);
|
|
|
73 |
return;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
var userId = predictionToUserMapping[predictionId];
|
|
|
77 |
users[predictionId] = userId;
|
|
|
78 |
|
|
|
79 |
});
|
|
|
80 |
|
|
|
81 |
if (Object.keys(users).length === 0) {
|
|
|
82 |
return this;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
this.showSendMessage(users);
|
|
|
86 |
|
|
|
87 |
return this;
|
|
|
88 |
}.bind(this));
|
|
|
89 |
};
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Show the send message popup.
|
|
|
93 |
*
|
|
|
94 |
* @method showSendMessage
|
|
|
95 |
* @private
|
|
|
96 |
* @param {Object} users Prediction id to user id mapping.
|
|
|
97 |
* @returns {Promise}
|
|
|
98 |
*/
|
|
|
99 |
MessageUsers.prototype.showSendMessage = function(users) {
|
|
|
100 |
|
|
|
101 |
var userIds = new Set(Object.values(users));
|
|
|
102 |
|
|
|
103 |
if (userIds.length == 0) {
|
|
|
104 |
// Nothing to do.
|
|
|
105 |
return $.Deferred().resolve().promise();
|
|
|
106 |
}
|
|
|
107 |
var titlePromise = null;
|
|
|
108 |
if (userIds.size == 1) {
|
|
|
109 |
titlePromise = Str.get_string('sendbulkmessagesingle', 'core_message');
|
|
|
110 |
} else {
|
|
|
111 |
titlePromise = Str.get_string('sendbulkmessage', 'core_message', userIds.size);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// eslint-disable-next-line promise/catch-or-return
|
|
|
115 |
ModalSaveCancel.create({
|
|
|
116 |
body: Templates.render('core_user/send_bulk_message', {}),
|
|
|
117 |
title: titlePromise,
|
|
|
118 |
buttons: {
|
|
|
119 |
save: titlePromise,
|
|
|
120 |
},
|
|
|
121 |
show: true,
|
|
|
122 |
})
|
|
|
123 |
.then(function(modal) {
|
|
|
124 |
// Keep a reference to the modal.
|
|
|
125 |
this.modal = modal;
|
|
|
126 |
|
|
|
127 |
// We want to focus on the action select when the dialog is closed.
|
|
|
128 |
this.modal.getRoot().on(ModalEvents.hidden, function() {
|
|
|
129 |
$(SELECTORS.BULKACTIONSELECT).focus();
|
|
|
130 |
this.modal.getRoot().remove();
|
|
|
131 |
}.bind(this));
|
|
|
132 |
|
|
|
133 |
this.modal.getRoot().on(ModalEvents.save, this.submitSendMessage.bind(this, users));
|
|
|
134 |
|
|
|
135 |
return this.modal;
|
|
|
136 |
}.bind(this));
|
|
|
137 |
};
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Send a message to these users.
|
|
|
141 |
*
|
|
|
142 |
* @method submitSendMessage
|
|
|
143 |
* @private
|
|
|
144 |
* @param {Object} users Prediction id to user id mapping.
|
|
|
145 |
* @returns {Promise}
|
|
|
146 |
*/
|
|
|
147 |
MessageUsers.prototype.submitSendMessage = function(users) {
|
|
|
148 |
|
|
|
149 |
var messageText = this.modal.getRoot().find('form textarea').val();
|
|
|
150 |
|
|
|
151 |
var messages = [];
|
|
|
152 |
|
|
|
153 |
var userIds = new Set(Object.values(users));
|
|
|
154 |
userIds.forEach(function(userId) {
|
|
|
155 |
messages.push({touserid: userId, text: messageText});
|
|
|
156 |
});
|
|
|
157 |
|
|
|
158 |
var actionName = this.actionName;
|
|
|
159 |
var message = null;
|
|
|
160 |
return Ajax.call([{
|
|
|
161 |
methodname: 'core_message_send_instant_messages',
|
|
|
162 |
args: {messages: messages}
|
|
|
163 |
}])[0].then(function(messageIds) {
|
|
|
164 |
if (messageIds.length == 1) {
|
|
|
165 |
return Str.get_string('sendbulkmessagesentsingle', 'core_message');
|
|
|
166 |
} else {
|
|
|
167 |
return Str.get_string('sendbulkmessagesent', 'core_message', messageIds.length);
|
|
|
168 |
}
|
|
|
169 |
}).then(function(msg) {
|
|
|
170 |
|
|
|
171 |
// Save this for the following callback. Now that we got everything
|
|
|
172 |
// done we can flag this action as executed.
|
|
|
173 |
message = msg;
|
|
|
174 |
|
|
|
175 |
return Ajax.call([{
|
|
|
176 |
methodname: 'report_insights_action_executed',
|
|
|
177 |
args: {
|
|
|
178 |
actionname: actionName,
|
|
|
179 |
predictionids: Object.keys(users)
|
|
|
180 |
}
|
|
|
181 |
}])[0];
|
|
|
182 |
}).then(function() {
|
|
|
183 |
Notification.addNotification({
|
|
|
184 |
message: message,
|
|
|
185 |
type: "success"
|
|
|
186 |
});
|
|
|
187 |
return true;
|
|
|
188 |
}).catch(Notification.exception);
|
|
|
189 |
};
|
|
|
190 |
|
|
|
191 |
return /** @alias module:report_insights/message_users */ {
|
|
|
192 |
// Public variables and functions.
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* @method init
|
|
|
196 |
* @param {String} rootNode
|
|
|
197 |
* @param {String} actionName
|
|
|
198 |
* @returns {MessageUsers}
|
|
|
199 |
*/
|
|
|
200 |
'init': function(rootNode, actionName) {
|
|
|
201 |
return new MessageUsers(rootNode, actionName);
|
|
|
202 |
}
|
|
|
203 |
};
|
|
|
204 |
});
|