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