<?php
namespace App\Entity;
use App\Repository\ScoringQuestionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Scoring;
/**
* @ORM\Entity(repositoryClass=ScoringQuestionRepository::class)
*/
class ScoringQuestion
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $label;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $subDescription;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $imageUrl;
/**
* @ORM\ManyToOne(targetEntity=ScoringSection::class, inversedBy="scoringQuestions")
* @ORM\JoinColumn(nullable=false)
*/
private $scoringSection;
/**
* @ORM\OneToMany(targetEntity=ScoringAnswer::class, mappedBy="scoringQuestion", orphanRemoval=true)
*/
private $scoringAnswers;
/**
* @ORM\Column(type="integer")
*/
private $sort;
/**
* @ORM\Column(type="boolean")
*/
private $image_active = false;
/**
* @ORM\Column(type="boolean")
*/
private $comment_active = false;
/**
* @ORM\Column(type="float", options={"default": 1.0})
*/
private $weight = 1.0;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $reco;
public function __construct()
{
$this->scoringAnswers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getSubDescription(): ?string
{
return $this->subDescription;
}
public function setSubDescription(?string $subDescription): self
{
$this->subDescription = $subDescription;
return $this;
}
public function getImageUrl(): ?string
{
return $this->imageUrl;
}
public function setImageUrl(?string $imageUrl): self
{
$this->imageUrl = $imageUrl;
return $this;
}
public function getScoringSection(): ?ScoringSection
{
return $this->scoringSection;
}
public function setScoringSection(?ScoringSection $scoringSection): self
{
$this->scoringSection = $scoringSection;
return $this;
}
/**
* @return Collection<int, ScoringAnswer>
*/
public function getScoringAnswers(): Collection
{
return $this->scoringAnswers;
}
public function addScoringAnswer(ScoringAnswer $scoringAnswer): self
{
if (!$this->scoringAnswers->contains($scoringAnswer)) {
$this->scoringAnswers[] = $scoringAnswer;
$scoringAnswer->setScoringQuestion($this);
}
return $this;
}
public function removeScoringAnswer(ScoringAnswer $scoringAnswer): self
{
if ($this->scoringAnswers->removeElement($scoringAnswer)) {
// set the owning side to null (unless already changed)
if ($scoringAnswer->getScoringQuestion() === $this) {
$scoringAnswer->setScoringQuestion(null);
}
}
return $this;
}
public function getSort(): ?int
{
return $this->sort;
}
public function setSort(int $sort): self
{
$this->sort = $sort;
return $this;
}
public function isImageActive(): ?bool
{
return $this->image_active;
}
public function setImageActive(bool $image_active): self
{
$this->image_active = $image_active;
return $this;
}
public function isCommentActive(): ?bool
{
return $this->comment_active;
}
public function setCommentActive(bool $comment_active): self
{
$this->comment_active = $comment_active;
return $this;
}
public function getWeight(): float
{
return $this->weight;
}
public function setWeight(float $weight): self
{
$this->weight = $weight;
return $this;
}
/*public function calculateQuestionScore($weighted = true): ?float
{
$answer = $this->scoringAnswers->first();
if (!$answer) {
return null;
}
return $answer->getCalculatedValue($weighted);
}*/
public function calculateQuestionScore(bool $weighted = true, ?Scoring $scoring = null): ?float
{
$answer = null;
if ($scoring !== null) {
// on cherche la réponse qui appartient à CE scoring
foreach ($this->scoringAnswers as $a) {
if ($a->getScoring() && $a->getScoring()->getId() === $scoring->getId()) {
$answer = $a;
break;
}
}
} else {
// fallback ancien comportement
$answer = $this->scoringAnswers->first() ?: null;
}
if (!$answer) {
return null;
}
return $answer->getCalculatedValue($weighted);
}
public function calculateQuestionScoreForScoring(bool $weighted = true, ?Scoring $scoring = null): ?float
{
// si pas de scoring passé, on retombe sur le comportement historique
if ($scoring === null) {
return $this->calculateQuestionScore($weighted);
}
// on cherche la réponse qui appartient à CE scoring
$answer = null;
foreach ($this->scoringAnswers as $a) {
if ($a->getScoring() && $a->getScoring()->getId() === $scoring->getId()) {
$answer = $a;
break;
}
}
if (!$answer) {
return null;
}
return $answer->getCalculatedValue($weighted);
}
/**
* @return mixed
*/
public function getReco() {
return $this->reco;
}
/**
* @param mixed $reco
* @return self
*/
public function setReco($reco): self {
$this->reco = $reco;
return $this;
}
}