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

class Actionlog_model extends CI_Model {
    protected $_table='action_log';

    public function __construct(){
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($action, $description, $model, $table, $record_id){
        $dataInsert = array(
            'actor_id' => $this->session->userdata('id'),
            'actor_name' => $this->session->userdata('username'),
            'action' => $action,
            'description' => $description,
            'model' => $model,
            'table' => $table,
            'record_id' => $record_id,
            'ip_address' => $this->input->ip_address(),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    public function update($id, $time_login){
        $time_logout = date('Y-m-d H:i:s');
        $duration = (int)(strtotime($time_logout) - strtotime($time_login));
        $dataUpdate = array(
            'time_logout' => $time_logout,
            'duration' => $duration
        );
        $this->db->where('id', $id);
        $this->db->update($this->_table, $dataUpdate);
    }

    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->_table);
    }

    public function getById($id){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table.'.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function countAll($dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByActionType=null){
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);

        if($dtFrom != null){$this->db->where('DATE(created_at) >=', $dtFrom);}
        if($dtTo != null){$this->db->where('DATE(created_at) <=', $dtTo);}
        if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
        if($filterByAccount != null){$this->db->like('actor_name', $filterByAccount);}
        if($filterByActionType != null){$this->db->where('UPPER(action)', $filterByActionType);}

        $query = $this->db->get();
        $result = $query->result_array();
        if($result){
            return $result[0]['totalResults'];
        }else{
            return 0;
        }
    }

    public function getPagination($record, $start, $dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByActionType=null){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);

        if($dtFrom != null){$this->db->where('DATE(created_at) >=', $dtFrom);}
        if($dtTo != null){$this->db->where('DATE(created_at) <=', $dtTo);}
        if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
        if($filterByAccount != null){$this->db->like('actor_name', $filterByAccount);}
        if($filterByActionType != null){$this->db->where('UPPER(action)', $filterByActionType);}

        $this->db->order_by($this->_table.'.id', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */