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 ModalCancel from 'core/modal_cancel';
  25. import Notification from 'core/notification';
  26. import * as Str from 'core/str';
  27. let modal = null;
  28. /**
  29. * Event listeners for the module.
  30. *
  31. * @method clickEvent
  32. * @param {int} questionId
  33. * @param {int} contextId
  34. * @param {boolean} specificVersion Is the view listing specific question versions?
  35. */
  36. const usageEvent = async(questionId, contextId, specificVersion) => {
  37. const args = {
  38. questionid: questionId,
  39. specificversion: specificVersion,
  40. };
  41. if (modal === null) {
  42. try {
  43. modal = await ModalCancel.create({
  44. title: Str.get_string('usageheader', 'qbank_usage'),
  45. body: Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args),
  46. large: true,
  47. show: true,
  48. });
  49. } catch (e) {
  50. Notification.exception(e);
  51. return;
  52. }
  53. modal.getRoot().on('click', 'a[href].page-link', function(e) {
  54. e.preventDefault();
  55. let attr = e.target.getAttribute("href");
  56. if (attr !== '#') {
  57. args.querystring = attr;
  58. modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  59. }
  60. });
  61. // Version selection event.
  62. modal.getRoot().on('change', '#question_usage_version_dropdown', function(e) {
  63. args.questionid = e.target.value;
  64. modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  65. });
  66. } else {
  67. modal.setBody(Fragment.loadFragment('qbank_usage', 'question_usage', contextId, args));
  68. modal.show();
  69. }
  70. };
  71. /**
  72. * Entrypoint of the js.
  73. *
  74. * @method init
  75. * @param {boolean} specificVersion Is the view listing specific question versions?
  76. */
  77. export const init = (specificVersion = false) => {
  78. const target = document.querySelector('#categoryquestions');
  79. if (target !== null) {
  80. target.addEventListener('click', (e) => {
  81. if (e.target.dataset.target && e.target.dataset.target.includes('questionusagepreview')) {
  82. // Call for the event listener to listed for clicks in any usage count row.
  83. usageEvent(e.target.dataset.questionid, e.target.dataset.contextid, specificVersion);
  84. }
  85. });
  86. }
  87. };