2012-01-11 39 views
1

我正在开发一个在线考试,我需要在整个考试过程中显示一个倒数计时器/时钟。在显示最终结果时,还应显示解决每个单独问题所需的时间以及测试所花费的总时间。定时器为在线考试系统存储每个部分的时间

实现此功能的最佳方法是什么?

<?php 

$countfile = "counter.txt"; 

// location of site statistics. 
$statsfile = "stats.txt"; 

// checks whether the file exist, if not then server will create it. 
if (file_exists($countfile)) { 

// open the counter hit file. 
$fp = fopen($countfile, "r"); 

// reads the counter hit file and gets the size of the file. 
$output = fread($fp, filesize($countfile)); 

// close the counter hit file. 
fclose($fp); 

// get the integer value of the variable. 
$count = intval($output); 
} 

// if file is doesn't exist, the server will create the counter hit file and gives a value of zero. 
else { 
$count = 0; 
} 

// showcount function starts here. 
function ShowCount() { 

// declares the global variables. 
global $ShowCount, $countfile, $statsfile, $count; 

// get the current month. 
$month = date('m'); 

// get the current day. 
$day = date('d'); 

// get the current year. 
$year = date('Y'); 

// get the current hour. 
$hour = date('G'); 

// get the current minute. 
$minute = date('i'); 

// get the current second. 
$second = date('s'); 

// this is the date used in the stats file 
$date = "$month/$day/$year $hour:$minute:$second"; 

// this is the remote IP address of the user. 
$remoteip = getenv("REMOTE_ADDR"); 

// some of the browser details of the user. 
$otherinfo = getenv("HTTP_USER_AGENT"); 

// retrieve the last URL where the user visited before visiting the current file. 
$ref = getenv("HTTP_REFERER"); 

// open the statistics file. 
$fp = fopen($statsfile, "a"); 

// put the given data into the statistics file. 
fputs($fp, "Remote Address: $remoteip | "); 
fputs($fp, "Information: $otherinfo | "); 
fputs($fp, "Date: $date | "); 
fputs($fp, "Referer: $ref\n"); 

// close the statistics file. 
fclose($fp); 

// adds 1 count to the counter hit file. 
$count++; 

// open the counter hit file. 
$fp = fopen($countfile, "w"); 

// write at the counter hit file. 
// if the value is 34, it will be changed to 35. 
fwrite($fp, $count); 

// close the counter hit file. 
fclose($fp); 

// showcount variable is equal to count variable. 
$ShowCount = $count; 

// return the value of the count variable into showcount variable. 
return $ShowCount; 
} 

// display the value in the counter hits file. 
echo showcount(), " visits"; 

?> 

我使用这个脚本来追踪访问者的统计和访问的时间......这是工作fine.however我不能够跟踪单个问题的时间在考试的每个类别(每场考试有多个类别有多个问题)。需要帮助

回答

0

您可以通过执行

  1. 当学生开始考试达到这个目的,存储开始时间在一些商店/数据库。
  2. 要显示倒计时,您可以在客户端使用JavaScript。写一个需要2次的函数,第一个是服务器时间,第二个是StartTime。使用这些,你可以找出候选人解决​​问题的时间是多少,,从这里你可以找到剩余时间。使用setInterval您可以显示滴答的手表。
  3. 对于解决问题所花费的时间,将候选人可见问题的持续时间存储起来。
  4. 增加个别问题所需的时间,并显示总时间。

我想这是你正在寻找的答案。

2

使用一个会话,你需要跟踪,当该部分用户的时间到期

<?php 
// Upon starting the section 
session_start(); 
$_SESSION['TIMER'] = time() + 600; // Give the user Ten minutes 
?> 

不要做,对页面重新加载,但因为他们可以简单地刷新页面,复位定时器

在页面上,使用Javascript来显示一个时钟:

<script type="text/javascript"> 
var TimeLimit = new Date('<?php echo date('r', $_SESSION['TIMER']) ?>'); 
</script> 

然后,您可以使用变量“时限”显示倒计时

<script type="text/javascript"> 
function countdownto() { 
    var date = Math.round((TimeLimit-new Date())/1000); 
    var hours = Math.floor(date/3600); 
    date = date - (hours*3600); 
    var mins = Math.floor(date/60); 
    date = date - (mins*60); 
    var secs = date; 
    if (hours<10) hours = '0'+hours; 
    if (mins<10) mins = '0'+mins; 
    if (secs<10) secs = '0'+secs; 
    document.body.innerHTML = hours+':'+mins+':'+secs; 
    setTimeout("countdownto()",1000); 
    } 

countdownto(); 
</script>