<?php

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

class Vote_model extends CI_Model {

    public function vote_all() {
			$sql = "SELECT DISTINCT user.noAhli, user.userID, user.name,
								user.companyID,
								(SELECT companyName FROM company WHERE company.companyID = user.companyID) companyName
							FROM vote, user
							WHERE vote.projectID = ".$this->session->userdata('projectID')."
							AND vote.projectID = user.projectID
							AND vote.userID = user.userID
							AND user.status = 1";
			$q = $this->db->query($sql);
			return ($q && ($r = $q->result())) ? json_decode(json_encode($r),true) : NULL;
			
							
			/*				
		$this->db->select('a.*, c.companyName, b.noAhli, b.userID, b.name');
		$this->db->from('vote a');
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('user b', 'a.userID = b.userID and b.status = 1', 'LEFT');
		$this->db->join('company c', 'b.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
		
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
				*/
    }
	
	public function vote_filterbydatatables($valuedatatable5){
		$this->db->select('a.*, c.companyName, b.noAhli, b.userID, b.name');
		$this->db->from('vote a');
		$this->db->where($valuedatatable5);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('user b', 'a.userID = b.userID', 'LEFT');
		$this->db->join('company c', 'b.companyID = c.companyID', 'LEFT');
		$this->db->group_by('a.userID');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	public function vote_byID($userID) {
		$this->db->select('a.*, b.positionName, b.elected, c.candidateName, d.noAhli, d.name, e.companyName');
		$this->db->from('vote a');
		$this->db->where('a.userID', $userID);
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('position b', 'a.positionID = b.positionID', 'LEFT');
		$this->db->join('candidate c', 'a.candidateID = c.candidateID', 'LEFT');
		$this->db->join('user d', 'a.userID = d.userID', 'LEFT');
		$this->db->join('company e', 'd.companyID = e.companyID', 'LEFT');
		$this->db->order_by('b.ordering');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }


	public function insert($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function update($tname, $ID, $data) {
        $this->db->where_in('userID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function delete($tname, $ID) {
        $this->db->where_in('userID', $ID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	

	
	
//***********************************
	

}
