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