Skip to content

Following Up Questions

mnipper edited this page Dec 12, 2013 · 1 revision

Following up questions are questions that use the response to a previous question in the same survey in their question text. The question identifier of the question that is being followed up on is loaded from the api during syncing.

Generating the Text

The location to insert the follow up question response is specified by a token. This token is set as a constant in the Question model.

    public static final String FOLLOW_UP_TRIGGER_STRING = "\\[followup\\]";

The SurveyFragment sets the text of the question as it is being asked. If a question is a following up question, then it will return the question object which it is following up for getFollowingUpQuestion().

    private void setQuestionText(TextView text) {
        if (mQuestion.getFollowingUpQuestion() != null) {
            text.setText(mQuestion.getFollowingUpText(mSurvey));
        } else {
            text.setText(mQuestion.getText());
        }
    }

getFollowingUpText() in Question handles what text should be inserted in place of the token. If the response is an option, then it must display the option text. If it is a free response, then it must display the written response.

    public String getFollowingUpText(Survey survey) {
        Response followUpResponse = survey.getResponseByQuestion(getFollowingUpQuestion());
        if (getFollowingUpQuestion().getQuestionType().equals(QuestionType.FREE_RESPONSE)) {
            return getText().replaceAll(FOLLOW_UP_TRIGGER_STRING, followUpResponse.getText());
        } else {
            return getText().replaceAll(
                    FOLLOW_UP_TRIGGER_STRING,
                    getFollowingUpQuestion().getOptionTextByResponse(followUpResponse)               
            );
        }
    }

getOptionTextByResponse() handles mapping an answer to an option, which is stored as an index for questions with options.

Clone this wiki locally