refactor: simplify attendance and assignment queries in StatsOverview widget
This commit is contained in:
parent
2f0d8395ed
commit
3c3836437a
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use App\Models\Assignment;
|
use App\Models\Assignment;
|
||||||
use App\Models\Attendance;
|
use App\Models\Attendance;
|
||||||
|
use App\Models\ClassSession;
|
||||||
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
||||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
@ -19,50 +20,37 @@ public static function canView(): bool
|
|||||||
|
|
||||||
protected function getStats(): array
|
protected function getStats(): array
|
||||||
{
|
{
|
||||||
$studentId = auth()->user()?->student?->id;
|
$student = auth()->user()?->student;
|
||||||
|
$studentId = $student?->id;
|
||||||
|
|
||||||
if (! $studentId) {
|
if (! $studentId) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attendance Stats
|
||||||
$presentCount = Attendance::where('student_id', $studentId)
|
$presentCount = Attendance::where('student_id', $studentId)
|
||||||
->whereNotNull('attended_at')
|
|
||||||
->count();
|
->count();
|
||||||
$totalSessions = Attendance::where('student_id', $studentId)->count();
|
|
||||||
|
$totalSessions = ClassSession::where('date', '<=', now()->startOfDay())
|
||||||
|
->count();
|
||||||
|
|
||||||
$attendanceRate = $totalSessions > 0 ? round(($presentCount / $totalSessions) * 100) : 0;
|
$attendanceRate = $totalSessions > 0 ? round(($presentCount / $totalSessions) * 100) : 0;
|
||||||
|
|
||||||
|
// Base Assignment Query (Assignments involving the student)
|
||||||
$baseAssignmentQuery = Assignment::query()
|
$baseAssignmentQuery = Assignment::query()
|
||||||
->whereHas('assignmentTargets', function (Builder $query) use ($studentId) {
|
->whereHas('assignmentTargets', function (Builder $query) use ($studentId) {
|
||||||
$query->where('student_id', $studentId)
|
$query->where('student_id', $studentId);
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
|
||||||
$q->select('study_group_id')->from('study_group_members')->where('student_id', $studentId);
|
|
||||||
})
|
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
|
||||||
$q->select('id')->from('study_groups')->where('leader_id', $studentId);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Completed Assignments (Student has submitted)
|
||||||
$completedAssignments = (clone $baseAssignmentQuery)->whereHas('assignmentSubmissions', function (Builder $query) use ($studentId) {
|
$completedAssignments = (clone $baseAssignmentQuery)->whereHas('assignmentSubmissions', function (Builder $query) use ($studentId) {
|
||||||
$query->where('student_id', $studentId)
|
$query->where('student_id', $studentId);
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
|
||||||
$q->select('study_group_id')->from('study_group_members')->where('student_id', $studentId);
|
|
||||||
})
|
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
|
||||||
$q->select('id')->from('study_groups')->where('leader_id', $studentId);
|
|
||||||
});
|
|
||||||
})->count();
|
})->count();
|
||||||
|
|
||||||
|
// Pending Assignments (Not submitted yet)
|
||||||
$pendingAssignments = (clone $baseAssignmentQuery)->whereDoesntHave('assignmentSubmissions', function (Builder $query) use ($studentId) {
|
$pendingAssignments = (clone $baseAssignmentQuery)->whereDoesntHave('assignmentSubmissions', function (Builder $query) use ($studentId) {
|
||||||
$query->where('student_id', $studentId)
|
$query->where('student_id', $studentId);
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
})->count();
|
||||||
$q->select('study_group_id')->from('study_group_members')->where('student_id', $studentId);
|
|
||||||
})
|
|
||||||
->orWhereIn('study_group_id', function ($q) use ($studentId) {
|
|
||||||
$q->select('id')->from('study_groups')->where('leader_id', $studentId);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
->where('due_date', '>=', now())
|
|
||||||
->count();
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Stat::make('Kehadiran', $presentCount.' / '.$totalSessions.' Sesi')
|
Stat::make('Kehadiran', $presentCount.' / '.$totalSessions.' Sesi')
|
||||||
@ -76,7 +64,7 @@ protected function getStats(): array
|
|||||||
->color('success'),
|
->color('success'),
|
||||||
|
|
||||||
Stat::make('Tugas Mendatang', $pendingAssignments.' Tugas')
|
Stat::make('Tugas Mendatang', $pendingAssignments.' Tugas')
|
||||||
->description('Belum dikerjakan')
|
->description($pendingAssignments > 0 ? 'Belum dikerjakan' : 'Semua tugas telah dikerjakan')
|
||||||
->descriptionIcon($pendingAssignments > 0 ? 'heroicon-m-exclamation-circle' : 'heroicon-m-face-smile')
|
->descriptionIcon($pendingAssignments > 0 ? 'heroicon-m-exclamation-circle' : 'heroicon-m-face-smile')
|
||||||
->color($pendingAssignments > 0 ? 'warning' : 'gray'),
|
->color($pendingAssignments > 0 ? 'warning' : 'gray'),
|
||||||
];
|
];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user