admin/tool/policy/amd/src/policyactions.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. * Policy actions.
  17. *
  18. * @module tool_policy/policyactions
  19. * @copyright 2018 Sara Arjona (sara@moodle.com)
  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/modal',
  27. ], function($, Ajax, Notification, Modal) {
  28. /**
  29. * PolicyActions class.
  30. *
  31. * @param {jQuery} root
  32. */
  33. var PolicyActions = function(root) {
  34. this.registerEvents(root);
  35. };
  36. /**
  37. * Register event listeners.
  38. *
  39. * @param {jQuery} root
  40. */
  41. PolicyActions.prototype.registerEvents = function(root) {
  42. root.on("click", function(e) {
  43. e.preventDefault();
  44. var versionid = $(this).data('versionid');
  45. var behalfid = $(this).data('behalfid');
  46. var params = {
  47. 'versionid': versionid,
  48. 'behalfid': behalfid
  49. };
  50. var request = {
  51. methodname: 'tool_policy_get_policy_version',
  52. args: params
  53. };
  54. var modalTitle = $.Deferred();
  55. var modalBody = $.Deferred();
  56. var modal = Modal.create({
  57. title: modalTitle,
  58. body: modalBody,
  59. large: true,
  60. removeOnClose: true,
  61. show: true,
  62. })
  63. .catch(Notification.exception);
  64. // Make the request now that the modal is configured.
  65. var promises = Ajax.call([request]);
  66. $.when(promises[0]).then(function(data) {
  67. if (data.result.policy) {
  68. modalTitle.resolve(data.result.policy.name);
  69. modalBody.resolve(data.result.policy.content);
  70. return data;
  71. } else {
  72. throw new Error(data.warnings[0].message);
  73. }
  74. }).catch(function(message) {
  75. modal.then(function(modal) {
  76. modal.hide();
  77. return modal;
  78. })
  79. .catch(Notification.exception);
  80. return Notification.addNotification({
  81. message: message,
  82. type: 'error'
  83. });
  84. });
  85. });
  86. };
  87. return /** @alias module:tool_policy/policyactions */ {
  88. // Public variables and functions.
  89. /**
  90. * Initialise the actions helper.
  91. *
  92. * @method init
  93. * @param {object} root
  94. * @return {PolicyActions}
  95. */
  96. 'init': function(root) {
  97. root = $(root);
  98. return new PolicyActions(root);
  99. }
  100. };
  101. });