auditionadmin/app/Http/Controllers/Judging/JudgingController.php

160 lines
6.4 KiB
PHP

<?php
namespace App\Http\Controllers\Judging;
use App\Actions\Tabulation\EnterScore;
use App\Http\Controllers\Controller;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\JudgeAdvancementVote;
use App\Models\ScoreSheet;
use App\Services\AuditionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use function compact;
use function redirect;
class JudgingController extends Controller
{
protected AuditionService $auditionService;
public function __construct(AuditionService $auditionService)
{
$this->auditionService = $auditionService;
}
public function index()
{
$rooms = Auth::user()->judgingAssignments()->with('auditions')->with('prelimAuditions')->get();
$bonusScoresToJudge = Auth::user()->bonusJudgingAssignments()->with('auditions')->get();
//$rooms->load('auditions');
return view('judging.index', compact('rooms', 'bonusScoresToJudge'));
}
public function auditionEntryList(Request $request, Audition $audition)
{
// TODO: Add error message if scoring guide is not set
if ($request->user()->cannot('judge', $audition)) {
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge that audition');
}
$entries = Entry::where('audition_id', '=', $audition->id)->orderBy('draw_number')->with('audition')->get();
$subscores = $audition->scoringGuide->subscores()->orderBy('display_order')->get();
$votes = JudgeAdvancementVote::where('user_id', Auth::id())->get();
$published = $audition->hasFlag('advancement_published') || $audition->hasFlag('seats_published');
return view('judging.audition_entry_list', compact('audition', 'entries', 'subscores', 'votes', 'published'));
}
public function entryScoreSheet(Request $request, Entry $entry)
{
// Turn away users not assigned to judge this entry
if ($request->user()->cannot('judge', $entry->audition)) {
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge this entry');
}
// Turn away users if the audition is published
if ($entry->audition->hasFlag('seats_published') || $entry->audition->hasFlag('advancement_published')) {
return redirect()->route('judging.auditionEntryList', $entry->audition)->with('error',
'Scores for entries in published auditions cannot be modified');
}
// Turn away users if the entry is flagged as a no-show
if ($entry->hasFlag('no_show')) {
return redirect()->route('judging.auditionEntryList', $entry->audition)->with('error',
'The requested entry is marked as a no-show. Scores cannot be entered.');
}
// Turn away users if the entry is flagged as a failed-prelim
if ($entry->hasFlag('failed_prelim')) {
return redirect()->route('judging.auditionEntryList', $entry->audition)->with('error',
'The requested entry is marked as having failed a prelim. Scores cannot be entered.');
}
$oldSheet = ScoreSheet::where('user_id', Auth::id())->where('entry_id', $entry->id)->value('subscores') ?? null;
$oldVote = JudgeAdvancementVote::where('user_id', Auth::id())->where('entry_id', $entry->id)->first();
$oldVote = $oldVote ? $oldVote->vote : 'noVote';
return view('judging.entry_score_sheet', compact('entry', 'oldSheet', 'oldVote'));
}
public function saveScoreSheet(Request $request, Entry $entry, EnterScore $enterScore)
{
if ($request->user()->cannot('judge', $entry->audition)) {
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge this entry');
}
// Validate form data
$subscores = $entry->audition->subscoreDefinitions;
$validationChecks = [];
foreach ($subscores as $subscore) {
$validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score;
}
$validatedData = $request->validate($validationChecks);
// Enter the score
/** @noinspection PhpUnhandledExceptionInspection */
$enterScore(Auth::user(), $entry, $validatedData['score']);
// Deal with an advancement vote if needed
$this->advancementVote($request, $entry);
return redirect(route('judging.auditionEntryList', $entry->audition))->with('success',
'Entered scores for '.$entry->audition->name.' '.$entry->draw_number);
}
public function updateScoreSheet(Request $request, Entry $entry, EnterScore $enterScore)
{
if ($request->user()->cannot('judge', $entry->audition)) {
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge this entry');
}
// We can't update a scoresheet that doesn't exist
$scoreSheet = ScoreSheet::where('user_id', Auth::id())->where('entry_id', $entry->id)->first();
if (! $scoreSheet) {
return redirect()->back()->with('error', 'Attempt to edit non existent score sheet');
}
Gate::authorize('update', $scoreSheet);
// Validate form data
$subscores = $entry->audition->subscoreDefinitions;
$validationChecks = [];
foreach ($subscores as $subscore) {
$validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score;
}
$validatedData = $request->validate($validationChecks);
// Enter the score
$enterScore(Auth::user(), $entry, $validatedData['score'], $scoreSheet);
$this->advancementVote($request, $entry);
return redirect(route('judging.auditionEntryList', $entry->audition))->with('success',
'Updated scores for '.$entry->audition->name.' '.$entry->draw_number);
}
protected function advancementVote(Request $request, Entry $entry)
{
if ($entry->for_advancement and auditionSetting('advanceTo')) {
$request->validate([
'advancement-vote' => ['required', 'in:yes,no,dq'],
]);
JudgeAdvancementVote::where('user_id', Auth::id())->where('entry_id', $entry->id)->delete();
JudgeAdvancementVote::create([
'user_id' => Auth::user()->id,
'entry_id' => $entry->id,
'vote' => $request->input('advancement-vote'),
]);
}
return null;
}
}