<?php
Class Result_model extends CI_Model
{
	
 
 function result_list($limit,$status='0'){
	$result_open=$this->lang->line('open');
	$logged_in=$this->session->userdata('logged_in');
	$uid=$logged_in['uid'];
	  
		
	if($this->input->post('search')){
		 $search=$this->input->post('search');
		 $this->db->or_where('savsoft_users.email',$search);
		 $this->db->or_where('savsoft_users.first_name',$search);
		 $this->db->or_where('savsoft_users.last_name',$search);
		 $this->db->or_where('savsoft_users.contact_no',$search);
		 $this->db->or_where('savsoft_result.rid',$search);
		 $this->db->or_where('savsoft_quiz.quiz_name',$search);
 
	 }else{
		 $this->db->where('savsoft_result.result_status !=',$result_open);
	 }
	 	 
		if(!in_array('List_all',explode(',',$logged_in['results']))){
		$this->db->where('savsoft_result.uid',$uid);
		}else{
		$logged_in=$this->session->userdata('logged_in');
	
		 if($logged_in['uid'] != '1'){
	 $uid=$logged_in['uid'];
	 $this->db->where('savsoft_quiz.inserted_by',$uid);
	 } 
		}
	 	if($status !='0'){
			$this->db->where('savsoft_result.result_status',$status);
		}
		
		
		
		$this->db->limit($this->config->item('number_of_rows'),$limit);
		$this->db->order_by('rid','desc');
		$this->db->join('savsoft_users','savsoft_users.uid=savsoft_result.uid');
		$this->db->join('savsoft_quiz','savsoft_quiz.quid=savsoft_result.quid');
		$query=$this->db->get('savsoft_result');
		return $query->result_array();
		
	 
 }
 
 function quiz_list(){
	 $this->db->order_by('quid','desc');
$query=$this->db->get('savsoft_quiz');	
return $query->result_array();	 
 }
 
 
 function no_attempt($quid,$uid){
	 
	$query=$this->db->query(" select * from savsoft_result where uid='$uid' and quid='$quid' ");
		return $query->num_rows(); 
 }
 
 
 function remove_result($rid){
	 
	 $this->db->where('savsoft_result.rid',$rid);
	 if($this->db->delete('savsoft_result')){
		  $this->db->where('rid',$rid);
		  $this->db->delete('savsoft_answers');
		 return true;
	 }else{
		 
		 return false; 
	 }
	 
	 
	 
 }


  function pass_student_list($limit){
	
/*$query=$this->db->query("SELECT `Score`.`email`,`Score`.`name`,`Score`.`reg_no`,`Score`.`branch`,`Score`.`pct_10`,`category_name`, COUNT(`Score`.`email`) As count FROM `Score` LEFT JOIN `savsoft_users` ON `savsoft_users`.`uid` = `Score`.`User_id`GROUP BY `Score`.`email`,`Score`.`category_name`
 ");*/
 if($this->input->post('search')){
	$search=$this->input->post('search');
	$this->db->or_like('qualified_final.email',$search);
	$this->db->or_like('qualified_final.name',$search);
 }
 $this->db->limit($this->config->item('number_of_rows'),$limit);
 /*$query=$this->db->query("SELECT  * FROM `qualified_final`
 ");*/
 $query=$this->db->get('qualified_final');
return $query->result_array();
 }


 function qualify_student_list(){
	// $this->db->order_by('quid','desc');
		$query=$this->db->query("SELECT  * FROM `qualified_final`
 ");
return $query->result_array();
 }


  function category_list(){
	// $this->db->order_by('cid','desc');
		$query=$this->db->query("SELECT  * FROM `savsoft_category`
 ");
return $query->result_array();
	 
 }
 
 public function export() {
	$this->db->select(array('email', 'name', 'reg_no', 'branch', 'contact_no','apti_score', 'english_score','reasoning_score','technical_score'));
	$this->db->from('qualified_final');
	//$this->db->limit(10);  
	$query = $this->db->get();
	return $query->result_array();
}
public function export_qualified($apti_cutoff,$eng_cutoff,$reasoning_cutoff,$tech_cutoff) {
	$query=$this->db->query("SELECT  * FROM `qualified_final` where(`apti_score`>=$apti_cutoff AND `english_score`>=$eng_cutoff AND `reasoning_score`>=$reasoning_cutoff AND `technical_score`>=$tech_cutoff)
 ");
return $query->result_array();
}
public function export_negative() {
	$query=$this->db->query("SELECT  * FROM `qualified_final`");	
		return $query->result_array();
	}
	public function export_qualified_negative() {
		$query=$this->db->query("SELECT  * FROM `qualified_final`");	
			return $query->result_array();
		}
 function generate_report($quid,$gid){
	$logged_in=$this->session->userdata('logged_in');
	$uid=$logged_in['uid'];
	$date1=$this->input->post('date1');
	 $date2=$this->input->post('date2');
		
		if($quid != '0'){
			$this->db->where('savsoft_result.quid',$quid);
		}
		if($gid != '0'){
			$this->db->where('savsoft_users.gid',$gid);
		}
		if($date1 != ''){
			$this->db->where('savsoft_result.start_time >=',strtotime($date1));
		}
		if($date2 != ''){
			$this->db->where('savsoft_result.start_time <=',strtotime($date2));
		}

	 	$this->db->order_by('rid','desc');
		$this->db->join('savsoft_users','savsoft_users.uid=savsoft_result.uid');
		$this->db->join('savsoft_group','savsoft_group.gid=savsoft_users.gid');
		$this->db->join('savsoft_quiz','savsoft_quiz.quid=savsoft_result.quid');
		$query=$this->db->get('savsoft_result');
		return $query->result_array();
 }
 
 
 
 
 
 function get_result($rid){
	$logged_in=$this->session->userdata('logged_in');
	$uid=$logged_in['uid'];
		if($logged_in['su']=='0'){
			$this->db->where('savsoft_result.uid',$uid);
		}
		$this->db->where('savsoft_result.rid',$rid);
	 	$this->db->join('savsoft_users','savsoft_users.uid=savsoft_result.uid');
		$this->db->join('savsoft_group','savsoft_group.gid=savsoft_users.gid');
		$this->db->join('savsoft_quiz','savsoft_quiz.quid=savsoft_result.quid');
		$query=$this->db->get('savsoft_result');
		return $query->row_array();
	 
	 
 }
 
 
 function last_ten_result($quid){
		$this->db->order_by('percentage_obtained','desc');
		$this->db->limit(10);		
	 	$this->db->where('savsoft_result.quid',$quid);
	 	$this->db->join('savsoft_users','savsoft_users.uid=savsoft_result.uid'); 
		$this->db->join('savsoft_quiz','savsoft_quiz.quid=savsoft_result.quid');
		$query=$this->db->get('savsoft_result');
		return $query->result_array();
 }
 
 
 
   function get_percentile($quid,$uid,$score){
  $logged_in =$this->session->userdata('logged_in');
$gid= $logged_in['gid'];
$res=array();
	$this->db->where("savsoft_result.quid",$quid);
	 $this->db->group_by("savsoft_result.uid");
	 $this->db->order_by("savsoft_result.score_obtained",'DESC');
	$query = $this -> db -> get('savsoft_result');
	$res[0]=$query -> num_rows();

	
	$this->db->where("savsoft_result.quid",$quid);
	$this->db->where("savsoft_result.uid !=",$uid);
	$this->db->where("savsoft_result.score_obtained <=",$score);
	$this->db->group_by("savsoft_result.uid");
	 $this->db->order_by("savsoft_result.score_obtained",'DESC');
	$querys = $this -> db -> get('savsoft_result');
	$res[1]=$querys -> num_rows();
		
   return $res;
  
  
 }
 
 
 
 
 
 
 
 
 
 
 
 
 

}












?>
