<?php

    header('Access-Control-Allow-Origin: *');

    if ($_SERVER['REQUEST_METHOD'] != 'POST')
    {
        echo "POST request expected";
        echo'<br><br>';
        echo'<a href="result/">Download Hasil</a>';
        echo'<br><br>';
        echo'<a href="export-excel/index.php">Lihat Hasil Ujian</a>';
        return;
    }

    error_reporting(E_ALL && E_WARNING && E_NOTICE);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);

    require_once 'includes/common.inc.php';
    require_once 'classes/QuizResults.class.php';
    require_once 'classes/QuizReportGenerator.class.php';


    $requestParameters = RequestParametersParser::getRequestParameters($_POST, !empty($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : null);
    _log($requestParameters);

    try
    {
//here coonection to mysql
$host	= "localhost";
$user	= "poltekbangsby_tpa-nonreg";
$pass	= "Aurel@12345";
$db	= "poltekbangsby_tpa-nonreg";

//Menggunakan objek mysqli untuk membuat koneksi dan menyimpanya dalam variabel $mysqli
$mysqli = new mysqli($host, $user, $pass, $db);

        $quizResults = new QuizResults();
        $quizResults->InitFromRequest($requestParameters);
        $generator = QuizReportFactory::CreateGenerator($quizResults, $requestParameters);
        $report = $generator->createReport();
        //here add extra code
        $nama= $_POST['USER_NAME'];
        $score = $_POST['sp']; //point
        $passing_score = $_POST['ps'];
        // $kelas = $_POST['KELAS'];
        // $no_urt= $_POST['NOURT'];
        $no_daftar = $_POST['NO_DAFTAR'];
        $nama_ujian = $_POST['qt'];
        $gained_score = $_POST['tp'];
        $score_percentage = $score/$gained_score*100;
        if($score < $passing_score)
            $status = 'Tidak Lulus';
        else
            $status = 'Lulus';
        
        date_default_timezone_set('Asia/Jakarta');
        $dateTime = date('Y-m-d H:i:s');
        $resultFilename = dirname(__FILE__) . "/result/{$nama}_{$score}-quiz_result_{$dateTime}.txt";
        //@file_put_contents($resultFilename, $report);
        //here add extra code to input to database
        // $sql = "INSERT INTO hasil_ujian (id, nama_siswa, kelas, no_urt, nilai, nama_ujian,waktu) VALUES ('default', '$nama', '$kelas','$no_urt','$gained_score','$nana_ujian','$dateTime')";
         $sql = "INSERT INTO tpa_nonreg_result (nama, no_daftar, score, nama_ujian, status, passing_score,score_percentage, waktu) VALUES ('$nama','$no_daftar','$score', '$nama_ujian', '$status', '$passing_score','$score_percentage', '$dateTime')";
if(mysqli_query($mysqli, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($mysqli);
}

// Close connection
mysqli_close($mysqli);

        echo "OK";
    }
    catch (Exception $e)
    {
        error_log($e);

        echo "Error: " . $e->getMessage();
    }

    function _log($requestParameters)
    {
        $logFilename = dirname(__FILE__) . '/log/quiz_results.log';
        $event       = array('ts' => date('Y-m-d H:i:s'), 'request_parameters' => $requestParameters, 'ts_' => time());
        $logMessage  = json_encode($event);
        $logMessage .= ',' . PHP_EOL;
        //@file_put_contents($logFilename, $logMessage, FILE_APPEND);
    }
    echo'<a href="result/">Download Hasil</a>';
