penjadwalan-ga/app/Http/Controllers/Guru/DashboardGuruController.php

119 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Guru;
use App\Http\Controllers\Controller;
use App\Models\Events;
use App\Models\HistoriSiswa;
use App\Models\Kelas;
use App\Models\Logbooks;
use App\Models\Siswa;
use App\Models\EvaluationAssignment;
use Carbon\Carbon;
use Illuminate\Http\Request;
class DashboardGuruController extends Controller
{
public function dashboardData()
{
Carbon::setLocale('id');
$guru_id = auth()->user()->Guru->id;
$total_sesi_hari_ini = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
$query->where('guru_id', $guru_id);
})->whereDate('tanggal', Carbon::today())->count();
$total_sesi_seminggu = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
$query->where('guru_id', $guru_id);
})->whereBetween('tanggal', [
Carbon::now()->startOfWeek(),
Carbon::now()->endOfWeek(),
])->count();
$total_sesi = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
$query->where('guru_id', $guru_id);
})->count();
$total_kelas = Kelas::where('guru_id', $guru_id)->count();
$jharini = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
$query->where('guru_id', $guru_id);
})->whereDate('tanggal', Carbon::today())->count();
$jcompleted = Logbooks::whereHas('kelas', function($query) use ($guru_id) {
$query->where('guru_id', $guru_id);
})
->whereDate('tanggal', Carbon::today())
->whereIn('status', ['completed', 'libur'])
->count();
$pending_p2p = EvaluationAssignment::where('evaluator_id', $guru_id)
->where('status', 'pending')
->count();
return response()->json([
'total_sesi_hari_ini' => $total_sesi_hari_ini,
'total_sesi_seminggu' => $total_sesi_seminggu,
'total_sesi' => $total_sesi,
'total_kelas' => $total_kelas,
'jharini' => $jharini,
'jcompleted' => $jcompleted,
'pending_p2p' => $pending_p2p,
]);
}
public function getChartData(Request $request)
{
Carbon::setLocale('id');
$guru_id = auth()->user()->Guru->id;
// Jumlah bulan yang ditampilkan (default 5, max 24)
$monthCount = min((int) $request->get('months', 5), 24);
if ($monthCount < 1) $monthCount = 1;
$months = collect();
for ($i = $monthCount - 1; $i >= 0; $i--) {
$months->push(Carbon::now()->subMonths($i)->startOfMonth());
}
$labels = [];
$totalSesi = [];
$totalCompleted = [];
foreach ($months as $month) {
$start = $month->copy()->startOfMonth();
$end = $month->copy()->endOfMonth();
$total = Logbooks::whereHas('kelas', function($q) use ($guru_id) {
$q->where('guru_id', $guru_id);
})
->whereBetween('tanggal', [$start->toDateString(), $end->toDateString()])
->count();
$completed = Logbooks::whereHas('kelas', function($q) use ($guru_id) {
$q->where('guru_id', $guru_id);
})
->whereBetween('tanggal', [$start->toDateString(), $end->toDateString()])
->where('status', 'completed')
->count();
$labels[] = $month->translatedFormat('M Y');
$totalSesi[] = $total;
$totalCompleted[] = $completed;
}
return response()->json([
'labels' => $labels,
'total_sesi' => $totalSesi,
'total_completed' => $totalCompleted,
]);
}
public function index()
{
return view('guru.pages.dashboard');
}
}