<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	require_once('source/restrict_user.php');  
	
	
	if($_GET['msg'] == "refered"){
		$msg = "<h5 class='alert alert-success'>Refered successfully</h5>";
	}else if($_GET['msg'] == "failed"){
		$msg = "<h5 class='alert alert-warning'>Something went wrong, please try again</h5>";
	}
	
	if($_SESSION['msg'] == "update")
	{
		$msg = "<h5 class='alert alert-success text-center'>Order Update Succesful. Customer will receive email.<a 
		class='btn-sm' href='new_order.php'>Refresh</a></h5>";
	}       
	          
	      
                    $rows1 = sql($DBH, "SELECT * FROM tbl_student_results where csv_no = ?", 
            	array($_GET['val']), "rows");
		foreach($rows1 as $row1)
		{	
			$u_name	 		= $row1['name'];
			$u_registration_no 		= $row1['registration_no'];
			$u_test1 			= $row1['test1'];
			$u_test2 			= $row1['test2'];
			$u_test3 			= $row1['test3'];
			$u_exam			= $row1['exam'];
			
			$total = ($u_test1 + $u_test2 + $u_test3 + $u_exam);


			sql($DBH, "UPDATE tbl_student_results set total = ?, teacher_no = ? WHERE registration_no =? AND csv_no = ?", array($total,$_GET['hsh'],$u_registration_no,$_GET['val']));
			
			
                    $rows2 = sql($DBH, "SELECT * FROM tbl_student_results where csv_no = ? ORDER by (total) desc", 
            	array($_GET['val']), "rows");
            	$sno = 0;
		foreach($rows2 as $row2)
		{	
			$sno++;
			$us_name	 		= $row2['name'];
			$us_registration_no 		= $row2['registration_no'];
			$us_test1 			= $row2['test1'];
			$us_test2 			= $row2['test2'];
			$us_test3 			= $row2['test3'];
			$us_exam			= $row2['exam'];
			$us_total			= $row2['total'];
			$us_class			= $row2['class'];
			$us_year			= $row2['year'];
			$us_term			= $row2['term'];
			
			if($us_total <=49){
			$grade = "Fail" AND $hgrade = "Poor Result, work hard to improve";
			}
			else if ($us_total >49 && $us_total<= 59)
			{
			$grade = "Fair" AND $hgrade = "Fair Result, needs serious improvement";
			}
			else if ($us_total >59 && $us_total<= 69)
			{
			$grade = "Faily Good" AND $hgrade = "Fairly Good Result, room for improvement";
			}
			else if ($us_total >69 && $us_total<= 79)
			{
			$grade = "Good" AND $hgrade = "Good Result, work harder to do much better";
			}
			else if ($us_total >79 && $us_total<= 89)
			{
			$grade = "Very Good" AND $hgrade = "Very Good Result, kudos to hardwork";
			}
			else if ($us_total >89 && $us_total<= 100)
			{
			$grade = "Excellent" AND $hgrade = "Excellent Result, Keep it up";
			}
			sql($DBH, "UPDATE tbl_student_results set grade = ?, position = ? WHERE registration_no =? AND csv_no = ?", array($grade,$sno,$us_registration_no,$_GET['val']));
			
			sql($DBH, "UPDATE tbl_student_results set hgrade = ?, position = ? WHERE registration_no =? AND csv_no = ?", array($hgrade,$sno,$us_registration_no,$_GET['val']));
		}
	}
			
			//Overall
			
		$STH 			= $DBH->prepare("SELECT count(*) FROM tbl_overall WHERE class = ? AND year = ? AND term = ? ");
        	$result 	   	= $STH->execute(array($_GET['stclass'],$_GET['year'],$_GET['stterm']));
        	$cnt_overall	  	= $STH->fetchColumn();
        	if($cnt_overall == 0){
        	
			
                    $rows1 = sql($DBH, "SELECT * FROM tbl_student_results WHERE class = ? AND year = ? AND term = ?", array($_GET['stclass'],$_GET['year'],$_GET['stterm']), "rows");
		foreach($rows1 as $row1)
		{	
			$st_registration_no	=	$row1['registration_no'];
			$st_class	=	$row1['class'];
			$st_name	=	$row1['name'];
			
		                                 
		$b_in_s = sql($DBH, "SELECT SUM(total) FROM tbl_student_results WHERE class = ? AND year = ? AND term = ? AND registration_no = ?", array($_GET['stclass'],$_GET['year'],$_GET['stterm'],$st_registration_no), "rows");			
		foreach($b_in_s as $b_in_row){	
			$b_ins	=	$b_in_row[0];
		}
			
		
		sql($DBH, "insert into  tbl_overall (name , reg_no , class , year , term, overall)values (?,?,?,?,?,?)", array($st_name,$st_registration_no,$_GET['stclass'],$_GET['year'],$_GET['stterm'],$b_ins));	
		
		
                    $rows2 = sql($DBH, "SELECT * FROM tbl_overall WHERE class = ? AND year = ? AND term = ? ORDER by (overall) desc", 
            	array($_GET['stclass'],$_GET['year'],$_GET['stterm']), "rows");
            	$snum = 0;
		foreach($rows2 as $row2)
		{	
			$snum++;
			$ust_name	 		= $row2['name'];
			$ust_reg_no 		= $row2['reg_no'];
			$ust_test 			= $row2['test'];	
		
			$rows1 = sql($DBH, "SELECT * FROM tbl_user where registration_no = ?", array($ust_reg_no), "rows");							
			foreach($rows1 as $row1)
			{	
				$dob   		= $row1['age'];
				
				//date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "$dob";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
    }
			sql($DBH, "UPDATE tbl_overall set position = ?, age = ? WHERE class = ? AND year = ? AND term = ? AND reg_no = ?", array($snum,$age,$_GET['stclass'],$_GET['year'],$_GET['stterm'],$ust_reg_no), "rows");
		}
	}	
}else if($cnt_overall > 0){
                    $rows2 = sql($DBH, "SELECT * FROM tbl_overall WHERE class = ? AND year = ? AND term = ? ORDER by (overall) desc", 
            	array($_GET['stclass'],$_GET['year'],$_GET['stterm']), "rows");
            	$snum = 0;
		foreach($rows2 as $row2)
		{	
			$snum++;
			$ust_name	 		= $row2['name'];
			$ust_reg_no 		= $row2['reg_no'];
			$ust_test 			= $row2['test'];	
			
			                                 
		$b_in_s = sql($DBH, "SELECT SUM(total) FROM tbl_student_results WHERE class = ? AND year = ? AND term = ? AND registration_no = ?", array($_GET['stclass'],$_GET['year'],$_GET['stterm'],$ust_reg_no), "rows");			
		foreach($b_in_s as $b_in_row){	
			$b_ins	=	$b_in_row[0];
		}
			
			
	sql($DBH, "UPDATE tbl_overall set overall = ? WHERE class = ? AND year = ? AND term = ? AND reg_no = ?", array($b_ins,$_GET['stclass'],$_GET['year'],$_GET['stterm'],$ust_reg_no), "rows");
			sql($DBH, "UPDATE tbl_overall set position = ? WHERE class = ? AND year = ? AND term = ? AND reg_no = ?", array($snum,$_GET['stclass'],$_GET['year'],$_GET['stterm'],$ust_reg_no), "rows");
			
			
	}
}


//CummClass
			
		$STH 			= $DBH->prepare("SELECT count(*) FROM tbl_CummClass WHERE class = ? AND year = ?");
        	$result 	   	= $STH->execute(array($_GET['stclass'],$_GET['year']));
        	$cnt_overall	  	= $STH->fetchColumn();
        	if($cnt_overall == 0){
        	
			
                    $rows1 = sql($DBH, "SELECT * FROM tbl_student_results WHERE class = ? AND year = ?", array($_GET['stclass'],$_GET['year']), "rows");
		foreach($rows1 as $row1)
		{	
			$st_registration_no	=	$row1['registration_no'];
			$st_class	=	$row1['class'];
			$st_name	=	$row1['name'];
			
		                                 
		$b_in_s = sql($DBH, "SELECT SUM(total) FROM tbl_student_results WHERE class = ? AND year = ? AND registration_no = ?", array($_GET['stclass'],$_GET['year'],$st_registration_no), "rows");			
		foreach($b_in_s as $b_in_row){	
			$b_ins	=	$b_in_row[0];
		}
			
		
		sql($DBH, "insert into  tbl_CummClass (name , reg_no , class , year , overall)values (?,?,?,?,?)", array($st_name,$st_registration_no,$_GET['stclass'],$_GET['year'],$b_ins));	
		
		
                    $rows2 = sql($DBH, "SELECT * FROM tbl_CummClass WHERE class = ? AND year = ? ORDER by (overall) desc", 
            	array($_GET['stclass'],$_GET['year']), "rows");
            	$snum = 0;
		foreach($rows2 as $row2)
		{	
			$snum++;
			$ust_name	 		= $row2['name'];
			$ust_reg_no 		= $row2['reg_no'];
			$ust_test 			= $row2['test'];	 
		
			$rows1 = sql($DBH, "SELECT * FROM tbl_user where registration_no = ?", array($ust_reg_no), "rows");							
			foreach($rows1 as $row1)
			{	
				$dob   		= $row1['age'];
				
				//date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "$dob";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
    }
			sql($DBH, "UPDATE tbl_CummClass set position = ?, age = ? WHERE class = ? AND year = ? AND reg_no = ?", array($snum,$age,$_GET['stclass'],$_GET['year'],$ust_reg_no), "rows");
		}
	}	
}else if($cnt_overall > 0){
                    $rows2 = sql($DBH, "SELECT * FROM tbl_CummClass WHERE class = ? AND year = ? ORDER by (overall) desc", 
            	array($_GET['stclass'],$_GET['year']), "rows");
            	$snum = 0;
		foreach($rows2 as $row2)
		{	
			$snum++;
			$ust_name	 		= $row2['name'];
			$ust_reg_no 		= $row2['reg_no'];
			$ust_test 			= $row2['test'];	
			
			                                 
		$b_in_s = sql($DBH, "SELECT SUM(total) FROM tbl_student_results WHERE class = ? AND year = ? AND registration_no = ?", array($_GET['stclass'],$_GET['year'],$ust_reg_no), "rows");			
		foreach($b_in_s as $b_in_row){	
			$b_ins	=	$b_in_row[0];
		}
			
			
	sql($DBH, "UPDATE tbl_CummClass set overall = ? WHERE class = ? AND year = ? AND reg_no = ?", array($b_ins,$_GET['stclass'],$_GET['year'],$ust_reg_no), "rows");
			sql($DBH, "UPDATE tbl_CummClass set position = ? WHERE class = ? AND year = ? AND reg_no = ?", array($snum,$_GET['stclass'],$_GET['year'],$ust_reg_no), "rows");
			
	}
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>TEACHERS DASHBOARD: BRITISH LEARNERS COLLEGE, OWERRI, IMO STATE</title>
  <!-- Bootstrap core CSS-->
  <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Custom fonts for this template-->
  <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <!-- Page level plugin CSS-->
  <link href="vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">
  <!-- Custom styles for this template-->
  <link href="css/sb-admin.css" rel="stylesheet">
  <link href="css/folder.css" rel="stylesheet">
  <link href="css/user_profile.css" rel="stylesheet">
  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <!-- select-->
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  
  <style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
  </style>
</head>

<body class="fixed-nav sticky-footer bg-dark" id="page-top">
  <!-- Navigation-->
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" id="mainNav">
<h4 style='color: LIME'><b>BRITISH LEARNERS COLLEGE, OWERRI, IMO STATE: TEACHER DASHBOARD </b></h4>  
<?php
            	
            	$rows = sql($DBH, "SELECT * FROM tbl_admin where status = ?", 
            	array("Active"), "rows");							
            	foreach($rows as $row)
            	{
            		$admin_name 		= $row['username'];
            	}
		
      if (isset($_SESSION['SESS_HASH_ADMIN'])) {
		$user_true 			 = "<h4 style='color: yellow'><!--<b>SCORE SHEET--></b></h4>";
		
}
		echo "$user_true ";
?>
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
     <div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav navbar-sidenav" id="exampleAccordion">
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Dashboard">
    <a class="nav-link" href="main.php">
      <i class="fa fa-dashboard" style="color: #3498db"></i>
      <span class="nav-link-text" style="color: white">Teacher Dashboard</span>
    </a>
  </li>
  <!--<li class="nav-item" data-toggle="tooltip" data-placement="right" title="Generate Score Sheet">
    <a class="nav-link" href="students.php">
      <i class="fa fa-clock-o" style="color: #3498db"></i>
      <span class="nav-link-text" style="color: white">Generate Score Sheet</span>
    </a>
  </li> -->
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Download Broadsheet">
    <a class="nav-link" href="download_broadsheet.php">
      <i class="fa fa-download" style="color: blue"></i>
      <span class="nav-link-text" style="color: white">Download Broadsheet</span>
    </a>
  </li>
  <!--<li class="nav-item" data-toggle="tooltip" data-placement="right" title="Fill Attendance Sheet">
    <a class="nav-link" href="fill_attendance_sheet.php">
      <i class="fa fa-download" style="color: red"></i>
      <span class="nav-link-text" style="color: white">Download Specimen Excel Sheets</span>
    </a>
  </li> -->
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Upload Tests (CA 1, C.A.2, EXAM)">
    <a class="nav-link" href="upload_ca1.php">
      <i class="fa fa-upload" style="color: orange"></i>
      <span class="nav-link-text" style="color: white">Upload Tests (CA 1, CA 2, EXAM)</span>
    </a>
  </li>
    <!--<li class="nav-item" data-toggle="tooltip" data-placement="right" title="Upload Tests (CA 2)">
    <a class="nav-link" href="upload_ca2.php">
      <i class="fa fa-upload" style="color: orange"></i>
      <span class="nav-link-text" style="color: white">Upload Tests (CA 2)</span>
    </a>
  </li>
    <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Upload Tests (CA 3)">
    <a class="nav-link" href="upload_ca3.php">
      <i class="fa fa-upload" style="color: orange"></i>
      <span class="nav-link-text" style="color: white">Upload Tests (CA 3)</span>
    </a>
  
    <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Upload Exams">
    <a class="nav-link" href="upload_exam.php">
      <i class="fa fa-upload" style="color: orange"></i>
      <span class="nav-link-text" style="color: white">Upload Exam Scores</span>
    </a></li>-->
  </li>
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Fill Attendance Sheet">
    <a class="nav-link" href="upload_attendance.php">
      <i class="fa fa-upload" style="color: red"></i>
      <span class="nav-link-text" style="color: white">Upload Attendance Scores</span>
    </a>
  </li>
  <!--<li class="nav-item" data-toggle="tooltip" data-placement="right" title="Fill Attendance Sheet">
    <a class="nav-link" href="fill_attendance_sheet.php">
      <i class="fa fa-upload" style="color: red"></i>
      <span class="nav-link-text" style="color: white">Upload Exam Scores</span>
    </a>
  </li> -->
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Upload Skills, Psychomotor, and others">
    <a class="nav-link" href="upload_skills.php">
      <i class="fa fa-upload" style="color: purple"></i>
      <span class="nav-link-text" style="color: white">Upload Skills, Psychomotor, and others</span>
    </a>
  </li>
  <li class="nav-item" data-toggle="tooltip" data-placement="right" title="Logout">
    <a class="nav-link" href="logout.php">
      <i class="fa fa-sign-out" style="color: #3498db"></i>
      <span class="nav-link-text" style="color: white">Logout</span>
    </a>
  </li>
</ul>
    </div>
  </nav>
  
  <div class="content-wrapper">
  <span class="col-lg-12"><img src='BLC_Banner_Result.png' alt="BRITISH LEARNERS COLLEGE" width='1024' height="90" style=' margin-left: auto; margin-right: auto; display: block'/></span>
  <div class="container-fluid">
      <!-- Breadcrumbs-->
      <ol class="breadcrumb" style="background: white">
        <li class="breadcrumb-item">
          <a href="main.php" style="color: black; font-weight: bold">Teacher's Dashboard</a>
          

<p>Contact DataSwift Support line in case you run into issues: <b>(MENDOH -08032929756)</b></p>
   
                    <BR>
<Center><h2>ENTER ATTENDANCE SCORES HERE</h2>
<h4>Pls ensure that the sheet you're uploading also contains <B>Number of Days School Opened</B></h4></Center>
                        <form action="importDataAttendance.php" method="post" enctype="multipart/form-data" id="importFrm">
                <input type="file" name="file" /><br/><br/>
                
			<div class='row'>
			<div class='col-sm-2'>
			<select name='subject' class='form-control'>
<?php
			$rows1 = sql($DBH, "SELECT * FROM tbl_subject where id >= ?", array(1), "rows");							
			foreach($rows1 as $row1)
			{	
				$sch_subject   		= $row1['subject'];
				
				echo "
				<option value='$sch_subject'>$sch_subject</option>";
				}
			?>
			</select>
		</div>
			<div class='col-sm-2'>
			<select name='term' class='form-control'>
				<option value='1st Term'>1st Term</option>";
				<option value='2nd Term'>2nd Term</option>";
				<option value='3rd Term'>3rd Term</option>";
			?>
			</select>
		</div>
			<div class='col-sm-2'>
			<select name='year' class='form-control'>
<?php
			$rows1 = sql($DBH, "SELECT * FROM tbl_session where id >= ?", array(1), "rows");							
			foreach($rows1 as $row1)
			{	
				$sch_session   		= $row1['session'];
				
				echo "
				<option value='$sch_session'>$sch_session</option>";
				}
			?>
			</select>
		</div>
			<div class='col-sm-3'>
			<select name='teacher_no' class='form-control'>
<?php
			$rows1 = sql($DBH, "SELECT * FROM tbl_user where teacher = ?", array("true"), "rows");							
			foreach($rows1 as $row1)
			{	
				$sch_hash   		= $row1['hash'];
				$sch_name   		= $row1['fullname'];
				$sc_class   		= $row1['class'];
				
				echo "
				<option value='$sch_hash'>$sc_class - $sch_name</option>";
				}
			?>
			</select>
		</div>
		<div class='col-sm-2'>
                <input type="submit" class="btn btn-primary" name="importSubmit" style="background: #81C0DA; value="IMPORT">
		</div>
		</div>
            </form>

<HR>                <BR>
<br/>
            <br/><br/>
      <div class="row">
                <?php
                    //get records from database
                      
            	//AND status = ? "DONATED",
                $STH 			= $DBH->prepare("SELECT count(*) FROM tbl_student_results WHERE id >= ?");
            	$result 	   	= $STH->execute(array(1));
            	$count		  	= $STH->fetchColumn();
            	
            	if($count >= 1){
                           
		$rows = sql($DBH, "SELECT MIN(id) AS id, year FROM tbl_student_results  WHERE id >= ? GROUP BY year", 
            	array(1), "rows");
		foreach($rows as $row)
		{	
			$name	 		= $row['name'];
			$registration_no 		= $row['registration_no'];
			$test 			= $row['test'];
			$test2 			= $row['test2'];
			$test3 			= $row['test3'];
			$test4 			= $row['test4'];
			$exam			= $row['exam'];
			$remark_subject			= $row['remark_subject'];
			$t_total			= $row['total'];
			$t_grade			= $row['grade'];
			$position			= $row['position'];
			$t_subject			= $row['subject'];
			$t_class			= $row['class'];
			$t_year			= $row['year'];
			
		
			echo"
			
                    <div1><a href='main_term.php?id=$t_yearHIDE' target='blank'>$t_yearHIDE</a></div1>";
                    }
                    }else{
                    echo "
                    No Result(s) found.....";
                    }
                   
                    ?>
          </div>
        </div>
      </div>
    <!-- /.container-fluid-->
    <!-- /.content-wrapper-->
    <footer class="sticky-footer" style="background: #ac39ac">
      <div class="container">
        <div class="text-center"> <small><strong><span class="style1">Copyright &copy; 2022-2024 BRITISH LEARNERS COLLEGE, OWERRI, IMO STATE: All Rights Reserved.</span></strong></small> </div>
      </div>
    </footer>
    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fa fa-angle-up"></i>
    </a>
    <!-- Logout Modal-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
            <a class="btn btn-primary" href="Sign_in.php">Logout</a>
          </div>
        </div>
      </div>
    </div>
    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
    <!-- Page level plugin JavaScript-->
    <script src="vendor/chart.js/Chart.min.js"></script>
    <script src="vendor/datatables/jquery.dataTables.js"></script>
    <script src="vendor/datatables/dataTables.bootstrap4.js"></script>
    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin.min.js"></script>
    <!-- Custom scripts for this page-->
    <script src="js/sb-admin-datatables.min.js"></script>
    <script src="js/sb-admin-charts.min.js"></script>
  </div>
</body>

</html>
