diff --git a/app/Http/Controllers/ScoreboardController.php b/app/Http/Controllers/ScoreboardController.php index d060c14f3..ecc6e62a0 100644 --- a/app/Http/Controllers/ScoreboardController.php +++ b/app/Http/Controllers/ScoreboardController.php @@ -7,24 +7,34 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; +use Illuminate\Validation\Rule; class ScoreboardController extends Controller { public function index(Request $request) { - if ($request->has('edition')) { - $edition = $request->query('edition'); - } else { - $edition = Carbon::now('Europe/Brussels')->year; + // Define valid years range + $years = range(Carbon::now('Europe/Brussels')->year, 2014, -1); + + // Get edition from request, default to current year if not present + $edition = $request->query('edition', Carbon::now('Europe/Brussels')->year); + + // Validate that edition is a valid year within the range + if (!in_array((int) $edition, $years)) { + abort(400, 'Invalid edition year.'); } + // Cast edition to integer after validation + $edition = (int) $edition; + $cache_time = 60 * 60 * 24; // One day cache + // Set shorter cache time for the current year if ($edition == Carbon::now('Europe/Brussels')->year) { $cache_time = 15 * 60; // 15 minutes Cache - }; - + } + // Retrieve total events count from cache or DB $total = Cache::remember('total_' . $edition, $cache_time, function () use ($edition) { Log::info("Setting cache for scoreboard total in " . $edition); return DB::table('events') @@ -34,12 +44,11 @@ public function index(Request $request) ->count(); }); - - - + // Retrieve events data from cache or DB $events = Cache::remember('events_' . $edition, $cache_time, function () use ($edition) { Log::info("Setting cache for scoreboard events in " . $edition); + // Fetch main country events $events = DB::table('events') ->join('countries', 'events.country_iso', '=', 'countries.iso') ->select('countries.iso as country_iso', 'countries.name as country_name', 'countries.population as country_population', DB::raw('count(*) as total')) @@ -47,12 +56,10 @@ public function index(Request $request) ->whereYear('end_date', '=', $edition) ->whereNull('deleted_at') ->where('countries.parent', "=", "") - ->groupBy(['countries.iso','countries.name','countries.population']) + ->groupBy(['countries.iso', 'countries.name', 'countries.population']) ->get(); - - - + // Fetch dependent country events (children) $eventsFromDependencies = DB::table('events') ->join('countries', 'events.country_iso', '=', 'countries.iso') ->select('countries.population as population', 'countries.parent as iso', DB::raw('count(*) as total')) @@ -60,10 +67,10 @@ public function index(Request $request) ->whereNull('deleted_at') ->whereYear('end_date', '=', $edition) ->whereNotNull('countries.parent') - ->groupBy(['parent','countries.population']) + ->groupBy(['parent', 'countries.population']) ->get(); - + // Merge dependent country events with main country events $parentsArr = []; foreach ($eventsFromDependencies as $dependantCountryEvents) { if (isset($parentsArr[$dependantCountryEvents->iso])) { @@ -72,38 +79,32 @@ public function index(Request $request) } else { $parentsArr[$dependantCountryEvents->iso] = array($dependantCountryEvents->total, $dependantCountryEvents->population); } - } - + // Add dependent data to main country events foreach ($events as $mainCountry) { if (isset($parentsArr[$mainCountry->country_iso])) { $mainCountry->total += $parentsArr[$mainCountry->country_iso][0]; $mainCountry->country_population += $parentsArr[$mainCountry->country_iso][1]; - } - //countries.population / count(*) + // Calculate rank based on population and total events $mainCountry->rank = $mainCountry->country_population / $mainCountry->total; - } + // Calculate the total events count for the scoreboard $total = 0; foreach ($events as $line) { $total += $line->total; - }; + } Log::info("Total from countries displayed: " . $total); - return $events; - - }); Log::info("Total displayed in scoreboard: " . $total); - $years = range(\Carbon\Carbon::now()->year, 2014, -1); - + // Return the view with the relevant data return view('scoreboard', [ 'events' => $events->sortBy('rank'), 'total' => $total,