forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
83 lines (73 loc) · 2.82 KB
/
timer.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
/*/////////////////////////////////////////////////////////
// This code is based off of
// "Live Clock Lite" script - Version 1.0
// By Mark Plachetta ([email protected])
//
// The original script displayed a clock.
// Mark Nielsen modified it to be a countdown timer
// for the lesson module in moodle.
//
// Below is the code that is used to call this page.
// echo "<script type=\"text/javascript\">\n";
// echo "var starttime = ". $timer->starttime . ";\n";
// echo "var servertime = ". time() . ";\n";
// echo "var testlength = ". $lesson->timelimit .";\n";
// echo "document.write('<script type=\"text/javascript\" src=\"liveclock_lite.js\"><\/script>');\n";
// echo "window.onload = function () { show_clock(); }";
// echo "</script>\n";
//
//////////////////////////////////////////////////////////*/
var stopclock = 0;
var myclock = '';
var timeleft, hours, minutes, secs;
var javatimeDate = new Date();
var javatime = javatimeDate.getTime();
javatime = Math.floor(javatime / 1000);
if (typeof(clocksettings) != 'undefined') {
if (clocksettings.starttime) {
starttime = parseInt(clocksettings.starttime);
}
if (clocksettings.servertime) {
servertime = parseInt(clocksettings.servertime);
}
if (clocksettings.testlength) {
testlength = parseInt(clocksettings.testlength);
}
}
difference = javatime - servertime;
starttime = starttime + difference;
/*function leave() { // feable attempt to run a script when someone leaves a timed test early, failed so far
window.onunload = window.open('http://www.google.com','','toolbar=no,menubar=no,location=no,height=500,width=500');
}
leave();*/
function show_clock() {
currentDate = new Date();
current = currentDate.getTime();
current = Math.floor(current / 1000);
var mytime = '',
myclock = document.getElementById("lesson-timer");
if (current > starttime + testlength) {
mytime += M.util.get_string('timeisup', 'lesson');
stopclock = 1;
} else {
timeleft = starttime + testlength - current;
if (timeleft < 60) {
myclock.className = "timeleft1";
}
hours = Math.floor(timeleft / 3600);
timeleft = timeleft - (hours * 3600);
minutes = Math.floor(timeleft / 60);
secs = timeleft - (minutes * 60);
if (secs < 10) {
secs = "0" + secs;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
mytime += hours + ":" + minutes + ":" + secs;
}
myclock.innerHTML = mytime;
if (stopclock == 0) {
setTimeout("show_clock()", 1000);
}
}