Merge branch 'wip-mdl-37062-m24' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / question / qbank.js
blob39caf4673d7cf2bd84df24ab92b6b30fa895e8bb
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.
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/>.
16 /**
17  * JavaScript belonging to question_bank_view.
18  *
19  * This script is included by question_bank_view and other parts of question/editlib.php.
20  *
21  * @package    moodlecore
22  * @subpackage questionbank
23  * @copyright  2009 Tim Hunt
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 question_bank = {
29     strselectall: '',
30     strdeselectall: '',
31     headercheckbox: null,
32     firstcheckbox: null,
34     init_checkbox_column: function(Y, strselectall, strdeselectall, firstcbid) {
35         question_bank.strselectall = strselectall;
36         question_bank.strdeselectall = strdeselectall;
38         // Find the header checkbox, and initialise it.
39         question_bank.headercheckbox = document.getElementById('qbheadercheckbox');
40         question_bank.headercheckbox.disabled = false;
41         question_bank.headercheckbox.title = strselectall;
43         // Find the first real checkbox.
44         question_bank.firstcheckbox = document.getElementById(firstcbid);
46         // Add the event handler.
47         Y.YUI2.util.Event.addListener(question_bank.headercheckbox, 'click', question_bank.header_checkbox_click);
48     },
50     header_checkbox_click: function() {
51         if (question_bank.firstcheckbox.checked) {
52             select_all_in_element_with_id('categoryquestions', '');
53             question_bank.headercheckbox.title = question_bank.strselectall;
54         } else {
55             select_all_in_element_with_id('categoryquestions', 'checked');
56             question_bank.headercheckbox.title = question_bank.strdeselectall;
57         }
58         question_bank.headercheckbox.checked = false;
59     }
62 // JavaScript to make the list of question types pop-up when you click an add
63 // add question button.
64 qtype_chooser = {
65     radiobuttons: [],
66     labels: [],
67     container: null,
68     submitbutton: null,
69     yui3: null,
71     init: function(Y, boxid) {
72         // Store Y reference.
73         qtype_chooser.yui3 = Y;
74         // Find the radio buttons.
75         qtype_chooser.radiobuttons = Y.YUI2.util.Dom.getElementsBy(
76                 function(el) { return el.type == 'radio'; }, 'input' , boxid);
77         qtype_chooser.labels = Y.YUI2.util.Dom.getElementsByClassName('qtypeoption', 'div', boxid);
79         // Find the submit button.
80         qtype_chooser.submitbutton = document.getElementById(boxid + '_submit');
81         qtype_chooser.enable_disable_submit();
83         // Add the event handlers.
84         Y.YUI2.util.Event.addListener(boxid, 'click', qtype_chooser.enable_disable_submit);
85         Y.YUI2.util.Event.addListener(boxid, 'key_down', qtype_chooser.enable_disable_submit);
86         Y.YUI2.util.Event.addListener(boxid, 'key_up', qtype_chooser.enable_disable_submit);
87         Y.YUI2.util.Event.addListener(boxid, 'dblclick', function(e) {
88                 if (!qtype_chooser.submitbutton.disabled) {
89                     M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
90                     qtype_chooser.submitbutton.form.submit();
91                 }
92             });
94         Y.YUI2.util.Event.onDOMReady(qtype_chooser.init_container);
95         Y.on('submit', function(e) {
96             M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
97         }, qtype_chooser.submitbutton.form);
98     },
100     enable_disable_submit: function() {
101         var Y = qtype_chooser.yui3;
102         var ok = false;
103         for (var i = 0; i < qtype_chooser.radiobuttons.length; i++) {
104             if (qtype_chooser.radiobuttons[i].checked) {
105                 ok = true;
106                 Y.YUI2.util.Dom.addClass(qtype_chooser.labels[i], 'selected');
107             } else {
108                 Y.YUI2.util.Dom.removeClass(qtype_chooser.labels[i], 'selected');
109             }
110         }
111         qtype_chooser.submitbutton.disabled = !ok;
112     },
114     init_container: function() {
115         var Y = qtype_chooser.yui3;
116         if (!document.getElementById('qtypechoicecontainer')) {
117             return;
118         }
119         var qtypechoicecontainer = document.getElementById('qtypechoicecontainer');
120         qtypechoicecontainer.style.display = 'block';
121         qtypechoicecontainer.parentNode.removeChild(qtypechoicecontainer);
122         document.body.appendChild(qtypechoicecontainer);
123         qtype_chooser.container = new Y.YUI2.widget.Dialog(qtypechoicecontainer, {
124             constraintoviewport: true,
125             visible: false,
126             modal: true,
127             fixedcenter: true,
128             close: true,
129             draggable: true,
130             dragOnly: true,
131             postmethod: 'form',
132             zIndex: 1000
133         });
134         qtype_chooser.container.render();
136         Y.YUI2.util.Event.addListener('chooseqtypecancel', 'click', qtype_chooser.cancel_popup);
138         var addforms = Y.YUI2.util.Dom.getElementsBy(function(el) {
139                 return /question\/addquestion\.php/.test(el.action); }, 'form', document.body);
140         for (var i = 0; i < addforms.length; i++) {
141             Y.YUI2.util.Event.addListener(addforms[i], 'submit', qtype_chooser.add_button_click);
142         }
143     },
145     add_button_click: function(e) {
146         var Y = qtype_chooser.yui3;
147         var form = document.getElementById('qtypeformdiv');
149         var oldhidden = Y.YUI2.util.Dom.getElementsBy(
150                 function(el) { return el.type == 'hidden'; }, 'input', form);
151         for (var i = 0; i < oldhidden.length; i++) {
152             oldhidden[i].parentNode.removeChild(oldhidden[i]);
153         }
155         var wantedhidden = Y.YUI2.util.Dom.getElementsBy(
156                 function(el) { return el.type == 'hidden'; }, 'input', this);
157         for (i = 0; i < wantedhidden.length; i++) {
158             form.appendChild(wantedhidden[i].cloneNode(true));
159         }
161         qtype_chooser.container.show();
162         Y.YUI2.util.Event.preventDefault(e);
163     },
165     cancel_popup: function(e) {
166         var Y = qtype_chooser.yui3;
167         qtype_chooser.container.hide();
168         Y.YUI2.util.Event.preventDefault(e);
169     }