An activity may have multiple reasons

The relationship between Activity and Reasons is now many-to-many.

Some improvement in show activity layout.

refs #4 #7
This commit is contained in:
Julien Fastré 2016-02-22 14:48:35 +01:00
parent 65e7a130c5
commit c60d6acf97
11 changed files with 197 additions and 39 deletions

View File

@ -84,9 +84,15 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface, C
*
* @return \Chill\ActivityBundle\Entity\ActivityReason
*/
private function getRandomActivityReason()
private function getRandomActivityReason(array $excludingIds)
{
$reasonRef = LoadActivityReason::$references[array_rand(LoadActivityReason::$references)];
if (in_array($this->getReference($reasonRef)->getId(), $excludingIds)) {
// we have a reason which should be excluded. Find another...
return $this->getRandomActivityReason($excludingIds);
}
return $this->getReference($reasonRef);
}
@ -109,10 +115,17 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface, C
->setDate($this->faker->dateTimeThisYear())
->setDurationTime($this->faker->dateTime(36000))
->setType($this->getRandomActivityType())
->setReason($this->getRandomActivityReason())
->setScope($this->getRandomScope())
->setAttendee($this->faker->boolean())
->setRemark('A remark');
$usedId = array();
for ($i = 0; $i < rand(0, 4); $i++) {
$reason = $this->getRandomActivityReason($usedId);
$usedId[] = $reason->getId();
$activity->addReason($reason);
}
return $activity;
}

View File

@ -27,6 +27,7 @@ use Chill\ActivityBundle\Entity\ActivityType;
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Entity\HasCenterInterface;
use Chill\MainBundle\Entity\HasScopeInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Activity
@ -64,9 +65,9 @@ class Activity implements HasCenterInterface, HasScopeInterface
private $attendee;
/**
* @var ActivityReason
* @var \Doctrine\Common\Collections\Collection
*/
private $reason;
private $reasons;
/**
* @var ActivityType
@ -82,6 +83,11 @@ class Activity implements HasCenterInterface, HasScopeInterface
* @var Person
*/
private $person;
public function __construct()
{
$this->reasons = new ArrayCollection();
}
/**
@ -215,27 +221,32 @@ class Activity implements HasCenterInterface, HasScopeInterface
}
/**
* Set reason
* Add a reason
*
* @param ActivityReason $reason
*
* @return Activity
*/
public function setReason(ActivityReason $reason)
public function addReason(ActivityReason $reason)
{
$this->reason = $reason;
$this->reasons[] = $reason;
return $this;
}
public function removeReason(ActivityReason $reason)
{
$this->reasons->removeElement($reason);
}
/**
* Get reason
* Get reasons
*
* @return ActivityReason
* @return \Doctrine\Common\Collections\Collection
*/
public function getReason()
public function getReasons()
{
return $this->reason;
return $this->reasons;
}
/**

View File

@ -85,7 +85,10 @@ class ActivityType extends AbstractType
))
->add('user')
->add('scope')
->add('reason', 'translatable_activity_reason')
->add('reasons', 'translatable_activity_reason', array(
'multiple' => true,
'required' => false
))
->add('type', 'translatable_activity_type')
//->add('person')
;

View File

@ -16,13 +16,14 @@ Chill\ActivityBundle\Entity\Activity:
type: text
attendee:
type: boolean
manyToMany:
reasons:
targetEntity: Chill\ActivityBundle\Entity\ActivityReason
manyToOne:
user:
targetEntity: Chill\MainBundle\Entity\User
scope:
targetEntity: Chill\MainBundle\Entity\Scope
reason:
targetEntity: Chill\ActivityBundle\Entity\ActivityReason
type:
targetEntity: Chill\ActivityBundle\Entity\ActivityType
person:

View File

@ -0,0 +1,79 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Migrate schema to allow multiple or empty reasons on an activity.
*
* The relation between the activity and reason **was** oneToMany. After this
* migration, the relation will be manyToMany.
*/
class Version20160222103457 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql',
'Migration can only be executed safely on \'postgresql\'.');
// create the new table activity reason
$this->addSql('CREATE TABLE activity_activityreason ('
. 'activity_id INT NOT NULL, '
. 'activityreason_id INT NOT NULL, '
. 'PRIMARY KEY(activity_id, activityreason_id))'
);
$this->addSql('CREATE INDEX IDX_338A864381C06096 ON activity_activityreason (activity_id)');
$this->addSql('CREATE INDEX IDX_338A8643D771E0FC ON activity_activityreason (activityreason_id)');
$this->addSql('ALTER TABLE activity_activityreason '
. 'ADD CONSTRAINT FK_338A864381C06096 FOREIGN KEY (activity_id) '
. 'REFERENCES Activity (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE activity_activityreason '
. 'ADD CONSTRAINT FK_338A8643D771E0FC FOREIGN KEY (activityreason_id) '
. 'REFERENCES ActivityReason (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
// migrate old data to new table
$this->addSql('INSERT INTO activity_activityreason (activity_id, activityreason_id) '
. 'SELECT id, reason_id FROM activity WHERE reason_id IS NOT NULL');
// remove old column
$this->addSql('ALTER TABLE activity DROP CONSTRAINT fk_55026b0c59bb1592');
$this->addSql('DROP INDEX idx_55026b0c59bb1592');
$this->addSql('ALTER TABLE activity DROP reason_id');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql',
'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER TABLE Activity ADD reason_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE Activity ADD CONSTRAINT '
. 'fk_55026b0c59bb1592 FOREIGN KEY (reason_id) '
. 'REFERENCES activityreason (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_55026b0c59bb1592 ON Activity (reason_id)');
// try to keep at least on activity reason...
$this->addSql('UPDATE activity
SET reason_id=rid
FROM (
SELECT activity_id AS aid, MIN(activityreason_id) AS rid
FROM activity_activityreason
GROUP BY activity_id ) AS sb
WHERE sb.aid = activity.id'
);
$this->addSql('DROP TABLE activity_activityreason');
}
}

View File

@ -4,7 +4,7 @@ Edit the activity: Modifier l'activité
Activity: Activité
Duration time: Durée
Duration Time: Durée
Reason: Sujet
Reasons: Sujets
Attendee: Présence de la personne
Remark: Notes
Add a new activity: Ajouter une nouvelle activité
@ -15,6 +15,8 @@ Delete: Supprimer
Update: Mettre à jour
Update activity: Édition de l'activité
Scope: Cercle
Activity data: Données de l'activité
No reason associated: Aucun sujet
#forms
Activity creation: Nouvelle activité

View File

@ -18,12 +18,23 @@
{% set activeRouteKey = 'chill_activity_activity_list' %}
{% block title %}{{ 'Activity edit' |trans }}{% endblock title %}
{% block title 'Update activity'|trans %}
{% block personcontent %}
<h1>{{ "Update activity"|trans }}</h1>
{{ form_start(edit_form) }}
{{ form_row(edit_form.user) }}
{{ form_row(edit_form.scope) }}
<h2>{{ 'Activity data'|trans }}</h2>
{{ form_row(edit_form.date) }}
{{ form_row(edit_form.durationTime) }}
{{ form_row(edit_form.remark) }}
{{ form_row(edit_form.attendee) }}
{{ form_row(edit_form.reasons) }}
{{ form_row(edit_form.type) }}
{{ form_widget(edit_form) }}
<div class="grid-12 centered sticky-form-buttons">

View File

@ -16,6 +16,8 @@
#}
{% extends "ChillPersonBundle::layout.html.twig" %}
{% import 'ChillActivityBundle:ActivityReason:macro.html.twig' as m %}
{% set activeRouteKey = 'chill_activity_activity_list' %}
{% block title %}{{ 'Activity list' |trans }}{% endblock title %}
@ -26,10 +28,9 @@
<tr>
<th class="chill-red">{{'Date' | trans }}</th>
<th class="chill-green">{{'Duration Time' | trans }}</th>
<th class="chill-orange">{{'Reason' | trans}}</th>
<th class="chill-orange">{{'Reasons' | trans}}</th>
<th>{{'Type' | trans}}</th>
<th></th>
<th></th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
@ -37,13 +38,22 @@
<tr>
<td>{% if activity.date %}{{ activity.date|localizeddate('long', 'none') }}{% endif %}</td>
<td>{{ activity.durationTime|date('H:i') }}</td>
<td>{{ activity.reason.name | localize_translatable_string }}</td>
<td>
{%- if activity.reasons is empty -%}
{{ 'No reason associated'|trans }}
{%- else -%}
{% for r in activity.reasons %}{{ m.reason(r) }} {% endfor %}
{%- endif -%}
</td>
<td>{{ activity.type.name | localize_translatable_string }}</td>
<td>
<a href="{{ path('chill_activity_activity_show', { 'id': activity.id, 'person_id': person.id }) }}" class="sc-button black">{{ 'Show' | trans }}</a>
</td>
<td>
<a href="{{ path('chill_activity_activity_edit', { 'id': activity.id, 'person_id': person.id }) }}" class="sc-button bt-update">{{ 'Edit' | trans }}</a>
<ul class="record_actions">
<li>
<a href="{{ path('chill_activity_activity_show', { 'id': activity.id, 'person_id': person.id }) }}" class="sc-button">{{ 'show'|trans|capitalize }}</a>
</li>
<li>
<a href="{{ path('chill_activity_activity_edit', { 'id': activity.id, 'person_id': person.id }) }}" class="sc-button bt-update">{{ 'Edit' | trans }}</a>
</li>
</td>
</tr>
{% endfor %}

View File

@ -2,28 +2,43 @@
{% set activeRouteKey = 'chill_activity_activity_list' %}
{% block title 'Activity'|trans %}
{% import 'ChillActivityBundle:ActivityReason:macro.html.twig' as m %}
{% block personcontent -%}
<h2 class="chill-red">{{ "Activity"|trans }}</h1>
<h1 >{{ "Activity"|trans }}</h1>
<dl>
<dt class="inline">{{ 'Person'|trans }}</dt>
<dd>{{ entity.person }}</dd>
<dl class="chill_view_data">
<dt class="inline">{{ 'User'|trans }}</dt>
<dd>{{ entity.user }}</dd>
<dt class="inline">{{ 'Scope'|trans }}</dt>
<dd><span class="scope">{{ entity.scope.name|localize_translatable_string }}</span></dd>
<h2 class="chill-red">{{ 'Activity data'|trans }}</h2>
<dt class="inline">{{ 'Person'|trans }}</dt>
<dd>{{ entity.person }}</dd>
<dt class="inline">{{ 'Date'|trans }}</dt>
<dd>{{ entity.date|localizeddate('long', 'none') }}</dd>
<dt class="inline">{{ 'Duration Time'|trans }}</dt>
<dd>{{ entity.durationTime|date('H:i') }}</dd>
<dt class="inline">{{ 'User'|trans }}</dt>
<dd>{{ entity.user }}</dd>
<dt class="inline">{{ 'Reason'|trans }}</dt>
<dd>{{ entity.reason.name | localize_translatable_string }}</dd>
<dt class="inline">{{ 'Type'|trans }}</dt>
<dd>{{ entity.type.name | localize_translatable_string }}</dd>
<dt class="inline">{{ 'Remark'|trans }}</dt>
<dd>{% if entity.remark is empty %}{{ 'No remarks'|trans }}{% else %}<blockquote class="chill-user-quote">{{ entity.remark|nl2br }}</blockquote>{% endif %}</dd>
<dt class="inline">{{ 'Attendee'|trans }}</dt>
<dd>{% if entity.attendee is not null %}{% if entity.attendee %}{{ 'present'|trans|capitalize }} {% else %} {{ 'not present'|trans|capitalize }}{% endif %}{% else %}{{ 'None'|trans|capitalize }}{% endif %}</dd>
<dt class="inline">{{ 'Remark'|trans }}</dt>
<dd>{% if entity.remark is empty %}{{ 'No remarks'|trans }}{% else %}<pre>{{ entity.remark }}</pre>{% endif %}</dd>
<dt class="inline">{{ 'Reasons'|trans }}</dt>
{%- if entity.reasons is empty -%}
<dd><span class="chill-no-data-statement">{{ 'No reason associated'|trans }}</span></dd>
{%- else -%}
<dd>{% for r in entity.reasons %}{{ m.reason(r) }} {% endfor %}</dd>
{%- endif -%}
<dt class="inline">{{ 'Type'|trans }}</dt>
<dd>{{ entity.type.name | localize_translatable_string }}</dd>
<dt class="inline">{{ ''|trans }}</dt>
<dd></dd>
<dt class="inline">{{ ''|trans }}</dt>

View File

@ -0,0 +1,3 @@
{%- macro reason(r) -%}
<span class="entity entity-activity activity-reason"><i class="fa fa-question-circle"></i>&nbsp;{{ r.name | localize_translatable_string }}</span>
{%- endmacro -%}

View File

@ -106,6 +106,9 @@ class ActivityControllerTest extends WebTestCase
"Unexpected HTTP status code for GET /activity/");
$crawler = $client->click($crawler->selectLink('Ajouter une nouvelle activité')
->link());
$reason1 = $this->getRandomActivityReason();
$reason2 = $this->getRandomActivityReason(array($reason1->getId()));
// Fill in the form and submit it
$form = $crawler->selectButton('Ajouter une nouvelle activité')->form(array(
@ -117,10 +120,10 @@ class ActivityControllerTest extends WebTestCase
),
'remark' => 'blabla',
'scope' => $this->getRandomScope('center a_social', 'Center A')->getId(),
'reason' => $this->getRandomActivityReason()->getId(),
'type' => $this->getRandomActivityType()->getId()
)
));
$form['chill_activitybundle_activity[reasons]']->select(array ($reason1->getId(), $reason2->getId()));
$client->submit($form);
@ -240,16 +243,23 @@ class ActivityControllerTest extends WebTestCase
/**
*
* @param int[] $excludeIds An array of id to exclude
* @return \Chill\ActivityBundle\Entity\ActivityReason
*/
private function getRandomActivityReason()
private function getRandomActivityReason(array $excludeIds = array())
{
$reasons = static::$kernel->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('ChillActivityBundle:ActivityReason')
->findAll();
return $reasons[array_rand($reasons)];
$reason = $reasons[array_rand($reasons)];
if (in_array($reason->getId(), $excludeIds)) {
return $this->getRandomActivityReason($excludeIds);
}
return $reason;
}
/**