diff --git a/app/Http/Controllers/Tabulation/TabulationController.php b/app/Http/Controllers/Tabulation/TabulationController.php index 044ac9c..16950df 100644 --- a/app/Http/Controllers/Tabulation/TabulationController.php +++ b/app/Http/Controllers/Tabulation/TabulationController.php @@ -7,6 +7,7 @@ use App\Models\Audition; use App\Models\Entry; use App\Models\ScoreSheet; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Session; use function dump; use function redirect; @@ -19,26 +20,28 @@ class TabulationController extends Controller public function entryScoreSheet(Request $request) { + $existing_sheets = []; $entry = Entry::with(['student','audition.room.judges'])->find($request->input('entry_id')); $judges = $entry->audition->room->judges; + foreach ($judges as $judge) { + $scoreSheet = ScoreSheet::where('entry_id',$entry->id)->where('user_id',$judge->id)->first(); + if ($scoreSheet) { + Session::flash('caution','Scores exist for this entry. Now editing existing scores'); + $existing_sheets[$judge->id] = $scoreSheet; + } + } $scoring_guide = $entry->audition->scoringGuide; $subscores = $entry->audition->scoringGuide->subscores->sortBy('display_order'); if (!$entry) { return redirect()->route('tabulation.chooseEntry')->with('error','Entry not found'); } - return view('tabulation.entry_score_sheet', compact('entry','judges','scoring_guide','subscores')); + return view('tabulation.entry_score_sheet', compact('entry','judges','scoring_guide','subscores','existing_sheets')); } public function saveEntryScoreSheet(Request $request, Entry $entry) { $judges = $entry->audition->room->judges; - // Check if there is already a ScoreSheet for the entry $entry by judge $judge ( replaced this by making the last function updateOrCreate instead of Create) -// foreach ($judges as $judge) { -// $scoreSheet = ScoreSheet::where('entry_id',$entry->id)->where('user_id',$judge->id)->first(); -// if ($scoreSheet) { -// return redirect(url()->previous())->with('error', $judge->full_name() . ' already scored this entry'); -// } -// } + $subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order'); $scoringGuide = $entry->audition->scoringGuide; $preparedScoreSheets = []; @@ -64,8 +67,8 @@ class TabulationController extends Controller } foreach ($preparedScoreSheets as $sheet) { ScoreSheet::updateOrCreate( - ['entry_id' => $entry->id, 'user_id' => $judge->id], - ['subscores' => json_encode($scoresToSave)] + ['entry_id' => $sheet['entry_id'], 'user_id' => $sheet['user_id']], + ['subscores' => $sheet['scores']] ); } return redirect()->route('tabulation.chooseEntry')->with('success',count($preparedScoreSheets) . " Scores created"); diff --git a/app/Models/ScoreSheet.php b/app/Models/ScoreSheet.php index 370cb61..e86441a 100644 --- a/app/Models/ScoreSheet.php +++ b/app/Models/ScoreSheet.php @@ -4,9 +4,30 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class ScoreSheet extends Model { - use HasFactory; - protected $guarded = []; + protected $fillable = [ + 'user_id', + 'entry_id', + 'subscores' + ]; + + protected $casts = ['subscores' => 'json']; + + public function entry(): BelongsTo + { + return $this->belongsTo(Entry::class); + } + + public function judge(): BelongsTo + { + return $this->belongsTo(User::class, 'user_id'); + } + + public function getSubscore($id) + { + return $this->subscores[$id]['score'] ?? false; + } } diff --git a/resources/views/tabulation/entry_score_sheet.blade.php b/resources/views/tabulation/entry_score_sheet.blade.php index ac55965..28eb19c 100644 --- a/resources/views/tabulation/entry_score_sheet.blade.php +++ b/resources/views/tabulation/entry_score_sheet.blade.php @@ -32,6 +32,7 @@ @foreach($judges as $judge) + @php($existingSheet = $existing_sheets[$judge->id] ?? null) {{ $judge->full_name() }} @foreach($subscores as $subscore) @@ -41,7 +42,11 @@ id="j{{ $judge->id }}ss{{ $subscore->id }}" name="judge{{ $judge->id }}[{{ $subscore->id }}]" class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 judge{{$judge->id}}score" - @if($oldScores) value="{{ $oldScores['judge'.$judge->id][$subscore->id] }}" @endif + @if($oldScores) + value="{{ $oldScores['judge'.$judge->id][$subscore->id] }}" + @elseif($existingSheet) + value="{{ $existingSheet->getSubscore($subscore->id) }}" + @endif required {{-- onchange="judge{{$judge->id}}sum()"--}} > @@ -85,5 +90,12 @@ }); }); @endforeach + + window.onload = function() { + // Call the function for each judge + @foreach($judges as $judge) + calculateTotal({{ $judge->id }}); + @endforeach + }; diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index df88c43..0e7f87a 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -1,24 +1,9 @@ -@php use App\Models\Audition;use App\Models\School;use App\Models\SchoolEmailDomain;use App\Models\ScoringGuide;use App\Models\User;use App\Settings;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Session; @endphp +@php use App\Models\Audition;use App\Models\School;use App\Models\SchoolEmailDomain;use App\Models\ScoreSheet;use App\Models\ScoringGuide;use App\Models\User;use App\Settings;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Session; @endphp Test Page - @php - $auditions = Audition::with('entries')->get(); - @endphp -@foreach($auditions as $audition) - {{ $audition->name }} has {{ $audition->entries->count() }} entries.
- @if($audition->has_complete_draw()) - Draw is complete. - @endif - - @if($audition->has_partial_draw()) - Has a partial draw. - @endif - - @if($audition->has_no_draw()) - Has no draw. - @endif -

-@endforeach + @php($scoreSheet = ScoreSheet::find(11)) + @php(dump($scoreSheet->subscores[1]['score'])) + @php(dump($scoreSheet->getSubscore(1)))