<?php
define("root", $_SERVER['DOCUMENT_ROOT']);
session_start();
require(root."/___simple_config.php");
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
global $mainHttpHost;
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "users";
$pageName = "user_activities";
$pageTitle = "User Activities";
$thisUrl = "users/activities";
$ajax_page = "ajax_user_activities.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$pageUrl = "/".adminFolder."/users/activities";
$a=cekpost(cleanQuery($_REQUEST["a"]), "");
if ($a=="section_main") {
	#printR($_REQUEST);
	$stgl = trim(cekpost(cleanQuery($_REQUEST["fil_stgl"]), date('d-m-Y', time())));
	$etgl = trim(cekpost(cleanQuery($_REQUEST["fil_etgl"]), date('d-m-Y', time())));
	$stgl = $etgl=="undefined"?date('d-m-Y', time()):$stgl;
	$etgl = $etgl=="undefined"?date('d-m-Y', time()):$etgl;
	$q_stgl = strtotime($stgl);
	$q_etgl = strtotime($etgl)+86400;
	$fil_stgl = strtotime($stgl);
	$fil_etgl = strtotime($etgl);
	$fil_kduser = trim(cekpost(cleanQuery($_REQUEST["fil_kduser"]), ""));
	$fil_limit = trim(cekpost(cleanQuery($_REQUEST["fil_limit"]), "0"));
	$fil_kduser = trim($fil_kduser)=="undefined"?'':$fil_kduser;
	$fil_limit = trim($fil_limit)=="undefined"?20:$fil_limit;
	$swhere = "";
	if ($fil_kduser!="") { $swhere .= " AND a.kduser = '$fil_kduser' "; }
	if ($fil_limit!="0") { $act_ket .= '(LIMIT: '.$fil_limit.') '; }
	$rsu=array(); $qu="SELECT * FROM _users a WHERE 1=1 AND kduser!=0 $swhere";
	callRS($rsu, $qu);
	$dataM = array();
	for ($u=0;$u<count($rsu);$u++) {
		$dataM[$rsu[$u]["kduser"]] = array(
			"username"=>$rsu[$u]["username"],
			"inisial"=>$rsu[$u]["inisial"],
			"isactive"=>$rsu[$u]["isactive"],
			"creby"=>$rsu[$u]["creby"],
			"cretime"=>$rsu[$u]["cretime"],
			"modby"=>$rsu[$u]["modby"],
			"modtime"=>$rsu[$u]["modtime"]
		);
	}
	if ($_SESSION["user"]["kduser"]==0) {
		$q = "SELECT * FROM _user_activities a
			WHERE 1=1 
			$swhere
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(last_click))>=UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) 
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(last_click))< UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) 
			ORDER BY last_click DESC
			LIMIT 0,$fil_limit";
	} else {
		$q = "SELECT * FROM _user_activities a
			WHERE 1=1 AND kduser!=0
			$swhere
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(last_click))>=UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) 
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(last_click))< UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) 
			ORDER BY last_click DESC
			LIMIT 0,$fil_limit";
	}
	callRSL($rs, $q);
	#echo $q;
	if (count($rs)!=0) {
		$ld = $fil_limit;
		$cp = cekpost(cleanQuery($_GET["cp"]), "1");
		$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
		$sr = ($cp-1) * $ld;
		$pagination = '';
		if ($tp!=1) {
			$pagination .= ('<ul class="pagination pagination-sm">');
				$disabled = '';
				$purl = '#';
				$onclick = '';
				if ($cp<=1) {
					$disabled = 'disabled';
					$onclick = ' onclick="return false;" ';
				}
				$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
				$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
				for ($c=1;$c<=$tp;$c++) {
					$active = '';
					$purl = '#';
					$disabled = '';
					$onclick = '';
					if ($c==$cp) {
						$active = 'active';
					} else {
						$purl = '#';
						$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
					}
					$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
				}
				$disabled = '';
				$onclick = '';
				if ($cp>=$tp) {
					$disabled = 'disabled';
					$onclick = ' onclick="return false;" ';
				}
				$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
				$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
			$pagination .= ('</ul>');
		}
		cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
		
		if ($tp!=1) {
			cout($pagination);
		}
		cout('<table class="table table-striped table-bordered">');
			cout('<thead>');
				cout('<tr>');
				
					cout('<th class="text-center">ACT#ID</th>');
					cout('<th class="text-center">Admin</th>');
					cout('<th class="text-center">Initial</th>');
					cout('<th class="text-center">Session ID</th>');
					cout('<th class="text-center">Login Time</th>');
					cout('<th class="text-center">Last Click</th>');
					cout('<th class="text-center">Logout Time</th>');
					cout('<th class="text-center">Activity</th>');
					cout('<th class="text-center"><input type="button" value="Refresh" onclick="filterReload(\''.$pageName.'\', '.$cp.');" /></th>');
				cout('</tr>');
			cout('</thead>');
			cout('<tbody>');
			$cnt = 0;
			for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
				$cnt++;
				$kd = $rs[$i]["kd"];
				$last_click = ''.date('d-m-Y', $rs[$i]["last_click"]).' <strong>'.date('H:i:s', $rs[$i]["last_click"]).'</strong>';
				cout('<tr class="tr-data'.$kd.'">');
					cout('<td class="text-center">'.$kd.'</td>');
					cout('<td class="text-center">'.$dataM[$rs[$i]["kduser"]]["username"].'</td>');
					cout('<td class="text-center">'.$dataM[$rs[$i]["kduser"]]["inisial"].'</td>');
					cout('<td class="text-center">'.$rs[$i]["session_id"].'</td>');
					cout('<td class="text-center">'.(isset($rs[$i]["login_time"])?date('d-M-Y H:i:s', $rs[$i]["login_time"]):'').'</td>');
					cout('<td class="text-center">'.$last_click.'</td>');
					cout('<td class="text-center">'.(isset($rs[$i]["logout_time"])?date('d-M-Y H:i:s', $rs[$i]["logout_time"]):'').'</td>');
					cout('<td class="text-center" colspan="2">'.str_replace(array(", ", ","), ", ", $rs[$i]["ket"]).'</td>');
				cout('</tr>');
			}
			cout('</tbody>');
		cout('</table>');
		if ($tp!=1) {
			cout($pagination);
		}
		cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
		$act_ket = "User Activities: Main List";
		if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
		updateUserActivities($act_ket);
	} else {
		cout('<div class="alert alert-info" role="alert">');
			cout('<strong>No data available.</strong> Please add new data for display.');
		cout('</div>');
	}
}
?>
