52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use phpDocumentor\Reflection\Types\Boolean;
|
|
use PhpParser\Node\Expr\Cast\Bool_;
|
|
use PhpParser\Node\Scalar\String_;
|
|
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
|
|
* @param array $prospective_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';
|
|
// TODO this probably needs to be rewritten as a validation rule
|
|
}
|
|
}
|