1 // This file is part of Moodle - http://moodle.org/
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.
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/>.
17 * JavaScript library for the quiz module editing interface.
21 * @copyright 2008 Olli Savolainen
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // Initialise everything on the quiz edit/order and paging page.
28 function quiz_edit_init(Y) {
29 M.core_scroll_manager.scroll_to_saved_pos(Y);
30 Y.on('submit', function(e) {
31 M.core_scroll_manager.save_scroll_pos(Y, 'id_existingcategory');
33 Y.on('submit', function(e) {
34 M.core_scroll_manager.save_scroll_pos(Y, e.target.get('firstChild'));
35 }, '.quizsavegradesform');
37 // Add random question dialogue --------------------------------------------
38 var randomquestiondialog = YAHOO.util.Dom.get('randomquestiondialog');
39 if (randomquestiondialog) {
40 YAHOO.util.Dom.get(document.body).appendChild(randomquestiondialog);
43 quiz_edit.randomquestiondialog = new YAHOO.widget.Dialog('randomquestiondialog', {
47 zIndex: 1000, // zIndex must be way above 99 to be above the active quiz tab
51 constraintoviewport: true,
54 quiz_edit.randomquestiondialog.render();
55 var div = document.getElementById('randomquestiondialog');
57 div.style.display = 'block';
60 // Show the form on button click.
61 YAHOO.util.Event.addListener(quiz_edit_config.dialoglisteners, 'click', function(e) {
62 // Transfer the page number from the button form to the pop-up form.
63 var addrandombutton = YAHOO.util.Event.getTarget(e);
64 var addpagehidden = YAHOO.util.Dom.getElementsByClassName('addonpage_formelement', 'input', addrandombutton.form);
65 document.getElementById('rform_qpage').value = addpagehidden[0].value;
67 // Show the dialogue and stop the default action.
68 quiz_edit.randomquestiondialog.show();
69 YAHOO.util.Event.stopEvent(e);
72 // Make escape close the dialogue.
73 quiz_edit.randomquestiondialog.cfg.setProperty('keylisteners', [new YAHOO.util.KeyListener(
74 document, {keys:[27]}, function(types, args, obj) { quiz_edit.randomquestiondialog.hide();
77 // Make the form cancel button close the dialogue.
78 YAHOO.util.Event.addListener('id_cancel', 'click', function(e) {
79 quiz_edit.randomquestiondialog.hide();
80 YAHOO.util.Event.preventDefault(e);
83 YAHOO.util.Event.addListener('id_existingcategory', 'click', quiz_yui_workaround);
85 YAHOO.util.Event.addListener('id_newcategory', 'click', quiz_yui_workaround);
87 // Repaginate dialogue -----------------------------------------------------
88 quiz_edit.repaginatedialog = new YAHOO.widget.Dialog('repaginatedialog', {
93 context: ['repaginatecommand', 'tr', 'br', ['beforeShow']],
96 constraintoviewport: true,
99 quiz_edit.repaginatedialog.render();
100 quiz_edit.randomquestiondialog.render();
101 var div = document.getElementById('repaginatedialog');
103 div.style.display = 'block';
106 // Show the form on button click.
107 YAHOO.util.Event.addListener('repaginatecommand', 'click', function() {
108 quiz_edit.repaginatedialog.show();
111 // Reposition the dialogue when the window resizes. For some reason this was not working automatically.
112 YAHOO.widget.Overlay.windowResizeEvent.subscribe(function() {
113 quiz_edit.repaginatedialog.cfg.setProperty('context', ['repaginatecommand', 'tr', 'br', ['beforeShow']]);
116 // Make escape close the dialogue.
117 quiz_edit.repaginatedialog.cfg.setProperty('keylisteners', [new YAHOO.util.KeyListener(
118 document, {keys:[27]}, function(types, args, obj) { quiz_edit.repaginatedialog.hide();
121 // Nasty hack, remove once the YUI bug causing MDL-17594 is fixed.
122 // https://sourceforge.net/tracker/index.php?func=detail&aid=2493426&group_id=165715&atid=836476
123 var elementcauseinglayoutproblem = document.getElementById('_yuiResizeMonitor');
124 if (elementcauseinglayoutproblem) {
125 elementcauseinglayoutproblem.style.left = '0px';
129 function quiz_yui_workaround(e) {
130 // YUI does not send the button pressed with the form submission, so copy
131 // the button name to a hidden input.
132 var submitbutton = YAHOO.util.Event.getTarget(e);
133 var input = document.createElement('input');
134 input.type = 'hidden';
135 input.name = submitbutton.name;
137 submitbutton.form.appendChild(input);
140 // Initialise everything on the quiz settings form.
141 function quiz_settings_init() {
142 var repaginatecheckbox = document.getElementById('id_repaginatenow');
143 if (!repaginatecheckbox) {
144 // This checkbox does not appear on the create new quiz form.
147 var qppselect = document.getElementById('id_questionsperpage');
148 var qppinitialvalue = qppselect.value;
149 YAHOO.util.Event.addListener([qppselect, 'id_shufflequestions'] , 'change', function() {
150 setTimeout(function() { // Annoyingly, this handler runs before the formlib disabledif code, hence the timeout.
151 if (!repaginatecheckbox.disabled) {
152 repaginatecheckbox.checked = qppselect.value != qppinitialvalue;