class mUser
{
function __construct()
{
//empty construct
}
function isOnline($login)
{
global $DB;
$table_name='monitoring_result';
$login=trim($login);
$date=date("d.m.Y");
$nowtime=date("H:i:s");
self::isUser($login);
$a=$DB->Query("SELECT * FROM $table_name WHERE DATE='".$date."' and USER='".$login."' ORDER BY ID DESC LIMIT 1")->GetNext();
if($a["ID"])
{
$nowtime=date("H:i:s");
$last_time=$a["END"];
$diff=strtotime($nowtime)-strtotime($last_time);
$interval=intval(COption::GetOptionString("micros.monitoring", "checkFrequency"));
if(!$interval) $interval=30;
//echo "<pre>";print_r($diff);echo"</pre>";
if(intval($diff)<($interval+10))//if in time
{
$DB->Query("UPDATE $table_name SET END='".$nowtime."' WHERE ID='".$a["ID"]."'");
}else
{
$DB->Query("INSERT INTO $table_name (ID, USER, DATE, BEGIN, END, UF_DATE) VALUES ('', '".$login."', '".$date."', '".$nowtime."', '".$nowtime."', '".date("Y-m-d H:i:s", strtotime($date))."')" );
}
}else
{
$DB->Query("INSERT INTO $table_name (ID, USER, DATE, BEGIN, END, UF_DATE) VALUES ('', '".$login."', '".$date."', '".$nowtime."', '".$nowtime."', '".date("Y-m-d H:i:s", strtotime($date))."')" );
}
self::ShowResult("success");
}
function isUser($login)
{
if($login)
{
if(!$inf=$GLOBALS["USER"]->GetList($by="id", $order="desc", array("LOGIN"=>$login, "ACTIVE"=>"Y"), array("FIELDS"=>array("ID")))->Fetch())
{
self::ShowResult("error", "Employee login ".$login." is not found",false, 404);
}
}else
{
self::ShowResult("error", "Employee login ".$login." is not found", false, 404);
}
}
function ShowResult($status, $str="", $result=array(), $code=200)
{
$response=array();
if($status) $response["status"]=$status;
if($str) $response["message"]=$GLOBALS["APPLICATION"]->ConvertCharset($str, LANG_CHARSET, "UTF-8");
if($result) $response["result"]=$GLOBALS["APPLICATION"]->ConvertCharsetArray($result, LANG_CHARSET, "UTF-8");
$response["statusCode"]=$code;
$GLOBALS["APPLICATION"]->RestartBuffer();
echo json_encode($response);
exit();
}
//get User status
function GetActiveUsers()
{
$table_name='monitoring_result';
$interval=intval(COption::GetOptionString("micros.monitoring", "checkFrequency"));
$date=date("d.m.Y");
$time=date("H:i:s");
//
$max=date("H:i:s", (strtotime($time)-$interval-10));
$q="SELECT * FROM $table_name WHERE DATE='".$date."' AND END > '".$max."'";
$query=$GLOBALS["DB"]->Query($q);
$arResult=array();
while($result=$query->GetNext(false, false))
{
$arResult[$result["USER"]]=$result;
}
return $arResult;
}
///get User report by date
function GetReport($login, $date)
{
$filter="";
if(is_array($date))
{
$filter.=" (USER='".trim($login)."') AND (UF_DATE >= '".date("Y-m-d H:i:s", strtotime($date["from"]))."' AND UF_DATE <= '".date("Y-m-d H:i:s", strtotime($date["to"]))."') ";
}
else
{
$filter.=" (USER='".trim($login)."') AND (UF_DATE = '".date("Y-m-d H:i:s", strtotime($date))."') ";
}
global $DB;
$table_name='monitoring_result';
$login=trim($login);
$sql="SELECT * FROM $table_name WHERE ".$filter." ORDER BY ID ASC";
$query=$DB->Query($sql);
$result=array();
while($res=$query->GetNext())
{
//echo "<pre>";print_r($res);echo"</pre>";
$result[$res["DATE"]][]=$res;
}
if($result){
$report=new DoReport($result);
return $report->ShowReport();
}
else
return false;
}
function MigDate(){
global $DB;
$a=$DB->Query("SELECT * FROM monitoring_result");
while($b=$a->GetNext(false, false))
{
//echo "<pre>";print_r($b);echo"</pre>";
//$date=date("Y-m-d H:i:s", strtotime($b["DATE"]));
//echo "<pre>";print_r($date);echo"</pre>";
//$DB->Query("UPDATE monitoring_result SET UF_DATE='".$date."' WHERE ID='".$b['ID']."'");
}
}
}