65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
|
|
|
class ScoreSheet extends Model
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'entry_id',
|
|
'subscores'
|
|
];
|
|
|
|
protected $casts = ['subscores' => 'json'];
|
|
protected $with = ['entry','judge','audition.scoringGuide'];
|
|
|
|
public function entry(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Entry::class);
|
|
}
|
|
|
|
public function judge(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function audition(): HasOneThrough
|
|
{
|
|
return $this->hasOneThrough(
|
|
Audition::class, // The final model you want to access
|
|
Entry::class, // The intermediate model
|
|
'id', // Foreign key on the intermediate model (Entry)
|
|
'id', // Foreign key on the final model (Audition)
|
|
'entry_id', // Local key on the current model (ScoreSheet)
|
|
'audition_id' // Local key on the intermediate model (Entry)
|
|
);
|
|
}
|
|
|
|
public function getSubscore($id)
|
|
{
|
|
return $this->subscores[$id]['score'] ?? false;
|
|
}
|
|
|
|
public function totalScore() {
|
|
$totalScore = 0;
|
|
$totalWeights = 0;
|
|
foreach ( $this->audition->scoringGuide->subscores as $subscore) {
|
|
$totalScore += $this->getSubscore($subscore->id) * $subscore->weight;
|
|
$totalWeights += $subscore->weight;
|
|
}
|
|
return $totalScore / $totalWeights;
|
|
}
|
|
|
|
public function isValid() {
|
|
$judges = $this->audition->judges();
|
|
return $judges->contains('id', $this->judge->id);
|
|
}
|
|
|
|
|
|
}
|