<?php require "_header.php";?>
<script language="javascript" type="text/javascript">
document.getElementById("main-content").style.display = 'none';
</script>
<div id="main-report" align="center">
<style>
	.table-report{
		font-family:Verdana, Geneva, sans-serif;
		font-size:12px;
		text-align:left;
		border-right:0px none;
		border-bottom:0px none;
	}
	.table-report-inner{
		font-family:Verdana, Geneva, sans-serif;
		font-size:12px;
	}
	.table-report tr{
		padding:2px 2px 2px 2px;
	}
	.table-report td{
		padding:2px 2px 2px 2px;
		border-top:0px none;
		border-left:0px none;
	}
	.table-report th{
		padding:2px 2px 2px 2px;
		font-weight:bold;
		text-align:left;
		border-top:0px none;
		border-left:0px none;
	}	
	.college_name{
		font-family:QTAbbie;
		font-size:22px;	
		font-weight:bold;
		padding:2px 2px 2px 2px;
		text-align:center;
	}
	.college_address{
		font-family:Tahoma, Geneva, sans-serif;
		font-size:11px;	
		text-align:center;
		padding:2px 2px 2px 2px;
	}
	.report_name{
		font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
		font-size:13px;	
		background-color:#CCC;
		text-align:center;
		font-weight:bold;
		padding:2px 2px 2px 2px;
	}
</style>
<?php 
	$header_row_string="Exam Marks Report";
	/*echo "<pre>";
	print_r($_REQUEST);
	exit;*/
	$branch_id 		= Escape($_REQUEST['branch_hidden']);
	$semester_id 	  = Escape($_REQUEST['semester_hidden']);
	$division_id 	  = Escape($_REQUEST['division_id']);
	$exam_id 		  = Escape($_REQUEST['exam_hidden']); 
	$subject_id 	   = Escape($_REQUEST['subject_hidden']); 
	$result_disp 	  = Escape($_REQUEST['result']); 
	$branch_disp 	  = Escape($_REQUEST['branch_disp']);
	$semester_disp	= Escape($_REQUEST['semester_disp']);
	$exam_name 		= Escape($_REQUEST['exam_disp']);
	$subject_name 	 = Escape($_REQUEST['subject_disp']);
	
	$test_type=$_REQUEST['test_hidden']=='retest'?'r':'f';
    $show_pass_marks=($_REQUEST['show_pass_marks']=='true'?1:0);
	$show_retest_fees=($_REQUEST['show_retest_fees']=='true'?1:0);
	if($branch_id == "" && $semester_id == "" && $exam_id == ""){?>
		<script language="javascript" type="text/javascript">
			location.href='report-exampass.php';
		</script><?php
	}
	elseif($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])){
		ob_start();
		$i=1;
		
		/* START SUBJECT DATA */
		if($show_retest_fees==1){
			$gtu_fess=" select * from cms_gtufeeshead where head_active=1 AND gtuhead_name='GTU Exam Retest Fee' And  year_id= ".$_SESSION["VedantnrYearId"];
			$res_gtu = dbQuery($gtu_fess);	
			$gtu_numros=dbRows($res_gtu);
			$rec_gtu=dbArray($res_gtu);
		}
		
		 $sql_subject = "
			SELECT
				distinct(res.subject_id),
				sub.subject_name,
				sub.short_name,
				res.login_id,
				user_name
			FROM
				cms_subjectmaster sub
				left join cms_examsetup es on es.subject_id = sub.subject_id,
				cms_resultmaster res 
				left join cms_usermaster user on user.user_id=res.login_id
			WHERE
				sub.subject_active = 1 AND
			    res.subject_id=sub.subject_id AND
				sub.branch_id = res.branch_id AND
				sub.semester_id = res.semester_id AND
				sub.college_id = res.college_id AND
				res.branch_id = $branch_id AND	
				res.semester_id = $semester_id AND
				res.exam_id=$exam_id AND 
				es.total_marks != 0 AND es.passing_marks != 0 AND
				res.year_id='".$_SESSION['aYearId']."' AND
				res.test_type='".$test_type."' AND
				sub.college_id = ".$_SESSION['VedantnrCollegeId'];
			
		if($subject_id != "" && $subject_id != "undefined"){
			$sql_subject .= " AND sub.subject_id = $subject_id "; 
		}	
		
		$sql_subject .= " group by res.subject_id ORDER BY sub.subject_id ASC ";	
		/*echo $sql_subject;
		exit;*/
		$res_subject = dbQuery($sql_subject);		
		/* END SUBJECT DATA */
				
		/* START STUDENT DATA */
		$sql_student = "
			SELECT
				distinct(sm.student_id)As student_id,
				sm.student_gturegno,
				CONCAT(sm.student_surname,' ',sm.student_firstname,' ',SUBSTR(sm.student_lastname, 1, 1),'.') AS student_name
			FROM
				cms_studentmaster sm,
				cms_studentdetail sd,
				cms_resultmaster res
			WHERE
				sm.student_id = sd.student_id AND
				res.student_id = sd.student_id AND
				res.student_id = sm.student_id AND
				
				sm.student_active = 1 AND 
				res.year_id = '".$_SESSION["VedantnrYearId"]."' AND
				sd.year_id = ".$_SESSION["VedantnrYearId"];
		
		$where = "";
		/*if($student_id != 0){
			$where .= " AND sm.student_id = $student_id "; 
		}*/
		if($branch_id != ""){
			$where .= " AND sd.cur_branchid = $branch_id ";
		}
		if($semester_id != ""){
			$where .= " AND sd.cur_semesterid = $semester_id ";
		}
		if($division_id != ""){
			$where .= " AND sd.cur_divison = '$division_id'";
		}
			
		if($exam_id != "")
		{	$where .= " AND res.exam_id=$exam_id  ";
			$where .= " AND res.test_type='".$test_type."'";
		}
		if($subject_id != "" && $subject_id != "undefined"){
			$where .= " AND res.subject_id = $subject_id "; 
		}	
		
		
		if($where != "")
			$sql_student .= $where;	
			
		 $sql_student .= " ORDER BY sm.student_gturegno ASC ";	
					
		$res_student = dbQuery($sql_student);
		/* END STUDENT DATA */
		
		/* START MARK DATA */
		function getMark($student_id,$subject_id,$exam_id,$test_type,$semester_id,$branch_id)
		{
			$sql_mark = "
				SELECT 
					obtain_mark,
					result,
					present 
				FROM 
					cms_resultmaster 
				WHERE
					college_id = ".$_SESSION['VedantnrCollegeId']." AND
					year_id = ".$_SESSION['aYearId']." AND	
					branch_id = ".$branch_id." AND		
					semester_id = ".$semester_id." AND		
					exam_id = ".$exam_id." AND	
					test_type='".$test_type."' AND	
					subject_id = ".$subject_id." AND		
					student_id = ".$student_id;
			$res_mark = dbQuery($sql_mark);		
			return $res_mark;
		}
		/* END MARK DATA */
		
		if(dbRows($res_student)>0){?>
        	<div class="college_name">
                <?php echo $_SESSION["VedantnrCollegeName"]; ?> 
            </div>
            <div class="college_address">
               	<?php echo $_SESSION["VedantnrCollegeAddress"]; ?>
            </div>
            <div class="report_name">
              	Exam Marks Report
            </div>
            <div>
                <div class="left"><b>Branch</b> : <?php echo $branch_disp." - ".$semester_disp." - ".$division_id; ?></div>
                <div class="right"><b>Exam</b> : <?php echo $exam_disp.'&nbsp;('.ucfirst($_REQUEST['test_hidden']).')'; ?></div>
            </div>
            <br />
            <table width="100%" class="table-report" border="1" cellpadding="0" cellspacing="0">
            	<tr>
                	<th style="text-align:center;" width="5%" rowspan="2">Sr No</th>
                	<th style="text-align:center;" width="10%" rowspan="2">Enrollment No</th>
                    <th style="text-align:center;" width="25%" rowspan="2">Student Name</th>
                    <th style="text-align:center;" width="30%" colspan="<?php echo dbRows($res_subject); ?>">Subject</th>
                   <?php if($show_pass_marks){ ?>
                    <th style="text-align:center;" width="10%" rowspan="2">Total Mark</th>
                    <?php } ?>
                    <?php if($show_retest_fees==1 && $gtu_numros>0 ){ ?>
                    <th style="text-align:center;" width="10%" rowspan="2">Retest Fees</th>
                    <?php } ?>
                    <th style="text-align:center;" width="10%" rowspan="2">Result</th>
                </tr>
				<tr><?php
				while($rec_subject = dbArray($res_subject)){
					?>
					<th style="text-align:center;"><?php echo $rec_subject['short_name']; ?><br/><span style="font-size: 9px;"><?php echo $rec_subject['user_name']; ?></span></th><?php
				}?>
				</tr><?php	
			$count = 0;
			$count=  dbRows($res_subject);
			$arr_data = array();	
			$i = 0;		
			$subjcet_total_pass="";
			$subjcet_total_fail="";	
			$subjcet_total_abcent="";				
			while($rec_student = dbArray($res_student))
			{
				$student_id = $rec_student['student_id'];
				$student_gturegno = $rec_student['student_gturegno'];
				$student_name = $rec_student['student_name'];
				$total_mark = 0; 
				$result = "Pass"; 
				
				$arr_data[$i]['student_gturegno'] = $student_gturegno;
				$arr_data[$i]['student_name'] = $student_name;
				mysql_data_seek($res_subject,0);
				$studnetfail_cnt=0;
				
				while($rec_subject = dbArray($res_subject))
				{
					$subject_id = $rec_subject['subject_id'];
					$subject_pass=0;
					$subject_fail=0;
					$subject_abcent=0;
					$RES_MARK = getMark($student_id,$subject_id,$exam_id,$test_type,$semester_id,$branch_id);
					$REC_MARK = dbArray($RES_MARK);
					if(dbRows($RES_MARK) > 0){
						$total_mark += $REC_MARK['obtain_mark']; 
						if($result != "Fail"){
							$result = $REC_MARK['result'];
						}
						if($REC_MARK['present'] == 1){
							if($REC_MARK['result']=='Fail'){
								$arr_data[$i][$subject_id] ="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>".$REC_MARK['obtain_mark']."</span>";
								$studnetfail_cnt++;	
								$subject_fail++;
								$subjcet_total_fail[$subject_id]= $subjcet_total_fail[$subject_id] + $subject_fail ;
							}
							else{
								if($show_pass_marks){
									$arr_data[$i][$subject_id] =$REC_MARK['obtain_mark'];	
								}else{
									$arr_data[$i][$subject_id] ='--';
									}
									$subject_pass++;
									$subjcet_total_pass[$subject_id]= $subjcet_total_pass[$subject_id] + $subject_pass ;
							}
						}
						else{
							$arr_data[$i][$subject_id] = "<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>A</span>";
							$studnetfail_cnt++;	
							$subject_abcent++;
							$subjcet_total_abcent[$subject_id]= $subjcet_total_abcent[$subject_id] + $subject_abcent ;
						}
					}
					else{
						$arr_data[$i][$subject_id] = '';
					}
				}
				$arr_data[$i]['studnetfail_cnt'] = $studnetfail_cnt;
				$arr_data[$i]['total_mark'] = $total_mark;
				$arr_data[$i]['result'] = $result;
				$i++;
			}
			$c = 1;
			if($_REQUEST['short_type']=='Max Mark')
			{
				foreach($arr_data as $temp_list)
				{
				   $sort_mark[] = ($temp_list['total_mark']+(rand(1,9)/10));
				}
				array_multisort($sort_mark,SORT_DESC, $arr_data);
			}elseif($_REQUEST['short_type']=='Pass-Fail')
			{
				foreach($arr_data as $temp_list)
				{
				   $sort_result[] = ($temp_list['result']);
				}
				array_multisort($sort_result,SORT_DESC, $arr_data);
			}elseif($_REQUEST['short_type']=='Both')
			{
				foreach($arr_data as $temp_list)
				{
				   $sort_mark[] = ($temp_list['total_mark']+(rand(1,9)/10));
				   $sort_result[] = ($temp_list['result']);
				}
				array_multisort($sort_result,SORT_DESC,$sort_mark,SORT_DESC, $arr_data);
			}
			if($result_disp == "Both(Pass-Fail)"){
				for($i=0; $i<count($arr_data); $i++){
					$color='color:#000000';
					$color=($arr_data[$i]['result']=='Fail'?'color:#FF0000;font-weight: bold;text-decoration: underline;':'#000000');
				?>
					<tr>
                        <td>&nbsp;<?php echo $c++; ?></td>
                        <td>&nbsp;<?php echo $arr_data[$i]['student_gturegno']; ?></td>
                        <td>&nbsp;<?php echo $arr_data[$i]['student_name']; ?></td><?php    
                        mysql_data_seek($res_subject,0);
						while($rec_subject = dbArray($res_subject))
                        {
                            $subject_id = $rec_subject['subject_id']; ?>
                            <td style="text-align:center;" width="5%">&nbsp;<?php echo $arr_data[$i][$subject_id]; ?></td><?php	
                        } ?>
                         <?php if($show_pass_marks){ ?>
                        <td style="text-align:center;">&nbsp;<?php echo $arr_data[$i]['total_mark']; ?></td>
                        <?php } ?>
                         <?php if($show_retest_fees==1 && $gtu_numros>0 ){ 
 						 $studnetfail_cnt= $arr_data[$i]['studnetfail_cnt'];
						 $gtuhead_amount = $rec_gtu['gtuhead_amount']*$studnetfail_cnt;
						 $max_amount = $rec_gtu['max_amount'];
						 if($max_amount > $gtuhead_amount ){
							 $amount=$gtuhead_amount;
						 }else{
							$amount= $max_amount;
						 }
						 
						 ?>
                    		<td style="text-align:center;"><?php echo $amount; ?></td>
                  		  <?php } ?>
                        <td style="text-align:center;"> <span style="<?php echo $color; ?>" ><?php echo $arr_data[$i]['result']; ?></span></td>
                    </tr><?php				
				}
			}
			else{
				for($i=0; $i<count($arr_data); $i++){
					$color='#000000';
                    $color=($arr_data[$i]['result']=='Fail'?'color:#FF0000;font-weight: bold;text-decoration: underline;':'#000000');
					if($arr_data[$i]['result'] == $result_disp){?>
						<tr>
							<td>&nbsp;<?php echo $c++; ?></td>
							<td>&nbsp;<?php echo $arr_data[$i]['student_gturegno']; ?></td>
							<td>&nbsp;<?php echo $arr_data[$i]['student_name']; ?></td><?php    
							mysql_data_seek($res_subject,0);
							while($rec_subject = dbArray($res_subject))
							{
								$subject_id = $rec_subject['subject_id']; ?>
								<td style="text-align:center;" width="5%" style="color:<?php echo $color; ?>">&nbsp;<?php echo $arr_data[$i][$subject_id]; ?></td> <?php	
							}?>
                             <?php if($show_pass_marks){ ?>
							<td style="text-align:center;">&nbsp;<?php echo $arr_data[$i]['total_mark']; ?></td>
                            <?php } ?>
                             <?php if($show_retest_fees==1 && $gtu_numros>0 ){ 
							 $studnetfail_cnt= $arr_data[$i]['studnetfail_cnt'];
							 $gtuhead_amount = $rec_gtu['gtuhead_amount']*$studnetfail_cnt;
							 $max_amount = $rec_gtu['max_amount'];
							 if($max_amount > $gtuhead_amount ){
							 $amount=$gtuhead_amount;
						 }else{
							$amount= $max_amount;
						 }
							 ?>
			                    <td style="text-align:center;"><?php echo $amount; ?></td>
            		        <?php } ?>
							<td style="text-align:center;"><span style="<?php echo $color; ?>"><?php echo $arr_data[$i]['result']; ?></span></td>
						</tr><?php
					}
				}
			}?>	
            <tr>
							<th colspan="3" style="text-align:right;height:25px;">Total Absent</th>
							<?php  mysql_data_seek($res_subject,0);
							while($rec_subject = dbArray($res_subject))
							{
								$subject_id = $rec_subject['subject_id']; ?>
								<th style="text-align:center;" width="5%">&nbsp;<?php echo $subjcet_total_abcent[$subject_id]==""?'--':$subjcet_total_abcent[$subject_id]; ?></span></th><?php	
							} ?>
                           <th>&nbsp;</th>
                             <?php if($show_pass_marks){ ?>
							  <th>&nbsp;</th>
                            <?php } ?>
                             <?php if($show_retest_fees==1 && $gtu_numros>0 ){ ?>
                            <th>&nbsp;</th>
                            <?php } ?>
            </tr>
             <tr>
							<th colspan="3" style="text-align:right;height:25px;">Total Fail</th>
							<?php  mysql_data_seek($res_subject,0);
							while($rec_subject = dbArray($res_subject))
							{
								$subject_id = $rec_subject['subject_id']; ?>
								<th style="text-align:center;" width="5%">&nbsp;<?php echo $subjcet_total_fail[$subject_id]==""?'--':$subjcet_total_fail[$subject_id]; ?></span></th><?php	
							} ?>
                           <th>&nbsp;</th>
                             <?php if($show_pass_marks){ ?>
							  <th>&nbsp;</th>
                            <?php } ?>
                             <?php if($show_retest_fees==1 && $gtu_numros>0 ){ ?>
                            <th>&nbsp;</th>
                            <?php } ?>
            </tr>
            
             <tr>
							<th colspan="3" style="text-align:right;height:25px;">Total Pass</th>
							<?php  mysql_data_seek($res_subject,0);
							while($rec_subject = dbArray($res_subject))
							{
								$subject_id = $rec_subject['subject_id']; ?>
								<th style="text-align:center;" width="5%">&nbsp;<?php echo $subjcet_total_pass[$subject_id]==""?'--':$subjcet_total_pass[$subject_id]; ?></th><?php	
							} ?>
                           <th>&nbsp;</th>
                             <?php if($show_pass_marks){ ?>
							  <th>&nbsp;</th>
                            <?php } ?>
                             <?php if($show_retest_fees==1 && $gtu_numros>0 ){ ?>
                            <th>&nbsp;</th>
                            <?php } ?>
            </tr>
            
            <tr>
							<th colspan="3" style="text-align:right;height:25px;">Subject Avg</th>
							<?php  mysql_data_seek($res_subject,0);
							while($rec_subject = dbArray($res_subject))
							{
								$subject_id = $rec_subject['subject_id']; ?>
								<th style="text-align:center;" width="5%">&nbsp;
								<?php $total_stu= $subjcet_total_pass[$subject_id]+$subjcet_total_fail[$subject_id];//+$subjcet_total_abcent[$subject_id];
									  $pass_stud=$subjcet_total_pass[$subject_id];
									echo round(($pass_stud*100)/$total_stu);								
									
								 ?>%</th><?php	
							} ?>
                           <th>&nbsp;</th>
                             <?php if($show_pass_marks){ ?>
							  <th>&nbsp;</th>
                            <?php } ?>
                             <?php if($show_retest_fees==1 && $gtu_numros>0 ){ ?>
                            <th>&nbsp;</th>
                            <?php } ?>
            </tr>
       <!--       <tr>
              <?php if($subject_id != 'undefined' OR $count != 0){
				  $cols= 6 + $count?>
                    <td colspan="<?php echo $cols;?>" >
               <?php }else{?>
               			<td colspan="6" >
                 <?php }?>
                    <table class="table-report-footer" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td align="left" style="border:solid 0px red;" width="69%">Created by <b><?php echo $_SESSION["VedantnrUserName"]; ?></b> </td>
                            <td align="right" style="border:solid 0px red;" width="31%">Developed By <b>Aum Imagineering Pvt.Ltd.</b></td>
                        </tr>
                    </table>
                    </td>        
					</tr> -->
            </table><?php				
			
			$HtmlCode= ob_get_contents();
			ob_end_flush();
			$HtmlUrl  =	"exampass-report.html";
			$HtmlPath =	"report/".$HtmlUrl;
			createHTMLReportPage($header_row_string,$HtmlUrl,$HtmlPath,$HtmlCode);
		?>
		<br/>
		<form name="frm" id="frm" action="" method="post">		
			<input type="hidden"   name="txtFile" id="txtFile" value="<?php echo $HtmlUrl ;?>" />
            <input type="button"  name="btnPrint" id="btnPrint" value="Print" onClick="SetPageAction('PRINT');"/>
			<input type="button"  name="btnPdf" id="btnPdf" value="Create PDF" onClick="SetPageAction('PDF');"/>
			<input type="button" name="btnHtml" id="btnHtml" value="Create HTML" onClick="SetPageAction('HTML');"/>
			<input type="button" name="btnWord" id="btnWord" value="Create WORD" onClick="SetPageAction('WORD');"/>
			<input type="button" name="btnExcel" id="btnExcel" value="Create EXCEL" onClick="SetPageAction('Excel');"/>
		</form>
		<script language="javascript" type="text/javascript">
            function SetPageAction(varAction) {
                if(varAction=="PRINT"){			
                    document.frm.action="print/create-print.php";
                    document.frm.target = "_blank";
                    document.frm.submit();
                }
                else if(varAction=="PDF"){			
                    document.frm.action="convertpdf/create-pdf.php";
                    document.frm.target = "_blank";
                    document.frm.submit();
                }
                else if(varAction=="HTML"){
                    document.frm.action="<?php echo $HtmlPath; ?>";
                    document.frm.target = "_blank";
                    document.frm.submit();
                }
                else if(varAction=="WORD"){
                    document.frm.action="convertword/create-word.php";
                    document.frm.target="_blank";
                    document.frm.submit();
                }
                else if(varAction=="Excel"){
                    document.frm.action="convertword/create-excel.php";
                    document.frm.target="_blank";
                    document.frm.submit();
                }
            }
        </script><?php
		}
		else{
			echo "No data found for selected criteria.";	
		}		
	}  
   	else{
      	echo "Session Expired";      
   	} 
?>
</div>
<?php require "_footer.php";?>
