Skip to content

Commit

Permalink
Create new history entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mthinon committed Oct 11, 2024
1 parent 9a722a0 commit 19580e5
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
35 changes: 35 additions & 0 deletions migrations/Version20241011100716.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241011100716 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
37 changes: 37 additions & 0 deletions src/Entity/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ class Question
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $help = null;

/**
* @var Collection<int, UserHistory>
*/
#[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
Expand Down Expand Up @@ -128,4 +135,34 @@ public function setHelp(?string $help): static

return $this;
}

/**
* @return Collection<int, UserHistory>
*/
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;
}
}
83 changes: 83 additions & 0 deletions src/Entity/UserHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Entity;

use App\Repository\UserHistoryRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: UserHistoryRepository::class)]
class UserHistory
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;

#[ORM\ManyToOne(inversedBy: 'userHistories')]
#[ORM\JoinColumn(nullable: false)]
private ?Question $question = null;

#[ORM\Column]
private ?bool $answerCorrectly = null;

#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTimeInterface $date = null;

public function getId(): ?int
{
return $this->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;
}
}
43 changes: 43 additions & 0 deletions src/Repository/UserHistoryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Repository;

use App\Entity\UserHistory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<UserHistory>
*/
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()
// ;
// }
}
2 changes: 1 addition & 1 deletion templates/components/pages/quizz.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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])) %}
<div class="questions__random__form__tab d-none {{ correction is defined and is_valid ? 'sucess' : 'error' }}">
<div class="questions__random__form__tab d-none {{ correction is defined ? (is_valid ? 'sucess' : 'error') : '' }}">
{% if child.vars.row_attr.name is defined %}
<h3>{{ child.vars.row_attr.name }}</h3>
{% endif %}
Expand Down

0 comments on commit 19580e5

Please sign in to comment.