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