<?php
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/var/cpanel/php/sessions/easytech-bpatcsc'));
session_start();
include "config.php";
include "check_mac.php";
//echo $row_count;



include "check_token.php";  // Check user token
//include "check_class_time.php";  // Check user class time

// Check user login or not
if(!isset($_SESSION['username'])){
   header('Location: index.php');
}

?>



<?php




$classname = $_SESSION['class_name'];
$subjectcode = $_SESSION['subject_name'];
date_default_timezone_set("Asia/Dhaka");

$today_date= date("Y-m-d");
$today_time=date("h:i:s");

	//echo $today_date;		
	


//whether ip is from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
  {
    $mac = $_SERVER['HTTP_CLIENT_IP'];
  }
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
  {
    $mac = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
//whether ip is from remote address
else
  {
    $mac = $_SERVER['REMOTE_ADDR'];
  }
//echo $mac;


if(isset($_POST['submit'])){
    if($row_count == 0){
     
$idnumber=$_SESSION['idnumber'];
//echo $idnumber;
$mac_id = mysqli_real_escape_string($link, $mac);
//echo $mac_id;

include "config.php";

$query = "SELECT st_subject from subject_name where subject_code='$subjectcode'";
$result = mysqli_query($link, $query);
	$row = mysqli_fetch_array($result);
	$subjectname=$row['st_subject'];
	

	$query = "INSERT INTO zoom_attendent_class (st_id, st_name,st_group,st_mobile,st_class,st_subject,class_date,class_time,mac_id)
SELECT st_id,st_name,st_group,st_mobile,st_class,'$subjectname','$today_date','$today_time','$mac_id' FROM student_class
WHERE st_id='$idnumber'";
//echo $query;	
			$success = mysqli_query($link,$query);
		
	

if (!$success) {
    die("Couldn't enter data: ".$link->error);
} 

}


//header("Location: index.php");

//mysqli_close($link); 
//exit();



$subject_name=$_SESSION['class_name'];
$class_name=$_SESSION['subject_name'];
$idnumber=$_SESSION['idnumber'];
//echo $subject_name;
//echo $class_name;


//include('config.php'); 


$query = "SELECT b.group_id  sst_group,a.st_section  sst_section 
FROM 
student_class a
inner join
group_name b 
on a.st_group=b.st_group 
where a.st_id='$idnumber' ";
$result = mysqli_query($link, $query);

//	$result = mysqli_query($link, "SELECT st_group,st_section FROM student_class where st_id='$idnumber'");
	$row = mysqli_fetch_array($result);
	$studentgroup=$row['sst_group'];
	$studentsection=$row['sst_section'];





	// $result = mysqli_query($link, "SELECT st_group,st_section FROM student_class where st_id='$idnumber'");
	// $row = mysqli_fetch_array($result);
	// $studentgroup=$row[0];
  // $studentsection=$row[1];
  


// Escape user inputs for security
$classname = mysqli_real_escape_string($link, $_SESSION['class_name']);
$subjectname = mysqli_real_escape_string($link, $_SESSION['subject_name']);
//$zoomlink = mysqli_real_escape_string($link, $_POST['zoomlink']);
 	$date_today=date("Y-m-d");
 
 
			$query = "select zoom_link from zoomlink_class where st_class = '$classname' and st_group = '$studentgroup' and st_section = '$studentsection' and st_subject = '$subjectname' and class_date = '$date_today'";
			$result = mysqli_query($link,$query);
			$numRows = mysqli_num_rows($result);
			//echo $numRows;
			if($numRows>0){
			while ($row = mysqli_fetch_array($result))
            {
				//echo $row[0];
				$linkzoom= $row[0];
			}
			
			} 
			
			/*else{
			 
				header("location: index.php");
		$_SESSION['error'] = "<div class='alert alert-danger' role='alert'>Invalid Class and Subject.</div>";
				
			}*/
				
	// close connection
mysqli_close($link);
// remove all session variables
session_unset();

// destroy the session
//session_destroy();
}


?>





<?php
// Initialize the session
//session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    
<script type="text/javascript">
<?php echo 'var x = ' . json_encode($linkzoom) . ';';?>
setTimeout(function rrdirect()
{
 //window.location= x;
 window.location= x;
}, 5000);

//document.write("You will be redirected to main page in 10 sec.");
//setTimeout('rrdirect()', 10000);
</script>
 
    
    
    
    
	<style>
	.form-group{
		margin-top: 200px;
	}
	
		
.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}
	

	
	</style>
</head>
<body>

    
<div class="topnav">
  <a href="index.php">Home</a>
  <a class="active" href="index.php">Join Live Class</a>
  <a href="logout.php">Logout</a>
  <a href="#about">About</a>
</div>


<center>
<div class="form-group">
<!--<h3 style="color:blue;"> Join Exam Click On This Button</h3>
                <input type="submit"  class="btn btn-primary" onclick="location.href='<?php echo $linkzoom;?>';" value="Join Exam">-->
            <p><h3 style="color:blue;"> Please wait for 5 sec to Join Live Class..</h3></p>
            
			</div>

			
	</center>
</form>	

<?php

echo '<script type="text/javascript">rrdirect(); </script>'; 
/*echo '<script>',
			 'rrdirect();',
			 '</script>';*/
   
   
   
   /* $number = $linkzoom;

    echo '<script>';
    echo 'var x = ' . json_encode($number) . ';';
    //echo 'alert(x);';
		echo 'window.location= x;';
    echo '</script>';*/


?>


</body>
</html>