diff --git a/app/Http/Controllers/Tabulation/ScoreController.php b/app/Http/Controllers/Tabulation/ScoreController.php index d9b5931..b7e2013 100644 --- a/app/Http/Controllers/Tabulation/ScoreController.php +++ b/app/Http/Controllers/Tabulation/ScoreController.php @@ -2,10 +2,13 @@ namespace App\Http\Controllers\Tabulation; +use App\Actions\Tabulation\EnterScore; +use App\Exceptions\ScoreEntryException; use App\Http\Controllers\Controller; use App\Models\CalculatedScore; use App\Models\Entry; use App\Models\ScoreSheet; +use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; @@ -27,7 +30,8 @@ class ScoreController extends Controller return redirect()->back()->with('error', 'Cannot delete scores for an entry where seats are published'); } if ($score->entry->audition->hasFlag('advancement_published')) { - return redirect()->back()->with('error', 'Cannot delete scores for an entry where advancement is published'); + return redirect()->back()->with('error', + 'Cannot delete scores for an entry where advancement is published'); } $score->delete(); @@ -66,51 +70,35 @@ class ScoreController extends Controller compact('entry', 'judges', 'scoring_guide', 'subscores', 'existing_sheets')); } - public function saveEntryScoreSheet(Request $request, Entry $entry) + public function saveEntryScoreSheet(Request $request, Entry $entry, EnterScore $scoreRecorder) { - CalculatedScore::where('entry_id', $entry->id)->delete(); $publishedCheck = $this->checkIfPublished($entry); if ($publishedCheck) { return $publishedCheck; } + foreach ($request->all() as $key => $value) { + if (! str_contains($key, 'judge')) { + continue; + } + $judge_id = str_replace('judge', '', $key); + $judge = User::find($judge_id); + $existingScore = ScoreSheet::where('entry_id', $entry->id) + ->where('user_id', $judge->id)->first(); + if ($existingScore === null) { + $existingScore = false; + } + try { + $scoreRecorder($judge, $entry, $value, $existingScore); + } catch (ScoreEntryException $e) { + return redirect()->route('scores.entryScoreSheet', ['entry_id' => $entry->id]) + ->with('error', $e->getMessage()); + } + } + // Since we're entering a score, this apparently isn't a no show. $entry->removeFlag('no_show'); - $judges = $entry->audition->room->judges; - - $subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order'); - $scoringGuide = $entry->audition->scoringGuide; - $preparedScoreSheets = []; - foreach ($judges as $judge) { - $preparedScoreSheets[$judge->id]['user_id'] = $judge->id; - $preparedScoreSheets[$judge->id]['entry_id'] = $entry->id; - - $scoreValidation = $scoringGuide->validateScores($request->input('judge'.$judge->id)); - if ($scoreValidation != 'success') { - return redirect(url()->previous())->with('error', - $judge->full_name().': '.$scoreValidation)->with('oldScores', $request->all()); - } - - $scoreSubmission = $request->input('judge'.$judge->id); - $scoresToSave = []; - foreach ($subscores as $subscore) { - $scoresToSave[$subscore->id] = [ - 'subscore_id' => $subscore->id, - 'subscore_name' => $subscore->name, - 'score' => intval($scoreSubmission[$subscore->id]), - ]; - } - $preparedScoreSheets[$judge->id]['scores'] = $scoresToSave; - } - foreach ($preparedScoreSheets as $sheet) { - ScoreSheet::updateOrCreate( - ['entry_id' => $sheet['entry_id'], 'user_id' => $sheet['user_id']], - ['subscores' => $sheet['scores']] - ); - } - // TODO rewrite to use EnterScore action or clear score cache - - return redirect()->route('scores.chooseEntry')->with('success', count($preparedScoreSheets).' Scores saved'); + return redirect()->route('scores.chooseEntry')->with('success', 'Scores saved'); } protected function checkIfPublished($entry)