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\Attendance;
|
||||
use App\Models\ClassSession;
|
||||
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@ -19,50 +20,37 @@ public static function canView(): bool
|
||||
|
||||
protected function getStats(): array
|
||||
{
|
||||
$studentId = auth()->user()?->student?->id;
|
||||
$student = auth()->user()?->student;
|
||||
$studentId = $student?->id;
|
||||
|
||||
if (! $studentId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Attendance Stats
|
||||
$presentCount = Attendance::where('student_id', $studentId)
|
||||
->whereNotNull('attended_at')
|
||||
->count();
|
||||
$totalSessions = Attendance::where('student_id', $studentId)->count();
|
||||
|
||||
$totalSessions = ClassSession::where('date', '<=', now()->startOfDay())
|
||||
->count();
|
||||
|
||||
$attendanceRate = $totalSessions > 0 ? round(($presentCount / $totalSessions) * 100) : 0;
|
||||
|
||||
// Base Assignment Query (Assignments involving the student)
|
||||
$baseAssignmentQuery = Assignment::query()
|
||||
->whereHas('assignmentTargets', function (Builder $query) use ($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);
|
||||
});
|
||||
$query->where('student_id', $studentId);
|
||||
});
|
||||
|
||||
// Completed Assignments (Student has submitted)
|
||||
$completedAssignments = (clone $baseAssignmentQuery)->whereHas('assignmentSubmissions', function (Builder $query) use ($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);
|
||||
});
|
||||
$query->where('student_id', $studentId);
|
||||
})->count();
|
||||
|
||||
// Pending Assignments (Not submitted yet)
|
||||
$pendingAssignments = (clone $baseAssignmentQuery)->whereDoesntHave('assignmentSubmissions', function (Builder $query) use ($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);
|
||||
});
|
||||
})
|
||||
->where('due_date', '>=', now())
|
||||
->count();
|
||||
$query->where('student_id', $studentId);
|
||||
})->count();
|
||||
|
||||
return [
|
||||
Stat::make('Kehadiran', $presentCount.' / '.$totalSessions.' Sesi')
|
||||
@ -76,7 +64,7 @@ protected function getStats(): array
|
||||
->color('success'),
|
||||
|
||||
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')
|
||||
->color($pendingAssignments > 0 ? 'warning' : 'gray'),
|
||||
];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user