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

class Adminusermodel extends CI_Model {


	function getusers($username, $password)

{

   $this -> db -> select('*');

   $this -> db -> from('admintable');

   $this -> db -> where('username', $username);

   $this -> db -> where('password', ($password));

   $this -> db -> limit(1);

 

   $query = $this -> db -> get();

 

   if($query -> num_rows() == 1)

   {

     return $query->result();

   }

   else

   {

     return false;

   }

}











function uploadimage($data,$img_id)
  
{
   
  if($img_id!=null){
   $this->db->insert('uploadedimages', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }
      
     
      
  
  }
}


function updateusertatus($id,$data) 
{

   $this->db->where('id',$id);


   if ( $this->db->update('people',$data) ) 
   {
      return true;
   } else {
     
          return false;
   }
}











function populateuser($id){

if($id!=null){
$this->db->select(" * from people where id='$id' ");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

  return $bioquery->result_array();
 
}else{

  return false;
}

}else{
 return false;	
}


}
function addnewlostfound($data)
{
// Query to insert data in database
  //$regnumber = $data['regnumber'];
  
    $this->db->insert('lostfound', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }


   else

   {

     return false;

   }


}
function populatelostfoundtype($type){

if($type!=null){
$this->db->select("* from lostfound where type='$type'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
  //return true;
}else{

  return false;
}

}

}


function populateallusers(){



$bioquery = $this -> db -> get('people');
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}


function populatelostfoundcategory(){



$bioquery = $this -> db -> get('category');
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}
function getlostfoundbyid($id){

if($id!=null){
$this->db->select(" * from  lostfound where id= '$id'");
$this -> db -> limit(1);
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}

function populatallproperty(){


$this->db->select("* from lostfound where status='store'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
  //return true;
}else{

  return false;
}



}

















function populaterequiremets($id){

if($id!=null){
$this->db->select(" * from  coursemessage where courseid='$id'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}

}


}













function addmessage($data,$id) 
{
   $this->db->where('courseid',$id);
   $q = $this->db->get('coursemessage');

   if ( $q->num_rows() > 0 ) 
   {
      $this->db->where('courseid',$id);
      $this->db->update('coursemessage',$data);
    if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
        return false; 
      }

   } else {
     
      $this->db->insert('coursemessage',$data);
        if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
        return false; 
      }
   }
}
function specificapplications($campusid,$collegeid,$departmentid){


  $this -> db -> select('*');

   $this -> db -> from('studentapplication');
$this -> db -> where('campusname', $campusid);
   $this -> db -> where('department', $departmentid);

   $this -> db -> where('college', $collegeid);
 $this->db->order_by('id');
$appquery = $this -> db -> get();
if($appquery->num_rows()>0){

   return $appquery->result();
   


}else{

  return false;
}
}




	function count_all($tablename)
	{
		$this->db->from($tablename);
		
		return $this->db->count_all_results();
	}
	
	public function fetch_students($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("registration");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
	
	
	public function searchallregistredstudents($searchterm,$limit)
{
    $sql = "SELECT * FROM registration
            WHERE regno LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or surname LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or coursename LIKE '%" . $searchterm . "%' or status LIKE '%" . $searchterm . "%'LIMIT " .$limit . ",20";
    $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	public function searchstudents($searchterm,$limit)
{
    $sql = "SELECT * FROM registration
            WHERE regno = '$searchterm ' or firstname LIKE '%" . $searchterm . "%'  or surname LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or coursename LIKE '%" . $searchterm . "%' or status LIKE '%" . $searchterm . "%'LIMIT " .$limit . ",20";
    $q = $this->db->query($sql);
	$this -> db -> limit(1);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
		
	public function search_record_count($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM registration WHERE regno LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or surname LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or coursename LIKE '%" . $searchterm . "%'or status LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
	public function searchallstudents($searchterm,$limit)
{
    $sql = "SELECT * FROM studentlogin
            WHERE gender LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or surname LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or county LIKE '%" . $searchterm . "%' LIMIT " .$limit . ",20";
    $q = $this->db->query($sql);
    if($q->num_rows() > 0)
    {
        foreach($q->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        return false;
    }
}
	
	
	
	
	public function searchallstudents_record_count($searchterm)
	{
		 $sql = "SELECT COUNT(*) As cnt FROM studentlogin WHERE gender LIKE '%" . $searchterm . "%' or firstname LIKE '%" . $searchterm . "%'  or surname LIKE '%" . $searchterm . "%' or idno LIKE '%" . $searchterm . "%' or county LIKE '%" . $searchterm . "%' ";
   
		$q = $this->db->query($sql);
		$row = $q->row(); 
		return $row->cnt;
	}
	
	public function searchterm_handler($searchterm)
	{
		if($searchterm)
		{
			$this->session->set_userdata('searchterm', $searchterm);
			return $searchterm;
		}
		elseif($this->session->userdata('searchterm'))
		{
			$searchterm = $this->session->userdata('searchterm');
			return $searchterm;
		}
		else
		{
			$searchterm ="";
			return $searchterm;
		}
	}
	
	
	
	
	
	
	
function getapplicationsadmission($id){


  $this -> db -> select('*');

   $this -> db -> from('studentapplication');
$this -> db -> where('id', $id);
 
 
 $this->db->order_by('id');
$appquery = $this -> db -> get();
if($appquery->num_rows()>0){

   return $appquery->result();
   


}else{

  return false;
}
}


function getspecificapplications($campusid, $courseid){


  $this -> db -> select('*');

   $this -> db -> from('coursedepartment');
$this -> db -> where('campusid', $campusid);
   $this -> db -> where('courseid', $courseid);
 
 $this->db->order_by('id');
$appquery = $this -> db -> get();
if($appquery->num_rows()>0){

   return $appquery->result_array();
   


}else{

  return false;
}
}

function getmyapplications($id){


$this->db->select(" * from  studentapplication where studentid= '$id'");
$this -> db -> limit(1);
 $this->db->order_by('id','DESC');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}
function verfyuserusername($username){


$this->db->select(" * from  admintable where username= '$username'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}

function verfyvisitorusername($username){


$this->db->select(" * from   registration where regno= '$username'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}
function verfyuserusernameemail($username,$email){


$this->db->select(" * from  admintable where username= '$username' and email='$email'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}
function verfyvisitorusernameemail($username,$email){


$this->db->select(" * from   registration where regno= '$username' and email='$email'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}

function verfyuseremail($email){


$this->db->select(" * from  admintable where email= '$email'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}
function verfyvisitoremail($email){


$this->db->select(" * from   registration where email= '$email'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return true;
   


}else{

  return false;
}




}
function viewallspecificapplications($id){


$this->db->select(" * from  studentapplication where coursename= '$id'");

 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}



function populatestudentbyid($id){


$this->db->select(" * from  studentstatus where code= '$id'");

 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}
function getapplications($id){


$this->db->select(" * from  studentapplication where id= '$id'");
$this -> db -> limit(1);
 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();
   


}else{

  return false;
}




}




function getstudents($id){


$this->db->select(" * from  students where studentid= '$id'");
$this -> db -> limit(1);
 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}











function getmymessage($id){

if($id!=null){
$this->db->select(" * from coursemessage where courseid= '$id'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();

}else{

  return false;
}

}

}

function getapplicationsfeedback($id){


$this->db->select(" * from  studentapplication where id= '$id'");
$this -> db -> limit(1);
 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   


}else{

  return false;
}




}


function checkstudentemailid($email,$idno) {


$this->db->select(" * from  registration where regno= '$regno'  and studentid= '$studentid' ");

 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   
  return true;

}else{

  return false;
}




}


   function checkstudentemaill($email) 
{
   $this->db->where('email',$email);
  

   $q = $this->db->get('studentlogin');

   if ( $q->num_rows() > 0 ) 
   {return $q->result();
      return true;
   } else {
     
          return false;
   }
}						   
  function checkstudentidno($idno) 
{
   $this->db->where('idno',$idno);
  

   $q = $this->db->get('studentlogin');

   if ( $q->num_rows() > 0 ) 
   {
	   return $q->result();
      return true;
   } else {
     
          return false;
   }
}


  function checkstudentregno($regno) 
{
   $this->db->where('regno',$regno);
  

   $q = $this->db->get('registration');

   if ( $q->num_rows() > 0 ) 
   {
	   return $q->result();
      return true;
   } else {
     
          return false;
   }
}



 


function checkstudentregnoverification($regno,$studentid) {


$this->db->select(" * from  registration where regno= '$regno'  and studentid= '$studentid' ");

 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   
  return true;

}else{

  return false;
}




}

function checkstudentregnoidverification($regno,$studentid) {


$this->db->select(" * from  registration where regno= '$regno' and studentid!='' and studentid!= '$studentid' ");
$this -> db -> limit(1);
 $this->db->order_by('id');
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   
  return true;

}else{

  return false;
}




}









		function add_registration($data,$regno) 
{
   $this->db->where('regno',$regno);
   $q = $this->db->get('registration');

   if ( $q->num_rows() > 0 ) 
   {
      $this->db->where('regno',$regno);
      $this->db->update('registration',$data);
   } else {
     
      $this->db->insert('registration',$data);
   }
   $this->db->trans_complete(); 
       if($this->db->trans_status() === TRUE)
	   {// Check Transaction Result successful                          
 
  
  return true;

      }


   else

   {

     return false;

   }
}
								
								
							











function signupuser($data)
{// Query to insert data in database
  $regno = $data['regno'];
   $this -> db -> select("* from studentlogin where regno='$regno'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {

    $this->db->insert('studentlogin', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }

   }

   else

   {

     return false;

   }


}
function populateadmindata($id){

if($id!=null){
$this->db->select(" * from admintable where id= '$id'");
$this -> db -> limit(1);
$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result_array();

}else{

  return false;
}

}


}

function sigadmin($data)
{
// Query to insert data in database
	$pfnumber = $data['username'];
	 $this -> db -> select("* from admintable where username='$username'");
 	$this -> db -> limit(1);
	 $query = $this -> db -> get();
	if($query -> num_rows() == 0)

   {

    $this->db->insert('admintable', $data);
	if ($this->db->affected_rows() > 0) 
			{
	return true;

			}

   }

   else

   {

     return false;

   }


}
function signvisitor($data)
{
// Query to insert data in database
	$username = $data['regno'];
	 $this -> db -> select("* from registration where regno='$username'");
 	$this -> db -> limit(1);
	 $query = $this -> db -> get();
	if($query -> num_rows() == 0)

   {

    $this->db->insert('registration', $data);
	if ($this->db->affected_rows() > 0) 
			{
	return true;

			}

   }

   else

   {

     return false;

   }


}
            function uploadstudent($data)
{
// Query to insert data in database
  $regno = $data['regno'];
   $this -> db -> select("* from registration where regno='$regno'");
  $this -> db -> limit(1);
   $query = $this -> db -> get();
  if($query -> num_rows() == 0)

   {

    $this->db->insert('registration', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }

   }

   else

   {

     return false;

   }


}


            function uploadstudent2($data)
{
// Query to insert data in database



    $this->db->insert('district', $data);
  if ($this->db->affected_rows() > 0) 
      {
  return true;

      }

   

   else

   {

     return false;

   }


}

function populateusers(){
 

  $this -> db -> select('*');

   $this -> db -> from('admintable');


   $this->db->order_by('id');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

function populatevisitor(){


$this->db->select(" * from registration where  type='visitor'or  type='staff'");

$bioquery = $this -> db -> get();
if($bioquery->num_rows()>0){

   return $bioquery->result();
   //return true;
}else{

  return false;
}




}



















function populateallapplication(){
 

  $this -> db -> select('*');

   $this -> db -> from('studentapplication');


   $this->db->order_by('id');
 $query1 = $this -> db -> get();
 
 if($query1 -> num_rows() >0)

   {
  return $query1->result();

   }

   else

   {

     return false;

   }

}

           
    function updateuser($data,$id)
    {
	  $updatedat = Date('Y-m-d H:i:s') ;
$this->db->where('id', $id);
  $this->db->set('updatedat', $updatedat);
  $this->db->update('admintable', $data);
   if ($this->db->affected_rows() > 0) 
      {
  return true;

      }else{
        return false; 
      }
  }
  
			
			
			
			
			
 

public function displayallstudents($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get("studentlogin");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }


} 
?>