155 lines
6.4 KiB
PHP
155 lines
6.4 KiB
PHP
<?php
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
/** @noinspection PhpMissingReturnTypeInspection */
|
|
|
|
namespace App\Actions\Tabulation;
|
|
|
|
use App\Exceptions\ScoreEntryException;
|
|
use App\Models\AuditLogEntry;
|
|
use App\Models\Entry;
|
|
use App\Models\ScoreSheet;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use function auth;
|
|
|
|
class EnterScore
|
|
{
|
|
/**
|
|
* @param User $user A user acting as the judge for this sheet
|
|
* @param Entry $entry An entry to which this score should be assigned
|
|
* @param array $scores Scores to be entered in the form of SubscoreID => score
|
|
* @param ScoreSheet|false $scoreSheet If this is an update to an existing scoresheet, pass it here
|
|
* @return ScoreSheet The scoresheet that was created or updated
|
|
*
|
|
* @throws ScoreEntryException
|
|
*/
|
|
public function __invoke(User $user, Entry $entry, array $scores, ScoreSheet|false $scoreSheet = false): ScoreSheet
|
|
{
|
|
// TODO: Remove the CalculatedScore model and table when rewrite is complete, they'll be obsolete
|
|
// CalculatedScore::where('entry_id', $entry->id)->delete();
|
|
$scores = collect($scores);
|
|
|
|
// Basic Validity Checks
|
|
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');
|
|
}
|
|
|
|
// Check that the specified user is assigned to judge this 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');
|
|
}
|
|
|
|
// Check if a score already exists
|
|
if (! $scoreSheet) {
|
|
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');
|
|
}
|
|
} else {
|
|
if ($scoreSheet->user_id !== $user->id) {
|
|
throw new ScoreEntryException('Existing score sheet is from a different judge');
|
|
}
|
|
if ($scoreSheet->entry_id !== $entry->id) {
|
|
throw new ScoreEntryException('Existing score sheet is for a different entry');
|
|
}
|
|
}
|
|
|
|
// Check the validity of submitted subscores, format array for storage, and sum score
|
|
$subscoresRequired = $entry->audition->scoringGuide->subscores;
|
|
$subscoresStorageArray = [];
|
|
$seatingTotal = 0;
|
|
$seatingMaxPossible = 0;
|
|
$advancementTotal = 0;
|
|
$advancementMaxPossible = 0;
|
|
if ($scores->count() !== $subscoresRequired->count()) {
|
|
throw new ScoreEntryException('Invalid number of scores');
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
// Add subscore to the storage array
|
|
$subscoresStorageArray[$subscore->id] = [
|
|
'score' => $scores[$subscore->id],
|
|
'subscore_id' => $subscore->id,
|
|
'subscore_name' => $subscore->name,
|
|
];
|
|
|
|
// If included in seating, multiply by weight and add to the total and max possible
|
|
if ($subscore->for_seating) {
|
|
$seatingTotal += ($subscore->weight * $scores[$subscore->id]);
|
|
$seatingMaxPossible += ($subscore->weight * $subscore->max_score);
|
|
}
|
|
|
|
// If included in advancement, multiply by weight and add to the total and max possible
|
|
if ($subscore->for_advance) {
|
|
$advancementTotal += ($subscore->weight * $scores[$subscore->id]);
|
|
$advancementMaxPossible += ($subscore->weight * $subscore->max_score);
|
|
}
|
|
}
|
|
$finalSeatingTotal = ($seatingMaxPossible === 0) ? 0 : (($seatingTotal / $seatingMaxPossible) * 100);
|
|
$finalAdvancementTotal = ($advancementMaxPossible === 0) ? 0 : (($advancementTotal / $advancementMaxPossible) * 100);
|
|
|
|
$entry->removeFlag('no_show');
|
|
if ($scoreSheet instanceof ScoreSheet) {
|
|
$scoreSheet->update([
|
|
'user_id' => $user->id,
|
|
'entry_id' => $entry->id,
|
|
'subscores' => $subscoresStorageArray,
|
|
'seating_total' => $finalSeatingTotal,
|
|
'advancement_total' => $finalAdvancementTotal,
|
|
]);
|
|
} else {
|
|
$scoreSheet = ScoreSheet::create([
|
|
'user_id' => $user->id,
|
|
'entry_id' => $entry->id,
|
|
'subscores' => $subscoresStorageArray,
|
|
'seating_total' => $finalSeatingTotal,
|
|
'advancement_total' => $finalAdvancementTotal,
|
|
]);
|
|
}
|
|
|
|
// Log the score entry
|
|
$log_message = 'Entered Score for entry id '.$entry->id.'.<br />';
|
|
$log_message .= 'Judge: '.$user->full_name().'<br />';
|
|
foreach ($scoreSheet->subscores as $subscore) {
|
|
$log_message .= $subscore['subscore_name'].': '.$subscore['score'].'<br />';
|
|
}
|
|
$log_message .= 'Seating Total: '.$scoreSheet->seating_total.'<br />';
|
|
$log_message .= 'Advancement Total: '.$scoreSheet->advancement_total.'<br />';
|
|
AuditLogEntry::create([
|
|
'user' => auth()->user()->email ?? 'no user',
|
|
'ip_address' => request()->ip(),
|
|
'message' => $log_message,
|
|
'affected' => [
|
|
'entries' => [$entry->id],
|
|
'users' => [$user->id],
|
|
'auditions' => [$entry->audition_id],
|
|
'students' => [$entry->student_id],
|
|
],
|
|
]);
|
|
|
|
return $scoreSheet;
|
|
}
|
|
}
|