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
 * A javascript module to handle submission confirmation for quiz.
18
 *
19
 * @module    mod_quiz/submission_confirmation
20
 * @copyright 2022 Huong Nguyen <huongnv13@gmail.com>
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since     4.1
23
 */
24
 
25
import {saveCancelPromise} from 'core/notification';
26
import Prefetch from 'core/prefetch';
27
import Templates from 'core/templates';
28
import {getString} from 'core/str';
29
 
30
const SELECTOR = {
31
    attemptSubmitButton: '.path-mod-quiz .btn-finishattempt button',
32
    attemptSubmitForm: 'form#frm-finishattempt',
33
};
34
 
35
const TEMPLATES = {
36
    submissionConfirmation: 'mod_quiz/submission_confirmation',
37
};
38
 
39
/**
40
 * Register events for attempt submit button.
41
 * @param {int} unAnsweredQuestions Total number of un-answered questions
42
 */
43
const registerEventListeners = (unAnsweredQuestions) => {
44
    const submitAction = document.querySelector(SELECTOR.attemptSubmitButton);
45
    if (submitAction) {
46
        submitAction.addEventListener('click', async(e) => {
47
            e.preventDefault();
48
            try {
49
                await saveCancelPromise(
50
                    getString('submission_confirmation', 'quiz'),
51
                    Templates.render(TEMPLATES.submissionConfirmation, {
52
                        hasunanswered: unAnsweredQuestions > 0,
53
                        totalunanswered: unAnsweredQuestions
54
                    }),
55
                    getString('submitallandfinish', 'quiz')
56
                );
57
 
58
                // Save pressed.
59
                submitAction.closest(SELECTOR.attemptSubmitForm).submit();
60
            } catch {
61
                // Cancel pressed.
62
                return;
63
            }
64
        });
65
    }
66
};
67
 
68
/**
69
 * Initialises.
70
 * @param {int} unAnsweredQuestions Total number of unanswered questions
71
 */
72
export const init = (unAnsweredQuestions) => {
73
    Prefetch.prefetchStrings('core', ['submit']);
74
    Prefetch.prefetchStrings('core_admin', ['confirmation']);
75
    Prefetch.prefetchStrings('quiz', ['submitallandfinish', 'submission_confirmation']);
76
    Prefetch.prefetchTemplate(TEMPLATES.submissionConfirmation);
77
    registerEventListeners(unAnsweredQuestions);
78
};