Search in sources :

Example 1 with MultiOptionQuestionDefinition

use of services.question.types.MultiOptionQuestionDefinition in project civiform by seattle-uat.

the class QuestionTest method canSerializeAndDeserializeMultiOptionQuestion.

@Test
public void canSerializeAndDeserializeMultiOptionQuestion() throws UnsupportedQuestionTypeException {
    QuestionDefinition definition = new QuestionDefinitionBuilder().setQuestionType(QuestionType.DROPDOWN).setName("").setDescription("").setEnumeratorId(Optional.of(123L)).setQuestionText(LocalizedStrings.of()).setQuestionHelpText(LocalizedStrings.empty()).setQuestionOptions(ImmutableList.of(QuestionOption.create(1L, LocalizedStrings.of(Locale.US, "option")))).build();
    Question question = new Question(definition);
    question.save();
    Question found = repo.lookupQuestion(question.id).toCompletableFuture().join().get();
    assertThat(found.getQuestionDefinition().getQuestionType().isMultiOptionType()).isTrue();
    MultiOptionQuestionDefinition multiOption = (MultiOptionQuestionDefinition) found.getQuestionDefinition();
    assertThat(multiOption.getOptions()).isEqualTo(ImmutableList.of(QuestionOption.create(1L, LocalizedStrings.of(Locale.US, "option"))));
    assertThat(multiOption.getEnumeratorId()).hasValue(123L);
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) TextQuestionDefinition(services.question.types.TextQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) Test(org.junit.Test)

Example 2 with MultiOptionQuestionDefinition

use of services.question.types.MultiOptionQuestionDefinition in project civiform by seattle-uat.

the class ProgramBlockPredicatesEditView method createValueField.

private ContainerTag createValueField(QuestionDefinition questionDefinition) {
    if (questionDefinition.getQuestionType().isMultiOptionType()) {
        // If it's a multi-option question, we need to provide a discrete list of possible values to
        // choose from instead of a freeform text field. Not only is it a better UX, but we store the
        // ID of the options rather than the display strings since the option display strings are
        // localized.
        ImmutableList<QuestionOption> options = ((MultiOptionQuestionDefinition) questionDefinition).getOptions();
        ContainerTag valueOptionsDiv = div().with(div("Values").withClasses(BaseStyles.CHECKBOX_GROUP_LABEL));
        for (QuestionOption option : options) {
            ContainerTag optionCheckbox = FieldWithLabel.checkbox().setFieldName("predicateValues[]").setValue(String.valueOf(option.id())).setLabelText(option.optionText().getDefault()).getContainer();
            valueOptionsDiv.with(optionCheckbox);
        }
        return valueOptionsDiv;
    } else {
        return div().with(FieldWithLabel.input().setFieldName("predicateValue").setLabelText("Value").addReferenceClass(ReferenceClasses.PREDICATE_VALUE_INPUT).getContainer()).with(div().withClasses(ReferenceClasses.PREDICATE_VALUE_COMMA_HELP_TEXT, Styles.HIDDEN, Styles.TEXT_XS, BaseStyles.FORM_LABEL_TEXT_COLOR).withText("Enter a list of comma-separated values. For example, \"v1,v2,v3\"."));
    }
}
Also used : QuestionOption(services.question.QuestionOption) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) ContainerTag(j2html.tags.ContainerTag)

Example 3 with MultiOptionQuestionDefinition

use of services.question.types.MultiOptionQuestionDefinition in project civiform by seattle-uat.

the class Question method setFieldsFromQuestionDefinition.

private void setFieldsFromQuestionDefinition(QuestionDefinition questionDefinition) {
    if (questionDefinition.isPersisted()) {
        id = questionDefinition.getId();
    }
    enumeratorId = questionDefinition.getEnumeratorId().orElse(null);
    name = questionDefinition.getName();
    description = questionDefinition.getDescription();
    questionText = questionDefinition.getQuestionText();
    questionHelpText = questionDefinition.getQuestionHelpText();
    questionType = questionDefinition.getQuestionType().toString();
    validationPredicates = questionDefinition.getValidationPredicatesAsString();
    if (questionDefinition.getQuestionType().isMultiOptionType()) {
        MultiOptionQuestionDefinition multiOption = (MultiOptionQuestionDefinition) questionDefinition;
        questionOptions = multiOption.getOptions();
    }
    if (questionDefinition.getQuestionType().equals(QuestionType.ENUMERATOR)) {
        EnumeratorQuestionDefinition enumerator = (EnumeratorQuestionDefinition) questionDefinition;
        enumeratorEntityType = enumerator.getEntityType();
    }
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition)

Example 4 with MultiOptionQuestionDefinition

use of services.question.types.MultiOptionQuestionDefinition in project civiform by seattle-uat.

the class MultiSelectQuestion method getQuestionErrors.

@Override
public ImmutableSet<ValidationErrorMessage> getQuestionErrors() {
    if (!isAnswered()) {
        return ImmutableSet.of();
    }
    MultiOptionQuestionDefinition definition = getQuestionDefinition();
    int numberOfSelections = getSelectedOptionsValue().map(ImmutableList::size).orElse(0);
    ImmutableSet.Builder<ValidationErrorMessage> errors = ImmutableSet.builder();
    if (definition.getMultiOptionValidationPredicates().minChoicesRequired().isPresent()) {
        int minChoicesRequired = definition.getMultiOptionValidationPredicates().minChoicesRequired().getAsInt();
        if (numberOfSelections < minChoicesRequired) {
            errors.add(ValidationErrorMessage.create(MessageKey.MULTI_SELECT_VALIDATION_TOO_FEW, minChoicesRequired));
        }
    }
    if (definition.getMultiOptionValidationPredicates().maxChoicesAllowed().isPresent()) {
        int maxChoicesAllowed = definition.getMultiOptionValidationPredicates().maxChoicesAllowed().getAsInt();
        if (numberOfSelections > maxChoicesAllowed) {
            errors.add(ValidationErrorMessage.create(MessageKey.MULTI_SELECT_VALIDATION_TOO_MANY, maxChoicesAllowed));
        }
    }
    return errors.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) ValidationErrorMessage(services.applicant.ValidationErrorMessage)

Example 5 with MultiOptionQuestionDefinition

use of services.question.types.MultiOptionQuestionDefinition in project civiform by seattle-uat.

the class AdminQuestionControllerTest method update_mergesTranslations.

@Test
public void update_mergesTranslations() {
    QuestionDefinition definition = new DropdownQuestionDefinition("applicant ice cream", Optional.empty(), "Select your favorite ice cream flavor", LocalizedStrings.of(Locale.US, "Ice cream?", Locale.FRENCH, "crème glacée?"), LocalizedStrings.of(Locale.US, "help", Locale.FRENCH, "aider"), ImmutableList.of(QuestionOption.create(1L, LocalizedStrings.of(Locale.US, "chocolate", Locale.FRENCH, "chocolat")), QuestionOption.create(2L, LocalizedStrings.of(Locale.US, "strawberry", Locale.FRENCH, "fraise")), QuestionOption.create(3L, LocalizedStrings.of(Locale.US, "vanilla", Locale.FRENCH, "vanille")), QuestionOption.create(4L, LocalizedStrings.of(Locale.US, "coffee", Locale.FRENCH, "café"))));
    // We can only update draft questions, so save this in the DRAFT version.
    Question question = testQuestionBank.maybeSave(definition, LifecycleStage.DRAFT);
    ImmutableMap<String, String> formData = ImmutableMap.<String, String>builder().put("questionName", definition.getName()).put("questionDescription", definition.getDescription()).put("questionType", definition.getQuestionType().name()).put("questionText", "new question text").put("questionHelpText", "new help text").put("options[0]", // Unchanged but out of order
    "coffee").put("options[1]", // Unchanged and in order
    "vanilla").put("newOptions[0]", // New flavor
    "lavender").put("optionIds[0]", "4").put("optionIds[1]", "3").put("nextAvailableId", "5").put("questionExportState", "NON_DEMOGRAPHIC").build();
    RequestBuilder requestBuilder = addCSRFToken(Helpers.fakeRequest().bodyForm(formData));
    Result result = controller.update(requestBuilder.build(), question.id, definition.getQuestionType().toString());
    assertThat(result.status()).isEqualTo(SEE_OTHER);
    Question found = questionRepo.lookupQuestion(question.id).toCompletableFuture().join().get();
    assertThat(found.getQuestionDefinition().getQuestionText().translations()).containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(Locale.US, "new question text", Locale.FRENCH, "crème glacée?"));
    assertThat(found.getQuestionDefinition().getQuestionHelpText().translations()).containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(Locale.US, "new help text", Locale.FRENCH, "aider"));
    ImmutableList<QuestionOption> expectedOptions = ImmutableList.of(QuestionOption.create(4, 0, LocalizedStrings.of(Locale.US, "coffee", Locale.FRENCH, "café")), QuestionOption.create(3, 1, LocalizedStrings.of(Locale.US, "vanilla", Locale.FRENCH, "vanille")), QuestionOption.create(5, 2, LocalizedStrings.withDefaultValue("lavender")));
    assertThat(((MultiOptionQuestionDefinition) found.getQuestionDefinition()).getOptions()).isEqualTo(expectedOptions);
}
Also used : MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) DropdownQuestionDefinition(services.question.types.DropdownQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) DropdownQuestionDefinition(services.question.types.DropdownQuestionDefinition) QuestionOption(services.question.QuestionOption) RequestBuilder(play.mvc.Http.RequestBuilder) Question(models.Question) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) Helpers.contentAsString(play.test.Helpers.contentAsString) Result(play.mvc.Result) Test(org.junit.Test)

Aggregations

MultiOptionQuestionDefinition (services.question.types.MultiOptionQuestionDefinition)8 QuestionOption (services.question.QuestionOption)4 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)3 QuestionDefinition (services.question.types.QuestionDefinition)3 QuestionDefinitionBuilder (services.question.types.QuestionDefinitionBuilder)3 ImmutableList (com.google.common.collect.ImmutableList)2 MultiOptionQuestionForm (forms.MultiOptionQuestionForm)2 Test (org.junit.Test)2 Result (play.mvc.Result)2 LocalizedStrings (services.LocalizedStrings)2 UnsupportedQuestionTypeException (services.question.exceptions.UnsupportedQuestionTypeException)2 Authorizers (auth.Authorizers)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CiviFormController (controllers.CiviFormController)1 EnumeratorQuestionForm (forms.EnumeratorQuestionForm)1 QuestionForm (forms.QuestionForm)1 QuestionFormBuilder (forms.QuestionFormBuilder)1