File: timer.js

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (113 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download | duplicates (2)
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
/*/////////////////////////////////////////////////////////
// This code is based off of
// "Live Clock Lite" script - Version 1.0
// By Mark Plachetta (astroboy@zip.com.au)
//
// 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 language=\"javascript\">\n";
//        echo "var starttime = ". $timer->starttime . ";\n";
//        echo "var servertime = ". time() . ";\n";
//        echo "var testlength = ". $lesson->maxtime * 60 .";\n";
//        echo "document.write('<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"liveclock_lite.js\"><\/SCRIPT>');\n";
//        echo "window.onload = function () { show_clock(); }";
//    echo "</script>\n";
//
//////////////////////////////////////////////////////////*/

    var myfont_face = "Arial";
    var myfont_size = "12";
    var myfont_color = "#000000";
    var myback_color = "#FFFFFF";
    var mywidth = 80;
    var my12_hour = 1;
    var stopclock = 0;
    var myclock = '';
    var timeleft, hours, minutes, secs;
    var javatimeDate = new Date();
    var javatime = javatimeDate.getTime();
    javatime = Math.floor(javatime/1000);
    
    difference = javatime - servertime;    
    starttime = starttime + difference;

    var dn = ""; 
    var old = "";

    if (document.all||document.getElementById) { 
        document.write('<span id="LiveClockIE" style="width:'+mywidth+'px;"></span>'); 
    } else if (document.layers) { 
        document.write('<ilayer id="ClockPosNS"><layer width="'+mywidth+'" id="LiveClockNS"></layer></ilayer>');
    } else { 
        old = "true"; show_clock();
    }
    
    /*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() {

        //show clock in NS 4
        if (document.layers)
                document.ClockPosNS.visibility="show"
        if (old == "die") { return; }

        currentDate = new Date();
        current = currentDate.getTime();
        current = Math.floor(current/1000);
        
        if (current > starttime + testlength) {
            myclock = '';
            myclock += '<font style="color:'+myfont_color+'; font-family:'+myfont_face+'; font-size:'+myfont_size+'pt;">';
            myclock += "Time is up";
            myclock += '</font>';
            stopclock = 1;
        } else {
            timeleft = starttime + testlength - current;
            hours = Math.floor(timeleft/3600);
            timeleft = timeleft - (hours * 3600);
            minutes = Math.floor(timeleft/60);
            secs = timeleft - (minutes * 60);           
            
            if (secs < 10) {
                secs = "0"+secs;
            }
            
            myclock = '';
            myclock += '<font style="color:'+myfont_color+'; font-family:'+myfont_face+'; font-size:'+myfont_size+'pt;">';
            if (hours > 0) {
                myclock += hours+":";
                if (minutes < 10) {
                    minutes = "0"+minutes;
                }
            }
            myclock += minutes+":"+secs;
            myclock += '</font>';
        }
        
        if (old == "true") {
            document.write(myclock);
            old = "die"; return;
        }

        if (document.layers) {
            clockpos = document.ClockPosNS;
            liveclock = clockpos.document.LiveClockNS;
            liveclock.document.write(myclock);
            liveclock.document.close();
        } else if (document.all) {
            LiveClockIE.innerHTML = myclock;
        } else if (document.getElementById) {
            document.getElementById("LiveClockIE").innerHTML = myclock;
        }

        if (stopclock == 0) {
            setTimeout("show_clock()",1000);
        }
}