mod/quiz/amd/src/reopen_attempt_ui.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. * This module has the code to make the Re-open attempt button work, if present.
  17. *
  18. * That is, it looks for buttons with HTML like
  19. * &lt;button type="button" data-action="reopen-attempt" data-attempt-id="227000" data-after-action-url="/mod/quiz/report.php">
  20. * and if that is clicked, it first shows an 'Are you sure' pop-up, and if they are sure,
  21. * the attempt is re-opened, and then the page reloads.
  22. *
  23. * @module mod_quiz/reopen_attempt_ui
  24. * @copyright 2023 The Open University
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. import {exception as displayException} from 'core/notification';
  28. import {call as fetchMany} from 'core/ajax';
  29. import {getString} from 'core/str';
  30. import {saveCancelPromise} from 'core/notification';
  31. /**
  32. * Handle a click if it is on one of our buttons.
  33. *
  34. * @param {MouseEvent} e the click event.
  35. */
  36. const reopenButtonClicked = async(e) => {
  37. if (!(e.target instanceof HTMLElement) || !e.target.matches('button[data-action="reopen-attempt"]')) {
  38. return;
  39. }
  40. e.preventDefault();
  41. const attemptId = e.target.dataset.attemptId;
  42. try {
  43. // We fetch the confirmation message from the server now, so the message is based
  44. // on the latest state of the attempt, rather than when the containing page loaded.
  45. const messages = fetchMany([{
  46. methodname: 'mod_quiz_get_reopen_attempt_confirmation',
  47. args: {
  48. "attemptid": attemptId,
  49. },
  50. }]);
  51. await saveCancelPromise(
  52. getString('reopenattemptareyousuretitle', 'mod_quiz'),
  53. messages[0],
  54. getString('reopenattempt', 'mod_quiz'),
  55. {triggerElement: e.target},
  56. );
  57. await (fetchMany([{
  58. methodname: 'mod_quiz_reopen_attempt',
  59. args: {
  60. "attemptid": attemptId,
  61. },
  62. }])[0]);
  63. window.location = M.cfg.wwwroot + e.target.dataset.afterActionUrl;
  64. } catch (error) {
  65. if (error.type === 'modal-save-cancel:cancel') {
  66. // User clicked Cancel, so do nothing.
  67. return;
  68. }
  69. await displayException(error);
  70. }
  71. };
  72. export const init = () => {
  73. document.addEventListener('click', reopenButtonClicked);
  74. };