Add capability to store closing motive when closing accompanying period

The commit introduces new functionality in the bundle that allows storing the closing motive when a course is closed. This is achieved by modifying the model and database schema to include a new `closingMotive` field in AccompanyingPeriodStepHistory entity.
This commit is contained in:
Julien Fastré 2024-01-23 21:56:17 +01:00
parent 0c9010f065
commit f8840d89bf
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 108 additions and 4 deletions

View File

@ -62,7 +62,7 @@ class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Contr
$workflow = $this->registry->get($accompanyingCourse);
if ($workflow->can($accompanyingCourse, 'close')) {
$workflow->apply($accompanyingCourse, 'close');
$workflow->apply($accompanyingCourse, 'close', ['closing_motive' => $form['closingMotive']->getData()]);
$em->flush();

View File

@ -1449,7 +1449,7 @@ class AccompanyingPeriod implements
return $this;
}
public function setStep(string $step): self
public function setStep(string $step, array $context = []): self
{
$previous = $this->step;
@ -1464,7 +1464,7 @@ class AccompanyingPeriod implements
$history = new AccompanyingPeriodStepHistory();
$history->setStep($this->step)->setStartDate(new \DateTimeImmutable('now'));
$this->addStepHistory($history);
$this->addStepHistory($history, $context);
}
return $this;
@ -1507,11 +1507,14 @@ class AccompanyingPeriod implements
return $this;
}
private function addStepHistory(AccompanyingPeriodStepHistory $stepHistory): self
private function addStepHistory(AccompanyingPeriodStepHistory $stepHistory, array $context = []): self
{
if (!$this->stepHistories->contains($stepHistory)) {
$this->stepHistories[] = $stepHistory;
$stepHistory->setPeriod($this);
if (($context['closing_motive'] ?? null) instanceof ClosingMotive) {
$stepHistory->setClosingMotive($context['closing_motive']);
}
$this->ensureStepContinuity();
}

View File

@ -59,6 +59,13 @@ class AccompanyingPeriodStepHistory implements TrackCreationInterface, TrackUpda
*/
private string $step;
/**
* @ORM\ManyToOne(targetEntity=ClosingMotive::class)
*
* @ORM\JoinColumn(nullable=true)
*/
private ?AccompanyingPeriod\ClosingMotive $closingMotive = null;
public function getEndDate(): ?\DateTimeImmutable
{
return $this->endDate;
@ -114,4 +121,16 @@ class AccompanyingPeriodStepHistory implements TrackCreationInterface, TrackUpda
return $this;
}
public function getClosingMotive(): ?AccompanyingPeriod\ClosingMotive
{
return $this->closingMotive;
}
public function setClosingMotive(?AccompanyingPeriod\ClosingMotive $closingMotive): self
{
$this->closingMotive = $closingMotive;
return $this;
}
}

View File

@ -312,4 +312,34 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase
$this->assertNull($period->getRequestorPerson());
$this->assertNull($period->getRequestor());
}
public function testSetStep(): void
{
$period = new AccompanyingPeriod();
$period->setStep(AccompanyingPeriod::STEP_CONFIRMED);
self::assertEquals(AccompanyingPeriod::STEP_CONFIRMED, $period->getStep());
self::assertCount(1, $period->getStepHistories());
$period->setStep(AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT);
self::assertEquals(AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT, $period->getStep());
self::assertCount(2, $period->getStepHistories());
$periodInactiveSteps = $period->getStepHistories()->filter(fn (AccompanyingPeriod\AccompanyingPeriodStepHistory $h) => AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT === $h->getStep());
self::assertCount(1, $periodInactiveSteps);
$period->setStep(AccompanyingPeriod::STEP_CLOSED, ['closing_motive' => $closingMotive = new AccompanyingPeriod\ClosingMotive()]);
self::assertEquals(AccompanyingPeriod::STEP_CLOSED, $period->getStep());
self::assertCount(3, $period->getStepHistories());
$periodClosedSteps = $period->getStepHistories()->filter(fn (AccompanyingPeriod\AccompanyingPeriodStepHistory $h) => AccompanyingPeriod::STEP_CLOSED === $h->getStep());
self::assertCount(1, $periodClosedSteps);
$periodClosedStep = $periodClosedSteps->first();
self::assertSame($closingMotive, $periodClosedStep->getClosingMotive());
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20240123161457 extends AbstractMigration
{
public function getDescription(): string
{
return 'Store closing motive when closing a course';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD closingMotive_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD CONSTRAINT FK_84D514AC504CB38D FOREIGN KEY (closingMotive_id) REFERENCES chill_person_accompanying_period_closingmotive (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD CONSTRAINT FK_84D514AC65FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql(<<<'EOF'
WITH last_step AS (
SELECT * FROM (
SELECT *, rank() OVER (partition by period_id ORDER BY startdate DESC, id DESC) AS r FROM chill_person_accompanying_period_step_history cpapsh
) as sq
WHERE r = 1
)
UPDATE chill_person_accompanying_period_step_history
SET closingMotive_id = chill_person_accompanying_period.closingmotive_id
FROM last_step, chill_person_accompanying_period
WHERE last_step.period_id = chill_person_accompanying_period_step_history.period_id AND chill_person_accompanying_period.id = chill_person_accompanying_period_step_history.period_id
AND last_step.step = 'CLOSED';
EOF);
$this->addSql('CREATE INDEX IDX_84D514AC504CB38D ON chill_person_accompanying_period_step_history (closingMotive_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP CONSTRAINT FK_84D514AC504CB38D');
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP CONSTRAINT FK_84D514AC65FF1AEC');
$this->addSql('DROP INDEX IDX_84D514AC504CB38D');
$this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP closingMotive_id');
}
}