<?php // $Id: online.inc.php,v 1.9 2005/05/01 11:51:47 darkden81 Exp $
/*
============================================================================== 
	Dokeos - elearning and course management software
	
	Copyright (c) 2004-2005 Dokeos S.A.
	Copyright (c) Istvan Mandak
	
	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	See the GNU General Public License for more details.
	
	Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
	Mail: info@dokeos.com
============================================================================== 
*/
/**
============================================================================== 
*	Code library for showing Who is online
*
*	@author Istvan Mandak, principal author
*	@author Denes Nagy, principal author
*	@author Bart Mollet
*	@author Roan Embrechts, cleaning and bugfixing
*	@package dokeos.whoisonline
============================================================================== 
*/

function LoginCheck($uid,$statsDbName)
{
	$online_table = Database::get_statistic_table(STATISTIC_TRACK_E_ONLINE_TABLE);
	if ($uid!="")
	{
		LoginDelete($uid,$statsDbName);
		$login_ip = $_SERVER['REMOTE_ADDR'];
		$reallyNow = time();
		$login_date = date("Y-m-d H:i:s",$reallyNow);	
		$query = "INSERT INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip) VALUES ($uid,$uid,'$login_date','$login_ip')";								
		@api_sql_query($query,__FILE__,__LINE__);
	}
}

function LoginDelete($uid,$statsDbName)
{
	$online_table = Database::get_statistic_table(STATISTIC_TRACK_E_ONLINE_TABLE);
	$query = "DELETE FROM ".$online_table ." WHERE login_user_id = '$uid'";
	@api_sql_query($query,__FILE__,__LINE__);
}

function WhoIsOnline($uid,$statsDbName,$valid)
{				
	$TBL_TRACK_ONLINE = $statsDbName.".track_e_online";
	$query = "SELECT login_user_id,login_date FROM ".$TBL_TRACK_ONLINE ." ";	
	$result = @api_sql_query($query,__FILE__,__LINE__);							
	if (count($result)>0)
	{
		$rtime = time();
		$rdate = date("Y-m-d H:i:s",$rtime);
		$validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
		$rarray = array();
		
		while(list($login_user_id,$login_date)= mysql_fetch_row($result))
		{	
			$barray = array();
			array_push($barray,$login_user_id);
			array_push($barray,$login_date);

			// YYYY-MM-DD HH:MM:SS, db date format
			$hour = substr($login_date,11,2);
			$minute = substr($login_date,14,2);
			$secund = substr($login_date,17,2);
			$month = substr($login_date,5,2);
			$day = substr($login_date,8,2);
			$year = substr($login_date,0,4);
			// db timestamp
			$dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
			
			if ($dbtime>$validtime)
			{
				array_push($rarray,$barray);
			}
			//echo $dbtime.":".$rtime.">".$validtime."<BR>";
			//echo "$login_user_id.":".$login_date.";";
		}					
		return $rarray;
	}
	else 
	{
		return false;
	}
}

function GetFullUserName($uid,$mysqlMainDb)
{
	$user_table = Database::get_main_table(MAIN_USER_TABLE);
	$query = "SELECT `firstname`,`lastname` FROM ".$user_table." WHERE `user_id`='$uid'";
	$result = @api_sql_query($query,__FILE__,__LINE__);
	if (count($result)>0)
	{
		$str = "";
		while(list($firstname,$lastname)= mysql_fetch_array($result))
		{
			$str = $lastname."&nbsp;".$firstname;
			return $str;
		}
		
	}
}
		
function GetURL($path)
{
	$str = "";	
	$url = explode('/',$path);			
	for($i=0;$i < sizeof($url)-2; $i++)
	{ 
		if($i==sizeof($url)-3)
			{$str = $str.$url[$i]; }
		else
		{
			$str = $str.$url[$i]."/"; 
		}
	}
	return $str;
}		

// picture? 
function IsValidUser($uid,$mysqlMainDb)
{
	$user_table = Database::get_main_table(MAIN_USER_TABLE);
	
	$query = "SELECT `picture_uri`  FROM ".$user_table." WHERE `user_id`='$uid'";	
	$result = @api_sql_query($query,__FILE__,__LINE__);
	
	if (count($result)>0)
	{
		while(list($picture_uri)= mysql_fetch_array($result))
		{
			if (count($picture_uri)>0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
}
	
function ClearURL($path)
{
	$url = explode('?id=',$path);
		return $url[0];
}

function chatcall() {

	global $_uid, $_cid;
	
	if (!$_uid)
	{
		return (false);
	}
	$track_user_table = Database::get_main_table(MAIN_USER_TABLE);
	$sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = $_uid )";
	$result=api_sql_query($sql,__FILE__,__LINE__);
	$row=mysql_fetch_array($result);

	$login_date=$row['chatcall_date'];
	$hour = substr($login_date,11,2);
	$minute = substr($login_date,14,2);
	$secund = substr($login_date,17,2);
	$month = substr($login_date,5,2);
	$day = substr($login_date,8,2);
	$year = substr($login_date,0,4);
	$calltime = mktime($hour,$minute,$secund,$month,$day,$year);

	$time = time();
	$time = date("Y-m-d H:i:s", $time);
	$minute_passed=5;  //within this limit, the chat call request is valid
	$limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));

	if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
		$webpath=api_get_code_web_path();
		$message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
							."<p>"
				."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
				. get_lang("Yes")
				."</a>"
				."&nbsp;&nbsp;|&nbsp;&nbsp;"
				."<a href=\"".api_get_path('WEB_PATH')."webchatdeny.php\">"
				. get_lang("No")
				."</a>"
				."</p>";
		
		return($message);
	}
	else 
	{
		return(false);
	}

}

?>
