Merge branch 'install_21_STABLE' of git://github.com/amosbot/moodle into MOODLE_21_STABLE
[moodle.git] / question / qbank.js
blob69cf07fc3cf7a367f2cfdb1bfde092f5c97b9d2f
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(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         YAHOO.util.Event.addListener(question_bank.headercheckbox, 'change', 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,
70     init: function(Y, boxid) {
71         // Find the radio buttons.
72         qtype_chooser.radiobuttons = YAHOO.util.Dom.getElementsBy(
73                 function(el) { return el.type == 'radio'; }, 'input' , boxid);
74         qtype_chooser.labels = YAHOO.util.Dom.getElementsByClassName('qtypeoption', 'div', boxid);
76         // Find the submit button.
77         qtype_chooser.submitbutton = document.getElementById(boxid + '_submit');
78         qtype_chooser.enable_disable_submit();
80         // Add the event handlers.
81         YAHOO.util.Event.addListener(boxid, 'click', qtype_chooser.enable_disable_submit);
82         YAHOO.util.Event.addListener(boxid, 'key_down', qtype_chooser.enable_disable_submit);
83         YAHOO.util.Event.addListener(boxid, 'key_up', qtype_chooser.enable_disable_submit);
84         YAHOO.util.Event.addListener(boxid, 'dblclick', function(e) {
85                 if (!qtype_chooser.submitbutton.disabled) {
86                     M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
87                     qtype_chooser.submitbutton.form.submit();
88                 }
89             });
91         YAHOO.util.Event.onDOMReady(qtype_chooser.init_container);
92         Y.on('submit', function(e) {
93             M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
94         }, qtype_chooser.submitbutton.form);
95     },
97     enable_disable_submit: function() {
98         var ok = false;
99         for (var i = 0; i < qtype_chooser.radiobuttons.length; i++) {
100             if (qtype_chooser.radiobuttons[i].checked) {
101                 ok = true;
102                 YAHOO.util.Dom.addClass(qtype_chooser.labels[i], 'selected');
103             } else {
104                 YAHOO.util.Dom.removeClass(qtype_chooser.labels[i], 'selected');
105             }
106         }
107         qtype_chooser.submitbutton.disabled = !ok;
108     },
110     init_container: function() {
111         if (!document.getElementById('qtypechoicecontainer')) {
112             return;
113         }
114         var qtypechoicecontainer = document.getElementById('qtypechoicecontainer');
115         qtypechoicecontainer.style.display = 'block';
116         qtypechoicecontainer.parentNode.removeChild(qtypechoicecontainer);
117         document.body.appendChild(qtypechoicecontainer);
118         qtype_chooser.container = new YAHOO.widget.Dialog(qtypechoicecontainer, {
119             constraintoviewport: true,
120             visible: false,
121             modal: true,
122             fixedcenter: true,
123             close: true,
124             draggable: true,
125             dragOnly: true,
126             postmethod: 'form',
127             zIndex: 1000
128         });
129         qtype_chooser.container.render();
131         YAHOO.util.Event.addListener('chooseqtypecancel', 'click', qtype_chooser.cancel_popup);
133         var addforms = YAHOO.util.Dom.getElementsBy(function(el) {
134                 return /question\/addquestion\.php/.test(el.action); }, 'form', document.body);
135         for (var i = 0; i < addforms.length; i++) {
136             YAHOO.util.Event.addListener(addforms[i], 'submit', qtype_chooser.add_button_click);
137         }
138     },
140     add_button_click: function(e) {
141         var form = document.getElementById('qtypeformdiv');
143         var oldhidden = YAHOO.util.Dom.getElementsBy(
144                 function(el) { return el.type == 'hidden'; }, 'input', form);
145         for (var i = 0; i < oldhidden.length; i++) {
146             oldhidden[i].parentNode.removeChild(oldhidden[i]);
147         }
149         var wantedhidden = YAHOO.util.Dom.getElementsBy(
150                 function(el) { return el.type == 'hidden'; }, 'input', this);
151         for (i = 0; i < wantedhidden.length; i++) {
152             form.appendChild(wantedhidden[i].cloneNode(true));
153         }
155         qtype_chooser.container.show();
156         YAHOO.util.Event.preventDefault(e);
157     },
159     cancel_popup: function(e) {
160         qtype_chooser.container.hide();
161         YAHOO.util.Event.preventDefault(e);
162     }