<?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 
/*echo "<pre>";
print_r($_REQUEST);
exit;*/
	//$header_row_string="Exam Marks Report";
	
	$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_disp 		   = Escape($_REQUEST['exam_disp']);
	$subject_name 		= Escape($_REQUEST['subject_disp']);
	$show_obtain_mark 	= ($_REQUEST['show_obtain_mark']=='true'?1:0);
	
	if($branch_id == "" && $semester_id == "" && $exam_id == ""){?>
		<script language="javascript" type="text/javascript">
			location.href='report-exampass-all.php';
		</script><?php
	}
	elseif($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])){
		ob_start();
		$i=1;
		
		/* START SUBJECT DATA */
		
		 $sql_subject = "
			SELECT 
				distinct(res.subject_id),
				sub.subject_name,
				sub.short_name,
				sub.subject_code,
				es.year_id,
				es.total_marks,
				es.passing_marks,
				es.total_marks_pr_ese,
				es.passing_marks_pr_ese,
				es.total_marks_pr_pa,
				es.passing_marks_pr_pa,   
				res.login_id
			FROM 
				cms_examsetup es,
				cms_subjectmaster sub,
				cms_resultmaster res
			
			WHERE 
				es.subject_id=sub.subject_id AND 
				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 
				es.branch_id = '".$branch_id."' AND 
				es.semester_id = '".$semester_id."' AND 
				es.exam_id='".$exam_id."' AND 
				es.year_id='".$_SESSION['aYearId']."' AND
				es.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 ";
		$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 
				sd.year_id = ".$_SESSION["VedantnrYearId"];
		
		$where = "";
		/*
		sd.current_active = 1 AND
		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  ";
		}
		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 */

		
		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>
            <br />
            <table width="100%">
                <tr>
                <td style="text-align: left;border:none;  font-size: 11px;"><b>Branch:-</b> <?php echo $branch_disp." - ".$semester_disp." - ".$division_id; ?></td>
                <td style="text-align: right;border:none;  font-size: 11px;"><b>Sem:-</b><?php echo $exam_disp; ?></td>
                </tr>
                </table>
      
            <table width="100%" class="table-report" border="1" cellpadding="0" cellspacing="0">
            <tr>
            <th style="text-align:center;" width="2%" rowspan="4">Sr No</th>
            <th style="text-align:center;" width="8%" rowspan="4">GTU Reg No</th>
            <th style="text-align:center;" width="20%" rowspan="4">Student Name</th>
			<?php    while($rec_code = dbArray($res_subject)){
			 		 $colspan=0;
					 $pr_colspan=0;
  					 if($rec_code['total_marks']>0){$colspan=2;}
					 if($rec_code['total_marks_pr_ese']>0){	$colspan++;$pr_colspan++;	}
					 if($rec_code['total_marks_pr_pa']>0){ $colspan++;$pr_colspan++;	}
					   echo $first_row='<th style="text-align:center;" colspan="'.$colspan.'" >'.$rec_code['short_name'].'</th>';
					   $second_row.='<th style="text-align:center;" colspan="'.$colspan.'" >'.$rec_code['subject_code'].'</th>';
					 
					  if($rec_code['total_marks']>0){
					   $third_row.='<th style="text-align:center;" colspan="2" >Th.</th>';
					   $four_row.='<th style="text-align:center;">FI.<br/>'.$rec_code['total_marks'].'</th><th style="text-align:center;">RE.<br/>'.$rec_code['total_marks'].'</th>';
					   }
					   if($pr_colspan>0){
						$third_row.='<th style="text-align:center;" colspan="'.$pr_colspan.'" >Int.</th>';
					   }
					   if($rec_code['total_marks_pr_ese']>0){
						$four_row.='<th style="text-align:center;">ESE.<br/>'.$rec_code['total_marks_pr_ese'].'</th>';
					   }
					  if($rec_code['total_marks_pr_pa']>0){
							$four_row.='<th style="text-align:center;">PA.<br/>'.$rec_code['total_marks_pr_pa'].'</th>';
					   }
           } ?>
            </tr>
            
    		<?php echo '<tr>'.$second_row.'</tr>';
			  echo '<tr>'.$third_row.'</tr>';
			  echo '<tr>'.$four_row.'</tr>'; ?>
            
              <?php 
				$srno = 1;		
				while($rec_student = dbArray($res_student)){
					$student_id = $rec_student['student_id'];
					$student_gturegno = $rec_student['student_gturegno'];
					$student_name = $rec_student['student_name'];?>
                    <tr>
                    <td style="text-align:center;"><?php echo $srno; ?></td>
                    <td style="text-align:left;"><?php echo $student_gturegno; ?></td>
                    <td style="text-align:left;"><?php echo $student_name; ?></td>
           		 <?php
					mysql_data_seek($res_subject,0);
					while($rec_subject = dbArray($res_subject)){
						$subject_id = $rec_subject['subject_id'];
						$RES_MARK = getMark($student_id,$subject_id,$exam_id,$semester_id,$branch_id);
						$REC_MARK = dbArray($RES_MARK);
						$obtain_mark=$REC_MARK['obtain_mark'];
						$obtain_mark_re=$REC_MARK['obtain_mark_re'];
						$obtain_marks_pr_ese=$REC_MARK['obtain_marks_pr_ese'];
						$obtain_marks_pr_pa=$REC_MARK['obtain_marks_pr_pa'];
						
						if($show_obtain_mark==0 && $obtain_mark_re > $rec_subject['passing_marks']){
							$obtain_mark_re=$rec_subject['passing_marks'];
						
						}else{
							$obtain_mark_re=$REC_MARK['obtain_mark_re'];
						}
						
						
						if($REC_MARK['present_fi']==1){
							if($obtain_mark < $rec_subject['passing_marks']){
							 $obtain_mark="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>".$obtain_mark."</span>";						
							}
						}else{
							$obtain_mark="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>A</span>";						
						}
						
						
						if($REC_MARK['present_re']==1){
							if($obtain_mark_re < $rec_subject['passing_marks']){
							 $obtain_mark_re="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>".$obtain_mark_re."</span>";						
							}
						}else{
							
							if($REC_MARK['present_fi']==0 || $REC_MARK['obtain_mark'] < $rec_subject['passing_marks']){
								$obtain_mark_re="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>A</span>";						
							}else{
							$obtain_mark_re='-';
							}
							
						}
						
						
						if($REC_MARK['present_pr']==1){
							if($obtain_marks_pr_ese < $rec_subject['passing_marks_pr_ese']){
								$obtain_marks_pr_ese="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>".$obtain_marks_pr_ese."</span>";						
							}
						}else{
							$obtain_marks_pr_ese="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>A</span>";						
						}
						
						if($REC_MARK['present_pr']==1){
							if($obtain_marks_pr_pa < $rec_subject['passing_marks_pr_pa']){
								$obtain_marks_pr_pa="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>".$obtain_marks_pr_pa."</span>";						
							}
						}else{
							$obtain_marks_pr_pa="<span style='color: #FF0000;font-weight: bold;text-decoration: underline;'>A</span>";						
						}
						
						  if($rec_subject['total_marks']>0){
						?>
						<td style="text-align:center;"><?php echo $obtain_mark; ?></td>
                        <td style="text-align:center;"><?php echo $obtain_mark_re; ?></td>
                         <?php } if($rec_subject['total_marks_pr_ese']>0){?>
						  <td style="text-align:center;"><?php echo $obtain_marks_pr_ese; ?></td>
                      	<?php  }
						if($rec_subject['total_marks_pr_pa']>0){?>
						  <td style="text-align:center;"><?php echo $obtain_marks_pr_pa; ?></td>
						<?php  }
                       
					}
					$srno++; ?>
                    </tr>	
				<?php }  ?>    
		<!--		 <tr>
				<?php
				
					if($_REQUEST['subject_hidden'] != 'undefined')
				 	{?>
				 		 <td colspan="7" >
				 <?php	}elseif($_REQUEST['show_obtain_mark'] == 'on' ){?><td colspan="30" >
				 <?php }else {?><td colspan="30" > <?php }?>
                   
                    <table class="table-report-footer" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td align="left" style="border:solid 0px red; padding:0px;" width="70%">Created by <b><?php echo $_SESSION["VedantnrUserName"]; ?></b> </td>
                            <td align="right" style="border:solid 0px red; padding:0px;" width="30%">Developed By <b>Aum Imagineering Pvt.Ltd.</b></td>
                        </tr>
                    </table>
                    </td>        
					</tr> -->                          
                  
            </table>
			
					
					<?php				
			$HtmlCode= ob_get_contents();
			ob_end_flush();
			$HtmlUrl  =	"exampass-all-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";

function getMark($student_id,$subject_id,$exam_id,$semester_id,$branch_id){
		//echo "<br/>".$student_id.'-'.$subject_id;
		$sql_mark = "SELECT 
					 obtain_mark,
					 present as present_fi,
					 present_pr,
					 obtain_marks_pr_ese,
					 obtain_marks_pr_pa,
					(select obtain_mark from cms_resultmaster resin  WHERE  resin.student_id = ".$student_id." AND resin.subject_id = ".$subject_id." AND resin.test_type='r' AND resin.year_id = ".$_SESSION['aYearId']." limit 0,1)as obtain_mark_re,
					(select present from cms_resultmaster resin  WHERE  resin.student_id = ".$student_id." AND resin.subject_id = ".$subject_id." AND resin.test_type='r' AND resin.year_id = ".$_SESSION['aYearId']." limit 0,1)as present_re
				FROM 
				cms_resultmaster res 
				WHERE
					res.college_id = ".$_SESSION['VedantnrCollegeId']." AND
					res.test_type='f'  AND
					res.year_id = ".$_SESSION['aYearId']." AND	
					res.branch_id = ".$branch_id." AND		
					res.semester_id = ".$semester_id." AND		
					res.exam_id = ".$exam_id." AND	
					res.subject_id = ".$subject_id." AND		
					res.student_id = ".$student_id."
					group by student_id";
				//echo $sql_mark; exit;	
			$res_mark = dbQuery($sql_mark);	
			return $res_mark;
}
?>
