From 1f4f919c488e4b4e10a2819dc6865e2302c85085 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Sat, 8 Jun 2024 21:53:58 -0500 Subject: [PATCH] Judging working --- app/Http/Controllers/JudgingController.php | 29 +++++++++++++++---- app/Models/Entry.php | 12 ++++++++ app/Models/User.php | 16 ++++++++++ app/Policies/ScoreSheetPolicy.php | 6 ++-- .../judging/audition_entry_list.blade.php | 17 +++++++++-- resources/views/test.blade.php | 19 +++++++++--- 6 files changed, 85 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/JudgingController.php b/app/Http/Controllers/JudgingController.php index 9fb260f..a531089 100644 --- a/app/Http/Controllers/JudgingController.php +++ b/app/Http/Controllers/JudgingController.php @@ -4,8 +4,10 @@ namespace App\Http\Controllers; use App\Models\Audition; use App\Models\Entry; +use App\Models\ScoreSheet; use App\Models\SubscoreDefinition; use App\Models\User; +use Illuminate\Support\Facades\Gate; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use function compact; @@ -23,9 +25,9 @@ class JudgingController extends Controller public function auditionEntryList(Audition $audition) { // TODO verify user is assigned to judge this audition - $entries = Entry::where('audition_id','=',$audition->id)->orderBy('draw_number')->get(); - - return view('judging.audition_entry_list', compact('audition','entries')); + $entries = Entry::where('audition_id','=',$audition->id)->orderBy('draw_number')->with('audition')->get(); + $subscores = $audition->scoringGuide->subscores()->orderBy('display_order')->get(); + return view('judging.audition_entry_list', compact('audition','entries','subscores')); } public function entryScoreSheet(Entry $entry) @@ -36,13 +38,30 @@ class JudgingController extends Controller public function saveScoreSheet(Request $request, Entry $entry) { + Gate::authorize('create',[ScoreSheet::class,$entry]); // TODO verify user is assigned to judge this audition - $scoringGuide = $entry->audition->scoringGuide; + $scoringGuide = $entry->audition->scoringGuide()->with('subscores')->first(); $scoreValidation = $scoringGuide->validateScores($request->input('score')); if ($scoreValidation != 'success') { return redirect(url()->previous())->with('error', $scoreValidation)->with('oldScores',$request->all()); } - dd($scoreValidation); + $scoreSheetArray = []; + foreach($scoringGuide->subscores as $subscore) { + $scoreSheetArray[$subscore->id] = [ + 'score' => $request->input('score')[$subscore->id], + 'subscore_id' => $subscore->id, + 'subscore_name' => $subscore->name + ]; + } + + ScoreSheet::create([ + 'user_id' => Auth::user()->id, + 'entry_id' => $entry->id, + 'subscores' => $scoreSheetArray + ]); + + return redirect('/judging/audition/' . $entry->audition_id)->with('success','Entered scores for ' . $entry->audition->name . ' ' . $entry->draw_number); + } diff --git a/app/Models/Entry.php b/app/Models/Entry.php index fc6a7a5..f6ac8c4 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -5,6 +5,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOneThrough; class Entry extends Model @@ -32,4 +34,14 @@ class Entry extends Model 'student_id', 'school_id'); } + + public function scoreSheets(): HasMany + { + return $this->hasMany(ScoreSheet::class); + } + + public function scoreFromJudge($user): ScoreSheet|null + { + return $this->scoreSheets()->where('user_id','=',$user)->first() ?? null; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 0072943..9e29c8b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -129,4 +130,19 @@ class User extends Authenticatable implements MustVerifyEmail if ($this->is_admin) return true; return $this->is_tab; } + + public function scoreSheets(): HasMany + { + return $this->hasMany(ScoreSheet::class); + } + + public function scoresForEntry($entry) + { + return $this->scoreSheets->where('entry_id','=',$entry)->first()?->subscores; + } + + public function timeForEntryScores($entry) + { + return $this->scoreSheets->where('entry_id','=',$entry)->first()?->created_at; + } } diff --git a/app/Policies/ScoreSheetPolicy.php b/app/Policies/ScoreSheetPolicy.php index bab4ca1..3b3f900 100644 --- a/app/Policies/ScoreSheetPolicy.php +++ b/app/Policies/ScoreSheetPolicy.php @@ -2,6 +2,7 @@ namespace App\Policies; +use App\Models\Entry; use App\Models\ScoreSheet; use App\Models\User; use Illuminate\Auth\Access\Response; @@ -27,9 +28,10 @@ class ScoreSheetPolicy /** * Determine whether the user can create models. */ - public function create(User $user): bool + public function create(User $user, Entry $entry): bool { - // + $room = $entry->audition->room; + return $user->judgingAssignments->contains($room); } /** diff --git a/resources/views/judging/audition_entry_list.blade.php b/resources/views/judging/audition_entry_list.blade.php index 4b87eea..878ff4c 100644 --- a/resources/views/judging/audition_entry_list.blade.php +++ b/resources/views/judging/audition_entry_list.blade.php @@ -5,10 +5,11 @@ - Entry - @foreach($audition->scoringGuide->subscores()->orderBy('display_order')->get(); as $subscore) - {{ $subscore->name }} + Entry + @foreach($subscores as $subscore) + {{ $subscore->name }} @endforeach + Timestamp @@ -19,6 +20,16 @@ {{ $audition->name }} {{ $entry->draw_number }} + @foreach($subscores as $subscore) + + @php + if( $x = Auth::user()->scoresForEntry($entry->id)) echo $x[$subscore->id]['score']; + @endphp + + @endforeach + + {{ Auth::user()->timeForEntryScores($entry->id)?->setTimezone('America/Chicago')->format('m/d/y H:i') }} + @endforeach diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index da16902..48bebb5 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -1,5 +1,5 @@ @php use App\Models\Audition; - use App\Models\School; + use App\Models\Entry;use App\Models\School; use App\Models\SchoolEmailDomain; use App\Models\ScoreSheet; use App\Models\ScoringGuide; @@ -8,13 +8,24 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Session; - @endphp +@endphp Test Page @php - $user = User::find(1); - dump($user->isJudge()); + dump(Auth::user()->scoreSheets->where('entry_id','=','997')->first()->subscores[6]['score']); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)[6]['score']); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)[7]['score']); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)[8]['score']); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)[9]['score']); + echo "-----"; + dump(Auth::user()->scoresForEntry(997)[10]['score']); @endphp