1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-11-13 22:50:27 -06:00

Add checking vulcan average from both semesters (#1140)

This commit is contained in:
Rafał Borcz 2021-02-13 12:33:53 +01:00 committed by GitHub
parent dd085a14fa
commit 5e3b89636f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 15 deletions

View File

@ -86,7 +86,11 @@ class GradeAverageProvider @Inject constructor(
return@combine firstSemesterGradeSubject return@combine firstSemesterGradeSubject
} }
val isAnyAverage = secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 } val isAnyVulcanAverageInFirstSemester =
firstSemesterGradeSubject.data.orEmpty().any { it.average != .0 }
val isAnyVulcanAverageInSecondSemester =
secondSemesterGradeSubject.data.orEmpty().any { it.average != .0 }
val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject -> val updatedData = secondSemesterGradeSubject.data?.map { secondSemesterSubject ->
val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty() val firstSemesterSubject = firstSemesterGradeSubject.data.orEmpty()
.singleOrNull { it.subject == secondSemesterSubject.subject } .singleOrNull { it.subject == secondSemesterSubject.subject }
@ -94,7 +98,7 @@ class GradeAverageProvider @Inject constructor(
val updatedAverage = if (averageMode == ALL_YEAR) { val updatedAverage = if (averageMode == ALL_YEAR) {
calculateAllYearAverage( calculateAllYearAverage(
student = student, student = student,
isAnyAverage = isAnyAverage, isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
gradeAverageForceCalc = gradeAverageForceCalc, gradeAverageForceCalc = gradeAverageForceCalc,
secondSemesterSubject = secondSemesterSubject, secondSemesterSubject = secondSemesterSubject,
firstSemesterSubject = firstSemesterSubject firstSemesterSubject = firstSemesterSubject
@ -102,7 +106,7 @@ class GradeAverageProvider @Inject constructor(
} else { } else {
calculateBothSemestersAverage( calculateBothSemestersAverage(
student = student, student = student,
isAnyAverage = isAnyAverage, isAnyVulcanAverage = isAnyVulcanAverageInFirstSemester || isAnyVulcanAverageInSecondSemester,
gradeAverageForceCalc = gradeAverageForceCalc, gradeAverageForceCalc = gradeAverageForceCalc,
secondSemesterSubject = secondSemesterSubject, secondSemesterSubject = secondSemesterSubject,
firstSemesterSubject = firstSemesterSubject firstSemesterSubject = firstSemesterSubject
@ -116,11 +120,11 @@ class GradeAverageProvider @Inject constructor(
private fun calculateAllYearAverage( private fun calculateAllYearAverage(
student: Student, student: Student,
isAnyAverage: Boolean, isAnyVulcanAverage: Boolean,
gradeAverageForceCalc: Boolean, gradeAverageForceCalc: Boolean,
secondSemesterSubject: GradeSubject, secondSemesterSubject: GradeSubject,
firstSemesterSubject: GradeSubject? firstSemesterSubject: GradeSubject?
) = if (!isAnyAverage || gradeAverageForceCalc) { ) = if (!isAnyVulcanAverage || gradeAverageForceCalc) {
val updatedSecondSemesterGrades = val updatedSecondSemesterGrades =
secondSemesterSubject.grades.updateModifiers(student) secondSemesterSubject.grades.updateModifiers(student)
val updatedFirstSemesterGrades = val updatedFirstSemesterGrades =
@ -133,20 +137,23 @@ class GradeAverageProvider @Inject constructor(
private fun calculateBothSemestersAverage( private fun calculateBothSemestersAverage(
student: Student, student: Student,
isAnyAverage: Boolean, isAnyVulcanAverage: Boolean,
gradeAverageForceCalc: Boolean, gradeAverageForceCalc: Boolean,
secondSemesterSubject: GradeSubject, secondSemesterSubject: GradeSubject,
firstSemesterSubject: GradeSubject? firstSemesterSubject: GradeSubject?
) = if (!isAnyAverage || gradeAverageForceCalc) { ): Double {
val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1
return if (!isAnyVulcanAverage || gradeAverageForceCalc) {
val secondSemesterAverage = val secondSemesterAverage =
secondSemesterSubject.grades.updateModifiers(student).calcAverage() secondSemesterSubject.grades.updateModifiers(student).calcAverage()
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student) val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
?.calcAverage() ?: secondSemesterAverage ?.calcAverage() ?: secondSemesterAverage
val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1
(secondSemesterAverage + firstSemesterAverage) / divider (secondSemesterAverage + firstSemesterAverage) / divider
} else { } else {
(secondSemesterSubject.average + (firstSemesterSubject?.average ?: secondSemesterSubject.average)) / 2 (secondSemesterSubject.average + (firstSemesterSubject?.average ?: secondSemesterSubject.average)) / divider
}
} }
private fun getGradeSubjects( private fun getGradeSubjects(

View File

@ -198,6 +198,19 @@ class GradeAverageProviderTest {
assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0)
} }
@Test
fun `calc both semesters average with no grade in second semester but with average in first semester`() {
every { preferencesRepository.gradeAverageForceCalc } returns false
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS
coEvery { gradeRepository.getGrades(student, semesters[1], false) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier }
coEvery { gradeRepository.getGrades(student, semesters[2], false) } returns flowWithResource { emptyList<Grade>() to listOf(getSummary(24, "Język polski", .0))}
val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, false).getResult() }
assertEquals(3.49, items.single { it.subject == "Język polski" }.average, .0)
}
@Test @Test
fun `force calc average on no grades`() { fun `force calc average on no grades`() {
every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageForceCalc } returns true

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists