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
@@ -6,6 +6,7 @@ use App\Service\Cron\DispositionReminderService;
|
||||
use App\Service\Cron\DispositionStatusService;
|
||||
use App\Service\Cron\FeedbackReminderService;
|
||||
use App\Service\Cron\FlushLogsService;
|
||||
use App\Service\Cron\QuestionnaireReminderService;
|
||||
use App\Service\Cron\SeasonTaskReminderService;
|
||||
use App\Service\Cron\UploadReminderService;
|
||||
use App\Service\Cron\WorkMonthReminderService;
|
||||
@@ -29,6 +30,7 @@ class CronCommand extends Command
|
||||
private readonly DispositionReminderService $dispositionReminderService,
|
||||
private readonly SeasonTaskReminderService $seasonTaskReminderService,
|
||||
private readonly WorkMonthReminderService $workMonthReminderService,
|
||||
private readonly QuestionnaireReminderService $questionnaireReminderService,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
@@ -68,6 +70,9 @@ class CronCommand extends Command
|
||||
$message = $this->workMonthReminderService->sendReminders();
|
||||
$io->info($message);
|
||||
|
||||
$message = $this->questionnaireReminderService->sendReminders();
|
||||
$io->info($message);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ enum EmailTextKey: string
|
||||
case REMINDER_INVOICE_UPLOAD = 'reminder_invoice_upload';
|
||||
case REMINDER_INVOICE_UPLOAD_FINAL = 'reminder_invoice_upload_final';
|
||||
case REMINDER_DISPOSITION = 'reminder_disposition';
|
||||
case REMINDER_QUESTIONNAIRE = 'reminder_questionnaire';
|
||||
case REMINDER_SEASON_TASKS = 'reminder_season_tasks';
|
||||
case REMINDER_WORK_MONTH_UPLOAD = 'reminder_work_month_upload';
|
||||
case WORK_MONTH_REJECTED = 'work_month_rejected';
|
||||
|
||||
@@ -35,7 +35,7 @@ class QuestionnaireCatalog
|
||||
*/
|
||||
public const string BUS_FEEDBACK = 'bus_feedback';
|
||||
|
||||
private const array ROOT_FIELDS = ['key', 'version', 'title', 'description', 'sections'];
|
||||
private const array ROOT_FIELDS = ['key', 'version', 'title', 'description', 'per_bus_leg', 'sections'];
|
||||
private const array SECTION_FIELDS = ['key', 'title', 'questions'];
|
||||
private const array QUESTION_FIELDS = ['key', 'label', 'type', 'required', 'help', 'widget', 'options', 'show_if'];
|
||||
private const array OPTION_FIELDS = ['key', 'label'];
|
||||
@@ -57,17 +57,14 @@ class QuestionnaireCatalog
|
||||
}
|
||||
|
||||
/**
|
||||
* The first end date the teamer questionnaire is still offered for: the services.yaml
|
||||
* parameter questionnaire_window_days, in days before today. Placements that ended earlier are neither
|
||||
* pending nor open, so a deploy does not offer it for every past placement at once.
|
||||
* The services.yaml parameter questionnaire_go_live as a date: the teamer questionnaire is
|
||||
* offered for placements ending on or after it, and stays pending until submitted. Without
|
||||
* it, every past placement would be pending and reminded of at once.
|
||||
*/
|
||||
public static function windowStart(int $windowDays): \DateTimeImmutable
|
||||
public static function parseGoLive(string $goLive): \DateTimeImmutable
|
||||
{
|
||||
if ($windowDays < 0) {
|
||||
throw new \InvalidArgumentException(sprintf('questionnaire_window_days must not be negative, got %d', $windowDays));
|
||||
}
|
||||
|
||||
return new \DateTimeImmutable(sprintf('today -%d days', $windowDays));
|
||||
return \DateTimeImmutable::createFromFormat('!Y-m-d', $goLive)
|
||||
?: throw new \InvalidArgumentException(sprintf('questionnaire_go_live "%s" is not a Y-m-d date', $goLive));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,6 +147,12 @@ class QuestionnaireCatalog
|
||||
throw $this->error($file, 'sections must be a non-empty list');
|
||||
}
|
||||
|
||||
$perBusLeg = $data['per_bus_leg'] ?? false;
|
||||
|
||||
if (!is_bool($perBusLeg)) {
|
||||
throw $this->error($file, 'per_bus_leg must be true or false');
|
||||
}
|
||||
|
||||
$sections = [];
|
||||
$sectionKeys = [];
|
||||
/** @var array<string, Question> $questions every question parsed so far, for show_if */
|
||||
@@ -189,6 +192,7 @@ class QuestionnaireCatalog
|
||||
$this->string($data['title'], 'title', $file),
|
||||
$this->optionalString($data['description'] ?? null, 'description', $file),
|
||||
$sections,
|
||||
$perBusLeg,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,9 +42,10 @@ class DetailController extends AbstractController
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
// the other questionnaire of the same placement, e.g. the bus feedback beside the teamer feedback
|
||||
// the other responses of the same placement, e.g. the bus feedback beside the teamer feedback,
|
||||
// or the other leg's
|
||||
$siblings = array_filter(
|
||||
$this->responseRepository->findByDisposition($response->getDisposition()),
|
||||
array_merge(...array_values($this->responseRepository->findByDisposition($response->getDisposition()))),
|
||||
static fn (QuestionnaireResponse $sibling): bool => $sibling !== $response && $sibling->isSubmitted(),
|
||||
);
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ use App\Config\QuestionnaireCatalog;
|
||||
use App\Controller\Traits\ReturnUrlTrait;
|
||||
use App\Entity\Assignment;
|
||||
use App\Form\QuestionnaireType;
|
||||
use App\Model\Questionnaire\Definition;
|
||||
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -54,10 +53,11 @@ class TestController extends AbstractController
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if (QuestionnaireType::isFinal($form)) {
|
||||
$submitted = array_map(static fn (Definition $definition): array => [
|
||||
'definition' => $definition,
|
||||
'answers' => $definition->withoutHiddenAnswers($form->getData()[$definition->key] ?? []),
|
||||
], $definitions);
|
||||
$submitted = array_map(static fn (array $set): array => [
|
||||
'definition' => $set['definition'],
|
||||
'leg' => $set['leg'],
|
||||
'answers' => $set['definition']->withoutHiddenAnswers($set['answers']),
|
||||
], QuestionnaireType::answerSets($form->getData(), $definitions));
|
||||
} else {
|
||||
$draft = true;
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ class IndexController extends AbstractController
|
||||
->getRepository(Disposition::class)
|
||||
->findPendingQuestionnaireByTeamer(
|
||||
$teamer,
|
||||
QuestionnaireCatalog::windowStart((int) $this->getParameter('questionnaire_window_days')),
|
||||
QuestionnaireCatalog::parseGoLive($this->getParameter('questionnaire_go_live')),
|
||||
)
|
||||
;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Controller\Teamer\Questionnaire;
|
||||
|
||||
use App\Config\QuestionnaireCatalog;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\QuestionnaireResponse;
|
||||
use App\Repository\QuestionnaireResponseRepository;
|
||||
use App\Service\Questionnaire\DispositionContext;
|
||||
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
||||
@@ -13,8 +14,8 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
/**
|
||||
* The teamer's own submitted questionnaires for a placement, read-only, each rendered with the
|
||||
* definition version it was submitted under.
|
||||
* The teamer's own submitted questionnaires for a placement, read-only - the bus feedback once per
|
||||
* escorted leg - each rendered with the definition version it was submitted under.
|
||||
*/
|
||||
class DetailController extends AbstractController
|
||||
{
|
||||
@@ -39,16 +40,16 @@ class DetailController extends AbstractController
|
||||
$keys = array_unique([...DispositionContext::questionnaires($disposition->getAssignment()), ...array_keys($responses)]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$response = $responses[$key] ?? null;
|
||||
$done = array_filter($responses[$key] ?? [], static fn (QuestionnaireResponse $response): bool => $response->isSubmitted());
|
||||
|
||||
if (true === $response?->isSubmitted()) {
|
||||
foreach ($done as $response) {
|
||||
$submitted[] = [
|
||||
'definition' => $this->catalog->get($key, $response->getVersion()),
|
||||
'response' => $response,
|
||||
];
|
||||
} else {
|
||||
$open = true;
|
||||
}
|
||||
|
||||
$open = $open || [] === $done;
|
||||
}
|
||||
|
||||
if ([] === $submitted) {
|
||||
|
||||
@@ -19,8 +19,8 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
/**
|
||||
* The teamer's questionnaires after a placement on one page: the teamer feedback, and after a bus
|
||||
* escort the bus feedback too. Saved as drafts until submitted; one submit submits all of them,
|
||||
* each as a response of its own. Submitted ones are final and only shown read-only
|
||||
* escort the bus feedback too, once per ride the teamer escorted. Saved as drafts until submitted;
|
||||
* one submit submits all of them, each questionnaire and leg as a response of its own. Submitted ones are final and only shown read-only
|
||||
* (DetailController).
|
||||
*
|
||||
* Only the questionnaires not yet submitted are offered, so one that applies later - an escort
|
||||
@@ -48,7 +48,7 @@ class EditController extends AbstractController
|
||||
$definitions = [];
|
||||
|
||||
foreach (DispositionContext::questionnaires($disposition->getAssignment()) as $key) {
|
||||
if (true !== ($responses[$key] ?? null)?->isSubmitted()) {
|
||||
if ([] === array_filter($responses[$key] ?? [], static fn (QuestionnaireResponse $response): bool => $response->isSubmitted())) {
|
||||
$definitions[] = $this->catalog->get($key);
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,14 @@ class EditController extends AbstractController
|
||||
$data = [];
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$data[$definition->key] = ($responses[$definition->key] ?? null)?->getAnswers() ?? [];
|
||||
$stored = $responses[$definition->key] ?? [];
|
||||
|
||||
$data[$definition->key] = $definition->perBusLeg
|
||||
? QuestionnaireType::legData(array_column(array_map(
|
||||
static fn (QuestionnaireResponse $response): array => [$response->getLeg()->value, $response->getAnswers()],
|
||||
$stored,
|
||||
), 1, 0))
|
||||
: ($stored[0] ?? null)?->getAnswers() ?? [];
|
||||
}
|
||||
|
||||
$form = $this->createForm(QuestionnaireType::class, $data, ['definitions' => $definitions]);
|
||||
@@ -69,31 +76,57 @@ class EditController extends AbstractController
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$context = DispositionContext::build($disposition);
|
||||
$final = QuestionnaireType::isFinal($form);
|
||||
$saved = [];
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$response = $responses[$definition->key] ?? null;
|
||||
foreach (QuestionnaireType::answerSets($form->getData(), $definitions) as ['definition' => $definition, 'leg' => $leg, 'answers' => $answers]) {
|
||||
$response = array_find(
|
||||
$responses[$definition->key] ?? [],
|
||||
static fn (QuestionnaireResponse $response): bool => $response->getLeg() === $leg,
|
||||
);
|
||||
|
||||
if (null === $response) {
|
||||
$response = QuestionnaireResponse::forDisposition($definition, $disposition);
|
||||
$response = QuestionnaireResponse::forDisposition($definition, $disposition, $leg);
|
||||
$this->entityManager->persist($response);
|
||||
$responses[$definition->key] = $response;
|
||||
}
|
||||
|
||||
$answers = $form->getData()[$definition->key] ?? [];
|
||||
|
||||
if (true === $final) {
|
||||
$response->submit($definition, $answers, $context, new \DateTimeImmutable());
|
||||
} else {
|
||||
$response->saveDraft($answers, $context);
|
||||
}
|
||||
|
||||
$saved[] = $response;
|
||||
}
|
||||
|
||||
// the draft of a bus leg the teamer no longer says they escorted; never submitted, so
|
||||
// nothing refers to it
|
||||
$dropped = [];
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
foreach ($responses[$definition->key] ?? [] as $response) {
|
||||
if (!in_array($response, $saved, true)) {
|
||||
$this->entityManager->remove($response);
|
||||
$dropped[] = $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
foreach ($saved as $response) {
|
||||
$this->logger->info($final ? 'Submit questionnaire' : 'Save questionnaire draft', [
|
||||
'questionnaire' => $definition->key,
|
||||
'questionnaire_response_id' => $responses[$definition->key]->getId(),
|
||||
'questionnaire' => $response->getQuestionnaire(),
|
||||
'leg' => $response->getLeg()?->value,
|
||||
'questionnaire_response_id' => $response->getId(),
|
||||
'disposition_id' => $disposition->getId(),
|
||||
'teamer_id' => $disposition->getTeamer()->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($dropped as $response) {
|
||||
$this->logger->info('Remove questionnaire draft of a bus leg no longer chosen', [
|
||||
'questionnaire' => $response->getQuestionnaire(),
|
||||
'leg' => $response->getLeg()?->value,
|
||||
'disposition_id' => $disposition->getId(),
|
||||
'teamer_id' => $disposition->getTeamer()->getId(),
|
||||
]);
|
||||
@@ -110,7 +143,7 @@ class EditController extends AbstractController
|
||||
return $this->redirectToRoute('app_teamer_questionnaire_edit', ['uuid' => $disposition->getUuid()]);
|
||||
}
|
||||
|
||||
$drafts = array_filter($responses, static fn (QuestionnaireResponse $response): bool => !$response->isSubmitted());
|
||||
$drafts = array_filter(array_merge(...array_values($responses)), static fn (QuestionnaireResponse $response): bool => !$response->isSubmitted());
|
||||
|
||||
return $this->render('teamer/questionnaire/edit.html.twig', [
|
||||
'disposition' => $disposition,
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Entity;
|
||||
|
||||
use App\Entity\Traits\BlameableEntity;
|
||||
use App\Entity\Traits\TimestampableEntity;
|
||||
use App\Enum\BusLeg;
|
||||
use App\Enum\QuestionnaireResponseStatus;
|
||||
use App\Model\Questionnaire\Definition;
|
||||
use App\Repository\QuestionnaireResponseRepository;
|
||||
@@ -19,6 +20,9 @@ use Symfony\Component\Uid\Uuid;
|
||||
* who, where, when and the escort pickup - so those questions are not asked and the response stays
|
||||
* readable when the assignment is edited later.
|
||||
*
|
||||
* A per-bus-leg questionnaire (Definition::$perBusLeg) has one response per escorted leg, the
|
||||
* others one per disposition.
|
||||
*
|
||||
* Its subject is a Disposition today. A questionnaire about something else gets a nullable foreign
|
||||
* key of its own beside $disposition, not a generic subject column.
|
||||
*
|
||||
@@ -26,7 +30,7 @@ use Symfony\Component\Uid\Uuid;
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: QuestionnaireResponseRepository::class)]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
#[ORM\UniqueConstraint(name: 'questionnaire_disposition', columns: ['questionnaire', 'disposition_id'])]
|
||||
#[ORM\UniqueConstraint(name: 'questionnaire_disposition_leg', columns: ['questionnaire', 'disposition_id', 'leg'])]
|
||||
class QuestionnaireResponse implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
{
|
||||
use BlameableEntity;
|
||||
@@ -68,6 +72,13 @@ class QuestionnaireResponse implements BlameableEntityInterface, TimestampableEn
|
||||
#[ORM\JoinColumn(onDelete: 'CASCADE')]
|
||||
private ?Disposition $disposition = null;
|
||||
|
||||
/**
|
||||
* A BusLeg value, or '' for a questionnaire without legs rather than null: part of the unique
|
||||
* key, and MariaDB lets NULLs repeat in one.
|
||||
*/
|
||||
#[ORM\Column(length: 16, options: ['default' => ''])]
|
||||
private string $leg = '';
|
||||
|
||||
private function __construct(Definition $definition)
|
||||
{
|
||||
$this->uuid = Uuid::v4();
|
||||
@@ -75,10 +86,18 @@ class QuestionnaireResponse implements BlameableEntityInterface, TimestampableEn
|
||||
$this->version = $definition->version;
|
||||
}
|
||||
|
||||
public static function forDisposition(Definition $definition, Disposition $disposition): static
|
||||
/**
|
||||
* @param BusLeg|null $leg required for a per-bus-leg questionnaire, refused for any other
|
||||
*/
|
||||
public static function forDisposition(Definition $definition, Disposition $disposition, ?BusLeg $leg = null): static
|
||||
{
|
||||
if ($definition->perBusLeg !== (null !== $leg)) {
|
||||
throw new \LogicException(sprintf('Questionnaire "%s" %s a bus leg', $definition->key, $definition->perBusLeg ? 'needs' : 'takes no'));
|
||||
}
|
||||
|
||||
$instance = new static($definition);
|
||||
$instance->disposition = $disposition;
|
||||
$instance->leg = $leg->value ?? '';
|
||||
|
||||
return $instance;
|
||||
}
|
||||
@@ -180,6 +199,11 @@ class QuestionnaireResponse implements BlameableEntityInterface, TimestampableEn
|
||||
return $this->disposition;
|
||||
}
|
||||
|
||||
public function getLeg(): ?BusLeg
|
||||
{
|
||||
return BusLeg::tryFrom($this->leg);
|
||||
}
|
||||
|
||||
private function assertDraft(): void
|
||||
{
|
||||
if ($this->isSubmitted()) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
/**
|
||||
* One ride of a bus escort. The bus feedback is given once per leg the teamer escorted: the
|
||||
* assignment only records the outbound stop, so the teamer says which legs they rode.
|
||||
*/
|
||||
enum BusLeg: string
|
||||
{
|
||||
case OUTBOUND = 'outbound';
|
||||
case RETURN = 'return';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::OUTBOUND => 'Hinfahrt',
|
||||
self::RETURN => 'Rückfahrt',
|
||||
};
|
||||
}
|
||||
}
|
||||
+149
-19
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Enum\BusLeg;
|
||||
use App\Enum\ChoiceWidget;
|
||||
use App\Enum\QuestionType;
|
||||
use App\Model\Questionnaire\Definition;
|
||||
@@ -27,6 +28,11 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
* of its own, and each of its sections a child with inherit_data, which groups the fields on screen
|
||||
* without nesting the answers.
|
||||
*
|
||||
* A per-bus-leg questionnaire (bus_feedback) opens with "which rides did you escort?" (LEGS), followed
|
||||
* by one child per BusLeg holding that leg's answers, shown only while the leg is chosen:
|
||||
* {legs: 'outbound'|'return'|'both'|null, outbound: {...}, return: {...}}. answerSets() turns the
|
||||
* data into one answer set per questionnaire and chosen leg, the responses to store.
|
||||
*
|
||||
* Two buttons: "draft" saves whatever is filled in, "submit" also demands every required question
|
||||
* that is shown. No field is HTML-required, or the browser would refuse to send a half-filled
|
||||
* draft. Whether a question is shown is Definition::isVisible(), the same rule the
|
||||
@@ -36,6 +42,8 @@ class QuestionnaireType extends AbstractType
|
||||
{
|
||||
public const string DRAFT = 'draft';
|
||||
public const string SUBMIT = 'submit';
|
||||
public const string LEGS = 'legs';
|
||||
public const string BOTH_LEGS = 'both';
|
||||
|
||||
private const int RATING_MAX = 5;
|
||||
|
||||
@@ -49,21 +57,14 @@ class QuestionnaireType extends AbstractType
|
||||
'label' => $definition->title,
|
||||
'help' => $definition->description,
|
||||
'empty_data' => [],
|
||||
'required' => false,
|
||||
'block_prefix' => 'questionnaire_part',
|
||||
]);
|
||||
|
||||
foreach ($definition->sections as $section) {
|
||||
$sectionBuilder = $builder->create($section->key, FormType::class, [
|
||||
'inherit_data' => true,
|
||||
'label' => $section->title ?? false,
|
||||
'block_prefix' => 'questionnaire_section',
|
||||
]);
|
||||
|
||||
foreach ($section->questions as $question) {
|
||||
$sectionBuilder->add($question->key, ...$this->field($definition, $question));
|
||||
}
|
||||
|
||||
$questionnaireBuilder->add($sectionBuilder);
|
||||
if ($definition->perBusLeg) {
|
||||
$this->addLegs($builder, $questionnaireBuilder, $definition);
|
||||
} else {
|
||||
$this->addSections($builder, $questionnaireBuilder, $definition, $definition->key);
|
||||
}
|
||||
|
||||
$builder->add($questionnaireBuilder);
|
||||
@@ -83,8 +84,20 @@ class QuestionnaireType extends AbstractType
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $form->getData();
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$answers = $form->getData()[$definition->key] ?? [];
|
||||
if ($definition->perBusLeg && [] === self::chosenLegs($data[$definition->key] ?? [])) {
|
||||
$form->get($definition->key)->get(self::LEGS)->addError(new FormError('Bitte beantworte diese Frage.'));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (self::answerSets($data, $definitions) as ['definition' => $definition, 'leg' => $leg, 'answers' => $answers]) {
|
||||
$parent = $form->get($definition->key);
|
||||
|
||||
if (null !== $leg) {
|
||||
$parent = $parent->get($leg->value);
|
||||
}
|
||||
|
||||
foreach ($definition->sections as $section) {
|
||||
foreach ($section->questions as $question) {
|
||||
@@ -92,7 +105,7 @@ class QuestionnaireType extends AbstractType
|
||||
&& $definition->isVisible($question, $answers)
|
||||
&& $this->isEmpty($answers[$question->key] ?? null)
|
||||
) {
|
||||
$form->get($definition->key)->get($section->key)->get($question->key)->addError(
|
||||
$parent->get($section->key)->get($question->key)->addError(
|
||||
new FormError('Bitte beantworte diese Frage.')
|
||||
);
|
||||
}
|
||||
@@ -128,20 +141,137 @@ class QuestionnaireType extends AbstractType
|
||||
return $button instanceof ClickableInterface && $button->isClicked();
|
||||
}
|
||||
|
||||
/**
|
||||
* The answers to store, one set per questionnaire - and for a per-bus-leg one, per chosen leg.
|
||||
* A leg not chosen yields nothing, whatever its fields still hold.
|
||||
*
|
||||
* @param array<string, mixed> $data the form's data, keyed by questionnaire key
|
||||
* @param list<Definition> $definitions
|
||||
*
|
||||
* @return list<array{definition: Definition, leg: BusLeg|null, answers: array<string, mixed>}>
|
||||
*/
|
||||
public static function answerSets(array $data, array $definitions): array
|
||||
{
|
||||
$sets = [];
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$part = $data[$definition->key] ?? [];
|
||||
|
||||
if (!$definition->perBusLeg) {
|
||||
$sets[] = ['definition' => $definition, 'leg' => null, 'answers' => $part];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (self::chosenLegs($part) as $leg) {
|
||||
$sets[] = ['definition' => $definition, 'leg' => $leg, 'answers' => $part[$leg->value] ?? []];
|
||||
}
|
||||
}
|
||||
|
||||
return $sets;
|
||||
}
|
||||
|
||||
/**
|
||||
* The form data of a per-bus-leg questionnaire from its stored answers, one set per leg. The
|
||||
* legs that have answers are the ones chosen.
|
||||
*
|
||||
* @param array<string, array<string, mixed>> $answersByLeg keyed by BusLeg value
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function legData(array $answersByLeg): array
|
||||
{
|
||||
$legs = array_keys($answersByLeg);
|
||||
|
||||
return [self::LEGS => match (count($legs)) {
|
||||
0 => null,
|
||||
1 => $legs[0],
|
||||
default => self::BOTH_LEGS,
|
||||
}] + $answersByLeg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $part the data of a per-bus-leg questionnaire
|
||||
*
|
||||
* @return list<BusLeg>
|
||||
*/
|
||||
private static function chosenLegs(array $part): array
|
||||
{
|
||||
$legs = $part[self::LEGS] ?? null;
|
||||
|
||||
return self::BOTH_LEGS === $legs ? BusLeg::cases() : array_values(array_filter([BusLeg::tryFrom((string) $legs)]));
|
||||
}
|
||||
|
||||
private function addLegs(FormBuilderInterface $builder, FormBuilderInterface $questionnaireBuilder, Definition $definition): void
|
||||
{
|
||||
$questionnaireBuilder->add(self::LEGS, ChoiceType::class, [
|
||||
'label' => 'Welche Fahrten hast du begleitet?',
|
||||
'required' => false,
|
||||
'choices' => [...array_map(static fn (BusLeg $leg): string => $leg->value, BusLeg::cases()), self::BOTH_LEGS],
|
||||
'choice_label' => static fn (string $legs): string => BusLeg::tryFrom($legs)?->label() ?? 'Hin- und Rückfahrt',
|
||||
'expanded' => true,
|
||||
'placeholder' => false,
|
||||
'block_prefix' => 'questionnaire_choice',
|
||||
'row_attr' => ['data-question-key' => $definition->key.'.'.self::LEGS],
|
||||
]);
|
||||
|
||||
foreach (BusLeg::cases() as $leg) {
|
||||
$legBuilder = $builder->create($leg->value, FormType::class, [
|
||||
'label' => $leg->label(),
|
||||
'empty_data' => [],
|
||||
'required' => false,
|
||||
'block_prefix' => 'questionnaire_leg',
|
||||
// shown by the questionnaire Stimulus controller like a conditional question
|
||||
'row_attr' => [
|
||||
'data-question-key' => $definition->key.'.'.$leg->value,
|
||||
'data-show-if' => json_encode([
|
||||
'question' => $definition->key.'.'.self::LEGS,
|
||||
'operator' => 'in',
|
||||
'value' => [$leg->value, self::BOTH_LEGS],
|
||||
], JSON_THROW_ON_ERROR),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->addSections($builder, $legBuilder, $definition, $definition->key.'.'.$leg->value);
|
||||
$questionnaireBuilder->add($legBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix makes the question keys the Stimulus controller sees unique on the page
|
||||
*/
|
||||
private function addSections(FormBuilderInterface $builder, FormBuilderInterface $parent, Definition $definition, string $prefix): void
|
||||
{
|
||||
foreach ($definition->sections as $section) {
|
||||
$sectionBuilder = $builder->create($section->key, FormType::class, [
|
||||
'inherit_data' => true,
|
||||
'required' => false,
|
||||
'label' => $section->title ?? false,
|
||||
'block_prefix' => 'questionnaire_section',
|
||||
]);
|
||||
|
||||
foreach ($section->questions as $question) {
|
||||
$sectionBuilder->add($question->key, ...$this->field($prefix, $question));
|
||||
}
|
||||
|
||||
$parent->add($sectionBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: class-string, 1: array<string, mixed>}
|
||||
*/
|
||||
private function field(Definition $definition, Question $question): array
|
||||
private function field(string $prefix, Question $question): array
|
||||
{
|
||||
$common = [
|
||||
'label' => $question->label,
|
||||
'help' => $question->help,
|
||||
'required' => false,
|
||||
// read by assets/controllers/questionnaire_controller.js; prefixed with the questionnaire,
|
||||
// since question keys are only unique within one
|
||||
'row_attr' => ['data-question-key' => $definition->key.'.'.$question->key] + (null === $question->showIf ? [] : [
|
||||
// read by assets/controllers/questionnaire_controller.js; prefixed with the questionnaire
|
||||
// (and leg), since question keys are only unique within one
|
||||
'row_attr' => ['data-question-key' => $prefix.'.'.$question->key] + (null === $question->showIf ? [] : [
|
||||
'data-show-if' => json_encode([
|
||||
'question' => $definition->key.'.'.$question->showIf->question,
|
||||
'question' => $prefix.'.'.$question->showIf->question,
|
||||
'operator' => $question->showIf->operator->value,
|
||||
'value' => $question->showIf->value,
|
||||
], JSON_THROW_ON_ERROR),
|
||||
|
||||
@@ -5,6 +5,9 @@ namespace App\Model\Questionnaire;
|
||||
/**
|
||||
* One version of a questionnaire as read from config/questionnaires/. Responses store its key and
|
||||
* version, and their answers are keyed by Question::$key.
|
||||
*
|
||||
* A $perBusLeg questionnaire is answered once per bus ride the teamer escorted (App\Enum\BusLeg),
|
||||
* each leg a response of its own.
|
||||
*/
|
||||
readonly class Definition
|
||||
{
|
||||
@@ -17,6 +20,7 @@ readonly class Definition
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public array $sections,
|
||||
public bool $perBusLeg = false,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -25,13 +25,16 @@ class DispositionVoter extends Voter
|
||||
public const MANAGE_RECEIPTS = 'MANAGE_RECEIPTS';
|
||||
public const QUESTIONNAIRE = 'QUESTIONNAIRE';
|
||||
|
||||
private readonly \DateTimeImmutable $questionnaireGoLive;
|
||||
|
||||
/**
|
||||
* @param int $questionnaireWindowDays the services.yaml parameter questionnaire_window_days
|
||||
* @param string $questionnaireGoLive Y-m-d, the services.yaml parameter questionnaire_go_live
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly Security $security,
|
||||
private readonly int $questionnaireWindowDays,
|
||||
string $questionnaireGoLive,
|
||||
) {
|
||||
$this->questionnaireGoLive = QuestionnaireCatalog::parseGoLive($questionnaireGoLive);
|
||||
}
|
||||
|
||||
protected function supports(string $attribute, mixed $subject): bool
|
||||
@@ -143,7 +146,7 @@ class DispositionVoter extends Voter
|
||||
$dateTo = $disposition->getAssignment()?->getDestination()?->getDateTo();
|
||||
|
||||
return null !== $dateTo
|
||||
&& $dateTo >= QuestionnaireCatalog::windowStart($this->questionnaireWindowDays)
|
||||
&& $dateTo >= $this->questionnaireGoLive
|
||||
&& $dateTo <= new \DateTimeImmutable('today');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Cron;
|
||||
|
||||
use App\Config\EmailTextKey;
|
||||
use App\Config\QuestionnaireCatalog;
|
||||
use App\Email\Mailer;
|
||||
use App\Entity\Disposition;
|
||||
use App\Repository\DispositionRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Reminds teamers of their unsubmitted questionnaires, every day from the day after the trip ended
|
||||
* until they are submitted - a saved draft does not count. One mail per teamer, listing every such
|
||||
* placement.
|
||||
*
|
||||
* Like the other reminders it records nothing: the cron runs once a day, and that is what makes it
|
||||
* daily.
|
||||
*/
|
||||
class QuestionnaireReminderService
|
||||
{
|
||||
private readonly \DateTimeImmutable $questionnaireGoLive;
|
||||
|
||||
/**
|
||||
* @param string $questionnaireGoLive Y-m-d, the services.yaml parameter questionnaire_go_live
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly DispositionRepository $dispositionRepository,
|
||||
private readonly Mailer $mailer,
|
||||
private readonly LoggerInterface $logger,
|
||||
string $questionnaireGoLive,
|
||||
) {
|
||||
$this->questionnaireGoLive = QuestionnaireCatalog::parseGoLive($questionnaireGoLive);
|
||||
}
|
||||
|
||||
public function sendReminders(?\DateTimeImmutable $today = null): string
|
||||
{
|
||||
$today = ($today ?? new \DateTimeImmutable('today'))->setTime(0, 0);
|
||||
|
||||
/** @var array<int, list<Disposition>> $perTeamer keyed by object id: Doctrine hands out one Teamer instance per row */
|
||||
$perTeamer = [];
|
||||
|
||||
foreach ($this->dispositionRepository->findQuestionnaireReminders($this->questionnaireGoLive, $today) as $disposition) {
|
||||
$perTeamer[spl_object_id($disposition->getTeamer())][] = $disposition;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($perTeamer as $dispositions) {
|
||||
$email = $dispositions[0]->getTeamer()->getCommunication()?->getEmail();
|
||||
|
||||
if (null === $email) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->mailer->createAndSendText(EmailTextKey::REMINDER_QUESTIONNAIRE, [
|
||||
'destinations' => implode(', ', array_map(
|
||||
static fn (Disposition $disposition): string => (string) $disposition->getAssignment()->getDestination(),
|
||||
$dispositions,
|
||||
)),
|
||||
], [
|
||||
'to' => $email,
|
||||
]);
|
||||
|
||||
++$count;
|
||||
}
|
||||
|
||||
$message = 'Sent '.$count.' questionnaire reminders to teamers';
|
||||
$this->logger->info($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user