Refine survey editor flow and auth styling
This commit is contained in:
@@ -41,7 +41,17 @@ final class SurveyEditorService
|
||||
$this->surveyRepository->update($survey, $memberId, $data->toArray());
|
||||
}
|
||||
|
||||
public function saveQuestion(SurveyModel $survey, SurveyQuestionData $data, int $questionId = 0): void
|
||||
public function toggleSurveyPublished(SurveyModel $survey, int $memberId): bool
|
||||
{
|
||||
$data = SurveyEditorData::fromModel($survey);
|
||||
$data->published = !$data->published;
|
||||
|
||||
$this->surveyRepository->update($survey, $memberId, $data->toArray());
|
||||
|
||||
return $data->published;
|
||||
}
|
||||
|
||||
public function saveQuestion(SurveyModel $survey, SurveyQuestionData $data, int $questionId = 0): int
|
||||
{
|
||||
$this->assertUnlocked($survey);
|
||||
|
||||
@@ -55,11 +65,13 @@ final class SurveyEditorService
|
||||
$this->surveyQuestionRepository->update($question, $data->toArray());
|
||||
$this->surveyRepository->touch((int) $survey->id);
|
||||
|
||||
return;
|
||||
return (int) $question->id;
|
||||
}
|
||||
|
||||
$this->surveyQuestionRepository->create((int) $survey->id, $data->toArray());
|
||||
$question = $this->surveyQuestionRepository->create((int) $survey->id, $data->toArray());
|
||||
$this->surveyRepository->touch((int) $survey->id);
|
||||
|
||||
return (int) $question->id;
|
||||
}
|
||||
|
||||
public function deleteQuestion(SurveyModel $survey, int $questionId): void
|
||||
@@ -70,6 +82,11 @@ final class SurveyEditorService
|
||||
$this->surveyRepository->touch((int) $survey->id);
|
||||
}
|
||||
|
||||
public function deleteSurvey(SurveyModel $survey): void
|
||||
{
|
||||
$this->surveyRepository->deleteCascade((int) $survey->id);
|
||||
}
|
||||
|
||||
public function saveCondition(SurveyModel $survey, SurveyConditionData $data, int $conditionId = 0): void
|
||||
{
|
||||
$this->assertUnlocked($survey);
|
||||
|
||||
@@ -12,6 +12,7 @@ use Mummert\SurveyBundle\Repository\SurveyAnswerRepository;
|
||||
use Mummert\SurveyBundle\Repository\SurveyRepository;
|
||||
use Mummert\SurveyBundle\Repository\SurveySubmissionRepository;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Uid\Ulid;
|
||||
|
||||
final class SurveySubmissionService
|
||||
@@ -21,6 +22,7 @@ final class SurveySubmissionService
|
||||
private readonly SurveyAnswerRepository $surveyAnswerRepository,
|
||||
private readonly SurveyRepository $surveyRepository,
|
||||
private readonly QuestionTypeRegistry $questionTypeRegistry,
|
||||
private readonly TranslatorInterface $translator,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -80,7 +82,9 @@ final class SurveySubmissionService
|
||||
*/
|
||||
public function getAnswersForSubmission(SurveySubmissionModel $submission): array
|
||||
{
|
||||
return $this->surveyAnswerRepository->findAnswersBySubmission((int) $submission->id);
|
||||
$answers = $this->surveyAnswerRepository->findAnswersBySubmission((int) $submission->id);
|
||||
|
||||
return array_map(fn (array $answer): array => $this->formatSubmissionAnswer($answer), $answers);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,23 +95,48 @@ final class SurveySubmissionService
|
||||
return $this->surveySubmissionRepository->findOverviewBySurvey((int) $survey->id);
|
||||
}
|
||||
|
||||
public function hasFinishedSubmissionForSurvey(int $surveyId, SessionInterface $session): bool
|
||||
{
|
||||
if ($surveyId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$token = (string) $session->get($this->buildSessionKey($surveyId), '');
|
||||
|
||||
if ('' === $token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->surveySubmissionRepository->hasFinishedBySurveyAndToken($surveyId, $token);
|
||||
}
|
||||
|
||||
private function buildSessionKey(int $surveyId): string
|
||||
{
|
||||
return 'mummert_survey_submission_'.$surveyId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $answer
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function formatSubmissionAnswer(array $answer): array
|
||||
{
|
||||
$questionType = (string) ($answer['questionType'] ?? 'text');
|
||||
$value = trim((string) ($answer['value'] ?? ''));
|
||||
|
||||
$answer['questionType'] = $this->translateQuestionType($questionType);
|
||||
$answer['value'] = $this->translateAnswerValue($questionType, $value);
|
||||
|
||||
return $answer;
|
||||
}
|
||||
|
||||
private function translateQuestionType(string $questionType): string
|
||||
{
|
||||
return $this->translator->trans('survey.survey.question_type_'.$questionType, [], 'messages');
|
||||
}
|
||||
|
||||
private function translateAnswerValue(string $questionType, string $value): string
|
||||
{
|
||||
if ('' === $value) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ('yes_no_maybe' === $questionType) {
|
||||
return $this->translator->trans('survey.survey.answer_'.$value, [], 'messages');
|
||||
}
|
||||
|
||||
if ('choice' === $questionType) {
|
||||
$parts = array_values(array_filter(array_map('trim', explode(' | ', $value)), static fn (string $entry): bool => '' !== $entry));
|
||||
|
||||
return implode(' | ', $parts);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user