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 |
* Contain the logic for the question bank modal.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_quiz/modal_quiz_question_bank
|
|
|
20 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import $ from 'jquery';
|
|
|
25 |
import Modal from './add_question_modal';
|
|
|
26 |
import * as Fragment from 'core/fragment';
|
|
|
27 |
import * as FormChangeChecker from 'core_form/changechecker';
|
|
|
28 |
import * as ModalEvents from 'core/modal_events';
|
|
|
29 |
|
|
|
30 |
const SELECTORS = {
|
|
|
31 |
ADD_TO_QUIZ_CONTAINER: 'td.addtoquizaction',
|
|
|
32 |
ANCHOR: 'a[href]',
|
|
|
33 |
PREVIEW_CONTAINER: 'td.previewquestionaction',
|
|
|
34 |
ADD_QUESTIONS_FORM: 'form#questionsubmit',
|
|
|
35 |
SORTERS: '.sorters',
|
|
|
36 |
};
|
|
|
37 |
|
|
|
38 |
export default class ModalQuizQuestionBank extends Modal {
|
|
|
39 |
static TYPE = 'mod_quiz-quiz-question-bank';
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Create the question bank modal.
|
|
|
43 |
*
|
|
|
44 |
* @param {Number} contextId Current context id.
|
|
|
45 |
*/
|
|
|
46 |
static init(contextId) {
|
|
|
47 |
const selector = '.menu [data-action="questionbank"]';
|
|
|
48 |
document.addEventListener('click', (e) => {
|
|
|
49 |
const trigger = e.target.closest(selector);
|
|
|
50 |
if (!trigger) {
|
|
|
51 |
return;
|
|
|
52 |
}
|
|
|
53 |
e.preventDefault();
|
|
|
54 |
|
|
|
55 |
ModalQuizQuestionBank.create({
|
|
|
56 |
contextId,
|
|
|
57 |
title: trigger.dataset.header,
|
|
|
58 |
addOnPage: trigger.dataset.addonpage,
|
|
|
59 |
templateContext: {
|
|
|
60 |
hidden: true,
|
|
|
61 |
},
|
|
|
62 |
large: true,
|
|
|
63 |
});
|
|
|
64 |
});
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Override the parent show function.
|
|
|
69 |
*
|
|
|
70 |
* Reload the body contents when the modal is shown. The current
|
|
|
71 |
* window URL is used to inform the new content that should be
|
|
|
72 |
* displayed.
|
|
|
73 |
*
|
|
|
74 |
* @method show
|
|
|
75 |
* @return {void}
|
|
|
76 |
*/
|
|
|
77 |
show() {
|
|
|
78 |
this.reloadBodyContent(window.location.search);
|
|
|
79 |
return super.show(this);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Replaces the current body contents with a new version of the question
|
|
|
84 |
* bank.
|
|
|
85 |
*
|
|
|
86 |
* The contents of the question bank are generated using the provided
|
|
|
87 |
* query string.
|
|
|
88 |
*
|
|
|
89 |
* @method reloadBodyContent
|
|
|
90 |
* @param {string} querystring URL encoded string.
|
|
|
91 |
*/
|
|
|
92 |
reloadBodyContent(querystring) {
|
|
|
93 |
// Load the question bank fragment to be displayed in the modal.
|
|
|
94 |
this.setBody(Fragment.loadFragment(
|
|
|
95 |
'mod_quiz',
|
|
|
96 |
'quiz_question_bank',
|
|
|
97 |
this.getContextId(),
|
|
|
98 |
{
|
|
|
99 |
querystring,
|
|
|
100 |
}
|
|
|
101 |
));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Update the URL of the anchor element that the user clicked on to make
|
|
|
106 |
* sure that the question is added to the correct page.
|
|
|
107 |
*
|
|
|
108 |
* @method handleAddToQuizEvent
|
|
|
109 |
* @param {event} e A JavaScript event
|
|
|
110 |
* @param {object} anchorElement The anchor element that was triggered
|
|
|
111 |
*/
|
|
|
112 |
handleAddToQuizEvent(e, anchorElement) {
|
|
|
113 |
// 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
|
|
|
115 |
// href and include the correct add on page id before the page is
|
|
|
116 |
// redirected.
|
|
|
117 |
const href = new URL(anchorElement.attr('href'));
|
|
|
118 |
href.searchParams.set('addonpage', this.getAddOnPageId());
|
|
|
119 |
anchorElement.attr('href', href);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Set up all of the event handling for the modal.
|
|
|
124 |
*
|
|
|
125 |
* @method registerEventListeners
|
|
|
126 |
*/
|
|
|
127 |
registerEventListeners() {
|
|
|
128 |
// Apply parent event listeners.
|
|
|
129 |
super.registerEventListeners(this);
|
|
|
130 |
|
|
|
131 |
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
|
|
|
133 |
// then we need to intercept the submit in order to include the correct "add on page id" before the form is
|
|
|
134 |
// submitted.
|
|
|
135 |
const formElement = $(e.currentTarget);
|
|
|
136 |
|
|
|
137 |
$('<input />').attr('type', 'hidden')
|
|
|
138 |
.attr('name', "addonpage")
|
|
|
139 |
.attr('value', this.getAddOnPageId())
|
|
|
140 |
.appendTo(formElement);
|
|
|
141 |
});
|
|
|
142 |
|
|
|
143 |
this.getModal().on('click', SELECTORS.ANCHOR, (e) => {
|
|
|
144 |
const anchorElement = $(e.currentTarget);
|
|
|
145 |
|
|
|
146 |
// If the anchor element was the add to quiz link.
|
|
|
147 |
if (anchorElement.closest(SELECTORS.ADD_TO_QUIZ_CONTAINER).length) {
|
|
|
148 |
this.handleAddToQuizEvent(e, anchorElement);
|
|
|
149 |
return;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
// If the anchor element was a preview question link.
|
|
|
153 |
if (anchorElement.closest(SELECTORS.PREVIEW_CONTAINER).length) {
|
|
|
154 |
return;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Sorting links have their own handler.
|
|
|
158 |
if (anchorElement.closest(SELECTORS.SORTERS).length) {
|
|
|
159 |
return;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// Anything else means reload the pop-up contents.
|
|
|
163 |
e.preventDefault();
|
|
|
164 |
this.reloadBodyContent(anchorElement.prop('search'));
|
|
|
165 |
});
|
|
|
166 |
|
|
|
167 |
// Disable the form change checker when the body is rendered.
|
|
|
168 |
this.getRoot().on(ModalEvents.bodyRendered, () => {
|
|
|
169 |
// Make sure the form change checker is disabled otherwise it'll stop the user from navigating away from the
|
|
|
170 |
// page once the modal is hidden.
|
|
|
171 |
FormChangeChecker.disableAllChecks();
|
|
|
172 |
});
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
ModalQuizQuestionBank.registerModalType();
|