<?php
	require "../commonincludefiles.php";
   	
	$i=0;
	
	if($_REQUEST['test_type'] == "firsttest"){
		
		$sql = "
			SELECT
				CONCAT(sm.student_surname,' ',sm.student_firstname) AS student_name,
				sm.student_id,
				sm.student_gturegno,
				rm.obtain_mark,
				rm.present,
				rm.present_pr,
				rm.obtain_marks_pr_ese,
				rm.obtain_marks_pr_pa
			FROM
				cms_studentmaster sm LEFT JOIN cms_resultmaster rm ON sm.student_id = rm.student_id 
					AND rm.exam_id = ".Escape($_REQUEST['exam_name'])."
					AND rm.subject_id = ".Escape($_REQUEST['subject_name'])."
					AND rm.test_type='f',
				   cms_studentdetail sd	 
			WHERE 
				sm.student_id = sd.student_id AND
				sm.student_active = 1 AND
				
				sd.cur_branchid = ".Escape($_REQUEST['branch_name'])." AND 
				sd.cur_semesterid = ".Escape($_REQUEST['semester_name'])." AND 
				sd.cur_branchyearid = '".Escape($_REQUEST['branchyear_name'])."' AND 
				sd.year_id = '".Escape($_REQUEST['year_name'])."' 
				ORDER BY student_gturegno ASC, student_name ASC";	
	}
	 else if($_REQUEST['test_type'] == "retest"){
		
		$qry = "SELECT
				CONCAT(sm.student_surname,' ',sm.student_firstname) AS student_name,
				sm.student_id,
				sm.student_gturegno,
				rm.obtain_mark,
				rm.present,
				rm.present_pr,
				rm.obtain_marks_pr_ese,
				rm.obtain_marks_pr_pa
			FROM
				cms_studentmaster sm,
				cms_resultmaster rm,
				cms_studentdetail sd	 
			WHERE 
				sm.student_id = sd.student_id AND
				sm.student_id = rm.student_id AND 
				rm.college_id = ".$_SESSION["VedantnrCollegeId"]." AND
				sd.college_id = ".$_SESSION["VedantnrCollegeId"]." AND
				rm.test_type='r' AND
				rm.exam_id = ".Escape($_REQUEST['exam_name'])." AND 
				rm.subject_id = ".Escape($_REQUEST['subject_name'])." AND 
				
				sm.student_active = 1 AND
				sd.cur_branchid = ".Escape($_REQUEST['branch_name'])." AND 
				sd.cur_semesterid = ".Escape($_REQUEST['semester_name'])." AND
				sd.cur_branchyearid = '".Escape($_REQUEST['branchyear_name'])."' AND
				sd.year_id = '".Escape($_REQUEST['year_name'])."' 
			    ORDER BY student_gturegno ASC,
				         student_name ASC";
			
			$result = dbQuery($qry);
			$result_count=dbRows($result);		 
			if($result_count>0)
			{
				$sql=$qry;	
				
		  
			
			 
			}
			else
			{
				 $sql = "SELECT
				CONCAT(sm.student_surname,' ',sm.student_firstname) AS student_name,
				sm.student_id,
				sm.student_gturegno,
				rm.present,
				rm.present_pr,
				rm.obtain_marks_pr_ese,
				rm.obtain_marks_pr_pa
			FROM
				cms_studentmaster sm,
				cms_resultmaster rm,
				cms_studentdetail sd	 
			WHERE 
				sm.student_id = sd.student_id AND
				sm.student_id = rm.student_id AND 
				rm.college_id = ".$_SESSION["VedantnrCollegeId"]." AND
				sd.college_id = ".$_SESSION["VedantnrCollegeId"]." AND
				rm.result = 'Fail' AND 
				rm.exam_id = ".Escape($_REQUEST['exam_name'])." AND 
				rm.subject_id = ".Escape($_REQUEST['subject_name'])." AND 
				
				sm.student_active = 1 AND
				sd.cur_branchid = ".Escape($_REQUEST['branch_name'])." AND 
				sd.cur_semesterid = ".Escape($_REQUEST['semester_name'])." AND
				sd.cur_branchyearid = '".Escape($_REQUEST['branchyear_name'])."' AND
				sd.year_id = '".Escape($_REQUEST['year_name'])."' 
			    ORDER BY student_gturegno ASC, student_name ASC";
				
			}
	}
		
	
		
	$rs = dbQuery($sql);		
	$total = dbRows($rs);
	
	$sql_pmark = "SELECT total_marks,passing_marks,total_marks_pr_ese,passing_marks_pr_ese,total_marks_pr_pa,passing_marks_pr_pa 
			FROM cms_examsetup WHERE branch_id = ".Escape($_REQUEST['branch_name'])." AND 
	              semester_id = ".Escape($_REQUEST['semester_name'])." AND exam_id = ".Escape($_REQUEST['exam_name'])." 
				  AND subject_id = ".Escape($_REQUEST['subject_name'])."";
	$res_pmark = dbQuery($sql_pmark);			
	$rec_pmark = dbArray($res_pmark);
	$total_marks = $rec_pmark['total_marks'];
	$passing_marks = $rec_pmark['passing_marks'];
	$total_marks_pr_ese = $rec_pmark['total_marks_pr_ese'];
	$passing_marks_pr_ese = $rec_pmark['passing_marks_pr_ese'];
	$total_marks_pr_pa = $rec_pmark['total_marks_pr_pa'];
	$passing_marks_pr_pa = $rec_pmark['passing_marks_pr_pa'];
	$SetupData = array("count" => $total, "data" => array());
	$j=0;
	if(dbRows($rs) > 0){
		while($row = dbArray($rs)){
			$temparray["student_name"]  	= $row["student_name"];
			$temparray["student_id"] 		= $row["student_id"];
			$temparray["student_gturegno"]	= $row["student_gturegno"];
			$temparray["total_marks"]		= $total_marks;
			$temparray["passing_marks"]		= $passing_marks;
			$temparray["total_marks_pr_ese"]		= $total_marks_pr_ese;
			$temparray["passing_marks_pr_ese"]		= $passing_marks_pr_ese;
			$temparray["total_marks_pr_pa"]		= $total_marks_pr_pa;
			$temparray["passing_marks_pr_pa"]		= $passing_marks_pr_pa;
			
			
			
			if($row["present"] == 1 || $row["present"] == ""){
				 $temparray["present"] 		= true;
				 $temparray["obtain_mark"]  	= $row["obtain_mark"];
			} else {
				 $temparray["present"]  		= false;
				$temparray["obtain_mark"]  	= '';	
			}
			
			if($row["present_pr"] == 1 || $row["present_pr"] == ""){
				 $temparray["present_pr"] = true;
				 $temparray["obtain_marks_pr_ese"]  = $row["obtain_marks_pr_ese"];
				 $temparray["obtain_marks_pr_pa"]  	= $row["obtain_marks_pr_pa"];
			}else {
				 $temparray["present_pr"]  		= false;
				 $temparray["obtain_marks_pr_ese"] = '';	
				 $temparray["obtain_marks_pr_pa"]  = '';
			}
			//$temparray["obtain_mark"]  	= $j++;	
			$SetupData["data"][$i] 			= $temparray;   
			$i++;
		}
	}
	echo json_encode($SetupData);	
?>