<?php
/*
Officity - Web application platform - Version 6.0 - 2011-07-05

François Dispaux, Boris Verdeyen, Thomas Hermant,
Jérémie Roy, Grégory Meurice, Abdelila Harbi, 
Marc Mignonsin, Jonathan Sanchez, Julien Gonzalez, Pierre Fouchez

Sushee and Officity is © Copyright 2011 Nectil SA.

`/sushee-source/sushee/private/searchConnected.inc.php` is part of Officity.

Sushee 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 3 of the License, or
(at your option) any later version.

Officity and Sushee are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sushee. If not, see <http://www.gnu.org/licenses/>.
*/
function parseTime($now_time,$action_time){
	$hour = substr($action_time,11,2);
	$minute = substr($action_time,14,2);
	$second = substr($action_time,17,2);
	$day = substr($action_time,8,2);
	$year = substr($action_time,0,4);
	$month = substr($action_time,5,2);
	$lastActionTime = mktime($hour,$minute,$second,$month,$day,$year);
	$elapsed = $now_time - $lastActionTime;
	$hour_elapsed = floor($elapsed/3600);
	$elapsed-=$hour_elapsed*3600;
	$minutes_elapsed = floor($elapsed/60);
	$elapsed-=$minutes_elapsed*60;
	if($elapsed<10)
		$elapsed='0'.$elapsed;
	if($hour_elapsed<10)
		$hour_elapsed='0'.$hour_elapsed;
	if($minutes_elapsed<10)
		$minutes_elapsed='0'.$minutes_elapsed;
	return array('hours_elapsed'=>$hour_elapsed,'minutes_elapsed'=>$minutes_elapsed,'seconds_elapsed'=>$elapsed);
}

function searchConnected($name,&$xml,$requestName,$current_path,$firstNode,$firstNodePath){
	$db_conn = db_connect();
	$time = time();
	$before_24h = $time - 3600;
	$sql = 'SELECT * FROM `connected` WHERE `Type`="real" AND (`Timestamp` >"'.date("Y-m-d H:i:s",$before_24h).'" OR `UserID`=\''.$_SESSION[$GLOBALS["nectil_url"]]['SESSIONuserID'].'\')';
	//echo $sql;
	//debug_log($sql);
	$rs = $db_conn->Execute($sql);
	$connected='';
	$ContactModuleInfo = moduleInfo('contact');
	while($complete_row = $rs->FetchRow()){
		
			$contact = getInfo($ContactModuleInfo,$complete_row['UserID']);
			
			$detail = parseTime($time,$complete_row['Timestamp']);
			$hour_elapsed = $detail['hours_elapsed'];
			$minutes_elapsed = $detail['minutes_elapsed'];
			$elapsed = $detail['seconds_elapsed'];
			$lastTop_sql = 'SELECT `Timestamp` FROM `connected` WHERE `UserID`='.$complete_row['UserID'].' AND `Type`="connected" ORDER BY `Timestamp` DESC LIMIT 0,1';
			$lastTop_row = $db_conn->getRow($lastTop_sql);
			if(!$lastTop_row)
				$lastTop_row['Timestamp']=$GLOBALS['sushee_today'];
			//debug_log($lastTop_sql);
			$detail = parseTime($time,$lastTop_row['Timestamp']);
			$hour_elapsed_top = $detail['hours_elapsed'];
			$minutes_elapsed_top = $detail['minutes_elapsed'];
			$elapsed_top = $detail['seconds_elapsed'];
			$connected.='<CONTACT IP="'.$complete_row['IP'].'" elapsedSinceTop="'.$hour_elapsed_top.':'.$minutes_elapsed_top.':'.$elapsed_top.'" LastTop="'.$lastTop_row['Timestamp'].'" elapsed="'.$hour_elapsed.':'.$minutes_elapsed.':'.$elapsed.'" lastAction="'.$complete_row['Timestamp'].'" ID="'.$complete_row['UserID'].'">'.generateInfoXML($ContactModuleInfo,$contact,$ContactModuleInfo->getFieldsBySecurity('R'),array('ID'=>true,'FIRSTNAME'=>true,'LASTNAME'=>true,'DENOMINATION'=>true,'EMAIL1'=>true)).'</CONTACT>';
	}
	$attributes = '';
	if ($name)
		$attributes.=" name='$name'";
	$external_file = $xml->getData($current_path.'/@fromFile');
	if($external_file)
		$attributes.=" fromFile='".$external_file."'";
	return '<RESULTS'.$attributes.'>'.$connected.'</RESULTS>';
}
?>
