mod/quiz/amd/src/question_slot.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. * Render the question slot template for each question in the quiz edit view.
  17. *
  18. * @module mod_quiz/question_slot
  19. * @copyright 2021 Catalyst IT Australia Pty Ltd
  20. * @author Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import {call as fetchMany} from 'core/ajax';
  24. import Notification from 'core/notification';
  25. /**
  26. * Set the question version for the slot.
  27. *
  28. * @param {Number} slotId
  29. * @param {Number} newVersion
  30. * @return {Array} The modified question version
  31. */
  32. const setQuestionVersion = (slotId, newVersion) => fetchMany([{
  33. methodname: 'mod_quiz_set_question_version',
  34. args: {
  35. slotid: slotId,
  36. newversion: newVersion,
  37. }
  38. }])[0];
  39. /**
  40. * Replace the container with a new version.
  41. */
  42. const registerEventListeners = () => {
  43. document.addEventListener('change', e => {
  44. if (!e.target.matches('[data-action="mod_quiz-select_slot"][data-slot-id]')) {
  45. return;
  46. }
  47. const slotId = e.target.dataset.slotId;
  48. const newVersion = parseInt(e.target.value);
  49. setQuestionVersion(slotId, newVersion === 0 ? null : newVersion)
  50. .then(() => {
  51. location.reload();
  52. return;
  53. })
  54. .catch(Notification.exception);
  55. });
  56. };
  57. /**
  58. * Entrypoint of the js.
  59. */
  60. export const init = () => {
  61. registerEventListeners();
  62. };