<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");

if ($koneksi) {
    $idujian = $_POST['id'];
    $query = mysqli_query($koneksi, "select * from nilai where id_ujian='$idujian' and status is null");
    $cek = mysqli_num_rows($query);
    if ($cek <> 0) {
        $array_nilai = array();
        while ($nilai = mysqli_fetch_assoc($query)) {
            $array_nilai[] = $nilai;
        }
        $payload = json_encode($array_nilai);

        $url = $setting['url_host'] . '/syncnilai.php?token=' . $setting['token_api'];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);

        if ($result == 'sukses') {
            mysqli_query($koneksi, "UPDATE nilai SET status='1' where id_ujian='$idujian'");
        }
    } else {
        echo 'gagal';
    }
} else {
    echo "server tidak terhubung";
}
