62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Tabulation;
|
|
|
|
use App\Exceptions\AuditionAdminException;
|
|
use App\Models\Entry;
|
|
|
|
class CheckPrelimResult
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws AuditionAdminException
|
|
*/
|
|
public function __invoke(Entry $entry, bool $recalc = false): string
|
|
{
|
|
if ($recalc) {
|
|
$entry->removeFlag('passed_prelim');
|
|
$entry->removeFlag('failed_prelim');
|
|
}
|
|
|
|
if (! $entry->exists) {
|
|
throw new AuditionAdminException('Entry does not exist');
|
|
}
|
|
|
|
if (! $entry->audition->prelimDefinition) {
|
|
throw new AuditionAdminException('Entry does not have a prelim');
|
|
}
|
|
|
|
if ($entry->hasFlag('failed_prelim') || $entry->hasFlag('passed_prelim')) {
|
|
return 'noChange';
|
|
}
|
|
|
|
if (! $entry->audition->prelimDefinition->room || $entry->audition->prelimDefinition->room->judges()->count() == 0) {
|
|
return 'noJudgesAssigned';
|
|
}
|
|
|
|
$scoresRequired = $entry->audition->prelimDefinition->room->judges()->count();
|
|
$scoresAssigned = $entry->prelimScoreSheets()->count();
|
|
if ($scoresAssigned < $scoresRequired) {
|
|
return 'missing'.$scoresRequired - $scoresAssigned.'scores';
|
|
}
|
|
|
|
$totalScore = 0;
|
|
foreach ($entry->prelimScoreSheets as $scoreSheet) {
|
|
$totalScore += $scoreSheet->total;
|
|
}
|
|
$averageScore = $totalScore / $scoresAssigned;
|
|
if ($averageScore >= $entry->audition->prelimDefinition->passing_score) {
|
|
$entry->addFlag('passed_prelim');
|
|
|
|
return 'markedPassed';
|
|
} else {
|
|
$entry->addFlag('failed_prelim');
|
|
|
|
return 'markedFailed';
|
|
}
|
|
}
|
|
}
|