<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class User_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();    
    }

    public function login($u="",$p="")
    {
         
        if($u=="" OR $p=="")
            return "Empty Username/Password";
        $pass = md5($p);
        $query=$this->db->select('*')
                        ->from('admin')
                        ->where('username',$u)
                        ->where('password',$pass)
                        ->where('student_id >',0)
                        ->where('teacher_id',0)
                        ->where('login_status',0)
                        ->where('is_delete',0)
                        ->get()->row_array();
         //var_dump('<pre>',$this->db->last_query());
        // die();
      if(!empty($query)){
          
                $id = $query['id'];
                $u=$query['username'];
          $newdata =array(
                   'username'  => $u,
                   'id'     => $id
               );
        //      var_dump('<pre>',$newdata);
        // die();
         $this->session->set_userdata('user_sec',$newdata);

               
                        $name="user";
                        $value=$u;
                        $expire=time()+1*60*60*24;
                    set_cookie($name,$value,$expire);    
                        $name2="id";
                        $value2=$id;
                        $expire2=time()+1*60*60*24;
                    set_cookie($name2,$value2,$expire2); 
              
                redirect("dashboard/index");
         
        }else{

            $this->session->set_flashdata('login_fail', 'Incorrect Username or Password');
            redirect("user");
        }
    }
    
    

    public function logout()
    {
        delete_cookie('id');
        delete_cookie('user');
        //$this->session->unset_userdata('user_sec');
        
        //header('Location: $base_url');
       
        redirect($base_url,"refresh");
    }
    
    public function check_login($p="")
    { 
     
        if($p=="")
            die("Empty Page name in check login");
        $admin = get_cookie('id');
        //$this->session->userdata('user_sec');
        // var_dump('<pre>',$admin);
        // die();
        if(!empty($admin))
        {
          if($p=="login")
             redirect("dashboard","refresh");
        }else{
            if($p!="login")
                redirect("user","refresh");
        }
    }

    public function userInfo($c="")
    {
        
        if($c=="")
            $c = "*";
          //  echo $c;exit;
       //$admin2 = $this->session->userdata('user_sec');
        $id =get_cookie('id');
       $q = $this->db->query("SELECT $c FROM `admin` WHERE `id`='$id'");
        //echo "<pre>";print_r($q->row_array());exit;
        return $q->row_array();      
    }


    public function check_permissions($l="")
    {
        $admin = $this->userInfo('id')['id'];
        $submenu = $this->db->query("SELECT id FROM `submenu` WHERE `is_delete`='0' AND `link`='$l'")->result_array()[0]['id'];
        $permission = $this->db->query("SELECT id FROM `permission` WHERE `admin_id`='$admin' AND `submenu_id`='$submenu'");

        if($permission->num_rows() != 1)
        {
            redirect("home","refresh");
        }
    }

    public function getBranch()
    {
        $role = $this->userInfo('role_id')['role_id'];
        $branch = @$this->db->query("SELECT `branch_id` FROM `role` WHERE `id`='$role' AND `is_delete`='0'")->result_array()[0]['branch_id'];
        return $branch;
    }

    public function is_super()
    {
        if($this->getBranch()==0)
            return true;
        else
            return false;
    }

    public function limitText($t="",$l=10,$dots=true)
    {
        if($dots)
            $do = "...";
        else
            $do = "";
        if(strlen($t) > $l)
            $ret = substr($t, 0, $l) . $do;
        else
            $ret = $t;
        return $ret;
    }

    public function search_multi_arr($array=array(), $key, $val) 
    {
        foreach ($array as $item)
            if (isset($item[$key]) && $item[$key] == $val)
                return true;
        return false;
    }

    public function sendDetails($name=""){
        $user = $this->userInfo("first_name,last_name,contact_no");
        $bid = $this->getBranch();
        if($this->is_super())
            $b_name = "(Super)";
        else
            $b_name = $this->db->select("name")->from("branch")->where("id",$bid)->where("is_delete","0")->get()->row()->name;
         $message="Name: ".$user['first_name']." ".$user['last_name']." | ".$user['contact_no']." with username: ".$name." of branch: ".$b_name." has logged in to panel";
        //$this->hajanasms->sendOneNumber('923009318468',$message);
        }
        public function change_sess_by_id($id){
            $student = $this->db->query("SELECT * FROM student WHERE id = $id")->row();
            $admin = $this->db->query("SELECT * FROM admin WHERE student_id = $student->id AND username = $student->grno")->row();
            $this->session->unset_userdata('user_sec');
            $new_sec=array('id' => $admin->id, 'username' => $student->grno, 'is_parent' => 1);
            $this->session->userdata('user_sec',$new_sec);
            //$this->sendDetails($u);
            redirect("dashboard/index");
        }
 public function co_curricular_activites()
        {
        $this->db->select('*');
        $this->db->from('curricular_activites');
        $this->db->order_by('date');
        

        $query = $this->db->get();
        return $query->result_array();  
        }
        
        public function weekly_activities()
		{

	   	 $query=$this->db->get('weekly_activities');
	   	 return $query->result_array();

		}
		
		public function holiday()
		{
			$query=$this->db->get('holiday_vacation');
	   	 return $query->result_array();

		}

}

/* End of file User_model.php */
/* Location: ./application/models/User_model.php */
