Skip to content

Commit

Permalink
Merge pull request #97 from digio-ch/issue/hc-235
Browse files Browse the repository at this point in the history
WIP: Add filter logic to Abteilungen im Vergleich
  • Loading branch information
SebastianStorz authored Sep 27, 2023
2 parents d764bcf + 455ea41 commit 50aeeb4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/Controller/Api/Apps/CensusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public function getFilterData(Group $group)
return $this->json($this->censusFilterDataProvider->getFilterData($group));
}

/**
* @param Group $group
* @param CensusRequestData $censusRequestData
* @return JsonResponse
*
* @ParamConverter("group", options={"mapping": {"groupId": "id"}})
*/
public function postFilterData(Group $group, CensusRequestData $censusRequestData)
{
$this->denyAccessUnlessGranted(PermissionVoter::VIEWER, $group);
Expand Down
68 changes: 62 additions & 6 deletions src/DTO/Mapper/CensusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\DTO\Model\Apps\Census\DevelopmentWidgetDTO;
use App\DTO\Model\Apps\Census\LineChartDataDTO;
use App\DTO\Model\Apps\Census\TableDTO;
use App\DTO\Model\FilterRequestData\CensusRequestData;
use App\Entity\Midata\CensusGroup;
use App\Entity\Statistics\StatisticGroup;
use Symfony\Component\Validator\Constraints\Date;
Expand All @@ -17,7 +18,7 @@ class CensusMapper
* @param int[] $relevantYears
* @return TableDTO
*/
public static function mapToCensusTable(StatisticGroup $statisticGroup, array $censusGroups, array $relevantYears)
public static function mapToCensusTable(StatisticGroup $statisticGroup, array $censusGroups, array $relevantYears, CensusRequestData $censusRequestData)
{
$dto = new TableDTO();
$dto->setId($statisticGroup->getId());
Expand All @@ -33,6 +34,10 @@ public static function mapToCensusTable(StatisticGroup $statisticGroup, array $c
}
$dto->setMissing(false);

foreach ($censusGroups as $censusGroup) {
self::filterCensusGroup($censusGroup, $censusRequestData);
}

$incomplete = false;
$totalCounts = [];
foreach ($relevantYears as $year) {
Expand All @@ -53,11 +58,11 @@ public static function mapToCensusTable(StatisticGroup $statisticGroup, array $c
$improvementVsLastYear = null;
$improvementVs3YearsAgo = null;
$improvementVsAvg5Years = null;
if (!is_null($totalCounts[count($totalCounts) - 1])) {
if (!is_null($totalCounts[count($totalCounts) - 2])) {
if (!is_null($totalCounts[count($totalCounts) - 1]) && $totalCounts[count($totalCounts) - 1] !== 0) {
if (!is_null($totalCounts[count($totalCounts) - 2]) && $totalCounts[count($totalCounts) - 2] !== 0) {
$improvementVsLastYear = (100 / $totalCounts[count($totalCounts) - 2]) * $totalCounts[count($totalCounts) - 1] - 100;
}
if (!is_null($totalCounts[count($totalCounts) - 4])) {
if (!is_null($totalCounts[count($totalCounts) - 4]) && $totalCounts[count($totalCounts) - 4] !== 0) {
$improvementVs3YearsAgo = (100 / $totalCounts[count($totalCounts) - 4]) * $totalCounts[count($totalCounts) - 1] - 100;
}
}
Expand All @@ -73,13 +78,13 @@ public static function mapToCensusTable(StatisticGroup $statisticGroup, array $c
* @param CensusGroup[] $censusGroups
* @param int[] $relevantYears
*/
public static function mapToLineChart(StatisticGroup $statisticGroup, array $censusGroups, array $relevantYears)
public static function mapToLineChart(StatisticGroup $statisticGroup, array $censusGroups, array $relevantYears, CensusRequestData $censusRequestData)
{
$groupData = new DevelopmentWidgetDTO();
$absolute = [];
$relative = [];
$firstRelevantTotal = null;
foreach ($censusGroups as $censusGroup) {
self::filterCensusGroup($censusGroup, $censusRequestData);
if ($censusGroup->getYear() == $relevantYears[0]) {
$firstRelevantTotal = $censusGroup->getCalculatedTotal();
}
Expand Down Expand Up @@ -111,4 +116,55 @@ public static function mapToLineChart(StatisticGroup $statisticGroup, array $cen
$return->setRelative([$relativeDTO]);
return $return;
}

public static function filterCensusGroup(CensusGroup $group, CensusRequestData $censusRequestData)
{
if (self::isFiltered('biber', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setPtaMCount(0);
}
if (self::isFiltered('biber', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setBiberFCount(0);
}
if (self::isFiltered('woelfe', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setWoelfeMCount(0);
}
if (self::isFiltered('woelfe', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setWoelfeFCount(0);
}
if (self::isFiltered('pfadis', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setPfadisMCount(0);
}
if (self::isFiltered('pfadis', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setPfadisFCount(0);
}
if (self::isFiltered('rover', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setRoverMCount(0);
}
if (self::isFiltered('rover', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setRoverFCount(0);
}
if (self::isFiltered('pio', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setPiosMCount(0);
}
if (self::isFiltered('pio', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setPiosFCount(0);
}
if (self::isFiltered('pta', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setPtaMCount(0);
}
if (self::isFiltered('pta', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setPtaFCount(0);
}
if (self::isFiltered('leiter', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setLeiterMCount(0);
}
if (self::isFiltered('leiter', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setLeiterFCount(0);
}
}

public static function isFiltered($needle, $haystack)
{
return stripos(json_encode($haystack ?? []), $needle) !== false;
}
}
14 changes: 5 additions & 9 deletions src/EventListener/WidgetControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ private function validateWidgetRequest(Group $group, Request $request): WidgetRe

private function validateCensusRequest(Group $group, Request $request): CensusRequestData
{
$genders = $request->get('census-filter-genders');
$m = $request->get('census-filter-males');
$f = $request->get('census-filter-females');
$groups = $request->get('census-filter-departments');
$roles = $request->get('census-filter-roles');
$rolesChoice = new Choice(WidgetDataProvider::CENSUS_ROLES);
Expand All @@ -238,14 +239,9 @@ private function validateCensusRequest(Group $group, Request $request): CensusRe
$data->setGroup($group);
$data->setGroups($groups);
$data->setRoles($roles);
if (is_array($genders)) {
if (array_search('m', $genders)) {
$data->setFilterMales(true);
}
if (array_search('f', $genders)) {
$data->setFilterFemales(true);
}
}
$data->setFilterMales($m === 'true');
$data->setFilterFemales($f === 'true');

return $data;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Repository/General/GroupSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function remove(GroupSettings $entity, bool $flush = true): void
}
}

public function flush()
{
$this->_em->flush();
}

// /**
// * @return GroupSettings[] Returns an array of GroupSettings objects
// */
Expand Down
25 changes: 23 additions & 2 deletions src/Service/DataProvider/CensusDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function getTableData(Group $group, CensusRequestData $censusRequestData)
$dataTransferObjects = [];
$relevantYears = range(date('Y') - 5, date('Y'));
foreach ($flattenedGroups as $flattenedGroup) {
$dataTransferObjects[] = CensusMapper::mapToCensusTable($flattenedGroup, $this->censusGroupRepository->findBy(['group_id' => $flattenedGroup->getId()]), $relevantYears);
$dataTransferObjects[] = CensusMapper::mapToCensusTable($flattenedGroup, $this->censusGroupRepository->findBy(['group_id' => $flattenedGroup->getId()]), $relevantYears, $censusRequestData);
}
return [
'years' => $relevantYears,
Expand All @@ -219,14 +219,15 @@ public function getTableData(Group $group, CensusRequestData $censusRequestData)
public function getDevelopmentData(Group $group, CensusRequestData $censusRequestData)
{
$relevantGroups = $this->getRelevantGroups($group);
$relevantGroups = $this->filterGroups($relevantGroups, $censusRequestData);

$absolute = [];
$relative = [];
$relevantYears = range(date('Y') - 5, date('Y'));
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId()]);
if (!sizeof($data) == 0) {
$dto = CensusMapper::mapToLineChart($relevantGroup, $data, $relevantYears);
$dto = CensusMapper::mapToLineChart($relevantGroup, $data, $relevantYears, $censusRequestData);
$absolute[] = $dto->getAbsolute()[0];
$relative[] = $dto->getRelative()[0];
}
Expand All @@ -242,11 +243,13 @@ public function getDevelopmentData(Group $group, CensusRequestData $censusReques
public function getMembersData(Group $group, CensusRequestData $censusRequestData): array
{
$relevantGroups = $this->getRelevantGroups($group);
$relevantGroups = $this->filterGroups($relevantGroups, $censusRequestData);

$rawResults = [];
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => date('Y')]);
if (!sizeof($data) == 0) {
CensusMapper::filterCensusGroup($data[0], $censusRequestData);
$biber = $data[0]->getBiberMCount() + $data[0]->getBiberFCount();
$woelfe = $data[0]->getWoelfeMCount() + $data[0]->getWoelfeFCount();
$pfadi = $data[0]->getPfadisMCount() + $data[0]->getPfadisFCount();
Expand Down Expand Up @@ -275,13 +278,16 @@ public function getMembersData(Group $group, CensusRequestData $censusRequestDat
public function getTreemapData(Group $group, CensusRequestData $censusRequestData)
{
$relevantGroups = $this->getRelevantGroups($group);
$relevantGroups = $this->filterGroups($relevantGroups, $censusRequestData);

$return = [];
$colors = ['#EEE09F', '#3BB5DC', '#9A7A54', '#1DA650', '#DD1F19', '#d9b826', '#929292'];
$colorIndex = 0;
$usedColors = [];
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => date('Y')]);
if (!sizeof($data) == 0) {
CensusMapper::filterCensusGroup($data[0], $censusRequestData);
$dto = new TreemapWidgetDTO();
$dto->setName($relevantGroup->getName());
$parentName = $relevantGroup->getParentGroup()->getName();
Expand All @@ -297,4 +303,19 @@ public function getTreemapData(Group $group, CensusRequestData $censusRequestDat
}
return $return;
}

/**
* Filter out groups based on the Frontend Table filter
* @param array $statisticGroups
* @param CensusRequestData $censusRequestData
* @return array
*/
private function filterGroups(array $statisticGroups, CensusRequestData $censusRequestData)
{
return array_filter($statisticGroups, function ($group) use ($censusRequestData) {
return sizeof(array_filter($censusRequestData->getGroups(), function ($groupId) use ($group) {
return $groupId == $group->getId();
})) === 0;
});
}
}
9 changes: 5 additions & 4 deletions src/Service/DataProvider/CensusFilterDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(GroupSettingsRepository $groupSettingsRepository)
$this->groupSettingsRepository = $groupSettingsRepository;
}

public function getFilterData(Group $group): CensusFilterDTO
public function getFilterData(Group $group)
{
$groupSettings = $this->groupSettingsRepository->find($group->getId());
return $this->mapGroupSettingsToCensusFilter($groupSettings);
Expand All @@ -25,9 +25,9 @@ public function getFilterData(Group $group): CensusFilterDTO
private function mapGroupSettingsToCensusFilter(GroupSettings $groupSettings): CensusFilterDTO
{
$filterData = new CensusFilterDTO();
$filterData->setFilterFemales($groupSettings->getCensusFilterFemales() ?? false);
$filterData->setFilterMales($groupSettings->getCensusFilterMales() ?? false);
$filterData->setRoles($groupSettings->getCensusRoles() ?? ['rover']);
$filterData->setFilterFemales(is_null($groupSettings->getCensusFilterFemales()) ? true : $groupSettings->getCensusFilterFemales());
$filterData->setFilterMales(is_null($groupSettings->getCensusFilterMales()) ? true : $groupSettings->getCensusFilterMales());
$filterData->setRoles($groupSettings->getCensusRoles() ?? []);
$filterData->setGroups($groupSettings->getCensusGroups() ?? []);
return $filterData;
}
Expand All @@ -39,6 +39,7 @@ public function setFilterData(Group $group, CensusRequestData $censusRequestData
$groupSettings->setCensusRoles($censusRequestData->getRoles());
$groupSettings->setCensusFilterFemales($censusRequestData->isFilterFemales());
$groupSettings->setCensusFilterMales($censusRequestData->isFilterMales());
$this->groupSettingsRepository->flush();
return $this->mapGroupSettingsToCensusFilter($groupSettings);
}
}

0 comments on commit 50aeeb4

Please sign in to comment.