<?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-danger' style='text-align: center'>Wrong Pin! Try again or buy a valid pin.</h5>";
	
	}else if($_GET['msg'] == "sfailed"){
		$msg = "<h5 class='alert alert-danger' style='text-align: center'>Wrong Pin! Pin User Already Exist.</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_test 			= $row1['test'];
			$u_home_work 			= $row1['home_work'];
			$u_project			= $row1['project'];
			$u_exam			= $row1['exam'];
			
			$total = ($u_test + $u_home_work + $u_project + $u_exam);


			sql($DBH, "UPDATE tbl_student_results set total = ?, teacher_no = ? WHERE registration_no =? AND test = ?", array($total,$SESS_HASH_USER,$u_registration_no,$u_test));
			
			
                    $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_test 			= $row2['test'];
			$us_home_work 			= $row2['home_work'];
			$us_project			= $row2['project'];
			$us_exam			= $row2['exam'];
			$us_total			= $row2['total'];
			
			if($us_total <=39){
			$grade = "Poor";
			}
			else if ($us_total >39 && $us_total<= 49)
			{
			$grade = "Fair";
			}
			else if ($us_total >49 && $us_total<= 59)
			{
			$grade = "Credit";
			}
			else if ($us_total >59 && $us_total<= 69)
			{
			$grade = "Good";
			}
			else if ($us_total >69 && $us_total<= 79)
			{
			$grade = "Very Good";
			}
			else if ($us_total >79 && $us_total<= 100)
			{
			$grade = "Excellent";
			}
			
			
			sql($DBH, "UPDATE tbl_student_results set grade = ?, position = ? WHERE registration_no =? AND test = ?", array($grade,$sno,$us_registration_no,$us_test));
			}
			}
            
	
?>
<!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>GILFORD MEMORIAL SCHOOLS: STUDENT/PUPIL PAGE</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/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">
  
</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">
  
<?php
     
    if(isset($_SESSION['SESS_TOKEN']))
	{
	$rows = sql($DBH, "SELECT * FROM tbl_user where hash = ?", array($SESS_HASH_USER), "rows");							
		foreach($rows as $row)
		{	
                $fullname 	 	= $row['fullname'];
                $profile_pic 		= $row['profile_pic'];
                $user_email	 	= $row['email'];
                $user_phone	 	= $row['phone'];
                $user_class	 	= $row['class'];
                $user_subclass	 	= $row['subclass'];
                $user_age	 	= $row['age'];
                $user_reg	 	= $row['registration_no'];
                
		}
		
		if($profile_pic == "default"){
		$user_true 			 = "<img class='user one' src='profile_pic/profile.jpg' width='500px'>";
		
		}
		else{
		$user_true 			 = "<img class='user one' src='/sssys/admin/profile_pic/$profile_pic' width='500px'>";
		}
		
}
		echo "$user_true ";
?>
      <ul class="navbar-nav ml-auto">
        <li class="nav-item">
          <a class="nav-link" href="main.php">
            <i class="fa fa-fw fa-dashboard" style="color: white"></i>
            <span class="nav-link-text" style="color: white">Student Dashboard</span>
          </a>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="logout.php">
            <i class="fa fa-fw fa-sign-out" style="color: white"></i>
            <span class="nav-link-text" style="color: white">Log Out</span>
          </a>
        </li>
      </ul>
    </div>
  </nav>
  
  <div class="content-wrapper">
    <div class="container-fluid">
      <!-- Breadcrumbs-->
      <ol class="breadcrumb" style="background: white">
        <li class="breadcrumb-item">
          <a href="main.php" style="color: grey; font-weight: bold"><?php echo"$fullname"; ?></a>
          
        </li>
        <li class="breadcrumb-item active" style="color: black">Student Page</li>
      </ol>
    <?php if(!empty($statusMsg)){
        echo '<div class="alert '.$statusMsgClass.'">'.$statusMsg.'</div>';
    } ?>
      <div class="row">
        <div class="col-lg-12">
          <!-- Example Bar Chart Card-->
      <ol class="breadcrumb"style="background: Navy">
                                    
            <h4 style="color: white">Result Checker
                                    </ol>
                                    <?php echo $msg; ?>
                                    
	<div class='row'>
		<div class='col-sm-2'>
			<h4><b>Age:</b> <?php
			
			$rows1 = sql($DBH, "SELECT * FROM tbl_user where hash = ?", array($SESS_HASH_USER), "rows");							
			foreach($rows1 as $row1)
			{	
				$dob   		= $row1['age'];
				$userst_registration_no   		= $row1['registration_no'];
				
				//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]));
    }
    
              
		$rows = sql($DBH, "SELECT MIN(id) AS id, class FROM tbl_student_results  WHERE registration_no = ? GROUP BY class", 
            	array($userst_registration_no), "rows");
		foreach($rows as $row)
		{	
			$userst_class	 		= $row['class'];
		}
		
		$rows = sql($DBH, "SELECT MAX(id) AS id, subclass FROM tbl_student_results  WHERE registration_no = ? GROUP BY subclass", 
            	array($userst_registration_no), "rows");
		foreach($rows as $row)
		{	
			$userst_subclass	 		= $row['class'];
		}
		
			 echo $age; ?></h4>
		</div>
		<div class='col-sm-2'>
			<h4><b>Class:</b> <?php echo $userst_class; ?></h4>
		</div>
		<div class='col-sm-2'>
		</div>
	</div><br/>
	<hr>
	 <form action="check_results.php" method="post" enctype="multipart/form-data" id="importFrm">
	<div class='row'>
		<div class='col-sm-2'>
			<h4><b>Check Your Results</b></h4>
		</div>
		<div class='col-sm-2'>
			<input type='password' class='form-control' name='pin' placeholder='Enter Pin' required />
		</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'>
			<input type='text' class='form-control' name='registration_no'  placeholder='Enter Reg. No' required />
		</div>
		<div class='col-sm-2'>
			
			<select name='mclass' class='form-control'>
<?php
			$rows1 = sql($DBH, "SELECT * FROM tbl_class where id >= ?", array(1), "rows");							
			foreach($rows1 as $row1)
			{	
				$sch_class   		= $row1['class'];
				
				echo "
				<option value='$sch_class'>$sch_class</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-2'>
			<input type="submit" class="btn btn-primary" name="check" value="CHECK">
		</div>
	</div>
	</form>
	<hr>
			
            </div>
           </div>
          </div>
    <!-- /.container-fluid-->
    <!-- /.content-wrapper-->
    <footer class="sticky-footer" style="background: Orange">
      <div class="container">
        <div class="text-center">
          <small><strong>Copyright © 2017-2018 Gilford Memorial Schools: All Rights Reserved!</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">Close from Here?</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">Select "Sign Out" below if you through with your Result Checking.</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">Sign Out</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>
