102 lines
3.6 KiB
PHP
102 lines
3.6 KiB
PHP
<?php
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
/** @noinspection PhpMissingReturnTypeInspection */
|
|
|
|
namespace App\Actions\Tabulation;
|
|
|
|
use App\Exceptions\ScoreEntryException;
|
|
use App\Models\Entry;
|
|
use App\Models\ScoreSheet;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class EnterScore
|
|
{
|
|
// TODO implement this action every place that can save a score
|
|
public function __invoke(User $user, Entry $entry, array $scores): ScoreSheet
|
|
{
|
|
$scores = collect($scores);
|
|
$this->basicChecks($user, $entry, $scores);
|
|
$this->checkJudgeAssignment($user, $entry);
|
|
$this->checkForExistingScore($user, $entry);
|
|
$this->validateScoresSubmitted($entry, $scores);
|
|
$entry->removeFlag('no_show');
|
|
$newScoreSheet = ScoreSheet::create([
|
|
'user_id' => $user->id,
|
|
'entry_id' => $entry->id,
|
|
'subscores' => $this->subscoresForStorage($entry, $scores),
|
|
]);
|
|
|
|
return $newScoreSheet;
|
|
}
|
|
|
|
protected function subscoresForStorage(Entry $entry, Collection $scores)
|
|
{
|
|
$subscores = [];
|
|
foreach ($entry->audition->scoringGuide->subscores as $subscore) {
|
|
$subscores[$subscore->id] = [
|
|
'score' => $scores[$subscore->id],
|
|
'subscore_id' => $subscore->id,
|
|
'subscore_name' => $subscore->name,
|
|
];
|
|
}
|
|
|
|
return $subscores;
|
|
}
|
|
|
|
protected function checkForExistingScore(User $user, Entry $entry)
|
|
{
|
|
if (ScoreSheet::where('user_id', $user->id)->where('entry_id', $entry->id)->exists()) {
|
|
throw new ScoreEntryException('That judge has already entered scores for that entry');
|
|
}
|
|
}
|
|
|
|
protected function validateScoresSubmitted(Entry $entry, Collection $scores)
|
|
{
|
|
$subscoresRequired = $entry->audition->scoringGuide->subscores;
|
|
|
|
foreach ($subscoresRequired as $subscore) {
|
|
// check that there is an element in the $scores collection with the key = $subscore->id
|
|
if (! $scores->keys()->contains($subscore->id)) {
|
|
throw new ScoreEntryException('Invalid Score Submission');
|
|
}
|
|
if ($scores[$subscore->id] > $subscore->max_score) {
|
|
throw new ScoreEntryException('Supplied subscore exceeds maximum allowed');
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function checkJudgeAssignment(User $user, Entry $entry)
|
|
{
|
|
$check = DB::table('room_user')
|
|
->where('room_id', $entry->audition->room_id)
|
|
->where('user_id', $user->id)->exists();
|
|
if (! $check) {
|
|
throw new ScoreEntryException('This judge is not assigned to judge this entry');
|
|
}
|
|
}
|
|
|
|
protected function basicChecks(User $user, Entry $entry, Collection $scores)
|
|
{
|
|
if (! $user->exists()) {
|
|
throw new ScoreEntryException('User does not exist');
|
|
}
|
|
if (! $entry->exists()) {
|
|
throw new ScoreEntryException('Entry does not exist');
|
|
}
|
|
if ($entry->audition->hasFlag('seats_published')) {
|
|
throw new ScoreEntryException('Cannot score an entry in an audition with published seats');
|
|
}
|
|
if ($entry->audition->hasFlag('advancement_published')) {
|
|
throw new ScoreEntryException('Cannot score an entry in an audition with published advancement');
|
|
}
|
|
$requiredScores = $entry->audition->scoringGuide->subscores()->count();
|
|
if ($scores->count() !== $requiredScores) {
|
|
throw new ScoreEntryException('Invalid number of scores');
|
|
}
|
|
}
|
|
}
|