143 lines
5.8 KiB
PHP
143 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Audition;
|
|
use App\Models\Entry;
|
|
use App\Models\JudgeAdvancementVote;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Tests\Feature\Models\ScoreSheet;
|
|
|
|
use function compact;
|
|
use function redirect;
|
|
use function url;
|
|
|
|
class JudgingController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$rooms = Auth::user()->judgingAssignments;
|
|
|
|
return view('judging.index', compact('rooms'));
|
|
}
|
|
|
|
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)
|
|
{
|
|
if ($request->user()->cannot('judge', $entry->audition)) {
|
|
return redirect()->route('judging.index')->with('error', 'You are not assigned to judge this entry');
|
|
}
|
|
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');
|
|
}
|
|
$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)
|
|
{
|
|
if ($request->user()->cannot('judge', $entry->audition)) {
|
|
abort(403, 'You are not assigned to judge this entry');
|
|
}
|
|
$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());
|
|
}
|
|
$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,
|
|
]);
|
|
|
|
$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)
|
|
{
|
|
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 entry');
|
|
}
|
|
Gate::authorize('update', $scoreSheet);
|
|
|
|
$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());
|
|
}
|
|
$scoreSheetArray = [];
|
|
foreach ($scoringGuide->subscores as $subscore) {
|
|
$scoreSheetArray[$subscore->id] = [
|
|
'score' => $request->input('score')[$subscore->id],
|
|
'subscore_id' => $subscore->id,
|
|
'subscore_name' => $subscore->name,
|
|
];
|
|
}
|
|
|
|
$scoreSheet->update([
|
|
'subscores' => $scoreSheetArray,
|
|
]);
|
|
|
|
$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 $e) {
|
|
return redirect(url()->previous())->with('error', 'Error saving advancement vote');
|
|
}
|
|
}
|
|
}
|
|
}
|