Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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