Updated the 19 build version
[moodle.git] / mod / lesson / timer.js
blob4f6290d0d538b14e5372d3a22e64da6cd2bf4f5c
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->maxtime * 60 .";\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 myfont_face = "Arial";
22     var myfont_size = "12";
23     var myfont_color = "#000000";
24     var myback_color = "#FFFFFF";
25     var mywidth = 80;
26     var my12_hour = 1;
27     var stopclock = 0;
28     var myclock = '';
29     var timeleft, hours, minutes, secs;
30     var javatimeDate = new Date();
31     var javatime = javatimeDate.getTime();
32     javatime = Math.floor(javatime/1000);
33     
34     difference = javatime - servertime;    
35     starttime = starttime + difference;
37     var dn = ""; 
38     var old = "";
40     if (document.all||document.getElementById) { 
41         document.write('<span id="LiveClockIE" style="width:'+mywidth+'px;"></span>'); 
42     } else if (document.layers) { 
43         document.write('<ilayer id="ClockPosNS"><layer width="'+mywidth+'" id="LiveClockNS"></layer></ilayer>');
44     } else { 
45         old = "true"; show_clock();
46     }
47     
48     /*function leave() {  // feable attempt to run a script when someone leaves a timed test early, failed so far
49         window.onunload = window.open('http://www.google.com','','toolbar=no,menubar=no,location=no,height=500,width=500');
50     }
51     leave();*/
52     
53     function show_clock() {
55         //show clock in NS 4
56         if (document.layers)
57                 document.ClockPosNS.visibility="show"
58         if (old == "die") { return; }
60         currentDate = new Date();
61         current = currentDate.getTime();
62         current = Math.floor(current/1000);
63         
64         if (current > starttime + testlength) {
65             myclock = '';
66             myclock += '<font style="color:'+myfont_color+'; font-family:'+myfont_face+'; font-size:'+myfont_size+'pt;">';
67             myclock += "Time is up";
68             myclock += '</font>';
69             stopclock = 1;
70         } else {
71             timeleft = starttime + testlength - current;
72             hours = Math.floor(timeleft/3600);
73             timeleft = timeleft - (hours * 3600);
74             minutes = Math.floor(timeleft/60);
75             secs = timeleft - (minutes * 60);           
76             
77             if (secs < 10) {
78                 secs = "0"+secs;
79             }
80             if (minutes < 10) {
81                 minutes = "0"+minutes;
82             }
83             myclock = '';
84             myclock += '<font style="color:'+myfont_color+'; font-family:'+myfont_face+'; font-size:'+myfont_size+'pt;">';
85             myclock += hours+":"+minutes+":"+secs;
86             myclock += '</font>';
87         }
88         
89         if (old == "true") {
90             document.write(myclock);
91             old = "die"; return;
92         }
94         if (document.layers) {
95             clockpos = document.ClockPosNS;
96             liveclock = clockpos.document.LiveClockNS;
97             liveclock.document.write(myclock);
98             liveclock.document.close();
99         } else if (document.all) {
100             LiveClockIE.innerHTML = myclock;
101         } else if (document.getElementById) {
102             document.getElementById("LiveClockIE").innerHTML = myclock;
103         }
105         if (stopclock == 0) {
106             setTimeout("show_clock()",1000);
107         }