Make function useable for creating and updating scores
This commit is contained in:
parent
e7330378b2
commit
c06e22abdd
|
|
@ -5,7 +5,7 @@ namespace App\Http\Controllers\Tabulation;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Audition;
|
use App\Models\Audition;
|
||||||
use App\Models\Entry;
|
use App\Models\Entry;
|
||||||
use App\Rules\ValidScoreArray;
|
use App\Models\ScoreSheet;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use function dump;
|
use function dump;
|
||||||
use function redirect;
|
use function redirect;
|
||||||
|
|
@ -32,14 +32,44 @@ class TabulationController extends Controller
|
||||||
public function saveEntryScoreSheet(Request $request, Entry $entry)
|
public function saveEntryScoreSheet(Request $request, Entry $entry)
|
||||||
{
|
{
|
||||||
$judges = $entry->audition->room->judges;
|
$judges = $entry->audition->room->judges;
|
||||||
|
// Check if there is already a ScoreSheet for the entry $entry by judge $judge ( replaced this by making the last function updateOrCreate instead of Create)
|
||||||
|
// foreach ($judges as $judge) {
|
||||||
|
// $scoreSheet = ScoreSheet::where('entry_id',$entry->id)->where('user_id',$judge->id)->first();
|
||||||
|
// if ($scoreSheet) {
|
||||||
|
// return redirect(url()->previous())->with('error', $judge->full_name() . ' already scored this entry');
|
||||||
|
// }
|
||||||
|
// }
|
||||||
$subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order');
|
$subscores = $entry->audition->scoringGuide->subscores->sortBy('tiebreak_order');
|
||||||
$scoringGuide = $entry->audition->scoringGuide;
|
$scoringGuide = $entry->audition->scoringGuide;
|
||||||
|
$preparedScoreSheets = [];
|
||||||
foreach ($judges as $judge) {
|
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));
|
$scoreValidation = $scoringGuide->validateScores($request->input('judge'.$judge->id));
|
||||||
if ($scoreValidation != 'success') {
|
if ($scoreValidation != 'success') {
|
||||||
return redirect(url()->previous())->with('error', $judge->full_name() . ': ' . $scoreValidation)->with('oldScores',$request->all());
|
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' => $entry->id, 'user_id' => $judge->id],
|
||||||
|
['subscores' => json_encode($scoresToSave)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return redirect()->route('tabulation.chooseEntry')->with('success',count($preparedScoreSheets) . " Scores created");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,5 @@ use Illuminate\Database\Eloquent\Model;
|
||||||
class ScoreSheet extends Model
|
class ScoreSheet extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
protected $guarded = [];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue