/*
   * score the previous question and add its
   */
  void scorePreviousAnswer(HttpSession session, HttpServletRequest request) {
    Integer previousQuestionIndex =
        (Integer) session.getAttribute(Constants.session_currentQuestionIndex);

    Integer score = (Integer) session.getAttribute(Constants.session_currentScore);
    Question prevQuestion = (Question) session.getAttribute(Constants.session_currentQuestion);
    if (prevQuestion != null) score += prevQuestion.scoreAnswer(request);

    if (((Quiz) session.getAttribute(Constants.session_currentQuiz)).immediateFeedback) {
      if (prevQuestion != null)
        session.setAttribute(Constants.session_previousFeedback, prevQuestion.getFeedback(request));
    }
    String allFeedback = (String) session.getAttribute(Constants.session_allFeedback);

    ArrayList<Feedback> allFeedbacks =
        (ArrayList<Feedback>) session.getAttribute(Constants.session_allFeedbackObjs);
    if (allFeedbacks == null) allFeedbacks = new ArrayList<Feedback>();
    if (prevQuestion != null) {
      Feedback f =
          new Feedback(
              prevQuestion.displayQuestion(),
              prevQuestion.getFeedback(request),
              prevQuestion.scoreAnswer(request),
              previousQuestionIndex);
      allFeedbacks.add(f);
      session.setAttribute(Constants.session_previousFeedbackObj, f);
    }
    session.setAttribute(Constants.session_allFeedbackObjs, allFeedbacks);

    if (allFeedback != null) allFeedback += "<br><br> " + prevQuestion.getFeedback(request);
    else if (prevQuestion != null) allFeedback = "<br><br> " + prevQuestion.getFeedback(request);
    else allFeedback = "";
    session.setAttribute(Constants.session_allFeedback, allFeedback);
    session.setAttribute(Constants.session_currentScore, score);
  }
  /* build up a session attribute, currentQuestionHTML, containing the HTML for *ALL*
   * the questions.
   * set session attribute lastQuestion to true so that only one page of questions is displayed.
   */
  void onePageQuiz(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    HttpSession session = request.getSession();
    Boolean alreadyAnswered = (Boolean) session.getAttribute(Constants.session_lastQuestionBool);
    Vector<Integer> quizQuestions =
        (Vector<Integer>)
            session.getAttribute(Constants.session_quizQuestions); // need to initialize this
    ArrayList<Feedback> feedbacks = new ArrayList<Feedback>();
    if (alreadyAnswered) {
      Integer score = 0;
      String feedback = "";
      int i = 1;
      for (Integer questionI : quizQuestions) {
        Question question =
            mysqlManager.getQuestion(
                request,
                session,
                questionI,
                (java.sql.Connection)
                    (request.getServletContext().getAttribute(Constants.context_Connection)));
        score += question.scoreAnswer(request);
        Feedback f =
            new Feedback(
                question.displayQuestion(),
                question.getFeedback(request),
                question.scoreAnswer(request),
                i);
        feedbacks.add(f);
        feedback += "<br>Question " + i + ":<br> " + question.getFeedback(request);
        i++;
      }
      session.setAttribute(Constants.session_allFeedbackObjs, feedbacks);

      session.setAttribute(Constants.session_allFeedback, feedback);
      session.setAttribute(Constants.session_currentScore, score);
      RequestDispatcher dispatcher = request.getRequestDispatcher("quizTakenSummary.jsp");
      dispatcher.forward(request, response);
    } else {
      String allHTML = "<form action = \"questionServlet\" method = \"post\">";
      for (Integer questionI : quizQuestions) {
        Question question =
            mysqlManager.getQuestion(
                request,
                session,
                questionI,
                (java.sql.Connection)
                    (request.getServletContext().getAttribute(Constants.context_Connection)));
        allHTML += question.displayQuestion();
        allHTML += "<br><br>";
      }
      allHTML += "<input type=\"submit\" value=\"submit\">";
      allHTML += "</form>";
      session.setAttribute(Constants.session_lastQuestionBool, true);
      session.setAttribute(Constants.session_questionHTML, allHTML);
      RequestDispatcher dispatcher = request.getRequestDispatcher("showQuestion.jsp");
      dispatcher.forward(request, response);
    }
  }