/** {@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;
 }
Ejemplo n.º 2
0
  /**
   * @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