<?php
include "db.php";
$ch=$_REQUEST['type'];

if($ch=="startexam"){

	$pprdetail=$db->getRow(" Select * from papers where id=".$_REQUEST['pid']);


$time=explode(':',$pprdetail['ttime']);
$min=$time['0']*60+$time['1'];
$cdt=date("Y-m-d H:i:s"); 
$endTime = strtotime("+ $min minutes ", strtotime($cdt));
$finaltime=date('Y-m-d H:i:s', $endTime);

//echo $cdt."||".$finaltime ;//exit;	
	$aryData=array(
	"paper_id"=>$_REQUEST['pid'],
	"student_id"=>$_SESSION['user']['uid'],
	"date"=>$cdt,
	"exp_date"=>$finaltime
	);
//echo "woow";exit;
	
	$checkexistance=$db->getVal(" Select exp_date from result where id=".$_REQUEST['pid']." and student_id=".$_SESSION['user']['uid']."");

	if($checkexistance>0)
	{
		$cdt=strtotime(date("Y-m-d H:i:s")) ;
	
//	echo $cdt."||". strtotime($checkexistance);exit;
	
		if($cdt<strtotime($checkexistance))
		{
			echo 1;
		  //$updt=$db->updateAry(RESULT,$aryData," where ");
		}else{
			echo "cannot eligble to give exam exam time finished";
		}
	
	}else{
		
		$ins=$db->insertAry("result",$aryData);
	
		if($ins>0){
			echo "1";
			}else{
				echo $db->getErMsg();
				}
		
	}
	
	exit;
	
	
}
elseif($ch=="question"){


$data=explode("||",$_REQUEST['bdata']);

	$check_existance=$db->getVal("select count(*) from examdetails where qid=".$data[0]." and user_id='".$data[3]."'");
 

	$aryData=array( 
					"qid"=>$data[0],		
					"answer"=>$data[1],		
					"paper_id"=>$data[2],
					"student_id"=>$data[3]		
	);
	
//echo"<pre>";
//print_r($aryData);exit;

	if($check_existance==0){
		 $insert_ans=$db->insertAry("examdetails",$aryData);
		 if($insert_ans>0){
			  echo "data inserted ";exit;
			 }
		 else{echo "insert_ans ".$db->getErMsg();}
		}
	else{
		 $update_ans=$db->updateAry("examdetails",$aryData," where qid='".$_REQUEST['qid']."' and user_id='".$_REQUEST['uid']."'");
		 if($update_ans>0){
			 echo"data updated";exit;
			 }
		 else{echo "update_ans ".$db->getErMsg();}
		}
}
elseif($ch=="smtppr")
{
//echo "<pre>";print_r($_REQUEST);exit;	
//print_r($_SESSION['user']['uid']);exit;

	$pprdtl=explode('||',$_REQUEST['pprdtl']);	
	
	$pid=$pprdtl['0'];
	$uid=$_SESSION['user']['uid'];
	
	$arry=array('status'=>1);
	$update=$db->updateAry("examdetails",$arry," where paper_id=$pid and student_id=$uid");

	if($update>0)
	{
		$all_q=$db->getRows("select * from examdetails where paper_id=$pid and student_id=$uid and status=1");
$ttl=0;

		if($all_q>0)
			{	
				foreach($all_q as $qs)
					{
						$rs="";
						$rs=checkans($qs['qid'],$qs['answer']);
						if($rs>0)
						{
							$c[]=$qs['qid'];
						}else{
							$w[]=$qs['qid'];
						}
						
					 $ttl++;	
					}


				
				$uc['ans_status']=1;
				for($ca=0;$ca<count($c);$ca++)
				{
				  $updt=$db->updateAry("examdetails",$uc," where paper_id=$pid and student_id=$uid and qid=".$c[$ca]);
				  
			/*if($updt>0)echo "<p>$ca </p>";
			else{echo $db->getErMsg()."||".$db->getLastQuery();}*/
				}
							
				$uw['ans_status']=0;
				for($wa=0;$wa<count($w);$wa++)
				{
					$updt=$db->updateAry("examdetails",$uw," where paper_id=$pid and student_id=$uid and qid=".$w[$wa]);
					/*if($updt>0)echo "<p>$wa </p>";
					else{
						 echo $db->getErMsg();
						}*/
				}
				$status['status']=1	;
				

				
				$updt=$db->updateAry("result",$status," where paper_id=$pid and student_id=$uid " );
				if($updt>0)
				{	
						
					 redirect(URL_ROOT."result_view.php?pid=".$pid."&uid=".$uid);	 
				
				}else{
				 	echo "<hr>error sss".$db->getLastQuery().$db->getErMsg();exit;
				 }
			
			}
		else{
				  echo "<hr>error hhh".$db->getLastQuery().$db->getLastQuery();	
			}
			
	}else{
		echo "error ".$db->getLastQuery().$db->getErMsg();	
	
	}
	
}
elseif($ch=='free_exam')
{
	$getFreeExam=$db->getRows("select * from ".PAPER." where class='".$_REQUEST['class_id']."' and subject='".$_REQUEST['id']."' and is_paid=0 and status=1");
	if(is_array($getFreeExam) && count($getFreeExam)>0)
	{
		echo "<label>Available Exams:</label><br>";
	?>	
<script language="javascript">
$("#saveBtn").show();
</script>    
    <select name="exam" class="form-control">
	<?php
		foreach($getFreeExam as $exm)
		{
/*echo "<label class='exam_radio'><input type='radio' name='exam' class='exam_radio' value='".$exm['id']."' />".$exm['name']."</label>";*/	
	?>
	
	<option value="<?php echo $exm['id'];?>"><?php echo $exm['name'] ?></option>

	<?php
    	}
		?>
		</select>
        <?php

	}else{
?>

<script language="javascript">
$("#saveBtn").hide();
</script>  		
<?php
	echo "Sorry Their are No Exams";	
	}
}
elseif($ch=='online_test_paper')
{
	$logdata=$objsubject->nonUserExam($_REQUEST);//echo $ch;
	if($logdata["status"]=="ok"){
		echo "success==>".$logdata["msg"]."==>".$logdata["url"];
	}else{
		echo "error==>".$logdata["msg"];
	}
}
elseif($ch=='online_admission')
{
	$logdata=$objsubject->online_admission($_REQUEST);//echo $ch;
	if($logdata["status"]=="ok"){
		echo "success==>".$logdata["msg"]."==>".$logdata['url'];
	}else{
		echo "error==>".$logdata["msg"];
	}
}
elseif($ch=='reattempt'){

//*****REQUEST FOR REATTEMPT BY STUDENT***//
	
	if(isset($_REQUEST['sid'])&&isset($_REQUEST['pid']))
	{   
		$aryData=array(
						'student_id'=>$_REQUEST['sid'],
						'paper_id'=>$_REQUEST['pid'],
						'status'=>1,
					  );

		$qry=$db->insertAry("reattempt",$aryData);
		
		notification("New Request for Re-attempt exam","request",0);
		if($aryData>0)
		{echo "ok";}	else {	echo "error";}	
}

	
}
elseif($ch=="getsubname"){
	$class=trim($_REQUEST['clsname']);
	$subname=$db->getRows("select subject from app_subject where class='".$class."'");
//echo $db->getLastQuery();

?>
 <select  id="subjectname"  class="form-control" name="subjectname">
                          <option value="0" >select</option>


<?php

foreach ($subname as $sname)
{
?>
                         
                          <option value="<?php echo $sname['subject'];?>" <?php  if($_REQUEST['subname']==$sname['subject'])echo 'selected="selected"';?>> <?php echo $sname['subject']; ?> </option>

<?php
}	
echo "</select>";

}

elseif($ch=="enquiry"){
$Err=="";
	$aryData=array(
					"name"=>$_REQUEST['name'],
					"phone"=>$_REQUEST['phone'],
					"email"=>$_REQUEST['email'],
					"detail"=>$_REQUEST['detail'],
			 	 );

 if($Err=="")
 {
	$insert=$db->insertAry("enquiry",$aryData);	
		if($insert!="")
		{
			$_SESSION['msg']="update successfully";
		}
		else{
			$_SESSION['error']=$Err;
		}
	   redirect(URL_ROOT); exit;
	}
 
}

elseif($ch=="editProfilePic"){
	$Err="";
	if(isset($_FILES["profile_pic"]["name"]) && !empty($_FILES["profile_pic"]["name"]))
		{
			$lfilename = basename($_FILES['profile_pic']['name']);
			$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
			if(!in_array($lext,array('exe','php','html','js','css','htm')))
			{
				$lnewfile=md5(microtime()).".".$lext;
				if(move_uploaded_file($_FILES['profile_pic']['tmp_name'],"uploads/profilepic/".$lnewfile))
				{
					$aryData['photo']=$lnewfile;	
				}else{
					$Err="Invalid file directory";
				}

			}else{
				$Err="Invalid file extension";
			}
		}
		if($Err==""){
			$flgn=$db->updateAry("student",$aryData,"where idno=".$_SESSION['user']['uid']);
			$_SESSION['msg']="update successfully";
		}
		else{
			$_SESSION['error']=$Err;
		}
		redirect(URL_ROOT); exit;
}

?>