163 lines
5.5 KiB
PHP
163 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\SubscoreDefinitionRequest;
|
|
use App\Models\ScoringGuide;
|
|
use App\Models\SubscoreDefinition;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use function auditionSetting;
|
|
use function request;
|
|
use function response;
|
|
|
|
class ScoringGuideController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
DB::table('auditions')
|
|
->whereNull('scoring_guide_id')
|
|
->update(['scoring_guide_id' => 0]);
|
|
$guides = ScoringGuide::with(['auditions'])->withCount('subscores')->orderBy('name')->get();
|
|
|
|
return view('admin.scoring.index', ['guides' => $guides]);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
request()->validate([
|
|
'name' => ['required', 'unique:scoring_guides'],
|
|
]);
|
|
|
|
ScoringGuide::create([
|
|
'name' => request('name'),
|
|
]);
|
|
|
|
return redirect(route('admin.scoring.index'))->with('success', 'Scoring guide created');
|
|
}
|
|
|
|
public function edit(ScoringGuide $guide, string $tab = 'detail')
|
|
{
|
|
if ($tab == 'tiebreakOrder') {
|
|
$subscores = SubscoreDefinition::where('scoring_guide_id', $guide->id)->orderBy('tiebreak_order')->get();
|
|
} else {
|
|
$subscores = SubscoreDefinition::where('scoring_guide_id', $guide->id)->orderBy('display_order')->get();
|
|
}
|
|
|
|
return view('admin.scoring.edit', ['guide' => $guide, 'subscores' => $subscores, 'tab' => $tab]);
|
|
}
|
|
|
|
public function update(ScoringGuide $guide)
|
|
{
|
|
request()->validate([
|
|
'name' => ['required', 'unique:scoring_guides'],
|
|
]);
|
|
|
|
$guide->update([
|
|
'name' => request('name'),
|
|
]);
|
|
|
|
return redirect('/admin/scoring/guides/'.$guide->id.'/edit')->with('success', 'Scoring guide updated');
|
|
}
|
|
|
|
public function destroy(ScoringGuide $guide)
|
|
{
|
|
if ($guide->auditions()->count() > 0) {
|
|
return redirect('/admin/scoring')->with('error',
|
|
'Cannot delete scoring guide being used by one or more auditions');
|
|
}
|
|
|
|
$guide->delete();
|
|
|
|
return redirect('/admin/scoring')->with('success', 'Scoring guide deleted');
|
|
}
|
|
|
|
public function subscore_store(SubscoreDefinitionRequest $request, ScoringGuide $guide)
|
|
{
|
|
|
|
$validateData = $request->validated();
|
|
|
|
// Put the new subscore at the end of the list for both display and tiebreak order
|
|
$display_order = SubscoreDefinition::where('scoring_guide_id', '=', $guide->id)->max('display_order') + 1;
|
|
$tiebreak_order = SubscoreDefinition::where('scoring_guide_id', '=', $guide->id)->max('tiebreak_order') + 1;
|
|
if (! auditionSetting('advanceTo')) {
|
|
$validateData['for_advance'] = 0;
|
|
$validateData['for_seating'] = 1;
|
|
}
|
|
SubscoreDefinition::create([
|
|
'scoring_guide_id' => $guide->id,
|
|
'name' => $validateData['name'],
|
|
'max_score' => $validateData['max_score'],
|
|
'weight' => $validateData['weight'],
|
|
'display_order' => $display_order,
|
|
'tiebreak_order' => $tiebreak_order,
|
|
'for_seating' => $validateData['for_seating'],
|
|
'for_advance' => $validateData['for_advance'],
|
|
]);
|
|
|
|
return redirect(route('admin.scoring.edit', $guide))->with('success', 'Subscore added');
|
|
}
|
|
|
|
public function subscore_update(
|
|
SubscoreDefinitionRequest $request,
|
|
ScoringGuide $guide,
|
|
SubscoreDefinition $subscore
|
|
) {
|
|
if ($subscore->scoring_guide_id !== $guide->id) { // Make sure the subscore were updating belongs to the guide
|
|
return redirect('/admin/scoring/guides/'.$subscore->scoring_guide_id.'/edit')->with('error',
|
|
'Cannot update a subscore for a different scoring guide');
|
|
}
|
|
$validateData = $validateData = $request->validated();
|
|
|
|
$subscore->update([
|
|
'name' => $validateData['name'],
|
|
'max_score' => $validateData['max_score'],
|
|
'weight' => $validateData['weight'],
|
|
'for_seating' => $validateData['for_seating'],
|
|
'for_advance' => $validateData['for_advance'],
|
|
]);
|
|
|
|
return redirect(route('admin.scoring.edit', $guide))->with('success', 'Subscore updated');
|
|
}
|
|
|
|
public function subscore_destroy(ScoringGuide $guide, SubscoreDefinition $subscore)
|
|
{
|
|
if ($subscore->scoring_guide_id !== $guide->id) { // Make sure the subscore were updating belongs to the guide
|
|
|
|
return redirect(route('admin.scoring.edit', $subscore->scoring_guide_id))->with('error',
|
|
'Cannot delete a subscore for a different scoring guide');
|
|
}
|
|
|
|
$subscore->delete();
|
|
|
|
return redirect('/admin/scoring/guides/'.$guide->id.'/edit')->with('success', 'Subscore deleted');
|
|
|
|
}
|
|
|
|
public function reorder_display(Request $request)
|
|
{
|
|
$order = $request->order;
|
|
foreach ($order as $index => $id) {
|
|
$subscore = SubscoreDefinition::find($id);
|
|
$subscore->update(['display_order' => $index + 1]);
|
|
}
|
|
|
|
return response()->json(['status' => 'success']);
|
|
|
|
}
|
|
|
|
public function reorder_tiebreak(Request $request)
|
|
{
|
|
$order = $request->order;
|
|
foreach ($order as $index => $id) {
|
|
$subscore = SubscoreDefinition::find($id);
|
|
$subscore->update(['tiebreak_order' => $index + 1]);
|
|
}
|
|
|
|
return response()->json(['status' => 'success']);
|
|
|
|
}
|
|
}
|