user/amd/src/local/participants/bulkactions.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. * Bulk actions for lists of participants.
  17. *
  18. * @module core_user/local/participants/bulkactions
  19. * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import * as Repository from 'core_user/repository';
  23. import * as Str from 'core/str';
  24. import ModalEvents from 'core/modal_events';
  25. import SaveCancelModal from 'core/modal_save_cancel';
  26. import Notification from 'core/notification';
  27. import Templates from 'core/templates';
  28. import {add as notifyUser} from 'core/toast';
  29. /**
  30. * Show the add note popup
  31. *
  32. * @param {Number} courseid
  33. * @param {Number[]} users
  34. * @param {String[]} noteStateNames
  35. * @param {HTMLElement} stateHelpIcon
  36. * @return {Promise}
  37. */
  38. export const showAddNote = (courseid, users, noteStateNames, stateHelpIcon) => {
  39. if (!users.length) {
  40. // No users were selected.
  41. return Promise.resolve();
  42. }
  43. const states = [];
  44. for (let key in noteStateNames) {
  45. switch (key) {
  46. case 'draft':
  47. states.push({value: 'personal', label: noteStateNames[key]});
  48. break;
  49. case 'public':
  50. states.push({value: 'course', label: noteStateNames[key], selected: 1});
  51. break;
  52. case 'site':
  53. states.push({value: key, label: noteStateNames[key]});
  54. break;
  55. }
  56. }
  57. const context = {
  58. stateNames: states,
  59. stateHelpIcon: stateHelpIcon.innerHTML,
  60. };
  61. let titlePromise = null;
  62. if (users.length === 1) {
  63. titlePromise = Str.get_string('addbulknotesingle', 'core_notes');
  64. } else {
  65. titlePromise = Str.get_string('addbulknote', 'core_notes', users.length);
  66. }
  67. return SaveCancelModal.create({
  68. body: Templates.render('core_user/add_bulk_note', context),
  69. title: titlePromise,
  70. buttons: {
  71. save: titlePromise,
  72. },
  73. removeOnClose: true,
  74. show: true,
  75. })
  76. .then(modal => {
  77. modal.getRoot().on(ModalEvents.save, () => submitAddNote(courseid, users, modal));
  78. return modal;
  79. });
  80. };
  81. /**
  82. * Add a note to this list of users.
  83. *
  84. * @param {Number} courseid
  85. * @param {Number[]} users
  86. * @param {Modal} modal
  87. * @return {Promise}
  88. */
  89. const submitAddNote = (courseid, users, modal) => {
  90. const text = modal.getRoot().find('form textarea').val();
  91. const publishstate = modal.getRoot().find('form select').val();
  92. const notes = users.map(userid => {
  93. return {
  94. userid,
  95. text,
  96. courseid,
  97. publishstate,
  98. };
  99. });
  100. return Repository.createNotesForUsers(notes)
  101. .then(noteIds => {
  102. if (noteIds.length === 1) {
  103. return Str.get_string('addbulknotedonesingle', 'core_notes');
  104. } else {
  105. return Str.get_string('addbulknotedone', 'core_notes', noteIds.length);
  106. }
  107. })
  108. .then(msg => notifyUser(msg))
  109. .catch(Notification.exception);
  110. };
  111. /**
  112. * Show the send message popup.
  113. *
  114. * @param {Number[]} users
  115. * @return {Promise}
  116. */
  117. export const showSendMessage = users => {
  118. if (!users.length) {
  119. // Nothing to do.
  120. return Promise.resolve();
  121. }
  122. let titlePromise;
  123. if (users.length === 1) {
  124. titlePromise = Str.get_string('sendbulkmessagesingle', 'core_message');
  125. } else {
  126. titlePromise = Str.get_string('sendbulkmessage', 'core_message', users.length);
  127. }
  128. return SaveCancelModal.create({
  129. body: Templates.render('core_user/send_bulk_message', {}),
  130. title: titlePromise,
  131. buttons: {
  132. save: titlePromise,
  133. },
  134. removeOnClose: true,
  135. show: true,
  136. })
  137. .then(modal => {
  138. modal.getRoot().on(ModalEvents.save, (e) => {
  139. const text = modal.getRoot().find('form textarea').val();
  140. if (text.trim() === '') {
  141. modal.getRoot().find('[data-role="messagetextrequired"]').removeAttr('hidden');
  142. e.preventDefault();
  143. return;
  144. }
  145. submitSendMessage(modal, users, text);
  146. });
  147. return modal;
  148. });
  149. };
  150. /**
  151. * Send a message to these users.
  152. *
  153. * @param {Modal} modal
  154. * @param {Number[]} users
  155. * @param {String} text
  156. * @return {Promise}
  157. */
  158. const submitSendMessage = (modal, users, text) => {
  159. const messages = users.map(touserid => {
  160. return {
  161. touserid,
  162. text,
  163. };
  164. });
  165. return Repository.sendMessagesToUsers(messages)
  166. .then(messageIds => {
  167. if (messageIds.length == 1) {
  168. return Str.get_string('sendbulkmessagesentsingle', 'core_message');
  169. } else {
  170. return Str.get_string('sendbulkmessagesent', 'core_message', messageIds.length);
  171. }
  172. })
  173. .then(msg => notifyUser(msg))
  174. .catch(Notification.exception);
  175. };