From 009c85e0443328eb30a3b29cab38dda362445d05 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 12 Jul 2024 15:36:51 -0500 Subject: [PATCH] Successfully accessing doubler information --- app/Actions/Tabulation/AllJudgesCount.php | 3 +- .../Tabulation/SeatAuditionController.php | 53 +++++++++++++++---- app/Services/DoublerService.php | 14 +++-- app/Services/EntryService.php | 2 +- .../auditionSeating-results-table.blade.php | 6 +++ .../Pages/Seating/auditionSeatingTest.php | 36 +++++++++++++ 6 files changed, 97 insertions(+), 17 deletions(-) diff --git a/app/Actions/Tabulation/AllJudgesCount.php b/app/Actions/Tabulation/AllJudgesCount.php index 9e40fee..9ee70b7 100644 --- a/app/Actions/Tabulation/AllJudgesCount.php +++ b/app/Actions/Tabulation/AllJudgesCount.php @@ -22,6 +22,7 @@ class AllJudgesCount implements CalculateEntryScore public function calculate(string $mode, Entry $entry): array { + $cacheKey = 'entryScore-'.$entry->id.'-'.$mode; return Cache::remember($cacheKey, 10, function () use ($mode, $entry) { $this->basicValidation($mode, $entry); @@ -35,6 +36,7 @@ class AllJudgesCount implements CalculateEntryScore protected function getJudgeTotals($mode, Entry $entry) { + $scores = []; foreach ($this->auditionService->getJudges($entry->audition) as $judge) { $scores[] = $this->calculator->__invoke($mode, $entry, $judge); @@ -49,7 +51,6 @@ class AllJudgesCount implements CalculateEntryScore $index++; } } - return $sums; } diff --git a/app/Http/Controllers/Tabulation/SeatAuditionController.php b/app/Http/Controllers/Tabulation/SeatAuditionController.php index 91dec57..7e8970a 100644 --- a/app/Http/Controllers/Tabulation/SeatAuditionController.php +++ b/app/Http/Controllers/Tabulation/SeatAuditionController.php @@ -4,32 +4,64 @@ namespace App\Http\Controllers\Tabulation; use App\Actions\Tabulation\CalculateEntryScore; use App\Actions\Tabulation\RankAuditionEntries; -use App\Exceptions\TabulationException; use App\Http\Controllers\Controller; use App\Models\Audition; -use App\Models\Entry; +use App\Services\DoublerService; +use App\Services\EntryService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; class SeatAuditionController extends Controller { protected CalculateEntryScore $calc; + + protected DoublerService $doublerService; + protected RankAuditionEntries $ranker; - public function __construct(CalculateEntryScore $calc, RankAuditionEntries $ranker) - { + protected EntryService $entryService; + + public function __construct( + CalculateEntryScore $calc, + RankAuditionEntries $ranker, + DoublerService $doublerService, + EntryService $entryService + ) { $this->calc = $calc; $this->ranker = $ranker; + $this->doublerService = $doublerService; + $this->entryService = $entryService; } public function __invoke(Request $request, Audition $audition) { + $doublers = $this->doublerService->doublersForEvent($audition->event); $entryData = []; - #$entries = Entry::forSeating()->with('student.school')->where('audition_id', $audition->id)->get(); $entries = $this->ranker->rank('seating', $audition); $entries->load('student.school'); + foreach ($entries as $entry) { - $totalScoreColumn = $entry->score_totals[0] >= 0 ? - $entry->score_totals[0] : $entry->score_message; + $isDoubler = false; + if (array_key_exists($entry->student->id, $doublers)) { + $isDoubler = true; + $doubleData = []; + $doublerEntries = $doublers[$entry->student->id]['entries']; + foreach ($doublerEntries as $doublerEntry) { + Log::debug('doubler check for entry ' . $doublerEntry->id); + log::debug('Rank: ' . $this->entryService->rankOfEntry('seating', $doublerEntry)); + $doubleData[] = [ + 'name' => $doublerEntry->audition->name, + 'entryId' => $doublerEntry->id, + 'rank' => $this->entryService->rankOfEntry('seating', $doublerEntry), + ]; + } + } + $totalScoreColumn = 'No Score'; + $fullyScored = false; + if ($entry->score_totals) { + $totalScoreColumn = $entry->score_totals[0] >= 0 ? $entry->score_totals[0] : $entry->score_message; + $fullyScored = $entry->score_totals[0] >= 0; + } $entryData[] = [ 'rank' => $entry->rank, 'id' => $entry->id, @@ -37,11 +69,12 @@ class SeatAuditionController extends Controller 'schoolName' => $entry->student->school->name, 'drawNumber' => $entry->draw_number, 'totalScore' => $totalScoreColumn, - 'fullyScored' => $entry->score_totals[0] >= 0, + 'fullyScored' => $fullyScored, + 'isDoubler' => $isDoubler, + 'doubleData' => $doubleData ?? [], ]; } - //dd($entryData); - return view('tabulation.auditionSeating', ['entryData' => $entryData, 'audition' => $audition]); + return view('tabulation.auditionSeating', compact('entryData', 'audition', 'doublers')); } } diff --git a/app/Services/DoublerService.php b/app/Services/DoublerService.php index d286fec..6601da8 100644 --- a/app/Services/DoublerService.php +++ b/app/Services/DoublerService.php @@ -14,21 +14,25 @@ use Illuminate\Support\Facades\Cache; */ class DoublerService { - public function doublersForEvent(Event $event) + public function doublersForEvent(Event $event, string $mode = 'seating') { - $cacheKey = 'event'.$event->id.'doublers'; - return Cache::remember($cacheKey, 60, function () use ($event) { - return $this->findDoublersForEvent($event); + $cacheKey = 'event'.$event->id.'doublers-'.$mode; + return Cache::remember($cacheKey, 60, function () use ($event, $mode) { + return $this->findDoublersForEvent($event, $mode); }); } /** * @throws TabulationException */ - protected function findDoublersForEvent(Event $event): array + protected function findDoublersForEvent(Event $event, string $mode): array { $this->validateEvent($event); $entries = $event->entries; + $entries = match ($mode) { + 'seating' => $entries->filter(fn ($entry) => $entry->for_seating === 1), + 'advancement' => $entries->filter(fn ($entry) => $entry->for_advance === 1), + }; $entries->load('student.school'); $entries->load('audition'); $grouped = $entries->groupBy('student_id'); diff --git a/app/Services/EntryService.php b/app/Services/EntryService.php index d0ba84f..aa9ba17 100644 --- a/app/Services/EntryService.php +++ b/app/Services/EntryService.php @@ -40,6 +40,6 @@ class EntryService { $ranker = App::make(RankAuditionEntries::class); $rankings = $ranker->rank($mode, $entry->audition); - return $rankings->find($entry->id)->rank; + return $rankings->find($entry->id)->rank ?? 'No Rank'; } } diff --git a/resources/views/tabulation/auditionSeating-results-table.blade.php b/resources/views/tabulation/auditionSeating-results-table.blade.php index 6845dc9..610b33f 100644 --- a/resources/views/tabulation/auditionSeating-results-table.blade.php +++ b/resources/views/tabulation/auditionSeating-results-table.blade.php @@ -26,6 +26,12 @@ {{ $entry['schoolName'] }} + @if($entry['isDoubler']) + DOUBLER
+ @foreach($entry['doubleData'] as $double) + ID: {{ $double['entryId'] }} - {{ $double['name'] }} - {{ $double['rank'] }}
+ @endforeach + @endif {{-- @if($doublerService->studentIsDoubler($entry->student_id))--}} {{-- @include('tabulation.auditionSeating-doubler-block')--}} {{-- @endif--}} diff --git a/tests/Feature/Pages/Seating/auditionSeatingTest.php b/tests/Feature/Pages/Seating/auditionSeatingTest.php index d46e0ed..9a37742 100644 --- a/tests/Feature/Pages/Seating/auditionSeatingTest.php +++ b/tests/Feature/Pages/Seating/auditionSeatingTest.php @@ -1,6 +1,8 @@ r)->assertOk(); }); +// TODO make tests with varied information +it('returns the audition object and an array of info on each entry', function () { + // Arrange + $entry = Entry::factory()->create(['audition_id' => $this->audition->id]); + actAsAdmin(); + // Act + $response = get($this->r); + $response + ->assertOk() + ->assertViewHas('audition', $this->audition); + $viewData = $response->viewData('entryData'); + expect($viewData[0]['rank'])->toBe(1); + expect($viewData[0]['id'])->toBe($entry->id); + expect($viewData[0]['studentName'])->toBe($entry->student->full_name()); + expect($viewData[0]['schoolName'])->toBe($entry->student->school->name); + expect($viewData[0]['drawNumber'])->toBe($entry->draw_number); + expect($viewData[0]['totalScore'])->toBe('No Score'); + expect($viewData[0]['fullyScored'])->toBeFalse(); +}); +it('identifies a doubler', function () { + // Arrange + $audition1 = Audition::factory()->create(['event_id' => $this->audition->event_id]); + $audition2 = Audition::factory()->create(['event_id' => $this->audition->event_id]); + $student = Student::factory()->create(); + Entry::factory()->create(['audition_id' => $audition1->id, 'student_id' => $student->id]); + Entry::factory()->create(['audition_id' => $audition2->id, 'student_id' => $student->id]); + Entry::factory()->create(['audition_id' => $this->audition->id, 'student_id' => $student->id]); + actAsAdmin(); + // Act & Assert + $response = get($this->r); + $response->assertOk(); + $viewData = $response->viewData('entryData'); + expect($viewData[0]['isDoubler'])->toBeTrue(); +});