MDL-23740 fixed PARAM_TAG compatibility with utf-8 chars
[moodle.git] / mod / quiz / module.js
CommitLineData
ff065f96
TH
1/*
2 * JavaScript library for the quiz module.
3 *
4 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
5 */
6
7M.mod_quiz = M.mod_quiz || {};
8
9M.mod_quiz.init_attempt_form = function(Y) {
10 Y.one('#responseform').setAttribute('autocomplete', 'off');
11 Y.on('key', function (e) {
12 if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
13 !e.target.test('input[type=img]')) {
14 e.preventDefault();
15 }
16 }, '#responseform', 'press:13');
17 Y.on('submit', M.mod_quiz.timer.stop, '#responseform');
18}
19
20M.mod_quiz.init_review_form = function(Y) {
21 Y.all('.questionflagsavebutton').remove();
22
23 Y.on('key', function (e) {
24 if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
25 !e.target.test('input[type=img]')) {
26 e.preventDefault();
27 }
28 }, '.questionflagsaveform', 'press:13');
29
30 Y.on('submit', function(e) { e.halt(); }, '.questionflagsaveform');
31}
32
33// Code for updating the countdown timer that is used on timed quizzes.
34M.mod_quiz.timer = {
35 // YUI object.
36 Y: null,
37
38 // Timestamp at which time runs out, according to the student's computer's clock.
39 endtime: 0,
40
41 // This records the id of the timeout that updates the clock periodically,
42 // so we can cancel.
43 timeoutid: null,
44
45 /**
46 * @param Y the YUI object
47 * @param timeleft, the time remaining, in seconds.
48 */
49 init: function(Y, timeleft) {
50 M.mod_quiz.timer.Y = Y;
51 M.mod_quiz.timer.endtime = new Date().getTime() + timeleft*1000;
52 M.mod_quiz.timer.update();
53 Y.one('#quiz-timer').setStyle('display', 'block');
54 },
55
56 /**
57 * Stop the timer, if it is running.
58 */
59 stop: function(e) {
60 if (M.mod_quiz.timer.timeoutid) {
61 clearTimeout(M.mod_quiz.timer.timeoutid);
62 }
63 },
64
65 /**
66 * Function to convert a number between 0 and 99 to a two-digit string.
67 */
68 two_digit: function(num) {
69 if (num < 10) {
70 return '0' + num;
71 } else {
72 return num;
73 }
74 },
75
76 // Function to update the clock with the current time left, and submit the quiz if necessary.
77 update: function() {
78 var Y = M.mod_quiz.timer.Y;
79 var secondsleft = Math.floor((M.mod_quiz.timer.endtime - new Date().getTime())/1000);
80
81 // If time has expired, Set the hidden form field that says time has expired.
82 if (secondsleft < 0) {
83 M.mod_quiz.timer.stop(null);
84 Y.one('#quiz-time-left').setContent(M.str.quiz.timesup);
85 var input = Y.one('input[name=timeup]');
86 input.set('value', 1);
87 input.ancestor('form').submit();
88 return;
89 }
90
91 // If time has nearly expired, change the colour.
92 if (secondsleft < 100) {
93 Y.one('#quiz-timer').removeClass('timeleft' + (secondsleft + 2))
94 .removeClass('timeleft' + (secondsleft + 1))
95 .addClass('timeleft' + secondsleft);
96 }
97
98 // Update the time display.
99 var hours = Math.floor(secondsleft/3600);
100 secondsleft -= hours*3600;
101 var minutes = Math.floor(secondsleft/60);
102 secondsleft -= minutes*60;
103 var seconds = secondsleft;
104 Y.one('#quiz-time-left').setContent('' + hours + ':' +
105 M.mod_quiz.timer.two_digit(minutes) + ':' +
106 M.mod_quiz.timer.two_digit(seconds));
107
108 // Arrange for this method to be called again soon.
109 M.mod_quiz.timer.timeoutid = setTimeout(M.mod_quiz.timer.update, 100);
110 }
111};
112
113M.mod_quiz.nav = M.mod_quiz.nav || {};
114
115M.mod_quiz.nav.update_flag_state = function(attemptid, questionid, newstate) {
116 var Y = M.mod_quiz.nav.Y;
117 var navlink = Y.one('#quiznavbutton' + questionid);
118 navlink.removeClass('flagged');
119 if (newstate == 1) {
120 navlink.addClass('flagged');
a26246ea
TH
121 navlink.one('.accesshide .flagstate').setContent(M.str.question.flagged);
122 } else {
123 navlink.one('.accesshide .flagstate').setContent('');
ff065f96
TH
124 }
125};
126
127M.mod_quiz.nav.init = function(Y) {
128 M.mod_quiz.nav.Y = Y;
129
130 Y.all('#quiznojswarning').remove();
131
132 Y.delegate('click', function(e) {
133 if (this.hasClass('thispage')) {
134 return;
135 }
136
137 e.preventDefault(e);
138
139 var pageidmatch = this.get('href').match(/page=(\d+)/);
140 var pageno;
141 if (pageidmatch) {
142 pageno = pageidmatch[1];
143 } else {
144 pageno = 0;
145 }
146 Y.one('#nextpagehiddeninput').set('value', pageno);
147
148 var form = Y.one('#responseform');
149
150 var questionidmatch = this.get('href').match(/#q(\d+)/);
151 if (questionidmatch) {
152 form.set(action, form.get(action) + '#q' + questionidmatch[1]);
153 }
154
155 form.submit();
156 }, document.body, '.qnbutton');
157
158 if (Y.one('a.endtestlink')) {
159 Y.on('click', function(e) {
160 e.preventDefault();
161 Y.one('#nextpagehiddeninput').set('value', -1);
162 Y.one('#responseform').submit();
163 }, 'a.endtestlink');
164 }
165
166 if (M.core_question_flags) {
167 M.core_question_flags.add_listener(M.mod_quiz.nav.update_flag_state);
168 }
169};
170
171M.mod_quiz.secure_window = {
172 init: function(Y) {
173 if (window.location.href.substring(0,4) == 'file') {
174 window.location = 'about:blank';
175 }
176 Y.delegate('contextmenu', M.mod_quiz.secure_window.prevent, document.body, '*');
177 Y.delegate('mousedown', M.mod_quiz.secure_window.prevent_mouse, document.body, '*');
178 Y.delegate('mouseup', M.mod_quiz.secure_window.prevent_mouse, document.body, '*');
179 Y.delegate('dragstart', M.mod_quiz.secure_window.prevent, document.body, '*');
180 Y.delegate('selectstart', M.mod_quiz.secure_window.prevent, document.body, '*');
181 M.mod_quiz.secure_window.clear_status;
182 Y.on('beforeprint', function() {
183 Y.one(document.body).setStyle('display', 'none');
184 }, window);
185 Y.on('afterprint', function() {
186 Y.one(document.body).setStyle('display', 'block');
187 }, window);
188 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
189 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
190 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
191 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+meta');
192 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+meta');
193 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+meta');
194 },
195
196 clear_status: function() {
197 window.status = '';
198 setTimeout(M.mod_quiz.secure_window.clear_status, 10);
199 },
200
201 prevent: function(e) {
202 alert(M.str.quiz.functiondisabledbysecuremode);
203 e.halt();
204 },
205
206 prevent_mouse: function(e) {
207 if (e.button != 1 || !/^(INPUT|TEXTAREA|BUTTON|SELECT)$/i.test(e.target.get('tagName'))) {
208 alert(M.str.quiz.functiondisabledbysecuremode);
209 }
210 e.halt();
211 },
212
213 close: function(url, delay) {
214 setTimeout(function() {
215 if (window.opener) {
216 window.opener.document.location.reload();
217 window.close();
218 } else {
219 window.location.href = url;
220 }
221 }, delay*1000);
222 }
223};