493 lines
20 KiB
PHP
493 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exports\AllKelasExport;
|
|
use App\Exports\DetailKelasExport;
|
|
use App\Exports\PesertaEventExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DetailKelas;
|
|
use App\Models\Events;
|
|
use App\Models\FotoLogbooks;
|
|
use App\Models\Guru;
|
|
use App\Models\Kelas;
|
|
use App\Models\Logbooks;
|
|
use App\Models\Siswa;
|
|
use App\Models\Programs;
|
|
use App\Models\Ruangan;
|
|
use App\Models\Sekolah;
|
|
use App\Models\Sesi;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class KelasController extends Controller
|
|
{
|
|
public function index(Request $request){
|
|
$data = Kelas::withCount('siswa')
|
|
->with(['sekolah', 'ruanganFix', 'Programs'])
|
|
->when($request->filled('semester'), function ($q) use ($request) {
|
|
$q->where('semester', $request->semester);
|
|
})
|
|
->when($request->filled('tahun'), function ($q) use ($request) {
|
|
$q->where('tahun_ajaran', $request->tahun);
|
|
})
|
|
->when($request->filled('is_ekstrakurikuler'), function ($q) use ($request) {
|
|
$q->where('is_ekstrakurikuler', $request->is_ekstrakurikuler);
|
|
})
|
|
->when($request->filled('program_id'), function ($q) use ($request) {
|
|
$q->where('program_id', $request->program_id);
|
|
})
|
|
->orderByRaw("CASE
|
|
WHEN status = 'Aktif' THEN 1
|
|
WHEN status = 'Tidak Aktif' THEN 2
|
|
ELSE 3 END")
|
|
->get();
|
|
|
|
$semesters = Kelas::whereNotNull('semester')->distinct()->pluck('semester');
|
|
$years = Kelas::whereNotNull('tahun_ajaran')->distinct()->pluck('tahun_ajaran');
|
|
$programs = Programs::all();
|
|
|
|
return view('admin.pages.kelas', compact('data', 'semesters', 'years', 'programs'));
|
|
}
|
|
public function create()
|
|
{
|
|
$programs = Programs::all();
|
|
$guru = Guru::where('status', 'Aktif')->select('id', 'nama')->get();
|
|
$sekolahList = Sekolah::where('is_active', 1)->orderBy('nama_sekolah', 'asc')->get();
|
|
$ruanganList = Ruangan::where('status', 'aktif')->orderBy('nama_ruangan', 'asc')->get();
|
|
return view ('admin.pages.kelas-tambah', compact('programs', 'guru', 'sekolahList', 'ruanganList'));
|
|
}
|
|
public function checkScheduleConflict(Request $request)
|
|
{
|
|
if ($request->is_ekstrakurikuler != 1) {
|
|
return response()->json(['conflict' => false]);
|
|
}
|
|
|
|
$ruanganId = $request->ruangan_fix_id;
|
|
$hari = $request->hari_fix;
|
|
$sesi = $request->sesi_fix;
|
|
$semester = trim($request->semester ?? '');
|
|
$tahunAjaran = trim($request->tahun_ajaran ?? '');
|
|
$excludeId = $request->kelas_id;
|
|
|
|
if (!$ruanganId || !$hari || !$sesi) {
|
|
return response()->json(['conflict' => false]);
|
|
}
|
|
|
|
$query = Kelas::where('is_ekstrakurikuler', 1)
|
|
->where('ruangan_fix_id', $ruanganId)
|
|
->where('hari_fix', $hari)
|
|
->where('sesi_fix', $sesi)
|
|
->where('status', 'Aktif');
|
|
|
|
if (!empty($semester)) {
|
|
$query->where('semester', $semester);
|
|
}
|
|
if (!empty($tahunAjaran)) {
|
|
$query->where('tahun_ajaran', $tahunAjaran);
|
|
}
|
|
|
|
if ($excludeId) {
|
|
$query->where('id', '!=', $excludeId);
|
|
}
|
|
|
|
$conflicting = $query->first();
|
|
|
|
if ($conflicting) {
|
|
return response()->json([
|
|
'conflict' => true,
|
|
'nama_kelas' => $conflicting->nama_kelas,
|
|
'message' => "Ruangan, Hari, dan Sesi telah digunakan oleh kelas ekstrakurikuler <b>{$conflicting->nama_kelas}</b>. Silahkan pilih ruangan, hari, atau sesi yang berbeda"
|
|
]);
|
|
}
|
|
|
|
return response()->json(['conflict' => false]);
|
|
}
|
|
|
|
public function store(Request $request){
|
|
$validated = $request->validate(
|
|
[
|
|
'nama_kelas' => 'required|string|max:255',
|
|
'jenis_kelas' => 'required',
|
|
'program_id' => 'required',
|
|
'harga_kelas' => 'required',
|
|
'status' => 'required',
|
|
'semester' => 'nullable',
|
|
'tahun_ajaran' => 'nullable',
|
|
'frekuensi_per_minggu' => 'required|integer|in:1,2,3',
|
|
'is_ekstrakurikuler' => 'required|in:0,1',
|
|
'sekolah_id' => 'required_if:is_ekstrakurikuler,1|nullable|exists:sekolah,id',
|
|
'hari_fix' => 'required_if:is_ekstrakurikuler,1|nullable|integer|between:1,6',
|
|
'sesi_fix' => 'required_if:is_ekstrakurikuler,1|nullable|integer|between:1,5',
|
|
'ruangan_fix_id' => 'required_if:is_ekstrakurikuler,1|nullable|exists:ruangan,id',
|
|
],
|
|
[
|
|
'nama_kelas.required' => 'Nama kelas wajib diisi',
|
|
'nama_kelas.max' => 'Nama kelas terlalu panjang',
|
|
'jenis_kelas.required' => 'Total pertemuan wajib diisi',
|
|
'program_id.required' => 'Program wajib diisi',
|
|
'harga_kelas.required' => 'Harga kelas wajib diisi',
|
|
'status.required' => 'Status kelas wajib diisi',
|
|
'frekuensi_per_minggu.required' => 'Frekuensi per minggu wajib diisi',
|
|
'is_ekstrakurikuler.required' => 'Tipe kelas wajib dipilih',
|
|
'sekolah_id.required_if' => 'Sekolah wajib dipilih untuk kelas ekstrakurikuler',
|
|
'hari_fix.required_if' => 'Hari fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
'sesi_fix.required_if' => 'Sesi fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
'ruangan_fix_id.required_if' => 'Ruangan fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
]
|
|
);
|
|
|
|
if ($validated['is_ekstrakurikuler'] == 0) {
|
|
$validated['sekolah_id'] = null;
|
|
$validated['hari_fix'] = null;
|
|
$validated['sesi_fix'] = null;
|
|
$validated['ruangan_fix_id'] = null;
|
|
} else if ($validated['status'] === 'Aktif') {
|
|
$conflictQuery = Kelas::where('is_ekstrakurikuler', 1)
|
|
->where('ruangan_fix_id', $validated['ruangan_fix_id'])
|
|
->where('hari_fix', $validated['hari_fix'])
|
|
->where('sesi_fix', $validated['sesi_fix'])
|
|
->where('status', 'Aktif');
|
|
|
|
if (!empty($validated['semester'])) {
|
|
$conflictQuery->where('semester', $validated['semester']);
|
|
}
|
|
if (!empty($validated['tahun_ajaran'])) {
|
|
$conflictQuery->where('tahun_ajaran', $validated['tahun_ajaran']);
|
|
}
|
|
|
|
$conflicting = $conflictQuery->first();
|
|
if ($conflicting) {
|
|
return redirect()->back()
|
|
->withErrors(['ruangan_fix_id' => "Ruangan, Hari, dan Sesi Fix telah digunakan oleh kelas ekstrakurikuler '{$conflicting->nama_kelas}'."])
|
|
->withInput();
|
|
}
|
|
}
|
|
|
|
$validated['harga_kelas'] = (int) str_replace('.', '', $validated['harga_kelas']);
|
|
$validated['kode_kelas'] = strtoupper(Str::random(6));
|
|
|
|
if ($request->keterangan){ $validated['keterangan'] = $request->keterangan; }
|
|
|
|
Kelas::create($validated);
|
|
return redirect('/admin/kelas')->with('success', 'Data Berhasil Disimpan');
|
|
}
|
|
|
|
public function show($id){
|
|
$data = Kelas::with(['sekolah', 'ruanganFix'])->where('id', $id)->first();
|
|
$programs = Programs::all();
|
|
$guru = Guru::where('status', 'Aktif')->select('id', 'nama')->get();
|
|
$sekolahList = Sekolah::where('is_active', 1)->orderBy('nama_sekolah', 'asc')->get();
|
|
$ruanganList = Ruangan::where('status', 'aktif')->orderBy('nama_ruangan', 'asc')->get();
|
|
|
|
if ($data == null){
|
|
return redirect('/admin/kelas')->with('error', 'Data kelas tidak ditemukan');
|
|
}
|
|
$peserta = DetailKelas::has('siswa')->where('kelas_id', $id)->get();
|
|
$logbook = Logbooks::with('Sesi')->where('kelas_id', $id)->orderBy('tanggal', 'asc')->get();
|
|
$sesiList = Sesi::orderBy('id', 'asc')->get();
|
|
|
|
return view('admin.pages.kelas-detail', compact('data', 'peserta', 'programs', 'logbook', 'guru', 'sekolahList', 'ruanganList', 'sesiList'));
|
|
}
|
|
|
|
public function update(Request $request, $id){
|
|
$data = Kelas::findOrFail($id);
|
|
$validated = $request->validate(
|
|
[
|
|
'nama_kelas' => 'required|string|max:255',
|
|
'jenis_kelas' => 'required',
|
|
'program_id' => 'required',
|
|
'harga_kelas' => 'required',
|
|
'status' => 'required',
|
|
'semester' => 'nullable',
|
|
'tahun_ajaran' => 'nullable',
|
|
'frekuensi_per_minggu' => 'required|integer|in:1,2,3',
|
|
'is_ekstrakurikuler' => 'required|in:0,1',
|
|
'sekolah_id' => 'required_if:is_ekstrakurikuler,1|nullable|exists:sekolah,id',
|
|
'hari_fix' => 'required_if:is_ekstrakurikuler,1|nullable|integer|between:1,6',
|
|
'sesi_fix' => 'required_if:is_ekstrakurikuler,1|nullable|integer|between:1,5',
|
|
'ruangan_fix_id' => 'required_if:is_ekstrakurikuler,1|nullable|exists:ruangan,id',
|
|
],
|
|
[
|
|
'nama_kelas.required' => 'Nama kelas wajib diisi',
|
|
'nama_kelas.max' => 'Nama kelas terlalu panjang',
|
|
'jenis_kelas.required' => 'Total pertemuan wajib diisi',
|
|
'program_id.required' => 'Program wajib diisi',
|
|
'harga_kelas.required' => 'Harga kelas wajib diisi',
|
|
'status.required' => 'Status kelas wajib diisi',
|
|
'frekuensi_per_minggu.required' => 'Frekuensi per minggu wajib diisi',
|
|
'is_ekstrakurikuler.required' => 'Tipe kelas wajib dipilih',
|
|
'sekolah_id.required_if' => 'Sekolah wajib dipilih untuk kelas ekstrakurikuler',
|
|
'hari_fix.required_if' => 'Hari fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
'sesi_fix.required_if' => 'Sesi fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
'ruangan_fix_id.required_if' => 'Ruangan fix wajib dipilih untuk kelas ekstrakurikuler',
|
|
]
|
|
);
|
|
|
|
if ($validated['is_ekstrakurikuler'] == 0) {
|
|
$validated['sekolah_id'] = null;
|
|
$validated['hari_fix'] = null;
|
|
$validated['sesi_fix'] = null;
|
|
$validated['ruangan_fix_id'] = null;
|
|
} else if ($validated['status'] === 'Aktif') {
|
|
$conflictQuery = Kelas::where('is_ekstrakurikuler', 1)
|
|
->where('ruangan_fix_id', $validated['ruangan_fix_id'])
|
|
->where('hari_fix', $validated['hari_fix'])
|
|
->where('sesi_fix', $validated['sesi_fix'])
|
|
->where('status', 'Aktif')
|
|
->where('id', '!=', $id);
|
|
|
|
if (!empty($validated['semester'])) {
|
|
$conflictQuery->where('semester', $validated['semester']);
|
|
}
|
|
if (!empty($validated['tahun_ajaran'])) {
|
|
$conflictQuery->where('tahun_ajaran', $validated['tahun_ajaran']);
|
|
}
|
|
|
|
$conflicting = $conflictQuery->first();
|
|
if ($conflicting) {
|
|
return redirect()->back()
|
|
->withErrors(['ruangan_fix_id' => "Ruangan, Hari, dan Sesi Fix telah digunakan oleh kelas ekstrakurikuler '{$conflicting->nama_kelas}'."])
|
|
->withInput();
|
|
}
|
|
}
|
|
|
|
if($request->status_selesai){
|
|
$validated['status_selesai'] = 'Selesai';
|
|
}else{
|
|
$validated['status_selesai'] = 'Belum';
|
|
}
|
|
|
|
$validated['harga_kelas'] = (int) str_replace('.', '', $validated['harga_kelas']);
|
|
|
|
if ($request->keterangan){ $validated['keterangan'] = $request->keterangan; }
|
|
|
|
$data->update($validated);
|
|
|
|
$direct = '/admin/kelas/' .$id;
|
|
return redirect($direct)->with('success', 'Data Kelas Berhasil Diubah');
|
|
}
|
|
|
|
public function getSiswasKelas($kelas_id){
|
|
$pesertaTerdaftar = DetailKelas::where('kelas_id', $kelas_id)->pluck('siswa_id')->toArray();
|
|
|
|
$siswa = Siswa::whereNotIn('id', $pesertaTerdaftar)->get();
|
|
|
|
return response()->json([$siswa]);
|
|
}
|
|
|
|
public function tambahSiswas(Request $request)
|
|
{
|
|
$validated = $request->validate(
|
|
[
|
|
'kelas_id' => 'required',
|
|
'id' => 'required',
|
|
],
|
|
[
|
|
'kelas_id.required' => 'Event tidak ditemukan',
|
|
'id.required' => 'Silahkan pilih setidaknya satu siswa',
|
|
]
|
|
);
|
|
|
|
$datas = [];
|
|
|
|
$kelas = Kelas::where('id', $request->kelas_id)->select('harga_kelas')->first();
|
|
|
|
for ($i = 0; $i < count($request->id); $i++) {
|
|
$siswa = Siswa::where('id', $request->id[$i])->select('id', 'nama_anak')->first();
|
|
$cekPeserta = DetailKelas::where('siswa_id', $request->id[$i])->where('kelas_id', $request->kelas_id)->count();
|
|
if ($cekPeserta == 0) {
|
|
if ($siswa != null) {
|
|
DetailKelas::create([
|
|
'kelas_id' => $request->kelas_id,
|
|
'siswa_id' => $request->id[$i],
|
|
'nama_anak' => $siswa->nama_anak,
|
|
'status_siswa' => "Aktif",
|
|
'harga_kelas' => $kelas->harga_kelas,
|
|
]);
|
|
}else{
|
|
$direct = '/admin/kelas/' . $request->kelas_id;
|
|
$message = "Gagal menyimpan siswa, id " . $request->id[$i] . " tidak ditemukan";
|
|
return redirect($direct)->with('error', $message);
|
|
}
|
|
}else{
|
|
$direct = '/admin/kelas/' . $request->kelas_id;
|
|
$message = "Anak dengan nama " . $siswa->nama_anak . " telah terdaftar di kelas ini";
|
|
return redirect($direct)->with('error', $message);
|
|
}
|
|
}
|
|
|
|
$direct = '/admin/kelas/' . $request->kelas_id;
|
|
$message = "Siswa berhasil ditambahkan pada kelas";
|
|
return redirect($direct)->with('success', $message);
|
|
}
|
|
|
|
public function getPesertaKelas($id){
|
|
$data = DetailKelas::where('id', $id)->first();
|
|
$peserta = Siswa::where('id', $data->siswa_id)->first();
|
|
// $user = User::where('id', $data->user_id)->select('id', 'email')->first();
|
|
|
|
$created_at = Carbon::parse($data->created_at)->locale('id')->translatedFormat('l, d F Y - H:i') . ' WIB';
|
|
|
|
return response()->json([
|
|
'id' => $data->id,
|
|
'nama_anak' => $peserta->nama_anak,
|
|
'nama_orangtua' => $peserta->nama_orangtua,
|
|
'no_hp_orangtua' => $peserta->no_hp_orangtua,
|
|
'usia' => $peserta->usia ?? null,
|
|
'jenis_kelamin' => $peserta->jenis_kelamin ?? null,
|
|
'email' => $user->email ?? null,
|
|
'asal_sekolah' => $peserta->asal_sekolah ?? null,
|
|
'program_id'=> $peserta->program_id ?? null,
|
|
'status' => $data->status_siswa ?? null,
|
|
'alamat' => $peserta->alamat ?? null,
|
|
'bukti_pembayaran' => $data->bukti_pembayaran ?? null,
|
|
'harga_kelas' => $data->harga_kelas,
|
|
'catatan' => $data->catatan,
|
|
'tanggal_daftar' => $created_at ?? null,
|
|
|
|
'foto' => $peserta->foto ?? null,
|
|
]);
|
|
}
|
|
|
|
public function editSiswa(Request $request, $id){
|
|
|
|
$data = DetailKelas::where('id', $id)->first();
|
|
$validated = $request->validate(
|
|
[
|
|
'nama_anak' => 'required',
|
|
'status_siswa' => 'required',
|
|
],
|
|
[
|
|
'nama_anak.required' => 'Nama anak tidak ditemukan',
|
|
'status_siswa.required' => 'Harap pilih status siswa terlebih dahulu',
|
|
]
|
|
);
|
|
|
|
if ($request->catatan !== null || $request->catatan !== ''){ $validated['catatan'] = $request->catatan; }
|
|
|
|
if ($request->hasFile('bukti_pembayaran')) {
|
|
if ($data->bukti_pembayaran) {
|
|
$imagePath = storage_path('app/public/' . $data->bukti_pembayaran);
|
|
if (file_exists($imagePath)) {
|
|
unlink($imagePath);
|
|
}
|
|
}
|
|
|
|
$file = $request->file('bukti_pembayaran');
|
|
|
|
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
|
|
|
|
$file->storeAs('bukti-pembayaran', $hashedName, 'public');
|
|
|
|
$validated['bukti_pembayaran'] = 'bukti-pembayaran/' . $hashedName;
|
|
|
|
DetailKelas::where('id', $id)->update([
|
|
'nama_anak' => $validated['nama_anak'],
|
|
'status_siswa' => $validated['status_siswa'],
|
|
'bukti_pembayaran' => $validated['bukti_pembayaran'],
|
|
'catatan' => $validated['catatan'],
|
|
]);
|
|
}else{
|
|
DetailKelas::where('id', $id)->update([
|
|
'nama_anak' => $validated['nama_anak'],
|
|
'status_siswa' => $validated['status_siswa'],
|
|
'catatan' => $validated['catatan'],
|
|
]);
|
|
}
|
|
|
|
$direct = '/admin/kelas/' . $request->kelas_id;
|
|
$message = "Data siswa dikelas berhasil diubah";
|
|
return redirect($direct)->with('success', $message);
|
|
}
|
|
|
|
public function destroyerSiswaFromKelas($id){
|
|
$data = DetailKelas::findOrFail($id);
|
|
$kelas_id = $data->kelas_id;
|
|
|
|
if ($data->bukti_pembayaran) {
|
|
$imagePath = storage_path('app/public/' . $data->bukti_pembayaran);
|
|
if (file_exists($imagePath)) {
|
|
unlink($imagePath);
|
|
}
|
|
}
|
|
|
|
$data->delete();
|
|
$direct = '/admin/kelas/' . $kelas_id;
|
|
return redirect($direct)->with('success', 'Data siswa berhasil dihapus');
|
|
}
|
|
public function destroy($id){
|
|
$data = Kelas::findOrFail($id);
|
|
|
|
$detailkelas = DetailKelas::where('kelas_id', $id)->get();
|
|
|
|
if($detailkelas->count() > 0){
|
|
for($i = 0; $i < count($detailkelas); $i++){
|
|
$detailkelas[$i]->delete();
|
|
}
|
|
}
|
|
$logbooks = Logbooks::where('kelas_id', $id)->get();
|
|
|
|
if($logbooks->count() > 0){
|
|
for($i = 0; $i < count($logbooks); $i++){
|
|
$logbooks_id = $logbooks[$i]->id;
|
|
|
|
$fotoLogbook = FotoLogbooks::where('logbook_id', $logbooks_id)->get();
|
|
if($fotoLogbook->count() > 0){
|
|
for($b = 0; $b < count($fotoLogbook); $b++){
|
|
if ($fotoLogbook[$b]->foto) {
|
|
$imagePath = storage_path('app/public/' . $fotoLogbook[$b]->foto);
|
|
if (file_exists($imagePath)) {
|
|
unlink($imagePath);
|
|
}
|
|
}
|
|
$fotoLogbook[$b]->delete();
|
|
}
|
|
}
|
|
|
|
$logbooks[$i]->delete();
|
|
}
|
|
}
|
|
|
|
$data->delete();
|
|
return redirect('/admin/kelas')->with('success', 'Data kelas berhasil dihapus');
|
|
}
|
|
|
|
public function exportExcel($id)
|
|
{
|
|
$namaKelas = Kelas::select('nama_kelas')->where('id', $id)->first();
|
|
|
|
$safeNamaKelas = preg_replace('/[\/\\\\]/', '-', $namaKelas->nama_kelas);
|
|
|
|
$fileName = 'Siswa ' . $safeNamaKelas . '.xlsx';
|
|
|
|
return Excel::download(new DetailKelasExport($id), $fileName);
|
|
}
|
|
|
|
public function exportExcelAll($year){
|
|
$fileName = "Kelas Robotku" . ($year !== "all" ? " $year" : "") . ".xlsx";
|
|
|
|
return Excel::download(new AllKelasExport($year), $fileName);
|
|
}
|
|
|
|
public function exportExcelFilter(Request $request)
|
|
{
|
|
$fileName = "Logbook Kelas Robotku " . date('Y-m-d') . ".xlsx";
|
|
return Excel::download(new \App\Exports\LogbookKelasMultiExport($request), $fileName);
|
|
}
|
|
|
|
public function exportLogbookFilter(Request $request)
|
|
{
|
|
$fileName = "Logbook Kelas Robotku " . date('Y-m-d') . ".xlsx";
|
|
return Excel::download(new \App\Exports\LogbookKelasMultiExport($request), $fileName);
|
|
}
|
|
|
|
public function exportSiswaFilter(Request $request)
|
|
{
|
|
$fileName = "Data Kelas & Siswa Robotku " . date('Y-m-d') . ".xlsx";
|
|
return Excel::download(new \App\Exports\FilteredKelasExport($request), $fileName);
|
|
}
|
|
}
|