Merge branch 'MDL-56959-master-fixup' of https://github.com/FMCorz/moodle
[moodle.git] / mod / lesson / timer.js
blob3f3c8f0d9ed16dd5377be044330c6bc7045c7e5b
1 /*/////////////////////////////////////////////////////////
2 // This code is based off of
3 // "Live Clock Lite" script - Version 1.0
4 // By Mark Plachetta (astroboy@zip.com.au)
5 //
6 // The original script displayed a clock.
7 // Mark Nielsen modified it to be a countdown timer
8 // for the lesson module in moodle.
9 //
10 //    Below is the code that is used to call this page.
11 //    echo "<script type=\"text/javascript\">\n";
12 //        echo "var starttime = ". $timer->starttime . ";\n";
13 //        echo "var servertime = ". time() . ";\n";
14 //        echo "var testlength = ". $lesson->timelimit .";\n";
15 //        echo "document.write('<script type=\"text/javascript\" src=\"liveclock_lite.js\"><\/script>');\n";
16 //        echo "window.onload = function () { show_clock(); }";
17 //    echo "</script>\n";
19 //////////////////////////////////////////////////////////*/
21     var stopclock = 0;
22     var myclock = '';
23     var timeleft, hours, minutes, secs;
24     var javatimeDate = new Date();
25     var javatime = javatimeDate.getTime();
26     javatime = Math.floor(javatime / 1000);
28     if (typeof(clocksettings) != 'undefined') {
29         if (clocksettings.starttime) {
30             starttime = parseInt(clocksettings.starttime);
31         }
32         if (clocksettings.servertime) {
33             servertime = parseInt(clocksettings.servertime);
34         }
35         if (clocksettings.testlength) {
36             testlength = parseInt(clocksettings.testlength);
37         }
38     }
40     difference = javatime - servertime;
41     starttime = starttime + difference;
43     /*function leave() {  // feable attempt to run a script when someone leaves a timed test early, failed so far
44         window.onunload = window.open('http://www.google.com','','toolbar=no,menubar=no,location=no,height=500,width=500');
45     }
46     leave();*/
48     function show_clock() {
50         currentDate = new Date();
51         current = currentDate.getTime();
52         current = Math.floor(current / 1000);
54         var mytime = '',
55             myclock = document.getElementById("lesson-timer");
56         if (current > starttime + testlength) {
57             mytime += M.util.get_string('timeisup', 'lesson');
58             stopclock = 1;
59         } else {
60             timeleft = starttime + testlength - current;
61             if (timeleft < 60) {
62                 myclock.className = "timeleft1";
63             }
64             hours = Math.floor(timeleft / 3600);
65             timeleft = timeleft - (hours * 3600);
66             minutes = Math.floor(timeleft / 60);
67             secs = timeleft - (minutes * 60);
69             if (secs < 10) {
70                 secs = "0" + secs;
71             }
72             if (minutes < 10) {
73                 minutes = "0" + minutes;
74             }
75             mytime += hours + ":" + minutes + ":" + secs;
76         }
78         myclock.innerHTML = mytime;
80         if (stopclock == 0) {
81             setTimeout("show_clock()", 1000);
82         }