<?php
echo GetBaseJavaFunc();
DisplayHeader(".:".ProgramTitle().":.");
//die('nnnnn');
require_once($staticpath.'classes/DataElem.class.php');
require_once($staticpath.'classes/HTMLElements.php');
require_once($staticpath.'classes/InputButton.php');
require_once($staticpath.'functions/extra/StudentsFunctional.fnc.php');

$DataElem = new DataElements;
$Button = new InputButton;
$HTMLElem = new HTMLGetElements;

if($_REQUEST['day_start'] && $_REQUEST['month_start'] && $_REQUEST['year_start'])
{
    while(!VerifyDate($start_date = $_REQUEST['year_start'].'-'.$_REQUEST['month_start'].'-'.$_REQUEST['day_start']))
            $_REQUEST['day_start']--;
}
elseif(!$start_date)
	$start_date = get_db_date();

if($_REQUEST['day_end'] && $_REQUEST['month_end'] && $_REQUEST['year_end'])
{
        while(!VerifyDate($end_date = $_REQUEST['year_end'].'-'.$_REQUEST['month_end'].'-'.$_REQUEST['day_end']))
                $_REQUEST['day_end']--;
}
elseif(!$end_date)
        $end_date = get_db_date();

//var_dump($end_date,$start_date);
//var_dump($_REQUEST);
//var_dump($_POST);
echo "<FORM name='adm_journal' action=modloader.php?servicemod=".$_REQUEST['servicemod']."&servicefunction=create_report&selected_menu=".$_REQUEST['selected_menu']."&link_menu=".$_REQUEST['link_menu']." METHOD=POST>";
echo '<table align=center>';
echo '<TR><TD colspan=2 ><CENTER><TABLE borber=1><TR><TD COLSPAN=2 ALIGN=CENTER><B>
    '._('Choose a period of time for Login History Report').'</B></TD></TR>'.
    '<TR><TD ALIGN=RIGHT>'._('Start Date').':</TD><TD>'.PrepareDate($start_date, '_start').'</TD></TR>'.
    '<TR><TD ALIGN=RIGHT>'._('End Date').':</TD><TD>'.PrepareDate($end_date, '_end').'</TD></TR></TABLE><BR></TD></TR>';
echo '<TR><TD ALIGN=CENTER colspan=2 >'.$Button->createIButton(_('View Log'),'view_log','submit').'</TD></TR>';
echo '</table>';
echo '</FORM>';

/*
 * create table LOGIN_HISTORY(
    ID NUMERIC NOT NULL IDENTITY,
    LOG_TEXT varchar(1000),
 *  LDATA NOT NULL DATETIME,
    IP varchar(15)
);
 */

if($_REQUEST['view_log'] && $end_date && $start_date)
{
    $sql = "SELECT IP,LOGIN,LOG_TEXT,REQUEST_METHOD,PROFILE,HTTP_USER_AGENT,QUERY_STRING,SUCSESS,USER_ID,LDATA FROM LOGIN_HISTORY WHERE LDATA >='$start_date 00:00:00' AND LDATA <= '$end_date 23:59:59' ORDER BY LDATA";
    
    $log_history = db_request(db_execute($sql),array('USER_ID'=>'get_user_name','PROFILE'=>'get_log_profile'),array());
    echo PopTable('header',_('Login History'));
    $column_names = array(
        'LDATA'=>_('Time'),
        'IP'=>_('IP Address'),
        'REQUEST_METHOD'=>_('Method'),
        'PROFILE'=>_('Profile'),
        'LOGIN'=>_('Username'),
        'SUCSESS'=>_('Login Success'),
        'USER_ID'=>_('User'),
//        'HTTP_USER_AGENT'=>_('Agent'),
        
        
        
    );
    Gridoutput($log_history, $column_names,_('Login History'),_('Amount of Records'));
    echo PopTable('footer');
}
function get_user_name($value,$name)
{   global $THIS_array,$STUDENTS_array,$USERS_array;

    if(is_numeric($THIS_array['PROFILE']))
    {
        if(!$USERS_array)
            $USERS_array = db_request(db_execute("SELECT USER_ID,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS FULL_NAME FROM USERS"),array(),array('USER_ID'));
        return ($USERS_array[$value][1]['FULL_NAME']<>'')?$USERS_array[$value][1]['FULL_NAME']:$value;
    }
    if($THIS_array['PROFILE']=='applicant' || $THIS_array['PROFILE']=='student')
    {
        if(!$STUDENTS_array)
        $STUDENTS_array = db_request(db_execute("SELECT STUDENT_ID,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS FULL_NAME FROM STUDENTS"),array(),array('STUDENT_ID'));
        return ($STUDENTS_array[$value][1]['FULL_NAME']<>'')?$STUDENTS_array[$value][1]['FULL_NAME']:$value;
    }
}
function get_log_profile($value,$name)
{
    global $PROFILE_array;
    if(!$PROFILE_array)
    {
        $PROFILE_array = db_request(db_execute("SELECT ID,TITLE FROM USER_PROFILES;"),array(),array('ID'));
    }
    if($value == 'student')
        return _('Student');
    elseif($value == 'applicant')
        return _('Applicant');
    elseif($value == 'none')
        return _('N/A');
    else
        return ($PROFILE_array[$value][1]['TITLE']<>'')?$PROFILE_array[$value][1]['TITLE']:_($value);
}
?>
