mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
improve exports and filters
This commit is contained in:
parent
67c8c19885
commit
9bcd7a2c10
157
Export/Aggregator/ActivityTypeAggregator.php
Normal file
157
Export/Aggregator/ActivityTypeAggregator.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 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\ActivityBundle\Export\Aggregator;
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ActivityTypeAggregator implements AggregatorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $typeRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $stringHelper;
|
||||
|
||||
const KEY = 'activity_type_aggregator';
|
||||
|
||||
public function __construct(
|
||||
EntityRepository $typeRepository,
|
||||
TranslatableStringHelper $stringHelper
|
||||
) {
|
||||
$this->typeRepository = $typeRepository;
|
||||
$this->stringHelper = $stringHelper;
|
||||
}
|
||||
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
// add select element
|
||||
$qb->addSelect(sprintf('IDENTITY(activity.type) AS %s', self::KEY));
|
||||
|
||||
// make a jointure only if needed
|
||||
/*$join = $qb->getDQLPart('join');
|
||||
if (
|
||||
(array_key_exists('activity', $join)
|
||||
&&
|
||||
!$this->checkJoinAlreadyDefined($join['activity'], 'reasons')
|
||||
)
|
||||
OR
|
||||
(! array_key_exists('activity', $join))
|
||||
) {
|
||||
$qb->add(
|
||||
'join',
|
||||
array('activity' =>
|
||||
new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons')
|
||||
),
|
||||
true);
|
||||
}
|
||||
|
||||
// join category if necessary
|
||||
if ($alias === 'activity_categories_id') {
|
||||
// add join only if needed
|
||||
if (!$this->checkJoinAlreadyDefined($qb->getDQLPart('join')['activity'], 'category')) {
|
||||
$qb->join('reasons.category', 'category');
|
||||
}
|
||||
}*/
|
||||
|
||||
// add the "group by" part
|
||||
$groupBy = $qb->addGroupBy(self::KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a join between Activity and another alias
|
||||
*
|
||||
* @param Join[] $joins
|
||||
* @param string $alias the alias to search for
|
||||
* @return boolean
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return 'activity';
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder)
|
||||
{
|
||||
// no form required for this aggregator
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return "Aggregate by activity type";
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
// for performance reason, we load data from db only once
|
||||
$this->typeRepository->findBy(array('id' => $values));
|
||||
|
||||
return function($value) use ($data) {
|
||||
if ($value === '_header') {
|
||||
return 'Activity type';
|
||||
}
|
||||
|
||||
/* @var $r \Chill\ActivityBundle\Entity\ActivityType */
|
||||
$t = $this->typeRepository->find($value);
|
||||
|
||||
return $this->stringHelper->localize($t->getName());
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
return array(self::KEY);
|
||||
}
|
||||
|
||||
}
|
159
Export/Export/StatActivityDuration.php
Normal file
159
Export/Export/StatActivityDuration.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 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\ActivityBundle\Export\Export;
|
||||
|
||||
use Chill\MainBundle\Export\ExportInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Doctrine\ORM\Query;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* This export allow to compute stats on activity duration.
|
||||
*
|
||||
* The desired stat must be given in constructor.
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class StatActivityDuration implements ExportInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
const SUM = 'sum';
|
||||
|
||||
/**
|
||||
* The action for this report.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $action;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param EntityManagerInterface $em
|
||||
* @param string $action the stat to perform
|
||||
*/
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
$action = 'sum'
|
||||
)
|
||||
{
|
||||
$this->entityManager = $em;
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
if ($this->action === self::SUM) {
|
||||
return "Sum activities duration by various parameters.";
|
||||
}
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
if ($this->action === self::SUM) {
|
||||
return "Sum activity duration";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return 'activity';
|
||||
}
|
||||
|
||||
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
|
||||
{
|
||||
$centers = array_map(function($el) { return $el['center']; }, $acl);
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
|
||||
if ($this->action === self::SUM) {
|
||||
$select = "SUM(activity.durationTime) AS export_stat_activity";
|
||||
}
|
||||
|
||||
$qb->select($select)
|
||||
->from('ChillActivityBundle:Activity', 'activity')
|
||||
->join('activity.person', 'person')
|
||||
->join('person.center', 'center')
|
||||
->where($qb->expr()->in('center', ':centers'))
|
||||
->setParameter(':centers', $centers)
|
||||
;
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function supportsModifiers()
|
||||
{
|
||||
return array('person', 'activity');
|
||||
}
|
||||
|
||||
public function requiredRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function getAllowedFormattersTypes()
|
||||
{
|
||||
return array(\Chill\MainBundle\Export\FormatterInterface::TYPE_TABULAR);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
if ($key !== 'export_stat_activity') {
|
||||
throw new \LogicException("the key $key is not used by this export");
|
||||
}
|
||||
|
||||
switch ($this->action) {
|
||||
case self::SUM:
|
||||
$header = "Sum of activities duration";
|
||||
}
|
||||
|
||||
return function($value) use ($header) {
|
||||
return $value === '_header' ?
|
||||
$header
|
||||
:
|
||||
$value
|
||||
;
|
||||
};
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
return array('export_stat_activity');
|
||||
}
|
||||
|
||||
public function getResult($qb, $data)
|
||||
{
|
||||
return $qb->getQuery()->getResult(Query::HYDRATE_SCALAR);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,14 @@ services:
|
||||
tags:
|
||||
- { name: chill.export, alias: 'count_activity' }
|
||||
|
||||
chill.activity.export.sum_activity_duration:
|
||||
class: Chill\ActivityBundle\Export\Export\StatActivityDuration
|
||||
arguments:
|
||||
- "@doctrine.orm.entity_manager"
|
||||
- "sum"
|
||||
tags:
|
||||
- { name: chill.export, alias: 'sum_activity_duration' }
|
||||
|
||||
chill.activity.export.list_activity:
|
||||
class: Chill\ActivityBundle\Export\Export\ListActivity
|
||||
arguments:
|
||||
@ -46,3 +54,11 @@ services:
|
||||
- "@chill.main.helper.translatable_string"
|
||||
tags:
|
||||
- { name: chill.export_aggregator, alias: activity_reason_aggregator }
|
||||
|
||||
chill.activity.export.type_aggregator:
|
||||
class: Chill\ActivityBundle\Export\Aggregator\ActivityTypeAggregator
|
||||
arguments:
|
||||
- "@chill_activity.repository.activity_type"
|
||||
- "@chill.main.helper.translatable_string"
|
||||
tags:
|
||||
- { name: chill.export_aggregator, alias: activity_type_aggregator }
|
||||
|
@ -106,6 +106,9 @@ Filter by reason: Filtrer par sujet d'activité
|
||||
|
||||
"Filtered by date of activity: only between %date_from% and %date_to%": "Filtré par date de l'activité: uniquement entre %date_from% et %date_to%"
|
||||
|
||||
Filtered by person having an activity in a period: Uniquement les personnes ayant eu une activité dans la période donnée
|
||||
Implied in an activity after this date: Impliqué dans une activité après cette date
|
||||
Implied in an activity before this date: Impliqué dans une activité avant cette date
|
||||
Filtered by person having an activity between %date_from% and %date_to% with reasons %reasons_name%: Filtré par personnes associées à une activité entre %date_from% et %date_to% avec les sujets %reasons_name%
|
||||
Activity reasons for those activities: Sujets de ces activités
|
||||
|
||||
|
92
Tests/Export/Aggregator/ActivityTypeAggregatorTest.php
Normal file
92
Tests/Export/Aggregator/ActivityTypeAggregatorTest.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?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\ActivityBundle\Tests\Aggregator;
|
||||
|
||||
use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
|
||||
|
||||
/**
|
||||
* Add tests for ActivityTypeAggregator
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class ActivityTypeAggregatorTest extends AbstractAggregatorTest
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator
|
||||
*/
|
||||
private $aggregator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
$container = static::$kernel->getContainer();
|
||||
|
||||
$this->aggregator = $container->get('chill.activity.export.type_aggregator');
|
||||
|
||||
// add a fake request with a default locale (used in translatable string)
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$request = $prophet->prophesize();
|
||||
$request->willExtend(\Symfony\Component\HttpFoundation\Request::class);
|
||||
$request->getLocale()->willReturn('fr');
|
||||
|
||||
$container->get('request_stack')
|
||||
->push($request->reveal());
|
||||
}
|
||||
|
||||
public function getAggregator()
|
||||
{
|
||||
return $this->aggregator;
|
||||
}
|
||||
|
||||
public function getFormData()
|
||||
{
|
||||
return array(
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
public function getQueryBuilders()
|
||||
{
|
||||
if (static::$kernel === null) {
|
||||
static::bootKernel();
|
||||
}
|
||||
|
||||
$em = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager');
|
||||
|
||||
return array(
|
||||
$em->createQueryBuilder()
|
||||
->select('count(activity.id)')
|
||||
->from('ChillActivityBundle:Activity', 'activity'),
|
||||
$em->createQueryBuilder()
|
||||
->select('count(activity.id)')
|
||||
->from('ChillActivityBundle:Activity', 'activity')
|
||||
->join('activity.reasons', 'reasons'),
|
||||
$em->createQueryBuilder()
|
||||
->select('count(activity.id)')
|
||||
->from('ChillActivityBundle:Activity', 'activity')
|
||||
->join('activity.reasons', 'reasons')
|
||||
->join('reasons.category', 'category')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
50
Tests/Export/Export/StatActivityDurationSumTest.php
Normal file
50
Tests/Export/Export/StatActivityDurationSumTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ActivityBundle\Tests\Export\Export;
|
||||
|
||||
use Chill\MainBundle\Test\Export\AbstractExportTest;
|
||||
|
||||
/**
|
||||
* Test the "sum" part of StatActivityDuration
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class StatActivityDurationSumTest extends AbstractExportTest
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ActivityBundle\Export\Export\StatActivityDuration
|
||||
*/
|
||||
private $export;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
/* @var $container \Symfony\Component\DependencyInjection\ContainerInterface */
|
||||
$container = self::$kernel->getContainer();
|
||||
|
||||
$this->export = $container->get('chill.activity.export.sum_activity_duration');
|
||||
}
|
||||
|
||||
public function getExport()
|
||||
{
|
||||
return $this->export;
|
||||
}
|
||||
|
||||
public function getFormData()
|
||||
{
|
||||
return array(
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
public function getModifiersCombination()
|
||||
{
|
||||
return array(
|
||||
array('activity'),
|
||||
array('activity', 'person')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2017 Champs Libres Cooperative <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\ActivityBundle\Tests\Filter;
|
||||
|
||||
use Chill\MainBundle\Test\Export\AbstractFilterTest;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class PersonHavingActivityBetweenDateFilterTest extends AbstractFilterTest
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\PersonBundle\Export\Filter\PersonHavingActivityBetweenDateFilter
|
||||
*/
|
||||
private $filter;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
$container = static::$kernel->getContainer();
|
||||
|
||||
$this->filter = $container->get('chill.activity.export.'
|
||||
. 'person_having_an_activity_between_date_filter');
|
||||
|
||||
// add a fake request with a default locale (used in translatable string)
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$request = $prophet->prophesize();
|
||||
$request->willExtend(\Symfony\Component\HttpFoundation\Request::class);
|
||||
$request->getLocale()->willReturn('fr');
|
||||
|
||||
$container->get('request_stack')
|
||||
->push($request->reveal());
|
||||
}
|
||||
|
||||
public function getFilter()
|
||||
{
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
public function getFormData()
|
||||
{
|
||||
$date_from = \DateTime::createFromFormat('Y-m-d', '2015-01-15');
|
||||
$date_to = new \DateTime(); // today
|
||||
$reasons = $this->getActivityReasons();
|
||||
|
||||
|
||||
$data = array();
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$data[] = array(
|
||||
'date_from' => $date_from,
|
||||
'date_to' => $date_to,
|
||||
'reasons' => array_slice($reasons, 0, 1 + $i)
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all activity reasons
|
||||
*
|
||||
* @return \Chill\ActivityBundle\Entity\ActivityReason[]
|
||||
*/
|
||||
private function getActivityReasons()
|
||||
{
|
||||
if (static::$kernel === null) {
|
||||
static::bootKernel();
|
||||
}
|
||||
|
||||
return static::$kernel->getContainer()
|
||||
->get('chill_activity.repository.reason')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
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')
|
||||
// add a fake where clause
|
||||
->where('person.id > 0'),
|
||||
$em->createQueryBuilder()
|
||||
->select('count(person.id)')
|
||||
->from('ChillActivityBundle:Activity', 'activity')
|
||||
->join('activity.person', 'person')
|
||||
// add a fake where clause
|
||||
->where('person.id > 0'),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user