From 19580e5a87967d396164cb255c01922cfcdb1529 Mon Sep 17 00:00:00 2001 From: mthinon Date: Fri, 11 Oct 2024 13:46:22 +0200 Subject: [PATCH] Create new history entity --- migrations/Version20241011100716.php | 35 +++++++++ src/Entity/Question.php | 37 ++++++++++ src/Entity/UserHistory.php | 83 ++++++++++++++++++++++ src/Repository/UserHistoryRepository.php | 43 +++++++++++ templates/components/pages/quizz.html.twig | 2 +- 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20241011100716.php create mode 100644 src/Entity/UserHistory.php create mode 100644 src/Repository/UserHistoryRepository.php diff --git a/migrations/Version20241011100716.php b/migrations/Version20241011100716.php new file mode 100644 index 0000000..5d6ab00 --- /dev/null +++ b/migrations/Version20241011100716.php @@ -0,0 +1,35 @@ +addSql('CREATE TABLE user_history (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, question_id INT NOT NULL, answer_correctly TINYINT(1) NOT NULL, date DATE NOT NULL, INDEX IDX_7FB76E41A76ED395 (user_id), INDEX IDX_7FB76E411E27F6BF (question_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE user_history ADD CONSTRAINT FK_7FB76E41A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE user_history ADD CONSTRAINT FK_7FB76E411E27F6BF FOREIGN KEY (question_id) REFERENCES question (id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user_history DROP FOREIGN KEY FK_7FB76E41A76ED395'); + $this->addSql('ALTER TABLE user_history DROP FOREIGN KEY FK_7FB76E411E27F6BF'); + $this->addSql('DROP TABLE user_history'); + } +} diff --git a/src/Entity/Question.php b/src/Entity/Question.php index d981844..99c2935 100644 --- a/src/Entity/Question.php +++ b/src/Entity/Question.php @@ -36,9 +36,16 @@ class Question #[ORM\Column(type: Types::TEXT, nullable: true)] private ?string $help = null; + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: UserHistory::class, mappedBy: 'question')] + private Collection $userHistories; + public function __construct() { $this->answers = new ArrayCollection(); + $this->userHistories = new ArrayCollection(); } public function getId(): ?int @@ -128,4 +135,34 @@ public function setHelp(?string $help): static return $this; } + + /** + * @return Collection + */ + public function getUserHistories(): Collection + { + return $this->userHistories; + } + + public function addUserHistory(UserHistory $userHistory): static + { + if (!$this->userHistories->contains($userHistory)) { + $this->userHistories->add($userHistory); + $userHistory->setQuestion($this); + } + + return $this; + } + + public function removeUserHistory(UserHistory $userHistory): static + { + if ($this->userHistories->removeElement($userHistory)) { + // set the owning side to null (unless already changed) + if ($userHistory->getQuestion() === $this) { + $userHistory->setQuestion(null); + } + } + + return $this; + } } diff --git a/src/Entity/UserHistory.php b/src/Entity/UserHistory.php new file mode 100644 index 0000000..1190ef7 --- /dev/null +++ b/src/Entity/UserHistory.php @@ -0,0 +1,83 @@ +id; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): static + { + $this->user = $user; + + return $this; + } + + public function getQuestion(): ?Question + { + return $this->question; + } + + public function setQuestion(?Question $question): static + { + $this->question = $question; + + return $this; + } + + public function isAnswerCorrectly(): ?bool + { + return $this->answerCorrectly; + } + + public function setAnswerCorrectly(bool $answerCorrectly): static + { + $this->answerCorrectly = $answerCorrectly; + + return $this; + } + + public function getDate(): ?\DateTimeInterface + { + return $this->date; + } + + public function setDate(\DateTimeInterface $date): static + { + $this->date = $date; + + return $this; + } +} diff --git a/src/Repository/UserHistoryRepository.php b/src/Repository/UserHistoryRepository.php new file mode 100644 index 0000000..ae70eba --- /dev/null +++ b/src/Repository/UserHistoryRepository.php @@ -0,0 +1,43 @@ + + */ +class UserHistoryRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, UserHistory::class); + } + + // /** + // * @return UserHistory[] Returns an array of UserHistory objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('u.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?UserHistory + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/templates/components/pages/quizz.html.twig b/templates/components/pages/quizz.html.twig index 9c66c4b..eb3a284 100644 --- a/templates/components/pages/quizz.html.twig +++ b/templates/components/pages/quizz.html.twig @@ -13,7 +13,7 @@ {% for child in form.children %} {% set questionId = (child.vars.name | toInt) %} {% set is_valid = (correction[questionId] is defined and not (false in correction[questionId])) %} -
+
{% if child.vars.row_attr.name is defined %}

{{ child.vars.row_attr.name }}

{% endif %}