-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
299 lines (261 loc) · 10.8 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// This file is part of mod_offlinequiz for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* JavaScript library for the offlinequiz module.
*
* @package mod
* @subpackage offlinequiz
* @author Juergen Zimmer <zimmerj7@univie.ac.at>
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @since Moodle 2.8+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
M.mod_offlinequiz = M.mod_offlinequiz || {};
M.mod_offlinequiz.init_attempt_form = function(Y) {
M.core_question_engine.init_form(Y, '#responseform');
Y.on('submit', M.mod_offlinequiz.timer.stop, '#responseform');
M.core_formchangechecker.init({formid: 'responseform'});
};
M.mod_offlinequiz.init_review_form = function(Y) {
M.core_question_engine.init_form(Y, '.questionflagsaveform');
Y.on('submit', function(e) { e.halt(); }, '.questionflagsaveform');
};
M.mod_offlinequiz.init_comment_popup = function(Y) {
// Add a close button to the window.
var closebutton = Y.Node.create('<input type="button" />');
closebutton.set('value', M.util.get_string('cancel', 'moodle'));
Y.one('#id_submitbutton').ancestor().append(closebutton);
Y.on('click', function() { window.close() }, closebutton);
}
// Code for updating the countdown timer that is used on timed offlinequizzes.
M.mod_offlinequiz.timer = {
// YUI object.
Y: null,
// Timestamp at which time runs out, according to the student's computer's clock.
endtime: 0,
// Is this a offlinequiz preview?
preview: 0,
// This records the id of the timeout that updates the clock periodically,
// so we can cancel.
timeoutid: null,
/**
* @param Y the YUI object
* @param start, the timer starting time, in seconds.
* @param preview, is this a offlinequiz preview?
*/
init: function(Y, start, preview) {
M.mod_offlinequiz.timer.Y = Y;
M.mod_offlinequiz.timer.endtime = new Date().getTime() + start * 1000;
M.mod_offlinequiz.timer.preview = preview;
M.mod_offlinequiz.timer.update();
Y.one('#offlinequiz-timer').setStyle('display', 'block');
},
/**
* Stop the timer, if it is running.
*/
stop: function(e) {
if (M.mod_offlinequiz.timer.timeoutid) {
clearTimeout(M.mod_offlinequiz.timer.timeoutid);
}
},
/**
* Function to convert a number between 0 and 99 to a two-digit string.
*/
two_digit: function(num) {
if (num < 10) {
return '0' + num;
} else {
return num;
}
},
// Function to update the clock with the current time left, and submit the offlinequiz if necessary.
update: function() {
var Y = M.mod_offlinequiz.timer.Y;
var secondsleft = Math.floor((M.mod_offlinequiz.timer.endtime - new Date().getTime()) / 1000);
// If this is a preview and time expired, display timeleft 0 and don't renew the timer.
if (M.mod_offlinequiz.timer.preview && secondsleft < 0) {
Y.one('#offlinequiz-time-left').setContent('0:00:00');
return;
}
// If time has expired, Set the hidden form field that says time has expired.
if (secondsleft < 0) {
M.mod_offlinequiz.timer.stop(null);
Y.one('#offlinequiz-time-left').setContent(M.str.offlinequiz.timesup);
var input = Y.one('input[name=timeup]');
input.set('value', 1);
var form = input.ancestor('form');
if (form.one('input[name=finishattempt]')) {
form.one('input[name=finishattempt]').set('value', 0);
}
M.core_formchangechecker.set_form_submitted();
form.submit();
return;
}
// If time has nearly expired, change the colour.
if (secondsleft < 100) {
Y.one('#offlinequiz-timer').removeClass('timeleft' + (secondsleft + 2))
.removeClass('timeleft' + (secondsleft + 1))
.addClass('timeleft' + secondsleft);
}
// Update the time display.
var hours = Math.floor(secondsleft / 3600);
secondsleft -= hours * 3600;
var minutes = Math.floor(secondsleft / 60);
secondsleft -= minutes * 60;
var seconds = secondsleft;
Y.one('#offlinequiz-time-left').setContent(hours + ':' +
M.mod_offlinequiz.timer.two_digit(minutes) + ':' +
M.mod_offlinequiz.timer.two_digit(seconds));
// Arrange for this method to be called again soon.
M.mod_offlinequiz.timer.timeoutid = setTimeout(M.mod_offlinequiz.timer.update, 100);
}
};
M.mod_offlinequiz.nav = M.mod_offlinequiz.nav || {};
M.mod_offlinequiz.nav.update_flag_state = function(attemptid, questionid, newstate) {
var Y = M.mod_offlinequiz.nav.Y;
var navlink = Y.one('#offlinequiznavbutton' + questionid);
navlink.removeClass('flagged');
if (newstate == 1) {
navlink.addClass('flagged');
navlink.one('.accesshide .flagstate').setContent(M.str.question.flagged);
} else {
navlink.one('.accesshide .flagstate').setContent('');
}
};
M.mod_offlinequiz.nav.init = function(Y) {
M.mod_offlinequiz.nav.Y = Y;
Y.all('#offlinequiznojswarning').remove();
var form = Y.one('#responseform');
if (form) {
function find_enabled_submit() {
var enabledsubmit = null;
form.all('input[type=submit]').each(function(submit) {
if (!enabledsubmit && !submit.get('disabled')) {
enabledsubmit = submit;
}
});
return enabledsubmit;
}
function nav_to_page(pageno) {
Y.one('#followingpage').set('value', pageno);
// Automatically submit the form. We do it this strange way because just
// calling form.submit() does not run the form's submit event handlers.
var submit = find_enabled_submit();
submit.set('name', '');
submit.getDOMNode().click();
};
Y.delegate('click', function(e) {
if (this.hasClass('thispage')) {
return;
}
e.preventDefault();
var pageidmatch = this.get('href').match(/page=(\d+)/);
var pageno;
if (pageidmatch) {
pageno = pageidmatch[1];
} else {
pageno = 0;
}
var questionidmatch = this.get('href').match(/#q(\d+)/);
if (questionidmatch) {
form.set('action', form.get('action') + '#q' + questionidmatch[1]);
}
nav_to_page(pageno);
}, document.body, '.qnbutton');
}
if (Y.one('a.endtestlink')) {
Y.on('click', function(e) {
e.preventDefault();
nav_to_page(-1);
}, 'a.endtestlink');
}
if (M.core_question_flags) {
M.core_question_flags.add_listener(M.mod_offlinequiz.nav.update_flag_state);
}
};
M.mod_offlinequiz.secure_window = {
init: function(Y) {
if (window.location.href.substring(0, 4) == 'file') {
window.location = 'about:blank';
}
Y.delegate('contextmenu', M.mod_offlinequiz.secure_window.prevent, document, '*');
Y.delegate('mousedown', M.mod_offlinequiz.secure_window.prevent_mouse, document, '*');
Y.delegate('mouseup', M.mod_offlinequiz.secure_window.prevent_mouse, document, '*');
Y.delegate('dragstart', M.mod_offlinequiz.secure_window.prevent, document, '*');
Y.delegate('selectstart', M.mod_offlinequiz.secure_window.prevent, document, '*');
Y.delegate('cut', M.mod_offlinequiz.secure_window.prevent, document, '*');
Y.delegate('copy', M.mod_offlinequiz.secure_window.prevent, document, '*');
Y.delegate('paste', M.mod_offlinequiz.secure_window.prevent, document, '*');
M.mod_offlinequiz.secure_window.clear_status;
Y.on('beforeprint', function() {
Y.one(document.body).setStyle('display', 'none');
}, window);
Y.on('afterprint', function() {
Y.one(document.body).setStyle('display', 'block');
}, window);
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'press:67,86,88+ctrl');
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'up:67,86,88+ctrl');
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'down:67,86,88+ctrl');
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'press:67,86,88+meta');
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'up:67,86,88+meta');
Y.on('key', M.mod_offlinequiz.secure_window.prevent, '*', 'down:67,86,88+meta');
},
clear_status: function() {
window.status = '';
setTimeout(M.mod_offlinequiz.secure_window.clear_status, 10);
},
prevent: function(e) {
alert(M.str.offlinequiz.functiondisabledbysecuremode);
e.halt();
},
prevent_mouse: function(e) {
if (e.button == 1 && /^(INPUT|TEXTAREA|BUTTON|SELECT|LABEL|A)$/i.test(e.target.get('tagName'))) {
// Left click on a button or similar. No worries.
return;
}
e.halt();
},
/**
* Event handler for the offlinequiz start attempt button.
*/
start_attempt_action: function(e, args) {
if (args.startattemptwarning == '') {
openpopup(e, args);
} else {
M.util.show_confirm_dialog(e, {
message: args.startattemptwarning,
callback: function() {
openpopup(e, args);
},
continuelabel: M.util.get_string('startattempt', 'offlinequiz')
});
}
},
init_close_button: function(Y, url) {
Y.on('click', function(e) {
M.mod_offlinequiz.secure_window.close(url, 0)
}, '#secureclosebutton');
},
close: function(Y, url, delay) {
setTimeout(function() {
if (window.opener) {
window.opener.document.location.reload();
window.close();
} else {
window.location.href = url;
}
}, delay * 1000);
}
};