mod/quiz/accessrule/seb/amd/src/view.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. * Manage the quiz views.
  17. *
  18. * @module quizaccess_seb/view
  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 Notification from "core/notification";
  24. import * as Templates from "core/templates";
  25. import * as Str from "core/str";
  26. import ModalAlert from "core/local/modal/alert";
  27. /** @var SELECTOR List of CSS selectors. */
  28. const SELECTOR = {
  29. MAIN: '#region-main',
  30. LOADING: '.seb-loading',
  31. };
  32. /** @var Template List of mustache templates. */
  33. const TEMPLATE = {
  34. LOADING: 'quizaccess_seb/loading',
  35. };
  36. /**
  37. * Manages view when access has been granted.
  38. */
  39. export const allowAccess = () => {
  40. window.location.reload();
  41. };
  42. /**
  43. * Add an alert to page to inform that Safe Exam Browser access is being checked.
  44. *
  45. * @return {Promise}
  46. */
  47. export const addLoadingAlert = () => {
  48. return Templates.render(TEMPLATE.LOADING, {}).then((html, js) => {
  49. const alertRegion = window.document.querySelector(SELECTOR.MAIN);
  50. return Templates.prependNodeContents(alertRegion, html, js);
  51. }).catch(Notification.exception);
  52. };
  53. /**
  54. * Remove the Safe Exam Browser access check alert from the page.
  55. */
  56. export const clearLoadingAlert = () => {
  57. const alert = window.document.querySelector(SELECTOR.LOADING);
  58. if (alert) {
  59. Templates.replaceNode(alert, '', '');
  60. }
  61. };
  62. /**
  63. * Display validation failed modal.
  64. */
  65. export const showValidationFailedModal = () => {
  66. ModalAlert.create({
  67. title: Str.get_string('sebkeysvalidationfailed', 'quizaccess_seb'),
  68. body: Str.get_string('invalidkeys', 'quizaccess_seb'),
  69. large: false,
  70. show: true,
  71. }).catch(Notification.exception);
  72. };