mod/quiz/amd/src/preflightcheck.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 class manages the confirmation pop-up (also called the pre-flight check)
  17. * that is sometimes shown when a use clicks the start attempt button.
  18. *
  19. * This is also responsible for opening the pop-up window, if the quiz requires to be in one.
  20. *
  21. * @module mod_quiz/preflightcheck
  22. * @copyright 2016 The Open University
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. * @since 3.1
  25. */
  26. define(['jquery', 'core/yui', 'core_form/changechecker'], function($, Y, FormChangeChecker) {
  27. /**
  28. * @alias module:mod_quiz/preflightcheck
  29. */
  30. var t = {
  31. confirmDialogue: null,
  32. /**
  33. * Initialise the start attempt button.
  34. *
  35. * @param {String} startButton the id of the start attempt button that we will be enhancing.
  36. * @param {String} confirmationTitle the title of the dialogue.
  37. * @param {String} confirmationForm selector for the confirmation form to show in the dialogue.
  38. * @param {String} popupoptions If not null, the quiz should be launced in a pop-up.
  39. */
  40. init: function(startButton, confirmationTitle, confirmationForm, popupoptions) {
  41. var finalStartButton = startButton;
  42. Y.use('moodle-core-notification', function() {
  43. if (Y.one(confirmationForm)) {
  44. t.confirmDialogue = new M.core.dialogue({
  45. headerContent: confirmationTitle,
  46. bodyContent: Y.one(confirmationForm),
  47. draggable: true,
  48. visible: false,
  49. center: true,
  50. modal: true,
  51. width: null,
  52. extraClasses: ['mod_quiz_preflight_popup']
  53. });
  54. Y.one(startButton).on('click', t.displayDialogue);
  55. Y.one('#id_cancel').on('click', t.hideDialogue);
  56. finalStartButton = t.confirmDialogue.get('boundingBox').one('[name="submitbutton"]');
  57. }
  58. if (popupoptions) {
  59. Y.one(finalStartButton).on('click', t.launchQuizPopup, t, popupoptions);
  60. }
  61. });
  62. },
  63. /**
  64. * Display the dialogue.
  65. * @param {Y.EventFacade} e the event being responded to, if any.
  66. */
  67. displayDialogue: function(e) {
  68. if (e) {
  69. e.halt();
  70. }
  71. t.confirmDialogue.show();
  72. },
  73. /**
  74. * Hide the dialogue.
  75. * @param {Y.EventFacade} e the event being responded to, if any.
  76. */
  77. hideDialogue: function(e) {
  78. if (e) {
  79. e.halt();
  80. }
  81. t.confirmDialogue.hide(e);
  82. },
  83. /**
  84. * Event handler for the quiz start attempt button.
  85. * @param {Event} e the event being responded to
  86. * @param {Object} popupoptions
  87. */
  88. launchQuizPopup: function(e, popupoptions) {
  89. e.halt();
  90. Y.use('io-form', function() {
  91. var form = e.target.ancestor('form');
  92. FormChangeChecker.resetFormDirtyState(form.getDOMNode());
  93. window.openpopup(e, {
  94. url: form.get('action') + '?' + Y.IO.stringify(form).replace(/\bcancel=/, 'x='),
  95. windowname: 'quizpopup',
  96. options: popupoptions,
  97. fullscreen: true,
  98. });
  99. });
  100. }
  101. };
  102. return t;
  103. });