<?php
	require_once("../libs/cDB.php");
	
  $db = new cMySqlDB("localhost", "probatur", "BPpr07eaP", "probatur" );
	$db->Connect(true);
	
	$userId = mysql_real_escape_string($_GET['userId']);
	$testId = mysql_real_escape_string($_GET['testId']);
	$time = mysql_real_escape_string($_GET['time']);

	$data = $db->QueryToRow("SELECT timeStamp FROM tTimeStamps WHERE id_user='$userId' AND id_test='$testId';");
	$timeDif = $time-$data['timeStamp'];
	if ($timeDif > 10)
	{
		// pravdepodobne nastal vypadok... uprav casy v tTest
		$result = $db->Query("UPDATE tTest SET totalTime=totalTime+$timeDif WHERE id='$testId';");
	}
	// zapis novu hodnotu
	$result = $db->Query("UPDATE tTimeStamps SET timeStamp=$time WHERE id_user='$userId' AND id_test='$testId';");

	// zisti aktualny cas
	$query = "SELECT totalTime-".time()." AS totalTime FROM tTest WHERE id='$testId';";
	$data = $db->QueryToRow($query);
	if ($data !== 0)
	{
		echo $data['totalTime'];
	}
	else
	{
		echo "0";
	}
?>
