/** {@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("/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";
  }