102 lines
4.0 KiB
PHP
102 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Tabulation;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Audition;
|
|
use App\Models\Entry;
|
|
use App\Models\ScoreSheet;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use function compact;
|
|
use function dump;
|
|
use function redirect;
|
|
|
|
class TabulationController extends Controller
|
|
{
|
|
public function chooseEntry(Request $request)
|
|
{
|
|
return view('tabulation.choose_entry');
|
|
}
|
|
|
|
public function destroyScore(ScoreSheet $score) {
|
|
$score->delete();
|
|
return redirect()->back()->with('success','Score Deleted');
|
|
}
|
|
|
|
public function entryScoreSheet(Request $request)
|
|
{
|
|
$existing_sheets = [];
|
|
$entry = Entry::with(['student','audition.room.judges'])->find($request->input('entry_id'));
|
|
$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) {
|
|
return redirect()->route('tabulation.chooseEntry')->with('error','Entry not found');
|
|
}
|
|
return view('tabulation.entry_score_sheet', compact('entry','judges','scoring_guide','subscores','existing_sheets'));
|
|
}
|
|
|
|
public function saveEntryScoreSheet(Request $request, Entry $entry)
|
|
{
|
|
$judges = $entry->audition->room->judges;
|
|
|
|
$subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order');
|
|
$scoringGuide = $entry->audition->scoringGuide;
|
|
$preparedScoreSheets = [];
|
|
foreach ($judges as $judge) {
|
|
$preparedScoreSheets[$judge->id]['user_id'] = $judge->id;
|
|
$preparedScoreSheets[$judge->id]['entry_id'] = $entry->id;
|
|
|
|
$scoreValidation = $scoringGuide->validateScores($request->input('judge'.$judge->id));
|
|
if ($scoreValidation != 'success') {
|
|
return redirect(url()->previous())->with('error', $judge->full_name() . ': ' . $scoreValidation)->with('oldScores',$request->all());
|
|
}
|
|
|
|
$scoreSubmission = $request->input('judge'.$judge->id);
|
|
$scoresToSave = [];
|
|
foreach ($subscores as $subscore) {
|
|
$scoresToSave[$subscore->id] = [
|
|
'subscore_id'=>$subscore->id,
|
|
'subscore_name' => $subscore->name,
|
|
'score' => intval($scoreSubmission[$subscore->id])
|
|
];
|
|
}
|
|
$preparedScoreSheets[$judge->id]['scores'] = $scoresToSave;
|
|
}
|
|
foreach ($preparedScoreSheets as $sheet) {
|
|
ScoreSheet::updateOrCreate(
|
|
['entry_id' => $sheet['entry_id'], 'user_id' => $sheet['user_id']],
|
|
['subscores' => $sheet['scores']]
|
|
);
|
|
}
|
|
return redirect()->route('tabulation.chooseEntry')->with('success',count($preparedScoreSheets) . " Scores created");
|
|
}
|
|
|
|
public function status()
|
|
{
|
|
$auditions = Audition::with('entries.scoreSheets')->with('room.judges')->orderBy('score_order')->get();
|
|
|
|
return view('tabulation.status',compact('auditions'));
|
|
}
|
|
|
|
public function auditionSeating(Audition $audition)
|
|
{
|
|
// $entries = $audition->entries()->with(['student','scoreSheets.audition.scoringGuide','audition.room.judges'])->get();
|
|
// $entries = $entries->sortByDesc(function ($entry) {
|
|
// return $entry->totalScore();
|
|
// });
|
|
$entries = $audition->rankedEntries()->load('student','scoreSheets.audition.scoringGuide.subscores');
|
|
$judges = $audition->judges();
|
|
return view('tabulation.auditionSeating',compact('audition','entries','judges'));
|
|
}
|
|
|
|
}
|