<?php

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

class Result_model extends CI_Model {

    public function result_all() {
		$this->db->select('c.companyName, b.userID, d.positionID, d.positionName, d.elected, e.candidateName');
		$this->db->select('if(count(a.candidateID) > 0, count(a.candidateID), 0) as countvote');
		// $this->db->from('vote a');
		$this->db->from('candidate e');
		$this->db->where('e.projectID', $this->session->userdata('projectID'));
		$this->db->join('company c', 'e.companyID = c.companyID', 'LEFT');
		$this->db->join('position d', 'e.positionID = d.positionID', 'LEFT');
		$this->db->join('vote a', 'e.candidateID = a.candidateID', 'LEFT');
		$this->db->join('user b', 'a.userID = b.userID', 'LEFT');
		// $this->db->join('candidate e', 'a.candidateID = e.candidateID', 'LEFT');
		$this->db->group_by('e.candidateID');
		$this->db->order_by('d.ordering');
		$this->db->order_by('countvote', 'DESC');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	 public function get_totaluser() {
		$this->db->select('count(userID) as totaluser');
		$this->db->where('projectID', $this->session->userdata('projectID'))
					->from('user');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function get_totalvote() {
		$this->db->select('count(userID) as totalvote');
		$this->db->where('projectID', $this->session->userdata('projectID'))
					->where('status', 1);
		$this->db->from('user');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function get_position() {
		$this->db->where('projectID', $this->session->userdata('projectID'))
					->from('position');
		$this->db->order_by('ordering');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }

}
