report/insights/amd/src/actions.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. * Module to manage report insights actions that are executed using AJAX.
  17. *
  18. * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. /**
  22. * This module manages prediction actions that require AJAX requests.
  23. *
  24. * @module report_insights/actions
  25. */
  26. define(['jquery', 'core/str', 'core/ajax', 'core/notification', 'core/url', 'core/modal_factory', 'core/modal_events'],
  27. function($, Str, Ajax, Notification, Url, ModalFactory, ModalEvents) {
  28. return {
  29. /**
  30. * Attach on click handlers for bulk actions.
  31. *
  32. * @param {String} rootNode
  33. * @access public
  34. */
  35. initBulk: function(rootNode) {
  36. /**
  37. * Executes the provided action.
  38. *
  39. * @param {Array} predictionIds
  40. * @param {Array} predictionContainers
  41. * @param {String} actionName
  42. * @return {Promise}
  43. */
  44. var executeAction = function(predictionIds, predictionContainers, actionName) {
  45. return Ajax.call([
  46. {
  47. methodname: 'report_insights_action_executed',
  48. args: {
  49. predictionids: predictionIds,
  50. actionname: actionName
  51. }
  52. }
  53. ])[0].then(function() {
  54. // Remove the selected elements from the list.
  55. var tableNode = false;
  56. predictionContainers.forEach(function(el) {
  57. if (tableNode === false) {
  58. tableNode = el.closest('table');
  59. }
  60. el.remove();
  61. });
  62. if (tableNode.find('tbody > tr').length === 0) {
  63. let params = {
  64. contextid: tableNode.closest('div.insight-container').data('context-id'),
  65. modelid: tableNode.closest('div.insight-container').data('model-id')
  66. };
  67. window.location.assign(Url.relativeUrl("report/insights/insights.php", params, false));
  68. }
  69. return;
  70. }).catch(Notification.exception);
  71. };
  72. $(rootNode + ' [data-bulk-actionname]').on('click', function(e) {
  73. e.preventDefault();
  74. var action = $(e.currentTarget);
  75. var actionName = action.data('bulk-actionname');
  76. var actionVisibleName = action.text().trim();
  77. var predictionIds = [];
  78. var predictionContainers = [];
  79. $('.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked').each(function() {
  80. var container = $(this).closest('tr[data-prediction-id]');
  81. predictionContainers.push(container);
  82. predictionIds.push(container.data('prediction-id'));
  83. });
  84. if (predictionIds.length === 0) {
  85. // No items selected message.
  86. return this;
  87. }
  88. var strings = [];
  89. Str.get_strings([{
  90. key: 'confirmbulkaction',
  91. component: 'report_insights',
  92. param: {
  93. action: actionVisibleName,
  94. nitems: predictionIds.length
  95. }
  96. }, {
  97. key: 'confirm',
  98. component: 'moodle'
  99. }]
  100. ).then(function(strs) {
  101. strings = strs;
  102. return ModalFactory.create({
  103. type: ModalFactory.types.SAVE_CANCEL,
  104. title: actionVisibleName,
  105. body: strings[0],
  106. });
  107. }).then(function(modal) {
  108. modal.setSaveButtonText(strings[1]);
  109. modal.show();
  110. modal.getRoot().on(ModalEvents.save, function() {
  111. // The action is now confirmed, sending an action for it.
  112. return executeAction(predictionIds, predictionContainers, actionName);
  113. });
  114. return modal;
  115. }).catch(Notification.exception);
  116. return this;
  117. });
  118. },
  119. };
  120. });