diff --git a/app/Http/Controllers/Tabulation/TabulationController.php b/app/Http/Controllers/Tabulation/TabulationController.php index 470ed91..d6154a8 100644 --- a/app/Http/Controllers/Tabulation/TabulationController.php +++ b/app/Http/Controllers/Tabulation/TabulationController.php @@ -5,7 +5,10 @@ namespace App\Http\Controllers\Tabulation; use App\Http\Controllers\Controller; use App\Models\Audition; use App\Models\Entry; +use App\Rules\ValidScoreArray; use Illuminate\Http\Request; +use function dump; +use function redirect; class TabulationController extends Controller { @@ -26,5 +29,17 @@ class TabulationController extends Controller return view('tabulation.entry_score_sheet', compact('entry','judges','scoring_guide','subscores')); } + public function saveEntryScoreSheet(Request $request, Entry $entry) + { + $judges = $entry->audition->room->judges; + $subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order'); + $scoringGuide = $entry->audition->scoringGuide; + foreach ($judges as $judge) { + $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()); + } + } + } } diff --git a/app/Models/ScoringGuide.php b/app/Models/ScoringGuide.php index 89d0eaf..67206f9 100644 --- a/app/Models/ScoringGuide.php +++ b/app/Models/ScoringGuide.php @@ -5,6 +5,12 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use phpDocumentor\Reflection\Types\Boolean; +use PhpParser\Node\Expr\Cast\Bool_; +use PhpParser\Node\Scalar\String_; +use function array_diff_key; +use function array_key_exists; +use function is_null; class ScoringGuide extends Model { @@ -20,4 +26,20 @@ class ScoringGuide extends Model { return $this->hasMany(SubscoreDefinition::class); } + + public function validateScores(Array $prospective_score) + { + foreach ($this->subscores as $subscore) { + if (! array_key_exists($subscore->id,$prospective_score)) return "A score must be provided for " . $subscore->name; + if (is_null($prospective_score[$subscore->id])) return "A score must be provided for " . $subscore->name; + if ($prospective_score[$subscore->id] > $subscore->max_score) return "The " . $subscore->name . " score must be less than or equal to " . $subscore->max_score; + } + + $subscore_ids = $this->subscores->pluck('id')->flip()->all(); + $diff = array_diff_key($prospective_score, $subscore_ids); + if (!empty($diff)) return "Invalid scores submitted"; + + return 'success'; + // TODO this probably needs to be rewritten as a validation rule + } } diff --git a/database/migrations/2024_06_07_054115_add_user_entry_unique_index_to_score_sheets_table.php b/database/migrations/2024_06_07_054115_add_user_entry_unique_index_to_score_sheets_table.php new file mode 100644 index 0000000..34559a9 --- /dev/null +++ b/database/migrations/2024_06_07_054115_add_user_entry_unique_index_to_score_sheets_table.php @@ -0,0 +1,28 @@ +unique('user_id','entry_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('score_sheets', function (Blueprint $table) { + // + }); + } +}; diff --git a/resources/views/components/layout/app.blade.php b/resources/views/components/layout/app.blade.php index 124f5f9..a1685e7 100644 --- a/resources/views/components/layout/app.blade.php +++ b/resources/views/components/layout/app.blade.php @@ -34,7 +34,7 @@ @endif - +{{-- @php(dump(session()->get('oldScores')))--}}
diff --git a/resources/views/tabulation/entry_score_sheet.blade.php b/resources/views/tabulation/entry_score_sheet.blade.php index 6dc53d3..a0d7f9e 100644 --- a/resources/views/tabulation/entry_score_sheet.blade.php +++ b/resources/views/tabulation/entry_score_sheet.blade.php @@ -1,4 +1,5 @@ + @php($oldScores = session()->get('oldScores')) {{-- TODO Need to handle if null --}} Entry Score Sheet @@ -10,29 +11,31 @@ - + Judges @foreach($subscores as $subscore) - {{ $subscore->name }} + +

{{ $subscore->name }}Max: {{ $subscore->max_score }}

+
@endforeach - Total (unweighted) @foreach($judges as $judge) - + {{ $judge->full_name() }} @foreach($subscores as $subscore) + @if($oldScores) value="{{ $oldScores['judge'.$judge->id][$subscore->id] }}" @endif + > @endforeach diff --git a/routes/web.php b/routes/web.php index c684be9..5640ef0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,6 +25,7 @@ Route::middleware(['auth','verified',CheckIfCanTab::class])->prefix('tabulation/ Route::get('/enter_scores','chooseEntry')->name('tabulation.chooseEntry'); Route::get('/record_noshow','chooseEntry'); Route::get('/entries','entryScoreSheet'); + Route::post('/entries/{entry}','saveEntryScoreSheet'); }); });