<?php require "_header.php";
error_reporting(1);
?>
<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;
	}
	.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;
	}	
</style>
<div id="main-report">                                      
<?php
if($_REQUEST['branch_hidden'] == "" && $_REQUEST['semester_hidden'] == "" && $_REQUEST['subject_hidden'] == "")
{
?>
	<script language="javascript" type="text/javascript">
		location.href='report-attendence-list.php';
	</script>
<?php
}
elseif($_SESSION['VedantnrCollegeId']!="" && isset($_SESSION['VedantnrCollegeId'])) 
{ 
	ob_start();	
	$branch_id 		= Escape($_REQUEST['branch_hidden']);
	$semester_id	  = Escape($_REQUEST['semester_hidden']);
	$year_id		   = Escape($_REQUEST['year_hidden']);
	$branchyear_id		   = Escape($_REQUEST['branchyear_hidden']);
	$subject_id	   = Escape($_REQUEST['subject_hidden']);
	$lectype		  = Escape($_REQUEST['lectype']);
	$batch_id 		 = Escape($_REQUEST['batch_idhidden']);
	
	//$lectype_disp	 = Escape($_REQUEST['lectype_disp']);
	$report_type 	  = Escape($_REQUEST['report_type']);
	$month 	  		= Escape($_REQUEST['monthname']);

	$branch_name = Escape($_REQUEST['branch_disp']);
		$subject_name = Escape($_REQUEST['subject_disp']);
		$batch_name = Escape($_REQUEST['batch_disp']);
		$year_name = Escape($_REQUEST['year_disp']);
		$branchyear_name = Escape($_REQUEST['branchyear_disp']);
		$semester_name = Escape($_REQUEST['semester_disp']);
	

		$month1 	  		= date('m',strtotime($month));
	$monthname 	= date("F", mktime(0, 0, 0, $month1, 1, 1));
	//echo "month=".$month;
	//echo "name=".$monthname;
	$orderby 		= $_REQUEST['hid_orderby'];
	//print_r($_REQUEST);
	
	$condition  = "";
	/*$SQL_STUDENT = "
		SELECT 
			sm.student_id,
			CONCAT(student_surname,' ',student_firstname) AS student_name,
			student_gturegno,
			sd.batch_id,
			student_mobilestudents,
			student_mobileparents
		FROM
			cms_studentmaster sm,
			cms_studentdetail sd
		WHERE sm.student_active = 1  AND sm.student_id = sd.student_id 
				AND sd.college_id = ".$_SESSION["VedantnrCollegeId"];*/
		
	if($branch_id != ""){
		$condition .= " AND sd.cur_branchid = ".$branch_id;
	}
	if($semester_id != ""){
		$condition .= " AND sd.cur_semesterid = ".$semester_id;
	}
	if($year_id != ""){
		$condition .= " AND sd.year_id = '".$year_id."'";
	}
	if($branchyear_id != ""){
		$condition .= " AND sd.cur_branchyearid = '".$branchyear_id."'";
	}
	if($batch_id != '' && $batch_id != '0'){
		$condition .= " AND sd.batch_id = ".$batch_id;
	}
	$condition .= " ORDER BY student_surname ASC ";
	
	$objStudentMgmt=new StudentMgmt();
		$RES_STUDENT=$objStudentMgmt->getstudentattnd($condition);


	/*$SQL_PRESENT = "SELECT 
		GROUP_CONCAT(day_present) AS day_present 
		FROM cms_presentmaster 
		WHERE day_working = 1 ";
		
	$SQL_WDAYS = "SELECT 
		COUNT(*) AS count ,batch_id
		FROM cms_presentmaster 
		WHERE day_working = 1 ";*/
		
	$condition1  = "";
	$condition1 .= " AND branch_id = '".$branch_id."' ";
   
   if($semester_id != ""){
		
		$condition1 .= " AND semester_id = '".$semester_id."' ";
   }
	if($year_id != ""){
		$condition1 .= " AND year_id = '".$year_id."'";
	}
	if($branchyear_id != ""){
		$condition1 .= " AND branchyear_id = '".$branchyear_id."'";
	}
	if($subject_id != "undefined"){
		$condition1 .= " AND subject_id = '".$subject_id."' ";	
	}
	
	if($lectype != ""){
		$condition1 .= " AND lectype = '".$lectype."' ";	
	}

	if($month != ""){
		$condition1 .= " AND Extract(MONTH FROM day_date)= '".$month."' ";	
	}
	
	$RES_PRESENT=$objStudentMgmt->getstudentdaypresent($condition1);
	$REC_PRESENT = dbArray($RES_PRESENT);


	$condition2="";
	$condition2 .=$condition1;
	$condition2 .="group by batch_id";
	$RES_WDAYS=$objStudentMgmt->getstudentdayworking($condition2);

	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);
	//print_r( $REC_WDAYS1);
	//exit();
	?>
        <div class="college_name"><?php echo $_SESSION["VedantnrCollegeName"]; ?></div>
                    <div class="college_address"><?php echo $_SESSION["VedantnrCollegeAddress"]; ?></div>
                    <div class="college_address">
                                    Web Site: <?php echo $_SESSION["VedantnrCollegeUrl"] ;?> 
                                    &nbsp;&nbsp; 
                                    Email: <?php echo $_SESSION["VedantnrCollegeEmail"] ;?> 
                                    &nbsp;&nbsp; 
                                    Phone: <?php echo $_SESSION["VedantnrCollegePhone"] ;?> 
                              	</div>
    
    <center><h4>Student Attendance Detail <?php if($monthname != ""  && $month != ""){ echo " - ".$monthname;} ?></h4></center>
	<table width="100%">
                <tr>
				<div align="left">
					<b>Course</b> :
					<?php echo $branch_name; ?>
				</div>
				<div align="right">
					<b>Batch Year</b>:
					<?php echo $year_name; ?>
				</div>
				<div align="left">
					<?php if ($branchyear_id != "") { ?>
						<b>Course Name</b>:
						<?php echo $branchyear_name; ?>
					</div>
					<div align="left">
					<?php }
					if ($semester_id != "") { ?>
						<b>Semester</b>:
						<?php echo $semester_name;
					} ?>
				</div>
				<div align="right">

					<b>subject</b>:
					<?php echo $subject_name; ?>

				</div> </tr>
                </table>
      
	
    <?php
	
	if(dbRows($RES_STUDENT) > 0){?>
    	<table border="1" class="attendance-table-report" cellpadding="0" cellspacing="0" width="100%">
			<tr>
            	<th width="5%" style="text-align:center;">Sr.</th>
            	<th width="15%" style="text-align:center;">Enrollment No</th>
            	<th width="20%" style="text-align:left;">Student Name</th>
                <th width="15%" style="text-align:center;">Mobile (Student)</th>
                <th width="15%" style="text-align:center;">Mobile (Parents)</th>
                <th width="15%" style="text-align:center;">Present / Working Days</th>
                <th width="15%" style="text-align:center;">Average Present (%)</th>
            </tr><?php
		$arr = [];
		$i = 0; 
			
		while($REC_STUDENT = dbArray($RES_STUDENT))
		{

			$arr[$i]['student_gturegno'] = $REC_STUDENT['student_gturegno'];
			$arr[$i]['student_name'] = $REC_STUDENT['student_name'];
			$arr[$i]['student_mobilestudents'] = $REC_STUDENT['student_mobilestudents'];
			$arr[$i]['student_mobileparents'] = $REC_STUDENT['student_mobileparents'];
			$temp_days = ($day_present[$REC_STUDENT['student_id']] == '') ? '0' : $day_present[$REC_STUDENT['student_id']];
			$arr[$i]['wp_days'] = $temp_days." / ".($REC_WDAYS1[$REC_STUDENT['batch_id']]+$REC_WDAYS1[0]);
			$arr[$i]['present_percent'] = ($REC_WDAYS1[$REC_STUDENT['batch_id']] + $REC_WDAYS1[0]) != 0 ? round(($day_present[$REC_STUDENT['student_id']] * 100) / ($REC_WDAYS1[$REC_STUDENT['batch_id']] + $REC_WDAYS1[0])) : 0;
			$i++; 
		}
		
		/* START FINAL 1 */
		/*function orderBy($data, $field){
			$code = "return strnatcmp(\$b['$field'],\$a['$field']);";
			usort($data, create_function('$a,$b', $code));
			return $data;
		}
		$arr = orderBy($arr, 'present_percent');*/
		/* END FINAL 1 */
		
		/* START FINAL 2 */ 
		/*function sortArray($data, $field)
		{
			if(!is_array($field)) 
					$field = array($field);
			usort($data, function($a, $b) use($field){
				$retval = 0;
				foreach($field as $fieldname) {
					if($retval == 0) $retval = strnatcmp($a[$fieldname],$b[$fieldname]);
				}
				return $retval;
			});
			return $data;
		}*/
		
		//$arr = sortArray($arr, $orderby);
  		
		//$arr = sortArray($arr, array($orderby,'student_gturegno'));
		
		/* END FINAL 2 */ 
		
		for($i=0; $i<count($arr); $i++){?>
            <tr>
            	<td style="text-align:center;"><?php echo $i+1; ?>&nbsp;</td>
            	<td style="text-align:center;"><?php echo $arr[$i]['student_gturegno']; ?>&nbsp;</td>
            	<td style="text-align:left;">&nbsp<?php echo $arr[$i]['student_name']; ?>&nbsp;</td>
                <td style="text-align:center;"><?php echo $arr[$i]['student_mobilestudents']; ?>&nbsp;</td>
                <td style="text-align:center;"><?php echo $arr[$i]['student_mobileparents']; ?>&nbsp;</td>
                <td style="text-align:center;"><?php echo $arr[$i]['wp_days']; ?>&nbsp;</td>
                <td style="text-align:center;"><?php echo $arr[$i]['present_percent']."%"; ?>&nbsp;</td>
            </tr><?php
		}?>
	<!--	<tr>
		  <td colspan="7" >
                    <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();
	$header_row_string="";
	$HtmlUrl = "attendancelist-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";
?>
