add an age aggregator for person

This commit is contained in:
Julien Fastré 2017-01-12 18:49:24 +01:00
parent 64749373aa
commit 136f78d4a9
4 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,85 @@
<?php
/*
* Copyright (C) 2017 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Export\Aggregator;
use Chill\MainBundle\Export\AggregatorInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\DateType;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class AgeAggregator implements AggregatorInterface
{
public function addRole()
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect('DATE_DIFF(:date_age_calculation, person.birthdate)/365 as person_age');
$qb->setParameter('date_age_calculation', $data['date_age_calculation']);
$qb->addGroupBy('person_age');
}
public function applyOn()
{
return 'person';
}
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
{
$builder->add('date_age_calculation', DateType::class, array(
'label' => "Calculate age in relation to this date",
'data' => new \DateTime(),
'attr' => array('class' => 'datepicker'),
'widget'=> 'single_text',
'format' => 'dd-MM-yyyy'
));
}
public function getLabels($key, array $values, $data)
{
return function($value) {
if ($value === '_header') {
return "Age";
}
return $value;
};
}
public function getQueryKeys($data)
{
return array(
'person_age'
);
}
public function getTitle()
{
return "Aggregate by age";
}
}

View File

@ -42,3 +42,8 @@ services:
- "@translator" - "@translator"
tags: tags:
- { name: chill.export_aggregator, alias: person_gender_aggregator } - { name: chill.export_aggregator, alias: person_gender_aggregator }
chill.person.export.aggregator_age:
class: Chill\PersonBundle\Export\Aggregator\AgeAggregator
tags:
- { name: chill.export_aggregator, alias: person_age_aggregator }

View File

@ -161,3 +161,6 @@ Group by continents: Grouper par continent
Group by country: Grouper par pays Group by country: Grouper par pays
Group people by gender: Aggréger les personnes par genre Group people by gender: Aggréger les personnes par genre
Aggregate by age: Aggréger par âge
Calculate age in relation to this date: Calculer l'âge par rapport à cette date

View File

@ -0,0 +1,76 @@
<?php
/*
* Copyright (C) 2017 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Tests\Export\Aggregator;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class AgeAggregatorTest extends AbstractAggregatorTest
{
/**
*
* @var \Chill\PersonBundle\Export\Aggregator\AgeAggregator
*/
private $aggregator;
public function setUp()
{
static::bootKernel();
$container = static::$kernel->getContainer();
$this->aggregator = $container->get('chill.person.export.aggregator_age');
}
public function getAggregator()
{
return $this->aggregator;
}
public function getFormData()
{
return array(
array(
'date_age_calculation' => \DateTime::createFromFormat('Y-m-d','2016-06-16')
)
);
}
public function getQueryBuilders()
{
if (static::$kernel === null) {
static::bootKernel();
}
$em = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager');
return array(
$em->createQueryBuilder()
->select('count(person.id)')
->from('ChillPersonBundle:Person', 'person')
);
}
}