<?php defined('BASEPATH') OR exit('No direct script access allowed');
class SessionModel extends CI_Model {
    var $table = 'sessions';
    // set column field database for datatable orderable
    var $column_order = array('id', 'name', 'video_link','assessment_link');
    // set column field database for datatable searchable
    var $column_search = array('name', 'video_link', 'assessment_link');
    // default order
    var $order = array('id' => 'ASC');
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    function insert($insert) {
        $this->db->insert('sessions', $insert);
    }
    function allcategory($id = NULL) {
        $query = $this->db->select('id,name');
        if ($id) {
            $query->where('id !=', $id);
        }
        $result = $query->get('sessions')->result_array();
        return $result;
    }
    private function _get_datatables_query() {
        $this->db->from($this->table);
        $i = 0;
        // loop column
        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 get_session() {
        $this->_get_datatables_query();
        if ($_POST['length'] != - 1) $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
       
        return $query->result();
    }

    function count_filtered() {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    public function count_all() {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }
    function updatestatus($id, $status) {
        $this->db->set('status', $status)->where('id', $id)->update('sessions');
    }
    
    function play_video($id){
        if($this->db->set('status', '1')->where('id', $id)->update('sessions')){
         //  $this->db->set('status', '0')->where('id !=', $id)->update('sessions');
        }
    }

    function pause_video($id){
        $this->db->set('status', '0')->where('id', $id)->update('sessions');
    }

    function delete_video($id) {
        $this->db->where('id', $id)->delete('sessions');
    }
    
    function update($data, $id) {
        $this->db->where('id', $id)->update('sessions', $data);
    }
    
    function delete($id) {
        $this->db->where('id', $id)->delete('sessions');
    }

    function check_status($id,$status){
        return $this->db->select('*')->from('sessions')->where(array('id' => $id,'status' =>$status))->get();
    }

    function insert_session($data)
    {
        
       $data['added'] = date('Y-m-d H:i:s');
       $data['status'] = '0';
       return  $this->db->insert('sessions', $data);
    }
    
    function get_all_question_lang() { 
        return $this->db->order_by('category_title','asc')->get('category')->result();
    }

    function get_all_test() { 
        return $this->db->order_by('title','asc')->get('quizes')->result();
    }
}
