<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Name of Class as mentioned in $hook['post_controller]
class User_status extends CI_Hooks{
 
    function __construct() {
       // Anything except exit() :P
    }
 
    // Name of function same as mentioned in Hooks Config
    function activeUser() {
        $CI = & get_instance();
        $class = $CI->router->fetch_class();

        //Checking weather user accessing from multiple pc's
        if(element('role', $CI->session->userdata('logged_in'))){
            $query = $CI->db->select('username,position,last_seen,last_seen_ip,online')->where(array('fldid'=>element('role', $CI->session->userdata('logged_in'))))->order_by('last_seen',"DESC")->get('tbl_usermaster');
            $user = $query->result();
            if($user[0]->last_seen_ip != $this->getIP()){
                $CI->session->set_flashdata('loginWarning', 'Berhenti! Someone else is already logged in useing your ID');
                header('Location: '.base_url()."/logout");
            }
        }


        if (isset($CI->db->queries)) {
                $CI->db->set(array(
                                'last_seen'=>date("Y-m-d H-i-s"),
                                'online'=>1,
                                'last_seen_ip'=>$this->getIP(),
                                'access_comp'=> element('compName', $CI->session->userdata('logged_in'))
                            ))
                        ->where(array(
                                        'fldid' => element('role', $CI->session->userdata('logged_in'))
                                    ))
                        ->update('tbl_usermaster');
        }

        
        if(element('role', $CI->session->userdata('logged_in')) == 33){
            //print_r($CI->session->userdata('logged_in'));
            /*$CI->db->set(array('last_seen'=>date("Y-m-d H-i-s", strtotime('-30 minutes', time()))))
                    ->update('tbl_usermaster');
            $query = $CI->db->select('username,position,last_seen,online')
                            ->where(array(
                                        'online'=>1,
                                        'last_seen >' => date("Y-m-d H-i-s", strtotime('-30 minutes', time()))
                                    ))
                            ->order_by('last_seen',"DESC")
                            ->get('tbl_usermaster');
            print_r($query->result());*/
        }
    }
     
    function getIP() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
            $ip = getenv("REMOTE_ADDR");
        } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
            $ip = $_SERVER['REMOTE_ADDR'];
        } else {
            $ip = "unknown";
        }
        return($ip);
    }


}