<?php defined('BASEPATH')or exit("NO DIRECT ACCESS IS ALLOWED");

class CommonModel extends CI_Model{
    protected $limit, $offset;
  public function  __construct(){
    parent::__construct();
  }
  public function setStsession($userid,$sessionid){
    //get previours mapped session id\
    //print_r(array($userid,$sessionid));
    $ss_id                 =   $userid;
    $user_id               =   substr($ss_id, 16);
    $oldsession_id         =   $this->db->select('session_id')->where(array('id'=>$user_id))->get('master_user')->row('session_id');

      //destroy session which mapped to previous user
      $this->db->where('id',$oldsession_id);
      $this->db->delete('ci_sessions');
      //map new session id to userid

      $this->db->where('id',$user_id);
      $this->db->update('master_user',array('session_id'=>$sessionid));
    //  echo $this->db->last_query();die;
  }
  // public function  find_session_id(){
  //
  // }
  //////Social //////////////////////////
  public function get_teachers_on_site(){
	  $query= $this->db->select(['*'])->from('teachers_on_site')->get();
	  return $query->result();
	  
  }
  public function get_parallel_session(){
      $response = array();
      $this->db->select('*');
      $q = $this->db->get('parallel_session_purchased');
      $response = $q->result_array();
      return $response;
  }
  public function find_duplicate_student_info($userid){
  //  echo $id.$path;die;
    $query =  $this->db->select('*')->from('mu_student_info')->where('user_id', $userid)->get();
  return  $query->num_rows();
  }
  public function update_extra_info($id,$array){

      $this->db->where('user_id', $id);
      $this->db->update('mu_student_info',$array);
      return true;
  }
  public function insert_extra_info($array){
      $this->db->insert('mu_student_info',$array);
      return $this->db->insert_id();
  }
  public function getcountry_dropdown(){
      $this->db->select('*');
      $q        = $this->db->get('countries');
      $return 	= 	array();
      if($q->num_rows() > 0) {
      $return[''] = ('Select Country');
        foreach($q->result_array() as $row) {
          $return[$row['id']] = strtoupper($row['name']);
        }
      }
          return $return;
  }
  public function getcountry(){
      $response = array();
      $this->db->select('*');
      $q = $this->db->get('countries');
      $response = $q->result_array();
      return $response;
  }
  public function get_country_state($postData){
      $response = array();
      $this->db->select('*');
      $this->db->where('country_id', $postData['country']);
      $q = $this->db->get('states');
      $response = $q->result_array();
      return $response;
  }
  public function get_state_city($postData){
      $response = array();
      $this->db->select('*');
      $this->db->where('state_id', $postData['state']);
      $q = $this->db->get('cities');
      $response = $q->result_array();
      return $response;
  }
  public function insert_twitter($data){
      $user = $this->checkIfUserExist_twitter($data['twitter_id']);

      if (!$user){
          $this->db->insert('master_user', $data);
          return $this->db->insert_id();
      }else{
          return $user->id;
      }
  }

  public function checkIfUserExist_twitter($twitterId){
      $sql = 'SELECT * FROM master_user WHERE twitter_id = ?';
      $query = $this->db->query($sql, array($twitterId));
      return $query->row();
  }


  public function insert_fb($data){
      $user = $this->checkIfUserExist_fb($data['fb_id']);

      if (!$user){
          $this->db->insert('master_user', $data);
          return $this->db->insert_id();
      }else{
          return $user->id;
      }
  }

  public function checkIfUserExist_fb($fbId){
      $sql = 'SELECT * FROM master_user WHERE fb_id = ?';
      $query = $this->db->query($sql, array($fbId));
      return $query->row();
  }


  public function insert_google($data){
      $user = $this->checkIfUserExist_google($data['google_id']);

      if (!$user){
          $this->db->insert('master_user', $data);
          return $this->db->insert_id();
      }else{
          return $user->id;
      }
  }

  public function checkIfUserExist_google($googleId){
      $sql = 'SELECT * FROM master_user WHERE google_id = ?';
      $query = $this->db->query($sql, array($googleId));
      return $query->row();
  }

  public function insert_insta($data){
      $user = $this->checkIfUserExist_insta($data['insta_id']);
      if (!$user){
          $this->db->insert('master_user', $data);
          return $this->db->insert_id();
      }else{
          return $user->id;
      }
  }
  public function insert_insta_info($data){

          $this->db->insert('master_user_insta_info', $data);
          return $this->db->insert_id();

  }

  public function checkIfUserExist_insta($instaId)
  {
      $sql = 'SELECT * FROM master_user WHERE insta_id = ?';
      $query = $this->db->query($sql, array($instaId));
      return $query->row();
  }

  ///////////////////Social /////////////////

  function getState(){
      $response = array();
      $this->db->select('*');
      $q        = $this->db->get('states');
      $response = $q->result_array();
      // echo "<pre>";print_r($response);die;
      return $response;
  }
  function getCity(){
      $response = array();
      $this->db->select('*');
      $q        = $this->db->get('cities');
      $response = $q->result_array();
      // echo "<pre>";print_r($response);die;
      return $response;
  }


  public function count_email_confirm($id,$hash){
      $query  =   $this->db->select(['is_confirmed','email','hashtag','id','admin_approve'])->where('id',$id)->where('hashtag',$hash)->from('master_user')->get();
      //if($query->num_rows()>0){
         // $row = $query->row();
     // }
      return $query->num_rows();
  }
  public function select_email_confirm($id,$hash){
      $query  =   $this->db->select(['is_confirmed','email','hashtag','id','category','admin_approve'])->where('id',$id)->where('hashtag',$hash)->from('master_user')->get();
      if($query->num_rows()>0){
       $row = $query->row();
       }
       return $row;
  }
  public function email_verified_process_done($id,$hash){
  	$this->db->set('is_confirmed','1');
  	$this->db->where('id',$id);
  	$this->db->where('hashtag',$hash);
  	$this->db->update('master_user');
  }
  public function reset_password_user($id,$hash,$password){

      $this->db->set('password',$password);
      $this->db->where('id',$id);
      $this->db->where('hashtag',$hash);
      $this->db->update('master_user');
  }

  public function category_list(){

		$query		=	  $this->db->select(['id','category'])->from('user_category')->order_by("id", "desc")->get();
		$return 	= 	array();
		if($query->num_rows() > 0) {
		$return[''] = ('Profile');
			foreach($query->result_array() as $row) {
				$return[$row['id']] = strtoupper($row['category']);
			}
		}
        return $return;
	}

  public function country_list(){

		$query		=	  $this->db->select(['id','name'])->from('countries')->order_by("id", "desc")->get();
		$return 	= 	array();
		if($query->num_rows() > 0) {
		$return[''] = ('Select Country');
			foreach($query->result_array() as $row) {
				$return[$row['id']] = strtoupper($row['name']);
			}
		}
        return $return;
	}

  public function insert_masteruser($data) {
        $this->db->insert('master_user', $data);
        return $this->db->insert_id();
  }
  public function insert_mu_teacher_basic($data) {
      $this->db->insert('mu_teacher_basic', $data);
      return $this->db->insert_id();
  }
  public function insert_mu_teacher_qualfication($data) {
      $this->db->insert('mu_teacher_qualfication', $data);
      return $this->db->insert_id();
  }
  public function insert_mu_teacher_pf_class($data) {
      $this->db->insert('mu_teacher_pf_class', $data);
      return $this->db->insert_id();
  }
  public function insert_mu_teacher_bank_detail($data) {
      $this->db->insert('mu_teacher_bank_detail', $data);
      return $this->db->insert_id();
  }
  public function insert_mu_teacher_extra_info($data) {
      $this->db->insert('mu_teacher_extra_info', $data);
      return $this->db->insert_id();
  }
  public function select_profile_info($id){
      $query  =   $this->db->select(['info'])->where('id',$id)->from('master_user')->get();
      if($query->num_rows()>0){
          //echo "<pre>";print_r($query->result());exit;
          //echo "<pre>";print_r($query->row());exit;
          $row = $query->row();
          return $row;
      }
      else{
          return false;
      }
  }
  public function update_profile_mu_teacher_basic($id,$field,$value){
      //  echo $id.$path;die;
      $this->db->set($field, $value);
      $this->db->where('user_id', $id);
      $this->db->update('mu_teacher_basic');
      return true;
  }
  public function update_profile_master_user($id,$field,$value){
      //  echo $id.$path;die;
      $this->db->set($field, $value);
      $this->db->where('id', $id);
      $this->db->update('master_user');
      return true;
  }

  public function update_profile($id,$path){
      //  echo $id.$path;die;
      $this->db->set('info', $path);
      $this->db->where('id', $id);
      $this->db->update('master_user');
      return true;
  }
  public function update_hash($id,$hash){
      $this->db->set('hashtag', $hash);
      $this->db->where('id', $id);
      $this->db->update('master_user');
      return true;
  }

  public function login_valid($email,$category){
		$query  =   $this->db->select(['id','email','password','category','hashtag','is_confirmed','admin_approve'])->where('email',$email)->where('category',$category)->from('master_user')->get();
		if($query->num_rows()>0){
			//   echo "<pre>";print_r($query->result());exit;
            //   echo "<pre>";print_r($query->row());exit;
			$row = $query->row();
			return $row;
		}
		else{
			return false;
		}
	}
	public function login_valid_count($email,$category){
	    $query  =   $this->db->select(['id','email','password','category','hashtag','is_confirmed','admin_approve'])->where('email',$email)->where('category',$category)->from('master_user')->get();
	   return $query->num_rows();

	}
	public function add_contact_us($data){

	    $this->db->insert('contact_us', $data);
	    return  $this->db->insert_id();
    }
    public function select_qualification_listing(){
	    $response = array();
        $this->db->select('*');
        $q = $this->db->get('qualification_list');
        $response = $q->result_array();
        return $response;
    }
    public function select_subject_listing(){
         $response = array();
        $this->db->select('*');
        $q = $this->db->get('subject_list_show');
        $response = $q->result_array();
        return $response;
    }

    public function qualification_listing(){

		$query		=	  $this->db->select(['id','full_name'])->from('qualification_list')->order_by("id", "asc")->get();
		$return 	= 	array();
		if($query->num_rows() > 0) {
		$return[''] = ('Qualification List');
			foreach($query->result_array() as $row) {
				$return[$row['id']] = strtoupper($row['full_name']);
			}
		}
        return $return;
    }

    public function subjectshow_listing($header){

		$query		=	  $this->db->select(['id','full_name'])->from('subject_list_show')->order_by("id", "asc")->get();
		$return 	= 	array();
		if($query->num_rows() > 0) {
		$return[''] = $header.' Subject List';
			foreach($query->result_array() as $row) {
				$return[$row['id']] = strtoupper($row['full_name']);
			}
		}
        return $return;
	}




	// public function duplicate_ia($ponumber){
	// 	$query	=	$this->db->select('ia.ponumber')->from('ia')->where('ponumber',$ponumber)->get();
	// 	return $query->num_rows();
	// }
	// public function ia_data_po($ponumber){
	// 	$query	=	$this->db->select('*')->from('ia')->where('ponumber',$ponumber)->get();
	// 	return $query->row();
	// }
	// public function update_ia($ponumber,$remarks){
	// 	$this->db->set('remarks',$remarks);
	// 	$this->db->where('ponumber',$ponumber);
	// 	$this->db->update('ia');
	// }
  //
	// public function update_iadone_iadate($ponumber,$ia_date,$technology,$update_by){
  //
	// 	$this->db->set('ia_date',$ia_date);
	// 	$this->db->set('technology',$technology);
	// 	$this->db->set('updated_by',$update_by);
	// 	$this->db->where('ponumber',$ponumber);
  //
	// 	$this->db->update('iadone');
	// }
	// public function projectcode_found_count($projectcode){
	// 	$query	=	$this->db->select('projectcode')->from('projectcode')->where('projectcode',$projectcode)->get();
	// 	return $query->num_rows();
	// }

}
