<?php defined('BASEPATH') or exit('No direct Access is Allowed'); ?>
<?php
class Categorymodel extends CI_Model{

	public function __construct() {
        parent::__construct();
    }
	public function category_list($limit, $offset){

		$offset = ($offset-1)*$limit;
		$query=$this->db->select(['*'])->from('category')
		//->order_by("category", "asc")
    	->limit($limit, $offset)
		->get();
		//echo $this->db->last_query();die;
		return $query->result();
	}

	public function num_rows(){

		$user_id = $this->session->userdata('user_id');
		$query=$this->db->select(['*'])->from('category')->get();
		return $query->num_rows();
	}
	public function insert_category($data_category){

		$this->db->insert('category', $data_category);
		$data_category['id'] = $this->db->insert_id();

		$this->db->set('category',$data_category['id']);
		$this->db->insert('course_name');
    	return $data_category['id'];


	}

	public function update_category($data_category,$list_id){

		$this->db->where('id',$list_id);
		$this->db->update('category', $data_category);
		return true;
	}

	public function view_category($list_id){

		$query=$this->db->select(['*'])->from('category')->where('id',$list_id)->get();
		return $query->row();
	}
	public function count_category($category){

		$query=$this->db->select(['*'])->from('category')->where('category',$category)->get();
		return $query->num_rows();
	}

	public function delete_category($list_id){

		$this->db->where('id', $list_id);
		$this->db->delete('category');
	}
	function getclass(){
	    $response = array();
	    // Select record
	    $this->db->select('*');
	    $q = $this->db->get('class');
	    $response = $q->result_array();

	    return $response;
	}

	// Get City departments
	function get_class_subject($postData){
	    $response = array();

	    // Select record
	    $this->db->select('id,subject');
	    $this->db->where('class_id', $postData['classes']);
	    $q = $this->db->get('subject');
	    $response = $q->result_array();

	    return $response;
	}

	// Get Department user
	function get_class_subject_faculty($postData){
	    // echo "<pre>";print_r($postData['subject']);die;
	    $response     = array();
	    $this->db->select('*');
	    $this->db->where('subject_id', $postData['subject']);
	    $q            = $this->db->get('v2_faculty');
	    $response     = $q->result_array();
	    return $response;
	}


	public function search($data_taskpo){

	    $condition	=	$data_taskpo['seach_term'];
	    $order		=	$data_taskpo['order_by'];
	    $query=$this->db->select(['category.*'])
	        ->from('category');

	        if($condition=='or' && !empty($order)){

	            if(!empty($data_taskpo['category'])){
	                $this->db->or_like('category.id',$data_taskpo['category'])->order_by('category.category',$order);
	            }
	            if(!empty($data_taskpo['category_code'])){
	                $this->db->or_like('category.category_code'	,$data_taskpo['category_code'])
	                ->order_by('category.category_code',$order);
	            }
	        }

	        if($condition=='or' && empty($order)){

	            if(!empty($data_taskpo['category'])){
	                $this->db->or_like('category.id'	,$data_taskpo['category'])		;
	            }
	            if(!empty($data_taskpo['category_code'])){
	                $this->db->or_like('category.category_code'	,$data_taskpo['category_code'])		;
	            }
	        }

	        if($condition=='and' && !empty($order)){

	            if(!empty($data_taskpo['category']))	{
	                $this->db->like('category.id',$data_taskpo['category'])->order_by('category.category'	,$order);
	            }
	            if(!empty($data_taskpo['category_code'])){
	                $this->db->like('category.category_code',$data_taskpo['category_code'])	->order_by('category.category_code'		,$order);
	            }
	        }

	        if($condition=='and' && empty($order)){

	            if(!empty($data_taskpo['category']))	{
	                $this->db->like('category.id',$data_taskpo['category']);
	            }
	            if(!empty($data_taskpo['category_code']))		{
	                $this->db->like('category.category_code',$data_taskpo['category_code'])	;
	            }
	        }

	        $query=$this->db->get();
	        //echo $this->db->last_query();die;
	        if($query->num_rows()>0){
	            return $query->result();
	        }
	        else{
	            return $query->result();
	        }
	}
}
?>
