mirrored from git://git.moodle.org/moodle.git
/
moodle-mod_quiz-quizquestionbank-debug.js
226 lines (191 loc) · 7.51 KB
/
moodle-mod_quiz-quizquestionbank-debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
YUI.add('moodle-mod_quiz-quizquestionbank', function (Y, NAME) {
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Add questions from question bank functionality for a popup in quiz editing page.
*
* @package mod_quiz
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
var CSS = {
QBANKLOADING: 'div.questionbankloading',
ADDQUESTIONLINKS: 'ul.menu a.questionbank',
ADDTOQUIZCONTAINER: 'td.addtoquizaction',
PREVIEWCONTAINER: 'td.previewaction',
SEARCHOPTIONS: '#advancedsearch'
};
var PARAMS = {
PAGE: 'addonpage',
HEADER: 'header'
};
var POPUP = function() {
POPUP.superclass.constructor.apply(this, arguments);
};
Y.extend(POPUP, Y.Base, {
loadingDiv: '',
dialogue: null,
addonpage: 0,
searchRegionInitialised: false,
create_dialogue: function() {
// Create a dialogue on the page and hide it.
config = {
headerContent : '',
bodyContent : Y.one(CSS.QBANKLOADING),
draggable : true,
modal : true,
centered: true,
width: null,
visible: false,
postmethod: 'form',
footerContent: null,
extraClasses: ['mod_quiz_qbank_dialogue']
};
this.dialogue = new M.core.dialogue(config);
this.dialogue.bodyNode.delegate('click', this.link_clicked, 'a[href]', this);
this.dialogue.hide();
this.loadingDiv = this.dialogue.bodyNode.getHTML();
Y.later(100, this, function() {this.load_content(window.location.search);});
},
initializer : function() {
if (!Y.one(CSS.QBANKLOADING)) {
return;
}
this.create_dialogue();
Y.one('body').delegate('click', this.display_dialogue, CSS.ADDQUESTIONLINKS, this);
},
display_dialogue : function (e) {
e.preventDefault();
this.dialogue.set('headerContent', e.currentTarget.getData(PARAMS.HEADER));
this.addonpage = e.currentTarget.getData(PARAMS.PAGE);
var controlsDiv = this.dialogue.bodyNode.one('.modulespecificbuttonscontainer');
if (controlsDiv) {
var hidden = controlsDiv.one('input[name=addonpage]');
if (!hidden) {
hidden = controlsDiv.appendChild('<input type="hidden" name="addonpage">');
}
hidden.set('value', this.addonpage);
}
this.initialiseSearchRegion();
this.dialogue.show();
},
load_content : function(queryString) {
Y.log('Starting load.', 'debug', 'moodle-mod_quiz-quizquestionbank');
this.dialogue.bodyNode.append(this.loadingDiv);
// If to support old IE.
if (window.history.replaceState) {
window.history.replaceState(null, '', M.cfg.wwwroot + '/mod/quiz/edit.php' + queryString);
}
Y.io(M.cfg.wwwroot + '/mod/quiz/questionbank.ajax.php' + queryString, {
method: 'GET',
on: {
success: this.load_done,
failure: this.load_failed
},
context: this
});
Y.log('Load request sent.', 'debug', 'moodle-mod_quiz-quizquestionbank');
},
load_done: function(transactionid, response) {
var result = JSON.parse(response.responseText);
if (!result.status || result.status !== 'OK') {
// Because IIS is useless, Moodle can't send proper HTTP response
// codes, so we have to detect failures manually.
this.load_failed(transactionid, response);
return;
}
Y.log('Load completed.', 'debug', 'moodle-mod_quiz-quizquestionbank');
this.dialogue.bodyNode.setHTML(result.contents);
Y.use('moodle-question-chooser', function() {M.question.init_chooser({});});
this.dialogue.bodyNode.one('form').delegate('change', this.options_changed, '.searchoptions', this);
if (this.dialogue.visible) {
Y.later(0, this.dialogue, this.dialogue.centerDialogue);
}
M.question.qbankmanager.init();
this.searchRegionInitialised = false;
if (this.dialogue.get('visible')) {
this.initialiseSearchRegion();
}
this.dialogue.fire('widget:contentUpdate');
// TODO MDL-47602 really, the base class should listen for the even fired
// on the previous line, and fix things like makeResponsive.
// However, it does not. So the next two lines are a hack to fix up
// display issues (e.g. overall scrollbars on the page). Once the base class
// is fixed, this comment and the following four lines should be deleted.
if (this.dialogue.get('visible')) {
this.dialogue.hide();
this.dialogue.show();
}
},
load_failed: function() {
Y.log('Load failed.', 'debug', 'moodle-mod_quiz-quizquestionbank');
},
link_clicked: function(e) {
// Add question to quiz. mofify the URL, then let it work as normal.
if (e.currentTarget.ancestor(CSS.ADDTOQUIZCONTAINER)) {
e.currentTarget.set('href', e.currentTarget.get('href') + '&addonpage=' + this.addonpage);
return;
}
// Question preview. Needs to open in a pop-up.
if (e.currentTarget.ancestor(CSS.PREVIEWCONTAINER)) {
openpopup(e, {
url: e.currentTarget.get('href'),
name: 'questionpreview',
options: 'height=600,width=800,top=0,left=0,menubar=0,location=0,scrollbars,resizable,toolbar,status,directories=0,fullscreen=0,dependent'
});
return;
}
// Click on expand/collaspse search-options. Has its own handler.
// We should not interfere.
if (e.currentTarget.ancestor(CSS.SEARCHOPTIONS)) {
return;
}
// Anything else means reload the pop-up contents.
e.preventDefault();
this.load_content(e.currentTarget.get('search'));
},
options_changed: function(e) {
e.preventDefault();
this.load_content('?' + Y.IO.stringify(e.currentTarget.get('form')));
},
initialiseSearchRegion: function() {
if (this.searchRegionInitialised === true) {
return;
}
if (!Y.one(CSS.SEARCHOPTIONS)) {
return;
}
M.util.init_collapsible_region(Y, "advancedsearch", "question_bank_advanced_search",
M.util.get_string('clicktohideshow', 'moodle'));
this.searchRegionInitialised = true;
}
});
M.mod_quiz = M.mod_quiz || {};
M.mod_quiz.quizquestionbank = M.mod_quiz.quizquestionbank || {};
M.mod_quiz.quizquestionbank.init = function() {
return new POPUP();
};
}, '@VERSION@', {
"requires": [
"base",
"event",
"node",
"io",
"io-form",
"yui-later",
"moodle-question-qbankmanager",
"moodle-core-notification-dialogue"
]
});