99 lines
3.7 KiB
PHP
99 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Tabulation;
|
|
|
|
use App\Actions\Tabulation\EnterBonusScore;
|
|
use App\Actions\Tabulation\GetBonusScoreRelatedEntries;
|
|
use App\Exceptions\ScoreEntryException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\BonusScore;
|
|
use App\Models\Entry;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use function request;
|
|
|
|
class BonusScoreController extends Controller
|
|
{
|
|
public function chooseEntry()
|
|
{
|
|
$method = 'GET';
|
|
$formRoute = 'bonus-scores.entryBonusScoreSheet';
|
|
$title = 'Enter Bonus Scores';
|
|
|
|
return view('tabulation.choose_entry', compact('method', 'formRoute', 'title'));
|
|
}
|
|
|
|
public function entryBonusScoreSheet(GetBonusScoreRelatedEntries $getRelatedEntries)
|
|
{
|
|
$validData = request()->validate([
|
|
'entry_id' => 'required|exists:entries,id',
|
|
]);
|
|
$entry = Entry::find($validData['entry_id']);
|
|
$bonusScoreDefinition = $entry->audition->bonusScore->first();
|
|
$assignedJudges = $bonusScoreDefinition->judges;
|
|
$relatedEntries = $getRelatedEntries($entry);
|
|
$existingScores = [];
|
|
foreach ($relatedEntries as $related) {
|
|
$existingScores[$related->id] = BonusScore::where('entry_id', $related->id)
|
|
->with('judge')
|
|
->with('entry')
|
|
->with('originallyScoredEntry')
|
|
->get();
|
|
}
|
|
|
|
return view('tabulation.bonus-score-sheet',
|
|
compact('entry', 'bonusScoreDefinition', 'assignedJudges', 'existingScores', 'relatedEntries'));
|
|
}
|
|
|
|
public function saveEntryBonusScoreSheet(Entry $entry, GetBonusScoreRelatedEntries $getRelatedEntries, EnterBonusScore $saveBonusScore)
|
|
{
|
|
if ($entry->audition->hasFlag('seats_published') || $entry->audition->hasFlag('results_published')) {
|
|
return redirect()->route('bonus-scores.entryBonusScoreSheet', ['entry_id' => $entry->id])->with('error', 'Bonus scores cannot be modified after results are published');
|
|
}
|
|
$validData = request()->validate([
|
|
'judge_id' => 'required|exists:users,id',
|
|
'entry_id' => 'required|exists:entries,id',
|
|
'score' => 'nullable|numeric',
|
|
]);
|
|
|
|
$judge = User::find($validData['judge_id']);
|
|
$entry = Entry::find($validData['entry_id']);
|
|
$relatedEntries = $getRelatedEntries($entry);
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// Delete existing bonus scores for the entries by the judge
|
|
foreach ($relatedEntries as $related) {
|
|
BonusScore::where('entry_id', $related->id)->where('user_id', $judge->id)->delete();
|
|
}
|
|
|
|
// If no score was submitted, were going to just stop at deleting the scores
|
|
if (! $validData['score'] == null) {
|
|
// Set the new score
|
|
try {
|
|
$saveBonusScore($judge, $entry, $validData['score']);
|
|
} catch (ScoreEntryException $ex) {
|
|
DB::rollBack();
|
|
|
|
return redirect()->route('bonus-scores.entryBonusScoreSheet',
|
|
['entry_id' => $entry->id])->with('error', 'Error entering score - '.$ex->getMessage());
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception) {
|
|
DB::rollBack();
|
|
|
|
return redirect()->route('bonus-scores.entryBonusScoreSheet', ['entry_id' => $entry->id])->with('error', 'Error entering score - '.$ex->getMessage());
|
|
}
|
|
|
|
return redirect()->route('bonus-scores.entryBonusScoreSheet', ['entry_id' => $entry->id])->with('success', 'New bonus score entered');
|
|
}
|
|
|
|
public function destroyBonusScore()
|
|
{
|
|
|
|
}
|
|
}
|