mod/quiz/accessrule/seb/amd/src/validate_quiz_access.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. * Validate Safe Exam Browser access keys.
  17. *
  18. * @module quizaccess_seb/validate_quiz_access
  19. * @author Andrew Madden <andrewmadden@catalyst-au.net>
  20. * @copyright 2021 Catalyst IT
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Ajax from 'core/ajax';
  24. import Config from 'core/config';
  25. import Notification from "core/notification";
  26. import * as View from 'quizaccess_seb/view';
  27. // SafeExamBrowser object will be automatically initialized if using the SafeExamBrowser application.
  28. window.SafeExamBrowser = window.SafeExamBrowser || null;
  29. /**
  30. * Once the keys are fetched, action checking access.
  31. *
  32. * @param {init} cmid Value of course module id of the quiz.
  33. * @param {boolean} autoreconfigure Value of Moodle setting: quizaccess_seb/autoreconfigureseb.
  34. */
  35. const safeExamBrowserKeysUpdated = (cmid, autoreconfigure = false) => {
  36. // Action opening up the quiz.
  37. isQuizAccessValid(cmid).then((response) => {
  38. // Show the alert for an extra second to allow user to see it.
  39. setTimeout(View.clearLoadingAlert, 1000);
  40. if (response.configkey && response.browserexamkey) {
  41. View.allowAccess();
  42. } else {
  43. // If autoreconfigureseb is enabled, attempt to reconfigure page with quiz settings.
  44. if (autoreconfigure === true && response.configkey === false) {
  45. reconfigureSafeExamBrowser(cmid);
  46. }
  47. setTimeout(View.showValidationFailedModal, 1000);
  48. }
  49. return response;
  50. }).catch(err => {
  51. Notification.exception(err);
  52. });
  53. };
  54. /**
  55. * Validate keys in Moodle backend.
  56. *
  57. * @param {init} cmid Value of course module id of the quiz.
  58. * @return {Promise}
  59. */
  60. const isQuizAccessValid = (cmid) => {
  61. const request = {
  62. methodname: 'quizaccess_seb_validate_quiz_keys',
  63. args: {
  64. cmid: cmid,
  65. url: window.location.href,
  66. configkey: window.SafeExamBrowser.security.configKey,
  67. browserexamkey: window.SafeExamBrowser.security.browserExamKey
  68. },
  69. };
  70. return Ajax.call([request])[0];
  71. };
  72. /**
  73. * Check if the key is not yet set.
  74. *
  75. * @param {string} key config key or browser exam key.
  76. * @return {boolean}
  77. */
  78. const isKeyEmpty = (key) => {
  79. // If the SafeExamBrowser object is defined, the default 'empty' value of the configKey and browserExamKey is ':'.
  80. return key === ":";
  81. };
  82. /**
  83. * Reload Safe Exam Browser with current quiz configuration.
  84. *
  85. * @param {init} cmid Value of course module id of the quiz.
  86. */
  87. const reconfigureSafeExamBrowser = (cmid) => {
  88. const domain = Config.wwwroot.replace(/^http/i, 'seb');
  89. const redirecturl = domain + '/mod/quiz/accessrule/seb/config.php?cmid=' + cmid;
  90. document.location.replace(redirecturl);
  91. };
  92. /**
  93. * Initialize the process of fetching the keys.
  94. *
  95. * @param {init} cmid Value of course module id of the quiz.
  96. * @param {boolean} autoreconfigure Value of Moodle setting: quizaccess_seb/autoreconfigureseb.
  97. */
  98. export const init = async(cmid, autoreconfigure = false) => {
  99. // If the SafeExamBrowser object is instantiated, try and use it to fetch the access keys.
  100. if (window.SafeExamBrowser !== null) {
  101. await View.addLoadingAlert();
  102. // If the SEB keys are already set, we can call our callback directly.
  103. if (!isKeyEmpty(window.SafeExamBrowser.security.configKey) || !isKeyEmpty(window.SafeExamBrowser.security.browserExamKey)) {
  104. safeExamBrowserKeysUpdated(cmid, autoreconfigure);
  105. } else {
  106. window.SafeExamBrowser.security.updateKeys(safeExamBrowserKeysUpdated);
  107. }
  108. }
  109. };