Skip to content

Commit

Permalink
Add activities to the model (#181)
Browse files Browse the repository at this point in the history
* 180: Add activities and activity entries tables

* 180: Modify files model
  • Loading branch information
Mathieu414 authored Mar 25, 2024
1 parent 7ac03f3 commit 1bc5802
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 2 deletions.
47 changes: 47 additions & 0 deletions database/migrations/Version20240324072035.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace intranose\Migrations;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240324072035 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 orm_activities (id INT AUTO_INCREMENT NOT NULL, event_id INT DEFAULT NULL, type VARCHAR(255) NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) NOT NULL, place VARCHAR(255) NOT NULL, INDEX IDX_DA9A084071F7E88B (event_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE orm_activity_entries (user_id INT NOT NULL, activity_id INT NOT NULL, category_id INT DEFAULT NULL, present TINYINT(1) NOT NULL, comment VARCHAR(255) NOT NULL, INDEX IDX_9D0FAE1A76ED395 (user_id), INDEX IDX_9D0FAE181C06096 (activity_id), INDEX IDX_9D0FAE112469DE2 (category_id), PRIMARY KEY(user_id, activity_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE orm_activities ADD CONSTRAINT FK_DA9A084071F7E88B FOREIGN KEY (event_id) REFERENCES orm_events (id)');
$this->addSql('ALTER TABLE orm_activity_entries ADD CONSTRAINT FK_9D0FAE1A76ED395 FOREIGN KEY (user_id) REFERENCES orm_users (id)');
$this->addSql('ALTER TABLE orm_activity_entries ADD CONSTRAINT FK_9D0FAE181C06096 FOREIGN KEY (activity_id) REFERENCES orm_activities (id)');
$this->addSql('ALTER TABLE orm_activity_entries ADD CONSTRAINT FK_9D0FAE112469DE2 FOREIGN KEY (category_id) REFERENCES orm_categories (id)');
$this->addSql('ALTER TABLE orm_categories ADD activity_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE orm_categories ADD CONSTRAINT FK_5598E1CD81C06096 FOREIGN KEY (activity_id) REFERENCES orm_activities (id)');
$this->addSql('CREATE INDEX IDX_5598E1CD81C06096 ON orm_categories (activity_id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE orm_categories DROP FOREIGN KEY FK_5598E1CD81C06096');
$this->addSql('ALTER TABLE orm_activities DROP FOREIGN KEY FK_DA9A084071F7E88B');
$this->addSql('ALTER TABLE orm_activity_entries DROP FOREIGN KEY FK_9D0FAE1A76ED395');
$this->addSql('ALTER TABLE orm_activity_entries DROP FOREIGN KEY FK_9D0FAE181C06096');
$this->addSql('ALTER TABLE orm_activity_entries DROP FOREIGN KEY FK_9D0FAE112469DE2');
$this->addSql('DROP TABLE orm_activities');
$this->addSql('DROP TABLE orm_activity_entries');
$this->addSql('DROP INDEX IDX_5598E1CD81C06096 ON orm_categories');
$this->addSql('ALTER TABLE orm_categories DROP activity_id');
}
}
87 changes: 87 additions & 0 deletions database/models/activities.db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;

#[Entity, Table(name: 'activity_entries')]
class ActivityEntry
{
#[Id, ManyToOne]
public User|null $user = null;

#[Id, ManyToOne]
public Activity|null $activity = null;

#[ManyToOne]
public Category|null $category = null;

#[Column]
public bool $present = false;

#[Column]
public string $comment = "";

function set($user, $activity, $present, $comment)
{
$this->user = $user;
$this->activity = $activity;
$this->present = $present;
$this->comment = $comment;
}
}

enum ActivityType: string
{
case RACE = "RACE";
case TRAINING = "TRAINING";
case OTHER = "OTHER";
}

#[Entity, Table(name: 'activities')]
class Activity
{
#[Id, Column, GeneratedValue]
public int|null $id = null;

#[Column]
public ActivityType $type;

#[Column]
public DateTime $date;

#[Column]
public string $name;

#[Column]
public string $place;

#[ManyToOne]
public Event|null $event = null;

#[OneToMany(targetEntity: ActivityEntry::class, mappedBy: "activity", cascade: ["remove"])]
public Collection $entries;

/** @var Collection<int, Category> categories */
#[OneToMany(targetEntity: Category::class, mappedBy: "activity", cascade: ["persist", "remove"])]
public Collection $categories;

function __construct()
{
$this->entries = new ArrayCollection();
$this->categories = new ArrayCollection();
}

function set(string $name, DateTime $date, string $place, Event $event)
{
$this->name = $name;
$this->place = $place;
$this->date = $date;
$this->event = $event;
}
}
7 changes: 7 additions & 0 deletions database/models/categories.db.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Category
#[ManyToOne(targetEntity: Race::class, inversedBy: "categories")]
public Race|null $race = null;

#[ManyToOne(targetEntity: Activity::class, inversedBy: "categories")]
public Activity|null $activity = null;

#[Column]
public string $name = "";

Expand All @@ -28,6 +31,10 @@ class Category
#[OneToMany(targetEntity: RaceEntry::class, mappedBy: "category", cascade: ["remove"])]
public Collection $entries;

/** @var Collection<int,ActivityEntry> entries */
#[OneToMany(targetEntity: ActivityEntry::class, mappedBy: "category", cascade: ["remove"])]
public Collection $activity_entries;

/** @param Collection<int,Category> categories */
static function toSelectOptions($categories): array
{
Expand Down
6 changes: 5 additions & 1 deletion database/models/events.db.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class Event
#[OneToMany(targetEntity: Race::class, mappedBy: 'event', cascade: ["remove"])]
public Collection $races;

/** @var Collection<int, Activity> */
#[OneToMany(targetEntity: Activity::class, mappedBy: 'event', cascade: ["remove"])]
public Collection $activities;

function __construct()
{
$this->start_date = date_create();
Expand Down Expand Up @@ -263,7 +267,7 @@ static function fromEventList(array $events)
$event['end_date'],
$event['deadline'],
$event['open'],
isset($event['present']) ? $event['present'] : null
isset ($event['present']) ? $event['present'] : null
);
}
return $result;
Expand Down
6 changes: 5 additions & 1 deletion database/models/users.db.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class User
#[OneToMany(targetEntity: RaceEntry::class, mappedBy: "user", cascade: ["remove"])]
public Collection $race_entries;

/** @var Collection<int,ActivityEntry> entries */
#[OneToMany(targetEntity: ActivityEntry::class, mappedBy: "user", cascade: ["remove"])]
public Collection $activity_entries;

function __construct()
{
$this->birthdate = date_create();
Expand Down Expand Up @@ -132,7 +136,7 @@ static function getCurrent(): User|null
if (!has_session("user_id")) {
return null;
}
if (isset($_SESSION['controlled_user_id'])) {
if (isset ($_SESSION['controlled_user_id'])) {
Page::getInstance()->controlled();
}
self::$currentUser ??= em()->find(User::class, $_SESSION['controlled_user_id'] ?? $_SESSION['user_id']);
Expand Down

0 comments on commit 1bc5802

Please sign in to comment.