auditionadmin/app/Http/Controllers/Tabulation/ScoreController.php

133 lines
5.0 KiB
PHP

<?php
namespace App\Http\Controllers\Tabulation;
use App\Actions\Tabulation\EnterScore;
use App\Exceptions\AuditionAdminException;
use App\Http\Controllers\Controller;
use App\Models\Entry;
use App\Models\EntryTotalScore;
use App\Models\ScoreSheet;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
/**
* Provides functionality for entering judge scores from the admin side.
*/
class ScoreController extends Controller
{
public function chooseEntry()
{
$method = 'GET';
$formRoute = 'scores.entryScoreSheet';
$title = 'Enter Scores';
return view('tabulation.choose_entry', compact('method', 'formRoute', 'title'));
}
public function destroyScore(ScoreSheet $score)
{
if ($score->entry->audition->hasFlag('seats_published')) {
return redirect()->back()->with('error', 'Cannot delete scores for an entry where seats are published');
}
if ($score->entry->audition->hasFlag('advancement_published')) {
return redirect()->back()->with('error',
'Cannot delete scores for an entry where advancement is published');
}
EntryTotalScore::where('entry_id', $score->entry_id)->delete();
$score->delete();
return redirect()->back()->with('success', 'Score Deleted');
}
public function entryScoreSheet(Request $request)
{
$existing_sheets = [];
$entry = Entry::with(['student', 'audition.room.judges'])->findOrFail($request->input('entry_id'));
$publishedCheck = $this->checkIfPublished($entry);
if ($publishedCheck) {
return $publishedCheck;
}
$judges = $entry->audition->room->judges;
foreach ($judges as $judge) {
$scoreSheet = ScoreSheet::where('entry_id', $entry->id)->where('user_id', $judge->id)->first();
if ($scoreSheet) {
Session::flash('caution', 'Scores exist for this entry. Now editing existing scores');
$existing_sheets[$judge->id] = $scoreSheet;
}
}
$scoring_guide = $entry->audition->scoringGuide;
$subscores = $entry->audition->scoringGuide->subscores->sortBy('display_order');
if ($entry->hasFlag('no_show')) {
session()->flash('error',
'This entry is marked as a no-show. Entering a score will remove the no-show flag');
}
return view('tabulation.entry_score_sheet',
compact('entry', 'judges', 'scoring_guide', 'subscores', 'existing_sheets'));
}
public function saveEntryScoreSheet(Request $request, Entry $entry, EnterScore $scoreRecorder)
{
$publishedCheck = $this->checkIfPublished($entry);
if ($publishedCheck) {
return $publishedCheck;
}
/**
* Here we process the submission from the scoring form.
* We're expecting submitted data to include an array for each judge.
* Each array should be called judge+ the judges ID number
* The array should have a key for each subscore and the value of the score submitted
*/
foreach ($request->all() as $key => $value) {
// We're not interested in submission values that don't ahve judge in the name
if (! str_contains($key, 'judge')) {
continue;
}
// Extract the judge ID from the field name and load the user
$judge_id = str_replace('judge', '', $key);
$judge = User::find($judge_id);
// Check for existing scores, if so, tell EnterScores action that we're updating it, otherwise a new score
$existingScore = ScoreSheet::where('entry_id', $entry->id)
->where('user_id', $judge->id)->first();
if ($existingScore === null) {
$existingScore = false;
}
try {
$scoreRecorder($judge, $entry, $value, $existingScore);
} catch (AuditionAdminException $e) {
return redirect()->route('scores.entryScoreSheet', ['entry_id' => $entry->id])
->with('error', $e->getMessage());
}
}
// Since we're entering a score, this apparently isn't a no show.
$entry->removeFlag('no_show');
return redirect()->route('scores.chooseEntry')->with('success', 'Scores saved');
}
protected function checkIfPublished($entry)
{
// We're not going to enter scores if seats are published
if ($entry->audition->hasFlag('seats_published')) {
return to_route('scores.chooseEntry')->with('error',
'Cannot enter scores for entry '.$entry->id.'. '.$entry->audition->name.' seats are published');
}
// Nope, not if advancement is published either
if ($entry->audition->hasFlag('advancement_published')) {
return to_route('scores.chooseEntry')->with('error',
'Cannot enter scores for entry '.$entry->id.'. '.$entry->audition->name.' advancement is published');
}
return false;
}
}