Paillasse/portail/app/Models/QuestionQuiz.php

49 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class QuestionQuiz extends Model
{
public const QCM = 'qcm';
public const VRAI_FAUX = 'vrai_faux';
protected $table = 'questions_quiz';
protected $fillable = ['quiz_id', 'enonce', 'type', 'ordre', 'reponse_vrai_faux'];
protected $casts = [
'reponse_vrai_faux' => 'boolean',
];
/** @return BelongsTo<Quiz, $this> */
public function quiz(): BelongsTo
{
return $this->belongsTo(Quiz::class);
}
/** @return HasMany<ChoixQuestionQuiz, $this> */
public function choix(): HasMany
{
return $this->hasMany(ChoixQuestionQuiz::class)->orderBy('ordre');
}
/**
* Vrai si $choixId (QCM) ou $reponseVraiFaux (vrai/faux) est la bonne
* réponse à cette question. Un seul des deux arguments compte, selon
* le type de la question.
*/
public function estCorrecte(?int $choixId, ?bool $reponseVraiFaux): bool
{
if ($this->type === self::VRAI_FAUX) {
return $reponseVraiFaux !== null && $reponseVraiFaux === $this->reponse_vrai_faux;
}
return $choixId !== null && $this->choix()->where('id', $choixId)->where('est_correct', true)->exists();
}
}