diff --git a/app/Http/Controllers/Admin/LeaderboardController.php b/app/Http/Controllers/Admin/LeaderboardController.php index d1e48be..20a27c2 100644 --- a/app/Http/Controllers/Admin/LeaderboardController.php +++ b/app/Http/Controllers/Admin/LeaderboardController.php @@ -4,15 +4,50 @@ use App\Http\Controllers\Controller; use App\Models\Leaderboard; +use App\Models\Kelas; +use Carbon\Carbon; +use Illuminate\Http\Request; class LeaderboardController extends Controller { - public function index() + public function index(Request $request) { - $leaderboards = Leaderboard::orderBy('ranking') - ->orderByDesc('total_exp') - ->paginate(10); + $now = Carbon::now(); + $semester = $request->input('semester', $now->month >= 7 ? '1' : '2'); + $tahunAjaran = $request->input('tahun_ajaran', $now->month >= 7 + ? $now->year . '/' . ($now->year + 1) + : ($now->year - 1) . '/' . $now->year); + $idKelas = $request->input('id_kelas'); - return view('admin.leaderboard.index', compact('leaderboards')); + $kelasList = Kelas::orderBy('tingkat')->orderBy('nama_kelas')->get(); + + $query = Leaderboard::with(['siswa', 'kelas']) + ->where('semester', $semester) + ->where('tahun_ajaran', $tahunAjaran); + + if ($idKelas) { + $query->where('id_kelas', $idKelas); + } + + $leaderboard = $query->orderBy('total_exp', 'desc')->get() + ->map(function ($item, $i) { + return [ + 'ranking' => $i + 1, + 'nama' => optional($item->siswa)->nama ?? '-', + 'nisn' => optional($item->siswa)->nisn ?? '-', + 'nama_kelas' => optional($item->kelas)->nama_kelas ?? '-', + 'exp' => $item->total_exp, + ]; + }); + + // Tahun ajaran list untuk dropdown (5 tahun ke belakang) + $tahunList = []; + for ($y = $now->year; $y >= $now->year - 4; $y--) { + $tahunList[] = $y . '/' . ($y + 1); + } + + return view('admin.leaderboard.index', compact( + 'leaderboard', 'kelasList', 'semester', 'tahunAjaran', 'idKelas', 'tahunList' + )); } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/Guru/LeaderboardController.php b/app/Http/Controllers/Guru/LeaderboardController.php index 0e75623..e8e034e 100644 --- a/app/Http/Controllers/Guru/LeaderboardController.php +++ b/app/Http/Controllers/Guru/LeaderboardController.php @@ -4,15 +4,64 @@ use App\Http\Controllers\Controller; use App\Models\Leaderboard; +use App\Models\Kelas; +use App\Models\Mengajar; +use Carbon\Carbon; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; class LeaderboardController extends Controller { - public function index() + public function index(Request $request) { - $leaderboards = Leaderboard::orderBy('ranking') - ->orderByDesc('total_exp') - ->paginate(10); + /** @var \App\Models\Guru $guru */ + $guru = Auth::guard('guru')->user(); - return view('admin.leaderboard.index', compact('leaderboards')); + $now = Carbon::now(); + $semester = $request->input('semester', $now->month >= 7 ? '1' : '2'); + $tahunAjaran = $request->input('tahun_ajaran', $now->month >= 7 + ? $now->year . '/' . ($now->year + 1) + : ($now->year - 1) . '/' . $now->year); + + // Kelas yang diajar guru ini + $idKelasList = Mengajar::where('id_guru', $guru->id_guru) + ->pluck('id_kelas') + ->unique() + ->toArray(); + + $idKelas = $request->input('id_kelas', $idKelasList[0] ?? null); + + $kelasList = Kelas::whereIn('id_kelas', $idKelasList) + ->orderBy('tingkat')->orderBy('nama_kelas') + ->get(); + + $leaderboard = collect(); + + if ($idKelas) { + $leaderboard = Leaderboard::with(['siswa', 'kelas']) + ->where('id_kelas', $idKelas) + ->where('semester', $semester) + ->where('tahun_ajaran', $tahunAjaran) + ->orderBy('total_exp', 'desc') + ->get() + ->map(function ($item, $i) { + return [ + 'ranking' => $i + 1, + 'nama' => optional($item->siswa)->nama ?? '-', + 'nisn' => optional($item->siswa)->nisn ?? '-', + 'nama_kelas' => optional($item->kelas)->nama_kelas ?? '-', + 'exp' => $item->total_exp, + ]; + }); + } + + $tahunList = []; + for ($y = $now->year; $y >= $now->year - 4; $y--) { + $tahunList[] = $y . '/' . ($y + 1); + } + + return view('guru.leaderboard.index', compact( + 'leaderboard', 'kelasList', 'semester', 'tahunAjaran', 'idKelas', 'tahunList' + )); } -} +} \ No newline at end of file diff --git a/resources/views/admin/leaderboard/index.blade.php b/resources/views/admin/leaderboard/index.blade.php index 0c84d41..892d14b 100644 --- a/resources/views/admin/leaderboard/index.blade.php +++ b/resources/views/admin/leaderboard/index.blade.php @@ -2,33 +2,229 @@ @section('title', 'Leaderboard') +@push('styles') + +@endpush + @section('content') -

๐Ÿ† Leaderboard

+

๐Ÿ… Leaderboard

+

Peringkat siswa berdasarkan total EXP yang dikumpulkan.

- - - - - - - - - - - - @foreach($leaderboards as $lb) - - - - - - - +{{-- Filter --}} + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ + +Semester {{ $semester }} ยท {{ $tahunAjaran }}{{ $idKelas ? ' ยท ' . ($kelasList->firstWhere('id_kelas', $idKelas)?->nama_kelas ?? '') : ' ยท Semua Kelas' }} + +@if($leaderboard->isEmpty()) +
+
๐Ÿ“Š
+

Belum ada data leaderboard.

+

Belum ada siswa yang menyelesaikan challenge pada periode ini.

+
+@else + + {{-- Podium --}} + @php + $first = $leaderboard->firstWhere('ranking', 1); + $second = $leaderboard->firstWhere('ranking', 2); + $third = $leaderboard->firstWhere('ranking', 3); + @endphp + + @if($first) +
+ @if($second) +
+
{{ strtoupper(substr($second['nama'],0,1)) }}
+
{{ $second['nama'] }}
+
{{ $second['nama_kelas'] }}
+
โญ {{ number_format($second['exp']) }}
+
2
+
+ @endif + +
+
+ ๐Ÿ‘‘ + {{ strtoupper(substr($first['nama'],0,1)) }} +
+
{{ $first['nama'] }}
+
{{ $first['nama_kelas'] }}
+
โญ {{ number_format($first['exp']) }}
+
1
+
+ + @if($third) +
+
{{ strtoupper(substr($third['nama'],0,1)) }}
+
{{ $third['nama'] }}
+
{{ $third['nama_kelas'] }}
+
โญ {{ number_format($third['exp']) }}
+
3
+
+ @endif +
+ @endif + + {{-- Tabel --}} +
+

๐Ÿ“‹ Semua Peringkat ({{ $leaderboard->count() }} siswa)

+ + @foreach($leaderboard as $item) + @php + $rankClass = match($item['ranking']) { 1=>'gold', 2=>'silver', 3=>'bronze', default=>'' }; + @endphp +
+
+ @if($item['ranking']===1) ๐Ÿฅ‡ + @elseif($item['ranking']===2) ๐Ÿฅˆ + @elseif($item['ranking']===3) ๐Ÿฅ‰ + @else {{ $item['ranking'] }} + @endif +
+
+
{{ $item['nama'] }}
+
{{ $item['nisn'] }}
+
+ {{ $item['nama_kelas'] }} +
โญ {{ number_format($item['exp']) }}
+
@endforeach -
-
RankingNISNTotal EXPSemesterTahun Ajaran
{{ $lb->ranking }}{{ $lb->nisn }}{{ $lb->total_exp }}{{ $lb->semester }}{{ $lb->tahun_ajaran }}
+ -{{ $leaderboards->links() }} +@endif -@endsection +@endsection \ No newline at end of file diff --git a/resources/views/guru/leaderboard/index.blade.php b/resources/views/guru/leaderboard/index.blade.php index 1bc2ec8..f93fa0c 100644 --- a/resources/views/guru/leaderboard/index.blade.php +++ b/resources/views/guru/leaderboard/index.blade.php @@ -2,33 +2,228 @@ @section('title', 'Leaderboard') +@push('styles') + +@endpush + @section('content') -

๐Ÿ† Leaderboard

+

๐Ÿ… Leaderboard

+

Peringkat siswa di kelas yang Anda ajar.

- - - - - - - - - - - - @foreach($leaderboards as $lb) - - - - - - - +@if($kelasList->isEmpty()) +
+ โš ๏ธ Anda belum mengajar kelas manapun. Hubungi admin untuk mengatur jadwal mengajar. +
+@else + +{{-- Filter --}} + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ + +@php $namaKelas = $kelasList->firstWhere('id_kelas', $idKelas)?->nama_kelas ?? '-'; @endphp +{{ $namaKelas }} ยท Semester {{ $semester }} ยท {{ $tahunAjaran }} + +@if($leaderboard->isEmpty()) +
+
๐Ÿ“Š
+

Belum ada data leaderboard.

+

Belum ada siswa yang menyelesaikan challenge pada periode ini.

+
+@else + + {{-- Podium --}} + @php + $first = $leaderboard->firstWhere('ranking', 1); + $second = $leaderboard->firstWhere('ranking', 2); + $third = $leaderboard->firstWhere('ranking', 3); + @endphp + + @if($first) +
+ @if($second) +
+
{{ strtoupper(substr($second['nama'],0,1)) }}
+
{{ $second['nama'] }}
+
โญ {{ number_format($second['exp']) }}
+
2
+
+ @endif + +
+
+ ๐Ÿ‘‘ + {{ strtoupper(substr($first['nama'],0,1)) }} +
+
{{ $first['nama'] }}
+
โญ {{ number_format($first['exp']) }}
+
1
+
+ + @if($third) +
+
{{ strtoupper(substr($third['nama'],0,1)) }}
+
{{ $third['nama'] }}
+
โญ {{ number_format($third['exp']) }}
+
3
+
+ @endif +
+ @endif + + {{-- Tabel --}} +
+

๐Ÿ“‹ Semua Peringkat ({{ $leaderboard->count() }} siswa)

+ + @foreach($leaderboard as $item) + @php $rankClass = match($item['ranking']) { 1=>'gold', 2=>'silver', 3=>'bronze', default=>'' }; @endphp +
+
+ @if($item['ranking']===1) ๐Ÿฅ‡ + @elseif($item['ranking']===2) ๐Ÿฅˆ + @elseif($item['ranking']===3) ๐Ÿฅ‰ + @else {{ $item['ranking'] }} + @endif +
+
+
{{ $item['nama'] }}
+
{{ $item['nisn'] }}
+
+
โญ {{ number_format($item['exp']) }}
+
@endforeach -
-
RankingNISNTotal EXPSemesterTahun Ajaran
{{ $lb->ranking }}{{ $lb->nisn }}{{ $lb->total_exp }}{{ $lb->semester }}{{ $lb->tahun_ajaran }}
+ -{{ $leaderboards->links() }} +@endif +@endif -@endsection +@endsection \ No newline at end of file