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 |
* This module has the code to make the Re-open attempt button work, if present.
|
|
|
18 |
*
|
|
|
19 |
* That is, it looks for buttons with HTML like
|
|
|
20 |
* <button type="button" data-action="reopen-attempt" data-attempt-id="227000" data-after-action-url="/mod/quiz/report.php">
|
|
|
21 |
* and if that is clicked, it first shows an 'Are you sure' pop-up, and if they are sure,
|
|
|
22 |
* the attempt is re-opened, and then the page reloads.
|
|
|
23 |
*
|
|
|
24 |
* @module mod_quiz/reopen_attempt_ui
|
|
|
25 |
* @copyright 2023 The Open University
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
import {exception as displayException} from 'core/notification';
|
|
|
30 |
import {call as fetchMany} from 'core/ajax';
|
|
|
31 |
import {getString} from 'core/str';
|
|
|
32 |
import {saveCancelPromise} from 'core/notification';
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Handle a click if it is on one of our buttons.
|
|
|
36 |
*
|
|
|
37 |
* @param {MouseEvent} e the click event.
|
|
|
38 |
*/
|
|
|
39 |
const reopenButtonClicked = async(e) => {
|
|
|
40 |
if (!(e.target instanceof HTMLElement) || !e.target.matches('button[data-action="reopen-attempt"]')) {
|
|
|
41 |
return;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
e.preventDefault();
|
|
|
45 |
const attemptId = e.target.dataset.attemptId;
|
|
|
46 |
|
|
|
47 |
try {
|
|
|
48 |
// We fetch the confirmation message from the server now, so the message is based
|
|
|
49 |
// on the latest state of the attempt, rather than when the containing page loaded.
|
|
|
50 |
const messages = fetchMany([{
|
|
|
51 |
methodname: 'mod_quiz_get_reopen_attempt_confirmation',
|
|
|
52 |
args: {
|
|
|
53 |
"attemptid": attemptId,
|
|
|
54 |
},
|
|
|
55 |
}]);
|
|
|
56 |
|
|
|
57 |
await saveCancelPromise(
|
|
|
58 |
getString('reopenattemptareyousuretitle', 'mod_quiz'),
|
|
|
59 |
messages[0],
|
|
|
60 |
getString('reopenattempt', 'mod_quiz'),
|
|
|
61 |
{triggerElement: e.target},
|
|
|
62 |
);
|
|
|
63 |
|
|
|
64 |
await (fetchMany([{
|
|
|
65 |
methodname: 'mod_quiz_reopen_attempt',
|
|
|
66 |
args: {
|
|
|
67 |
"attemptid": attemptId,
|
|
|
68 |
},
|
|
|
69 |
}])[0]);
|
|
|
70 |
window.location = M.cfg.wwwroot + e.target.dataset.afterActionUrl;
|
|
|
71 |
|
|
|
72 |
} catch (error) {
|
|
|
73 |
if (error.type === 'modal-save-cancel:cancel') {
|
|
|
74 |
// User clicked Cancel, so do nothing.
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
await displayException(error);
|
|
|
78 |
}
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
export const init = () => {
|
|
|
82 |
document.addEventListener('click', reopenButtonClicked);
|
|
|
83 |
};
|