Search in sources :

Example 1 with QuestionType

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

the class QuestionEditView method buildQuestionForm.

private ContainerTag buildQuestionForm(QuestionForm questionForm, SelectWithLabel enumeratorOptions, boolean submittable, boolean forCreate) {
    QuestionType questionType = questionForm.getQuestionType();
    ContainerTag formTag = form().withMethod("POST");
    // The question enumerator and name fields should not be changed after the question is created.
    // If this form is not for creation, the fields are disabled, and hidden fields to pass
    // enumerator
    // and name data are added.
    formTag.with(enumeratorOptions.setDisabled(!forCreate).getContainer());
    formTag.with(repeatedQuestionInformation());
    FieldWithLabel nameField = FieldWithLabel.input().setId("question-name-input").setFieldName(QUESTION_NAME_FIELD).setLabelText("Name").setDisabled(!submittable).setPlaceholderText("The name displayed in the question builder").setValue(questionForm.getQuestionName());
    formTag.with(nameField.setDisabled(!forCreate).getContainer());
    if (!forCreate) {
        formTag.with(input().isHidden().withName(QUESTION_NAME_FIELD).withValue(questionForm.getQuestionName()), input().isHidden().withName(QUESTION_ENUMERATOR_FIELD).withValue(questionForm.getEnumeratorId().map(String::valueOf).orElse(NO_ENUMERATOR_ID_STRING)));
    }
    ContainerTag questionHelpTextField = FieldWithLabel.textArea().setId("question-help-text-textarea").setFieldName("questionHelpText").setLabelText("Question help text").setPlaceholderText("The question help text displayed to the applicant").setDisabled(!submittable).setValue(questionForm.getQuestionHelpText()).getContainer();
    if (questionType.equals(QuestionType.STATIC)) {
        // Hide help text for static questions.
        questionHelpTextField.withClasses(Styles.HIDDEN);
    }
    formTag.with(FieldWithLabel.textArea().setId("question-description-textarea").setFieldName("questionDescription").setLabelText("Description").setPlaceholderText("The description displayed in the question builder").setDisabled(!submittable).setValue(questionForm.getQuestionDescription()).getContainer(), FieldWithLabel.textArea().setId("question-text-textarea").setFieldName("questionText").setLabelText("Question text").setPlaceholderText("The question text displayed to the applicant").setDisabled(!submittable).setValue(questionForm.getQuestionText()).getContainer(), questionHelpTextField).with(formQuestionTypeSelect(questionType));
    formTag.with(QuestionConfig.buildQuestionConfig(questionForm, messages));
    if (!ExporterService.NON_EXPORTED_QUESTION_TYPES.contains(questionType)) {
        formTag.with(div().withId("demographic-field-content").with(buildDemographicFields(questionForm, submittable)));
    }
    return formTag;
}
Also used : QuestionType(services.question.types.QuestionType) ContainerTag(j2html.tags.ContainerTag) FieldWithLabel(views.components.FieldWithLabel)

Example 2 with QuestionType

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

the class QuestionEditView method renderNewQuestionForm.

private Content renderNewQuestionForm(Request request, QuestionForm questionForm, ImmutableList<EnumeratorQuestionDefinition> enumeratorQuestionDefinitions, Optional<String> message) {
    QuestionType questionType = questionForm.getQuestionType();
    String title = String.format("New %s question", questionType.toString().toLowerCase());
    ContainerTag formContent = buildQuestionContainer(title, questionForm).with(buildNewQuestionForm(questionForm, enumeratorQuestionDefinitions).with(makeCsrfTokenInputTag(request)));
    if (message.isPresent()) {
        formContent.with(ToastMessage.error(message.get()).setDismissible(false).getContainerTag());
    }
    return renderWithPreview(formContent, questionType, title);
}
Also used : QuestionType(services.question.types.QuestionType) ContainerTag(j2html.tags.ContainerTag)

Example 3 with QuestionType

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

the class Question method setQuestionOptions.

/**
 * Add {@link QuestionOption}s to the builder, taking into account legacy columns.
 *
 * <p>The majority of questions should have a `questionOptions` and not `legacyQuestionOptions`.
 */
private void setQuestionOptions(QuestionDefinitionBuilder builder) throws InvalidQuestionTypeException {
    if (!QuestionType.of(questionType).isMultiOptionType()) {
        return;
    }
    // `legacyQuestionOptions` is a legacy implementation that only supported a single locale.
    if (questionOptions != null) {
        builder.setQuestionOptions(questionOptions);
        return;
    }
    // If the multi option question does have legacyQuestionOptions, we can assume there is only one
    // locale and convert the strings to QuestionOption instances each with a single locale.
    Locale firstKey = legacyQuestionOptions.keySet().stream().iterator().next();
    ImmutableList<QuestionOption> options = Streams.mapWithIndex(legacyQuestionOptions.get(firstKey).stream(), (optionText, i) -> QuestionOption.create(Long.valueOf(i), Long.valueOf(i), LocalizedStrings.of(firstKey, optionText))).collect(toImmutableList());
    builder.setQuestionOptions(options);
}
Also used : Locale(java.util.Locale) PrePersist(javax.persistence.PrePersist) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionOption(services.question.QuestionOption) ArrayList(java.util.ArrayList) Table(javax.persistence.Table) ImmutableList(com.google.common.collect.ImmutableList) DbArray(io.ebean.annotation.DbArray) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) DbJsonB(io.ebean.annotation.DbJsonB) Locale(java.util.Locale) PostLoad(javax.persistence.PostLoad) Constraints(play.data.validation.Constraints) ManyToMany(javax.persistence.ManyToMany) Entity(javax.persistence.Entity) JoinTable(javax.persistence.JoinTable) PreUpdate(javax.persistence.PreUpdate) ImmutableMap(com.google.common.collect.ImmutableMap) PostPersist(javax.persistence.PostPersist) QuestionType(services.question.types.QuestionType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) QuestionDefinition(services.question.types.QuestionDefinition) Streams(com.google.common.collect.Streams) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) List(java.util.List) LocalizedStrings(services.LocalizedStrings) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Optional(java.util.Optional) PostUpdate(javax.persistence.PostUpdate) QuestionOption(services.question.QuestionOption)

Example 4 with QuestionType

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

the class AdminQuestionController method newOne.

/**
 * Return a HTML page containing a form to create a new question in the draft version.
 */
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public Result newOne(Request request, String type) {
    QuestionType questionType;
    try {
        questionType = QuestionType.of(type);
    } catch (InvalidQuestionTypeException e) {
        return badRequest(invalidQuestionTypeMessage(type));
    }
    ImmutableList<EnumeratorQuestionDefinition> enumeratorQuestionDefinitions = service.getReadOnlyQuestionService().toCompletableFuture().join().getUpToDateEnumeratorQuestions();
    try {
        return ok(editView.renderNewQuestionForm(request, questionType, enumeratorQuestionDefinitions));
    } catch (UnsupportedQuestionTypeException e) {
        return badRequest(e.getMessage());
    }
}
Also used : InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionType(services.question.types.QuestionType) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) Secure(org.pac4j.play.java.Secure)

Example 5 with QuestionType

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

the class QuestionEditView method renderViewQuestionForm.

/**
 * Render a read-only non-submittable question form.
 */
public Content renderViewQuestionForm(QuestionDefinition questionDefinition, Optional<QuestionDefinition> maybeEnumerationQuestionDefinition) throws InvalidQuestionTypeException {
    QuestionForm questionForm = QuestionFormBuilder.create(questionDefinition);
    QuestionType questionType = questionForm.getQuestionType();
    String title = String.format("View %s question", questionType.toString().toLowerCase());
    SelectWithLabel enumeratorOption = enumeratorOptionsFromMaybeEnumerationQuestionDefinition(maybeEnumerationQuestionDefinition);
    ContainerTag formContent = buildQuestionContainer(title, QuestionFormBuilder.create(questionDefinition)).with(buildViewOnlyQuestionForm(questionForm, enumeratorOption));
    return renderWithPreview(formContent, questionType, title);
}
Also used : MultiOptionQuestionForm(forms.MultiOptionQuestionForm) QuestionForm(forms.QuestionForm) QuestionType(services.question.types.QuestionType) ContainerTag(j2html.tags.ContainerTag) SelectWithLabel(views.components.SelectWithLabel)

Aggregations

QuestionType (services.question.types.QuestionType)7 ContainerTag (j2html.tags.ContainerTag)5 InvalidQuestionTypeException (services.question.exceptions.InvalidQuestionTypeException)2 UnsupportedQuestionTypeException (services.question.exceptions.UnsupportedQuestionTypeException)2 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Streams (com.google.common.collect.Streams)1 MultiOptionQuestionForm (forms.MultiOptionQuestionForm)1 QuestionForm (forms.QuestionForm)1 DbArray (io.ebean.annotation.DbArray)1 DbJsonB (io.ebean.annotation.DbJsonB)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Optional (java.util.Optional)1 Entity (javax.persistence.Entity)1