MDL-69165 Questions: Incorrect id used in init_submit_button function
[moodle.git] / mod / quiz / module.js
CommitLineData
aa9bdbe3
TH
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.
12//
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/>.
15
ba643847 16/**
ff065f96
TH
17 * JavaScript library for the quiz module.
18 *
ba643847
TH
19 * @package mod
20 * @subpackage quiz
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
ff065f96
TH
23 */
24
25M.mod_quiz = M.mod_quiz || {};
26
27M.mod_quiz.init_attempt_form = function(Y) {
157434a5 28 M.core_question_engine.init_form(Y, '#responseform');
ff065f96 29 Y.on('submit', M.mod_quiz.timer.stop, '#responseform');
aebbfb7b 30 M.core_formchangechecker.init({formid: 'responseform'});
f6d726c2 31};
ff065f96
TH
32
33M.mod_quiz.init_review_form = function(Y) {
157434a5 34 M.core_question_engine.init_form(Y, '.questionflagsaveform');
ff065f96 35 Y.on('submit', function(e) { e.halt(); }, '.questionflagsaveform');
f6d726c2 36};
ff065f96 37
73a7a0c9
TH
38M.mod_quiz.init_comment_popup = function(Y) {
39 // Add a close button to the window.
f3dc7a17 40 var closebutton = Y.Node.create('<input type="button" class="btn btn-secondary" />');
73a7a0c9
TH
41 closebutton.set('value', M.util.get_string('cancel', 'moodle'));
42 Y.one('#id_submitbutton').ancestor().append(closebutton);
43 Y.on('click', function() { window.close() }, closebutton);
44}
45
ff065f96
TH
46// Code for updating the countdown timer that is used on timed quizzes.
47M.mod_quiz.timer = {
48 // YUI object.
49 Y: null,
50
51 // Timestamp at which time runs out, according to the student's computer's clock.
52 endtime: 0,
ae8530e8 53
8e771aed
MP
54 // Is this a quiz preview?
55 preview: 0,
ff065f96
TH
56
57 // This records the id of the timeout that updates the clock periodically,
58 // so we can cancel.
59 timeoutid: null,
60
747f5ba0
RB
61 // Threshold for updating time remaining, in milliseconds.
62 threshold: 3000,
63
ff065f96
TH
64 /**
65 * @param Y the YUI object
8e771aed
MP
66 * @param start, the timer starting time, in seconds.
67 * @param preview, is this a quiz preview?
ff065f96 68 */
8e771aed 69 init: function(Y, start, preview) {
ff065f96 70 M.mod_quiz.timer.Y = Y;
a35ce084 71 M.mod_quiz.timer.endtime = M.pageloadstarttime.getTime() + start*1000;
8e771aed 72 M.mod_quiz.timer.preview = preview;
ff065f96
TH
73 M.mod_quiz.timer.update();
74 Y.one('#quiz-timer').setStyle('display', 'block');
75 },
76
77 /**
78 * Stop the timer, if it is running.
79 */
80 stop: function(e) {
81 if (M.mod_quiz.timer.timeoutid) {
82 clearTimeout(M.mod_quiz.timer.timeoutid);
83 }
84 },
85
86 /**
87 * Function to convert a number between 0 and 99 to a two-digit string.
88 */
89 two_digit: function(num) {
90 if (num < 10) {
91 return '0' + num;
92 } else {
93 return num;
94 }
95 },
96
97 // Function to update the clock with the current time left, and submit the quiz if necessary.
98 update: function() {
99 var Y = M.mod_quiz.timer.Y;
100 var secondsleft = Math.floor((M.mod_quiz.timer.endtime - new Date().getTime())/1000);
83a99173 101
bd166e61 102 // If time has expired, set the hidden form field that says time has expired and submit
ff065f96
TH
103 if (secondsleft < 0) {
104 M.mod_quiz.timer.stop(null);
64e7aa4d 105 Y.one('#quiz-time-left').setContent(M.util.get_string('timesup', 'quiz'));
ff065f96
TH
106 var input = Y.one('input[name=timeup]');
107 input.set('value', 1);
2b2b6458
TH
108 var form = input.ancestor('form');
109 if (form.one('input[name=finishattempt]')) {
110 form.one('input[name=finishattempt]').set('value', 0);
111 }
b5488ffd 112 M.core_formchangechecker.set_form_submitted();
2b2b6458 113 form.submit();
ff065f96
TH
114 return;
115 }
116
117 // If time has nearly expired, change the colour.
118 if (secondsleft < 100) {
119 Y.one('#quiz-timer').removeClass('timeleft' + (secondsleft + 2))
120 .removeClass('timeleft' + (secondsleft + 1))
121 .addClass('timeleft' + secondsleft);
122 }
123
124 // Update the time display.
125 var hours = Math.floor(secondsleft/3600);
126 secondsleft -= hours*3600;
127 var minutes = Math.floor(secondsleft/60);
128 secondsleft -= minutes*60;
129 var seconds = secondsleft;
babfb615 130 Y.one('#quiz-time-left').setContent(hours + ':' +
ff065f96
TH
131 M.mod_quiz.timer.two_digit(minutes) + ':' +
132 M.mod_quiz.timer.two_digit(seconds));
133
134 // Arrange for this method to be called again soon.
135 M.mod_quiz.timer.timeoutid = setTimeout(M.mod_quiz.timer.update, 100);
747f5ba0
RB
136 },
137
138 // Allow the end time of the quiz to be updated.
139 updateEndTime: function(timeleft) {
140 var newtimeleft = new Date().getTime() + timeleft * 1000;
141
142 // Only update if change is greater than the threshold, so the
143 // time doesn't bounce around unnecessarily.
144 if (Math.abs(newtimeleft - M.mod_quiz.timer.endtime) > M.mod_quiz.timer.threshold) {
145 M.mod_quiz.timer.endtime = newtimeleft;
146 M.mod_quiz.timer.update();
147 }
ff065f96
TH
148 }
149};
150
151M.mod_quiz.nav = M.mod_quiz.nav || {};
152
153M.mod_quiz.nav.update_flag_state = function(attemptid, questionid, newstate) {
154 var Y = M.mod_quiz.nav.Y;
155 var navlink = Y.one('#quiznavbutton' + questionid);
156 navlink.removeClass('flagged');
157 if (newstate == 1) {
158 navlink.addClass('flagged');
64e7aa4d 159 navlink.one('.accesshide .flagstate').setContent(M.util.get_string('flagged', 'question'));
a26246ea
TH
160 } else {
161 navlink.one('.accesshide .flagstate').setContent('');
ff065f96
TH
162 }
163};
164
165M.mod_quiz.nav.init = function(Y) {
166 M.mod_quiz.nav.Y = Y;
167
168 Y.all('#quiznojswarning').remove();
169
690510bd
TH
170 var form = Y.one('#responseform');
171 if (form) {
ff3b7c85
TH
172 function nav_to_page(pageno) {
173 Y.one('#followingpage').set('value', pageno);
174
175 // Automatically submit the form. We do it this strange way because just
176 // calling form.submit() does not run the form's submit event handlers.
67c8c9b4 177 var submit = form.one('input[name="next"]');
ff3b7c85
TH
178 submit.set('name', '');
179 submit.getDOMNode().click();
180 };
181
690510bd
TH
182 Y.delegate('click', function(e) {
183 if (this.hasClass('thispage')) {
184 return;
185 }
ff065f96 186
690510bd 187 e.preventDefault();
ff065f96 188
690510bd
TH
189 var pageidmatch = this.get('href').match(/page=(\d+)/);
190 var pageno;
191 if (pageidmatch) {
192 pageno = pageidmatch[1];
193 } else {
194 pageno = 0;
195 }
690510bd 196
579da44a 197 var questionidmatch = this.get('href').match(/#question-(\d+)-(\d+)/);
690510bd 198 if (questionidmatch) {
579da44a 199 form.set('action', form.get('action') + questionidmatch[0]);
690510bd
TH
200 }
201
ff3b7c85 202 nav_to_page(pageno);
690510bd
TH
203 }, document.body, '.qnbutton');
204 }
ff065f96
TH
205
206 if (Y.one('a.endtestlink')) {
207 Y.on('click', function(e) {
cb1564b1 208 e.preventDefault();
ff3b7c85 209 nav_to_page(-1);
ff065f96
TH
210 }, 'a.endtestlink');
211 }
212
213 if (M.core_question_flags) {
214 M.core_question_flags.add_listener(M.mod_quiz.nav.update_flag_state);
215 }
216};
217
218M.mod_quiz.secure_window = {
219 init: function(Y) {
25a03faa 220 if (window.location.href.substring(0, 4) == 'file') {
ff065f96
TH
221 window.location = 'about:blank';
222 }
71ece27e 223 Y.delegate('contextmenu', M.mod_quiz.secure_window.prevent, document, '*');
d6691fbe
RM
224 Y.delegate('mousedown', M.mod_quiz.secure_window.prevent_mouse, 'body', '*');
225 Y.delegate('mouseup', M.mod_quiz.secure_window.prevent_mouse, 'body', '*');
602a76d8 226 Y.delegate('dragstart', M.mod_quiz.secure_window.prevent, document, '*');
d250083e 227 Y.delegate('selectstart', M.mod_quiz.secure_window.prevent_selection, document, '*');
602a76d8
TH
228 Y.delegate('cut', M.mod_quiz.secure_window.prevent, document, '*');
229 Y.delegate('copy', M.mod_quiz.secure_window.prevent, document, '*');
230 Y.delegate('paste', M.mod_quiz.secure_window.prevent, document, '*');
ff065f96
TH
231 Y.on('beforeprint', function() {
232 Y.one(document.body).setStyle('display', 'none');
233 }, window);
234 Y.on('afterprint', function() {
235 Y.one(document.body).setStyle('display', 'block');
236 }, window);
237 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
238 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
239 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
240 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+meta');
241 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+meta');
242 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+meta');
243 },
244
dc37223b
TH
245 is_content_editable: function(n) {
246 if (n.test('[contenteditable=true]')) {
247 return true;
248 }
249 n = n.get('parentNode');
250 if (n === null) {
251 return false;
252 }
d5653c69 253 return M.mod_quiz.secure_window.is_content_editable(n);
dc37223b
TH
254 },
255
d250083e
DW
256 prevent_selection: function(e) {
257 return false;
258 },
259
ff065f96 260 prevent: function(e) {
64e7aa4d 261 alert(M.util.get_string('functiondisabledbysecuremode', 'quiz'));
ff065f96
TH
262 e.halt();
263 },
264
265 prevent_mouse: function(e) {
72d9358a
TH
266 if (e.button == 1 && /^(INPUT|TEXTAREA|BUTTON|SELECT|LABEL|A)$/i.test(e.target.get('tagName'))) {
267 // Left click on a button or similar. No worries.
268 return;
ff065f96 269 }
d5653c69 270 if (e.button == 1 && M.mod_quiz.secure_window.is_content_editable(e.target)) {
339554aa
TH
271 // Left click in Atto or similar.
272 return;
273 }
ff065f96
TH
274 e.halt();
275 },
276
b3782c71
TH
277 init_close_button: function(Y, url) {
278 Y.on('click', function(e) {
279 M.mod_quiz.secure_window.close(url, 0)
280 }, '#secureclosebutton');
281 },
282
ddda79ca 283 close: function(Y, url, delay) {
ff065f96
TH
284 setTimeout(function() {
285 if (window.opener) {
286 window.opener.document.location.reload();
287 window.close();
288 } else {
289 window.location.href = url;
290 }
291 }, delay*1000);
292 }
293};