<?php require "_header.php";?>
<script language="javascript" type="text/javascript">
document.getElementById("main-content").style.display = 'none';
</script>
<style type="text/css">
	.attendance-table-report{
		font-family:Verdana, Geneva, sans-serif;
		font-size:12px;
		text-align:left;
		border-right:0px none;
		border-bottom:0px none;
	}
	.attendance-table-report tr{
		padding:0px 0px 0px 0px;
	}
	.attendance-table-report td{
		padding:5px;
		border-top:0px none;
		border-left:0px none;
	}
	.attendance-table-report th{
		padding:5px;
		font-weight:bold;
		text-align:left;
		border-top:0px none;
		border-left:0px none;
	}	
</style>
<div id="main-report">           
<center><h4>Student Attendance Detail</h4></center>                           
<?php

if($_SESSION['aCollegeId']!="" && isset($_SESSION['aCollegeId'])) 
{ 
/*echo "<pre>";
print_r($_SESSION);
exit;*/
	ob_start();
	$student_id 	= $_SESSION['student_id'];
	$SQL_YEAR = "SELECT year_id  FROM cms_studentdetail sd	WHERE sd.year_id >= 7 and sd.student_id = ".$_SESSION["student_id"]." ";
	
	$RES_YEAR = dbQuery($SQL_YEAR);
	$temp_sem= "";
	while($REC_YEAR = dbArray($RES_YEAR))
	{
		//$REC_YEAR['year_id'];
	
		$SQL_STUDENT = "
		SELECT 
			sm.student_id,
			sd.year_id,
			sd.cur_branchid,
			sd.cur_semesterid,
			sem.semester_name,
			sd.cur_divison
		FROM
			cms_studentmaster sm,
			cms_studentdetail sd,
			cms_branchmaster bm,
			cms_semestermaster sem,
			cms_yearmaster ym
		WHERE 
			sm.student_active = 1 AND 
			sd.year_id = ym.year_id AND
			sd.year_id = ".$REC_YEAR['year_id']."  AND 
			sm.student_id = sd.student_id AND 
			bm.branch_id=sd.cur_branchid AND 
			sem.semester_id=sd.cur_semesterid AND 
			sm.student_id = ".$_SESSION["student_id"]." 
			order by sd.year_id desc limit 0,1";
		
	/*echo $SQL_STUDENT;
	echo "\n";
	//exit;*/
	
	$RES_STUDENT = dbQuery($SQL_STUDENT);
	?>
	
    
	<?php 
	if(dbRows($RES_STUDENT) > 0){
		
		dbquery("SET group_concat_max_len = 102400");
		$temp_sem = "";
	while($REC_STUDENT = dbArray($RES_STUDENT))
	{?>
        <br />
      	<table border="1" class="attendance-table-report" cellpadding="0" cellspacing="0" width="80%">
			<tr>
            	<th width="5%" style="text-align:center;">SrNo.</th>
            	<th width="25%" style="text-align:center;">Subject</th>
                <th width="15%" style="text-align:center;">Present / Working Days</th>
                <th width="15%" style="text-align:center;">Average Present (%)</th>
            </tr>
            <?php
			$subquery = "SELECT sm.*
						 FROM cms_subjectmaster sm
						 WHERE 
							sm.subject_active = 1 AND 
						 sm.semester_id =".$REC_STUDENT['cur_semesterid']." AND sm.branch_id = '".$REC_STUDENT['cur_branchid']."' order by sm.subject_name";
			$subresult = dbQuery($subquery);
			if(dbRows($subresult) > 0)
			{
				if(($REC_STUDENT['semester_name'] != $temp_sem )){ 
						if($temp_sem != ""){ 
								?>
									 <tr>
                                        <th style="text-align:center;" colspan="2">Total</th>
                                        <th style="text-align:center;"><?php echo $total_present." / ".$total_all; ?></th>
                                        <th style="text-align:center;"><?php echo round(($total_present*100)/$total_all); ?>%</th>
                    				</tr> 
                                 <?php 
								
								}
								$total_present=0;
								$total_all=0;
								$temp_sem = $REC_STUDENT['semester_name']; 
								$srno=1;
							?> 
						 <tr>
        					<th style="text-align:left;" colspan="4">&nbsp;<?php echo strtoupper($temp_sem); ?></th>
       					<tr/>
                        <?php }
				while($subrec = dbArray($subresult))
					{
						
						dbquery("SET group_concat_max_len = 1024000");				
						$SQL_CONDITION  = "";
						$SQL_PRESENT = "SELECT GROUP_CONCAT(day_present) AS day_present ,count(*) as total_lec 
										FROM cms_presentmaster 
										WHERE day_working = 1 AND year_id = ".$REC_YEAR['year_id']."";
						$SQL_WDAYS = "SELECT COUNT(*) AS count ,batch_id FROM cms_presentmaster WHERE day_working = 1 AND year_id = ".$REC_YEAR['year_id']."";
						$SQL_CONDITION .= " AND branch_id = '".$REC_STUDENT['cur_branchid']."' 
											and semester_id = ".$REC_STUDENT['cur_semesterid']."
											AND div_id = '".$REC_STUDENT['cur_divison']."'  
											AND subject_id = '".$subrec['subject_id']."' ";
											
						/*echo $SQL_PRESENT.$SQL_CONDITION;
						echo "<br/>";
						echo $SQL_WDAYS.$SQL_CONDITION;*/
							//exit;*/
						$RES_PRESENT = dbQuery($SQL_PRESENT.$SQL_CONDITION);
						$REC_PRESENT = dbArray($RES_PRESENT);
						$RES_WDAYS = dbQuery($SQL_WDAYS.$SQL_CONDITION);
						if(dbRows($RES_WDAYS ) > 0)
						{
							while($record = dbArray($RES_WDAYS))
							{
								$REC_WDAYS1[$record['batch_id']]=$record['count'];	
							}
						}
						$day_present = explode(",",$REC_PRESENT['day_present']);
						$day_present = array_count_values($day_present);
						
						$total_lec=$REC_PRESENT['total_lec'];
						if($total_lec>0){
							?>
                        <tr>
                        <td style="text-align:center;"><?php echo $srno; ?></td>
                        <td><?php echo $subrec['subject_name'] ?></td>
                        <td style="text-align:center;">
                        <?php 
							$temp_days = ($day_present[$REC_STUDENT['student_id']] == '') ? '0' : $day_present[$REC_STUDENT['student_id']];
							//echo $temp_days;
							
							$total_present+=$temp_days;
							$total_all+=$total_lec;
							$wp_days = $temp_days." / ".$total_lec;
							echo $wp_days;
						?>
                        </td>
                        <td style="text-align:center;">
                        <?php
						$present_percent = round(($day_present[$REC_STUDENT['student_id']]*100)/($total_lec));
						echo $present_percent."%";
						?>
                        </td>
                        </tr>
                        <?php
						$srno++;
						}
				}
					?>
                    <tr>
                        <th style="text-align:center;" colspan="2">Total</th>
                        <th style="text-align:center;"><?php echo $total_present." / ".$total_all; ?></th>
                        <th style="text-align:center;"><?php echo round(($total_present*100)/$total_all); ?>%</th>
                     </tr>   
                    </table>
                    <?php
			}
		
	}
	}
	}
	
	
	$HtmlCode = ob_get_contents();
	ob_end_flush();
	
	$HtmlUrl = "studentattendancelist-report.html";
	$HtmlPath =	"report/".$HtmlUrl;
	createHTMLReportPage($header_row_string,$HtmlUrl,$HtmlPath,$HtmlCode,false);
?>
<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 } ?>
</div>
<?php 
	require "_footer.php";
?>