question/bank/usage/amd/src/usage.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. * Usage column selector js.
  17. *
  18. * @module qbank_usage/usage
  19. * @copyright 2021 Catalyst IT Australia Pty Ltd
  20. * @author Safat Shahin <safatshahin@catalyst-au.net>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Fragment from 'core/fragment';
  24. import ModalFactory from 'core/modal_factory';
  25. import Notification from 'core/notification';
  26. import * as Str from 'core/str';
  27. /**
  28. * Event listeners for the module.
  29. *
  30. * @method clickEvent
  31. * @param {int} questionId
  32. * @param {int} contextId
  33. */
  34. const usageEvent = (questionId, contextId) => {
  35. let args = {
  36. questionid: questionId
  37. };
  38. ModalFactory.create({
  39. type: ModalFactory.types.CANCEL,
  40. title: Str.get_string('usageheader', 'qbank_usage'),
  41. body: Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args),
  42. large: true,
  43. }).then((modal) => {
  44. modal.show();
  45. modal.getRoot().on('click', 'a[href].page-link', function(e) {
  46. e.preventDefault();
  47. let attr = e.target.getAttribute("href");
  48. if (attr !== '#') {
  49. args.querystring = attr;
  50. modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  51. }
  52. });
  53. // Version selection event.
  54. modal.getRoot().on('change', '#question_usage_version_dropdown', function(e) {
  55. args.questionid = e.target.value;
  56. modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  57. });
  58. return modal;
  59. }).fail(Notification.exception);
  60. };
  61. /**
  62. * Entrypoint of the js.
  63. *
  64. * @method init
  65. * @param {string} questionSelector the question usage identifier.
  66. * @param {int} contextId the question context id.
  67. */
  68. export const init = (questionSelector, contextId) => {
  69. let target = document.querySelector(questionSelector);
  70. let questionId = target.getAttribute('data-questionid');
  71. target.addEventListener('click', () => {
  72. // Call for the event listener to listed for clicks in any usage count row.
  73. usageEvent(questionId, contextId);
  74. });
  75. };