<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_USERS_INQUIRY';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/manufacturing.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/admin/db/kpi_data_inquiry_db.inc");
//include_once($path_to_root . "/admin/db/kpi_list_values_db.inc");
//include_once($path_to_root . "/admin/includes/ui/kpi_data_inquiry_ui.inc");
include_once($path_to_root . "/admin/db/users_db.inc");


$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

page($help_context = "User Activity Inquiry", false, false, "", $js);

//----------------------------------------------------------------------------------------

simple_page_mode(true);

if(isset($_GET['level_1']) && $_GET['level_1'] > 0){
    $_POST['level_1'] = $_GET['level_1'];
}

if(isset($_GET['level_2']) && $_GET['level_2'] > 0){
    $_POST['level_2'] = $_GET['level_2'];
}

if(isset($_GET['month_id']) && $_GET['month_id'] > 0){
    $_POST['month_id'] = $_GET['month_id'];
}

if(isset($_GET['status_id']) && ($_GET['status_id']) != null){
    $_POST['status_id'] = $_GET['status_id'];
}

function build_user_folders_array()
{

//    global $path_to_root;
//    
//    $installed_path = $path_to_root;
//
//    $user_folders_array = array();
//    $dir =  $installed_path."/tmp/page_access_logs/";
//    $a = scandir($dir);
    
    $result = get_users();
    
    while ($user = db_fetch($result)) {
//            $user_line = array("id" => $user['id'], "login_id" => $user['user_id'], "real_name" => $user['real_name']);
            $user_folders_array[$user['user_id']] = $user['real_name']." [".$user['user_id']."]";
//            $user_folders_array[] = $user_line;
        
    }
    
    return $user_folders_array;
}


function build_user_log_file_months_array()
{

    global $path_to_root;
    
    $installed_path = $path_to_root;
    $user_log_files_array = array();
    
//    if($user_login_id === 0){
//        return $user_log_files_array;
//    }

    
    $dir =  $installed_path."/tmp/page_access_logs/";
    
    
    $a = scandir($dir,SCANDIR_SORT_DESCENDING);
    
    $i = 0;
    foreach ($a as $user_log_file) {
        if (!in_array($user_log_file, ['.', '..', 'index.php'])){
            if($i>500){
                break;
            }
            $time = substr($user_log_file, -19, 15);
            $year = substr($time, 0, 4);
            $month = substr($time, 4, 2);
            $month_name = date("F", strtotime("2001-$month-1"));
//            $date = substr($time, 6, 2);
//            $h = substr($time, 9, 2);
//            $m = substr($time, 11, 2);
//            $s = substr($time, 13, 2);
            
            $tn = $year." ".$month_name;
            $user_log_files_array[$user_log_file] = $tn;
//            $user_folders_array[] = $user_line;
            $i++;
        }
    }
    
    return $user_log_files_array;
}


function build_user_log_files_array($month_no, $user_login_id)
{

    global $path_to_root;
    
    
    
    $installed_path = $path_to_root;
    $user_log_files_array = array();
    
    if(!(strlen($month_no) > 3)){
        return $user_log_files_array;
    }
    
    if($user_login_id === 0){
        return $user_log_files_array;
    }

    
    $dir =  $installed_path."/tmp/page_access_logs/".$month_no."/".$user_login_id."/";
    if(!file_exists($dir)){
        return $user_log_files_array;
    }
    
    $a = scandir($dir,SCANDIR_SORT_DESCENDING);
    
    $i = 0;
    foreach ($a as $user_log_file) {
        if (!in_array($user_log_file, ['.', '..', 'index.php'])){
            if($i>500){
                break;
            }
            $time = substr($user_log_file, -19, 15);
            $year = substr($time, 0, 4);
            $month = substr($time, 4, 2);
            $month_name = date("F", strtotime("2001-$month-1"));
            $date = substr($time, 6, 2);
            $h = substr($time, 9, 2);
            $m = substr($time, 11, 2);
            $s = substr($time, 13, 2);
            
            $tn = $year." ".$month_name." ".$date.spaces(2).$h.":".$m.":".$s;
            $user_log_files_array[$user_log_file] = $tn;
//            $user_folders_array[] = $user_line;
            $i++;
        }
    }
    
    return $user_log_files_array;
}


function read_user_activity_log_file_data($month_no, $user_login_id, $file_name)
{
    global $path_to_root;
    
    $installed_path = $path_to_root;

    $user_activity_log_file_data_array = array();
//    $dir =  $installed_path."/tmp/page_access_logs/".$user_login_id."/";

    $file_path_name = $installed_path."/tmp/page_access_logs/".$month_no."/".$user_login_id."/".$file_name;
        
        if(file_exists($file_path_name)){
            $file_handle = fopen($file_path_name, "r");
        }
        else{
            return array();
        }

        if ($file_handle) {            
            while (!feof($file_handle) ) 
            {
                $line = fgetcsv($file_handle, 1024);
                if(!$line){
                    break;
                }
                $time = substr($line[0], 0, 19);
                $page_name = substr($line[0], 20);
                $a = array("time"=>$time, "page_name"=>$page_name);
                $user_activity_log_file_data_array[] = $a;
            }
        }

        return $user_activity_log_file_data_array;
}


function display_user_activity_log_file_data($activity_log_file_data_array)
{
    start_table(TABLESTYLE, "width=50%");
    $th = array("Time","Screen");
    table_header($th);

    foreach ($activity_log_file_data_array as $value)
    {
        start_row();
        label_cell($value["time"]); 
        label_cell($value["page_name"]);   
        end_row();
    }
    
    end_table();
}


$users_array = build_user_folders_array();

$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------


	start_form();
start_table(TABLESTYLE_NOBORDER);

start_row();
        //custom_values_list_row("User", 'user', $users_array);
//    custom_values_list_cells("User", 'user_id', $users_array, null, '', true);
    user_list_cells(_("Select a user: "), 'user', null, _('Select a user'), true, check_value('show_inactive'));
    
    if(isset_numeric_post('user')){
        $user = get_user($_POST['user']);
        $_POST['user_id'] = $user['user_id'];
    }else{
        $_POST['user_id']  = -1;
    }
end_row();

start_row();
    $user_log_file_months_array = build_user_log_file_months_array();
    custom_values_list_cells("Month", 'month_id', $user_log_file_months_array, null, '', true);
end_row();


    
start_row();   

    
    $user_log_files_array = build_user_log_files_array($_POST['month_id'], $_POST['user_id']);
    custom_values_list_cells("File", 'file_name', $user_log_files_array, null, '', true);
        
    submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');

end_row();
end_table();

br();


$activity_log_file_data_array = read_user_activity_log_file_data($_POST['month_id'], $_POST['user_id'], $_POST['file_name']);

display_user_activity_log_file_data($activity_log_file_data_array);

br(1);

end_form();
end_page();


?>