mod quiz + questions MDL-22370 convert JavaScript to YUI3 and modules.
[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');
121 }
122};
123
124M.mod_quiz.nav.init = function(Y) {
125 M.mod_quiz.nav.Y = Y;
126
127 Y.all('#quiznojswarning').remove();
128
129 Y.delegate('click', function(e) {
130 if (this.hasClass('thispage')) {
131 return;
132 }
133
134 e.preventDefault(e);
135
136 var pageidmatch = this.get('href').match(/page=(\d+)/);
137 var pageno;
138 if (pageidmatch) {
139 pageno = pageidmatch[1];
140 } else {
141 pageno = 0;
142 }
143 Y.one('#nextpagehiddeninput').set('value', pageno);
144
145 var form = Y.one('#responseform');
146
147 var questionidmatch = this.get('href').match(/#q(\d+)/);
148 if (questionidmatch) {
149 form.set(action, form.get(action) + '#q' + questionidmatch[1]);
150 }
151
152 form.submit();
153 }, document.body, '.qnbutton');
154
155 if (Y.one('a.endtestlink')) {
156 Y.on('click', function(e) {
157 e.preventDefault();
158 Y.one('#nextpagehiddeninput').set('value', -1);
159 Y.one('#responseform').submit();
160 }, 'a.endtestlink');
161 }
162
163 if (M.core_question_flags) {
164 M.core_question_flags.add_listener(M.mod_quiz.nav.update_flag_state);
165 }
166};
167
168M.mod_quiz.secure_window = {
169 init: function(Y) {
170 if (window.location.href.substring(0,4) == 'file') {
171 window.location = 'about:blank';
172 }
173 Y.delegate('contextmenu', M.mod_quiz.secure_window.prevent, document.body, '*');
174 Y.delegate('mousedown', M.mod_quiz.secure_window.prevent_mouse, document.body, '*');
175 Y.delegate('mouseup', M.mod_quiz.secure_window.prevent_mouse, document.body, '*');
176 Y.delegate('dragstart', M.mod_quiz.secure_window.prevent, document.body, '*');
177 Y.delegate('selectstart', M.mod_quiz.secure_window.prevent, document.body, '*');
178 M.mod_quiz.secure_window.clear_status;
179 Y.on('beforeprint', function() {
180 Y.one(document.body).setStyle('display', 'none');
181 }, window);
182 Y.on('afterprint', function() {
183 Y.one(document.body).setStyle('display', 'block');
184 }, window);
185 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
186 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
187 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
188 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+meta');
189 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+meta');
190 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+meta');
191 },
192
193 clear_status: function() {
194 window.status = '';
195 setTimeout(M.mod_quiz.secure_window.clear_status, 10);
196 },
197
198 prevent: function(e) {
199 alert(M.str.quiz.functiondisabledbysecuremode);
200 e.halt();
201 },
202
203 prevent_mouse: function(e) {
204 if (e.button != 1 || !/^(INPUT|TEXTAREA|BUTTON|SELECT)$/i.test(e.target.get('tagName'))) {
205 alert(M.str.quiz.functiondisabledbysecuremode);
206 }
207 e.halt();
208 },
209
210 close: function(url, delay) {
211 setTimeout(function() {
212 if (window.opener) {
213 window.opener.document.location.reload();
214 window.close();
215 } else {
216 window.location.href = url;
217 }
218 }, delay*1000);
219 }
220};