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