32
32
import java .util .List ;
33
33
import java .util .Map ;
34
34
import java .util .Map .Entry ;
35
+ import java .util .Optional ;
35
36
import java .util .Set ;
36
37
import java .util .stream .Collectors ;
37
38
58
59
import org .sakaiproject .service .gradebook .shared .AssessmentNotFoundException ;
59
60
import org .sakaiproject .service .gradebook .shared .AssignmentHasIllegalPointsException ;
60
61
import org .sakaiproject .service .gradebook .shared .CategoryDefinition ;
62
+ import org .sakaiproject .service .gradebook .shared .CategoryScoreData ;
61
63
import org .sakaiproject .service .gradebook .shared .CommentDefinition ;
62
64
import org .sakaiproject .service .gradebook .shared .ConflictingAssignmentNameException ;
63
65
import org .sakaiproject .service .gradebook .shared .ConflictingCategoryNameException ;
@@ -3132,7 +3134,7 @@ public List<GradingEvent> getGradingEvents(final String studentId, final long as
3132
3134
}
3133
3135
3134
3136
@ Override
3135
- public Double calculateCategoryScore (final Object gradebook , final String studentUuid , final CategoryDefinition category , final List <org .sakaiproject .service .gradebook .shared .Assignment > categoryAssignments , final Map <Long ,String > gradeMap ) {
3137
+ public Optional < CategoryScoreData > calculateCategoryScore (final Object gradebook , final String studentUuid , final CategoryDefinition category , final List <org .sakaiproject .service .gradebook .shared .Assignment > categoryAssignments , final Map <Long ,String > gradeMap ) {
3136
3138
3137
3139
final Gradebook gb = (Gradebook ) gradebook ;
3138
3140
@@ -3176,6 +3178,7 @@ public Double calculateCategoryScore(final Object gradebook, final String studen
3176
3178
a .setRemoved (false ); //shared.GradebookAssignment doesn't include removed so this will always be false
3177
3179
a .setGradebook (gb );
3178
3180
a .setCategory (c );
3181
+ a .setId (assignment .getId ()); // store the id so we can find out later which grades were dropped, if any
3179
3182
3180
3183
//create the AGR
3181
3184
final AssignmentGradeRecord gradeRecord = new AssignmentGradeRecord (a , studentUuid , grade );
@@ -3187,7 +3190,7 @@ public Double calculateCategoryScore(final Object gradebook, final String studen
3187
3190
}
3188
3191
3189
3192
@ Override
3190
- public Double calculateCategoryScore (final Long gradebookId , final String studentUuid , final Long categoryId ) {
3193
+ public Optional < CategoryScoreData > calculateCategoryScore (final Long gradebookId , final String studentUuid , final Long categoryId ) {
3191
3194
3192
3195
//get all grade records for the student
3193
3196
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -3208,22 +3211,22 @@ public Object doInHibernate(final Session session) throws HibernateException {
3208
3211
/**
3209
3212
* Does the heavy lifting for the category calculations.
3210
3213
* Requires the List of AssignmentGradeRecord so that we can applyDropScores.
3211
- * @param studentUuid the studnet uuid
3212
- * @param categoryId the cateogry id we are interested in
3214
+ * @param studentUuid the student uuid
3215
+ * @param categoryId the category id we are interested in
3213
3216
* @param gradeRecords all grade records for the student
3214
3217
* @return
3215
3218
*/
3216
- private Double calculateCategoryScore (final String studentUuid , final Long categoryId , final List <AssignmentGradeRecord > gradeRecords ) {
3219
+ private Optional < CategoryScoreData > calculateCategoryScore (final String studentUuid , final Long categoryId , final List <AssignmentGradeRecord > gradeRecords ) {
3217
3220
3218
3221
//validate
3219
3222
if (gradeRecords == null ) {
3220
3223
log .debug ("No grade records for student: {}. Nothing to do." , studentUuid );
3221
- return null ;
3224
+ return Optional . empty () ;
3222
3225
}
3223
3226
3224
3227
if (categoryId == null ) {
3225
3228
log .debug ("No category supplied, nothing to do." );
3226
- return null ;
3229
+ return Optional . empty () ;
3227
3230
}
3228
3231
3229
3232
//setup
@@ -3235,6 +3238,13 @@ private Double calculateCategoryScore(final String studentUuid, final Long categ
3235
3238
// apply any drop/keep settings for this category
3236
3239
applyDropScores (gradeRecords );
3237
3240
3241
+ // find the records marked as dropped (highest/lowest) before continuing,
3242
+ // as gradeRecords will be modified in place after this and these records will be removed
3243
+ List <Long > droppedItemIds = gradeRecords .stream ()
3244
+ .filter (AssignmentGradeRecord ::getDroppedFromGrade )
3245
+ .map (agr -> agr .getAssignment ().getId ())
3246
+ .collect (Collectors .toList ());
3247
+
3238
3248
// Since all gradeRecords for the student are passed in, not just for this category,
3239
3249
// plus they may not meet the criteria for including in the calculation,
3240
3250
// this list is filtered down according to the following rules:
@@ -3271,7 +3281,7 @@ private Double calculateCategoryScore(final String studentUuid, final Long categ
3271
3281
// pre-calculation
3272
3282
// Rule 1. If category only has a single EC item, don't try to calculate category total.
3273
3283
if (gradeRecords .size () == 1 && gradeRecords .get (0 ).getAssignment ().isExtraCredit ()) {
3274
- return null ;
3284
+ return Optional . empty () ;
3275
3285
}
3276
3286
3277
3287
//iterate the filtered list and set the variables for the calculation
@@ -3295,11 +3305,11 @@ private Double calculateCategoryScore(final String studentUuid, final Long categ
3295
3305
}
3296
3306
3297
3307
if (numScored == 0 || numOfAssignments == 0 || totalPossible .doubleValue () == 0 ) {
3298
- return null ;
3308
+ return Optional . empty () ;
3299
3309
}
3300
3310
3301
3311
final BigDecimal mean = totalEarned .divide (new BigDecimal (numScored ), GradebookService .MATH_CONTEXT ).divide ((totalPossible .divide (new BigDecimal (numOfAssignments ), GradebookService .MATH_CONTEXT )), GradebookService .MATH_CONTEXT ).multiply (new BigDecimal ("100" ));
3302
- return mean .doubleValue ();
3312
+ return Optional . of ( new CategoryScoreData ( mean .doubleValue (), droppedItemIds ) );
3303
3313
}
3304
3314
3305
3315
@ Override
0 commit comments