<?php

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

class Candidate_model extends CI_Model {

    public function candidate_all() {
		$this->db->select('a.*, c.positionName, d.companyName');
		$this->db->select('if(sum(b.candidateID) > 0, false, true) as checkStatus', false);
		$this->db->from('candidate a');
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->join('vote b', 'a.candidateID = b.candidateID', 'LEFT');
		$this->db->join('position c', 'a.positionID = c.positionID', 'LEFT');
		$this->db->join('company d', 'a.companyID = d.companyID', 'LEFT');
		$this->db->order_by('c.ordering', 'ASC');
		$this->db->order_by('a.candidateID', 'ASC');
		$this->db->group_by('candidateID');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function candidate_filterbydatatables($valuedatatable5) {
		$this->db->select('a.*, c.positionName, d.companyName');
		$this->db->select('if(sum(b.candidateID) > 0, false, true) as checkStatus', false);
		$this->db->from('candidate a');
		//$this->db->where('a.candidateName like "%'.$valuedatatable.'%" OR c.positionName like "%'.$valuedatatable.'%" OR d.companyName like "%'.$valuedatatable.'%"');
		$this->db->where('a.projectID', $this->session->userdata('projectID'));
		$this->db->where($valuedatatable5);
		$this->db->join('vote b', 'a.candidateID = b.candidateID', 'LEFT');
		$this->db->join('position c', 'a.positionID = c.positionID', 'LEFT');
		$this->db->join('company d', 'a.companyID = d.companyID', 'LEFT');
		$this->db->order_by('c.ordering', 'ASC');
		$this->db->order_by('a.candidateID', 'ASC');
		$this->db->group_by('candidateID');
		$query = $this->db->get();
		//echo $this->db->last_query();
        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  $this->db->insert_id();
		} else {
			return FALSE;
		}
	}
	
	public function update($tname, $ID, $data) {
        $this->db->where('candidateID', $ID);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function delete($tname, $ID) {
        $this->db->where('candidateID', $ID);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	
	public function get_position(){
		$this->db->from('position');
		$this->db->where('projectID', $this->session->userdata('projectID'))
				->order_by('ordering');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	public function get_company(){
		$this->db->where('projectID', $this->session->userdata('projectID'))
				->from('company');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
//***********************************
	

}
