<?php  
	require "commonincludefiles.php";
	if($_SESSION["VedantnrCollegeId"]!="" && isset($_SESSION["VedantnrCollegeId"]))
	{
		/* start new code */
		$objResult = new ResultMgmt();
		$branch_id 		= $_POST['branch_hidden'];	
		$semester_id 	= $_POST['semester_hidden'];
		$exam_id 		= $_POST['exam_hidden'];	
		$subject_id 	= $_POST['subject_hidden'];
		$student_id 	= explode(",",substr($_REQUEST['student_id'],0,-1));
		$obtain_mark	= explode(",",substr($_REQUEST['obtain_mark'],0,-1));
		$present 		= explode(",",substr($_REQUEST['present'],0,-1));
		$present_pr 		= explode(",",substr($_REQUEST['present_pr'],0,-1));		
		$obtainmark_pr_pa	= explode(",",substr($_REQUEST['obtainmark_pr_pa'],0,-1));
		$obtainmark_pr_ese	= explode(",",substr($_REQUEST['obtainmark_pr_ese'],0,-1));

		
		$test_type=$_POST['test_hidden']=='firsttest'?'f':'r';
		
		$sql_select = "SELECT * FROM cms_resultmaster WHERE branch_id = '".$branch_id."' AND 
		               semester_id = '".$semester_id."' AND exam_id = '".$exam_id."' AND subject_id = '".$subject_id."'
					   AND test_type='".$test_type."'
					   AND year_id = ".$_SESSION["VedantnrYearId"]." AND student_id IN(".substr($_REQUEST['student_id'],0,-1).") ";
		$res_select = dbQuery($sql_select);
		
		if(dbRows($res_select) > 0){
			$sql_delete = "DELETE FROM cms_resultmaster WHERE branch_id = '".$branch_id."' AND 
		                   semester_id = '".$semester_id."' AND exam_id = '".$exam_id."' AND subject_id = '".$subject_id."'
						   AND test_type='".$test_type."'
						   AND year_id = ".$_SESSION["VedantnrYearId"]." AND student_id IN(".substr($_REQUEST['student_id'],0,-1).") ";
			$res_delete = dbQuery($sql_delete);
		}
		
		$SQL_SETUP = "SELECT passing_marks FROM cms_examsetup WHERE branch_id = ".$branch_id." AND 
		              semester_id = ".$semester_id." AND exam_id = ".$exam_id." AND subject_id = ".$subject_id." AND
					  year_id = ".$_SESSION["VedantnrYearId"];
		
		$RES_SETUP = dbQuery($SQL_SETUP);
		$REC_SETUP = dbArray($RES_SETUP);
		$REC_SETUP = $REC_SETUP['passing_marks'];
		
		$objResult->branch_id	 = $branch_id;
		$objResult->semester_id	 = $semester_id;
		$objResult->exam_id		 = $exam_id;
		$objResult->subject_id	 = $subject_id;
		$objResult->test_type	 =$test_type;
		
		
		for($i = 0; $i < count($student_id); $i++){
		$objResult->student_id	= $student_id[$i];
		
		if($present[$i]=='true' || $present[$i]==' true'){
				
				$objResult->present	= 1;	
				$objResult->obtain_mark	= $obtain_mark[$i];
				if($obtain_mark[$i] < $REC_SETUP){
					$objResult->result	= 'Fail';	
				} else{
					$objResult->result	= 'Pass';	
				}
			} else{
				$objResult->present	= 0;
				$objResult->obtain_mark='';
				$objResult->result	= 'Fail';	
			}
			
		  if($present_pr[$i]=='true' || $present_pr[$i]==' true'){
				$objResult->present_pr	= 1;	
				$objResult->obtain_marks_pr_ese	= $obtainmark_pr_ese[$i];
				$objResult->obtain_marks_pr_pa	= $obtainmark_pr_pa[$i];
			}else{
				$objResult->present_pr	= 0;
				$objResult->obtain_marks_pr_ese='';
				$objResult->obtain_marks_pr_pa	= '';
			}
			$objResult->add();
		}
		/* end new code */
		
		echo '{"success":"true"}';
	}
	else
	{
		echo '{"success":"false","msg":"session expiry"}';
	}
?>