Skip to content

Commit f0235fc

Browse files
committed
feat: remove selection plan ref from presentation when a category is removed from a category group
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent b2e5f45 commit f0235fc

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,28 @@ public function calculateSlug(){
679679
return $this;
680680
}
681681

682+
/**
683+
* @param int[] $selection_plan_ids
684+
* @return Presentation[]
685+
*/
686+
public function getPresentationsBySelectionPlanIds(array $selection_plan_ids): array
687+
{
688+
if (empty($selection_plan_ids)) return [];
689+
690+
$query = <<<DQL
691+
SELECT p
692+
FROM models\summit\Presentation p
693+
WHERE p.category = :track
694+
AND p.selection_plan IN (:selection_plan_ids)
695+
DQL;
696+
697+
return $this->getEM()
698+
->createQuery($query)
699+
->setParameter('track', $this)
700+
->setParameter('selection_plan_ids', $selection_plan_ids)
701+
->getResult();
702+
}
703+
682704
/**
683705
* @return SummitEvent[]
684706
*/

app/Models/Foundation/Summit/Events/Presentations/PresentationCategoryGroup.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
**/
1414

1515
use App\Models\Foundation\Summit\ScheduleEntity;
16+
use App\Models\Foundation\Summit\SelectionPlan;
1617
use Doctrine\Common\Collections\Criteria;
1718
use Illuminate\Support\Facades\Cache;
1819
use Illuminate\Support\Facades\Log;
@@ -70,13 +71,43 @@ class PresentationCategoryGroup extends SilverstripeBaseModel
7071
protected $max_attendee_votes;
7172

7273

74+
/**
75+
* inverse side
76+
* @var SelectionPlan[]
77+
*/
78+
#[ORM\ManyToMany(targetEntity: \App\Models\Foundation\Summit\SelectionPlan::class, mappedBy: 'category_groups', fetch: 'EXTRA_LAZY')]
79+
protected $selection_plans;
80+
7381
public function __construct()
7482
{
7583
parent::__construct();
7684
$this->begin_attendee_voting_period_date = null;
7785
$this->end_attendee_voting_period_date = null;
7886
$this->max_attendee_votes = 0;
7987
$this->categories = new ArrayCollection;
88+
$this->selection_plans = new ArrayCollection;
89+
}
90+
91+
/**
92+
* @return int[]
93+
*/
94+
public function getSelectionPlanIds(): array
95+
{
96+
return $this->selection_plans->map(function ($sp) {
97+
return $sp->getId();
98+
})->toArray();
99+
}
100+
101+
public function addSelectionPlan(SelectionPlan $selection_plan): void
102+
{
103+
if ($this->selection_plans->contains($selection_plan)) return;
104+
$this->selection_plans->add($selection_plan);
105+
}
106+
107+
public function removeSelectionPlan(SelectionPlan $selection_plan): void
108+
{
109+
if (!$this->selection_plans->contains($selection_plan)) return;
110+
$this->selection_plans->removeElement($selection_plan);
80111
}
81112

82113

@@ -382,4 +413,4 @@ public function clearAttendeeVotingPeriod():void{
382413
}
383414

384415
use ScheduleEntity;
385-
}
416+
}

app/Models/Foundation/Summit/SelectionPlan.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class SelectionPlan extends SilverstripeBaseModel
169169
#[ORM\JoinTable(name: 'SelectionPlan_CategoryGroups')]
170170
#[ORM\JoinColumn(name: 'SelectionPlanID', referencedColumnName: 'ID')]
171171
#[ORM\InverseJoinColumn(name: 'PresentationCategoryGroupID', referencedColumnName: 'ID')]
172-
#[ORM\ManyToMany(targetEntity: \models\summit\PresentationCategoryGroup::class, fetch: 'EXTRA_LAZY')]
172+
#[ORM\ManyToMany(targetEntity: \models\summit\PresentationCategoryGroup::class, inversedBy: 'selection_plans', fetch: 'EXTRA_LAZY')]
173173
private $category_groups;
174174

175175
/**
@@ -596,6 +596,7 @@ public function addTrackGroup(PresentationCategoryGroup $track_group)
596596
{
597597
if ($this->category_groups->contains($track_group)) return;
598598
$this->category_groups->add($track_group);
599+
$track_group->addSelectionPlan($this);
599600
}
600601

601602
/**
@@ -605,6 +606,7 @@ public function removeTrackGroup(PresentationCategoryGroup $track_group)
605606
{
606607
if (!$this->category_groups->contains($track_group)) return;
607608
$this->category_groups->removeElement($track_group);
609+
$track_group->removeSelectionPlan($this);
608610
}
609611

610612
public function addEventType(SummitEventType $eventType)

app/Services/Model/Imp/PresentationCategoryGroupService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ public function disassociateTrack2TrackGroup(Summit $summit, $track_group_id, $t
252252
);
253253
}
254254

255+
$presentations = $track->getPresentationsBySelectionPlanIds($track_group->getSelectionPlanIds());
256+
257+
foreach ($presentations as $presentation) {
258+
$presentation->clearSelectionPlan();
259+
}
260+
255261
$track_group->removeCategory($track);
256262

257263
});
@@ -380,4 +386,4 @@ public function disassociateAllowedGroup2TrackGroup(Summit $summit, $track_group
380386

381387
});
382388
}
383-
}
389+
}

tests/oauth2/OAuth2TrackGroupsApiTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php namespace Tests;
2+
use LaravelDoctrine\ORM\Facades\Registry;
3+
use models\summit\Presentation;
4+
use models\utils\SilverstripeBaseModel;
5+
26
/**
37
* Copyright 2018 OpenStack Foundation
48
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -341,6 +345,16 @@ public function testDisassociateTrack2TrackGroup(){
341345

342346
$this->assertResponseStatus(201);
343347

348+
// verify presentations of defaultTrack have a selection plan before disassociation
349+
$presentations = self::$defaultTrack->getPresentationsBySelectionPlanIds(
350+
self::$defaultTrackGroup->getSelectionPlanIds()
351+
);
352+
$this->assertNotEmpty($presentations);
353+
foreach ($presentations as $presentation) {
354+
$this->assertTrue($presentation->getSelectionPlanId() > 0);
355+
}
356+
$presentation_ids = array_map(fn($p) => $p->getId(), $presentations);
357+
344358
// now disassociate
345359
$response = $this->action(
346360
"DELETE",
@@ -353,6 +367,14 @@ public function testDisassociateTrack2TrackGroup(){
353367
);
354368

355369
$this->assertResponseStatus(204);
370+
371+
// reset EM (closed after the API transaction) and re-fetch presentations to verify selection plan was cleared
372+
self::$em = Registry::resetManager(SilverstripeBaseModel::EntityManager);
373+
foreach ($presentation_ids as $id) {
374+
$presentation = self::$em->find(Presentation::class, $id);
375+
$this->assertNotNull($presentation);
376+
$this->assertEquals(0, $presentation->getSelectionPlanId());
377+
}
356378
}
357379

358380
public function testAddPrivateTrackGroup(){
@@ -480,4 +502,4 @@ public function testDeleteExistentTrackGroup(){
480502
$content = $response->getContent();
481503
$this->assertResponseStatus(204);
482504
}
483-
}
505+
}

0 commit comments

Comments
 (0)