
<center><img src="../images/loading.gif"></center>

<?php
if (isset($_GET['penilaian'])) {
    $penilaian = base64_decode($_GET['penilaian']);
    $txt = explode("|", $penilaian);
    $kdmpl = explode(",", $txt[3]);
    $kode_tapel = $txt[0];
    $penilaian_id = $txt[1];
    $guru_id = $txt[2];
    $kode_mapel = $kdmpl[0];
    $kelas = $txt[4];

    $database_file = '../../db/rapor.db'; 
    $pdo = new PDO("sqlite:$database_file"); 

    $query = "SELECT nis, nisn, nama, jk FROM peserta_didik WHERE kelas = :kelas AND kode_tapel='$kode_tapel'";
    $statement = $pdo->prepare($query);
    $statement->bindParam(':kelas', $kelas, PDO::PARAM_STR);
    $statement->execute();

    while ($datasiswalist = $statement->fetch(PDO::FETCH_ASSOC)) {
        $nis = $datasiswalist['nis']; 
        $nisn = $datasiswalist['nisn']; 
        $nama = $datasiswalist['nama']; 
        $jk = $datasiswalist['jk'];

        // Input ke database nilai_formatif
        $insertQuery = "INSERT INTO nilai_formatif (nis, nama, jk, kode_tapel, penilaian_id, guru_id, kode_mapel, kelas) VALUES (:nis, :nama, :jk, :kode_tapel, :penilaian_id, :guru_id, :kode_mapel, :kelas)";
        $insertStatement = $pdo->prepare($insertQuery);
        $insertStatement->bindParam(':nis', $nis, PDO::PARAM_STR); 
        $insertStatement->bindParam(':nama', $nama, PDO::PARAM_STR);
        $insertStatement->bindParam(':jk', $jk, PDO::PARAM_STR);
        $insertStatement->bindParam(':kode_tapel', $kode_tapel, PDO::PARAM_STR);
        $insertStatement->bindParam(':penilaian_id', $penilaian_id, PDO::PARAM_STR);
        $insertStatement->bindParam(':guru_id', $guru_id, PDO::PARAM_STR);
        $insertStatement->bindParam(':kode_mapel', $kode_mapel, PDO::PARAM_STR);
        $insertStatement->bindParam(':kelas', $kelas, PDO::PARAM_STR);
        
        $insertStatement->execute(); 
    }    
    echo "<script>";
    echo "alert('Lembar Kerja Nilai Berhasil dibuat')";
    echo "</script>";
    echo '<meta http-equiv="refresh" content="0;url=../PenilaianList" />';
}
?>
