<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ReportModel extends CI_Model 
{
	var $table = 'participants';
    var $column_order = array(NULL, 'first_name', 'questions', NULL, 'correct' , NULL, 'started', NULL);
    var $column_search = array('first_name', 'started','questions');
    var $order = array('participants.id' => 'DESC');

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    private function _get_datatables_query() {
        $this->db->from($this->table);
        $this->db->join('users', 'users.id = participants.user_id', 'left');
        $this->db->join("quizes", "quizes.id = participants.quiz_id", "left");
        $i = 0;
        foreach ($this->column_search as $item) {
            // if datatable send POST for search
            if ($_POST['search']['value']) {
                // first loop
                if ($i === 0) {
                    // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                    $this->db->group_start();
                    $this->db->like($item, $_POST['search']['value']);
                } else {
                    $this->db->or_like($item, $_POST['search']['value']);
                }
                // last loop
                if (count($this->column_search) - 1 == $i) {
                    // close bracket
                    $this->db->group_end();
                }
            }
            $i++;
        }
        // here order processing
        if (isset($_POST['order'])) {
            $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else if (isset($this->order)) {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order) ]);
        }
    }

    function count_filtered($quiz_id) {
        $this->_get_datatables_query();
        $query = $this->db->where('quiz_id',$quiz_id)->get();
        return $query->num_rows();
    }
    
    function count_all($quiz_id) {
        $this->db->from($this->table)->where('quiz_id',$quiz_id);
        return $this->db->count_all_results();
    }

    function get_quiz_history($quiz_id) {
        $this->_get_datatables_query();
        if ($_POST['length'] != - 1) 
            $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->select('participants.*,quizes.id as quiz_id, duration_min,first_name,last_name')
        ->where('participants.quiz_id',$quiz_id)
        ->order_by('participants.id', 'desc')
        ->get();
        return $query->result();
    }

    function get_participant_by_id($participant_id) 
	{
	    return $this->db->where('id',$participant_id)->get('participants')->row_array();
	}
	function get_user_question_by_participant_id($participant_id)
	{
	    return $this->db->where('participant_id',$participant_id)->order_by('question_id','asc')->get('user_questions')->result_array();
	}

	function get_quiz_by_id($quiz_id) 
    {
        return $this->db->where('deleted','0')->where('id',$quiz_id)->order_by('id','asc')->get('quizes')->row();
    }

    protected $DT_column2 = array(
        'p.id',
        'users.first_name',
        'users.mobile',
        'f.name',
        's.name',
        'd.name',
        'q.title',
        'p.questions',
        'p.correct',
        'p.total_attemp',
        'p.correct',
        ''
    );

    public function reportsAjax($searchVal = '', $sortColIndex = 0, $sortBy = 'DESC', $limit = 0, $offset = 0, $s_id =0, $d_id=0, $f_id=0, $q_id) {
        $this->db->select("users.*,p.id as participant_id, p.questions,p.correct, p.total_attemp, s.name as state, d.name as district, f.name as facility_name, q.title as quize_name");
        $this->db->from('participants as p');
        $this->db->join('users', 'users.id = p.user_id', 'left');
        $this->db->join('states as s', 's.id = users.state', 'left');
        $this->db->join('districts as d', 'd.id = users.district', 'left');
        $this->db->join('facilities as f', 'f.id = users.facility', 'left');
        $this->db->join('quizes as q', 'q.id = p.quiz_id', 'left');
        if($s_id>0){
            $this->db->where('s.id', $s_id);
        }
        if($d_id>0){
            $this->db->where('d.id', $d_id);
        }

        if($f_id>0){
            $this->db->where('f.id', $f_id);
        }
        if($q_id>0){
            $this->db->where('q.id', $q_id);
            
        }
        if (strlen($searchVal)) {
            $searchCondition = "(
                users.first_name like '%$searchVal%' or
                users.last_name like '%$searchVal%' or
                users.mobile like '%$searchVal%'
            )";
            $this->db->where($searchCondition);
        }
        $this->db->limit($limit, $offset);
        $this->db->order_by($this->DT_column2[$sortColIndex], $sortBy);
        return $this->db->get()->result_array();
    }


    public function reportsCount($searchVal = '', $s_id =0, $d_id=0, $f_id=0, $q_id) {
        
        $this->db->select("count(p.id) as CountRows");
        $this->db->from('participants as p');
        $this->db->join('users', 'users.id = p.user_id', 'left');
        $this->db->join('states as s', 's.id = users.state', 'left');
        $this->db->join('districts as d', 'd.id = users.district', 'left');
        $this->db->join('facilities as f', 'f.id = users.facility', 'left');
        $this->db->join('quizes as q', 'q.id = p.quiz_id', 'left');
        if($s_id>0){
            $this->db->where('s.id', $s_id);
        }
        if($d_id>0){
            $this->db->where('d.id', $d_id);
        }

        if($f_id>0){
            $this->db->where('f.id', $f_id);
        }
        if($q_id>0){
            $this->db->where('q.id', $q_id);
        }
        if (strlen($searchVal)) {
            $searchCondition = "(
                users.first_name like '%$searchVal%' or
                users.last_name like '%$searchVal%' or
                users.mobile like '%$searchVal%'
            )";
            $this->db->where($searchCondition);
        }
        $query = $this->db->get()->row_array();
        return $query['CountRows'];
    }
















}