From 01dafc8cc87907365035525c6cedc26fe9fcb49d Mon Sep 17 00:00:00 2001 From: Jonas Sander <29028262+Jonas-Sander@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:27:51 +0200 Subject: [PATCH] Don't show average grade if grading system is not numerical. (#1442) Don't show the calculated average grade if the grading system is not numerical. To be able to do this I added `GradeResult.gradingSystem` and `GradeValue.gradingSystem`. Before: ![image](https://github.com/SharezoneApp/sharezone-app/assets/29028262/d0eae3a3-f5db-4c0c-ba6d-0d0a2032e36d) After: ![image](https://github.com/SharezoneApp/sharezone-app/assets/29028262/8ab3adf2-5d30-45e7-a54d-3711070c08a4) Requires #1441 --------- Co-authored-by: Jonas <--help> --- .../grades/grades_service/grades_service.dart | 15 ++++--- .../grades_service/src/grading_systems.dart | 1 + .../grades_page/grades_page_controller.dart | 3 ++ app/test/grades/grades_test.dart | 39 +++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/app/lib/grades/grades_service/grades_service.dart b/app/lib/grades/grades_service/grades_service.dart index 6e10f0bb2..ddb991a31 100644 --- a/app/lib/grades/grades_service/grades_service.dart +++ b/app/lib/grades/grades_service/grades_service.dart @@ -385,6 +385,7 @@ class GradeResult { final GradeValue value; final bool isTakenIntoAccount; final Date date; + GradingSystem get gradingSystem => value.gradingSystem; GradeResult({ required this.id, @@ -447,6 +448,8 @@ class GradeValue extends Equatable { double get asDouble => asNum.toDouble(); final num asNum; + final GradingSystem gradingSystem; + /// Only available if there is a special displayable grade for the calculated /// grade. For example, if the calculated grade is 2.25, the displayable grade /// could be '2+' for the (1-6 with +-) grading system. @@ -457,12 +460,14 @@ class GradeValue extends Equatable { final String? suffix; @override - List get props => [asNum, displayableGrade, suffix]; + List get props => [asNum, gradingSystem, displayableGrade, suffix]; - const GradeValue( - {required this.asNum, - required this.displayableGrade, - required this.suffix}); + const GradeValue({ + required this.asNum, + required this.gradingSystem, + required this.displayableGrade, + required this.suffix, + }); } class Grade { diff --git a/app/lib/grades/grades_service/src/grading_systems.dart b/app/lib/grades/grades_service/src/grading_systems.dart index bd39c4ec4..f6c87c23f 100644 --- a/app/lib/grades/grades_service/src/grading_systems.dart +++ b/app/lib/grades/grades_service/src/grading_systems.dart @@ -81,6 +81,7 @@ class _GradingSystem { GradeValue toGradeResult(num grade) { return GradeValue( asNum: grade, + gradingSystem: toGradingSystems(), displayableGrade: spec.getSpecialDisplayableGradeOrNull?.call(grade), suffix: spec == zeroToHundredPercentWithDecimalsSpec ? '%' : null, ); diff --git a/app/lib/grades/pages/grades_page/grades_page_controller.dart b/app/lib/grades/pages/grades_page/grades_page_controller.dart index 792343dd5..1a5bd8129 100644 --- a/app/lib/grades/pages/grades_page/grades_page_controller.dart +++ b/app/lib/grades/pages/grades_page/grades_page_controller.dart @@ -97,6 +97,9 @@ class GradesPageController extends ChangeNotifier { // This is not covered by tests yet. String displayGrade(GradeValue? grade) { if (grade == null) return '—'; + if (!grade.gradingSystem.isNumericalAndContinous) { + return grade.displayableGrade ?? '—'; + } String withSuffix(String gs) => '$gs${grade.suffix ?? ''}'; diff --git a/app/test/grades/grades_test.dart b/app/test/grades/grades_test.dart index 9e4781071..ee1f9d1b7 100644 --- a/app/test/grades/grades_test.dart +++ b/app/test/grades/grades_test.dart @@ -704,6 +704,45 @@ void main() { .date, Date('2024-03-26')); }); + test('A grade and GradeValue has a gradingSystem', () { + final controller = GradesTestController(); + + final term = termWith( + subjects: [ + subjectWith( + id: const SubjectId('Philosophie'), + grades: [ + gradeWith( + id: GradeId('grade1'), + gradingSystem: GradingSystem.oneToFiveWithDecimals, + value: 2.5, + ), + gradeWith( + id: GradeId('grade2'), + gradingSystem: GradingSystem.sixToOneWithDecimals, + value: 4.2, + ), + ], + ), + ], + ); + controller.createTerm(term); + + final grade1 = controller + .term(term.id) + .subject(const SubjectId('Philosophie')) + .grade(GradeId('grade1')); + // On both since the calculated averageGrade is only a [GradeValue], not a [GradeResult]. + expect(grade1.gradingSystem, GradingSystem.oneToFiveWithDecimals); + expect(grade1.value.gradingSystem, GradingSystem.oneToFiveWithDecimals); + + final grade2 = controller + .term(term.id) + .subject(const SubjectId('Philosophie')) + .grade(GradeId('grade2')); + expect(grade2.gradingSystem, GradingSystem.sixToOneWithDecimals); + expect(grade2.value.gradingSystem, GradingSystem.sixToOneWithDecimals); + }); test('A subject has a Design', () { final controller = GradesTestController();