auditionadmin/app/Http/Controllers/MonitorController.php

99 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Entry;
class MonitorController extends Controller
{
public function index()
{
if (! auth()->user()->hasFlag('monitor')) {
return redirect()->route('dashboard')->with('error', 'You are not assigned as a monitor');
}
$method = 'GET';
$formRoute = 'monitor.enterFlag';
$title = 'Flag Entry';
return view('tabulation.choose_entry', compact('method', 'formRoute', 'title'));
}
public function flagForm()
{
if (! auth()->user()->hasFlag('monitor')) {
return redirect()->route('dashboard')->with('error', 'You are not assigned as a monitor');
}
$validData = request()->validate([
'entry_id' => ['required', 'integer', 'exists:entries,id'],
]);
$entry = Entry::find($validData['entry_id']);
// If the entries audition is published, bounce out
if ($entry->audition->hasFlag('seats_published') || $entry->audition->hasFlag('advance_published')) {
return redirect()->route('monitor.index')->with('error', 'Cannot set flags while results are published');
}
// If entry has scores, bounce on out
if ($entry->scoreSheets()->count() > 0) {
return redirect()->route('monitor.index')->with('error', 'That entry has existing scores');
}
return view('monitor_entry_flag_form', compact('entry'));
}
public function storeFlag(Entry $entry)
{
if (! auth()->user()->hasFlag('monitor')) {
return redirect()->route('dashboard')->with('error', 'You are not assigned as a monitor');
}
// If the entries audition is published, bounce out
if ($entry->audition->hasFlag('seats_published') || $entry->audition->hasFlag('advance_published')) {
return redirect()->route('monitor.index')->with('error', 'Cannot set flags while results are published');
}
// If entry has scores, bounce on out
if ($entry->scoreSheets()->count() > 0) {
return redirect()->route('monitor.index')->with('error', 'That entry has existing scores');
}
$action = request()->input('action');
$result = match ($action) {
'failed-prelim' => $this->setFlag($entry, 'failed_prelim'),
'no-show' => $this->setFlag($entry, 'no_show'),
'clear' => $this->setFlag($entry, 'clear'),
default => redirect()->route('monitor.index')->with('error', 'Invalid action requested'),
};
if (! $result) {
return redirect()->route('monitor.index')->with('error', 'Failed to set flag');
}
return redirect()->route('monitor.index')->with('success', 'Flag set for entry #'.$entry->id);
}
private function setFlag(Entry $entry, string $flag)
{
if ($flag === 'no_show') {
$entry->removeFlag('failed_prelim');
$entry->addFlag('no_show');
return true;
}
if ($flag === 'failed_prelim') {
$entry->addFlag('failed_prelim');
$entry->addFlag('no_show');
return true;
}
if ($flag === 'clear') {
$entry->removeFlag('failed_prelim');
$entry->removeFlag('no_show');
return true;
}
return false;
}
}