course/amd/src/manual_completion_toggle.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. * Provides the functionality for toggling the manual completion state of a course module through
  17. * the manual completion button.
  18. *
  19. * @module core_course/manual_completion_toggle
  20. * @copyright 2021 Jun Pataleta <jun@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Templates from 'core/templates';
  24. import Notification from 'core/notification';
  25. import {toggleManualCompletion} from 'core_course/repository';
  26. import * as CourseEvents from 'core_course/events';
  27. import Pending from 'core/pending';
  28. /**
  29. * Selectors in the manual completion template.
  30. *
  31. * @type {{MANUAL_TOGGLE: string}}
  32. */
  33. const SELECTORS = {
  34. MANUAL_TOGGLE: 'button[data-action=toggle-manual-completion]',
  35. };
  36. /**
  37. * Toggle type values for the data-toggletype attribute in the core_course/completion_manual template.
  38. *
  39. * @type {{TOGGLE_UNDO: string, TOGGLE_MARK_DONE: string}}
  40. */
  41. const TOGGLE_TYPES = {
  42. TOGGLE_MARK_DONE: 'manual:mark-done',
  43. TOGGLE_UNDO: 'manual:undo',
  44. };
  45. /**
  46. * Whether the event listener has already been registered for this module.
  47. *
  48. * @type {boolean}
  49. */
  50. let registered = false;
  51. /**
  52. * Registers the click event listener for the manual completion toggle button.
  53. */
  54. export const init = () => {
  55. if (registered) {
  56. return;
  57. }
  58. document.addEventListener('click', (e) => {
  59. const toggleButton = e.target.closest(SELECTORS.MANUAL_TOGGLE);
  60. if (toggleButton) {
  61. e.preventDefault();
  62. toggleManualCompletionState(toggleButton).catch(Notification.exception);
  63. }
  64. });
  65. registered = true;
  66. };
  67. /**
  68. * Toggles the manual completion state of the module for the given user.
  69. *
  70. * @param {HTMLElement} toggleButton
  71. * @returns {Promise<void>}
  72. */
  73. const toggleManualCompletionState = async(toggleButton) => {
  74. const pendingPromise = new Pending('core_course:toggleManualCompletionState');
  75. // Make a copy of the original content of the button.
  76. const originalInnerHtml = toggleButton.innerHTML;
  77. // Disable the button to prevent double clicks.
  78. toggleButton.setAttribute('disabled', 'disabled');
  79. // Get button data.
  80. const toggleType = toggleButton.getAttribute('data-toggletype');
  81. const cmid = toggleButton.getAttribute('data-cmid');
  82. const activityname = toggleButton.getAttribute('data-activityname');
  83. // Get the target completion state.
  84. const completed = toggleType === TOGGLE_TYPES.TOGGLE_MARK_DONE;
  85. // Replace the button contents with the loading icon.
  86. Templates.renderForPromise('core/loading', {})
  87. .then((loadingHtml) => {
  88. Templates.replaceNodeContents(toggleButton, loadingHtml, '');
  89. return;
  90. }).catch(() => {});
  91. try {
  92. // Call the webservice to update the manual completion status.
  93. await toggleManualCompletion(cmid, completed);
  94. // All good so far. Refresh the manual completion button to reflect its new state by re-rendering the template.
  95. const templateContext = {
  96. cmid: cmid,
  97. activityname: activityname,
  98. overallcomplete: completed,
  99. overallincomplete: !completed,
  100. istrackeduser: true, // We know that we're tracking completion for this user given the presence of this button.
  101. };
  102. const renderObject = await Templates.renderForPromise('core_course/completion_manual', templateContext);
  103. // Replace the toggle button with the newly loaded template.
  104. const replacedNode = await Templates.replaceNode(toggleButton, renderObject.html, renderObject.js);
  105. const newToggleButton = replacedNode.pop();
  106. // Build manualCompletionToggled custom event.
  107. const withAvailability = toggleButton.getAttribute('data-withavailability');
  108. const toggledEvent = new CustomEvent(CourseEvents.manualCompletionToggled, {
  109. bubbles: true,
  110. detail: {
  111. cmid,
  112. activityname,
  113. completed,
  114. withAvailability,
  115. }
  116. });
  117. // Dispatch the manualCompletionToggled custom event.
  118. newToggleButton.dispatchEvent(toggledEvent);
  119. } catch (exception) {
  120. // In case of an error, revert the original state and appearance of the button.
  121. toggleButton.removeAttribute('disabled');
  122. toggleButton.innerHTML = originalInnerHtml;
  123. // Show the exception.
  124. Notification.exception(exception);
  125. }
  126. pendingPromise.resolve();
  127. };