247 lines
8.1 KiB
PHP
247 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Sekolah;
|
|
use App\Models\Siswa;
|
|
use App\Models\DetailKelas;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SekolahController extends Controller
|
|
{
|
|
public function inputData(){
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$pesertas = Siswa::whereNull('sekolah_id')
|
|
->whereNotNull('asal_sekolah')
|
|
->get();
|
|
|
|
foreach ($pesertas as $peserta) {
|
|
$asal = trim($peserta->asal_sekolah);
|
|
|
|
// Cek apakah sekolah sudah ada
|
|
$sekolah = Sekolah::where('nama_sekolah', $asal)->first();
|
|
|
|
if (!$sekolah) {
|
|
// Kalau belum ada, buat baru
|
|
$sekolah = Sekolah::create([
|
|
'nama_sekolah' => $asal,
|
|
'status_kerja_sama' => 'Tidak Ada',
|
|
]);
|
|
}
|
|
|
|
// Update peserta dengan sekolah_id
|
|
$peserta->update([
|
|
'sekolah_id' => $sekolah->id
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
return response()->json(['message' => 'Data sekolah berhasil dihubungkan dengan peserta.']);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = Sekolah::withCount(['siswa as total_siswa'])
|
|
->where('id', '!=', 0)
|
|
->get()
|
|
->map(function ($item) {
|
|
$siswaIds = Siswa::where('sekolah_id', $item->id)->pluck('id');
|
|
|
|
// Hitung jumlah siswa yang ikut kelas
|
|
$siswaKelas = DetailKelas::whereIn('siswa_id', $siswaIds)
|
|
->distinct('siswa_id')
|
|
->count('siswa_id');
|
|
|
|
$item->siswa_kelas = $siswaKelas;
|
|
return $item;
|
|
})
|
|
->sortByDesc('total_siswa')
|
|
->values();
|
|
|
|
return view('admin.pages.sekolah', compact('data'));
|
|
}
|
|
|
|
public function getDataSekolahCount(){
|
|
$data = Sekolah::all();
|
|
|
|
return response()->json([
|
|
'Semua' => $data->count(),
|
|
'Aktif' => $data->where('status_kerja_sama', 'Aktif')->count(),
|
|
'Dalam Pengajuan' => $data->where('status_kerja_sama', 'Dalam Pengajuan')->count(),
|
|
'Tidak Ada' => $data->where('status_kerja_sama', 'Tidak Ada')->count(),
|
|
'Berakhir' => $data->where('status_kerja_sama', 'Berakhir')->count(),
|
|
'Tidak Tertarik' => $data->where('status_kerja_sama', 'Tidak Tertarik')->count(),
|
|
]
|
|
);
|
|
}
|
|
|
|
public function getDataSekolah($sekolah_id){
|
|
$sekolah = Sekolah::withCount(['siswa as total_siswa'])
|
|
->where('id', '!=', 0)
|
|
->get()
|
|
->map(function ($item) {
|
|
$siswaIds = Siswa::where('sekolah_id', $item->id)->pluck('id');
|
|
|
|
$siswaKelas = DetailKelas::whereIn('siswa_id', $siswaIds)
|
|
->distinct('siswa_id')
|
|
->count('siswa_id');
|
|
|
|
$item->siswa_kelas = $siswaKelas;
|
|
|
|
return $item;
|
|
})
|
|
->where('id', '!=', $sekolah_id)
|
|
->sortByDesc('total_siswa')
|
|
->values();
|
|
|
|
return response()->json([$sekolah]);
|
|
}
|
|
|
|
public function pindahSekolah(Request $request)
|
|
{
|
|
$request->validate([
|
|
'siswa_ids' => 'required|array',
|
|
'sekolah_id' => 'required|integer|exists:sekolah,id',
|
|
]);
|
|
|
|
Siswa::whereIn('id', $request->siswa_ids)
|
|
->update(['sekolah_id' => $request->sekolah_id]);
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function create(){
|
|
return view('admin.pages.create-sekolah');
|
|
}
|
|
|
|
public function store(Request $request){
|
|
$validated = $request->validate(
|
|
[
|
|
'nama_sekolah' => 'required',
|
|
'jenis_lembaga' => 'required',
|
|
'email' => 'nullable',
|
|
'alamat_sekolah' => 'nullable',
|
|
'provinsi' => 'nullable',
|
|
'kota' => 'nullable',
|
|
'status_kerja_sama' => 'nullable',
|
|
],
|
|
[
|
|
'nama_sekolah.required' => 'Nama sekolah wajib diisi',
|
|
'jenis_lembaga.required' => 'jenis lembaga wajib dipilih',
|
|
]
|
|
);
|
|
if ($request->no_telp !== null || $request->no_telp !== ''){
|
|
$str = ltrim($request->no_telp, '0');
|
|
$validated['no_telp'] = $str;
|
|
}
|
|
|
|
if ($request->hasFile('logo')) {
|
|
$file = $request->file('logo');
|
|
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
|
|
$file->storeAs('sekolah', $hashedName, 'public');
|
|
$validated['logo'] = 'sekolah/' . $hashedName;
|
|
}
|
|
|
|
$validated['latitude'] = 0;
|
|
$validated['longitude'] = 0;
|
|
|
|
try {
|
|
Sekolah::create($validated);
|
|
return redirect('/admin/sekolah')->with('success', 'Data Berhasil Disimpan');
|
|
}catch (QueryException $e){
|
|
return redirect('/admin/sekolah')->with('error', 'Data Gagal Disimpan');
|
|
}
|
|
}
|
|
|
|
public function edit($id){
|
|
$data = Sekolah::findOrFail($id);
|
|
|
|
$siswa = Siswa::where('sekolah_id', $id)->get();
|
|
|
|
$siswaIds = $siswa->pluck('id');
|
|
|
|
$kelasResume = DB::table('detail_kelas')
|
|
->join('kelas', 'detail_kelas.kelas_id', '=', 'kelas.id')
|
|
->whereIn('detail_kelas.siswa_id', $siswaIds)
|
|
->select(
|
|
'kelas.id',
|
|
'kelas.nama_kelas',
|
|
DB::raw('COUNT(DISTINCT detail_kelas.siswa_id) as total_siswa')
|
|
)
|
|
->groupBy('kelas.id', 'kelas.nama_kelas')
|
|
->orderByDesc('total_siswa')
|
|
->get();
|
|
|
|
return view('admin.pages.edit-sekolah', compact('data', 'siswa', 'kelasResume'));
|
|
}
|
|
|
|
public function update(Request $request, $id){
|
|
$data = Sekolah::findOrFail($id);
|
|
$validated = $request->validate(
|
|
[
|
|
'nama_sekolah' => 'required',
|
|
'jenis_lembaga' => 'required',
|
|
'email' => 'nullable',
|
|
'alamat_sekolah' => 'nullable',
|
|
'provinsi' => 'nullable',
|
|
'kota' => 'nullable',
|
|
'status_kerja_sama' => 'nullable',
|
|
],
|
|
[
|
|
'nama_sekolah.required' => 'Nama sekolah wajib diisi',
|
|
'jenis_lembaga.required' => 'jenis lembaga wajib dipilih',
|
|
]
|
|
);
|
|
if ($request->no_telp !== null || $request->no_telp !== ''){
|
|
$str = ltrim($request->no_telp, '0');
|
|
$validated['no_telp'] = $str;
|
|
}
|
|
|
|
if ($request->hasFile('logo')) {
|
|
if ($data->logo) {
|
|
$imagePath = storage_path('app/public/' . $data->logo);
|
|
if (file_exists($imagePath)) {
|
|
unlink($imagePath);
|
|
}
|
|
}
|
|
|
|
$file = $request->file('logo');
|
|
|
|
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
|
|
|
|
$file->storeAs('sekolah', $hashedName, 'public');
|
|
|
|
$validated['logo'] = 'sekolah/' . $hashedName;
|
|
}
|
|
|
|
try {
|
|
$data->update($validated);
|
|
return redirect('/admin/sekolah')->with('success', 'Data Berhasil Diedit');
|
|
}catch (QueryException $e){
|
|
return redirect('/admin/sekolah')->with('error', 'Data Gagal Disimpan');
|
|
}
|
|
}
|
|
|
|
public function destroy($id){
|
|
$data = Sekolah::findOrFail($id);
|
|
if ($data->logo) {
|
|
$imagePath = storage_path('app/public/' . $data->logo);
|
|
if (file_exists($imagePath)) {
|
|
unlink($imagePath);
|
|
}
|
|
}
|
|
|
|
$data->delete();
|
|
return redirect('/admin/sekolah')->with('success', 'Data Berhasil Dihapus');
|
|
}
|
|
}
|