<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity(repositoryClass="App\Repository\QuizResponseRepository")
*/
class QuizResponse
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $score;
/**
* @ORM\Column(type="string", length=255)
*/
private $message;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Quiz", inversedBy="quizResponses")
* @ORM\JoinColumn(name="quiz_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $quiz;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="quizResponses")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="App\Entity\QuizAnswer", mappedBy="quizResponse", orphanRemoval=true, cascade={"persist", "remove"}, fetch="EAGER")
*/
private $quizAnswers;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $nl_token;
/**
* @ORM\Column(type="string", length=127, nullable=true)
*/
private $nl_first_name;
/**
* @ORM\Column(type="string", length=127, nullable=true)
*/
private $nl_last_name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $nl_email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $refurl;
/**
* @ORM\Column(type="integer")
*/
private $is_finished;
/**
* @ORM\Column(type="datetime")
*/
private $datetaken;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\QuizResult", inversedBy="quizResponses")
* @ORM\JoinColumn(name="quiz_result_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $quizResult;
/**
* @ORM\Column(type="integer")
*/
private $num_correct_answers;
/**
* @ORM\Column(type="integer")
*/
private $num_scored_questions;
public function __construct ()
{
$this->score = "";
$this->message = "";
$this->quizAnswers = new ArrayCollection();
$this->sort = 0;
$this->is_finished = 0;
$this->datetaken = new \DateTime();
$this->num_correct_answers = 0;
$this->num_scored_questions = 0;
}
public function getId()
{
return $this->id;
}
public function getScore(): ?string
{
return $this->score;
}
public function setScore (string $score = null): ?self
{
$this->score = $score ? $score : "";
return $this;
}
public function calculateScore() {
$score = 0;
$correctAnswers = 0;
$totalQuestions = 0;
$criteria = Criteria::create()->orderBy(["id" => Criteria::DESC]);
$optionIds = [];
foreach ($this->getQuizAnswers()->matching($criteria) as $quizAnswer) {
if($quizAnswer->getQuizQuestionOption() && !in_array($quizAnswer->getQuizQuestionOption()->getId(), $optionIds)) {
$optionIds[] = $quizAnswer->getQuizQuestionOption()->getId();
$score += $quizAnswer->getQuizQuestionOption()->getAnswerWeight();
$totalQuestions++;
if($quizAnswer->getQuizQuestionOption()->getAnswerWeight()) {
$correctAnswers++;
}
}
}
$this->num_correct_answers = $correctAnswers;
$this->num_scored_questions = $totalQuestions;
$this->score = $score;
return $score;
}
/*
public function getActiveChildren(): Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("status", "1"))
->orderBy(["published_at" => Criteria::DESC])
;
return $this->getChildren()->matching($criteria);
}
*/
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage (string $message = null): ?self
{
$this->message = $message ? $message : "";
return $this;
}
public function getQuiz(): ?Quiz
{
return $this->quiz;
}
public function setQuiz(?Quiz $quiz = null): self
{
$this->quiz = $quiz;
return $this;
}
public function getQuizResult(): ?QuizResult
{
return $this->quizResult;
}
public function setQuizResult(?QuizResult $quizResult = null): self
{
$this->quizResult = $quizResult;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user = null): self
{
$this->user = $user;
return $this;
}
public function getNlToken(): ?string
{
return $this->nl_token;
}
public function setNlToken (string $nl_token = null): ?self
{
$this->nl_token = $nl_token;
return $this;
}
public function getNlFirstName(): ?string
{
return $this->nl_first_name;
}
public function setNlFirstName (string $nl_first_name = null): ?self
{
$this->nl_first_name = $nl_first_name;
return $this;
}
public function getNlLastName(): ?string
{
return $this->nl_last_name;
}
public function setNlLastName (string $nl_last_name = null): ?self
{
$this->nl_last_name = $nl_last_name;
return $this;
}
public function getNlFullName(): ?string
{
return $this->nl_first_name . " " . $this->nl_last_name;
}
public function getNlEmail(): ?string
{
return $this->nl_email;
}
public function setNlEmail (string $nl_email = null): ?self
{
$this->nl_email = $nl_email;
return $this;
}
public function getRefurl(): ?string
{
return $this->refurl;
}
public function setRefurl (string $refurl = null): ?self
{
$this->refurl = $refurl;
return $this;
}
public function getIsFinished(): ?bool
{
return $this->is_finished;
}
public function setIsFinished(bool $is_finished): self
{
$this->is_finished = $is_finished;
return $this;
}
public function getDatetaken(): ?\DateTimeInterface
{
return $this->datetaken;
}
public function setDatetaken(\DateTimeInterface $datetaken): self
{
$this->datetaken = $datetaken;
return $this;
}
/**
* @return Collection|QuizAnswer[]
*/
public function getQuizAnswers(): Collection
{
return $this->quizAnswers;
}
public function addQuizAnswer(QuizAnswer $quizAnswer): self
{
if (!$this->quizAnswers->contains($quizAnswer)) {
$this->quizAnswers[] = $quizAnswer;
$quizAnswer->setQuizResponse($this);
}
return $this;
}
public function removeQuizAnswer(QuizAnswer $quizAnswer): self
{
if ($this->quizAnswers->contains($quizAnswer)) {
$this->quizAnswers->removeElement($quizAnswer);
// set the owning side to null (unless already changed)
if ($quizAnswer->getQuizResponse() === $this) {
$quizAnswer->setQuizResponse(null);
}
}
return $this;
}
public function getAnswerByQuestionId($questionId)
{
foreach ($this->quizAnswers as $quizAnswer) {
if ($quizAnswer->getQuizQuestion() && $quizAnswer->getQuizQuestion()->getId() == $questionId) {
return $quizAnswer->getAnswer();
}
if ($quizAnswer->getQuizQuestionOption() && $quizAnswer->getQuizQuestionOption()->getQuizQuestion()->getId() == $questionId) {
return $quizAnswer->getAnswer();
}
}
return null;
}
public function getNumCorrectAnswers(): int
{
return $this->num_correct_answers;
}
public function setNumCorrectAnswers(int $num): self
{
$this->num_correct_answers = $num;
return $this;
}
public function getNumScoredQuestions(): int
{
return $this->num_scored_questions;
}
public function setNumScoredQuestions(int $num): self
{
$this->num_scored_questions = $num;
return $this;
}
public function __toString ()
{
return $this->getScore();
}
}