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

								
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'login') {
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysqli_query($koneksi, "SELECT * FROM siswa WHERE username='$username' AND password='$password' ");
$cek = mysqli_num_rows($query);
if ($cek <> 0) {
$user = mysqli_fetch_array($query);
   echo $user['id_siswa']."#";
   echo $user['nama']."#";
   echo $user['kelas']."#";
   echo $user['level']."#";
   echo $setting['npsn'];
}else{
	echo "gagal";
}
	
}

if ($pg == 'info') {
$query = "SELECT * FROM informasi WHERE untuk='0' ORDER BY id DESC LIMIT 1";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
 $getdata[] = $row;
 }
 echo json_encode($getdata);
} else {
echo "0";
}

}


if ($pg == 'jadwal') {
 
								$query = mysqli_query($koneksi, "SELECT * FROM soal "); 
								  while ($data = mysqli_fetch_array($query)) :
								$no++;
								 if($data['pilA']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilA=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilB']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilB=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilC']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilC=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilD']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilD=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['pilE']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilE=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['perA']==''):
									mysqli_query($koneksi,"UPDATE soal SET perA=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['perB']==''):
									mysqli_query($koneksi,"UPDATE soal SET perB=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['perC']==''):
									mysqli_query($koneksi,"UPDATE soal SET perC=NULL WHERE nomor='$data[nomor]'");
									endif;
									if($data['perD']==''):
									mysqli_query($koneksi,"UPDATE soal SET perD=NULL WHERE nomor='$data[nomor]'");
									endif;
									if($data['perE']==''):
									mysqli_query($koneksi,"UPDATE soal SET perE=NULL WHERE nomor='$data[nomor]'");
									endif;
									
								   	if($data['file']==''):
									mysqli_query($koneksi,"UPDATE soal SET file=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['file1']==''):
									mysqli_query($koneksi,"UPDATE soal SET file1=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileA']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileA=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileB']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileB=NULL WHERE nomor='$data[nomor]'");
									endif;	
										if($data['fileC']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileC=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileD']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileD=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileE']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileE=NULL WHERE nomor='$data[nomor]'");
									endif;
									
									endwhile;
								   	
	
$idsiswa = $_POST['idsiswa'];
$siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$idsiswa'"));
$level = $siswa['level'];
$idsesi = $siswa['sesi'];	

$query = "SELECT * FROM ujian WHERE status='1' AND level='$level' AND sesi='$idsesi' ORDER BY tgl_ujian ";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
	
	  $where = [
      'id_bank' => $row['id_bank'],
      'id_siswa' => $idsiswa
      ];
	
if (date('Y-m-d', strtotime($row['tgl_selesai'])) >= date('Y-m-d') and date('Y-m-d', strtotime($row['tgl_ujian'])) <= date('Y-m-d')) :
  $nilai = fetch($koneksi, 'nilai', $where);
  $ceknilai = rowcount($koneksi, 'nilai', $where);
if ($ceknilai == '0') :
if (strtotime($row['tgl_ujian']) <= time() and time() <= strtotime($row['tgl_selesai'])) :
 $getdata[] = $row;
  endif;
  endif;
   endif;
  
 }
 
 echo json_encode($getdata);
 
mysqli_close($koneksi);
} else {
echo "0";
mysqli_close($koneksi);
}

}

if ($pg == 'jadwal_agama') {
 $no=0;
								$query = mysqli_query($koneksi, "SELECT * FROM soal "); 
								  while ($data = mysqli_fetch_array($query)) :
								$no++;
								 if($data['pilA']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilA=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilB']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilB=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilC']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilC=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['pilD']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilD=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['pilE']==''):
									mysqli_query($koneksi,"UPDATE soal SET pilE=NULL WHERE nomor='$data[nomor]'");
									endif;
									 if($data['perA']==''):
									mysqli_query($koneksi,"UPDATE soal SET perA=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['perB']==''):
									mysqli_query($koneksi,"UPDATE soal SET perB=NULL WHERE nomor='$data[nomor]'");
									endif;
								   if($data['perC']==''):
									mysqli_query($koneksi,"UPDATE soal SET perC=NULL WHERE nomor='$data[nomor]'");
									endif;
									if($data['perD']==''):
									mysqli_query($koneksi,"UPDATE soal SET perD=NULL WHERE nomor='$data[nomor]'");
									endif;
									if($data['perE']==''):
									mysqli_query($koneksi,"UPDATE soal SET perE=NULL WHERE nomor='$data[nomor]'");
									endif;
									
								   	if($data['file']==''):
									mysqli_query($koneksi,"UPDATE soal SET file=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['file1']==''):
									mysqli_query($koneksi,"UPDATE soal SET file1=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileA']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileA=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileB']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileB=NULL WHERE nomor='$data[nomor]'");
									endif;	
										if($data['fileC']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileC=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileD']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileD=NULL WHERE nomor='$data[nomor]'");
									endif;
										if($data['fileE']==''):
									mysqli_query($koneksi,"UPDATE soal SET fileE=NULL WHERE nomor='$data[nomor]'");
									endif;
									
									endwhile;
								   
$idsiswa = $_POST['idsiswa'];
$siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$idsiswa'"));
$level = $siswa['level'];
$idsesi = $siswa['sesi'];	
$agama = $siswa['agama'];

$query = "SELECT * FROM ujian WHERE status='1' AND level='$level' AND sesi='$idsesi' AND soal_agama='$agama' ORDER BY tgl_ujian ";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
	
	  $where = [
      'id_bank' => $row['id_bank'],
      'id_siswa' => $idsiswa
      ];
	
if (date('Y-m-d', strtotime($row['tgl_selesai'])) >= date('Y-m-d') and date('Y-m-d', strtotime($row['tgl_ujian'])) <= date('Y-m-d')) :
  $nilai = fetch($koneksi, 'nilai', $where);
  $ceknilai = rowcount($koneksi, 'nilai', $where);
if ($ceknilai == '0') :
if (strtotime($row['tgl_ujian']) <= time() and time() <= strtotime($row['tgl_selesai'])) :
 $getdata[] = $row;
  endif;
  endif;
   endif;
  
 }
 
 echo json_encode($getdata);
 
mysqli_close($koneksi);
} else {
echo "0";
mysqli_close($koneksi);
}

}

if ($pg == 'reset') {
$ids = $_POST['idsiswa'];
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM reset_andro WHERE idsiswa='$ids' AND sts='1'"));

if ($jumlah >=1) {
	echo"OK";
}else{
	echo"0";
}
}
if ($pg == 'mintareset') {
$ids = $_POST['idsiswa'];
$nama = $_POST['nama'];
$siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$ids'"));
$waktu = date('d M Y H:i:s');
mysqli_query($koneksi,"INSERT INTO reset_andro VALUES('','$ids','$nama','0','$waktu')");
}
if ($pg == 'hapusreset') {
$ids = $_POST['idsiswa'];

mysqli_query($koneksi,"DELETE FROM reset_andro WHERE idsiswa='$ids'");
echo"OK";
}
if ($pg == 'jumsoal') {
$bank = $_POST['idbank'];
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_bank='$bank'"));
echo $jumlah;
}

if ($pg == 'jawab') {
	$idsiswa = $_POST['idsiswa'];
$idsoal = $_POST['idsoal'];
$idbank = $_POST['idbank'];
$bj = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_soal='$idsoal'"));
$jenis = $bj['jenis'];
$jawab = $_POST['jawaban'];
$npsn = $_POST['npsn'];

$uji = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM ujian WHERE id_bank='$idbank'"));
$idu = $uji['id_ujian'];
$nilaiQ = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_ujian='$idu' AND id_bank='$idbank' AND id_siswa='$idsiswa'"));
if($nilaiQ >0){
	
}else{
mysqli_query($koneksi,"INSERT INTO nilai(id_ujian,id_bank,id_siswa,npsn,ujian_mulai,online) VALUES('$idu','$idbank','$idsiswa','$npsn','$datetime','1')");
}
$soal = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_bank='$idbank' AND id_soal='$idsoal'"));
$skorpg = $soal['max_skor'];
$mks = mysqli_fetch_array(mysqli_query($koneksi, "SELECT SUM(max_skor) AS totskor FROM soal WHERE id_bank='$idbank'"));
$nilai = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_bank='$idbank' AND id_siswa='$idsiswa'"));

if($jenis=='1'){

$cekdata = "SELECT * FROM jawaban WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	$exec = mysqli_query($koneksi, "UPDATE jawaban SET jawaban='$jawab' WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'");

	$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawaban WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='1' AND jawaban.id_bank='$idbank'"));
    $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET jml_benar='$jumlah',skor='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
echo "OK";
	mysqli_close($koneksi);
	
	}else{
$exec = mysqli_query($koneksi, "INSERT INTO jawaban (id_siswa,id_bank,id_soal,jenis,id_ujian,jawaban) VALUES ('$idsiswa','$idbank','$idsoal','1','$idu','$jawab')");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawaban WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='1' AND jawaban.id_bank='$idbank'"));
     $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET jml_benar='$jumlah',skor='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
echo "OK";
mysqli_close($koneksi);
	}
	
}	
if($jenis=='2'){

$cekdata = "SELECT * FROM jawaban WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	$exec = mysqli_query($koneksi, "UPDATE jawaban SET esai='$jawab' WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.esai WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='2' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_esai='$jumlah',skor_esai='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	echo "OK";
	mysqli_close($koneksi);
	
	}else{
$exec = mysqli_query($koneksi, "INSERT INTO jawaban (id_siswa,id_bank,id_soal,jenis,id_ujian,esai) VALUES ('$idsiswa','$idbank','$idsoal','2','$idu','$jawab')");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.esai WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='2' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_esai='$jumlah',skor_esai='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
echo "OK";
mysqli_close($koneksi);
	}
	
}	
if($jenis=='3'){
$jawab = substr($_POST['jawaban'], 0, -2); 
$cekdata = "SELECT * FROM jawaban WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	
	$exec = mysqli_query($koneksi, "UPDATE jawaban SET jawabmulti='$jawab' WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawabmulti WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='3' AND jawaban.id_bank='$idbank'"));
    $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_multi='$jumlah',skor_multi='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	echo "OK";
	
	}else{
$exec = mysqli_query($koneksi, "INSERT INTO jawaban (id_siswa,id_bank,id_soal,jenis,id_ujian,jawabmulti) VALUES ('$idsiswa','$idbank','$idsoal','3','$idu','$jawab')");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawabmulti WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='3' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_multi='$jumlah',skor_multi='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	echo "OK";
	}
	
}
if($jenis=='4'){
$jawab = substr($_POST['jawaban'], 0, -2); 
$cekdata = "SELECT * FROM jawaban WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	
	$exec = mysqli_query($koneksi, "UPDATE jawaban SET jawabbs='$jawab' WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawabbs WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='4' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_bs='$jumlah',skor_bs='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	
	echo "OK";
	}else{
$exec = mysqli_query($koneksi, "INSERT INTO jawaban (id_siswa,id_bank,id_soal,jenis,id_ujian,jawabbs) VALUES ('$idsiswa','$idbank','$idsoal','4','$idu','$jawab')");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawabbs WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='4' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_bs='$jumlah',skor_bs='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	echo "OK";
	}
	
}
if($jenis=='5'){
$jawab = substr($_POST['jawaban'], 0, -2); 
$cekdata = "SELECT * FROM jawaban WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'";
$jikaada = mysqli_query($koneksi,$cekdata);
if(mysqli_num_rows($jikaada)>0){
	
	$exec = mysqli_query($koneksi, "UPDATE jawaban SET jawaburut='$jawab' WHERE id_siswa='$idsiswa' AND id_soal='$idsoal'");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawaburut WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='5' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_urut='$jumlah',skor_urut='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	
	echo "OK";
	}else{
$exec = mysqli_query($koneksi, "INSERT INTO jawaban (id_siswa,id_bank,id_soal,jenis,id_ujian,jawaburut) VALUES ('$idsiswa','$idbank','$idsoal','5','$idu','$jawab')");
$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban JOIN soal ON soal.jawaban=jawaban.jawaburut WHERE jawaban.id_soal=soal.id_soal AND jawaban.id_siswa='$idsiswa' AND jawaban.jenis='5' AND jawaban.id_bank='$idbank'"));
      $skor = (($skorpg*$jumlah)/$mks['totskor'])*100;
	$total = number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']);
$exec = mysqli_query($koneksi, "UPDATE nilai SET benar_urut='$jumlah',skor_urut='$skor',total='$total' WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
	echo "OK";
	}
	
}
}

if ($pg == 'selesai') {
	$idsiswa = $_POST['idsiswa'];
	$idbank = $_POST['idbank'];
	$uji = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM ujian WHERE id_bank='$idbank'"));
     $idu = $uji['id_ujian'];
	$nilai = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_bank='$idbank' AND id_siswa='$idsiswa'"));
	$where = array(
    'id_bank' => $idbank,
    'id_siswa' => $idsiswa,
    'id_ujian' => $idu
);
	
$mapel = fetch($koneksi, 'banksoal', array('id_bank' => $idbank));
$siswa = fetch($koneksi, 'siswa', array('id_siswa' => $idsiswa));
$ceksoal = select($koneksi, 'soal', array('id_bank' => $idbank, 'jenis' => 1));
$ceksoalesai = select($koneksi, 'soal', array('id_bank' => $idbank, 'jenis' => 2));
$cekmulti = select($koneksi, 'soal', array('id_bank' => $idbank, 'jenis' => 3));
$cekbs = select($koneksi, 'soal', array('id_bank' => $idbank, 'jenis' => 4));
$cekurut = select($koneksi, 'soal', array('id_bank' => $idbank, 'jenis' => 5));

$arrayjawabesai = array();
foreach ($ceksoalesai as $getesai) {
    $w2 = array(
        'id_siswa' => $idsiswa,
        'id_bank' => $idbank,
        'id_soal' => $getesai['id_soal'],
        'jenis' => 2
    );
   
    $getjwb2 = fetch($koneksi, 'jawaban', $w2);
    if ($getjwb2) {
       
        $arrayjawabesai[$getesai['id_soal']] = $getjwb2['esai'];
    } else {
        $arrayjawabesai[$getesai['id_soal']] = 'X';
    }
	 
}
$arrayjawab = array();
foreach ($ceksoal as $getsoal) {
    $w = array(
        'id_siswa' => $idsiswa,
        'id_bank' => $idbank,
        'id_soal' => $getsoal['id_soal'],
        'jenis' => 1
    );
    $getjwb = fetch($koneksi, 'jawaban', $w);
    if ($getjwb) {
        $arrayjawab[$getsoal['id_soal']] = $getjwb['jawaban'];
    } else {
        $arrayjawab[$getsoal['id_soal']] = 'X';
    }
   
}

$arraymulti = array();
foreach ($cekmulti as $getmulti) {
    $m = array(
        'id_siswa' => $idsiswa,
        'id_bank' => $idbank,
        'id_soal' => $getmulti['id_soal'],
        'jenis' => 3
    );
    $getmt = fetch($koneksi, 'jawaban', $m);
    if ($getmulti) {
        $arraymulti[$getmulti['id_soal']] = $getmt['jawabmulti'];
    } else {
        $arraymulti[$getmulti['id_soal']] = 'X';
    }
    
}

$arraybs = array();
foreach ($cekbs as $getbs) {
    $b = array(
        'id_siswa' => $idsiswa,
        'id_bank' => $idbank,
        'id_soal' => $getbs['id_soal'],
        'jenis' => 4
    );
    $getb = fetch($koneksi, 'jawaban', $b);
    if ($getbs) {
        $arraybs[$getbs['id_soal']] = $getb['jawabbs'];
    } else {
        $arraybs[$getbs['id_soal']] = 'X';
    }
   
}
$arrayurut = array();
foreach ($cekurut as $geturut) {
    $u = array(
        'id_siswa' => $idsiswa,
        'id_bank' => $idbank,
        'id_soal' => $geturut['id_soal'],
        'jenis' => 5
    );
    $getut = fetch($koneksi, 'jawaban', $u);
    if ($geturut) {
        $arrayurut[$geturut['id_soal']] = $getut['jawaburut'];
    } else {
        $arrayurut[$geturut['id_soal']] = 'X';
    }
    
}

	$data = array(
    'ujian_selesai' => $datetime,
    'total' => number_format($nilai['skor']+$nilai['skor_esai']+$nilai['skor_multi']+$nilai['skor_bs']+$nilai['skor_urut']),
    'online' => 0,
    'jawaban' => serialize($arrayjawab),
    'jawaban_esai' => serialize($arrayjawabesai),
	'jawaban_multi' => serialize($arraymulti),
	'jawaban_bs' => serialize($arraybs),
	'jawaban_urut' => serialize($arrayurut),
	'server' =>$setting['id_server'],
	'browser'=>1
);
$simpan = update($koneksi, 'nilai', $data, $where);
mysqli_query($koneksi,"DELETE FROM jawaban WHERE id_siswa='$idsiswa' AND id_bank='$idbank'");
echo "OK";
}
if ($pg == 'nilai_ujian') {
	$ids = $_POST['idsiswa'];
	$query = "SELECT * FROM nilai JOIN banksoal ON banksoal.id_bank=nilai.id_bank WHERE nilai.id_siswa='$ids' AND nilai.status='1'";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
 $getdata[] = $row;
 }
 echo json_encode($getdata);
} else {
echo "0";
}
	
}

if ($pg == 'ambildata') {

$idb = $_POST['idbank'];

$query = "SELECT * FROM soal WHERE id_bank='$idb'";
$data = mysqli_query($koneksi, $query) or die(mysqli_error($koneksi));
$ResultData = mysqli_num_rows($data);

if ($ResultData > 0) {
$getdata = array();
 while($row =mysqli_fetch_assoc($data))
 {
 $getdata[] = $row;
 }
 echo json_encode($getdata);
} else {
echo "0";
}

}
	?>