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