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