/** {@inheritDoc} */
  public boolean canEditNoteItem(Assignment a) {
    String userId = "";
    User u = m_userDirectoryService.getCurrentUser();
    if (u != null) {
      userId = u.getId();
    }

    if (a != null) {
      AssignmentNoteItem note = getNoteItem(a.getId());
      if (note != null) {
        if (note.getCreatorId().equals(userId)) {
          // being creator can edit
          return true;
        } else if (note.getShareWith() == AssignmentConstants.NOTE_READ_AND_WRITE_BY_OTHER
            && m_assignmentService.allowGradeSubmission(a.getReference())) {
          return true;
        }
      } else {
        return true;
      }
    } else {
      return true;
    }

    return false;
  }
 public void schedulePeerReview(String assignmentId) {
   // first remove any previously scheduled reviews:
   removeScheduledPeerReview(assignmentId);
   // now schedule a time for the review to be setup
   Assignment assignment;
   try {
     assignment = assignmentService.getAssignment(assignmentId);
     if (!assignment.getDraft() && assignment.getAllowPeerAssessment()) {
       Time assignmentCloseTime = assignment.getCloseTime();
       Time openTime = null;
       if (assignmentCloseTime != null) {
         openTime = timeService.newTime(assignmentCloseTime.getTime());
       }
       // Schedule the new notification
       if (openTime != null) {
         scheduledInvocationManager.createDelayedInvocation(
             openTime,
             "org.sakaiproject.assignment.api.AssignmentPeerAssessmentService",
             assignmentId);
       }
     }
   } catch (IdUnusedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (PermissionException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
  /** {@inheritDoc} */
  public boolean canReadNoteItem(Assignment a, String context) {
    if (a != null) {
      AssignmentNoteItem note = getNoteItem(a.getId());
      if (note != null) {
        User u = m_userDirectoryService.getCurrentUser();
        String noteCreatorId = note.getCreatorId();
        if (noteCreatorId.equals(u.getId())) {
          return true;
        } else if (m_assignmentService.allowGradeSubmission(a.getReference())) {
          // check whether the instructor type can view the note
          int share = note.getShareWith();
          if (share == AssignmentConstants.NOTE_READ_BY_OTHER
              || share == AssignmentConstants.NOTE_READ_AND_WRITE_BY_OTHER) {
            return true;
          }
        }
      }
    } else {
      if (m_assignmentService.allowAddAssignment(context)) {
        return true;
      }
    }

    return false;
  }
 /** {@inheritDoc} */
 public boolean canViewModelAnswer(Assignment a, AssignmentSubmission s) {
   if (a != null) {
     AssignmentModelAnswerItem m = getModelAnswer(a.getId());
     if (m != null) {
       if (m_assignmentService.allowGradeSubmission(a.getReference())) {
         // model answer is viewable to all graders
         return true;
       } else {
         int show = m.getShowTo();
         if (show == AssignmentConstants.MODEL_ANSWER_SHOW_TO_STUDENT_BEFORE_STARTS) {
           return true;
         } else if (show == AssignmentConstants.MODEL_ANSWER_SHOW_TO_STUDENT_AFTER_SUBMIT
             && s != null
             && s.getSubmitted()) {
           return true;
         } else if (show == AssignmentConstants.MODEL_ANSWER_SHOW_TO_STUDENT_AFTER_GRADE_RETURN
             && s != null
             && s.getGradeReleased()) {
           return true;
         } else if (show == AssignmentConstants.MODEL_ANSWER_SHOW_TO_STUDENT_AFTER_ACCEPT_UTIL
             && (a.getCloseTime().before(TimeService.newTime()))) {
           return true;
         }
       }
     }
   }
   return false;
 }
  /** {@inheritDoc} */
  public boolean canViewAllPurposeItem(Assignment a) {
    boolean rv = false;

    if (a != null) {
      AssignmentAllPurposeItem aItem = getAllPurposeItem(a.getId());
      if (aItem != null) {
        if (!aItem.getHide()) {
          Time now = TimeService.newTime();
          Date releaseDate = aItem.getReleaseDate();
          Date retractDate = aItem.getRetractDate();

          if (releaseDate == null && retractDate == null) {
            // no time limitation on showing the item
            rv = true;
          } else if (releaseDate != null && retractDate == null) {
            // has relase date but not retract date
            rv = now.getTime() > releaseDate.getTime();
          } else if (releaseDate == null && retractDate != null) {
            // has retract date but not release date
            rv = now.getTime() < retractDate.getTime();
          } else if (now != null) {
            // both releaseDate and retract date are not null
            // has both release and retract dates
            rv = now.getTime() > releaseDate.getTime() && now.getTime() < retractDate.getTime();
          }
        } else {
          rv = false;
        }
      }

      if (rv) {
        // reset rv
        rv = false;

        // need to check role/user permission only if the above time test returns true
        List<String> access = getAccessListForAllPurposeItem(aItem);
        User u = m_userDirectoryService.getCurrentUser();
        if (u != null) {
          if (access.contains(u.getId())) rv = true;
          else {
            try {
              String role =
                  m_authzGroupService.getUserRole(
                      u.getId(), m_siteService.siteReference(a.getContext()));
              if (access.contains(role)) rv = true;
            } catch (Exception e) {
              Log.warn(
                  this
                      + ".callViewAllPurposeItem() Hibernate cannot access user role for user id= "
                      + u.getId());
              return rv;
            }
          }
        }
      }
    }

    return rv;
  }
  @WebMethod
  @Path("/undeleteAssignments")
  @Produces("text/plain")
  @GET
  public String undeleteAssignments(
      @WebParam(name = "sessionId", partName = "sessionId") @QueryParam("sessionId")
          String sessionId,
      @WebParam(name = "context", partName = "context") @QueryParam("context") String context) {
    try {
      // establish the session
      Session s = establishSession(sessionId);

      Iterator assingments = assignmentService.getAssignmentsForContext(context);
      while (assingments.hasNext()) {
        Assignment ass = (Assignment) assingments.next();
        ResourceProperties rp = ass.getProperties();

        try {
          String deleted = rp.getProperty(ResourceProperties.PROP_ASSIGNMENT_DELETED);

          LOG.info("Assignment " + ass.getTitle() + " deleted status: " + deleted);
          if (deleted != null) {
            AssignmentEdit ae = assignmentService.editAssignment(ass.getId());
            ResourcePropertiesEdit rpe = ae.getPropertiesEdit();
            LOG.info("undeleting" + ass.getTitle() + " for site " + context);
            rpe.removeProperty(ResourceProperties.PROP_ASSIGNMENT_DELETED);

            assignmentService.commitEdit(ae);
          }
        } catch (IdUnusedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (PermissionException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (InUseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    } catch (Exception e) {
      LOG.error("WS undeleteAssignments(): " + e.getClass().getName() + " : " + e.getMessage());
      return e.getClass().getName() + " : " + e.getMessage();
    }

    return "success";
  }
  /** * Return list of AssignmentSubmissions, associated with this cell * for the current user */
  protected List getUserAssignments(CellFormBean cell) {
    ArrayList submissions = new ArrayList();
    try {
      Agent owner = cell.getCell().getWizardPage().getOwner();
      User user = UserDirectoryService.getUser(owner.getId().getValue());
      ArrayList assignments =
          AssignmentHelper.getSelectedAssignments(
              cell.getCell().getWizardPage().getPageDefinition().getAttachments());

      for (Iterator it = assignments.iterator(); it.hasNext(); ) {
        Assignment assign = (Assignment) it.next();
        AssignmentSubmission assignSubmission =
            AssignmentService.getSubmission(assign.getId(), user);
        if (assignSubmission != null) submissions.add(assignSubmission);
      }
    } catch (Exception e) {
      logger.warn(".getUserAssignments: " + e.toString());
    }

    return submissions;
  }
  /** Method called by the scheduledInvocationManager */
  public void execute(String opaqueContext) {
    try {
      // for group assignments, we need to have a user ID, otherwise, an exception is thrown:
      sessionManager.getCurrentSession().setUserEid("admin");
      sessionManager.getCurrentSession().setUserId("admin");
      Assignment assignment = assignmentService.getAssignment(opaqueContext);
      if (assignment.getAllowPeerAssessment() && !assignment.getDraft()) {
        int numOfReviews = assignment.getPeerAssessmentNumReviews();
        List<AssignmentSubmission> submissions =
            (List<AssignmentSubmission>) assignmentService.getSubmissions(assignment);
        // keep a map of submission ids to look up possible existing peer assessments
        Map<String, AssignmentSubmission> submissionIdMap =
            new HashMap<String, AssignmentSubmission>();
        // keep track of who has been assigned an assessment
        Map<String, Map<String, PeerAssessmentItem>> assignedAssessmentsMap =
            new HashMap<String, Map<String, PeerAssessmentItem>>();
        // keep track of how many assessor's each student has
        Map<String, Integer> studentAssessorsMap = new HashMap<String, Integer>();
        List<User> submitterUsersList =
            (List<User>) assignmentService.allowAddSubmissionUsers(assignment.getReference());
        List<String> submitterIdsList = new ArrayList<String>();
        if (submitterUsersList != null) {
          for (User u : submitterUsersList) {
            submitterIdsList.add(u.getId());
          }
        }
        // loop through the assignment submissions and setup the maps and lists
        for (AssignmentSubmission s : submissions) {
          if (s.getTimeSubmitted() != null
              // check if the submission is submitted, if not, see if there is any submission data
              // to review (i.e. draft was auto submitted)
              && (s.getSubmitted()
                  || ((s.getSubmittedText() != null && !"".equals(s.getSubmittedText().trim())
                      || (s.getSubmittedAttachments() != null
                          && s.getSubmittedAttachments().size() > 0))))
              && submitterIdsList.contains(s.getSubmitterId())
              && !"admin".equals(s.getSubmitterId())) {
            // only deal with users in the submitter's list
            submissionIdMap.put(s.getId(), s);
            assignedAssessmentsMap.put(
                s.getSubmitterId(), new HashMap<String, PeerAssessmentItem>());
            studentAssessorsMap.put(s.getSubmitterId(), 0);
          }
        }
        // this could be an update to an existing assessment... just make sure to grab any existing
        // review items first
        List<PeerAssessmentItem> existingItems =
            getPeerAssessmentItems(submissionIdMap.keySet(), assignment.getContent().getFactor());
        List<PeerAssessmentItem> removeItems = new ArrayList<PeerAssessmentItem>();
        // remove all empty items to start from scratch:
        for (Iterator iterator = existingItems.iterator(); iterator.hasNext(); ) {
          PeerAssessmentItem peerAssessmentItem = (PeerAssessmentItem) iterator.next();
          if (peerAssessmentItem.getScore() == null
              && (peerAssessmentItem.getComment() == null
                  || "".equals(peerAssessmentItem.getComment().trim()))) {
            removeItems.add(peerAssessmentItem);
            iterator.remove();
          }
        }
        if (removeItems.size() > 0) {
          getHibernateTemplate().deleteAll(removeItems);
        }
        // loop through the items and update the map values:
        for (PeerAssessmentItem p : existingItems) {
          if (submissionIdMap.containsKey(p.getSubmissionId())) {
            // first, add this assessment to the AssignedAssessmentsMap
            AssignmentSubmission s = submissionIdMap.get(p.getSubmissionId());
            // Next, increment the count for studentAssessorsMap
            Integer count = studentAssessorsMap.get(s.getSubmitterId());
            if (count == null) {
              // probably not possible, just check
              count = 0;
            }
            // check if the count is less than num of reviews before added another one,
            // otherwise, we need to delete this one (if it's empty)
            if (count < numOfReviews || p.getScore() != null || p.getComment() != null) {
              count++;
              studentAssessorsMap.put(s.getSubmitterId(), count);
              Map<String, PeerAssessmentItem> peerAssessments =
                  assignedAssessmentsMap.get(p.getAssessorUserId());
              if (peerAssessments == null) {
                // probably not possible, but just check
                peerAssessments = new HashMap<String, PeerAssessmentItem>();
              }
              peerAssessments.put(p.getSubmissionId(), p);
              assignedAssessmentsMap.put(p.getAssessorUserId(), peerAssessments);
            } else {
              // this shoudln't happen since the code above removes all empty assessments, but just
              // in case:
              getHibernateTemplate().delete(p);
            }
          } else {
            // this isn't realy possible since we looked up the peer assessments by submission id
            log.error(
                "AssignmentPeerAssessmentServiceImpl: found a peer assessment with an invalid session id: "
                    + p.getSubmissionId());
          }
        }

        // ok now that we have any existing assigned reviews accounted for, let's make sure that the
        // number of reviews is setup properly,
        // if not, add some
        // let's get a random order of submission IDs so we can have a random assigning algorithm
        List<String> randomSubmissionIds = new ArrayList<String>(submissionIdMap.keySet());
        Collections.shuffle(randomSubmissionIds);
        List<PeerAssessmentItem> newItems = new ArrayList<PeerAssessmentItem>();
        int i = 0;
        for (String submissionId : randomSubmissionIds) {
          AssignmentSubmission s = submissionIdMap.get(submissionId);
          // first find out how many existing items exist for this user:
          Integer assignedCount = studentAssessorsMap.get(s.getSubmitterId());
          // by creating a tailing list (snake style), we eliminate the issue where you can be stuck
          // with
          // a submission and the same submission user left, making for uneven distributions of
          // submission reviews
          List<String> snakeSubmissionList =
              new ArrayList<String>(randomSubmissionIds.subList(i, randomSubmissionIds.size()));
          if (i > 0) {
            snakeSubmissionList.addAll(new ArrayList<String>(randomSubmissionIds.subList(0, i)));
          }
          while (assignedCount < numOfReviews) {
            // we need to add more reviewers for this user's submission
            String lowestAssignedAssessor =
                findLowestAssignedAssessor(
                    assignedAssessmentsMap,
                    s.getSubmitterId(),
                    submissionId,
                    snakeSubmissionList,
                    submissionIdMap);
            if (lowestAssignedAssessor != null) {
              Map<String, PeerAssessmentItem> assessorsAssessmentMap =
                  assignedAssessmentsMap.get(lowestAssignedAssessor);
              if (assessorsAssessmentMap == null) {
                assessorsAssessmentMap = new HashMap<String, PeerAssessmentItem>();
              }
              PeerAssessmentItem newItem = new PeerAssessmentItem();
              newItem.setAssessorUserId(lowestAssignedAssessor);
              newItem.setSubmissionId(submissionId);
              newItem.setAssignmentId(assignment.getId());
              newItems.add(newItem);
              assessorsAssessmentMap.put(submissionId, newItem);
              assignedAssessmentsMap.put(lowestAssignedAssessor, assessorsAssessmentMap);
              // update this submission user's count:
              assignedCount++;
              studentAssessorsMap.put(submissionId, assignedCount);
            } else {
              break;
            }
          }
          i++;
        }
        if (newItems.size() > 0) {
          getHibernateTemplate().saveOrUpdateAll(newItems);
        }
      }
    } catch (IdUnusedException e) {
      log.error(e.getMessage(), e);
    } catch (PermissionException e) {
      log.error(e.getMessage(), e);
    } finally {
      sessionManager.getCurrentSession().setUserEid(null);
      sessionManager.getCurrentSession().setUserId(null);
    }
  }
  @WebMethod
  @Path("/getAssignmentsForContext")
  @Produces("text/plain")
  @GET
  public String getAssignmentsForContext(
      @WebParam(name = "sessionid", partName = "sessionid") @QueryParam("sessionid")
          String sessionid,
      @WebParam(name = "context", partName = "context") @QueryParam("context") String context) {

    try {
      // establish the session
      Session s = establishSession(sessionid);

      // ok will this give me a list of assignments for the course
      LOG.info("assignment list requested for " + context);

      Iterator assignments = assignmentService.getAssignmentsForContext(context);
      Document dom = Xml.createDocument();
      Node all = dom.createElement("assignments");
      dom.appendChild(all);

      while (assignments.hasNext()) {
        Assignment thisA = (Assignment) assignments.next();
        LOG.debug("got " + thisA.getTitle());
        if (!thisA.getDraft()) {
          AssignmentContent asCont = thisA.getContent();

          LOG.debug("about to start building xml doc");
          Element uElement = dom.createElement("assignment");
          uElement.setAttribute("id", thisA.getId());
          uElement.setAttribute("title", thisA.getTitle());
          LOG.debug("added title and id");
          if (asCont != null) {
            Integer temp = new Integer(asCont.getTypeOfGrade());
            String gType = temp.toString();
            uElement.setAttribute("gradeType", gType);
          }

          /* these need to be converted to strings
           */

          LOG.debug("About to get dates");

          Time dueTime = thisA.getDueTime();
          Time openTime = thisA.getOpenTime();
          Time closeTime = thisA.getCloseTime();
          LOG.debug("got dates");
          DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");

          if (openTime != null) {
            LOG.debug("open time is " + openTime.toString());
            uElement.setAttribute("openTime", format.format(new Date(openTime.getTime())));
          }
          if (closeTime != null) {
            LOG.debug("close time is " + closeTime.toString());
            uElement.setAttribute("closeTime", format.format(new Date(closeTime.getTime())));
          }

          if (dueTime != null) {
            LOG.debug("due time is " + dueTime.toString());
            uElement.setAttribute("dueTime", format.format(new Date(dueTime.getTime())));
          }

          LOG.debug("apending element to parent");
          all.appendChild(uElement);
        } else {
          LOG.debug("this is a draft assignment");
        }
      }
      String retVal = Xml.writeDocumentToString(dom);
      return retVal;
    } catch (Exception e) {
      LOG.error(
          "WS getAssignmentsForContext(): " + e.getClass().getName() + " : " + e.getMessage());
    }

    return "<assignments/ >";
  }
  /**
   * @param assignmentRef
   * @param associateGradebookAssignment
   * @param addUpdateRemoveAssignment
   * @param newAssignment_title
   * @param newAssignment_maxPoints
   * @param newAssignment_dueTime
   * @param submissionRef
   * @param updateRemoveSubmission
   * @param context
   */
  protected void integrateGradebook(
      String assignmentRef,
      String associateGradebookAssignment,
      String addUpdateRemoveAssignment,
      String newAssignment_title,
      int newAssignment_maxPoints,
      Time newAssignment_dueTime,
      String submissionRef,
      String updateRemoveSubmission,
      String context) {
    // add or remove external grades to gradebook
    // a. if Gradebook does not exists, do nothing, 'cos setting should have been hidden
    // b. if Gradebook exists, just call addExternal and removeExternal and swallow any exception.
    // The
    //    exception are indication that the assessment is already in the Gradebook or there is
    // nothing
    //    to remove.
    String gradebookUid = context;
    boolean gradebookExists = isGradebookDefined(context);

    String assignmentToolTitle = "Assignments";

    if (gradebookExists) {
      boolean isExternalAssignmentDefined =
          gradebookExternalAssessmentService.isExternalAssignmentDefined(
              gradebookUid, assignmentRef);
      boolean isExternalAssociateAssignmentDefined =
          gradebookExternalAssessmentService.isExternalAssignmentDefined(
              gradebookUid, associateGradebookAssignment);
      boolean isAssignmentDefined =
          gradebookService.isAssignmentDefined(gradebookUid, associateGradebookAssignment);

      if (addUpdateRemoveAssignment != null) {
        if (addUpdateRemoveAssignment.equals("add")
            || (addUpdateRemoveAssignment.equals("update")
                && !gradebookService.isAssignmentDefined(gradebookUid, newAssignment_title))) {
          // add assignment into gradebook
          try {
            // add assignment to gradebook
            gradebookExternalAssessmentService.addExternalAssessment(
                gradebookUid,
                assignmentRef,
                null,
                newAssignment_title,
                newAssignment_maxPoints / 10,
                new Date(newAssignment_dueTime.getTime()),
                "Assignment");
          } catch (AssignmentHasIllegalPointsException e) {
            // addAlert(state, rb.getString("addtogradebook.illegalPoints"));
          } catch (ConflictingAssignmentNameException e) {
            // try to modify assignment title, make sure there is no such assignment in the
            // gradebook, and insert again
            boolean trying = true;
            int attempts = 1;
            String titleBase = newAssignment_title;
            while (trying
                && attempts < MAXIMUM_ATTEMPTS_FOR_UNIQUENESS) // see end of loop for condition that
            // enforces attempts <= limit)
            {
              String newTitle = titleBase + "-" + attempts;

              if (!gradebookService.isAssignmentDefined(gradebookUid, newTitle)) {
                try {
                  // add assignment to gradebook
                  gradebookExternalAssessmentService.addExternalAssessment(
                      gradebookUid,
                      assignmentRef,
                      null,
                      newTitle,
                      newAssignment_maxPoints / 10,
                      new Date(newAssignment_dueTime.getTime()),
                      "Assignment");
                  trying = false;
                } catch (Exception ee) {
                  // try again, ignore the exception
                }
              }

              if (trying) {
                attempts++;
                if (attempts >= MAXIMUM_ATTEMPTS_FOR_UNIQUENESS) {
                  // add alert prompting for change assignment title
                  // addAlert(state, rb.getString("addtogradebook.nonUniqueTitle"));
                }
              }
            }
          } catch (ConflictingExternalIdException e) {
            // ignore
          } catch (GradebookNotFoundException e) {
            // ignore
          } catch (Exception e) {
            // ignore
          }
        } // (addUpdateRemoveAssignment.equals("add") || (
        // addUpdateRemoveAssignment.equals("update") && !g.isAssignmentDefined(gradebookUid,
        // newAssignment_title)))
      } // addUpdateRemoveAssignment != null

      if (updateRemoveSubmission != null) {
        try {
          Assignment a = assignmentService.getAssignment(assignmentRef);

          if (updateRemoveSubmission.equals("update")
              && a.getProperties().getProperty(NEW_ASSIGNMENT_ADD_TO_GRADEBOOK) != null
              && !a.getProperties()
                  .getProperty(NEW_ASSIGNMENT_ADD_TO_GRADEBOOK)
                  .equals(AssignmentService.GRADEBOOK_INTEGRATION_NO)
              && a.getContent().getTypeOfGrade() == Assignment.SCORE_GRADE_TYPE) {
            if (submissionRef == null) {
              // bulk add all grades for assignment into gradebook
              Iterator submissions = assignmentService.getSubmissions(a).iterator();

              Map m = new HashMap();

              // any score to copy over? get all the assessmentGradingData and copy over
              while (submissions.hasNext()) {
                AssignmentSubmission aSubmission = (AssignmentSubmission) submissions.next();
                if (aSubmission.getGradeReleased()) {
                  User[] submitters = aSubmission.getSubmitters();
                  String submitterId = submitters[0].getId();
                  String gradeString = StringUtils.trimToNull(aSubmission.getGrade(false));
                  Double grade =
                      gradeString != null ? Double.valueOf(displayGrade(gradeString)) : null;
                  m.put(submitterId, grade);
                }
              }

              // need to update only when there is at least one submission
              if (m.size() > 0) {
                if (associateGradebookAssignment != null) {
                  if (isExternalAssociateAssignmentDefined) {
                    // the associated assignment is externally maintained
                    gradebookExternalAssessmentService.updateExternalAssessmentScores(
                        gradebookUid, associateGradebookAssignment, m);
                  } else if (isAssignmentDefined) {
                    // the associated assignment is internal one, update records one by one
                    submissions = assignmentService.getSubmissions(a).iterator();
                    while (submissions.hasNext()) {
                      AssignmentSubmission aSubmission = (AssignmentSubmission) submissions.next();
                      User[] submitters = aSubmission.getSubmitters();
                      String submitterId = submitters[0].getId();
                      String gradeString = StringUtils.trimToNull(aSubmission.getGrade(false));
                      String grade =
                          (gradeString != null && aSubmission.getGradeReleased())
                              ? displayGrade(gradeString)
                              : null;
                      gradebookService.setAssignmentScoreString(
                          gradebookUid,
                          associateGradebookAssignment,
                          submitterId,
                          grade,
                          assignmentToolTitle);
                    }
                  }
                } else if (isExternalAssignmentDefined) {
                  gradebookExternalAssessmentService.updateExternalAssessmentScores(
                      gradebookUid, assignmentRef, m);
                }
              }
            } else {
              try {
                // only update one submission
                AssignmentSubmission aSubmission =
                    (AssignmentSubmission) assignmentService.getSubmission(submissionRef);
                User[] submitters = aSubmission.getSubmitters();
                String gradeString = StringUtils.trimToNull(aSubmission.getGrade(false));

                if (associateGradebookAssignment != null) {
                  if (gradebookExternalAssessmentService.isExternalAssignmentDefined(
                      gradebookUid, associateGradebookAssignment)) {
                    // the associated assignment is externally maintained
                    gradebookExternalAssessmentService.updateExternalAssessmentScore(
                        gradebookUid,
                        associateGradebookAssignment,
                        submitters[0].getId(),
                        (gradeString != null && aSubmission.getGradeReleased())
                            ? displayGrade(gradeString)
                            : null);
                  } else if (gradebookService.isAssignmentDefined(
                      gradebookUid, associateGradebookAssignment)) {
                    // the associated assignment is internal one, update records
                    gradebookService.setAssignmentScoreString(
                        gradebookUid,
                        associateGradebookAssignment,
                        submitters[0].getId(),
                        (gradeString != null && aSubmission.getGradeReleased())
                            ? displayGrade(gradeString)
                            : null,
                        assignmentToolTitle);
                  }
                } else {
                  gradebookExternalAssessmentService.updateExternalAssessmentScore(
                      gradebookUid,
                      assignmentRef,
                      submitters[0].getId(),
                      (gradeString != null && aSubmission.getGradeReleased())
                          ? displayGrade(gradeString)
                          : null);
                }
              } catch (Exception e) {
                LOG.warn("Cannot find submission " + submissionRef + ": " + e.getMessage());
              }
            } // submissionref != null

          } else if (updateRemoveSubmission.equals("remove")) {
            if (submissionRef == null) {
              // remove all submission grades (when changing the associated entry in Gradebook)
              Iterator submissions = assignmentService.getSubmissions(a).iterator();

              // any score to copy over? get all the assessmentGradingData and copy over
              while (submissions.hasNext()) {
                AssignmentSubmission aSubmission = (AssignmentSubmission) submissions.next();
                User[] submitters = aSubmission.getSubmitters();
                if (isExternalAssociateAssignmentDefined) {
                  // if the old associated assignment is an external maintained one
                  gradebookExternalAssessmentService.updateExternalAssessmentScore(
                      gradebookUid, associateGradebookAssignment, submitters[0].getId(), null);
                } else if (isAssignmentDefined) {
                  gradebookService.setAssignmentScoreString(
                      gradebookUid,
                      associateGradebookAssignment,
                      submitters[0].getId(),
                      null,
                      assignmentToolTitle);
                }
              }
            } else {
              // remove only one submission grade
              try {
                AssignmentSubmission aSubmission =
                    (AssignmentSubmission) assignmentService.getSubmission(submissionRef);
                User[] submitters = aSubmission.getSubmitters();
                gradebookExternalAssessmentService.updateExternalAssessmentScore(
                    gradebookUid, assignmentRef, submitters[0].getId(), null);
              } catch (Exception e) {
                LOG.warn("Cannot find submission " + submissionRef + ": " + e.getMessage());
              }
            }
          }
        } catch (Exception e) {
          LOG.warn("Cannot find assignment: " + assignmentRef + ": " + e.getMessage());
        }
      } // updateRemoveSubmission != null
    } // if gradebook exists
  } // integrateGradebook
  @WebMethod
  @Path("/setAssignmentGradeCommentforUser")
  @Produces("text/plain")
  @GET
  public String setAssignmentGradeCommentforUser(
      @WebParam(name = "sessionId", partName = "sessionId") @QueryParam("sessionId")
          String sessionId,
      @WebParam(name = "assignmentId", partName = "assignmentId") @QueryParam("assignmentId")
          String assignmentId,
      @WebParam(name = "userId", partName = "userId") @QueryParam("userId") String userId,
      @WebParam(name = "comment", partName = "comment") @QueryParam("comment") String comment,
      @WebParam(name = "grade", partName = "grade") @QueryParam("grade") String grade) {
    // establish the session

    try {
      Session s = establishSession(sessionId);

      LOG.info(
          "User "
              + s.getUserEid()
              + " setting assignment grade/comment for "
              + userId
              + " on "
              + assignmentId
              + " to "
              + grade);

      User user = userDirectoryService.getUserByEid(userId);
      if (user == null) {
        return "user does not exist";
      }

      Assignment assign = assignmentService.getAssignment(assignmentId);
      String aReference = assign.getReference();

      if (!securityService.unlock(
          AssignmentService.SECURE_GRADE_ASSIGNMENT_SUBMISSION, aReference)) {
        LOG.warn("User " + s.getUserEid() + " does not have permission to set assignment grades");
        return "failure: no permission";
      }

      LOG.info(
          "Setting assignment grade/comment for "
              + userId
              + " on "
              + assignmentId
              + " to "
              + grade);

      AssignmentSubmission sub = assignmentService.getSubmission(assignmentId, user);
      AssignmentSubmissionEdit asEdit = null;
      String context = assign.getContext();

      if (sub == null) {
        asEdit = assignmentService.addSubmission(context, assignmentId, user.getId());
      } else {
        asEdit = assignmentService.editSubmission(sub.getReference());
      }

      asEdit.setFeedbackComment(comment);
      asEdit.setGrade(grade);
      asEdit.setGraded(true);
      asEdit.setGradeReleased(true);
      assignmentService.commitEdit(asEdit);

      // If necessary, update the assignment grade in the Gradebook

      String sReference = asEdit.getReference();

      String associateGradebookAssignment =
          StringUtils.trimToNull(
              assign
                  .getProperties()
                  .getProperty(AssignmentService.PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT));

      // update grade in gradebook
      integrateGradebook(
          aReference,
          associateGradebookAssignment,
          null,
          null,
          -1,
          null,
          sReference,
          "update",
          context);

    } catch (Exception e) {
      LOG.error(
          "WS setAssignmentGradeCommentforUser(): Exception while setting assignment grade/comment for "
              + userId
              + " on "
              + assignmentId
              + " to "
              + grade,
          e);
      return e.getClass().getName() + " : " + e.getMessage();
    }

    return "success";
  }