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

174 lines
7.0 KiB
PHP

<?php
namespace App\Http\Controllers\Judging;
use App\Actions\Tabulation\EnterScore;
use App\Exceptions\AuditionServiceException;
use App\Exceptions\ScoreEntryException;
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 Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use function compact;
use function redirect;
use function url;
class JudgingController extends Controller
{
protected AuditionService $auditionService;
public function __construct(AuditionService $auditionService)
{
$this->auditionService = $auditionService;
}
public function index()
{
$rooms = Auth::user()->judgingAssignments()->with('auditions')->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)
{
if ($request->user()->cannot('judge', $audition)) {
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge this 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.');
}
$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)) {
abort(403, 'You are not assigned to judge this entry');
}
// Validate form data
try {
$subscores = $this->auditionService->getSubscores($entry->audition);
} catch (AuditionServiceException $e) {
return redirect()->back()->with('error', 'Unable to get subscores - '.$e->getMessage());
}
$validationChecks = [];
foreach ($subscores as $subscore) {
$validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score;
}
$validatedData = $request->validate($validationChecks);
// Enter the score
try {
$enterScore(Auth::user(), $entry, $validatedData['score']);
} catch (ScoreEntryException $e) {
return redirect()->back()->with('error', 'Error saving score - '.$e->getMessage());
}
// Deal with an advancement vote if needed
$this->advancementVote($request, $entry);
return redirect('/judging/audition/'.$entry->audition_id)->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)) {
abort(403, 'You are not assigned to judge this entry');
}
$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
try {
$subscores = $this->auditionService->getSubscores($entry->audition);
} catch (AuditionServiceException $e) {
return redirect()->back()->with('error', 'Error getting subscores - '.$e->getMessage());
}
$validationChecks = [];
foreach ($subscores as $subscore) {
$validationChecks['score'.'.'.$subscore->id] = 'required|integer|max:'.$subscore->max_score;
}
$validatedData = $request->validate($validationChecks);
// Enter the score
try {
$enterScore(Auth::user(), $entry, $validatedData['score'], $scoreSheet);
} catch (ScoreEntryException $e) {
return redirect()->back()->with('error', 'Error updating score - '.$e->getMessage());
}
$this->advancementVote($request, $entry);
return redirect('/judging/audition/'.$entry->audition_id)->with('success',
'Updated scores for '.$entry->audition->name.' '.$entry->draw_number);
}
protected function advancementVote(Request $request, Entry $entry)
{
if ($request->user()->cannot('judge', $entry->audition)) {
abort(403, 'You are not assigned to judge this entry');
}
if ($entry->for_advancement and auditionSetting('advanceTo')) {
$request->validate([
'advancement-vote' => ['required', 'in:yes,no,dq'],
]);
try {
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'),
]);
} catch (Exception) {
return redirect(url()->previous())->with('error', 'Error saving advancement vote');
}
}
return null;
}
}