src/Entity/QuizResponse.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Criteria;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\QuizResponseRepository")
  9.  */
  10. class QuizResponse
  11. {
  12.     /**
  13.      * @ORM\Id()
  14.      * @ORM\GeneratedValue()
  15.      * @ORM\Column(type="bigint")
  16.      */
  17.     private $id;
  18.     
  19.     /**
  20.      * @ORM\Column(type="string", length=255)
  21.      */
  22.     private $score;
  23.     
  24.     /**
  25.      * @ORM\Column(type="string", length=255)
  26.      */
  27.     private $message;
  28.     
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="App\Entity\Quiz", inversedBy="quizResponses")
  31.      * @ORM\JoinColumn(name="quiz_id", referencedColumnName="id", onDelete="SET NULL")
  32.      */
  33.     private $quiz;
  34.     /**
  35.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="quizResponses")
  36.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  37.      */
  38.     private $user;
  39.     /**
  40.      * @ORM\OneToMany(targetEntity="App\Entity\QuizAnswer", mappedBy="quizResponse", orphanRemoval=true, cascade={"persist", "remove"}, fetch="EAGER")
  41.      */
  42.     private $quizAnswers;
  43.     
  44.     /**
  45.      * @ORM\Column(type="string", length=255, nullable=true)
  46.      */
  47.     private $nl_token;
  48.     /**
  49.      * @ORM\Column(type="string", length=127, nullable=true)
  50.      */
  51.     private $nl_first_name;
  52.     
  53.     /**
  54.      * @ORM\Column(type="string", length=127, nullable=true)
  55.      */
  56.     private $nl_last_name;
  57.     /**
  58.      * @ORM\Column(type="string", length=255, nullable=true)
  59.      */
  60.     private $nl_email;
  61.     /**
  62.      * @ORM\Column(type="string", length=255, nullable=true)
  63.      */
  64.     private $refurl;
  65.     /**
  66.      * @ORM\Column(type="integer")
  67.      */
  68.     private $is_finished;
  69.     /**
  70.      * @ORM\Column(type="datetime")
  71.      */
  72.     private $datetaken;
  73.     /**
  74.      * @ORM\ManyToOne(targetEntity="App\Entity\QuizResult", inversedBy="quizResponses")
  75.      * @ORM\JoinColumn(name="quiz_result_id", referencedColumnName="id", onDelete="SET NULL")
  76.      */
  77.     private $quizResult;
  78.     /**
  79.      * @ORM\Column(type="integer")
  80.      */
  81.     private $num_correct_answers;
  82.     /**
  83.      * @ORM\Column(type="integer")
  84.      */
  85.     private $num_scored_questions;
  86.     
  87.     
  88.     public function __construct ()
  89.     {
  90.         $this->score "";
  91.         $this->message "";
  92.         $this->quizAnswers = new ArrayCollection();
  93.         $this->sort 0;
  94.         $this->is_finished 0;
  95.         $this->datetaken = new \DateTime();
  96.         $this->num_correct_answers 0;
  97.         $this->num_scored_questions 0;
  98.     }
  99.     public function getId()
  100.     {
  101.         return $this->id;
  102.     }
  103.     
  104.     public function getScore(): ?string
  105.     {
  106.         return $this->score;
  107.     }
  108.     
  109.     public function setScore (string $score null): ?self
  110.     {
  111.         $this->score $score $score "";
  112.         return $this;
  113.     }
  114.     public function calculateScore() {
  115.         $score 0;
  116.         $correctAnswers 0;
  117.         $totalQuestions 0;
  118.         $criteria Criteria::create()->orderBy(["id" => Criteria::DESC]);
  119.         $optionIds = [];
  120.         foreach ($this->getQuizAnswers()->matching($criteria) as $quizAnswer) {
  121.             if($quizAnswer->getQuizQuestionOption() && !in_array($quizAnswer->getQuizQuestionOption()->getId(), $optionIds)) {
  122.                 $optionIds[] = $quizAnswer->getQuizQuestionOption()->getId();
  123.                 $score += $quizAnswer->getQuizQuestionOption()->getAnswerWeight();
  124.                 $totalQuestions++;
  125.                 if($quizAnswer->getQuizQuestionOption()->getAnswerWeight()) {
  126.                     $correctAnswers++;
  127.                 }
  128.             }
  129.         }
  130.         $this->num_correct_answers $correctAnswers;
  131.         $this->num_scored_questions $totalQuestions;
  132.         $this->score $score;
  133.         return $score;
  134.     }
  135.     
  136.     /*
  137.     public function getActiveChildren(): Collection
  138.     {
  139.         $criteria = Criteria::create()
  140.             ->where(Criteria::expr()->eq("status", "1"))
  141.             ->orderBy(["published_at" => Criteria::DESC])
  142.         ;
  143.         
  144.         return $this->getChildren()->matching($criteria);
  145.     }
  146.     */
  147.     public function getMessage(): ?string
  148.     {
  149.         return $this->message;
  150.     }
  151.     
  152.     public function setMessage (string $message null): ?self
  153.     {
  154.         $this->message $message $message "";
  155.         return $this;
  156.     }
  157.     
  158.     public function getQuiz(): ?Quiz
  159.     {
  160.         return $this->quiz;
  161.     }
  162.     
  163.     public function setQuiz(?Quiz $quiz null): self
  164.     {
  165.         $this->quiz $quiz;
  166.         return $this;
  167.     }
  168.     
  169.     public function getQuizResult(): ?QuizResult
  170.     {
  171.         return $this->quizResult;
  172.     }
  173.     
  174.     public function setQuizResult(?QuizResult $quizResult null): self
  175.     {
  176.         $this->quizResult $quizResult;
  177.         return $this;
  178.     }
  179.     public function getUser(): ?User
  180.       {
  181.           return $this->user;
  182.       }
  183.   
  184.     public function setUser(?User $user null): self
  185.     {
  186.         $this->user $user;
  187.         return $this;
  188.     }
  189.     public function getNlToken(): ?string
  190.     {
  191.         return $this->nl_token;
  192.     }
  193.     
  194.     public function setNlToken (string $nl_token null): ?self
  195.     {
  196.         $this->nl_token $nl_token;
  197.         return $this;
  198.     }
  199.     public function getNlFirstName(): ?string
  200.     {
  201.         return $this->nl_first_name;
  202.     }
  203.     
  204.     public function setNlFirstName (string $nl_first_name null): ?self
  205.     {
  206.         $this->nl_first_name $nl_first_name;
  207.         return $this;
  208.     }
  209.     public function getNlLastName(): ?string
  210.     {
  211.         return $this->nl_last_name;
  212.     }
  213.     
  214.     public function setNlLastName (string $nl_last_name null): ?self
  215.     {
  216.         $this->nl_last_name $nl_last_name;
  217.         return $this;
  218.     }
  219.     public function getNlFullName(): ?string
  220.     {
  221.         return $this->nl_first_name " " $this->nl_last_name;
  222.     }
  223.     public function getNlEmail(): ?string
  224.     {
  225.         return $this->nl_email;
  226.     }
  227.     
  228.     public function setNlEmail (string $nl_email null): ?self
  229.     {
  230.         $this->nl_email $nl_email;
  231.         return $this;
  232.     }
  233.     public function getRefurl(): ?string
  234.     {
  235.         return $this->refurl;
  236.     }
  237.     
  238.     public function setRefurl (string $refurl null): ?self
  239.     {
  240.         $this->refurl $refurl;
  241.         return $this;
  242.     }
  243.     public function getIsFinished(): ?bool
  244.     {
  245.         return $this->is_finished;
  246.     }
  247.     public function setIsFinished(bool $is_finished): self
  248.     {
  249.         $this->is_finished $is_finished;
  250.         return $this;
  251.     }
  252.     public function getDatetaken(): ?\DateTimeInterface
  253.     {
  254.         return $this->datetaken;
  255.     }
  256.     public function setDatetaken(\DateTimeInterface $datetaken): self
  257.     {
  258.         $this->datetaken $datetaken;
  259.         return $this;
  260.     }
  261.     
  262.     /**
  263.      * @return Collection|QuizAnswer[]
  264.      */
  265.     public function getQuizAnswers(): Collection
  266.     {
  267.         return $this->quizAnswers;
  268.     }
  269.     
  270.     public function addQuizAnswer(QuizAnswer $quizAnswer): self
  271.     {
  272.         if (!$this->quizAnswers->contains($quizAnswer)) {
  273.             $this->quizAnswers[] = $quizAnswer;
  274.             $quizAnswer->setQuizResponse($this);
  275.         }
  276.         
  277.         return $this;
  278.     }
  279.     
  280.     public function removeQuizAnswer(QuizAnswer $quizAnswer): self
  281.     {
  282.         if ($this->quizAnswers->contains($quizAnswer)) {
  283.             $this->quizAnswers->removeElement($quizAnswer);
  284.             // set the owning side to null (unless already changed)
  285.             if ($quizAnswer->getQuizResponse() === $this) {
  286.                 $quizAnswer->setQuizResponse(null);
  287.             }
  288.         }
  289.         
  290.         return $this;
  291.     }
  292.     public function getAnswerByQuestionId($questionId)
  293.     {
  294.         foreach ($this->quizAnswers as $quizAnswer) {
  295.             if ($quizAnswer->getQuizQuestion() && $quizAnswer->getQuizQuestion()->getId() == $questionId) {
  296.                 return $quizAnswer->getAnswer();
  297.             }
  298.             if ($quizAnswer->getQuizQuestionOption() && $quizAnswer->getQuizQuestionOption()->getQuizQuestion()->getId() == $questionId) {
  299.                 return $quizAnswer->getAnswer();
  300.             }
  301.         }
  302.         return null;
  303.     }
  304.     
  305.     public function getNumCorrectAnswers(): int
  306.     {
  307.         return $this->num_correct_answers;
  308.     }
  309.     public function setNumCorrectAnswers(int $num): self
  310.     {
  311.         $this->num_correct_answers $num;
  312.         return $this;
  313.     }
  314.     public function getNumScoredQuestions(): int
  315.     {
  316.         return $this->num_scored_questions;
  317.     }
  318.     public function setNumScoredQuestions(int $num): self
  319.     {
  320.         $this->num_scored_questions $num;
  321.         return $this;
  322.     }
  323.     public function __toString ()
  324.     {
  325.         return $this->getScore();
  326.     }
  327.     
  328. }