feat: keep questionnaires until submitted, remind teamers daily and take bus feedback per leg
This commit is contained in:
1 parent
abf9c1be0b
commit
e72616fdbf
37 files changed
+1119
-238
No files matched your search
@@ -80,67 +80,18 @@ class DispositionRepository extends ServiceEntityRepository
|
||||
* The teamer's placements with a questionnaire still to be submitted - not started, or saved as
|
||||
* a draft: the teamer feedback, and after a bus escort the bus feedback too
|
||||
* (DispositionContext::questionnaires(), spelled out here in DQL). The rules are those of
|
||||
* DispositionVoter::QUESTIONNAIRE - over (today included), on or after $windowStart, neither
|
||||
* called off nor skip-formalities - so every row shown on the dashboard can be opened.
|
||||
* DispositionVoter::QUESTIONNAIRE - over (today included), on or after go-live, neither called
|
||||
* off nor skip-formalities - so every row shown on the dashboard can be opened.
|
||||
*
|
||||
* The responses are joined as arbitrary entities because the association is one-way; the
|
||||
* questionnaire key has to be part of each JOIN condition, or one questionnaire's response
|
||||
* would stand in for the other's.
|
||||
*
|
||||
* Rows are [0 => Disposition, 'teamerStatus' => ?QuestionnaireResponseStatus,
|
||||
* 'busStatus' => ?QuestionnaireResponseStatus].
|
||||
* Rows are [0 => Disposition, 'drafts' => int], the number of drafts saved for it.
|
||||
*/
|
||||
public function getPendingQuestionnaireQuery(Teamer $teamer, \DateTimeImmutable $windowStart): Query
|
||||
public function getPendingQuestionnaireQuery(Teamer $teamer, \DateTimeImmutable $goLive): Query
|
||||
{
|
||||
$qb = $this->createQueryBuilder('disposition');
|
||||
$qb = $this->createPendingQuestionnaireQueryBuilder($goLive, new \DateTimeImmutable('today'));
|
||||
|
||||
return $qb
|
||||
->select('disposition', 'assignment', 'destination', 'teamer_response.status AS teamerStatus', 'bus_response.status AS busStatus')
|
||||
->innerJoin('disposition.assignment', 'assignment')
|
||||
->innerJoin('assignment.destination', 'destination')
|
||||
->leftJoin(
|
||||
QuestionnaireResponse::class,
|
||||
'teamer_response',
|
||||
Join::ON,
|
||||
'teamer_response.disposition = disposition AND teamer_response.questionnaire = :teamerQuestionnaire'
|
||||
)
|
||||
->leftJoin(
|
||||
QuestionnaireResponse::class,
|
||||
'bus_response',
|
||||
Join::ON,
|
||||
'bus_response.disposition = disposition AND bus_response.questionnaire = :busQuestionnaire'
|
||||
)
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->eq('disposition.teamer', ':teamer'),
|
||||
$qb->expr()->neq('disposition.status', ':dispositionCalledOff'),
|
||||
$qb->expr()->notIn('assignment.status', ':assignmentStatuses'),
|
||||
$qb->expr()->eq('assignment.skipFormalities', ':skipFormalities'),
|
||||
$qb->expr()->gte('destination.dateTo', ':windowStart'),
|
||||
$qb->expr()->lte('destination.dateTo', ':today'),
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNull('teamer_response.id'),
|
||||
$qb->expr()->eq('teamer_response.status', ':draft'),
|
||||
// a bus escort, i.e. Assignment::hasBusEscort(), owing the bus feedback
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->isNotNull('assignment.pickup'),
|
||||
$qb->expr()->neq('assignment.pickup', 0),
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNull('bus_response.id'),
|
||||
$qb->expr()->eq('bus_response.status', ':draft')
|
||||
)
|
||||
)
|
||||
)
|
||||
))
|
||||
->orderBy('destination.dateTo', \SortDirection::Descending)
|
||||
->andWhere($qb->expr()->eq('disposition.teamer', ':teamer'))
|
||||
->setParameter('teamer', $teamer)
|
||||
->setParameter('teamerQuestionnaire', QuestionnaireCatalog::TEAMER_FEEDBACK)
|
||||
->setParameter('busQuestionnaire', QuestionnaireCatalog::BUS_FEEDBACK)
|
||||
->setParameter('dispositionCalledOff', Disposition::STATUS_CALLED_OFF)
|
||||
->setParameter('assignmentStatuses', [Assignment::STATUS_CALLED_OFF, Assignment::STATUS_DELETED])
|
||||
->setParameter('skipFormalities', false)
|
||||
->setParameter('windowStart', $windowStart, Types::DATE_IMMUTABLE)
|
||||
->setParameter('today', new \DateTimeImmutable('today'), Types::DATE_IMMUTABLE)
|
||||
->setParameter('draft', QuestionnaireResponseStatus::DRAFT)
|
||||
->getQuery()
|
||||
;
|
||||
}
|
||||
@@ -148,18 +99,109 @@ class DispositionRepository extends ServiceEntityRepository
|
||||
/**
|
||||
* @return list<array{disposition: Disposition, draft: bool}>
|
||||
*/
|
||||
public function findPendingQuestionnaireByTeamer(Teamer $teamer, \DateTimeImmutable $windowStart): array
|
||||
public function findPendingQuestionnaireByTeamer(Teamer $teamer, \DateTimeImmutable $goLive): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (array $row): array => [
|
||||
'disposition' => $row[0],
|
||||
'draft' => QuestionnaireResponseStatus::DRAFT === $row['teamerStatus']
|
||||
|| QuestionnaireResponseStatus::DRAFT === $row['busStatus'],
|
||||
'draft' => $row['drafts'] > 0,
|
||||
],
|
||||
$this->getPendingQuestionnaireQuery($teamer, $windowStart)->getResult(),
|
||||
$this->getPendingQuestionnaireQuery($teamer, $goLive)->getResult(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every teamer's placements with a questionnaire still to be submitted, for the daily reminder
|
||||
* (QuestionnaireReminderService): the dashboard's rules, but only from the day after the trip
|
||||
* ended, and only teamers who can still log in to submit - not deleted, with a user account that
|
||||
* is not blocked. Otherwise they would be reminded every day, forever.
|
||||
*
|
||||
* Rows are [0 => Disposition, 'drafts' => int], ordered by teamer.
|
||||
*/
|
||||
public function getQuestionnaireReminderQuery(\DateTimeImmutable $goLive, \DateTimeImmutable $today): Query
|
||||
{
|
||||
$qb = $this->createPendingQuestionnaireQueryBuilder($goLive, $today->modify('-1 day'));
|
||||
|
||||
return $qb
|
||||
->addSelect('teamer')
|
||||
->innerJoin('disposition.teamer', 'teamer')
|
||||
->innerJoin('teamer.user', 'user')
|
||||
->andWhere($qb->expr()->isNull('teamer.deletedAt'))
|
||||
->andWhere($qb->expr()->isNull('user.disabledAt'))
|
||||
->orderBy('teamer.id')
|
||||
->addOrderBy('destination.dateTo', \SortDirection::Descending)
|
||||
->getQuery()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<Disposition> ordered by teamer, teamer and user loaded
|
||||
*/
|
||||
public function findQuestionnaireReminders(\DateTimeImmutable $goLive, \DateTimeImmutable $today): array
|
||||
{
|
||||
return array_column($this->getQuestionnaireReminderQuery($goLive, $today)->getResult(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Placements ended between $goLive and $endedBy (both inclusive) that still owe a questionnaire.
|
||||
* A questionnaire counts as done once a response to it is submitted: the bus feedback has one
|
||||
* response per escorted leg, and all of a teamer's legs are submitted together. EXISTS rather
|
||||
* than joins, which would list a placement once per leg.
|
||||
*/
|
||||
private function createPendingQuestionnaireQueryBuilder(\DateTimeImmutable $goLive, \DateTimeImmutable $endedBy): QueryBuilder
|
||||
{
|
||||
$qb = $this->createQueryBuilder('disposition');
|
||||
|
||||
$submitted = fn (string $alias, string $questionnaire): string => $this->getEntityManager()->createQueryBuilder()
|
||||
->select($alias.'.id')
|
||||
->from(QuestionnaireResponse::class, $alias)
|
||||
->where($alias.'.disposition = disposition')
|
||||
->andWhere($alias.'.questionnaire = '.$questionnaire)
|
||||
->andWhere($alias.'.status = :submitted')
|
||||
->getDQL()
|
||||
;
|
||||
|
||||
$drafts = $this->getEntityManager()->createQueryBuilder()
|
||||
->select('COUNT(draft_response.id)')
|
||||
->from(QuestionnaireResponse::class, 'draft_response')
|
||||
->where('draft_response.disposition = disposition')
|
||||
->andWhere('draft_response.status = :draft')
|
||||
->getDQL()
|
||||
;
|
||||
|
||||
return $qb
|
||||
->select('disposition', 'assignment', 'destination', '('.$drafts.') AS drafts')
|
||||
->innerJoin('disposition.assignment', 'assignment')
|
||||
->innerJoin('assignment.destination', 'destination')
|
||||
->where($qb->expr()->andX(
|
||||
$qb->expr()->neq('disposition.status', ':dispositionCalledOff'),
|
||||
$qb->expr()->notIn('assignment.status', ':assignmentStatuses'),
|
||||
$qb->expr()->eq('assignment.skipFormalities', ':skipFormalities'),
|
||||
$qb->expr()->gte('destination.dateTo', ':goLive'),
|
||||
$qb->expr()->lte('destination.dateTo', ':endedBy'),
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->not($qb->expr()->exists($submitted('teamer_response', ':teamerQuestionnaire'))),
|
||||
// a bus escort, i.e. Assignment::hasBusEscort(), owing the bus feedback
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->isNotNull('assignment.pickup'),
|
||||
$qb->expr()->neq('assignment.pickup', 0),
|
||||
$qb->expr()->not($qb->expr()->exists($submitted('bus_response', ':busQuestionnaire')))
|
||||
)
|
||||
)
|
||||
))
|
||||
->orderBy('destination.dateTo', \SortDirection::Descending)
|
||||
->setParameter('teamerQuestionnaire', QuestionnaireCatalog::TEAMER_FEEDBACK)
|
||||
->setParameter('busQuestionnaire', QuestionnaireCatalog::BUS_FEEDBACK)
|
||||
->setParameter('submitted', QuestionnaireResponseStatus::SUBMITTED)
|
||||
->setParameter('draft', QuestionnaireResponseStatus::DRAFT)
|
||||
->setParameter('dispositionCalledOff', Disposition::STATUS_CALLED_OFF)
|
||||
->setParameter('assignmentStatuses', [Assignment::STATUS_CALLED_OFF, Assignment::STATUS_DELETED])
|
||||
->setParameter('skipFormalities', false)
|
||||
->setParameter('goLive', $goLive, Types::DATE_IMMUTABLE)
|
||||
->setParameter('endedBy', $endedBy, Types::DATE_IMMUTABLE)
|
||||
;
|
||||
}
|
||||
|
||||
public function getRecentDispositionsByTeamerQuery(Teamer $teamer): Query
|
||||
{
|
||||
$qb = $this->createQueryBuilder('disposition');
|
||||
|
||||
@@ -81,14 +81,16 @@ class QuestionnaireResponseRepository extends ServiceEntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, QuestionnaireResponse> keyed by questionnaire key
|
||||
* @return array<string, list<QuestionnaireResponse>> keyed by questionnaire key; one response,
|
||||
* or one per leg (outbound first) for a
|
||||
* per-bus-leg questionnaire
|
||||
*/
|
||||
public function findByDisposition(Disposition $disposition): array
|
||||
{
|
||||
$responses = [];
|
||||
|
||||
foreach ($this->findBy(['disposition' => $disposition]) as $response) {
|
||||
$responses[$response->getQuestionnaire()] = $response;
|
||||
foreach ($this->findBy(['disposition' => $disposition], ['leg' => 'ASC']) as $response) {
|
||||
$responses[$response->getQuestionnaire()][] = $response;
|
||||
}
|
||||
|
||||
return $responses;
|
||||
|
||||
Reference in new issue
Block a user