Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 19... Línea 19...
19
 * @module     mod_quiz/modal_quiz_question_bank
19
 * @module     mod_quiz/modal_quiz_question_bank
20
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
20
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
22
 */
Línea 23... Línea -...
23
 
-
 
24
import $ from 'jquery';
23
 
25
import Modal from './add_question_modal';
24
import Modal from './add_question_modal';
26
import * as Fragment from 'core/fragment';
25
import * as Fragment from 'core/fragment';
27
import * as FormChangeChecker from 'core_form/changechecker';
26
import * as FormChangeChecker from 'core_form/changechecker';
-
 
27
import * as ModalEvents from 'core/modal_events';
Línea 28... Línea 28...
28
import * as ModalEvents from 'core/modal_events';
28
import * as Notification from 'core/notification';
29
 
29
 
30
const SELECTORS = {
30
const SELECTORS = {
31
    ADD_TO_QUIZ_CONTAINER: 'td.addtoquizaction',
31
    ADD_TO_QUIZ_CONTAINER: 'td.addtoquizaction',
32
    ANCHOR: 'a[href]',
32
    ANCHOR: 'a[href]',
33
    PREVIEW_CONTAINER: 'td.previewquestionaction',
33
    PREVIEW_CONTAINER: 'td.previewquestionaction',
-
 
34
    ADD_QUESTIONS_FORM: 'form#questionsubmit',
-
 
35
    SORTERS: '.sorters',
-
 
36
    SWITCH_TO_OTHER_BANK: 'button[data-action="switch-question-bank"]',
-
 
37
    NEW_BANKMOD_ID: 'data-newmodid',
-
 
38
    BANK_SEARCH: '#searchbanks',
-
 
39
    GO_BACK_BUTTON: 'button[data-action="go-back"]',
34
    ADD_QUESTIONS_FORM: 'form#questionsubmit',
40
    ADD_ON_PAGE_FORM_ELEMENT: 'input[name="addonpage"]',
Línea 35... Línea 41...
35
    SORTERS: '.sorters',
41
    CMID_FORM_ELEMENT: 'form#questionsubmit input[name="cmid"]',
36
};
42
};
Línea 37... Línea 43...
37
 
43
 
38
export default class ModalQuizQuestionBank extends Modal {
44
export default class ModalQuizQuestionBank extends Modal {
39
    static TYPE = 'mod_quiz-quiz-question-bank';
45
    static TYPE = 'mod_quiz-quiz-question-bank';
40
 
46
 
-
 
47
    /**
-
 
48
     * Create the question bank modal.
41
    /**
49
     *
42
     * Create the question bank modal.
50
     * @param {Number} contextId Current module context id.
43
     *
51
     * @param {Number} bankCmId Current question bank course module id.
44
     * @param {Number} contextId Current context id.
52
     * @param {Number} quizCmId Current quiz course module id.
45
     */
53
     */
46
    static init(contextId) {
54
    static init(contextId, bankCmId, quizCmId) {
47
        const selector = '.menu [data-action="questionbank"]';
55
        const selector = '.menu [data-action="questionbank"]';
48
        document.addEventListener('click', (e) => {
56
        document.addEventListener('click', (e) => {
49
            const trigger = e.target.closest(selector);
57
            const trigger = e.target.closest(selector);
Línea 50... Línea 58...
50
            if (!trigger) {
58
            if (!trigger) {
51
                return;
59
                return;
-
 
60
            }
-
 
61
            e.preventDefault();
52
            }
62
 
53
            e.preventDefault();
63
            ModalQuizQuestionBank.create({
54
 
64
                contextId,
55
            ModalQuizQuestionBank.create({
65
                quizCmId,
56
                contextId,
66
                bankCmId,
Línea 88... Línea 98...
88
     *
98
     *
89
     * @method reloadBodyContent
99
     * @method reloadBodyContent
90
     * @param {string} querystring URL encoded string.
100
     * @param {string} querystring URL encoded string.
91
     */
101
     */
92
    reloadBodyContent(querystring) {
102
    reloadBodyContent(querystring) {
93
        // Load the question bank fragment to be displayed in the modal.
103
        // Load the question bank fragment to be displayed in the modal and hide the 'go back' button.
-
 
104
        this.hideFooter();
-
 
105
        this.setTitle(this.originalTitle);
94
        this.setBody(Fragment.loadFragment(
106
        this.setBody(Fragment.loadFragment(
95
            'mod_quiz',
107
            'mod_quiz',
96
            'quiz_question_bank',
108
            'quiz_question_bank',
97
            this.getContextId(),
109
            this.getContextId(),
98
            {
110
            {
99
                querystring,
111
                querystring,
-
 
112
                quizcmid: this.quizCmId,
-
 
113
                bankcmid: this.bankCmId,
100
            }
114
            }
101
        ));
115
        ));
102
    }
116
    }
Línea 103... Línea 117...
103
 
117
 
Línea 110... Línea 124...
110
     * @param {object} anchorElement The anchor element that was triggered
124
     * @param {object} anchorElement The anchor element that was triggered
111
     */
125
     */
112
    handleAddToQuizEvent(e, anchorElement) {
126
    handleAddToQuizEvent(e, anchorElement) {
113
        // If the user clicks the plus icon to add the question to the page
127
        // If the user clicks the plus icon to add the question to the page
114
        // directly then we need to intercept the click in order to adjust the
128
        // directly then we need to intercept the click in order to adjust the
115
        // href and include the correct add on page id before the page is
129
        // href and include the correct add on page id and cmid before the page is
116
        // redirected.
130
        // redirected.
117
        const href = new URL(anchorElement.attr('href'));
131
        const href = new URL(anchorElement.getAttribute('href'));
118
        href.searchParams.set('addonpage', this.getAddOnPageId());
132
        href.searchParams.set('addonpage', this.getAddOnPageId());
-
 
133
        href.searchParams.set('cmid', this.quizCmId);
119
        anchorElement.attr('href', href);
134
        anchorElement.setAttribute('href', href);
120
    }
135
    }
Línea 121... Línea 136...
121
 
136
 
122
    /**
137
    /**
123
     * Set up all of the event handling for the modal.
138
     * Set up all of the event handling for the modal.
Línea 128... Línea 143...
128
        // Apply parent event listeners.
143
        // Apply parent event listeners.
129
        super.registerEventListeners(this);
144
        super.registerEventListeners(this);
Línea 130... Línea 145...
130
 
145
 
131
        this.getModal().on('submit', SELECTORS.ADD_QUESTIONS_FORM, (e) => {
146
        this.getModal().on('submit', SELECTORS.ADD_QUESTIONS_FORM, (e) => {
132
            // If the user clicks on the "Add selected questions to the quiz" button to add some questions to the page
147
            // If the user clicks on the "Add selected questions to the quiz" button to add some questions to the page
133
            // then we need to intercept the submit in order to include the correct "add on page id" before the form is
148
            // then we need to intercept the submit in order to include the correct "add on page id"
134
            // submitted.
149
            // and the quizmod id before the form is submitted.
-
 
150
            const formElement = e.currentTarget;
135
            const formElement = $(e.currentTarget);
151
            document.querySelector(SELECTORS.ADD_ON_PAGE_FORM_ELEMENT).setAttribute('value', this.getAddOnPageId());
-
 
152
 
-
 
153
            // We also need to set the form cmid & action as the quiz modid as this could be coming from a module that isn't a quiz.
-
 
154
            document.querySelector(SELECTORS.CMID_FORM_ELEMENT).setAttribute('value', this.quizCmId);
136
 
155
            const actionUrl = new URL(formElement.getAttribute('action'));
-
 
156
            actionUrl.searchParams.set('cmid', this.quizCmId);
-
 
157
            formElement.setAttribute('action', actionUrl.toString());
-
 
158
        });
-
 
159
 
-
 
160
        this.getModal().on('click', SELECTORS.SWITCH_TO_OTHER_BANK, () => {
137
            $('<input />').attr('type', 'hidden')
161
            this.handleSwitchBankContentReload(SELECTORS.BANK_SEARCH)
-
 
162
                .then(function(ModalQuizQuestionBank) {
-
 
163
                        document.querySelector(SELECTORS.BANK_SEARCH)?.addEventListener('change', (e) => {
138
                .attr('name', "addonpage")
164
                            const bankCmId = e.currentTarget.value;
-
 
165
                            if (bankCmId > 0) {
-
 
166
                                ModalQuizQuestionBank.bankCmId = bankCmId;
-
 
167
                                ModalQuizQuestionBank.reloadBodyContent(window.location.search);
-
 
168
                            }
-
 
169
                        });
-
 
170
                        document.querySelector(SELECTORS.GO_BACK_BUTTON).addEventListener('click', (e) => {
-
 
171
                            ModalQuizQuestionBank.bankCmId = e.currentTarget.value;
-
 
172
                            ModalQuizQuestionBank.reloadBodyContent(window.location.search);
-
 
173
                        });
-
 
174
                    }
139
                .attr('value', this.getAddOnPageId())
175
                )
140
                .appendTo(formElement);
176
                .catch(Notification.exception);
Línea 141... Línea 177...
141
        });
177
        });
142
 
178
 
Línea 143... Línea 179...
143
        this.getModal().on('click', SELECTORS.ANCHOR, (e) => {
179
        this.getModal().on('click', SELECTORS.ANCHOR, (e) => {
144
            const anchorElement = $(e.currentTarget);
180
            const anchorElement = e.currentTarget;
145
 
181
 
146
            // If the anchor element was the add to quiz link.
182
            // If the anchor element was the add to quiz link.
147
            if (anchorElement.closest(SELECTORS.ADD_TO_QUIZ_CONTAINER).length) {
183
            if (anchorElement.closest(SELECTORS.ADD_TO_QUIZ_CONTAINER)) {
Línea 148... Línea 184...
148
                this.handleAddToQuizEvent(e, anchorElement);
184
                this.handleAddToQuizEvent(e, anchorElement);
149
                return;
185
                return;
150
            }
186
            }
151
 
187
 
Línea 152... Línea 188...
152
            // If the anchor element was a preview question link.
188
            // If the anchor element was a preview question link.
153
            if (anchorElement.closest(SELECTORS.PREVIEW_CONTAINER).length) {
189
            if (anchorElement.closest(SELECTORS.PREVIEW_CONTAINER)) {
154
                return;
190
                return;
155
            }
191
            }
Línea -... Línea 192...
-
 
192
 
-
 
193
            // Sorting links have their own handler.
-
 
194
            if (anchorElement.closest(SELECTORS.SORTERS)) {
-
 
195
                return;
-
 
196
            }
-
 
197
 
-
 
198
            if (anchorElement.closest('a[' + SELECTORS.NEW_BANKMOD_ID + ']')) {
-
 
199
                this.bankCmId = anchorElement.getAttribute(SELECTORS.NEW_BANKMOD_ID);
-
 
200
 
156
 
201
                // We need to clear the filter as we are about to reload the content.
157
            // Sorting links have their own handler.
202
                const url = new URL(location.href);
158
            if (anchorElement.closest(SELECTORS.SORTERS).length) {
203
                url.searchParams.delete('filter');
159
                return;
204
                history.pushState({}, '', url);
Línea 160... Línea 205...
160
            }
205
            }
161
 
206
 
162
            // Anything else means reload the pop-up contents.
207
            // Anything else means reload the pop-up contents.