From 8b4fd6870c8aa029a9b6b8830cd6950492e52528 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 17 Jul 2024 20:25:57 -0500 Subject: [PATCH] Move functions out of ScoreSheet Model Closes #35 --- .../Controllers/Admin/EntryController.php | 13 ++++++++---- app/Models/ScoreSheet.php | 21 ------------------- app/Services/ScoreService.php | 10 ++++++++- resources/views/admin/entries/edit.blade.php | 7 +++---- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/Admin/EntryController.php b/app/Http/Controllers/Admin/EntryController.php index 78c7d6d..4aa87a2 100644 --- a/app/Http/Controllers/Admin/EntryController.php +++ b/app/Http/Controllers/Admin/EntryController.php @@ -4,7 +4,7 @@ namespace App\Http\Controllers\Admin; use App\Actions\Entries\CreateEntry; use App\Actions\Entries\UpdateEntry; -use App\Actions\Tabulation\CalculateEntryScore; +use App\Actions\Tabulation\CalculateScoreSheetTotal; use App\Exceptions\ManageEntryException; use App\Http\Controllers\Controller; use App\Models\Audition; @@ -12,6 +12,7 @@ use App\Models\Entry; use App\Models\School; use App\Models\Seat; use App\Models\Student; +use App\Services\ScoreService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -123,7 +124,7 @@ class EntryController extends Controller return redirect(route('admin.entries.index'))->with('success', 'The entry has been added.'); } - public function edit(Entry $entry, CalculateEntryScore $calculator) + public function edit(Entry $entry, CalculateScoreSheetTotal $calculator, ScoreService $scoreService) { if ($entry->audition->hasFlag('seats_published')) { return to_route('admin.entries.index')->with('error', @@ -138,9 +139,13 @@ class EntryController extends Controller $students = Student::with('school')->orderBy('last_name')->orderBy('first_name')->get(); $auditions = Audition::orderBy('score_order')->get(); $scores = $entry->scoreSheets()->with('audition', 'judge')->get(); - $scores->each(fn ($score) => $score->entry = $entry); + foreach ($scores as $score) { + $score->entry = $entry; + $score->valid = $scoreService->isScoreSheetValid($score); + $score->seating_total_score = $calculator('seating', $entry, $score->judge)[0]; + $score->advancement_total_score = $calculator('advancement', $entry, $score->judge)[0]; + } - // return view('admin.entries.edit', ['entry' => $entry, 'students' => $students, 'auditions' => $auditions]); return view('admin.entries.edit', compact('entry', 'students', 'auditions', 'scores')); } diff --git a/app/Models/ScoreSheet.php b/app/Models/ScoreSheet.php index b80b95b..5c90fca 100644 --- a/app/Models/ScoreSheet.php +++ b/app/Models/ScoreSheet.php @@ -2,11 +2,9 @@ namespace App\Models; -use App\Actions\Tabulation\CalculateScoreSheetTotal; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasOneThrough; -use Illuminate\Support\Facades\App; class ScoreSheet extends Model { @@ -39,23 +37,4 @@ class ScoreSheet extends Model 'audition_id' // Local key on the intermediate model (Entry) ); } - - public function getSubscore($id) - { - return $this->subscores[$id]['score'] ?? false; - } - - public function isValid() - { - $judges = $this->audition->judges; - - return $judges->contains('id', $this->judge->id); - } - - public function totalScore($mode) - { - $calculator = App::make(CalculateScoreSheetTotal::class); - - return $calculator($mode, $this->entry, $this->judge); - } } diff --git a/app/Services/ScoreService.php b/app/Services/ScoreService.php index afd29c2..bc48b16 100644 --- a/app/Services/ScoreService.php +++ b/app/Services/ScoreService.php @@ -3,7 +3,7 @@ namespace App\Services; use App\Models\Entry; -use App\Models\User; +use App\Models\ScoreSheet; class ScoreService { @@ -14,6 +14,7 @@ class ScoreService { } + public function isEntryFullyScored(Entry $entry): bool { $requiredJudges = $entry->audition->judges()->count(); @@ -21,4 +22,11 @@ class ScoreService return $requiredJudges === $scoreSheets; } + + public function isScoreSheetValid(ScoreSheet $sheet) + { + $judges = $sheet->audition->judges; + + return $judges->contains('id', $sheet->judge->id); + } } diff --git a/resources/views/admin/entries/edit.blade.php b/resources/views/admin/entries/edit.blade.php index 8c96f7e..f4fdd6c 100644 --- a/resources/views/admin/entries/edit.blade.php +++ b/resources/views/admin/entries/edit.blade.php @@ -84,7 +84,6 @@
@foreach($scores as $score) - @php($score->isValid())
{{ $score->judge->full_name() }} @@ -106,16 +105,16 @@

{{ auditionSetting('auditionAbbreviation') }} Total - {{ $score->totalScore('seating')[0] }} + {{ $score->seating_total_score }}

@if( auditionSetting('advanceTo'))

{{ auditionSetting('advanceTo') }} Total - {{ $score->totalScore('advancement')[0] }} + {{ $score->advancement_total_score }}

@endif - @if(! $score->isValid()) + @if(! $score->valid))

This score is invalid