151 lines
5.3 KiB
PHP
151 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Guru;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Guru;
|
|
use App\Models\Logbooks;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class JadwalGuruController extends Controller
|
|
{
|
|
public function getDataHariIni(){
|
|
$jharini = Logbooks::whereDate('tanggal', Carbon::today())
|
|
->orderByRaw("CASE WHEN start IS NULL OR start = '' THEN 1 ELSE 0 END, start ASC")
|
|
->get();
|
|
return $jharini->toJson();
|
|
}
|
|
|
|
public function index(){
|
|
$data = [];
|
|
$peserta = [];
|
|
$guru = Guru::where('status', 'Aktif')->select('id', 'nama')->get();
|
|
|
|
// Batas waktu pengisian logbook hari ini adalah jam 19:00
|
|
$deadline = Carbon::today()->setTime(19, 0, 0);
|
|
$now = Carbon::now();
|
|
$isDeadlinePassed = $now->greaterThan($deadline);
|
|
$remainingSeconds = $isDeadlinePassed ? 0 : (int) $now->diffInSeconds($deadline);
|
|
|
|
return view('guru.pages.jadwal', compact('data', 'peserta', 'guru', 'deadline', 'isDeadlinePassed', 'remainingSeconds'));
|
|
}
|
|
|
|
public function calendarData(){
|
|
$guru_id = auth()->user()->Guru->id;
|
|
|
|
$jharini = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
|
|
$query->where('guru_id', $guru_id);
|
|
})
|
|
->orderByRaw("CASE WHEN start IS NULL OR start = '' THEN 1 ELSE 0 END, start ASC")
|
|
->with('Kelas', 'FotoLogbook')
|
|
->get();
|
|
return json_encode($jharini);
|
|
}
|
|
|
|
public function calendar(){
|
|
return view('guru.pages.jadwal-calendar');
|
|
}
|
|
|
|
// ============================================
|
|
// AJAX Endpoints untuk menampilkan data tanpa timeout
|
|
// ============================================
|
|
public function getJadwalHariIni(){
|
|
$guru_id = auth()->user()->Guru->id;
|
|
|
|
$jharini = Logbooks::where(function($q) use ($guru_id) {
|
|
$q->where('guru_id', $guru_id)
|
|
->orWhereHas('kelas', function($query) use ($guru_id) {
|
|
$query->where('guru_id', $guru_id);
|
|
});
|
|
})
|
|
->whereDate('tanggal', Carbon::today())
|
|
->orderByRaw("CASE WHEN start IS NULL OR start = '' THEN 1 ELSE 0 END, start ASC")
|
|
->with(['Kelas.guru', 'Guru', 'FotoLogbook'])
|
|
->get();
|
|
|
|
// Batas waktu pengisian logbook hari ini adalah jam 19:00
|
|
$deadline = Carbon::today()->setTime(19, 0, 0);
|
|
$now = Carbon::now();
|
|
$isDeadlinePassed = $now->greaterThan($deadline);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $jharini,
|
|
'count' => count($jharini),
|
|
'is_deadline_passed' => $isDeadlinePassed,
|
|
]);
|
|
}
|
|
|
|
public function getJadwalBesok(){
|
|
$guru_id = auth()->user()->Guru->id;
|
|
|
|
$jbesok = Logbooks::where(function($q) use ($guru_id) {
|
|
$q->where('guru_id', $guru_id)
|
|
->orWhereHas('kelas', function($query) use ($guru_id) {
|
|
$query->where('guru_id', $guru_id);
|
|
});
|
|
})
|
|
->whereDate('tanggal', Carbon::tomorrow())
|
|
->orderByRaw("CASE WHEN start IS NULL OR start = '' THEN 1 ELSE 0 END, start ASC")
|
|
->with(['Kelas.guru', 'Guru', 'FotoLogbook'])
|
|
->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $jbesok,
|
|
'count' => count($jbesok)
|
|
]);
|
|
}
|
|
|
|
public function getLogbookData(){
|
|
Carbon::setLocale('id');
|
|
$guru_id = auth()->user()->Guru->id;
|
|
|
|
$groupedLogbooks = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
|
|
$query->where('guru_id', $guru_id);
|
|
})
|
|
->orderBy('tanggal', 'asc')
|
|
->with('Kelas.Guru', 'Kelas.siswa', 'FotoLogbook')
|
|
->get()
|
|
->groupBy(function($item) {
|
|
return \Carbon\Carbon::parse($item->tanggal)->translatedFormat('F Y');
|
|
});
|
|
|
|
$now = \Carbon\Carbon::now();
|
|
|
|
$sortedLogbooks = collect([
|
|
'upcoming' => collect(),
|
|
'past' => collect(),
|
|
]);
|
|
|
|
$logbookSummary = collect();
|
|
|
|
foreach ($groupedLogbooks as $bulanTahun => $items) {
|
|
$firstItemDate = \Carbon\Carbon::parse($items->first()->tanggal);
|
|
$category = $firstItemDate->greaterThanOrEqualTo($now->copy()->startOfMonth()) ? 'upcoming' : 'past';
|
|
$sortedLogbooks[$category][$bulanTahun] = $items;
|
|
|
|
// Menyimpan jumlah logbook tiap bulan
|
|
$logbookSummary[$bulanTahun] = [
|
|
'jumlah_sesi' => $items->count(),
|
|
'jumlah_sesi_completed' => $items->where('status', 'completed')->count(),
|
|
'jumlah_siswa' => $items->pluck('Kelas')->filter()->flatMap->siswa->unique('id')->count(),
|
|
'jumlah_kelas' => $items->pluck('kelas_id')->unique()->count(),
|
|
];
|
|
}
|
|
|
|
$logbooksUpcoming = $sortedLogbooks['upcoming'];
|
|
$logbooksPast = $sortedLogbooks['past']->sortByDesc(function($items, $bulanTahun) {
|
|
return \Carbon\Carbon::parse($items->first()->tanggal);
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'upcoming' => $logbooksUpcoming,
|
|
'past' => $logbooksPast,
|
|
'summary' => $logbookSummary
|
|
]);
|
|
}
|
|
}
|