Skip to content

Commit

Permalink
Add gradeTypeWeights to GradesService.addTerm. (#1821)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-Sander authored Feb 20, 2025
1 parent 060887b commit ac779d4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
2 changes: 2 additions & 0 deletions app/lib/grades/grades_service/grades_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GradesService {
required GradeTypeId finalGradeType,
required GradingSystem gradingSystem,
required bool isActiveTerm,
IMap<GradeTypeId, Weight>? gradeTypeWeights,
@visibleForTesting TermId? id,
}) {
final newId = _service.addTerm(
Expand All @@ -54,6 +55,7 @@ class GradesService {
finalGradeType: finalGradeType,
gradingSystem: gradingSystem,
isActiveTerm: isActiveTerm,
gradeTypeWeights: gradeTypeWeights ?? const IMapConst({}),
);

return term(newId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class _GradesServiceInternal {
required GradeTypeId finalGradeType,
required GradingSystem gradingSystem,
required bool isActiveTerm,
IMap<GradeTypeId, Weight> gradeTypeWeights = const IMapConst({}),
@visibleForTesting TermId? id,
}) {
final termId = id ?? TermId(Id.generate().value);
Expand All @@ -131,6 +132,9 @@ class _GradesServiceInternal {
name: name,
finalGradeType: finalGradeType,
gradingSystem: gradingSystem.toGradingSystemModel(),
gradeTypeWeightings: gradeTypeWeights.map(
(key, value) => MapEntry(key, value.toNonNegativeWeightOrThrow()),
),
),
);
_updateTerms(newTerms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:crash_analytics/crash_analytics.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/material.dart';
import 'package:sharezone/grades/grades_service/grades_service.dart';
import 'package:sharezone/grades/pages/create_term_page/create_term_analytics.dart';
Expand Down Expand Up @@ -56,40 +57,23 @@ class CreateTermPageController extends ChangeNotifier {
}

try {
final termRef = gradesService.addTerm(
gradesService.addTerm(
name: view.name!,
isActiveTerm: view.isActiveTerm,
finalGradeType: GradeType.schoolReportGrade.id,
gradingSystem: view.gradingSystem,
gradeTypeWeights: IMap({
GradeType.writtenExam.id: const Weight.percent(50),
GradeType.oralParticipation.id: const Weight.percent(50),
}),
);
analytics.logCreateTerm();

// Firestore had a soft limit of 1 write per second per document. However,
// this limit isn't mentioned in the documentation anymore. We still keep
// the delay to be on the safe side.
//
// https://stackoverflow.com/questions/74454570/has-firestore-removed-the-soft-limit-of-1-write-per-second-to-a-single-document
await _waitForFirestoreLimit();
termRef.changeGradeTypeWeight(
GradeType.writtenExam.id,
const Weight.percent(50),
);

await _waitForFirestoreLimit();
termRef.changeGradeTypeWeight(
GradeType.oralParticipation.id,
const Weight.percent(50),
);
} catch (e, s) {
crashAnalytics.recordError('Could not save term: $e', s);
throw CouldNotSaveTermException('$e');
}
}

Future<void> _waitForFirestoreLimit() async {
await Future.delayed(const Duration(milliseconds: 200));
}

void setGradingSystem(GradingSystem system) {
view = view.copyWith(gradingSystem: system);
notifyListeners();
Expand Down
7 changes: 1 addition & 6 deletions app/test/grades/grades_test_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,13 @@ class GradesTestController {
isActiveTerm: testTerm.isActiveTerm,
name: testTerm.name,
gradingSystem: testTerm.gradingSystem,
gradeTypeWeights: testTerm.gradeTypeWeights?.toIMap(),
);

if (testTerm.weightDisplayType != null) {
service.term(termId).changeWeightDisplayType(testTerm.weightDisplayType!);
}

if (testTerm.gradeTypeWeights != null) {
for (var e in testTerm.gradeTypeWeights!.entries) {
service.term(termId).changeGradeTypeWeight(e.key, e.value);
}
}

for (var subject in testTerm.subjects.values) {
final subRef = termRef.subject(subject.id);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac779d4

Please sign in to comment.