<?php

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

class Voting_model extends CI_Model {

    
	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('userID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function delete($tname, $userID) {
        $this->db->where('userID', $userID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function get_position($projectID) {
		$this->db->select('b.*');
		$this->db->from('candidate a');
		$this->db->where('a.projectID', $projectID);
		$this->db->join('position b', 'a.positionID = b.positionID', 'LEFT');
		$this->db->order_by('ordering');
		$this->db->group_by('positionID');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function get_vote($userID, $projectID) {
		$this->db->select('d.noAhli, d.name, e.companyName, b.positionID, b.positionName, b.elected, c.candidateName');
		$this->db->from('vote a');
		$this->db->where('a.userID', $userID);
		$this->db->where('a.projectID', $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();
		
		//echo $this->db->last_query();
		//exit();
		
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function checkmemberNo($noAhli, $projectID) {
		$this->db->select('a.*, b.companyName, c.startDT, c.endDT, c.stopped');
		$this->db->from('user a');
		$this->db->where('a.projectID', $projectID);
		$this->db->where('a.noAhli', $noAhli);
		$this->db->join('company b', 'a.companyID = b.companyID', 'LEFT');
		$this->db->join('project c', 'a.projectID = c.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function get_candidate($positionID, $projectID) {
		$this->db->select('a.*, b.positionName, b.elected, c.companyName');
		$this->db->from('candidate a');
		$this->db->where('a.positionID', $positionID);
		$this->db->where('a.projectID', $projectID);
		$this->db->join('position b', 'a.positionID = b.positionID', 'LEFT');
		$this->db->join('company c', 'a.companyID = c.companyID', 'LEFT');
		$this->db->order_by('b.ordering');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	
	public function get_candidateselectIDandName($positionID, $projectID) {
		$this->db->select('a.candidateID, a.candidateName');
		$this->db->from('candidate a');
		$this->db->where('a.positionID', $positionID);
		$this->db->where('a.projectID', $projectID);
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	public function checkvoteby_userID_positionID($userID, $positionID, $projectID) {
		$this->db->from('vote');
		$this->db->where('userID', $userID);
		$this->db->where('positionID', $positionID);
		$this->db->where('projectID', $projectID);
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function deletevoteby_userID_positionID($tname, $userID, $positionID, $projectID) {
        $this->db->where('userID', $userID);
		$this->db->where('positionID', $positionID);
		$this->db->where('projectID', $projectID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	

}
