<?
  include "../../mainfile.php";
  include "include/CTEACHER.php";
  include "include/CTIMETABLE.php";
  date_default_timezone_set("Asia/Kuala_lumpur");
?>
<?
  function getDifSec($tfr,$dfr,$tto,$dto){
    $arrTimeF = explode(":",$tfr);
    $arrTimeT = explode(":",$tto);
    $arrDateF = explode("-",$dfr);
    $arrDateT = explode("-",$dto);
	
    $mytime1 = mktime( $arrTimeF[0] , $arrTimeF[1] , $arrTimeF[2] , $arrDateF[1] , $arrDateF[2] , $arrDateF[0] );
    $mytime2 = mktime( $arrTimeT[0] , $arrTimeT[1] , $arrTimeT[2] , $arrDateT[1] , $arrDateT[2] , $arrDateT[0] );

    return ($mytime2-$mytime1);
  }
  
  function conv2pc($value){
    if( strlen($value)<2 ){
	  $output = "0" . $value;
	}else{
	  $output = $value;
	}
	return $output;
  }
  
  function secConvTime($sec){
    $sec = sqrt(pow($sec,2));
	$tsecmin = ($sec%3600);
	
    $mysec = conv2pc( ($sec%60) );
	$mymin = conv2pc( floor($tsecmin/60) );
	$myhr = conv2pc( floor($sec/3600) );

	return "$myhr:$mymin:$mysec";
  }
?>
<?
  $mynowdate = date("Y-m-d");
  $myid = $_SESSION["teacherSessionId"];
  $hid = $_POST["hid"];
?>
<?
  //--- part for update punchrecord	
  $qrmk = "SELECT * FROM xoops_pc_remarkrecord WHERE id='$hid'";
  $rrmk = mysql_query($qrmk);
  $nrmk = mysql_num_rows($rrmk);
  $teaid = mysql_result($rrmk,0,"staffid");
  $pdate = mysql_result($rrmk,0,"remarkDate");

  $myuser = new CTEACHER($teaid);
  $startT = $myuser->getStartT( date("w") );
  $endT  = $myuser->getEndT( date("w") );
	
  $qpch = "SELECT * FROM xoops_pc_punchrecord WHERE punchDate='$pdate' AND staffid='$teaid'";
  $rpch = mysql_query($qpch);
	
  $tpchindate = mysql_result($rpch,0,"punchDate");
  $tpchin = mysql_result($rpch,0,"punchIn");
  $tpchoutdate = mysql_result($rpch,0,"punchOutDate");
  $tpchout = mysql_result($rpch,0,"punchOut");
	
  if($tpchoutdate=="0000-00-00"){
    $tpchoutdate=$tpchindate;
    $tpchout=$tpchin;
  }

  $tlatesec = getDifSec($startT,$tpchindate,$tpchin,$tpchindate);
  if($tlatesec<=0){
    $tlatesec=0;
  }
  $tearlysec = getDifSec($tpchout,$tpchoutdate,$endT,$tpchoutdate);
  if($tearlysec<=0){
    $tearlysec=0;
  }

  $tcomlate=secConvTime( $tlatesec );
  $tlevearly=secConvTime( $tearlysec );
?>
<?
  $submit = $_POST["submit"];
  if($submit=="APPROVE"){
    $strstatus = "1";
	$tcomlate="00:00:00";
	$tlevearly="00:00:00";
  }else if($submit=="REJECT"){
    $strstatus = "2";
  }
?>
<?
  //-- query update punch record
  $query = "UPDATE xoops_pc_punchrecord SET comLate='$tcomlate', levEarly='$tlevearly'
              WHERE staffid='$teaid' AND punchDate='$pdate'";
  mysql_query($query);

  //-- query update remark record
  $query = "UPDATE xoops_pc_remarkrecord SET status=$strstatus, apvby='$myid', apvdate='$mynowdate' WHERE id='$hid'";
//  echo "<br>".$query;
  mysql_query( $query );
?>
<script language="javascript">
  window.location="main.php?pg=rmk&ac=apv";
</script>