104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\NominationEnsembles;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\NominationEnsemble;
|
|
use App\Models\NominationEnsembleEntry;
|
|
|
|
class MeobdaNominationSeatingController extends Controller implements NominationSeatingController
|
|
{
|
|
public function index()
|
|
{
|
|
$ensembles = NominationEnsemble::all();
|
|
$ensemble = null;
|
|
|
|
return view('nomination_ensembles.meobda.admin.seating', compact('ensembles', 'ensemble'));
|
|
}
|
|
|
|
public function show(NominationEnsemble $ensemble)
|
|
{
|
|
$ensembles = NominationEnsemble::all();
|
|
$nominations = [];
|
|
|
|
if ($ensemble->data['seated'] ?? false) {
|
|
$nominations = NominationEnsembleEntry::where('nomination_ensemble_id', $ensemble->id)
|
|
->orderBy('data->instrument')
|
|
->orderByRaw('CAST(data->"$.seat" AS UNSIGNED)')
|
|
->with('student.school')
|
|
->get();
|
|
$nominations = $nominations->groupBy(function ($item) {
|
|
return $item->data['split'];
|
|
});
|
|
foreach ($nominations as $split => $splitNoms) {
|
|
$nominations[$split] = $splitNoms->groupBy(function ($item) {
|
|
return $item->data['instrument'];
|
|
});
|
|
}
|
|
}
|
|
|
|
return view('nomination_ensembles.meobda.admin.seating', compact('ensembles', 'ensemble', 'nominations'));
|
|
}
|
|
|
|
public function seat(NominationEnsemble $ensemble)
|
|
{
|
|
$validData = request()->validate([
|
|
'action' => 'required|in:seat,clear',
|
|
]);
|
|
$action = $validData['action'];
|
|
$data = $ensemble['data'];
|
|
$nominations = NominationEnsembleEntry::where('nomination_ensemble_id',
|
|
$ensemble->id)->inRandomOrder()->get();
|
|
$groupedNominations = $nominations->groupBy(function ($nom) {
|
|
return $nom->data['instrument'];
|
|
});
|
|
|
|
if ($action == 'seat') {
|
|
// If no split names are set, make the name of the ensemble the only split name
|
|
$splits = $data['split_names'];
|
|
if (count($splits) < 2 && strlen($splits[0]) < 2) {
|
|
$splits[0] = $ensemble->name;
|
|
}
|
|
$splitOn = 0;
|
|
|
|
foreach ($ensemble->data['instruments'] as $instrument) {
|
|
if (! $groupedNominations->has($instrument['name'])) {
|
|
continue;
|
|
}
|
|
$seatOn = 1;
|
|
foreach ($groupedNominations[$instrument['name']] as $nomination) {
|
|
$nomData = $nomination['data'];
|
|
$nomData['seat'] = $seatOn;
|
|
$nomData['split'] = $splits[$splitOn];
|
|
$nomination->data = $nomData;
|
|
$nomination->save();
|
|
|
|
$splitOn++;
|
|
if ($splitOn >= count($splits)) {
|
|
$seatOn++;
|
|
$splitOn = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
$data['seated'] = true;
|
|
|
|
}
|
|
|
|
if ($action == 'clear') {
|
|
$data['seated'] = false;
|
|
foreach ($nominations as $nomination) {
|
|
$nomData = $nomination['data'];
|
|
unset($nomData['split']);
|
|
unset($nomData['seat']);
|
|
$nomination->data = $nomData;
|
|
$nomination->save();
|
|
}
|
|
}
|
|
$ensemble->data = $data;
|
|
$ensemble->save();
|
|
|
|
return redirect(route('nomination.admin.seating.show', [$ensemble]));
|
|
}
|
|
}
|