|void * * @throws AuditionAdminException */ public function __invoke(Audition $audition, string $rank_type) { if ($rank_type !== 'seating' && $rank_type !== 'advancement') { throw new AuditionAdminException('Invalid rank type: '.$rank_type.' (must be seating or advancement)'); } $cache_duration = 15; if ($rank_type === 'seating') { return cache()->remember('rank_seating_'.$audition->id, $cache_duration, function () use ($audition) { return $this->get_seating_ranks($audition); }); } if ($rank_type === 'advancement') { return cache()->remember('rank_advancement_'.$audition->id, $cache_duration, function () use ($audition) { return $this->get_advancement_ranks($audition); }); } } private function get_seating_ranks(Audition $audition): Collection { if ($audition->bonusScore()->count() > 0) { $totalColumn = 'seating_total_with_bonus'; } else { $totalColumn = 'seating_total'; } $sortedEntries = $audition->entries() ->whereHas('totalScore') ->with('totalScore') ->with('student.school') ->with('audition') ->join('entry_total_scores', 'entries.id', '=', 'entry_total_scores.entry_id') ->orderBy('entry_total_scores.'.$totalColumn, 'desc') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[0]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[1]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[2]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[3]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[4]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[5]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[6]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[7]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[8]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[9]"), -999999) DESC') ->select('entries.*') ->get(); $rankOn = 1; foreach ($sortedEntries as $entry) { if ($entry->hasFlag('declined')) { $entry->seatingRank = 'declined'; } else { $entry->seatingRank = $rankOn; $rankOn++; } } return $sortedEntries; } private function get_advancement_ranks(Audition $audition): Collection { return $audition->entries() ->whereHas('totalScore') ->with('totalScore') ->with('student.school') ->with('audition') ->join('entry_total_scores', 'entries.id', '=', 'entry_total_scores.entry_id') ->orderBy('entry_total_scores.advancement_total', 'desc') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[0]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[1]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[2]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[3]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[4]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[5]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[6]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[7]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[8]"), -999999) DESC') ->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[9]"), -999999) DESC') ->select('entries.*') ->get(); } }