weekly release 2.3.3+
[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');
022a3555 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.
40 var closebutton = Y.Node.create('<input type="button" />');
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,
53
54 // This records the id of the timeout that updates the clock periodically,
55 // so we can cancel.
56 timeoutid: null,
57
58 /**
59 * @param Y the YUI object
60 * @param timeleft, the time remaining, in seconds.
61 */
62 init: function(Y, timeleft) {
63 M.mod_quiz.timer.Y = Y;
64 M.mod_quiz.timer.endtime = new Date().getTime() + timeleft*1000;
65 M.mod_quiz.timer.update();
66 Y.one('#quiz-timer').setStyle('display', 'block');
67 },
68
69 /**
70 * Stop the timer, if it is running.
71 */
72 stop: function(e) {
73 if (M.mod_quiz.timer.timeoutid) {
74 clearTimeout(M.mod_quiz.timer.timeoutid);
75 }
76 },
77
78 /**
79 * Function to convert a number between 0 and 99 to a two-digit string.
80 */
81 two_digit: function(num) {
82 if (num < 10) {
83 return '0' + num;
84 } else {
85 return num;
86 }
87 },
88
89 // Function to update the clock with the current time left, and submit the quiz if necessary.
90 update: function() {
91 var Y = M.mod_quiz.timer.Y;
92 var secondsleft = Math.floor((M.mod_quiz.timer.endtime - new Date().getTime())/1000);
93
94 // If time has expired, Set the hidden form field that says time has expired.
95 if (secondsleft < 0) {
96 M.mod_quiz.timer.stop(null);
97 Y.one('#quiz-time-left').setContent(M.str.quiz.timesup);
98 var input = Y.one('input[name=timeup]');
99 input.set('value', 1);
2b2b6458
TH
100 var form = input.ancestor('form');
101 if (form.one('input[name=finishattempt]')) {
102 form.one('input[name=finishattempt]').set('value', 0);
103 }
917cea60 104 M.core_formchangechecker.set_form_submitted();
2b2b6458 105 form.submit();
ff065f96
TH
106 return;
107 }
108
109 // If time has nearly expired, change the colour.
110 if (secondsleft < 100) {
111 Y.one('#quiz-timer').removeClass('timeleft' + (secondsleft + 2))
112 .removeClass('timeleft' + (secondsleft + 1))
113 .addClass('timeleft' + secondsleft);
114 }
115
116 // Update the time display.
117 var hours = Math.floor(secondsleft/3600);
118 secondsleft -= hours*3600;
119 var minutes = Math.floor(secondsleft/60);
120 secondsleft -= minutes*60;
121 var seconds = secondsleft;
babfb615 122 Y.one('#quiz-time-left').setContent(hours + ':' +
ff065f96
TH
123 M.mod_quiz.timer.two_digit(minutes) + ':' +
124 M.mod_quiz.timer.two_digit(seconds));
125
126 // Arrange for this method to be called again soon.
127 M.mod_quiz.timer.timeoutid = setTimeout(M.mod_quiz.timer.update, 100);
128 }
129};
130
131M.mod_quiz.nav = M.mod_quiz.nav || {};
132
133M.mod_quiz.nav.update_flag_state = function(attemptid, questionid, newstate) {
134 var Y = M.mod_quiz.nav.Y;
135 var navlink = Y.one('#quiznavbutton' + questionid);
136 navlink.removeClass('flagged');
137 if (newstate == 1) {
138 navlink.addClass('flagged');
a26246ea
TH
139 navlink.one('.accesshide .flagstate').setContent(M.str.question.flagged);
140 } else {
141 navlink.one('.accesshide .flagstate').setContent('');
ff065f96
TH
142 }
143};
144
145M.mod_quiz.nav.init = function(Y) {
146 M.mod_quiz.nav.Y = Y;
147
148 Y.all('#quiznojswarning').remove();
149
690510bd
TH
150 var form = Y.one('#responseform');
151 if (form) {
ff3b7c85
TH
152 function find_enabled_submit() {
153 // This is rather inelegant, but the CSS3 selector
154 // return form.one('input[type=submit]:enabled');
155 // does not work in IE7, 8 or 9 for me.
156 var enabledsubmit = null;
157 form.all('input[type=submit]').each(function(submit) {
158 if (!enabledsubmit && !submit.get('disabled')) {
159 enabledsubmit = submit;
160 }
161 });
162 return enabledsubmit;
163 }
164
165 function nav_to_page(pageno) {
166 Y.one('#followingpage').set('value', pageno);
167
168 // Automatically submit the form. We do it this strange way because just
169 // calling form.submit() does not run the form's submit event handlers.
170 var submit = find_enabled_submit();
171 submit.set('name', '');
172 submit.getDOMNode().click();
173 };
174
690510bd
TH
175 Y.delegate('click', function(e) {
176 if (this.hasClass('thispage')) {
177 return;
178 }
ff065f96 179
690510bd 180 e.preventDefault();
ff065f96 181
690510bd
TH
182 var pageidmatch = this.get('href').match(/page=(\d+)/);
183 var pageno;
184 if (pageidmatch) {
185 pageno = pageidmatch[1];
186 } else {
187 pageno = 0;
188 }
690510bd
TH
189
190 var questionidmatch = this.get('href').match(/#q(\d+)/);
191 if (questionidmatch) {
222fb6e5 192 form.set('action', form.get('action') + '#q' + questionidmatch[1]);
690510bd
TH
193 }
194
ff3b7c85 195 nav_to_page(pageno);
690510bd
TH
196 }, document.body, '.qnbutton');
197 }
ff065f96
TH
198
199 if (Y.one('a.endtestlink')) {
200 Y.on('click', function(e) {
cb1564b1 201 e.preventDefault();
ff3b7c85 202 nav_to_page(-1);
ff065f96
TH
203 }, 'a.endtestlink');
204 }
205
206 if (M.core_question_flags) {
207 M.core_question_flags.add_listener(M.mod_quiz.nav.update_flag_state);
208 }
209};
210
211M.mod_quiz.secure_window = {
212 init: function(Y) {
25a03faa 213 if (window.location.href.substring(0, 4) == 'file') {
ff065f96
TH
214 window.location = 'about:blank';
215 }
71ece27e 216 Y.delegate('contextmenu', M.mod_quiz.secure_window.prevent, document, '*');
602a76d8
TH
217 Y.delegate('mousedown', M.mod_quiz.secure_window.prevent_mouse, document, '*');
218 Y.delegate('mouseup', M.mod_quiz.secure_window.prevent_mouse, document, '*');
219 Y.delegate('dragstart', M.mod_quiz.secure_window.prevent, document, '*');
71ece27e 220 Y.delegate('selectstart', M.mod_quiz.secure_window.prevent, document, '*');
602a76d8
TH
221 Y.delegate('cut', M.mod_quiz.secure_window.prevent, document, '*');
222 Y.delegate('copy', M.mod_quiz.secure_window.prevent, document, '*');
223 Y.delegate('paste', M.mod_quiz.secure_window.prevent, document, '*');
ff065f96
TH
224 M.mod_quiz.secure_window.clear_status;
225 Y.on('beforeprint', function() {
226 Y.one(document.body).setStyle('display', 'none');
227 }, window);
228 Y.on('afterprint', function() {
229 Y.one(document.body).setStyle('display', 'block');
230 }, window);
231 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
232 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
233 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
234 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+meta');
235 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+meta');
236 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+meta');
237 },
238
239 clear_status: function() {
240 window.status = '';
241 setTimeout(M.mod_quiz.secure_window.clear_status, 10);
242 },
243
244 prevent: function(e) {
245 alert(M.str.quiz.functiondisabledbysecuremode);
246 e.halt();
247 },
248
249 prevent_mouse: function(e) {
72d9358a
TH
250 if (e.button == 1 && /^(INPUT|TEXTAREA|BUTTON|SELECT|LABEL|A)$/i.test(e.target.get('tagName'))) {
251 // Left click on a button or similar. No worries.
252 return;
ff065f96
TH
253 }
254 e.halt();
255 },
256
d755b0f5
TH
257 /**
258 * Event handler for the quiz start attempt button.
259 */
260 start_attempt_action: function(e, args) {
261 if (args.startattemptwarning == '') {
262 openpopup(e, args);
263 } else {
264 M.util.show_confirm_dialog(e, {
265 message: args.startattemptwarning,
266 callback: function() {
267 openpopup(e, args);
268 },
269 continuelabel: M.util.get_string('startattempt', 'quiz')
270 });
271 }
272 },
273
b3782c71
TH
274 init_close_button: function(Y, url) {
275 Y.on('click', function(e) {
276 M.mod_quiz.secure_window.close(url, 0)
277 }, '#secureclosebutton');
278 },
279
e9879c02 280 close: function(Y, url, delay) {
ff065f96
TH
281 setTimeout(function() {
282 if (window.opener) {
283 window.opener.document.location.reload();
284 window.close();
285 } else {
286 window.location.href = url;
287 }
288 }, delay*1000);
289 }
290};