<?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: 9px;
		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: 0px 0px 0px 0px;
		border-top: 0px none;
		border-left: 0px none;
	}

	.attendance-table-report th {
		padding: 1px 1px 1px 1px;
		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['division_id'] == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-attendence-summary.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']);
		$batch_id = Escape($_REQUEST['batch_idhidden']);
		$start_date = Escape($_REQUEST['startdate']);
		$end_date = Escape($_REQUEST['enddate']);
		$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']);

		$month 	  		= date('m',strtotime($start_date));
	$year 	  		 = date('Y',strtotime($start_date));
		$monthname1  	= date("F", mktime(0, 0, 0, $month, 1, 1));
		/*$qry = "
		SELECT 
			sm.student_id,
			CONCAT(student_surname,' ',student_firstname) AS student_name,
			student_gturegno,
			student_gender,
			batch_id
		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"];*/
		$condition="";
		if ($branch_id != "") {
			$condition .= " AND sd.cur_branchid = " . $branch_id;
		}
		if ($year_id != "") {
			$condition .= " AND sd.year_id = '" . $year_id . "'";
		}
		if ($branchyear_id != "") {
			$condition .= " AND sd.cur_branchyearid = '" . $branchyear_id . "'";
		}
		if ($semester_id != "") {
			$condition .= " AND sd.cur_semesterid = " . $semester_id;
		}
		if ($batch_id != '' && $batch_id != '0') {
			$condition .= " AND sd.batch_id = " . $batch_id;
		}

		$condition .= " ORDER BY student_gturegno ASC ";

	
		$objStudentMgmt=new StudentMgmt();
		$RES_STUDENT=$objStudentMgmt->getstudentattnd($condition);
		?>
		Managed by: Shree Shivshakti Eduction and Charitable Trust<br>
		<div class="college_name"><?php echo $_SESSION["VedantnrCollegeName"]; ?></div>
			(ANM / GNM / B.Sc. Nursing / P.B.B.Sc.(N) / M.Sc.(N))<br>
		<div class="college_address"><?php echo $_SESSION["VedantnrCollegeAddress"]; ?></div>
		<div class="college_address">
			
			Email: <?php echo $_SESSION["VedantnrCollegeEmail"]; ?>
			&nbsp;&nbsp;
			Phone: <?php echo $_SESSION["VedantnrCollegePhone"]; ?>
		</div>

		<center>
			<h4>Subject Wise Attendance Summary Report</h4>
		</center>
		<br />
		<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
		$i = 1;
		$present = array();
		$present_total = 0;
		$total_summary = array();
		$day_str = Get_PresentDays($start_date, $end_date, $branch_id, $semester_id, $year_id, $branchyear_id, $subject_id, $lectype, $emp_aay);
		if ($day_str != "") {
			?>
			<table cellpadding="0" cellspacing="0" border="1" class="attendance-table-report" width="100%">
				<tr>
					<th colspan="3"><?php print "Month :: " . $monthname1 . "&nbsp;&nbsp; Year :: " . $year; ?></th>
					<td rowspan="2" valign="top" width="100%" style="height:106px;">
						<?php prepare_subject_row($day_str); ?>
					</td>
					<th width="10%" rowspan="2" class="90degree_th 90degree"><?php echo "Total Present"; ?></th>
					<th width="10%" rowspan="2"><?php echo "Total Conduct"; ?></th>
					<th width="10%" rowspan="2"><?php echo "Per.[%]"; ?></th>
					<th width="6%" rowspan="2">Notes</th>
				</tr>
				<tr>
					<th width="2%" style="text-align:center;">Sr. No.</th>
					<th width="4%" style="text-align:center;">Roll No</th>
					<th width="15%" style="text-align:left; padding:0px 2px 0px 2px;">Student Name</th>
				</tr>

				<?php
				$i = 1;
				while ($row = dbArray($RES_STUDENT)) {
					extract($row);
					?>
					<tr>
						<td style="text-align:center"><?php print $i++; ?></td>
						<td style="text-align:center; padding:0px 2px 0px 2px;">
							<?php print ($student_gturegno == "" ? '&nbsp;' : $student_gturegno); ?></td>
						<td style="text-align:left; padding:0px 2px 0px 2px;"><?php print $student_name; ?></td>
						<td width="100%" valign="top">
							<?php makeDataRow($student_id, $day_str, $present_total, $total_a_p); ?>
						</td>
						<td style="text-align:center"><?php print $present_total; ?></td>
						<td style="text-align:center">
							<?php print $total_a_p;
							$style = "";
							$min_persentage = $_REQUEST['min_persentage'];
							$avrage = round(($present_total * 100) / $total_a_p, 2);
							if ($avrage < $min_persentage) {
								$style = "color:#FF0000;font-weight:bold;text-decoration:underline;";
							}
							?>
						</td>
						<td style="text-align:center"><span style="<?php echo $style; ?>"><?php print $avrage . '%'; ?></span></td>
						<td>&nbsp;</td>
					</tr>
					<?php
				}

				?>
				<!-- 	<tr>
					<td colspan="8" >
					<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 = "attendancesheet-blank-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>
</div>
<?php

require "_footer.php";

function Get_PresentDays($start_date, $end_date, $branch, $semester, $year_id, $branchyear_id, $subject, $lectype, &$data_array)
{
	$qry = "";
	$data_array = [];
	$total_day = "";
	$batch_id = $_REQUEST['batch_idhidden'];
	$qry = "SELECT count(day_id) total_lec,subject_name,pre.subject_id,short_name,pre.login_id,user_name,group_concat(day_id)day_ids
				FROM 
					cms_presentmaster pre,
					cms_subjectmaster sub,
					cms_usermaster u
				WHERE 
					sub.subject_id=pre.subject_id AND
					u.user_id=pre.login_id AND
					pre.branch_id = '" . $branch . "' AND
					pre.semester_id = '" . $semester . "' AND
					pre.branchyear_id = '" . $branchyear_id . "' AND
					pre.college_id = '" . $_SESSION['VedantnrCollegeId'] . "' AND
					pre.year_id = '" . $year_id . "'  AND
					pre.day_date >='" . $start_date . "' AND
					pre.day_date <='" . $end_date . "' ";

	if ($subject != 'undefined' && $subject != '') {
		$qry .= " AND pre.subject_id = '" . $subject . "' ";
	}

	if ($batch_id != '' && $batch_id != '0') {
		$qry .= " AND pre.batch_id In (0," . $batch_id . ")";
	}

	if ($lectype != '' && $lectype != '0' && $lectype != 'undefined') {
		$qry .= " AND pre.lectype = '" . $lectype . "'";
	}
	$qry .= " group by login_id,subject_id order by pre.subject_id ASC  ";
	//echo $qry;
	$out = dbQuery($qry);
	$k = 0;
	if (dbRows($out) > 0) {
		while ($row = dbArray($out)) {

			$data_array[$k]['subject_name'] = $row['subject_name'];
			$data_array[$k]['subject_id'] = $row['subject_id'];
			$data_array[$k]['short_name'] = $row['short_name'];
			$data_array[$k]['login_id'] = $row['login_id'];
			$data_array[$k]['user_name'] = $row['user_name'];
			$data_array[$k]['total_lec'] = $row['total_lec'];
			$data_array[$k]['day_ids'] = $row['day_ids'];
			$k++;
		}
		return $data_array;
	} else {
		return $data_array;
	}
}

function prepare_subject_row($day_str)
{
	$table_str = "";
	$table_str = '<table class="attendance-table-report" width="100%" cellspacing="0" cellpadding="0" border="1" style="height:106px;">';
	$table_str .= "<tr>";
	$count_colspan = 1;
	$i = 0;
	for ($i = 0; $i < count($day_str); $i++) {
		$subject_id = $day_str[$i]['short_name'];
		$next_subject = $day_str[$i + 1]['short_name'];
		if ($subject_id != $next_subject) {
			$table_str .= "<th colspan=" . ($count_colspan + 2) . " style='text-align: center;' >" . $day_str[$i]['short_name'] . '</th>';
			$count_colspan = 1;
		} else {
			$count_colspan++;
		}
	}
	$table_str .= "</tr><tr>";

	for ($i = 0; $i < count($day_str); $i++) {
		$table_str .= "<td style='text-align: center;min-width:50px;'>" . $day_str[$i]['user_name'] . '</td>';
		$subject_id = $day_str[$i]['short_name'];
		$next_subject = $day_str[$i + 1]['short_name'];
		if ($subject_id != $next_subject) {
			$table_str .= "<th style='text-align: center;min-width: 50px;'>Total</th>";
			$table_str .= "<th rowspan='2' style='text-align: center;min-width: 50px;'>AVG[%]</th>";
		}
	}
	$table_str .= "</tr><tr>";
	$total_lec = 0;
	for ($i = 0; $i < count($day_str); $i++) {
		$table_str .= "<td style='text-align: center;min-width: 30px;'>" . $day_str[$i]['total_lec'] . '</td>';
		$subject_id = $day_str[$i]['short_name'];
		$next_subject = $day_str[$i + 1]['short_name'];
		$total_lec += $day_str[$i]['total_lec'];
		if ($subject_id != $next_subject) {
			$table_str .= "<th style='text-align: center;min-width: 50px;'>" . $total_lec . "</th>";
			$total_lec = 0;
		}
	}
	$table_str .= "</tr>";

	$table_str .= "</table>";
	echo $table_str;
}


function makeDataRow($student_id, $day_str, &$present_total, &$total_a_p)
{
	$table_str = "";
	$present_total = 0;
	$total_a_p = 0;
	$min_persentage = $_REQUEST['min_persentage'];
	$table_str = '<table class="attendance-table-report" width="100%" cellspacing="0" cellpadding="0" border="1" style="height:30px;">';
	$table_str .= "<tr>";
	for ($i = 0; $i < count($day_str); $i++) {
		$student_count = 0;
		dbQuery("SET SESSION group_concat_max_len = 1000000");
		$student_sql = "select group_concat(`day_present`)as day_present from cms_presentmaster where day_id in(" . $day_str[$i]['day_ids'] . ") ";
		$student_out = dbQuery($student_sql);
		$k = 0;
		if (dbRows($student_out) > 0) {
			$student_row = dbArray($student_out);
			$student_count = substr_count($student_row['day_present'] . ',', $student_id . ',');
			$present_total += $student_count;
		}
		$total_student_lec += $student_count;
		$table_str .= "<td style='text-align: center;min-width: 50px;'>" . $student_count . '</td>';
		$subject_id = $day_str[$i]['short_name'];
		$next_subject = $day_str[$i + 1]['short_name'];
		$total_lec += $day_str[$i]['total_lec'];
		$total_a_p += $day_str[$i]['total_lec'];
		if ($subject_id != $next_subject) {
			$table_str .= "<td style='text-align: center;min-width: 50px;'>" . $total_student_lec . "</td>";
			$avg = round(($total_student_lec * 100) / $total_lec, 2);
			$style = "";
			if ($avg < $min_persentage) {
				$style = "color:#FF0000;font-weight:bold;text-decoration:underline;";
			}
			$table_str .= "<td style='text-align: center;min-width: 50px;'><span style=" . $style . ">" . round(($total_student_lec * 100) / $total_lec, 2) . "%</span></td>";
			$total_lec = 0;
			$total_student_lec = 0;
		}
	}

	$table_str .= "</tr>";
	$table_str .= "</table>";
	echo $table_str;

}

?>