59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Siswa;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Leaderboard;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Carbon\Carbon;
|
|
|
|
class LeaderboardController extends Controller
|
|
{
|
|
private function getData()
|
|
{
|
|
$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;
|
|
|
|
$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) {
|
|
$fotoProfil = optional($item->siswa)->foto_profil;
|
|
return [
|
|
'ranking' => $i + 1,
|
|
'nama' => optional($item->siswa)->nama ?? '-',
|
|
'nisn' => optional($item->siswa)->nisn ?? '-',
|
|
'exp' => $item->total_exp,
|
|
'id_siswa' => $item->id_siswa,
|
|
'foto_profil' => $fotoProfil,
|
|
'foto_url' => $fotoProfil ? Storage::url($fotoProfil) : null,
|
|
];
|
|
});
|
|
|
|
$myRank = $leaderboard->firstWhere('id_siswa', $siswa->id_siswa);
|
|
|
|
return compact('leaderboard', 'myRank', 'semester', 'tahunAjaran');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = $this->getData();
|
|
return view('siswa.leaderboard.index', $data);
|
|
}
|
|
|
|
// Endpoint JSON untuk polling real-time
|
|
public function json()
|
|
{
|
|
$data = $this->getData();
|
|
return response()->json($data);
|
|
}
|
|
} |