admin/tool/dataprivacy/amd/src/data_deletion.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. * Request actions.
  17. *
  18. * @module tool_dataprivacy/data_deletion
  19. * @copyright 2018 Jun Pataleta
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define([
  23. 'jquery',
  24. 'core/ajax',
  25. 'core/notification',
  26. 'core/str',
  27. 'core/modal_save_cancel',
  28. 'core/modal_events'],
  29. function($, Ajax, Notification, Str, ModalSaveCancel, ModalEvents) {
  30. /**
  31. * List of action selectors.
  32. *
  33. * @type {{MARK_FOR_DELETION: string}}
  34. * @type {{SELECT_ALL: string}}
  35. */
  36. var ACTIONS = {
  37. MARK_FOR_DELETION: '[data-action="markfordeletion"]',
  38. SELECT_ALL: '[data-action="selectall"]',
  39. };
  40. /**
  41. * List of selectors.
  42. *
  43. * @type {{SELECTCONTEXT: string}}
  44. */
  45. var SELECTORS = {
  46. SELECTCONTEXT: '.selectcontext',
  47. };
  48. /**
  49. * DataDeletionActions class.
  50. */
  51. var DataDeletionActions = function() {
  52. this.registerEvents();
  53. };
  54. /**
  55. * Register event listeners.
  56. */
  57. DataDeletionActions.prototype.registerEvents = function() {
  58. $(ACTIONS.MARK_FOR_DELETION).click(function(e) {
  59. e.preventDefault();
  60. var selectedIds = [];
  61. $(SELECTORS.SELECTCONTEXT).each(function() {
  62. var checkbox = $(this);
  63. if (checkbox.is(':checked')) {
  64. selectedIds.push(checkbox.val());
  65. }
  66. });
  67. showConfirmation(selectedIds);
  68. });
  69. $(ACTIONS.SELECT_ALL).change(function(e) {
  70. e.preventDefault();
  71. var selectallnone = $(this);
  72. if (selectallnone.is(':checked')) {
  73. $(SELECTORS.SELECTCONTEXT).attr('checked', 'checked');
  74. } else {
  75. $(SELECTORS.SELECTCONTEXT).removeAttr('checked');
  76. }
  77. });
  78. };
  79. /**
  80. * Show the confirmation dialogue.
  81. *
  82. * @param {Array} ids The array of expired context record IDs.
  83. */
  84. function showConfirmation(ids) {
  85. var keys = [
  86. {
  87. key: 'confirm',
  88. component: 'moodle'
  89. },
  90. {
  91. key: 'confirmcontextdeletion',
  92. component: 'tool_dataprivacy'
  93. }
  94. ];
  95. var wsfunction = 'tool_dataprivacy_confirm_contexts_for_deletion';
  96. var modalTitle = '';
  97. Str.get_strings(keys).then(function(langStrings) {
  98. modalTitle = langStrings[0];
  99. var confirmMessage = langStrings[1];
  100. return ModalSaveCancel.create({
  101. title: modalTitle,
  102. body: confirmMessage,
  103. });
  104. }).then(function(modal) {
  105. modal.setSaveButtonText(modalTitle);
  106. // Handle save event.
  107. modal.getRoot().on(ModalEvents.save, function() {
  108. // Confirm the request.
  109. var params = {
  110. 'ids': ids
  111. };
  112. var request = {
  113. methodname: wsfunction,
  114. args: params
  115. };
  116. Ajax.call([request])[0].done(function(data) {
  117. if (data.result) {
  118. window.location.reload();
  119. } else {
  120. Notification.addNotification({
  121. message: data.warnings[0].message,
  122. type: 'error'
  123. });
  124. }
  125. }).fail(Notification.exception);
  126. });
  127. // Handle hidden event.
  128. modal.getRoot().on(ModalEvents.hidden, function() {
  129. // Destroy when hidden.
  130. modal.destroy();
  131. });
  132. return modal;
  133. }).done(function(modal) {
  134. modal.show();
  135. }).fail(Notification.exception);
  136. }
  137. return DataDeletionActions;
  138. });