1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * JavaScript library for the quiz module.
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 M.mod_quiz = M.mod_quiz || {};
27 M.mod_quiz.init_attempt_form = function(Y) {
28 M.core_question_engine.init_form(Y, '#responseform');
29 Y.on('submit', M.mod_quiz.timer.stop, '#responseform');
30 M.core_formchangechecker.init({formid: 'responseform'});
33 M.mod_quiz.init_review_form = function(Y) {
34 M.core_question_engine.init_form(Y, '.questionflagsaveform');
35 Y.on('submit', function(e) { e.halt(); }, '.questionflagsaveform');
38 M.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);
46 // Code for updating the countdown timer that is used on timed quizzes.
51 // Timestamp at which time runs out, according to the student's computer's clock.
54 // Is this a quiz preview?
57 // This records the id of the timeout that updates the clock periodically,
62 * @param Y the YUI object
63 * @param start, the timer starting time, in seconds.
64 * @param preview, is this a quiz preview?
66 init: function(Y, start, preview) {
67 M.mod_quiz.timer.Y = Y;
68 M.mod_quiz.timer.endtime = new Date().getTime() + start*1000;
69 M.mod_quiz.timer.preview = preview;
70 M.mod_quiz.timer.update();
71 Y.one('#quiz-timer').setStyle('display', 'block');
75 * Stop the timer, if it is running.
78 if (M.mod_quiz.timer.timeoutid) {
79 clearTimeout(M.mod_quiz.timer.timeoutid);
84 * Function to convert a number between 0 and 99 to a two-digit string.
86 two_digit: function(num) {
94 // Function to update the clock with the current time left, and submit the quiz if necessary.
96 var Y = M.mod_quiz.timer.Y;
97 var secondsleft = Math.floor((M.mod_quiz.timer.endtime - new Date().getTime())/1000);
99 // If this is a preview and time expired, display timeleft 0 and don't renew the timer.
100 if (M.mod_quiz.timer.preview && secondsleft < 0) {
101 Y.one('#quiz-time-left').setContent('0:00:00');
105 // If time has expired, Set the hidden form field that says time has expired.
106 if (secondsleft < 0) {
107 M.mod_quiz.timer.stop(null);
108 Y.one('#quiz-time-left').setContent(M.str.quiz.timesup);
109 var input = Y.one('input[name=timeup]');
110 input.set('value', 1);
111 var form = input.ancestor('form');
112 if (form.one('input[name=finishattempt]')) {
113 form.one('input[name=finishattempt]').set('value', 0);
115 M.core_formchangechecker.set_form_submitted();
120 // If time has nearly expired, change the colour.
121 if (secondsleft < 100) {
122 Y.one('#quiz-timer').removeClass('timeleft' + (secondsleft + 2))
123 .removeClass('timeleft' + (secondsleft + 1))
124 .addClass('timeleft' + secondsleft);
127 // Update the time display.
128 var hours = Math.floor(secondsleft/3600);
129 secondsleft -= hours*3600;
130 var minutes = Math.floor(secondsleft/60);
131 secondsleft -= minutes*60;
132 var seconds = secondsleft;
133 Y.one('#quiz-time-left').setContent(hours + ':' +
134 M.mod_quiz.timer.two_digit(minutes) + ':' +
135 M.mod_quiz.timer.two_digit(seconds));
137 // Arrange for this method to be called again soon.
138 M.mod_quiz.timer.timeoutid = setTimeout(M.mod_quiz.timer.update, 100);
142 M.mod_quiz.nav = M.mod_quiz.nav || {};
144 M.mod_quiz.nav.update_flag_state = function(attemptid, questionid, newstate) {
145 var Y = M.mod_quiz.nav.Y;
146 var navlink = Y.one('#quiznavbutton' + questionid);
147 navlink.removeClass('flagged');
149 navlink.addClass('flagged');
150 navlink.one('.accesshide .flagstate').setContent(M.str.question.flagged);
152 navlink.one('.accesshide .flagstate').setContent('');
156 M.mod_quiz.nav.init = function(Y) {
157 M.mod_quiz.nav.Y = Y;
159 Y.all('#quiznojswarning').remove();
161 var form = Y.one('#responseform');
163 function find_enabled_submit() {
164 // This is rather inelegant, but the CSS3 selector
165 // return form.one('input[type=submit]:enabled');
166 // does not work in IE7, 8 or 9 for me.
167 var enabledsubmit = null;
168 form.all('input[type=submit]').each(function(submit) {
169 if (!enabledsubmit && !submit.get('disabled')) {
170 enabledsubmit = submit;
173 return enabledsubmit;
176 function nav_to_page(pageno) {
177 Y.one('#followingpage').set('value', pageno);
179 // Automatically submit the form. We do it this strange way because just
180 // calling form.submit() does not run the form's submit event handlers.
181 var submit = find_enabled_submit();
182 submit.set('name', '');
183 submit.getDOMNode().click();
186 Y.delegate('click', function(e) {
187 if (this.hasClass('thispage')) {
193 var pageidmatch = this.get('href').match(/page=(\d+)/);
196 pageno = pageidmatch[1];
201 var questionidmatch = this.get('href').match(/#q(\d+)/);
202 if (questionidmatch) {
203 form.set('action', form.get('action') + '#q' + questionidmatch[1]);
207 }, document.body, '.qnbutton');
210 if (Y.one('a.endtestlink')) {
211 Y.on('click', function(e) {
217 if (M.core_question_flags) {
218 M.core_question_flags.add_listener(M.mod_quiz.nav.update_flag_state);
222 M.mod_quiz.secure_window = {
224 if (window.location.href.substring(0, 4) == 'file') {
225 window.location = 'about:blank';
227 Y.delegate('contextmenu', M.mod_quiz.secure_window.prevent, document, '*');
228 Y.delegate('mousedown', M.mod_quiz.secure_window.prevent_mouse, document, '*');
229 Y.delegate('mouseup', M.mod_quiz.secure_window.prevent_mouse, document, '*');
230 Y.delegate('dragstart', M.mod_quiz.secure_window.prevent, document, '*');
231 Y.delegate('selectstart', M.mod_quiz.secure_window.prevent, document, '*');
232 Y.delegate('cut', M.mod_quiz.secure_window.prevent, document, '*');
233 Y.delegate('copy', M.mod_quiz.secure_window.prevent, document, '*');
234 Y.delegate('paste', M.mod_quiz.secure_window.prevent, document, '*');
235 M.mod_quiz.secure_window.clear_status;
236 Y.on('beforeprint', function() {
237 Y.one(document.body).setStyle('display', 'none');
239 Y.on('afterprint', function() {
240 Y.one(document.body).setStyle('display', 'block');
242 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
243 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
244 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
245 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'press:67,86,88+meta');
246 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'up:67,86,88+meta');
247 Y.on('key', M.mod_quiz.secure_window.prevent, '*', 'down:67,86,88+meta');
250 clear_status: function() {
252 setTimeout(M.mod_quiz.secure_window.clear_status, 10);
255 prevent: function(e) {
256 alert(M.str.quiz.functiondisabledbysecuremode);
260 prevent_mouse: function(e) {
261 if (e.button == 1 && /^(INPUT|TEXTAREA|BUTTON|SELECT|LABEL|A)$/i.test(e.target.get('tagName'))) {
262 // Left click on a button or similar. No worries.
269 * Event handler for the quiz start attempt button.
271 start_attempt_action: function(e, args) {
272 if (args.startattemptwarning == '') {
275 M.util.show_confirm_dialog(e, {
276 message: args.startattemptwarning,
277 callback: function() {
280 continuelabel: M.util.get_string('startattempt', 'quiz')
285 init_close_button: function(Y, url) {
286 Y.on('click', function(e) {
287 M.mod_quiz.secure_window.close(url, 0)
288 }, '#secureclosebutton');
291 close: function(Y, url, delay) {
292 setTimeout(function() {
294 window.opener.document.location.reload();
297 window.location.href = url;