Ejemplo n.º 1
0
 @SuppressWarnings("unchecked")
 public void sortDown() {
   Long assignmentId = getAssignmentIdFromParam();
   if (logger.isDebugEnabled()) {
     logger.debug("Sort: sorting down: " + assignmentId);
   }
   List<Assignment> assignments = getGradebookManager().getAssignments(getGradebookId());
   if (assignments.size() > 1) {
     ensureAssignmentsSorted(assignments, GradableObject.sortingComparator, true);
     // now adjust the numbering
     for (int i = 0; i < assignments.size(); i++) {
       Assignment a1 = assignments.get(i);
       if (a1.getId().equals(assignmentId)) {
         if (i < (assignments.size() - 1)) {
           Assignment a2 = assignments.get(i + 1);
           // only swap items which are in the same category
           if ((a1.getCategory() == null && a2.getCategory() == null)
               || (a1.getCategory().equals(a2.getCategory()))) {
             // swap the ordering of this item and the item below it
             Integer holder = a1.getSortOrder();
             a1.setSortOrder(a2.getSortOrder());
             a2.setSortOrder(holder);
             logger.info(
                 "Sort: DOWN swapping: "
                     + a1.getId()
                     + " (to "
                     + a1.getSortOrder()
                     + " from "
                     + holder
                     + ") with "
                     + a2.getId());
             // save the new orders
             getGradebookManager().updateAssignment(a1);
             getGradebookManager().updateAssignment(a2);
           } else {
             logger.info(
                 "Sort: DOWN: unable to swap items ("
                     + a1.getId()
                     + ","
                     + a2.getId()
                     + ") in different categories");
           }
         }
         break;
       }
     }
   }
 }
Ejemplo n.º 2
0
  /**
   * If we came to the instructor view from the assign details page, we need to set the previous and
   * next student info according to the order and filter on the assign details page
   *
   * @return
   */
  private List getOrderedEnrolleesFromAssignDetailsPage() {
    setSortColumn(getPreferencesBean().getAssignmentDetailsTableSortColumn());
    setSortAscending(getPreferencesBean().isAssignmentDetailsTableSortAscending());

    List assignGradeRecords = new ArrayList();
    List enrollments = new ArrayList();

    Long assignmentIdAsLong = getAssignmentIdAsLong();
    if (assignmentIdAsLong != null) {
      Assignment prevAssignment = getGradebookManager().getAssignment(assignmentIdAsLong);
      Category category = prevAssignment.getCategory();
      Long catId = null;
      if (category != null) catId = category.getId();

      Map enrollmentMap = getOrderedStudentIdEnrollmentMapForItem(catId);
      if (isEnrollmentSort()) {
        return new ArrayList(enrollmentMap.values());
      }

      List studentUids = new ArrayList(enrollmentMap.keySet());

      if (getGradeEntryByPoints())
        assignGradeRecords =
            getGradebookManager().getAssignmentGradeRecords(prevAssignment, studentUids);
      else if (getGradeEntryByPercent() || getGradeEntryByLetter())
        assignGradeRecords =
            getGradebookManager().getAssignmentGradeRecordsConverted(prevAssignment, studentUids);

      // Need to sort and page based on a scores column.
      List scoreSortedStudentUids = new ArrayList();
      for (Iterator iter = assignGradeRecords.iterator(); iter.hasNext(); ) {
        AbstractGradeRecord agr = (AbstractGradeRecord) iter.next();
        scoreSortedStudentUids.add(agr.getStudentId());
      }

      // Put enrollments with no scores at the beginning of the final list.
      studentUids.removeAll(scoreSortedStudentUids);

      // Add all sorted enrollments with scores into the final list
      studentUids.addAll(scoreSortedStudentUids);

      studentUids = finalizeSortingAndPaging(studentUids);

      if (studentUids != null) {
        Iterator studentIter = studentUids.iterator();
        while (studentIter.hasNext()) {
          String studentId = (String) studentIter.next();
          EnrollmentRecord enrollee = (EnrollmentRecord) enrollmentMap.get(studentId);
          if (enrollee != null) enrollments.add(enrollee);
        }
      }
    }

    return enrollments;
  }
Ejemplo n.º 3
0
  @SuppressWarnings("unchecked")
  private void ensureAssignmentsSorted(List assignments, Comparator comparator, boolean save) {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "ensureAssignmentsSorted: comparator="
              + comparator
              + ", save="
              + save
              + ", assignments= "
              + Arrays.toString(assignments.toArray()));
    }
    // remove any non-assignments first
    List gradeables = new ArrayList();
    for (Iterator iterator = assignments.iterator(); iterator.hasNext(); ) {
      GradableObject go = (GradableObject) iterator.next();
      if (!(go instanceof Assignment)) { // ! go.isAssignment()) {
        gradeables.add(go);
        iterator.remove();
      }
    }
    Collections.sort(gradeables, GradableObject.nameComparator);

    // put everything in the established sort order first (if needed)
    if (comparator == null) {
      comparator = GradableObject.dateComparator;
      if (logger.isDebugEnabled()) {
        logger.debug("ensureAssignmentsSorted: setting default comparator=" + comparator);
      }
    }
    if (!NoChangeMarkerComparator.class.isAssignableFrom(comparator.getClass())) {
      // only sort if this is not the no-sort marker
      if (logger.isDebugEnabled()) {
        logger.debug("ensureAssignmentsSorted: sorting with comparator=" + comparator);
      }
      Collections.sort(assignments, comparator);
    } else {
      if (logger.isDebugEnabled()) {
        logger.debug(
            "ensureAssignmentsSorted: no sort, using NoChangeMarkerComparator=" + comparator);
      }
    }
    // always need to sort by category
    Collections.sort(assignments, GradableObject.categoryComparator);
    // now ensure the numbering is set and correct
    int saveCount = 0;
    int updateCount = 0;
    for (int i = 0; i < assignments.size(); i++) {
      Assignment assignment = (Assignment) assignments.get(i);
      Integer curOrder = assignment.getSortOrder();
      if (logger.isDebugEnabled()) {
        logger.debug(
            "ensureAssignmentsSorted: checking if current order ("
                + curOrder
                + ") matches correct order ("
                + i
                + ") for assignment: "
                + assignment);
      }
      if (curOrder == null || i != curOrder.intValue()) {
        // no match so we need to update it (else it is already set correctly, only save if needed)
        assignment.setSortOrder(i);
        updateCount++;
        if (logger.isDebugEnabled()) {
          logger.debug(
              "ensureAssignmentsSorted: setting sort order ("
                  + i
                  + ") for assignment: "
                  + assignment);
        }
        if (save) {
          getGradebookManager().updateAssignment(assignment);
          saveCount++;
          if (logger.isDebugEnabled()) {
            logger.debug("ensureAssignmentsSorted: saving assignment: " + assignment);
          }
        }
      }
    }

    // set the ordering up in the assignment with support for categories
    Map<String, List<Assignment>> categoryAssignments =
        new LinkedHashMap<String, List<Assignment>>();
    for (Assignment assignment : (List<Assignment>) assignments) {
      String category = "NULL";
      if (assignment.getCategory() != null) {
        category = assignment.getCategory().getName();
      }
      if (!categoryAssignments.containsKey(category)) {
        categoryAssignments.put(category, new ArrayList<Assignment>());
      }
      categoryAssignments.get(category).add(assignment);
      // assignment.assignSorting(assignments.size(), i);
    }
    for (Entry<String, List<Assignment>> entry : categoryAssignments.entrySet()) {
      List<Assignment> l = entry.getValue();
      for (int i = 0; i < l.size(); i++) {
        Assignment assignment = l.get(i);
        // assign the counter for ordering
        assignment.assignSorting(l.size(), i);
        if (logger.isDebugEnabled()) {
          logger.debug("ensureAssignmentsSorted: ordered: " + i + " : " + assignment);
        }
      }
    }
    // add back in the gradeables to the end
    for (Object gradeable : gradeables) {
      assignments.add(gradeable);
    }
    if (logger.isDebugEnabled()) {
      logger.debug(
          "ensureAssignmentsSorted: sorted assignments (updated="
              + updateCount
              + ", saved="
              + saveCount
              + "): "
              + Arrays.toString(assignments.toArray()));
    }
  }
  public void testCreateExternalAssessmentWithCategory() throws Exception {
    String categoryName = "My favorite category";
    Long gbId = gradebookManager.getGradebook(GRADEBOOK_UID).getId();
    Long catId = gradebookManager.createCategory(gbId, categoryName, new Double(0), 0, 0, 0, false);
    Assert.assertTrue(gradebookFrameworkService.isGradebookDefined(GRADEBOOK_UID));

    // Make sure the service knows that the external id has not been defined
    Assert.assertFalse(
        gradebookExternalAssessmentService.isExternalAssignmentDefined(GRADEBOOK_UID, EXT_ID_1));

    gradebookExternalAssessmentService.addExternalAssessment(
        GRADEBOOK_UID,
        EXT_ID_1,
        null,
        EXT_TITLE_1,
        new Double(10),
        new Date(),
        "Samigo",
        new Boolean(false),
        catId);

    // Make sure the service knows that the external id has been defined
    Assert.assertTrue(
        gradebookExternalAssessmentService.isExternalAssignmentDefined(GRADEBOOK_UID, EXT_ID_1));

    // let's retrieve the assignment and double check the category setting
    // due to security in GradebookService, we need to retrieve all assign and then iterate through
    // to find this one
    List<Assignment> allAssigns = gradebookManager.getAssignments(gbId);
    boolean assignFound = false;
    for (Assignment assign : allAssigns) {
      if (assign.isExternallyMaintained() && EXT_ID_1.equals(assign.getExternalId())) {
        assignFound = true;
        // double check the category
        if (assign.getCategory() == null || !catId.equals(assign.getCategory().getId())) {
          fail("Category was not saved via addExternalAssessment");
        }
      }
    }

    if (!assignFound) {
      fail("Could not retrieve newly added external assignment");
    }

    // double check that leaving category null leaves category unassigned
    String externalId2 = "external ID 2";
    String externalTitle = "Another externally maintained one";
    gradebookExternalAssessmentService.addExternalAssessment(
        GRADEBOOK_UID,
        externalId2,
        null,
        externalTitle,
        new Double(10),
        new Date(),
        "Samigo",
        new Boolean(false),
        null);
    // Make sure the service knows that the external id has been defined
    Assert.assertTrue(
        gradebookExternalAssessmentService.isExternalAssignmentDefined(GRADEBOOK_UID, externalId2));
    // let's retrieve the assignment and double check the category setting
    // due to security in GradebookService, we need to retrieve all assign and then iterate through
    // to find this one
    allAssigns = gradebookManager.getAssignments(gbId);
    assignFound = false;
    for (Assignment assign : allAssigns) {
      if (assign.isExternallyMaintained() && externalId2.equals(assign.getExternalId())) {
        assignFound = true;
        // double check the category
        if (assign.getCategory() != null) {
          fail("Category should be null for external assignment added via addExternalAssessment");
        }
      }
    }

    if (!assignFound) {
      fail("Could not retrieve newly added external assignment without a category");
    }

    // Make sure that internal name conflicts are detected
    try {
      gradebookExternalAssessmentService.addExternalAssessment(
          GRADEBOOK_UID,
          "A unique external id",
          null,
          ASN_1,
          new Double(10),
          new Date(),
          "Samigo",
          new Boolean(false),
          catId);
      fail();
    } catch (ConflictingAssignmentNameException e) {
    }

    // Make sure that external name conflicts are detected
    try {
      gradebookExternalAssessmentService.addExternalAssessment(
          GRADEBOOK_UID,
          "Another unique external id",
          null,
          EXT_TITLE_1,
          new Double(10),
          new Date(),
          "Samigo",
          new Boolean(false),
          catId);
      fail();
    } catch (ConflictingAssignmentNameException e) {
    }

    // Make sure that external id conflicts are detected
    try {
      gradebookExternalAssessmentService.addExternalAssessment(
          GRADEBOOK_UID,
          EXT_ID_1,
          null,
          "A unique title",
          new Double(10),
          new Date(),
          "Samigo",
          new Boolean(false),
          catId);
      fail();
    } catch (ConflictingExternalIdException e) {
    }

    // Test a floating value.
    double floatingPoints = 10.66666;
    String floatingExtId = "Just another external ID";
    gradebookExternalAssessmentService.addExternalAssessment(
        GRADEBOOK_UID,
        floatingExtId,
        null,
        "AFractionalAssessment",
        new Double(floatingPoints),
        new Date(),
        "Samigo",
        new Boolean(false),
        catId);

    // Find the assessment and ensure that it has been updated
    Assignment asn = null;
    List assignments = gradebookManager.getAssignments(gbId);
    for (Iterator iter = assignments.iterator(); iter.hasNext(); ) {
      Assignment tmp = (Assignment) iter.next();
      if (tmp.getExternalId() != null && tmp.getExternalId().equals(floatingExtId)) {
        asn = tmp;
        break;
      }
    }
    Assert.assertEquals(asn.getPointsPossible(), new Double(floatingPoints));
  }