diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2013-09-17 22:28:51 -0700 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2013-09-17 22:28:51 -0700 | 
| commit | 9395177df22adcb90a573a9afa832e72b9a1930e (patch) | |
| tree | e8cdbc652f5c2dfe670833893c5f1393135338a4 /src/com/p4square/grow | |
| parent | feb118725012cb1929f245b446568fe1ecb6ce01 (diff) | |
Fixing question 4 to skip 4a in some cases.
For this fix I am moving Question and Answer from the backend into a new
model package. Question is now used in SurveyPageResource instead of the
Map. Eventually I should encode/decode the model from the json directly.
I am adding support to the Question model to find the next question
based on the answer to the current question. If the answer has a
specific nextQuestion field, that is used. Otherwise the question's
nextQuestion field is used. This is to facilitate skipping irrelevant
questions.
Diffstat (limited to 'src/com/p4square/grow')
| -rw-r--r-- | src/com/p4square/grow/backend/resources/SurveyResultsResource.java | 2 | ||||
| -rw-r--r-- | src/com/p4square/grow/frontend/SurveyPageResource.java | 79 | ||||
| -rw-r--r-- | src/com/p4square/grow/model/Answer.java (renamed from src/com/p4square/grow/backend/resources/Answer.java) | 10 | ||||
| -rw-r--r-- | src/com/p4square/grow/model/Question.java (renamed from src/com/p4square/grow/backend/resources/Question.java) | 37 | 
4 files changed, 89 insertions, 39 deletions
diff --git a/src/com/p4square/grow/backend/resources/SurveyResultsResource.java b/src/com/p4square/grow/backend/resources/SurveyResultsResource.java index e93e253..208fa2e 100644 --- a/src/com/p4square/grow/backend/resources/SurveyResultsResource.java +++ b/src/com/p4square/grow/backend/resources/SurveyResultsResource.java @@ -20,6 +20,8 @@ import org.restlet.representation.StringRepresentation;  import org.apache.log4j.Logger; +import com.p4square.grow.model.Answer; +import com.p4square.grow.model.Question;  import com.p4square.grow.backend.GrowBackend;  import com.p4square.grow.backend.db.CassandraDatabase; diff --git a/src/com/p4square/grow/frontend/SurveyPageResource.java b/src/com/p4square/grow/frontend/SurveyPageResource.java index 59be0fa..f4baa1c 100644 --- a/src/com/p4square/grow/frontend/SurveyPageResource.java +++ b/src/com/p4square/grow/frontend/SurveyPageResource.java @@ -26,6 +26,7 @@ import net.jesterpm.fmfacade.json.ClientException;  import net.jesterpm.fmfacade.FreeMarkerPageResource;  import com.p4square.grow.config.Config; +import com.p4square.grow.model.Question;  /**   * SurveyPageResource handles rendering the survey and processing user's answers. @@ -78,8 +79,8 @@ public class SurveyPageResource extends FreeMarkerPageResource {                  mQuestionId = getCurrentQuestionId();                  if (mQuestionId != null) { -                    Map<?, ?> lastQuestion = getQuestion(mQuestionId); -                    return redirectToNextQuestion(lastQuestion); +                    Question lastQuestion = getQuestion(mQuestionId); +                    return redirectToNextQuestion(lastQuestion, getAnswer(mQuestionId));                  }              } @@ -88,43 +89,41 @@ public class SurveyPageResource extends FreeMarkerPageResource {                  mQuestionId = "first";              } -            Map questionData = getQuestion(mQuestionId); -            if (questionData == null) { +            Question question = getQuestion(mQuestionId); +            if (question == null) {                  setStatus(Status.CLIENT_ERROR_NOT_FOUND);                  return new ErrorPage("Could not find the question.");              }              // Set the real question id if a meta-id was used (i.e. first) -            mQuestionId = (String) questionData.get("id"); +            mQuestionId = question.getId();              // Get any previous answer to the question -            String selectedAnswer = null; -            { -                JsonResponse response = backendGet("/accounts/" + mUserId + "/assessment/answers/" + mQuestionId); -                if (response.getStatus().isSuccess()) { -                    selectedAnswer = (String) response.getMap().get("answerId"); -                } -            } +            String selectedAnswer = getAnswer(mQuestionId);              Map root = getRootObject(); -            root.put("question", questionData); +            root.put("question", question.getMap());              root.put("selectedAnswerId", selectedAnswer);              // Get the question count and compute progress -            Map countData = getQuestion("count"); -            if (countData != null) { -                JsonResponse response = backendGet("/accounts/" + mUserId + "/assessment"); +            { +                JsonResponse response = backendGet("/assessment/question/count");                  if (response.getStatus().isSuccess()) { -                    Integer completed = (Integer) response.getMap().get("count"); -                    Integer total = (Integer) countData.get("count"); - -                    if (completed != null && total != null && total != 0) { -                        root.put("percentComplete", String.valueOf((int) (100.0 * completed) / total)); +                    Map countData = response.getMap(); +                    if (countData != null) { +                        response = backendGet("/accounts/" + mUserId + "/assessment"); +                        if (response.getStatus().isSuccess()) { +                            Integer completed = (Integer) response.getMap().get("count"); +                            Integer total = (Integer) countData.get("count"); + +                            if (completed != null && total != null && total != 0) { +                                root.put("percentComplete", String.valueOf((int) (100.0 * completed) / total)); +                            } +                        }                      }                  }              } -              return new TemplateRepresentation(mSurveyTemplate, root, MediaType.TEXT_HTML);          } catch (Exception e) { @@ -158,8 +157,8 @@ public class SurveyPageResource extends FreeMarkerPageResource {          try {              // Find the question -            Map<?, ?> questionData = getQuestion(mQuestionId); -            if (questionData == null) { +            Question question = getQuestion(mQuestionId); +            if (question == null) {                  // User is answering a question which doesn't exist                  setStatus(Status.CLIENT_ERROR_NOT_FOUND);                  return new ErrorPage("Question not found."); @@ -182,10 +181,10 @@ public class SurveyPageResource extends FreeMarkerPageResource {              // Find the next question or finish the assessment.              if ("previous".equals(direction)) { -                return redirectToPreviousQuestion(questionData); +                return redirectToPreviousQuestion(question);              } else { -                return redirectToNextQuestion(questionData); +                return redirectToNextQuestion(question, answerId);              }          } catch (Exception e) { @@ -213,7 +212,7 @@ public class SurveyPageResource extends FreeMarkerPageResource {          }      } -    private Map<?, ?> getQuestion(String id) { +    private Question getQuestion(String id) {          try {              Map<?, ?> questionData = null; @@ -223,7 +222,7 @@ public class SurveyPageResource extends FreeMarkerPageResource {              }              questionData = response.getMap(); -            return questionData; +            return new Question((Map<String, Object>) questionData);          } catch (ClientException e) {              LOG.warn("Error fetching question.", e); @@ -231,8 +230,22 @@ public class SurveyPageResource extends FreeMarkerPageResource {          }      } -    private Representation redirectToNextQuestion(Map<?, ?> questionData) { -        String nextQuestionId = (String) questionData.get("nextQuestion"); +    private String getAnswer(String questionId) { +        try { +            JsonResponse response = backendGet("/accounts/" + mUserId + "/assessment/answers/" + questionId); +            if (response.getStatus().isSuccess()) { +                return (String) response.getMap().get("answerId"); +            } + +        } catch (ClientException e) { +            LOG.warn("Error fetching answer to question " + questionId, e); +        } + +        return null; +    } + +    private Representation redirectToNextQuestion(Question question, String answerid) { +        String nextQuestionId = question.getNextQuestion(answerid);          if (nextQuestionId == null) {              // Just finished the last question. Update the user's account @@ -252,11 +265,11 @@ public class SurveyPageResource extends FreeMarkerPageResource {          return redirectToQuestion(nextQuestionId);      } -    private Representation redirectToPreviousQuestion(Map<?, ?> questionData) { -        String nextQuestionId = (String) questionData.get("previousQuestion"); +    private Representation redirectToPreviousQuestion(Question question) { +        String nextQuestionId = question.getPreviousQuestion();          if (nextQuestionId == null) { -            nextQuestionId = (String) questionData.get("id"); +            nextQuestionId = (String) question.getId();          }          return redirectToQuestion(nextQuestionId); diff --git a/src/com/p4square/grow/backend/resources/Answer.java b/src/com/p4square/grow/model/Answer.java index 5ba1bce..4c84060 100644 --- a/src/com/p4square/grow/backend/resources/Answer.java +++ b/src/com/p4square/grow/model/Answer.java @@ -2,7 +2,7 @@   * Copyright 2013 Jesse Morgan   */ -package com.p4square.grow.backend.resources; +package com.p4square.grow.model;  import java.util.Map; @@ -11,7 +11,7 @@ import java.util.Map;   *   * @author Jesse Morgan <jesse@jesterpm.net>   */ -class Answer { +public class Answer {      public static enum ScoreType {          NONE, AVERAGE, TRUMP;      } @@ -20,6 +20,7 @@ class Answer {      private final String mAnswerText;      private final ScoreType mType;      private final float mScoreFactor; +    private final String mNextQuestionId;      public Answer(final String id, final Map<String, Object> answer) {          mAnswerId = id; @@ -37,6 +38,7 @@ class Answer {              mScoreFactor = 0;          } +        mNextQuestionId = (String) answer.get("nextQuestion");      }      public String getId() { @@ -54,4 +56,8 @@ class Answer {      public float getScoreFactor() {          return mScoreFactor;      } + +    public String getNextQuestion() { +        return mNextQuestionId; +    }  } diff --git a/src/com/p4square/grow/backend/resources/Question.java b/src/com/p4square/grow/model/Question.java index e90ca77..387d723 100644 --- a/src/com/p4square/grow/backend/resources/Question.java +++ b/src/com/p4square/grow/model/Question.java @@ -2,7 +2,7 @@   * Copyright 2013 Jesse Morgan   */ -package com.p4square.grow.backend.resources; +package com.p4square.grow.model;  import java.util.Collections;  import java.util.HashMap; @@ -13,11 +13,12 @@ import java.util.Map;   *   * @author Jesse Morgan <jesse@jesterpm.net>   */ -class Question { +public class Question {      public static enum QuestionType {          TEXT, IMAGE, SLIDER, QUAD, CIRCLE;      } +    private final Map<String, Object> mMap;      private final String mQuestionId;      private final QuestionType mType;      private final String mQuestionText; @@ -27,6 +28,7 @@ class Question {      private final String mNextQuestionId;      public Question(final Map<String, Object> map) { +        mMap = map;          mQuestionId = (String) map.get("id");          mType = QuestionType.valueOf(((String) map.get("type")).toUpperCase()); @@ -58,15 +60,42 @@ class Question {          return mQuestionText;      } -    public String getPrevious() { +    public String getPreviousQuestion() {          return mPreviousQuestionId;      } -    public String getNext() { +    public String getNextQuestion() {          return mNextQuestionId;      }      public Map<String, Answer> getAnswers() {          return Collections.unmodifiableMap(mAnswers);      } + +    public Map<String, Object> getMap() { +        return Collections.unmodifiableMap(mMap); +    } + +    /** +     * Determine the id of the next question based on the answer to this +     * question. +     * +     * @param answerid +     *              The id of the selected answer. +     * @return a question id or null if this is the last question. +     */ +    public String getNextQuestion(String answerid) { +        String nextQuestion = null; + +        Answer a = mAnswers.get(answerid); +        if (a != null) { +            nextQuestion = a.getNextQuestion(); +        } + +        if (nextQuestion == null) { +            nextQuestion = mNextQuestionId; +        } + +        return nextQuestion; +    }  }  | 
