accompanying period validation - on social issues suppression

This commit is contained in:
juminet
2021-12-07 17:16:13 +00:00
committed by Julien Fastré
parent 6cc0f1f98b
commit b683630136
13 changed files with 206 additions and 39 deletions

View File

@@ -25,6 +25,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\AccompanyingPeriodValidity;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ResourceDuplicateCheck;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
@@ -58,6 +59,8 @@ use const SORT_REGULAR;
* "this.isConfidential and this.getUser === NULL",
* message="If the accompanying course is confirmed and confidential, a referrer must remain assigned."
* )
*
* @AccompanyingPeriodValidity(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*/
class AccompanyingPeriod implements
GroupSequenceProviderInterface,

View File

@@ -121,6 +121,27 @@ class SocialIssue
return $ancestors;
}
/**
* get all the ancestors of the social issue
*
* @param bool $includeThis if the array in the result must include the present SocialIssue
*/
public function getAncestors(bool $includeThis = true): array
{
$ancestors = [];
if ($includeThis) {
$ancestors[] = $this;
}
$current = $this;
while ($current->hasParent()) {
$ancestors[] = $current = $current->getParent();
}
return $ancestors;
}
/**
* @return Collection|self[]
*/

View File

@@ -61,4 +61,20 @@ final class SocialIssueTest extends TestCase
$this->assertFalse($child->isDescendantOf($grandChild));
}
public function testGetAncestors()
{
$parent = new SocialIssue();
$child = (new SocialIssue())->setParent($parent);
$grandChild = (new SocialIssue())->setParent($child);
$grandGrandChild = (new SocialIssue())->setParent($grandChild);
$unrelated = new SocialIssue();
$this->assertContains($parent, $grandGrandChild->getAncestors(true));
$this->assertContains($child, $grandGrandChild->getAncestors(true));
$this->assertContains($grandChild, $grandGrandChild->getAncestors(true));
$this->assertContains($grandGrandChild, $grandGrandChild->getAncestors(true));
$this->assertNotContains($grandGrandChild, $grandGrandChild->getAncestors(false));
$this->assertCount(0, $unrelated->getAncestors(false));
}
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class AccompanyingPeriodValidity extends Constraint
{
public $messageSocialIssueCannotBeDeleted = 'The social %name% issue cannot be deleted because it is associated with an activity or an action';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
use Chill\ActivityBundle\Repository\ActivityRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use function in_array;
class AccompanyingPeriodValidityValidator extends ConstraintValidator
{
private ActivityRepository $activityRepository;
private SocialIssueRender $socialIssueRender;
public function __construct(ActivityRepository $activityRepository, SocialIssueRender $socialIssueRender)
{
$this->activityRepository = $activityRepository;
$this->socialIssueRender = $socialIssueRender;
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof AccompanyingPeriodValidity) {
throw new UnexpectedTypeException($constraint, AccompanyingPeriodValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($period, AccompanyingPeriod::class);
}
$socialIssues = [];
$activities = $this->activityRepository->findBy(['accompanyingPeriod' => $period]);
foreach ($activities as $activity) {
$socialIssues = $activity->getSocialIssues()->toArray();
}
foreach ($period->getWorks() as $work) {
$socialIssues[] = $work->getSocialAction()->getIssue();
}
$socialIssuesByKey = [];
foreach ($socialIssues as $si) {
$socialIssuesByKey[$si->getId()] = $si;
}
$periodIssuesWithAncestors = [];
foreach ($period->getSocialIssues() as $si) {
/** @var SocialIssue $si */
$periodIssuesWithAncestors = array_merge($periodIssuesWithAncestors, \array_map(
function (SocialIssue $si) { return $si->getId(); },
$si->getAncestors(true))
);
}
foreach ($socialIssuesByKey as $key => $si) {
if (!in_array($key, $periodIssuesWithAncestors)) {
$this->context
->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted
)
->setParameter('%name%', $this->socialIssueRender->renderString($si, []))
->addViolation();
}
}
}
}

View File

@@ -46,3 +46,4 @@ household_membership:
'{{ name }} is already associated to this accompanying course.': '{{ name }} est déjà associé à ce parcours.'
A course must contains at least one social issue: 'Un parcours doit être associé à au moins une problématique sociale'
A course must be associated to at least one scope: 'Un parcours doit être associé à au moins un service'
The social %name% issue cannot be deleted because it is associated with an activity or an action: 'La problématique sociale "%name%" ne peut pas être supprimée car elle est associée à une activité ou une action'