<?php
include('config.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{   
    $eval_ID = $_POST["eval_id"];
    $user_email = $_POST["email"];
    $eval_stat = $_POST["eval_stat"];

    $sql_evaluation = mysqli_query($db, "SELECT * FROM evaluations WHERE ID='$eval_ID'");
    while($DB_ROW = mysqli_fetch_array($sql_evaluation)) {
        $turn_in = $DB_ROW['Turned_in'];
        $total = $turn_in - 1;
    }

    // Get the current time adn date
    date_default_timezone_set('Asia/Manila');
    $date = date("m/d/Y"); 

    $sql = "UPDATE submitted_evaluation SET `Eval_Stat`='2', `Date_Submitted`='$date' WHERE Eval_ID='$eval_ID' && Email='$user_email'";
    if (mysqli_query($db, $sql)){
        $sql1 = "UPDATE evaluations SET `Turned_in`='$total' WHERE `ID`='$eval_ID'";
        if (mysqli_query($db, $sql1)){
            echo '<script>alert("Unsubmitted successfully!");</script>';
            echo '<script>document.location = "view_evaluation.php?ID='.$eval_ID.'"</script>';
        }
    }
    else{
        echo mysqli_error($db);
    }   
}
?>