Initial survey bundle import
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\Controller\FrontendModule;
|
||||
|
||||
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
|
||||
use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule;
|
||||
use Contao\CoreBundle\Twig\FragmentTemplate;
|
||||
use Contao\ModuleModel;
|
||||
use Mummert\SurveyBundle\Form\Model\SurveyAnswerData;
|
||||
use Mummert\SurveyBundle\Form\SurveyQuestionAnswerType;
|
||||
use Mummert\SurveyBundle\Repository\SurveyRepository;
|
||||
use Mummert\SurveyBundle\Service\SurveyFlowService;
|
||||
use Mummert\SurveyBundle\Service\SurveySubmissionService;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
#[AsFrontendModule(type: 'show_survey', category: 'survey', template: 'frontend/show_survey')]
|
||||
final class ShowSurveyController extends AbstractFrontendModuleController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SurveyRepository $surveyRepository,
|
||||
private readonly SurveyFlowService $surveyFlowService,
|
||||
private readonly SurveySubmissionService $surveySubmissionService,
|
||||
) {
|
||||
}
|
||||
|
||||
protected function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
|
||||
{
|
||||
$publicAlias = $this->resolvePublicAlias($request);
|
||||
|
||||
if ('' === $publicAlias) {
|
||||
$template->set('errorMessage', 'Es wurde kein oeffentlicher Link auf der Anzeigeseite gefunden.');
|
||||
$template->set('question', null);
|
||||
$template->set('surveyForm', null);
|
||||
$template->set('isComplete', false);
|
||||
$template->set('answers', []);
|
||||
$template->set('survey', null);
|
||||
$template->set('progress', ['current' => 0, 'total' => 0, 'percentage' => 0]);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
$survey = $this->surveyRepository->findByPublicAlias($publicAlias, true);
|
||||
|
||||
if (null === $survey) {
|
||||
$template->set('errorMessage', 'Die Umfrage wurde nicht gefunden oder ist nicht veroeffentlicht.');
|
||||
$template->set('question', null);
|
||||
$template->set('surveyForm', null);
|
||||
$template->set('isComplete', false);
|
||||
$template->set('answers', []);
|
||||
$template->set('survey', null);
|
||||
$template->set('progress', ['current' => 0, 'total' => 0, 'percentage' => 0]);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
if ($htmlHeadBag = $this->getHtmlHeadBag()) {
|
||||
$htmlHeadBag->setTitle((string) $survey->title);
|
||||
}
|
||||
|
||||
$submission = $this->surveySubmissionService->resolveActiveSubmission($survey, $request->getSession(), null);
|
||||
$question = $this->surveyFlowService->resolveCurrentQuestion($survey, $submission);
|
||||
|
||||
if (null === $question && '1' !== (string) $submission->isFinished) {
|
||||
$template->set('errorMessage', 'Diese Umfrage ist noch nicht vollstaendig eingerichtet. Es fehlt eine gueltige erste Frage oder eine verknuepfte Folgefrage ist nicht mehr verfuegbar.');
|
||||
$template->set('question', null);
|
||||
$template->set('surveyForm', null);
|
||||
$template->set('isComplete', false);
|
||||
$template->set('answers', []);
|
||||
$template->set('survey', $survey);
|
||||
$template->set('progress', ['current' => 0, 'total' => 0, 'percentage' => 0]);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
if (null === $question) {
|
||||
$template->set('errorMessage', null);
|
||||
$template->set('survey', $survey);
|
||||
$template->set('progress', $this->surveyFlowService->getProgress($survey, null));
|
||||
$template->set('question', null);
|
||||
$template->set('surveyForm', null);
|
||||
$template->set('isComplete', true);
|
||||
$template->set('answers', $this->surveySubmissionService->getAnswersForSubmission($submission));
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
$answerData = new SurveyAnswerData();
|
||||
$form = $this->createForm(SurveyQuestionAnswerType::class, $answerData, ['question' => $question]);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$normalizedAnswer = $this->surveySubmissionService->storeAnswer($survey, $submission, $question, $answerData->answer);
|
||||
$nextQuestion = $this->surveyFlowService->determineNextQuestion($survey, $question, $normalizedAnswer);
|
||||
$this->surveySubmissionService->advance($submission, $nextQuestion);
|
||||
|
||||
return new RedirectResponse($request->getUri());
|
||||
}
|
||||
|
||||
$template->set('errorMessage', null);
|
||||
$template->set('survey', $survey);
|
||||
$template->set('progress', $this->surveyFlowService->getProgress($survey, $question));
|
||||
$template->set('question', $question);
|
||||
$template->set('surveyForm', $form->createView());
|
||||
$template->set('isComplete', false);
|
||||
$template->set('answers', []);
|
||||
|
||||
return $template->getResponse();
|
||||
}
|
||||
|
||||
private function resolvePublicAlias(Request $request): string
|
||||
{
|
||||
$candidate = $request->attributes->get('auto_item');
|
||||
|
||||
if (\is_string($candidate) && '' !== trim($candidate)) {
|
||||
return trim($candidate);
|
||||
}
|
||||
|
||||
$queryValue = (string) $request->query->get('auto_item', '');
|
||||
|
||||
return trim($queryValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user