59 lines
1.6 KiB
PHP
59 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use function array_diff_key;
|
|
use function array_key_exists;
|
|
use function is_null;
|
|
|
|
class ScoringGuide extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $with = ['subscores'];
|
|
|
|
public function auditions(): HasMany
|
|
{
|
|
return $this->hasMany(Audition::class)->orderBy('score_order');
|
|
}
|
|
|
|
public function subscores(): HasMany
|
|
{
|
|
return $this->hasMany(SubscoreDefinition::class);
|
|
}
|
|
|
|
/**
|
|
* Validate a set of subscores. Expects an array in the form of subscore_id => score
|
|
*
|
|
* @return string
|
|
*/
|
|
public function validateScores(array $prospective_score)
|
|
{
|
|
foreach ($this->subscores as $subscore) {
|
|
if (! array_key_exists($subscore->id, $prospective_score)) {
|
|
return 'A score must be provided for '.$subscore->name;
|
|
}
|
|
if (is_null($prospective_score[$subscore->id])) {
|
|
return 'A score must be provided for '.$subscore->name;
|
|
}
|
|
if ($prospective_score[$subscore->id] > $subscore->max_score) {
|
|
return 'The '.$subscore->name.' score must be less than or equal to '.$subscore->max_score;
|
|
}
|
|
}
|
|
|
|
$subscore_ids = $this->subscores->pluck('id')->flip()->all();
|
|
$diff = array_diff_key($prospective_score, $subscore_ids);
|
|
if (! empty($diff)) {
|
|
return 'Invalid scores submitted';
|
|
}
|
|
|
|
return 'success';
|
|
}
|
|
}
|