/**
   * Update the grading of the assessment.
   *
   * @param ag the assessment grading.
   * @param g the Gradebook Service
   * @throws java.lang.Exception
   */
  public void updateExternalAssessmentScore(AssessmentGradingIfc ag, GradebookService g)
      throws Exception {
    boolean testErrorHandling = false;
    // log.info("GradebookService instance=" + g);
    PublishedAssessmentService pubService = new PublishedAssessmentService();
    GradingService gradingService = new GradingService();
    PublishedAssessmentIfc pub =
        (PublishedAssessmentIfc)
            gradingService.getPublishedAssessmentByAssessmentGradingId(
                ag.getAssessmentGradingId().toString());

    String gradebookUId = pubService.getPublishedAssessmentOwner(pub.getPublishedAssessmentId());
    if (gradebookUId == null) {
      return;
    }

    // SAM-1562 We need to round the float score and covert to a double -DH
    float fScore = MathUtils.round(ag.getFinalScore(), 2);
    Double score = Float.valueOf(fScore).doubleValue();
    log.info("rounded:  " + ag.getFinalScore() + " to: " + score.toString());
    g.updateExternalAssessmentScore(
        gradebookUId, ag.getPublishedAssessmentId().toString(), ag.getAgentId(), score);
    if (testErrorHandling) {
      throw new Exception("Encountered an error in update ExternalAssessmentScore.");
    }
  }
  /**
   * Update a gradebook.
   *
   * @param publishedAssessment the published assessment
   * @param g the Gradebook Service
   * @return false: cannot update the gradebook
   * @throws java.lang.Exception
   */
  public boolean updateGradebook(PublishedAssessmentIfc publishedAssessment, GradebookService g)
      throws Exception {
    log.debug("updateGradebook start");
    String gradebookUId = GradebookFacade.getGradebookUId();
    if (gradebookUId == null) {
      return false;
    }

    log.debug("before g.isAssignmentDefined()");
    g.updateExternalAssessment(
        gradebookUId,
        publishedAssessment.getPublishedAssessmentId().toString(),
        null,
        publishedAssessment.getTitle(),
        publishedAssessment.getTotalScore().doubleValue(),
        publishedAssessment.getAssessmentAccessControl().getDueDate());
    return true;
  }
  private void populateSections(
      PublishedAssessmentIfc publishedAssessment,
      QuestionScoresBean bean,
      TotalScoresBean totalBean,
      ArrayList scores,
      PublishedAssessmentService pubService) {
    ArrayList sections = new ArrayList();
    log.debug(
        "questionScores(): populate sctions publishedAssessment.getSectionArraySorted size = "
            + publishedAssessment.getSectionArraySorted().size());
    Iterator iter = publishedAssessment.getSectionArraySorted().iterator();
    int i = 1;
    while (iter.hasNext()) {
      SectionDataIfc section = (SectionDataIfc) iter.next();
      ArrayList items = new ArrayList();
      PartData part = new PartData();
      boolean isRandomDrawPart =
          pubService.isRandomDrawPart(
              publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
      part.setIsRandomDrawPart(isRandomDrawPart);
      part.setPartNumber("" + i);
      part.setId(section.getSectionId().toString());

      if (isRandomDrawPart) {
        if (section.getSectionMetaDataByLabel(SectionDataIfc.NUM_QUESTIONS_DRAWN) != null) {
          int numberToBeDrawn =
              Integer.parseInt(
                  section.getSectionMetaDataByLabel(SectionDataIfc.NUM_QUESTIONS_DRAWN));
          part.setNumberQuestionsDraw(numberToBeDrawn);
        }
        PublishedAssessmentService publishedAssessmentService = new PublishedAssessmentService();
        HashSet itemSet =
            publishedAssessmentService.getPublishedItemSet(
                publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
        section.setItemSet(itemSet);
      } else {
        GradingService gradingService = new GradingService();
        HashSet itemSet =
            gradingService.getItemSet(
                publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
        section.setItemSet(itemSet);
      }
      Iterator iter2 = section.getItemArraySortedForGrading().iterator();
      int j = 1;
      while (iter2.hasNext()) {
        ItemDataIfc item = (ItemDataIfc) iter2.next();
        PartData partitem = new PartData();

        partitem.setPartNumber("" + j);
        partitem.setId(item.getItemId().toString());
        log.debug("*   item.getId = " + item.getItemId());
        partitem.setLinked(true);

        // Iterator iter3 = scores.iterator();
        items.add(partitem);
        j++;
      }
      log.debug("questionScores(): items size = " + items.size());
      part.setQuestionNumberList(items);
      sections.add(part);
      i++;
    }
    log.debug("questionScores(): sections size = " + sections.size());
    bean.setSections(sections);
  }
  /**
   * This will populate the QuestionScoresBean with the data associated with the particular
   * versioned assessment based on the publishedId.
   *
   * @todo Some of this code will change when we move this to Hibernate persistence.
   * @param publishedId String
   * @param bean QuestionScoresBean
   * @return boolean
   */
  public boolean questionScores(
      String publishedId, QuestionScoresBean bean, boolean isValueChange) {
    log.debug("questionScores()");
    try {
      PublishedAssessmentService pubService = new PublishedAssessmentService();
      PublishedItemService pubItemService = new PublishedItemService();
      // get the PublishedAssessment based on publishedId
      QuestionScoresBean questionBean =
          (QuestionScoresBean) ContextUtil.lookupBean("questionScores");
      PublishedAssessmentIfc publishedAssessment = questionBean.getPublishedAssessment();
      if (publishedAssessment == null) {
        publishedAssessment = pubService.getPublishedAssessment(publishedId);
        questionBean.setPublishedAssessment(publishedAssessment);
      }
      // build a hashMap (publishedItemId, publishedItem)
      HashMap publishedItemHash = pubService.preparePublishedItemHash(publishedAssessment);
      log.debug("questionScores(): publishedItemHash.size = " + publishedItemHash.size());
      // build a hashMap (publishedItemTextId, publishedItemText)
      HashMap publishedItemTextHash = pubService.preparePublishedItemTextHash(publishedAssessment);
      log.debug("questionScores(): publishedItemTextHash.size = " + publishedItemTextHash.size());
      HashMap publishedAnswerHash = pubService.preparePublishedAnswerHash(publishedAssessment);
      // re-attach session and load all lazy loaded parent/child stuff

      //			Set<Long> publishedAnswerHashKeySet = publishedAnswerHash.keySet();
      //
      //			for (Long key : publishedAnswerHashKeySet) {
      //				AnswerIfc answer = (AnswerIfc) publishedAnswerHash.get(key);
      //
      //				if (!Hibernate.isInitialized(answer.getChildAnswerSet())) {
      //					pubItemService.eagerFetchAnswer(answer);
      //				}
      //			}
      log.debug("questionScores(): publishedAnswerHash.size = " + publishedAnswerHash.size());
      HashMap agentResultsByItemGradingIdMap = new HashMap();

      GradingService delegate = new GradingService();

      TotalScoresBean totalBean = (TotalScoresBean) ContextUtil.lookupBean("totalScores");

      if (ContextUtil.lookupParam("sortBy") != null
          && !ContextUtil.lookupParam("sortBy").trim().equals(""))
        bean.setSortType(ContextUtil.lookupParam("sortBy"));

      String itemId = ContextUtil.lookupParam("itemId");
      if (ContextUtil.lookupParam("newItemId") != null
          && !ContextUtil.lookupParam("newItemId").trim().equals("")
          && !ContextUtil.lookupParam("newItemId").trim().equals("null"))
        itemId = ContextUtil.lookupParam("newItemId");

      if (ContextUtil.lookupParam("sortAscending") != null
          && !ContextUtil.lookupParam("sortAscending").trim().equals("")) {
        bean.setSortAscending(
            Boolean.valueOf(ContextUtil.lookupParam("sortAscending")).booleanValue());
      }

      String which = bean.getAllSubmissions();
      if (which == null && totalBean.getAllSubmissions() != null) {
        // use totalscore's selection
        which = totalBean.getAllSubmissions();
        bean.setAllSubmissions(which);
      }

      totalBean.setSelectedSectionFilterValue(
          bean.getSelectedSectionFilterValue()); // set section pulldown

      if (bean.getSelectedSARationaleView() == null) {
        // if bean.showSARationaleInLine is null, then set inline to be
        // the default
        bean.setSelectedSARationaleView(QuestionScoresBean.SHOW_SA_RATIONALE_RESPONSES_INLINE);
      }

      if ("true".equalsIgnoreCase(totalBean.getAnonymous())) {
        boolean groupRelease =
            publishedAssessment
                .getAssessmentAccessControl()
                .getReleaseTo()
                .equals(AssessmentAccessControl.RELEASE_TO_SELECTED_GROUPS);
        if (groupRelease) {
          totalBean.setSelectedSectionFilterValue(
              TotalScoresBean.RELEASED_SECTIONS_GROUPS_SELECT_VALUE);
        } else {
          totalBean.setSelectedSectionFilterValue(TotalScoresBean.ALL_SECTIONS_SELECT_VALUE);
        }
      }

      bean.setPublishedId(publishedId);
      Date dueDate = null;

      HashMap map =
          getItemScores(Long.valueOf(publishedId), Long.valueOf(itemId), which, isValueChange);
      log.debug("questionScores(): map .size = " + map.size());
      ResourceLoader rb = null;
      ArrayList allscores = new ArrayList();
      Iterator keyiter = map.keySet().iterator();
      while (keyiter.hasNext()) {
        allscores.addAll((ArrayList) map.get(keyiter.next()));
      }

      log.debug("questionScores(): allscores.size = " + allscores.size());

      // /

      // now we need filter by sections selected
      ArrayList scores = new ArrayList(); // filtered list
      Map useridMap = totalBean.getUserIdMap(TotalScoresBean.CALLED_FROM_QUESTION_SCORE_LISTENER);
      bean.setUserIdMap(useridMap);
      log.debug("questionScores(): useridMap.size = " + useridMap.size());

      /*
       * if ("true".equalsIgnoreCase(totalBean.getAnonymous())){ // skip
       * section filter if it is anonymous grading, SAK-4395,
       * scores.addAll(allscores); }
       */
      if (totalBean.getReleaseToAnonymous()) {
        // skip section filter if it's published to anonymous users
        scores.addAll(allscores);
      } else {
        Iterator allscores_iter = allscores.iterator();
        // get the Map of all users(keyed on userid) belong to the
        // selected sections
        while (allscores_iter.hasNext()) {
          // AssessmentGradingData data = (AssessmentGradingData)
          // allscores_iter.next();
          ItemGradingData idata = (ItemGradingData) allscores_iter.next();
          // String agentid =
          // idata.getAssessmentGrading().getAgentId();
          String agentid = idata.getAgentId();
          // now we only include scores of users belong to the
          // selected sections
          if (useridMap.containsKey(agentid)) {
            scores.add(idata);
          }
        }
      }

      log.debug("questionScores(): scores.size = " + scores.size());

      Iterator iter = scores.iterator();
      ArrayList agents = new ArrayList();

      log.debug("questionScores(): calling populateSections ");

      populateSections(
          publishedAssessment, bean, totalBean, scores, pubService); // set up the Q1, Q2... links
      if (!iter.hasNext()) {
        // this section has no students
        log.debug("questionScores(): this section has no students");
        bean.setAgents(agents);
        bean.setAllAgents(agents);
        bean.setTotalPeople(Integer.toString(bean.getAgents().size()));
        bean.setAnonymous(totalBean.getAnonymous());
        // return true;
      }

      // List them by item and assessmentgradingid, so we can
      // group answers by item and save them for update use.

      HashMap scoresByItem = new HashMap();
      while (iter.hasNext()) {
        ItemGradingData idata = (ItemGradingData) iter.next();
        ItemTextIfc pubItemText =
            (ItemTextIfc) publishedItemTextHash.get(idata.getPublishedItemTextId());
        AnswerIfc pubAnswer = (AnswerIfc) publishedAnswerHash.get(idata.getPublishedAnswerId());

        ArrayList temp =
            (ArrayList)
                scoresByItem.get(idata.getAssessmentGradingId() + ":" + idata.getPublishedItemId());
        if (temp == null) temp = new ArrayList();

        // Very small numbers, so bubblesort is fast
        Iterator iter2 = temp.iterator();
        ArrayList newList = new ArrayList();
        boolean added = false;
        while (iter2.hasNext()) {
          ItemGradingData tmpData = (ItemGradingData) iter2.next();
          ItemTextIfc tmpPublishedText =
              (ItemTextIfc) publishedItemTextHash.get(tmpData.getPublishedItemTextId());
          AnswerIfc tmpAnswer = (AnswerIfc) publishedAnswerHash.get(tmpData.getPublishedAnswerId());

          if (pubAnswer != null
              && tmpAnswer != null
              && !added
              && (pubItemText.getSequence().intValue() < tmpPublishedText.getSequence().intValue()
                  || (pubItemText.getSequence().intValue()
                          == tmpPublishedText.getSequence().intValue()
                      && pubAnswer.getSequence().intValue()
                          < tmpAnswer.getSequence().intValue()))) {
            newList.add(idata);
            added = true;
          }
          newList.add(tmpData);
        }
        if (!added) newList.add(idata);
        scoresByItem.put(
            idata.getAssessmentGradingId() + ":" + idata.getPublishedItemId(), newList);
      }
      log.debug("questionScores(): scoresByItem.size = " + scoresByItem.size());
      bean.setScoresByItem(scoresByItem);

      try {
        bean.setAnonymous(
            publishedAssessment
                    .getEvaluationModel()
                    .getAnonymousGrading()
                    .equals(EvaluationModel.ANONYMOUS_GRADING)
                ? "true"
                : "false");
      } catch (RuntimeException e) {
        // log.info("No evaluation model.");
        bean.setAnonymous("false");
      }

      // below properties don't seem to be used in jsf pages,
      try {
        bean.setLateHandling(
            publishedAssessment.getAssessmentAccessControl().getLateHandling().toString());
      } catch (Exception e) {
        // log.info("No access control model.");
        bean.setLateHandling(AssessmentAccessControl.NOT_ACCEPT_LATE_SUBMISSION.toString());
      }
      try {
        bean.setDueDate(publishedAssessment.getAssessmentAccessControl().getDueDate().toString());
        dueDate = publishedAssessment.getAssessmentAccessControl().getDueDate();
      } catch (RuntimeException e) {
        // log.info("No due date.");
        bean.setDueDate(new Date().toString());
      }
      try {
        bean.setMaxScore(publishedAssessment.getEvaluationModel().getFixedTotalScore());
      } catch (RuntimeException e) {
        double score = (double) 0.0;
        Iterator iter2 = publishedAssessment.getSectionArraySorted().iterator();
        while (iter2.hasNext()) {
          SectionDataIfc sdata = (SectionDataIfc) iter2.next();
          Iterator iter3 = sdata.getItemArraySortedForGrading().iterator();
          while (iter3.hasNext()) {
            ItemDataIfc idata = (ItemDataIfc) iter3.next();
            if (idata.getItemId().equals(Long.valueOf(itemId)))
              score = idata.getScore().doubleValue();
          }
        }
        bean.setMaxScore(score);
      }

      // need to get id from somewhere else, not from data. data only
      // contains answered items , we want to return all items.
      // ItemDataIfc item = (ItemDataIfc) publishedItemHash.get(data.getPublishedItemId());
      ItemDataIfc item = (ItemDataIfc) publishedItemHash.get(Long.valueOf(itemId));

      if (item != null) {
        log.debug("item!=null steting type id = " + item.getTypeId().toString());
        bean.setTypeId(item.getTypeId().toString());
        bean.setItemId(item.getItemId().toString());
        bean.setPartName(item.getSection().getSequence().toString());
        bean.setItemName(item.getSequence().toString());
        item.setHint("***"); // Keyword to not show student answer
        // for short answer/ essey question, if there is a model short
        // answer for this question
        // set haveModelShortAnswer to true
        if (item.getTypeId().equals(Long.valueOf(5))) {
          Iterator iterator = publishedAnswerHash.values().iterator();
          while (iterator.hasNext()) {
            PublishedAnswer publishedAnswer = (PublishedAnswer) iterator.next();
            if (publishedAnswer.getItem().getItemId().equals(item.getItemId())) {
              if (publishedAnswer.getText() == null || publishedAnswer.getText().equals("")) {
                bean.setHaveModelShortAnswer(false);
              } else {
                bean.setHaveModelShortAnswer(true);
              }
              break;
            }
          }
        }
      } else {
        log.debug("item==null ");
      }

      ArrayList deliveryItems = new ArrayList(); // so we can use the var
      if (item != null) deliveryItems.add(item);
      bean.setDeliveryItem(deliveryItems);

      if (ContextUtil.lookupParam("roleSelection") != null) {
        bean.setRoleSelection(ContextUtil.lookupParam("roleSelection"));
      }

      if (bean.getSortType() == null) {
        if (bean.getAnonymous().equals("true")) {
          bean.setSortType("totalAutoScore");
        } else {
          bean.setSortType("lastName");
        }
      }

      // recordingData encapsulates the inbeanation needed for recording.
      // set recording agent, agent assessmentId,
      // set course_assignment_context value
      // set max tries (0=unlimited), and 30 seconds max length

      // String courseContext = bean.getAssessmentName() + " total ";

      // Note this is HTTP-centric right now, we can't use in Faces
      // AuthoringHelper authoringHelper = new AuthoringHelper();
      // authoringHelper.getRemoteUserID() needs servlet stuff
      // authoringHelper.getRemoteUserName() needs servlet stuff

      /* Dump the grading and agent information into AgentResults */
      iter = scoresByItem.values().iterator();
      while (iter.hasNext()) {
        AgentResults results = new AgentResults();
        // Get all the answers for this question to put in one grading
        // row
        ArrayList answerList = (ArrayList) iter.next();
        results.setItemGradingArrayList(answerList);

        Iterator iter2 = answerList.iterator();
        ArrayList itemGradingAttachmentList = new ArrayList();
        HashMap<Long, Set<String>> fibmap = new HashMap<Long, Set<String>>();
        while (iter2.hasNext()) {
          ItemGradingData gdata = (ItemGradingData) iter2.next();
          results.setItemGrading(gdata);
          itemGradingAttachmentList.addAll(gdata.getItemGradingAttachmentList());
          agentResultsByItemGradingIdMap.put(gdata.getItemGradingId(), results);

          ItemTextIfc gdataPubItemText =
              (ItemTextIfc) publishedItemTextHash.get(gdata.getPublishedItemTextId());
          AnswerIfc gdataAnswer = (AnswerIfc) publishedAnswerHash.get(gdata.getPublishedAnswerId());

          // This all just gets the text of the answer to display
          String answerText = noAnswer;
          String rationale = "";
          String fullAnswerText = noAnswer;

          // if question type = MC, MR, Survey, TF, Matching, if user
          // has not submit an answer
          // answerText = noAnswer. These question type do not use the
          // itemGrading.answerText field for
          // storing answers, thye use temGrading.publishedAnswerId to
          // make their selection
          if (bean.getTypeId().equals("1")
              || bean.getTypeId().equals("2")
              || bean.getTypeId().equals("12")
              || bean.getTypeId().equals("3")
              || bean.getTypeId().equals("4")
              || bean.getTypeId().equals("9")
              || bean.getTypeId().equals("13")) {
            if (gdataAnswer != null) answerText = gdataAnswer.getText();
          } else {
            // this handles the other question types: SAQ, File
            // upload, Audio, FIB, Fill in Numeric
            // These question type use itemGrading.answetText to
            // store information about their answer
            if ((bean.getTypeId().equals("8")
                    || bean.getTypeId().equals("11")
                    || bean.getTypeId().equals("14"))
                && gdataAnswer == null) {
              answerText = "";
            } else if (bean.getTypeId().equals("14")) { // gopalrc - EMI
              answerText = gdataPubItemText.getSequence() + ": " + gdataAnswer.getLabel();
            } else {
              answerText = gdata.getAnswerText();
            }
          }

          if ("4".equals(bean.getTypeId())) {
            if (rb == null) {
              rb = new ResourceLoader("org.sakaiproject.tool.assessment.bundle.EvaluationMessages");
            }
            if ("true".equals(answerText)) {
              answerText = rb.getString("true_msg");
            } else if ("false".equals(answerText)) {
              answerText = rb.getString("false_msg");
            }
          }

          if (bean.getTypeId().equals("9")) {
            if (gdataPubItemText == null) {
              // the matching pair is deleted
              answerText = "";
            } else {
              answerText = gdataPubItemText.getSequence() + ":" + answerText;
            }
          }

          if (bean.getTypeId().equals("8")) {
            if (gdataAnswer != null && gdataAnswer.getSequence() != null) {
              answerText = gdataAnswer.getSequence() + ":" + answerText;
            }
          }

          if (bean.getTypeId().equals("11")) {
            if (gdataAnswer != null && gdataAnswer.getSequence() != null) {
              answerText = gdataAnswer.getSequence() + ":" + answerText;
            }
          }
          if (bean.getTypeId().equals("13")) {
            if (gdataPubItemText == null) {
              answerText = "";
            } else {
              int answerNo = gdataPubItemText.getSequence().intValue();
              answerText = answerNo + ":" + answerText;
            }
          }
          // file upload
          if (bean.getTypeId().equals("6")) {
            gdata.setMediaArray(delegate.getMediaArray2(gdata.getItemGradingId().toString()));
          }

          // audio recording
          if (bean.getTypeId().equals("7")) {
            ArrayList mediaList = delegate.getMediaArray2(gdata.getItemGradingId().toString());
            setDurationIsOver(item, mediaList);
            gdata.setMediaArray(mediaList);
          }
          if (bean.getTypeId().equals("16")) {
            if (gdataPubItemText == null) {
              // the matching pair is deleted
              answerText = "";
            } else {
              answerText = gdataPubItemText.getSequence() + ":" + answerText;
            }
          }
          if (answerText == null) answerText = noAnswer;
          else {
            if (gdata.getRationale() != null && !gdata.getRationale().trim().equals(""))
              rationale = "\nRationale: " + gdata.getRationale();
          }
          // Huong's temp commandout
          // answerText = answerText.replaceAll("<.*?>", "");
          answerText = answerText.replaceAll("(\r\n|\r)", "<br/>");
          rationale = rationale.replaceAll("<.*?>", "");
          rationale = rationale.replaceAll("(\r\n|\r)", "<br/>");
          fullAnswerText = answerText; // this is the
          // non-abbreviated answers
          // for essay questions

          int answerTextLength =
              ServerConfigurationService.getInt("samigo.questionScore.answerText.length", 1000);
          if (bean.getTypeId().equals("5")) {
            answerTextLength = 35;
          }

          // Fix for SAK-6932: Strip out all HTML tags except image tags
          if (answerText.length() > answerTextLength) {
            String noHTMLAnswerText;
            noHTMLAnswerText = answerText.replaceAll("<((..?)|([^iI][^mM][^gG].*?))>", "");

            int index = noHTMLAnswerText.toLowerCase().indexOf("<img");
            if (index != -1) {
              answerText = noHTMLAnswerText;
            } else {
              if (noHTMLAnswerText.length() > answerTextLength) {
                answerText = noHTMLAnswerText.substring(0, answerTextLength) + "...";
              } else {
                answerText = noHTMLAnswerText;
              }
            }
          }
          /*
           * // no need to shorten it if (rationale.length() > 35)
           * rationale = rationale.substring(0, 35) + "...";
           */

          // SAM-755-"checkmark" indicates right, add "X" to indicate wrong
          if (gdataAnswer != null) {
            String checkmarkGif = "<img src='/samigo-app/images/delivery/checkmark.gif'>";
            String crossmarkGif = "<img src='/samigo-app/images/crossmark.gif'>";

            if (bean.getTypeId().equals("8") || bean.getTypeId().equals("11")) {
              answerText = FormattedText.escapeHtml(answerText, true);
              if (gdata.getIsCorrect() == null) {
                boolean result = false;
                if (bean.getTypeId().equals("8")) {
                  result = delegate.getFIBResult(gdata, fibmap, item, publishedAnswerHash);
                } else {
                  result = delegate.getFINResult(gdata, item, publishedAnswerHash);
                }

                if (result) {
                  answerText = checkmarkGif + answerText;
                } else {
                  answerText = crossmarkGif + answerText;
                }
              } else {
                if (gdata.getIsCorrect().booleanValue()) {
                  answerText = checkmarkGif + answerText;
                } else {
                  answerText = crossmarkGif + answerText;
                }
              }
            } else if (bean.getTypeId().equals("15")) { // CALCULATED_QUESTION
              answerText = FormattedText.escapeHtml(answerText, true);
              // need to do something here for fill in the blanks
              if (gdataAnswer.getScore() > 0) {
                // if score is 0, there is no way to tell if user got the correct answer
                // by using "autoscore"... wish there was a better way to tell if its correct or not
                Double autoscore = gdata.getAutoScore();
                if (!(Double.valueOf(0)).equals(autoscore)) {
                  answerText = checkmarkGif + answerText;
                } else if (Double.valueOf(0).equals(autoscore)) {
                  answerText = crossmarkGif + answerText;
                }
              }
            } else if (!bean.getTypeId().equals("3")) {
              if ((gdataAnswer.getIsCorrect() != null && gdataAnswer.getIsCorrect())
                  || (gdataAnswer.getPartialCredit() != null
                      && gdataAnswer.getPartialCredit() > 0)) {
                answerText = checkmarkGif + answerText;
              } else if (gdataAnswer.getIsCorrect() != null && !gdataAnswer.getIsCorrect()) {
                answerText = crossmarkGif + answerText;
              }
            }
          }

          // -- Got the answer text --
          if (!answerList.get(0).equals(gdata)) { // We already have
            // an agentResults
            // for this one
            results.setAnswer(results.getAnswer() + "<br/>" + answerText);
            if (gdata.getAutoScore() != null) {
              results.setTotalAutoScore(
                  Double.toString(
                      (Double.valueOf(results.getExactTotalAutoScore())).doubleValue()
                          + gdata.getAutoScore().doubleValue()));
            } else {
              results.setTotalAutoScore(
                  Double.toString(
                      (Double.valueOf(results.getExactTotalAutoScore())).doubleValue()));
            }
            results.setItemGradingAttachmentList(itemGradingAttachmentList);
          } else {
            results.setItemGradingId(gdata.getItemGradingId());
            results.setAssessmentGradingId(gdata.getAssessmentGradingId());
            if (gdata.getAutoScore() != null) {
              // for example, if an assessment has one fileupload
              // question, the autoscore = null
              results.setTotalAutoScore(gdata.getAutoScore().toString());
            } else {
              results.setTotalAutoScore(Double.toString(0));
            }
            results.setComments(FormattedText.convertFormattedTextToPlaintext(gdata.getComments()));
            results.setAnswer(answerText);
            results.setFullAnswer(fullAnswerText);
            results.setRationale(rationale);
            results.setSubmittedDate(gdata.getSubmittedDate());

            AgentFacade agent = new AgentFacade(gdata.getAgentId());
            // log.info("Rachel: agentid = " + gdata.getAgentId());
            results.setLastName(agent.getLastName());
            results.setFirstName(agent.getFirstName());
            results.setEmail(agent.getEmail());
            if (results.getLastName() != null && results.getLastName().length() > 0)
              results.setLastInitial(results.getLastName().substring(0, 1));
            else if (results.getFirstName() != null && results.getFirstName().length() > 0)
              results.setLastInitial(results.getFirstName().substring(0, 1));
            else results.setLastInitial("Anonymous");
            results.setIdString(agent.getIdString());
            results.setAgentEid(agent.getEidString());
            results.setAgentDisplayId(agent.getDisplayIdString());
            log.debug("testing agent getEid agent.getFirstname= " + agent.getFirstName());
            log.debug("testing agent getEid agent.getid= " + agent.getIdString());
            log.debug("testing agent getEid agent.geteid = " + agent.getEidString());
            log.debug(
                "testing agent getDisplayId agent.getdisplayid = " + agent.getDisplayIdString());

            results.setRole(agent.getRole());
            results.setItemGradingAttachmentList(itemGradingAttachmentList);
            agents.add(results);
          }
        }
      }

      // log.info("Sort type is " + bean.getSortType() + ".");
      bs = new BeanSort(agents, bean.getSortType());
      if ((bean.getSortType()).equals("assessmentGradingId")
          || (bean.getSortType()).equals("totalAutoScore")
          || (bean.getSortType()).equals("totalOverrideScore")
          || (bean.getSortType()).equals("finalScore")) {
        bs.toNumericSort();
      } else {
        bs.toStringSort();
      }

      if (bean.isSortAscending()) {
        log.debug("sortAscending");
        agents = (ArrayList) bs.sort();
      } else {
        log.debug("!sortAscending");
        agents = (ArrayList) bs.sortDesc();
      }

      // log.info("Listing agents.");
      bean.setAgents(agents);
      bean.setAllAgents(agents);
      bean.setTotalPeople(Integer.valueOf(bean.getAgents().size()).toString());
      bean.setAgentResultsByItemGradingId(agentResultsByItemGradingIdMap);
    } catch (RuntimeException e) {
      e.printStackTrace();
      return false;
    }

    return true;
  }