<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
require("../../config/dis.php");
(isset($_SESSION['id_user'])) ? $id_user = $_SESSION['id_user'] : $id_user = 0;
($id_user == 0) ? header('location:sig-in.php') : null;
echo "<style> .str{ mso-number-format:\@; } </style>";
$id_kelas = $_POST['kelas'];
$user = fetch($koneksi, 'users', array('id_user' => $id_user));
$mapel = fetch($koneksi, 'mapel', array('id_bank' => null));
$kelas = fetch($koneksi, 'kelas', array('id_kelas' => $id_kelas));

if (date('m') >= 7 and date('m') <= 12) :
	$ajaran = date('Y') . "/" . (date('Y') + 1);
elseif (date('m') >= 1 and date('m') <= 6) :
	$ajaran = (date('Y') - 1) . "/" . date('Y');
endif;

$file = "REKAP NILAI KELAS " . $kelas['nama'];
$file = str_replace(" ", "_", $file);
$file = str_replace(":", "", $file);
header("Content-type: application/octet-stream");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Disposition: attachment; filename=" . $file . ".xls");
?>
REKAP NILAI HASIL UJIAN
<br>
<?= $setting['sekolah'] ?>
<table border='1'>
	<thead>
		<tr>
			<th rowspan='3' style="vertical-align:middle; width:5px">#</th>
			<th style='text-align:center;vertical-align:middle' rowspan='3'>No Peserta</th>
			<th style='text-align:center;vertical-align:middle' rowspan='3'>Nama Peserta</th>
			<th style='text-align:center;vertical-align:middle' rowspan='3'>Kelas</th>
			<?php
			$kodeujian = mysqli_fetch_array(mysqli_query($koneksi, "select * from jenis where status='aktif'"));
			$mapelQ = mysqli_query($koneksi, "SELECT * FROM banksoal a JOIN nilai b ON a.id_bank=b.id_bank where b.kode_ujian='$kodeujian[id_jenis]' GROUP BY b.id_bank ");
			while ($mapel = mysqli_fetch_array($mapelQ)) :
				echo "<th style='text-align:center' colspan='3'>$mapel[nama]</th>";
			endwhile;
			?>
		</tr>
		<tr>
			<?php
			$kode = mysqli_query($koneksi, "SELECT * FROM banksoal a JOIN nilai b ON a.id_bank=b.id_bank where b.kode_ujian='$kodeujian[id_jenis]' GROUP BY b.id_bank");
			while ($mapel = mysqli_fetch_array($kode)) :
				echo "<th style='text-align:center' colspan='3'>$mapel[kode_ujian]</th>";
			endwhile;
			?>
		</tr>
		<tr>
			<?php $mapelQ = mysqli_query($koneksi, "SELECT * FROM banksoal a JOIN nilai b ON a.id_bank=b.id_bank where b.kode_ujian='$kodeujian[id_jenis]' GROUP BY b.id_bank "); ?>
			<?php while ($mapel = mysqli_fetch_array($mapelQ)) : ?>
			<?php if($menu['soal']==0){?>
			<th style='text-align:center'>B</th>
				<th style='text-align:center'>S</th>
				<th style='text-align:center'>SKOR</th>
			<?php }else{ ?>
			<th style='text-align:center'>PG-B</th>
			<th style='text-align:center'>MT-B</th>
			<th style='text-align:center'>BS-B</th>
			<th style='text-align:center'>JD-B</th>
			<th style='text-align:center'>ES-B</th>
				<th style='text-align:center'>PG-S</th>
				<th style='text-align:center'>MT-S</th>
				<th style='text-align:center'>BS-S</th>
				<th style='text-align:center'>JD-S</th>
				<th style='text-align:center'>-S</th>
				<th style='text-align:center'>SKOR</th>
			<?php } ?>
			<?php endwhile; ?>
		</tr>
	</thead>
	<tbody>
		<?php $siswaQ = mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_kelas='$id_kelas' ORDER BY nama ASC"); ?>
		<?php while ($siswa = mysqli_fetch_array($siswaQ)) : ?>
			<?php
			$no++;
			$ket = '';
			$esai = $lama = $jawaban = $skor = $total = '--';
			?>
			<tr>
				<td><?= $no ?></td>
				<td style="text-align:center"><?= $siswa['no_peserta'] ?></td>
				<td><?= $siswa['nama'] ?></td>
				<td style="text-align:center"><?= $siswa['id_kelas'] ?></td>
				<?php $mapelQ = mysqli_query($koneksi, "SELECT * FROM banksoal a JOIN nilai b ON a.id_bank=b.id_bank where b.kode_ujian='$kodeujian[id_jenis]' GROUP BY b.id_bank "); ?>
				<?php while ($mapel = mysqli_fetch_array($mapelQ)) : ?>
					<?php
					$nilaiQ = mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_bank='$mapel[id_bank]' AND id_siswa='$siswa[id_siswa]' ");
					$nilaiC = mysqli_num_rows($nilaiQ);
					$nilai = mysqli_fetch_array($nilaiQ);
					?>
					<?php if($menu['soal']==0){?>
					<td style="text-align:center"><?= $nilai['jml_benar'] ?></td>
					<td style="text-align:center"><?= $nilai['jml_salah'] ?></td>
					<td style="text-align:center" class='str'><?= $nilai['skor'] ?></td>
					<?php }else{ ?>
		        	<td style="text-align:center"><?= $nilai['jml_benar'] ?></td>
					<td style="text-align:center"><?= $nilai['benar_multi'] ?></td>
					<td style="text-align:center"><?= $nilai['benar_bs'] ?></td>
					<td style="text-align:center"><?= $nilai['benar_urut'] ?></td>
					<td style="text-align:center"><?= $nilai['benar_esay'] ?></td>
					<td style="text-align:center"><?= $nilai['jml_salah'] ?></td>
					<td style="text-align:center"><?= $nilai['salah_multi'] ?></td>
					<td style="text-align:center"><?= $nilai['salah_bs'] ?></td>
					<td style="text-align:center"><?= $nilai['salah_urut'] ?></td>
					<td style="text-align:center"><?= $nilai['salah_esay'] ?></td>
					<td style="text-align:center" class='str'><?= $nilai['total'] ?></td>
		        	<?php } ?>
				<?php endwhile; ?>
			</tr>
		<?php endwhile; ?>
	</tbody>
</table>