From a1f5191a1904c33a23255390ac71c96b63b0578d Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 12 Jul 2024 23:24:14 -0500 Subject: [PATCH] Cleanup doubler info --- .../Tabulation/DoublerDecisionController.php | 3 - .../Tabulation/SeatAuditionController.php | 26 +------ app/Services/AuditionService.php | 8 +- app/Services/DoublerService.php | 75 +++++++++++++++++-- app/Services/EntryService.php | 4 + .../auditionSeating-doubler-block.blade.php | 18 ++--- .../auditionSeating-results-table.blade.php | 2 +- tests/Feature/Services/DoublerServiceTest.php | 35 +++++---- 8 files changed, 104 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/Tabulation/DoublerDecisionController.php b/app/Http/Controllers/Tabulation/DoublerDecisionController.php index 7d95056..73141e5 100644 --- a/app/Http/Controllers/Tabulation/DoublerDecisionController.php +++ b/app/Http/Controllers/Tabulation/DoublerDecisionController.php @@ -36,7 +36,6 @@ class DoublerDecisionController extends Controller } } - $this->doublerService->refreshDoublerCache(); $returnMessage = $entry->student->full_name().' accepted seating in '.$entry->audition->name; @@ -52,8 +51,6 @@ class DoublerDecisionController extends Controller $entry->addFlag('declined'); - $this->doublerService->refreshDoublerCache(); - $returnMessage = $entry->student->full_name().' declined seating in '.$entry->audition->name; return redirect()->back()->with('success', $returnMessage); diff --git a/app/Http/Controllers/Tabulation/SeatAuditionController.php b/app/Http/Controllers/Tabulation/SeatAuditionController.php index fe1ced3..1dea67c 100644 --- a/app/Http/Controllers/Tabulation/SeatAuditionController.php +++ b/app/Http/Controllers/Tabulation/SeatAuditionController.php @@ -10,7 +10,6 @@ use App\Services\AuditionService; use App\Services\DoublerService; use App\Services\EntryService; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; class SeatAuditionController extends Controller { @@ -46,28 +45,6 @@ class SeatAuditionController extends Controller $entries->load('student.school'); foreach ($entries as $entry) { - $isDoubler = false; - if (array_key_exists($entry->student->id, $doublers)) { - $isDoubler = true; - $doubleData = []; - $doublerEntries = $doublers[$entry->student->id]['entries']; - - foreach ($doublerEntries as $doublerEntry) { - $limits = $this->auditionService->getSeatingLimits($doublerEntry->audition); - $limits = $limits->reject(function ($lim) { - return $lim['limit'] === 0; - }); - $doubleData[] = [ - 'audition' => $doublerEntry->audition, - 'name' => $doublerEntry->audition->name, - 'entryId' => $doublerEntry->id, - 'rank' => $this->entryService->rankOfEntry('seating', $doublerEntry), - 'limits' => $limits, - 'status' => 'undecided', // Will be undecided, accepted, or declined - 'unscored_in_audition' => $doublerEntry->audition->unscoredEntries()->count(), - ]; - } - } $totalScoreColumn = 'No Score'; $fullyScored = false; if ($entry->score_totals) { @@ -82,8 +59,7 @@ class SeatAuditionController extends Controller 'drawNumber' => $entry->draw_number, 'totalScore' => $totalScoreColumn, 'fullyScored' => $fullyScored, - 'isDoubler' => $isDoubler, - 'doubleData' => $doubleData ?? [], + 'doubleData' => $this->doublerService->entryDoublerData($entry), ]; } diff --git a/app/Services/AuditionService.php b/app/Services/AuditionService.php index 1e16873..09edccf 100644 --- a/app/Services/AuditionService.php +++ b/app/Services/AuditionService.php @@ -98,15 +98,15 @@ class AuditionService $limits = SeatingLimit::all(); foreach ($limits as $limit) { - $lims[$limit->audition_id][$limit->ensemble_id] = collect([ + $lims[$limit->audition_id][$limit->ensemble_id] = [ 'ensemble' => $ensembles->find($limit->ensemble_id), 'limit' =>$limit->maximum_accepted, - ]); + ]; } - return collect($lims); + return $lims; }); - return collect($allLimits[$audition->id]) ?? []; + return $allLimits[$audition->id] ?? []; } protected function validateAudition($audition) diff --git a/app/Services/DoublerService.php b/app/Services/DoublerService.php index 6601da8..bc97006 100644 --- a/app/Services/DoublerService.php +++ b/app/Services/DoublerService.php @@ -6,17 +6,29 @@ use App\Exceptions\TabulationException; use App\Models\Entry; use App\Models\Event; use App\Models\Student; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; -/** - * returns a collection of doublers for the event in the form of - * [studentId => [student=>student, entries=>[entries]] - */ class DoublerService { + protected EntryService $entryService; + + protected AuditionService $auditionService; + + public function __construct(EntryService $entryService, AuditionService $auditionService) + { + $this->entryService = $entryService; + $this->auditionService = $auditionService; + } + + /** + * returns a collection of doublers for the event in the form of + * [studentId => [student=>student, entries=>[entries]] + */ public function doublersForEvent(Event $event, string $mode = 'seating') { $cacheKey = 'event'.$event->id.'doublers-'.$mode; + return Cache::remember($cacheKey, 60, function () use ($event, $mode) { return $this->findDoublersForEvent($event, $mode); }); @@ -25,7 +37,7 @@ class DoublerService /** * @throws TabulationException */ - protected function findDoublersForEvent(Event $event, string $mode): array + protected function findDoublersForEvent(Event $event, string $mode = 'seating'): array { $this->validateEvent($event); $entries = $event->entries; @@ -49,6 +61,59 @@ class DoublerService return $doubler_array; } + public function entryDoublerData(Entry $primaryEntry) + { + if (! isset($this->findDoublersForEvent($primaryEntry->audition->event)[$primaryEntry->student_id])) { + return false; + } + $entries = $this->findDoublersForEvent($primaryEntry->audition->event)[$primaryEntry->student_id]['entries']; + $entryData = collect([]); + /** @var Collection $entries */ + foreach ($entries as $entry) { + $status = 'undecided'; + if ($entry->hasFlag('declined')) { + $status = 'declined'; + } + if ($entry->hasFlag('no_show')) { + $status = 'no_show'; + } + + $lims = $this->auditionService->getSeatingLimits($entry->audition); + $limits = []; + foreach ($lims as $lim) { + $limits[] = [ + 'ensemble_name' => $lim['ensemble']->name, + 'accepts' => $lim['limit'], + 'ensemble' => $lim['ensemble'], + ]; + } + + $entryData[$entry->id] = [ + 'entry' => $entry, + 'audition' => $entry->audition, + 'auditionName' => $entry->audition->name, + 'status' => $status, + 'rank' => $this->entryService->rankOfEntry('seating', $entry), + 'unscored_entries' => $entry->audition->unscoredEntries()->count(), + 'seating_limits' => $limits, + ]; + } + // find out how many items in the collection $entryData have a status of 'undecided' + $undecided_count = $entryData->filter(fn ($entry) => $entry['status'] === 'undecided')->count(); + // if $undecided_count is 1 set the item where status is 'undecided' to 'accepted' + if ($undecided_count === 1) { + $entryData->transform(function ($entry) { + if ($entry['status'] === 'undecided') { + $entry['status'] = 'accepted'; + } + + return $entry; + }); + } + + return $entryData; + } + /** * @throws TabulationException */ diff --git a/app/Services/EntryService.php b/app/Services/EntryService.php index aa9ba17..0f719e0 100644 --- a/app/Services/EntryService.php +++ b/app/Services/EntryService.php @@ -40,6 +40,10 @@ class EntryService { $ranker = App::make(RankAuditionEntries::class); $rankings = $ranker->rank($mode, $entry->audition); + $rankedEntry = $rankings->find($entry->id); + if (isset($rankedEntry->score_message)) { + return $rankedEntry->score_message; + } return $rankings->find($entry->id)->rank ?? 'No Rank'; } } diff --git a/resources/views/tabulation/auditionSeating-doubler-block.blade.php b/resources/views/tabulation/auditionSeating-doubler-block.blade.php index 85ddbfa..9f7635d 100644 --- a/resources/views/tabulation/auditionSeating-doubler-block.blade.php +++ b/resources/views/tabulation/auditionSeating-doubler-block.blade.php @@ -6,9 +6,8 @@
  • - {{-- TODO put in link --}} - {{ $double['name'] }} - {{ $double['status'] }} + {{ $double['auditionName'] }} - {{ $double['status'] }}

    @@ -21,26 +20,23 @@
      -
    • +
    • Ranked {{ $double['rank'] }}

      - - - -

      {{ $double['unscored_in_audition'] }} Unscored

      +

      {{ $double['unscored_entries'] }} Unscored

    • - @foreach($double['limits'] as $limit) -
    • {{$limit['ensemble']->name}} accepts {{ $limit['limit'] }}
    • + @foreach($double['seating_limits'] as $limit) +
    • {{$limit['ensemble_name']}} accepts {{ $limit['accepts'] }}
    • @endforeach
    @if ($double['status'] == 'undecided') -
    + @csrf
    -
    + @csrf
    diff --git a/resources/views/tabulation/auditionSeating-results-table.blade.php b/resources/views/tabulation/auditionSeating-results-table.blade.php index 24c39ce..1279f96 100644 --- a/resources/views/tabulation/auditionSeating-results-table.blade.php +++ b/resources/views/tabulation/auditionSeating-results-table.blade.php @@ -26,7 +26,7 @@ {{ $entry['schoolName'] }} - @if($entry['isDoubler']) + @if($entry['doubleData']) @include('tabulation.auditionSeating-doubler-block') {{-- DOUBLER
    --}} {{-- @foreach($entry['doubleData'] as $double)--}} diff --git a/tests/Feature/Services/DoublerServiceTest.php b/tests/Feature/Services/DoublerServiceTest.php index 694068e..edd5df3 100644 --- a/tests/Feature/Services/DoublerServiceTest.php +++ b/tests/Feature/Services/DoublerServiceTest.php @@ -23,38 +23,37 @@ it('throws an error if an invalid event is provided', function () { it('returns doublers for an event', function () { $concertEvent = Event::factory()->create(['name' => 'Concert Band', 'id' => 1000]); $jazzEvent = Event::factory()->create(['name' => 'Jazz Band', 'id' => 1001]); - - $auditionAS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1000, 'name' => 'Alto Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1000, ]); - $auditionTS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1000, 'name' => 'Tenor Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1001, ]); - $auditionBS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1000, 'name' => 'Baritone Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1002, ]); - $auditionCL = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1000, 'name' => 'Clarinet', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1003, ]); - $auditionBCL = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1000, 'name' => 'Bass Clarinet', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1004, ]); - $auditionJAS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1001, 'name' => 'Jazz Alto', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1005, ]); - $auditionJTS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1001, 'name' => 'Jazz Tenor', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1006, ]); - $auditionJBS = Audition::factory()->create([ + Audition::factory()->create([ 'event_id' => 1001, 'name' => 'Jazz Baritone', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1007, ]); $allSaxDude = Student::factory()->create(['grade' => 11, 'id' => 1000]); - $clarinetGal = Student::factory()->create(['grade' => 9, 'id' => 1001]); - $justAlto = Student::factory()->create(['grade' => 9, 'id' => 1002]); + Student::factory()->create(['grade' => 9, 'id' => 1001]); + Student::factory()->create(['grade' => 9, 'id' => 1002]); Entry::create(['student_id' => 1000, 'audition_id' => 1000]); Entry::create(['student_id' => 1000, 'audition_id' => 1001]); Entry::create(['student_id' => 1000, 'audition_id' => 1002]); @@ -67,13 +66,13 @@ it('returns doublers for an event', function () { Entry::create(['student_id' => 1002, 'audition_id' => 1005]); $return = $this->doublerService->doublersForEvent($concertEvent); - expect(count($return))->toBe(2); - expect($return[1000]['student']->id)->toBe($allSaxDude->id); - expect($return[1000]['entries']->count())->toBe(3); - expect($return[1001]['entries']->count())->toBe(2); + expect(count($return))->toBe(2) + ->and($return[1000]['student']->id)->toBe($allSaxDude->id) + ->and($return[1000]['entries']->count())->toBe(3) + ->and($return[1001]['entries']->count())->toBe(2); assertArrayNotHasKey(1002, $return); $return = $this->doublerService->doublersForEvent($jazzEvent); - expect(count($return))->toBe(1); - expect($return[1000]['student']->id)->toBe($allSaxDude->id); - expect($return[1000]['entries']->count())->toBe(3); + expect(count($return))->toBe(1) + ->and($return[1000]['student']->id)->toBe($allSaxDude->id) + ->and($return[1000]['entries']->count())->toBe(3); });