aggregateMasteryLevel function
Aggregates per-topic mastery levels into a single integer rank suitable
for the MasteryRing widget. Used by the domain list and the learner home
to summarise a domain at a glance.
The mapping mirrors the data model: unstarted → 0 awareness → 1 understanding → 2 application → 3 evaluation → 4 mastery → 5
Empty input yields 0. The result is rounded and clamped to 0..5.
Implementation
int aggregateMasteryLevel(Map<String, String> topicIdToMastery) {
if (topicIdToMastery.isEmpty) return 0;
final ints = topicIdToMastery.values
.map(
(m) => switch (m) {
'awareness' => 1,
'understanding' => 2,
'application' => 3,
'evaluation' => 4,
'mastery' => 5,
_ => 0,
},
)
.toList();
if (ints.isEmpty) return 0;
final avg = ints.reduce((a, b) => a + b) / ints.length;
return avg.round().clamp(0, 5);
}