report/insights/amd/src/message_users.js

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