275 lines
9.6 KiB
PHP
275 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\HistoriSiswa;
|
|
use App\Models\Logbooks;
|
|
use App\Models\Siswa;
|
|
use App\Models\Programs;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use App\Models\Guru;
|
|
use App\Models\Ruangan;
|
|
use App\Models\Kelas;
|
|
use App\Models\Sesi;
|
|
use App\Models\JadwalKelas;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function insertHistori(){
|
|
$data = Siswa::all();
|
|
$successCount = 0;
|
|
$failedEntries = [];
|
|
|
|
for ($i = 0; $i < count($data); $i++) {
|
|
try {
|
|
$result = HistoriSiswa::create([
|
|
'siswa_id' => $data[$i]->id,
|
|
'status' => $data[$i]->status,
|
|
]);
|
|
|
|
if ($result) {
|
|
$successCount++;
|
|
} else {
|
|
$failedEntries[] = [
|
|
'siswa_id' => $data[$i]->id,
|
|
'reason' => 'Create method returned false'
|
|
];
|
|
}
|
|
} catch (\Exception $e) {
|
|
$failedEntries[] = [
|
|
'siswa_id' => $data[$i]->id,
|
|
'reason' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
$totalRecords = count($data);
|
|
$failedCount = count($failedEntries);
|
|
|
|
if ($failedCount > 0) {
|
|
// Log atau tampilkan error
|
|
|
|
// Opsional: kembalikan informasi kegagalan jika diperlukan
|
|
return [
|
|
'status' => 'partial',
|
|
'message' => "{$successCount} data berhasil disimpan, {$failedCount} data gagal",
|
|
'success_count' => $successCount,
|
|
'failed_count' => $failedCount,
|
|
'failed_entries' => $failedEntries
|
|
];
|
|
} else {
|
|
// Semua berhasil
|
|
return [
|
|
'status' => 'success',
|
|
'message' => "Semua {$totalRecords} data berhasil disimpan"
|
|
];
|
|
}
|
|
|
|
}
|
|
public function index()
|
|
{
|
|
$data = Siswa::with('Programs')->get();
|
|
$programs = Programs::all();
|
|
$programsCount = $programs->count();
|
|
$groupPrograms = DB::table('programs')
|
|
->leftJoin('siswa', 'programs.id', '=', 'siswa.program_id')
|
|
->selectRaw('programs.level, programs.kelas, COUNT(siswa.id) AS jumlah_peserta_program, (SELECT COUNT(*) FROM siswa) AS total_peserta')
|
|
->groupBy('programs.id', 'programs.level', 'programs.kelas')
|
|
->get();
|
|
|
|
$totalGuru = Guru::count();
|
|
$totalRuangan = Ruangan::count();
|
|
$totalKelas = Kelas::count();
|
|
$totalSesi = Sesi::count();
|
|
$totalJadwal = JadwalKelas::count();
|
|
$totalSiswaAktif = $data->whereIn('status', ['Aktif 24 Sesi', 'Aktif 12 Sesi', 'Aktif Ekstra'])->count();
|
|
|
|
$gaResult = session('ga_log_result');
|
|
|
|
return view('admin.pages.dashboard', compact(
|
|
'data',
|
|
'groupPrograms',
|
|
'programsCount',
|
|
'totalGuru',
|
|
'totalRuangan',
|
|
'totalKelas',
|
|
'totalSesi',
|
|
'totalJadwal',
|
|
'totalSiswaAktif',
|
|
'gaResult'
|
|
));
|
|
}
|
|
|
|
public function dashboardData(){
|
|
Carbon::setLocale('id');
|
|
|
|
$now = Carbon::now();
|
|
$tanggalMulai = $now->copy()->subMonths(5)->startOfMonth();
|
|
$tanggalAkhir = $now->copy()->endOfMonth();
|
|
|
|
// --- Data reguler per bulan ---
|
|
$siswaPerBulanReguler = HistoriSiswa::whereBetween('created_at', [$tanggalMulai, $tanggalAkhir])
|
|
->where(function($q) {
|
|
$q->where('status', 'Aktif 24 Sesi')
|
|
->orWhere('status', 'Aktif 12 Sesi');
|
|
})
|
|
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as bulan_id, COUNT(id) as jumlah_siswa")
|
|
->groupBy('bulan_id')
|
|
->pluck('jumlah_siswa', 'bulan_id'); // hasil key => value
|
|
|
|
// --- Data ekstra per bulan ---
|
|
$siswaPerBulanEkstra = HistoriSiswa::whereBetween('created_at', [$tanggalMulai, $tanggalAkhir])
|
|
->where('status', 'Aktif Ekstra')
|
|
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as bulan_id, COUNT(id) as jumlah_siswa")
|
|
->groupBy('bulan_id')
|
|
->pluck('jumlah_siswa', 'bulan_id');
|
|
|
|
// --- Siapkan array per bulan (6 bulan terakhir) ---
|
|
$dataLengkap = [];
|
|
$dataLengkapReguler = [];
|
|
$dataLengkapEkstra = [];
|
|
|
|
for ($i = 0; $i < 6; $i++) {
|
|
$bulan = $now->copy()->subMonths(5 - $i);
|
|
$bulanId = $bulan->format('Y-m');
|
|
$bulanTampil = $bulan->translatedFormat('M Y');
|
|
|
|
$jumlahReguler = $siswaPerBulanReguler[$bulanId] ?? 0;
|
|
$jumlahEkstra = $siswaPerBulanEkstra[$bulanId] ?? 0;
|
|
|
|
$dataLengkap[] = [
|
|
'bulan' => $bulanTampil,
|
|
'jumlah_siswa' => $jumlahReguler + $jumlahEkstra,
|
|
];
|
|
|
|
$dataLengkapReguler[] = [
|
|
'bulan' => $bulanTampil,
|
|
'jumlah_siswa' => $jumlahReguler,
|
|
];
|
|
|
|
$dataLengkapEkstra[] = [
|
|
'bulan' => $bulanTampil,
|
|
'jumlah_siswa' => $jumlahEkstra,
|
|
];
|
|
}
|
|
|
|
// --- Total siswa (berdasarkan status terakhir di tabel Siswa) ---
|
|
$data = Siswa::get();
|
|
$total_siswa_reguler = $data->where('status', 'Aktif 24 Sesi')->count()
|
|
+ $data->where('status', 'Aktif 12 Sesi')->count();
|
|
$total_siswa_ekstra = $data->where('status', 'Aktif Ekstra')->count();
|
|
$total_siswa_aktif = $total_siswa_reguler + $total_siswa_ekstra;
|
|
|
|
// --- Event dan sesi ---
|
|
$total_sesi = Logbooks::count();
|
|
$total_kelas = Kelas::count();
|
|
$total_guru = Guru::count();
|
|
$total_ruangan = Ruangan::count();
|
|
|
|
// --- Data Notifikasi GA Terakhir ---
|
|
$gaResult = session('ga_log_result');
|
|
$gaDataFormatted = null;
|
|
if ($gaResult) {
|
|
$gaDataFormatted = [
|
|
'semester' => $gaResult['semester'] ?? 'Genap',
|
|
'tahunAjaran' => $gaResult['tahunAjaran'] ?? '2025/2026',
|
|
'executionTime' => $gaResult['executionTime'] ?? '0.00',
|
|
'generations' => $gaResult['generations'] ?? 0,
|
|
'fitness' => isset($gaResult['bestChromosome']) ? number_format($gaResult['bestChromosome']->fitness, 4) : '1.0000',
|
|
'isOptimal' => $gaResult['isOptimal'] ?? true,
|
|
];
|
|
}
|
|
|
|
// --- Kelas hari ini ---
|
|
$jharini = Logbooks::whereDate('tanggal', Carbon::today())->count();
|
|
$jcompleted = Logbooks::whereDate('tanggal', Carbon::today())
|
|
->whereIn('status', ['completed', 'libur'])
|
|
->count();
|
|
|
|
// --- Izin Pending ---
|
|
$izinpending = [];
|
|
|
|
return response()->json([
|
|
'total_siswa' => $total_siswa_reguler,
|
|
'total_siswa_ekstra' => $total_siswa_ekstra,
|
|
'total_siswa_aktif' => $total_siswa_aktif,
|
|
'data_perbulan' => $dataLengkap,
|
|
'data_perbulanreguler' => $dataLengkapReguler,
|
|
'data_perbulanekstra' => $dataLengkapEkstra,
|
|
'total_sesi' => $total_sesi,
|
|
'total_kelas' => $total_kelas,
|
|
'total_guru' => $total_guru,
|
|
'total_ruangan' => $total_ruangan,
|
|
'ga_result' => $gaDataFormatted,
|
|
'jharini' => $jharini,
|
|
'jcompleted' => $jcompleted,
|
|
'izinpending' => $izinpending,
|
|
]);
|
|
}
|
|
|
|
public function dashboardChartData(Request $request)
|
|
{
|
|
Carbon::setLocale('id');
|
|
|
|
$monthCount = min((int) $request->get('months', 5), 24);
|
|
if ($monthCount < 1) $monthCount = 1;
|
|
|
|
$now = Carbon::now();
|
|
$tanggalMulai = $now->copy()->subMonths($monthCount - 1)->startOfMonth();
|
|
$tanggalAkhir = $now->copy()->endOfMonth();
|
|
|
|
// --- Data reguler per bulan ---
|
|
$siswaPerBulanReguler = HistoriSiswa::whereBetween('created_at', [$tanggalMulai, $tanggalAkhir])
|
|
->where(function($q) {
|
|
$q->where('status', 'Aktif 24 Sesi')
|
|
->orWhere('status', 'Aktif 12 Sesi');
|
|
})
|
|
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as bulan_id, COUNT(id) as jumlah_siswa")
|
|
->groupBy('bulan_id')
|
|
->pluck('jumlah_siswa', 'bulan_id');
|
|
|
|
// --- Data ekstra per bulan ---
|
|
$siswaPerBulanEkstra = HistoriSiswa::whereBetween('created_at', [$tanggalMulai, $tanggalAkhir])
|
|
->where('status', 'Aktif Ekstra')
|
|
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as bulan_id, COUNT(id) as jumlah_siswa")
|
|
->groupBy('bulan_id')
|
|
->pluck('jumlah_siswa', 'bulan_id');
|
|
|
|
$labels = [];
|
|
$dataTotal = [];
|
|
$dataReguler = [];
|
|
$dataEkstra = [];
|
|
|
|
for ($i = 0; $i < $monthCount; $i++) {
|
|
$bulan = $now->copy()->subMonths(($monthCount - 1) - $i);
|
|
$bulanId = $bulan->format('Y-m');
|
|
|
|
$jumlahReguler = $siswaPerBulanReguler[$bulanId] ?? 0;
|
|
$jumlahEkstra = $siswaPerBulanEkstra[$bulanId] ?? 0;
|
|
|
|
$labels[] = $bulan->translatedFormat('M Y');
|
|
$dataTotal[] = $jumlahReguler + $jumlahEkstra;
|
|
$dataReguler[] = $jumlahReguler;
|
|
$dataEkstra[] = $jumlahEkstra;
|
|
}
|
|
|
|
return response()->json([
|
|
'labels' => $labels,
|
|
'data_total' => $dataTotal,
|
|
'data_reguler' => $dataReguler,
|
|
'data_ekstra' => $dataEkstra,
|
|
]);
|
|
}
|
|
|
|
}
|