From d8e20d5f64f2ee1b431662c1eae46b216e663447 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 12 Jun 2024 22:15:28 -0500 Subject: [PATCH] Move scoring functions to their own controller. ScoringGuide cache service and listener to update --- app/Events/ScoringGuideChange.php | 36 +++++++++ .../Admin/ScoringGuideController.php | 8 +- .../Tabulation/ScoreController.php | 78 +++++++++++++++++++ .../Tabulation/TabulationController.php | 64 --------------- app/Listeners/RefreshScoringGuideCache.php | 30 +++++++ app/Providers/AppServiceProvider.php | 8 +- app/Services/ScoringGuideCacheService.php | 31 ++++++++ resources/views/admin/entries/edit.blade.php | 2 +- .../layout/navbar/menus/tabulation.blade.php | 2 +- .../views/tabulation/choose_entry.blade.php | 8 +- .../tabulation/entry_score_sheet.blade.php | 2 +- routes/web.php | 22 ++++-- 12 files changed, 210 insertions(+), 81 deletions(-) create mode 100644 app/Events/ScoringGuideChange.php create mode 100644 app/Http/Controllers/Tabulation/ScoreController.php create mode 100644 app/Listeners/RefreshScoringGuideCache.php create mode 100644 app/Services/ScoringGuideCacheService.php diff --git a/app/Events/ScoringGuideChange.php b/app/Events/ScoringGuideChange.php new file mode 100644 index 0000000..b265ad9 --- /dev/null +++ b/app/Events/ScoringGuideChange.php @@ -0,0 +1,36 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/app/Http/Controllers/Admin/ScoringGuideController.php b/app/Http/Controllers/Admin/ScoringGuideController.php index ac08428..374824b 100644 --- a/app/Http/Controllers/Admin/ScoringGuideController.php +++ b/app/Http/Controllers/Admin/ScoringGuideController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Events\ScoringGuideChange; use App\Http\Controllers\Controller; use App\Models\ScoringGuide; use App\Models\SubscoreDefinition; @@ -37,6 +38,8 @@ class ScoringGuideController extends Controller 'name' => request('name') ]); + ScoringGuideChange::dispatch(); + return redirect('/admin/scoring'); } @@ -62,6 +65,7 @@ class ScoringGuideController extends Controller $guide->update([ 'name' => request('name') ]); + ScoringGuideChange::dispatch(); return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Scoring guide updated'); } @@ -93,7 +97,7 @@ class ScoringGuideController extends Controller 'for_seating' => $for_seating, 'for_advance' => $for_advance, ]); - + ScoringGuideChange::dispatch(); return redirect('/admin/scoring/guides/' . $guide->id . '/edit' )->with('success','Subscore added'); } @@ -105,6 +109,7 @@ class ScoringGuideController extends Controller $subscore = SubscoreDefinition::find($id); $subscore->update(['display_order' => $index]); } + ScoringGuideChange::dispatch(); return response()->json(['status'=>'success']); } @@ -117,6 +122,7 @@ class ScoringGuideController extends Controller $subscore = SubscoreDefinition::find($id); $subscore->update(['tiebreak_order' => $index]); } + ScoringGuideChange::dispatch(); return response()->json(['status'=>'success']); } diff --git a/app/Http/Controllers/Tabulation/ScoreController.php b/app/Http/Controllers/Tabulation/ScoreController.php new file mode 100644 index 0000000..5504d77 --- /dev/null +++ b/app/Http/Controllers/Tabulation/ScoreController.php @@ -0,0 +1,78 @@ +delete(); + return redirect()->back()->with('success','Score Deleted'); + } + + 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','existing_sheets')); + } + + public function saveEntryScoreSheet(Request $request, Entry $entry) + { + $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']] + ); + } + return redirect()->route('scores.chooseEntry')->with('success',count($preparedScoreSheets) . " Scores saved"); + } + +} diff --git a/app/Http/Controllers/Tabulation/TabulationController.php b/app/Http/Controllers/Tabulation/TabulationController.php index c6ecbce..fc7e5b3 100644 --- a/app/Http/Controllers/Tabulation/TabulationController.php +++ b/app/Http/Controllers/Tabulation/TabulationController.php @@ -14,71 +14,7 @@ use function redirect; class TabulationController extends Controller { - public function chooseEntry(Request $request) - { - return view('tabulation.choose_entry'); - } - public function destroyScore(ScoreSheet $score) { - $score->delete(); - return redirect()->back()->with('success','Score Deleted'); - } - - 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','existing_sheets')); - } - - public function saveEntryScoreSheet(Request $request, Entry $entry) - { - $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']] - ); - } - return redirect()->route('tabulation.chooseEntry')->with('success',count($preparedScoreSheets) . " Scores created"); - } public function status() { diff --git a/app/Listeners/RefreshScoringGuideCache.php b/app/Listeners/RefreshScoringGuideCache.php new file mode 100644 index 0000000..f7480c2 --- /dev/null +++ b/app/Listeners/RefreshScoringGuideCache.php @@ -0,0 +1,30 @@ +cacheKey, function () { + return ScoringGuide::with('subscores')->get(); + }); + } + + public function refreshCache() + { + Cache::forget($this->cacheKey); + $this->getScoringGuides(); + } +} diff --git a/resources/views/admin/entries/edit.blade.php b/resources/views/admin/entries/edit.blade.php index 249b14a..bcf45e0 100644 --- a/resources/views/admin/entries/edit.blade.php +++ b/resources/views/admin/entries/edit.blade.php @@ -46,7 +46,7 @@ @endforeach @if(! $score->isValid()) -
+ @csrf @method('DELETE') INVALID SCORE - DELETE diff --git a/resources/views/components/layout/navbar/menus/tabulation.blade.php b/resources/views/components/layout/navbar/menus/tabulation.blade.php index 25fe9cc..9e1906f 100644 --- a/resources/views/components/layout/navbar/menus/tabulation.blade.php +++ b/resources/views/components/layout/navbar/menus/tabulation.blade.php @@ -20,7 +20,7 @@ -->
diff --git a/resources/views/tabulation/choose_entry.blade.php b/resources/views/tabulation/choose_entry.blade.php index d11664b..c7a88e9 100644 --- a/resources/views/tabulation/choose_entry.blade.php +++ b/resources/views/tabulation/choose_entry.blade.php @@ -3,12 +3,12 @@ Choose Entry
- + + + Select + - - Select -
diff --git a/resources/views/tabulation/entry_score_sheet.blade.php b/resources/views/tabulation/entry_score_sheet.blade.php index b09ab37..8aedd64 100644 --- a/resources/views/tabulation/entry_score_sheet.blade.php +++ b/resources/views/tabulation/entry_score_sheet.blade.php @@ -11,7 +11,7 @@ - + diff --git a/routes/web.php b/routes/web.php index 0110ac9..509f2db 100644 --- a/routes/web.php +++ b/routes/web.php @@ -27,14 +27,19 @@ Route::middleware(['auth','verified',CheckIfCanJudge::class])->prefix('judging') Route::post('/entry/{entry}','saveScoreSheet'); }); -// Score Tabulation Routes -Route::middleware(['auth','verified',CheckIfCanTab::class])->prefix('tabulation/')->group(function() { +// Tabulation Routes +Route::middleware(['auth','verified',CheckIfCanTab::class])->group(function() { + + // Score Management + Route::prefix('scores/')->controller(\App\Http\Controllers\Tabulation\ScoreController::class)->group(function() { + Route::get('/choose_entry','chooseEntry')->name('scores.chooseEntry'); + Route::get('/entry','entryScoreSheet')->name('scores.entryScoreSheet'); + Route::post('/entry/{entry}','saveEntryScoreSheet')->name('scores.saveEntryScoreSheet'); + Route::delete('/{score}',[\App\Http\Controllers\Tabulation\ScoreController::class,'destroyScore'])->name('scores.destroy'); + }); + // Generic Tabulation Routes - Route::controller(\App\Http\Controllers\Tabulation\TabulationController::class)->group(function() { - Route::get('/enter_scores','chooseEntry')->name('tabulation.chooseEntry'); - Route::get('/record_noshow','chooseEntry'); - Route::get('/entries','entryScoreSheet'); - Route::post('/entries/{entry}','saveEntryScoreSheet'); + Route::prefix('tabulation/')->controller(\App\Http\Controllers\Tabulation\TabulationController::class)->group(function() { Route::get('/status','status'); Route::get('/auditions/{audition}','auditionSeating'); }); @@ -45,7 +50,8 @@ Route::middleware(['auth','verified',CheckIfCanTab::class])->prefix('tabulation/ // Admin Routes Route::middleware(['auth','verified',CheckIfAdmin::class])->prefix('admin/')->group(function() { Route::view('/','admin.dashboard'); - Route::delete('/scores/{score}',[TabulationController::class,'destroyScore']); + + Route::post('/auditions/roomUpdate',[\App\Http\Controllers\Admin\AuditionController::class,'roomUpdate']); // Endpoint for JS assigning auditions to rooms Route::post('/scoring/assign_guide_to_audition',[\App\Http\Controllers\Admin\AuditionController::class,'scoringGuideUpdate']); // Endpoint for JS assigning scoring guides to auditions