Moodle release 2.8beta
[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
27
28var CSS = {
29 QBANKLOADING: 'div.questionbankloading',
30 ADDQUESTIONLINKS: 'ul.menu a.questionbank',
31 ADDTOQUIZCONTAINER: 'td.addtoquizaction'
32};
33
34var PARAMS = {
35 PAGE: 'addonpage',
36 HEADER: 'header'
37};
38
39var POPUP = function() {
40 POPUP.superclass.constructor.apply(this, arguments);
41};
42
43Y.extend(POPUP, Y.Base, {
44 loadingDiv: '',
45 dialogue: null,
46 addonpage: 0,
47
48 create_dialogue: function() {
49 // Create a dialogue on the page and hide it.
50 config = {
51 headerContent : '',
52 bodyContent : Y.one(CSS.QBANKLOADING),
53 draggable : true,
54 modal : true,
55 centered: true,
56 width: null,
57 visible: false,
58 postmethod: 'form',
59 footerContent: null,
60 extraClasses: ['mod_quiz_qbank_dialogue']
61 };
62 this.dialogue = new M.core.dialogue(config);
aec2a725
TH
63 this.dialogue.bodyNode.delegate('click', this.link_clicked,
64 '.paging a[href], thead tr a[href]', this);
e1a2d0d9
CC
65 this.dialogue.hide();
66
67 this.loadingDiv = this.dialogue.bodyNode.getHTML();
68
69 Y.later(100, this, function() {this.load_content(window.location.search);});
70 },
71
72 initializer : function() {
73 if (!Y.one(CSS.QBANKLOADING)) {
74 return;
75 }
76 this.create_dialogue();
77 Y.one('body').delegate('click', this.display_dialogue, CSS.ADDQUESTIONLINKS, this);
78 },
79
80 display_dialogue : function (e) {
81 e.preventDefault();
82 this.dialogue.set('headerContent', e.currentTarget.getData(PARAMS.HEADER));
83
84 this.addonpage = e.currentTarget.getData(PARAMS.PAGE);
85 var controlsDiv = this.dialogue.bodyNode.one('.modulespecificbuttonscontainer');
86 if (controlsDiv) {
87 var hidden = controlsDiv.one('input[name=addonpage]');
88 if (!hidden) {
89 hidden = controlsDiv.appendChild('<input type="hidden" name="addonpage">');
90 }
91 hidden.set('value', this.addonpage);
92 }
93
94 this.dialogue.show();
95 },
96
97 load_content : function(queryString) {
98 this.dialogue.bodyNode.append(this.loadingDiv);
99
100 // If to support old IE.
101 if (window.history.replaceState) {
102 window.history.replaceState(null, '', M.cfg.wwwroot + '/mod/quiz/edit.php' + queryString);
103 }
104
105 Y.io(M.cfg.wwwroot + '/mod/quiz/questionbank.ajax.php' + queryString, {
106 method: 'GET',
107 on: {
108 success: this.load_done,
109 failure: this.load_failed
110 },
111 context: this
112 });
113
114 },
115
116 load_done: function(transactionid, response) {
117 var result = JSON.parse(response.responseText);
118 if (!result.status || result.status !== 'OK') {
119 // Because IIS is useless, Moodle can't send proper HTTP response
120 // codes, so we have to detect failures manually.
121 this.load_failed(transactionid, response);
122 return;
123 }
124
125
126 this.dialogue.bodyNode.setHTML(result.contents);
127 Y.use('moodle-question-chooser', function() {M.question.init_chooser({});});
128 this.dialogue.bodyNode.one('form').delegate('change', this.options_changed, '.searchoptions', this);
129
130 if (this.dialogue.visible) {
131 Y.later(0, this.dialogue, this.dialogue.centerDialogue);
132 }
133 M.question.qbankmanager.init();
aec2a725
TH
134
135 if (Y.one('#advancedsearch')) {
136 M.util.init_collapsible_region(Y, "advancedsearch", "question_bank_advanced_search",
137 M.util.get_string('clicktohideshow'));
138 }
139
140 this.dialogue.fire('widget:contentUpdate');
141 // TODO MDL-47602 really, the base class should listen for the even fired
142 // on the previous line, and fix things like makeResponsive.
143 // However, it does not. So the next two lines are a hack to fix up
144 // display issues (e.g. overall scrollbars on the page). Once the base class
8ac5c115
TH
145 // is fixed, this comment and the following four lines should be deleted.
146 if (this.dialogue.get('visible')) {
147 this.dialogue.hide();
148 this.dialogue.show();
149 }
e1a2d0d9
CC
150 },
151
152 load_failed: function() {
153 },
154
155 link_clicked: function(e) {
156 if (e.currentTarget.ancestor(CSS.ADDTOQUIZCONTAINER)) {
157 // These links need to work like normal, after we modify the URL.
158 e.currentTarget.set('href', e.currentTarget.get('href') + '&addonpage=' + this.addonpage);
159 return;
160 }
161 e.preventDefault();
162 this.load_content(e.currentTarget.get('search'));
163 },
164
165 options_changed: function(e) {
166 e.preventDefault();
167 this.load_content('?' + Y.IO.stringify(e.currentTarget.get('form')));
168 }
169});
170
171M.mod_quiz = M.mod_quiz || {};
172M.mod_quiz.quizquestionbank = M.mod_quiz.quizquestionbank || {};
173M.mod_quiz.quizquestionbank.init = function() {
174 return new POPUP();
175};
176
177
816d0822
DP
178}, '@VERSION@', {
179 "requires": [
180 "base",
181 "event",
182 "node",
183 "io",
184 "io-form",
185 "yui-later",
186 "moodle-question-qbankmanager",
187 "moodle-core-notification-dialogue"
188 ]
189});