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

class dashboard_model extends CI_Model {
	private $states;

	public function __construct() {
		parent::__construct();
		$this->lang->load('dashboard', $this->session->userdata('language'));
	}

	public function getTotComp(){
		if ($this->session->userdata('projectID') == null || $this->session->userdata('projectID')==0) return 0; 
		$this->db->select('ifnull(count(companyID),0) as total',false)
				->from('company');
		$this->db->where('companyID <>', 0);
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result()[0]->total;
        } else {
            return 0;
    	}
/*		akmal 20170405 change query method. 
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
*/	}
	
	public function getTotVoter(){
		if ($this->session->userdata('projectID') == null || $this->session->userdata('projectID')==0) return 0; 

		$this->db->select('ifnull(count(userID),0) as total',false)
				->from('user');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result()[0]->total;
        } else {
            return 0;
    	}
	}
	
	public function getTotVote(){
		if ($this->session->userdata('projectID') == null || $this->session->userdata('projectID')==0) return 0; 

		$this->db->select('ifnull(count(userID),0) as totalVote',false);
		$this->db->from('user');
		$this->db->where('status', 1);
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result()[0]->totalVote;
        } else {
            return 0;
    	}
	}
	
	public function getTotNonVote(){
		if ($this->session->userdata('projectID') == null || $this->session->userdata('projectID')==0) return 0; 

		$this->db->select('ifnull(count(userID),0) as total',false);
		$this->db->from('user');
		$this->db->where('status', 0);
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result()[0]->total;
        } else {
            return 0;
    	}
	}
	public function getTotBlock(){
		if ($this->session->userdata('projectID') == null || $this->session->userdata('projectID')==0) return 0; 

		$this->db->select('ifnull(count(userID),0) as totalBlock',false);
		$this->db->from('user');
		$this->db->where('validity', 1);
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result()[0]->totalBlock;
        } else {
            return 0;
    	}
	}
	
	public function getTotPosition(){
		$this->db->select('ifnull(count(positionID),0) as totalPosition',false);
		$this->db->from('position');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result()[0]->totalPosition;;
        } else {
            return 0;
    	}
	}
	
	public function getvote_bydesignation($positionID){
		$this->db->select('c.candidateID, c.candidateName, count(a.candidateID) as countvote');
		$this->db->where('a.positionID', $positionID);
		$this->db->where('b.status', 1);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->from('vote a');
		$this->db->join('user b', 'a.userID=b.userID', 'LEFT');
		$this->db->join('candidate c', 'a.candidateID=c.candidateID', 'LEFT');
		$this->db->group_by('a.candidateID');
		$this->db->order_by('countvote', 'DESC');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}

	public function getdesignation(){
		$this->db->from('position');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$this->db->order_by('ordering');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}
	
	public function getfirstvoter(){
		$this->db->from('user');
		$this->db->where('status', 1);
		$this->db->where('start_time <>', '0000-00-00 00:00:00');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$this->db->order_by('end_time', 'ASC');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}
	
	public function getlastvoter(){
		$this->db->from('user');
		$this->db->where('status', 1);
		$this->db->where('start_time <>', '0000-00-00 00:00:00');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$this->db->order_by('end_time', 'DESC');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}

	
	public function getfastestvoter(){
		$this->db->select('user.noAhli, TIMESTAMPDIFF(SECOND, start_time, end_time) as duration', false);
		$this->db->from('user');
		$this->db->where('status', 1);
		$this->db->where('start_time <>', '0000-00-00 00:00:00');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$this->db->order_by('duration', 'ASC');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}
	
	public function getslowestvoter(){
		$this->db->select('user.noAhli, TIMESTAMPDIFF(SECOND, start_time, end_time) as duration', false);
		$this->db->from('user');
		$this->db->where('status', 1);
		$this->db->where('start_time <>', '0000-00-00 00:00:00');
		$this->db->where('projectID', $this->session->userdata('projectID'));
		$this->db->order_by('duration', 'DESC');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
    	}
	}
	
}
?>
