<?php
session_start();
if (isset($_SESSION['logged']) && $_SESSION['logged'] == "1" && $_SESSION['role'] == "admin") {

require_once('../../assets/constants/config.php');
require_once('../../assets/constants/uniques.php');

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        
    $stmt = $conn->prepare("SELECT * FROM tbl_school_info");
    $stmt->execute();
    $result = $stmt->fetchAll();
    
    foreach($result as $row) {

        $currency = $row[6];
        date_default_timezone_set($row[5]);

    }
                          
    }catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

$exam = ucwords($_POST['name']);
if (isset($_POST['duration'])) {
    $duration = $_POST['duration'];
}else{
    $duration = "";
}

$passmark = $_POST['passmark'];
if (isset($_POST['attempts'])) {
    $attempts = $_POST['attempts'];
}else{
    $attempts = "";
}


$deadline = $_POST['deadline'];
$department = $_POST['department'];
$class = $_POST['class'];
$subject = $_POST['subject'];
$type = $_POST['type'];

if ($type == "Free") {
$price = "";
}else{
$price = $_POST['price'];
}

$terms = $_POST['terms'];

if (isset($_POST['corrections'])) {
$corrections = "1";
}else{
$corrections = "0";
}

$questions = $_POST['questions'];


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	
$stmt = $conn->prepare("SELECT * FROM tbl_exams WHERE name = :name AND department = :department AND class = :class AND subject = :subject");
$stmt->bindParam(':name', $firstname);
$stmt->bindParam(':department', $lastname);
$stmt->bindParam(':class', $email);
$stmt->bindParam(':subject', $email);
$stmt->execute();
$result = $stmt->fetchAll();

$rec = count($result);

if ($rec > 0) {

$_SESSION['reply'] = "002";
header("location:../exams");

}else{

$stmt = $conn->prepare("INSERT INTO tbl_exams (name, duration, passmark, re_exam, deadline, department, class, subject, exam_type, fee, terms, corrections, questions) VALUES (:name, :duration, :pass, :rex, :dead, :dep, :class, :subject, :type, :fee, :terms, :correct, :questions)");
$stmt->bindParam(':name', $exam);
$stmt->bindParam(':duration', $duration);
$stmt->bindParam(':pass', $passmark);
$stmt->bindParam(':rex', $attempts);
$stmt->bindParam(':dead', $deadline);
$stmt->bindParam(':dep', $department);
$stmt->bindParam(':class', $class);
$stmt->bindParam(':subject', $subject);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':fee', $price);
$stmt->bindParam(':terms', $terms);
$stmt->bindParam(':correct', $corrections);
$stmt->bindParam(':questions', $questions);
$stmt->execute();

$_SESSION['reply'] = "018";
header("location:../exams");

}

foreach($result as $row)
{

}
					  
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}






}else{

header("location:../");

}

?>
