grade/amd/src/gradebooksetup_forms.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. * Prints the add item gradebook form
  17. *
  18. * @module core_grades
  19. * @copyright 2023 Mathew May <mathew.solutions>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  21. */
  22. import ModalForm from 'core_form/modalform';
  23. import {getString} from 'core/str';
  24. import Notification from 'core/notification';
  25. import * as FormChangeChecker from 'core_form/changechecker';
  26. import PendingPromise from 'core/pending';
  27. const Selectors = {
  28. advancedFormLink: 'a.showadvancedform'
  29. };
  30. const getDetailsFromEvent = (event) => {
  31. if (event.target.closest('[data-trigger="add-item-form"]')) {
  32. const trigger = event.target.closest('[data-trigger="add-item-form"]');
  33. return {
  34. trigger,
  35. formClass: 'core_grades\\form\\add_item',
  36. titleKey: trigger.getAttribute('data-itemid') === '-1' ? 'newitem' : 'itemsedit',
  37. args: {
  38. itemid: trigger.getAttribute('data-itemid'),
  39. },
  40. };
  41. } else if (event.target.closest('[data-trigger="add-category-form"]')) {
  42. const trigger = event.target.closest('[data-trigger="add-category-form"]');
  43. return {
  44. trigger,
  45. formClass: 'core_grades\\form\\add_category',
  46. titleKey: trigger.getAttribute('data-category') === '-1' ? 'newcategory' : 'categoryedit',
  47. args: {
  48. category: trigger.getAttribute('data-category'),
  49. },
  50. };
  51. } else if (event.target.closest('[data-trigger="add-outcome-form"]')) {
  52. const trigger = event.target.closest('[data-trigger="add-outcome-form"]');
  53. return {
  54. trigger,
  55. formClass: 'core_grades\\form\\add_outcome',
  56. titleKey: trigger.getAttribute('data-itemid') === '-1' ? 'newoutcomeitem' : 'outcomeitemsedit',
  57. args: {
  58. itemid: trigger.getAttribute('data-itemid'),
  59. },
  60. };
  61. }
  62. return null;
  63. };
  64. /**
  65. * Initialize module
  66. */
  67. export const init = () => {
  68. // Sometimes the trigger does not exist, so lets conditionally add it.
  69. document.addEventListener('click', event => {
  70. const triggerData = getDetailsFromEvent(event);
  71. if (triggerData) {
  72. event.preventDefault();
  73. const pendingPromise = new PendingPromise(`core_grades:add_item:${triggerData.args.itemid}`);
  74. const {trigger, formClass, titleKey, args} = triggerData;
  75. args.courseid = trigger.getAttribute('data-courseid');
  76. args.gpr_plugin = trigger.getAttribute('data-gprplugin');
  77. const modalForm = new ModalForm({
  78. modalConfig: {
  79. title: getString(titleKey, 'core_grades'),
  80. },
  81. formClass: formClass,
  82. args: args,
  83. saveButtonText: getString('save', 'core'),
  84. returnFocus: trigger,
  85. });
  86. // Show a toast notification when the form is submitted.
  87. modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
  88. if (event.detail.result) {
  89. new PendingPromise('core_grades:form_submitted');
  90. window.location.assign(event.detail.url);
  91. } else {
  92. Notification.addNotification({
  93. type: 'error',
  94. message: getString('saving_failed', 'core_grades')
  95. });
  96. }
  97. });
  98. modalForm.show();
  99. pendingPromise.resolve();
  100. }
  101. const showAdvancedForm = event.target.closest(Selectors.advancedFormLink);
  102. if (showAdvancedForm) {
  103. // Navigate to the advanced form page and cary over any entered data.
  104. event.preventDefault();
  105. // Do not resolve this pendingPromise - it will be cleared when the page changes.
  106. new PendingPromise('core_grades:show_advanced_form');
  107. const form = event.target.closest('form');
  108. form.action = showAdvancedForm.href;
  109. // Disable the form change checker as we are going to carry over the data to the advanced form.
  110. FormChangeChecker.disableAllChecks();
  111. form.submit();
  112. }
  113. });
  114. };