[O] Cache assignment types

This commit is contained in:
Hykilpikonna
2019-11-09 20:39:24 -05:00
parent 8c9f0a0e83
commit ae94f54a7b
+29 -22
View File
@@ -222,36 +222,43 @@ export default class Course
return this._cacheNumericGrade; return this._cacheNumericGrade;
} }
private _cacheAssignmentTypes: AssignmentType[];
/** /**
* Get assignment types * Get assignment types
*/ */
get assignmentTypes(): AssignmentType[] get assignmentTypes(): AssignmentType[]
{ {
// Get all types if (this._cacheAssignmentTypes == null)
let types = this.assignments.map(a => a.type);
// Remove duplicates
types = types.filter((type, i, a) => a.indexOf(type) == i);
// Get total possible score for weight calculation
let totalScoreMax = this.assignments.reduce((sum, a) => sum + a.scoreMax, 0);
// For every type...
return types.map(type =>
{ {
// Get assignments of the type // Get all types
let typeAssignments = this.assignments.filter(a => a.type == type); let types = this.assignments.map(a => a.type);
// Count scores and max scores // Remove duplicates
let score = typeAssignments.reduce((sum, a) => sum + a.score, 0); types = types.filter((type, i, a) => a.indexOf(type) == i);
let scoreMax = typeAssignments.reduce((sum, a) => sum + a.scoreMax, 0);
// Calculate weight // Get total possible score for weight calculation
let weight = this.grading.method == 'PERCENT_TYPE' let totalScoreMax = this.assignments.reduce((sum, a) => sum + a.scoreMax, 0);
? this.grading.weightingMap[type] : scoreMax / totalScoreMax;
// Return // For every type...
return {name: type, id: typeAssignments[0].typeId, weight: weight, scoreMax: scoreMax, score: score} this._cacheAssignmentTypes = types.map(type =>
}) {
// Get assignments of the type
let typeAssignments = this.assignments.filter(a => a.type == type);
// Count scores and max scores
let score = typeAssignments.reduce((sum, a) => sum + a.score, 0);
let scoreMax = typeAssignments.reduce((sum, a) => sum + a.scoreMax, 0);
// Calculate weight
let weight = this.grading.method == 'PERCENT_TYPE'
? this.grading.weightingMap[type] : scoreMax / totalScoreMax;
// Return
return {name: type, id: typeAssignments[0].typeId, weight: weight, scoreMax: scoreMax, score: score}
})
}
return this._cacheAssignmentTypes;
} }
} }