MDL-43089 quiz: improved interface for building quizzes
[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);
63 this.dialogue.bodyNode.delegate('click', this.link_clicked, 'a[href]', this);
64 this.dialogue.hide();
65
66 this.loadingDiv = this.dialogue.bodyNode.getHTML();
67
68 Y.later(100, this, function() {this.load_content(window.location.search);});
69 },
70
71 initializer : function() {
72 if (!Y.one(CSS.QBANKLOADING)) {
73 return;
74 }
75 this.create_dialogue();
76 Y.one('body').delegate('click', this.display_dialogue, CSS.ADDQUESTIONLINKS, this);
77 },
78
79 display_dialogue : function (e) {
80 e.preventDefault();
81 this.dialogue.set('headerContent', e.currentTarget.getData(PARAMS.HEADER));
82
83 this.addonpage = e.currentTarget.getData(PARAMS.PAGE);
84 var controlsDiv = this.dialogue.bodyNode.one('.modulespecificbuttonscontainer');
85 if (controlsDiv) {
86 var hidden = controlsDiv.one('input[name=addonpage]');
87 if (!hidden) {
88 hidden = controlsDiv.appendChild('<input type="hidden" name="addonpage">');
89 }
90 hidden.set('value', this.addonpage);
91 }
92
93 this.dialogue.show();
94 },
95
96 load_content : function(queryString) {
97 this.dialogue.bodyNode.append(this.loadingDiv);
98
99 // If to support old IE.
100 if (window.history.replaceState) {
101 window.history.replaceState(null, '', M.cfg.wwwroot + '/mod/quiz/edit.php' + queryString);
102 }
103
104 Y.io(M.cfg.wwwroot + '/mod/quiz/questionbank.ajax.php' + queryString, {
105 method: 'GET',
106 on: {
107 success: this.load_done,
108 failure: this.load_failed
109 },
110 context: this
111 });
112
113 },
114
115 load_done: function(transactionid, response) {
116 var result = JSON.parse(response.responseText);
117 if (!result.status || result.status !== 'OK') {
118 // Because IIS is useless, Moodle can't send proper HTTP response
119 // codes, so we have to detect failures manually.
120 this.load_failed(transactionid, response);
121 return;
122 }
123
124
125 this.dialogue.bodyNode.setHTML(result.contents);
126 Y.use('moodle-question-chooser', function() {M.question.init_chooser({});});
127 this.dialogue.bodyNode.one('form').delegate('change', this.options_changed, '.searchoptions', this);
128
129 if (this.dialogue.visible) {
130 Y.later(0, this.dialogue, this.dialogue.centerDialogue);
131 }
132 M.question.qbankmanager.init();
133 },
134
135 load_failed: function() {
136 },
137
138 link_clicked: function(e) {
139 if (e.currentTarget.ancestor(CSS.ADDTOQUIZCONTAINER)) {
140 // These links need to work like normal, after we modify the URL.
141 e.currentTarget.set('href', e.currentTarget.get('href') + '&addonpage=' + this.addonpage);
142 return;
143 }
144 e.preventDefault();
145 this.load_content(e.currentTarget.get('search'));
146 },
147
148 options_changed: function(e) {
149 e.preventDefault();
150 this.load_content('?' + Y.IO.stringify(e.currentTarget.get('form')));
151 }
152});
153
154M.mod_quiz = M.mod_quiz || {};
155M.mod_quiz.quizquestionbank = M.mod_quiz.quizquestionbank || {};
156M.mod_quiz.quizquestionbank.init = function() {
157 return new POPUP();
158};
159
160
161}, '@VERSION@', {"requires": ["base", "event", "node", "io", "io-form", "yui-later", "moodle-question-qbankmanager"]});