46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Siswa;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Leaderboard;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Carbon\Carbon;
|
|
|
|
class LeaderboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$siswa = Auth::guard('siswa')->user();
|
|
|
|
$now = Carbon::now();
|
|
$semester = $now->month >= 7 ? '1' : '2';
|
|
$tahunAjaran = $now->month >= 7
|
|
? $now->year . '/' . ($now->year + 1)
|
|
: ($now->year - 1) . '/' . $now->year;
|
|
|
|
// Top leaderboard kelas siswa ini
|
|
$leaderboard = Leaderboard::with('siswa')
|
|
->where('id_kelas', $siswa->id_kelas)
|
|
->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 ?? '-',
|
|
'exp' => $item->total_exp,
|
|
'id_siswa'=> $item->id_siswa,
|
|
];
|
|
});
|
|
|
|
// Posisi siswa yang login
|
|
$myRank = $leaderboard->firstWhere('id_siswa', $siswa->id_siswa);
|
|
|
|
return view('siswa.leaderboard.index', compact(
|
|
'leaderboard', 'myRank', 'semester', 'tahunAjaran'
|
|
));
|
|
}
|
|
} |