Skip to content

Commit

Permalink
Add suggestions to final test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasatisocco committed Aug 10, 2023
1 parent fbcb518 commit d02c77b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/admin_panel/admin_page/widgets/results_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class ResultsWidget extends StatelessWidget {
),
),
...state.test.questions.map((e) {
;
return ListTile(
title: Text(e.getQuestion(context.l10n)),
subtitle: Text(e.answerText(context.l10n)),
Expand Down
8 changes: 4 additions & 4 deletions lib/coaching_test/cubit/coaching_test_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class CoachingTestCubit extends Cubit<CoachingTestState> {
Future<void> updateTest(String key, int value) async {
emit(CoachingTestUpdating(state.testModel));
final updatedTest = _updateTest(key, value);
await _dataPersistenceRepository.setCoachingTest(updatedTest.toMap());
final map = await updatedTest.toMap();
await _dataPersistenceRepository.setCoachingTest(map);
emit(CoachingTestUpdated(updatedTest));
}

Future<void> submitTest(AppLocalizations l10n) async {
emit(CoachingTestUpdating(state.testModel));
try {
await _uploadPDF(l10n);
final testId = await _firestoreRepository.addCoachingTest(
state.testModel.toMap(),
);
final map = await state.testModel.toMap();
final testId = await _firestoreRepository.addCoachingTest(map);
final user =
UserDataModel.fromJson(_dataPersistenceRepository.getUser()!);
final updated = user.copyWith(
Expand Down
94 changes: 93 additions & 1 deletion lib/coaching_test/models/test_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';
import 'package:coaching/coaching_test/models/question_model.dart';
import 'package:coaching/coaching_test/models/question_model_implementation.dart';
import 'package:coaching/coaching_test/models/test_model_keys.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';

class CoachingTest {
Expand Down Expand Up @@ -81,20 +82,103 @@ class CoachingTest {
return 'master_guide.pdf';
}

Map<String, dynamic> toMap() {
String get getQualityLevelName {
final total = getGroupAnswersTotal(AnswerGroup.qualityOfService);
if (total < 22) return Level.beginner;
if (total < 28) return Level.advancedBeginner;
if (total < 37) return Level.advanced;
if (total < 41) return Level.expert;
return Level.master;
}

String get getBusinessLevelName {
final total = getGroupAnswersTotal(AnswerGroup.business);
if (total < 3) return Level.beginner;
if (total < 17) return Level.advancedBeginner;
if (total < 28) return Level.advanced;
if (total < 37) return Level.expert;
return Level.master;
}

String get getPersonalLevelName {
final total = getGroupAnswersTotal(AnswerGroup.personal);
if (total < 6) return Level.beginner;
if (total < 10) return Level.advancedBeginner;
if (total < 15) return Level.advanced;
if (total < 20) return Level.expert;
return Level.master;
}

String get getCommunityLevelName {
final total = getGroupAnswersTotal(AnswerGroup.community);
if (total < 8) return Level.beginner;
if (total < 12) return Level.advancedBeginner;
if (total < 17) return Level.advanced;
if (total < 25) return Level.expert;
return Level.master;
}

Future<Map<String, dynamic>> toMap() async {
final answersMap = <String, dynamic>{};
final qualitySuggestions = <String>[];
final businessSuggestions = <String>[];
final wellnessSuggestions = <String>[];
final communitySuggestions = <String>[];
for (final question in questions) {
answersMap[question.key] = question.value;
final group = AnswerGroup.fromKey(question.key);
switch (group) {
case AnswerGroup.qualityOfService:
final filePath = './suggestions/quality/$getQualityLevelName.json';
final suggestion = await getSuggestion(filePath, question);
if (suggestion != null) qualitySuggestions.add(suggestion);
break;
case AnswerGroup.business:
final filePath = './suggestions/business/$getBusinessLevelName.json';
final suggestion = await getSuggestion(filePath, question);
if (suggestion != null) businessSuggestions.add(suggestion);
break;
case AnswerGroup.personal:
final filePath = './suggestions/wellness/$getPersonalLevelName.json';
final suggestion = await getSuggestion(filePath, question);
if (suggestion != null) wellnessSuggestions.add(suggestion);
break;
case AnswerGroup.community:
final filePath =
'./suggestions/community/$getCommunityLevelName.json';
final suggestion = await getSuggestion(filePath, question);
if (suggestion != null) communitySuggestions.add(suggestion);
break;
case null:
break;
}
}
return <String, dynamic>{
'userId': userId,
'coachingTestDate':
DateFormat('yyyy-MM-dd hh:mm').format(coachingTestDate),
'answers': answersMap,
'guide': _getGuide(),
'quality_suggestions': qualitySuggestions,
'business_suggestions': businessSuggestions,
'wellness_suggestions': wellnessSuggestions,
'community_suggestions': communitySuggestions,
};
}

Future<String?> getSuggestion(String filePath, QuestionModel question) async {
try {
final file = await rootBundle.loadString(filePath);
final completeMap = json.decode(file) as Map<String, dynamic>;
final questionMap = completeMap[question.key] as Map<String, dynamic>?;
final suggestion =
questionMap?[question.answerIndex.toString()] as String?;
return suggestion;
} catch (e) {
return null;
}
}

@override
String toString() {
return '''
Expand Down Expand Up @@ -150,3 +234,11 @@ Set<QuestionModel> emptyQuestions = {
ProfessionIntelectualQuestion(),
CertificationsQuestion(),
};

class Level {
static const beginner = 'beginner';
static const advancedBeginner = 'advanced_beginner';
static const advanced = 'advanced';
static const expert = 'expert';
static const master = 'master';
}
8 changes: 8 additions & 0 deletions lib/coaching_test/models/test_model_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ enum AnswerGroup {
personal,
community;

static AnswerGroup? fromKey(String key) {
if (key.startsWith('1')) return AnswerGroup.qualityOfService;
if (key.startsWith('2')) return AnswerGroup.business;
if (key.startsWith('3')) return AnswerGroup.personal;
if (key.startsWith('4')) return AnswerGroup.community;
return null;
}

int get groupIndex => super.index + 1;
}
2 changes: 1 addition & 1 deletion lib/coaching_test/view/question_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class QuestionPageDesktopView extends StatelessWidget {
Column(
children: [
const Spacer(flex: 2),
// TODO: Abstract this calculation
// TODO(TT): Abstract this calculation
SizedBox(
height: MediaQuery.of(context).size.height *
0.15 *
Expand Down

0 comments on commit d02c77b

Please sign in to comment.