Skip to content

Commit

Permalink
Merge pull request #66 from ia-toki/update/quiz-options
Browse files Browse the repository at this point in the history
update quiz answer
  • Loading branch information
atnanahidiw authored Nov 10, 2023
2 parents 582a139 + ee8547a commit 1e2c0fe
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
String? quizParticipantId;

String selectedAnswer = '';
String shortAnswer = '';
late int remainingDuration;
Timer? timer;

Expand Down Expand Up @@ -103,6 +104,7 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
} else {
Expand All @@ -116,6 +118,7 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
} catch (e) {
Expand All @@ -135,27 +138,55 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
);
}

void fillAnswer(String answer) {
shortAnswer = answer;

emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
shortAnswer: shortAnswer,
),
);
}

FutureOr<void> submitAnswer() async {
if (selectedAnswer == '') {
if ((currentProblem.type == 'MULTIPLE_CHOICE' && selectedAnswer == '') ||
(currentProblem.type == 'SHORT_ANSWER') && shortAnswer == '') {
emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
modalErrorMessage: 'Pilih salah satu jawaban',
shortAnswer: shortAnswer,
modalErrorMessage: currentProblem.type == 'MULTIPLE_CHOICE'
? 'Pilih salah satu jawaban'
: 'Isi jawaban anda',
),
);
return;
}
try {
var verdict = 'INCORRECT';
if (currentProblem.answer.correctAnswer.contains(selectedAnswer)) {

if (currentProblem.type == 'MULTIPLE_CHOICE' &&
currentProblem.answer.correctAnswer.contains(selectedAnswer)) {
verdict = 'CORRECT';
}

if (currentProblem.type == 'SHORT_ANSWER' &&
currentProblem.answer.correctAnswer
.contains(shortAnswer.trim().toLowerCase())) {
verdict = 'CORRECT';
}

attempt.answers?.add(
QuizExerciseAnswer(
answer: selectedAnswer,
answer: currentProblem.type == 'SHORT_ANSWER'
? shortAnswer
: selectedAnswer,
correctAnswer: currentProblem.answer.correctAnswer,
taskChallengeGroup: currentProblem.challengeGroup,
taskId: currentProblem.id,
Expand All @@ -177,12 +208,14 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
currentProblem = await quizExerciseRepository
.getQuizExercise(problemIdList[currentProblemIndex]);
selectedAnswer = '';
shortAnswer = '';
emit(
QuizExerciseShow(
quiz: quiz,
quizExercise: currentProblem,
remainingDuration: Duration(seconds: remainingDuration),
selectedAnswer: selectedAnswer,
shortAnswer: shortAnswer,
),
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class QuizExerciseShow extends QuizExerciseState {
final WeeklyQuiz quiz;
final Duration remainingDuration;
final String selectedAnswer;
final String shortAnswer;
final String modalErrorMessage;

const QuizExerciseShow({
required this.quiz,
required this.quizExercise,
required this.remainingDuration,
required this.selectedAnswer,
this.shortAnswer = '',
this.selectedAnswer = '',
this.modalErrorMessage = '',
});

Expand All @@ -31,6 +34,7 @@ class QuizExerciseShow extends QuizExerciseState {
quizExercise,
remainingDuration,
selectedAnswer,
shortAnswer,
modalErrorMessage,
];
}
Expand Down
12 changes: 7 additions & 5 deletions app/lib/features/quiz_exercise/presentation/model/questions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import 'options.dart';

class Question {
final String content;
final List<Options> options;
final List<Options>? options;

Question({required this.content, required this.options});
Question({required this.content, this.options});

factory Question.fromJson(Map<String, dynamic> json) {
return Question(
content: json['content'] as String,
options: (json['options'] as List<dynamic>)
.map((e) => Options.fromJson(e as Map<String, dynamic>))
.toList(),
options: json['options'] != null
? (json['options'] as List<dynamic>)
.map((e) => Options.fromJson(e as Map<String, dynamic>))
.toList()
: [],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../../../../core/bases/widgets/atoms/button.dart';
import '../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../core/constants/assets.dart';
import '../../../../core/theme/font_theme.dart';
import '../../../authentication/register/presentation/widgets/custom_text_field.dart';
import '../bloc/quiz_exercise_cubit.dart';

part 'quiz_exercise_page.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ class _QuizExercisePageState extends State<QuizExercisePage> {
width: 40,
height: 20,
),
Text(
state.quizExercise.title,
textAlign: TextAlign.center,
style: FontTheme.blackSubtitleBold(),
Flexible(
child: Text(
state.quizExercise.title,
textAlign: TextAlign.center,
style: FontTheme.blackSubtitleBold(),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
Expand Down Expand Up @@ -166,67 +168,107 @@ class _QuizExercisePageState extends State<QuizExercisePage> {
return state is QuizExerciseShow;
}, builder: (context, state) {
if (state is QuizExerciseShow) {
return AlertDialog(
content: SingleChildScrollView(
child: Column(
children: [
Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
'Pertanyaan',
style: FontTheme.blackTextBold(),
return Scaffold(
backgroundColor: Colors.transparent,
body: AlertDialog(
content: SingleChildScrollView(
child: Column(
children: [
Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
'Pertanyaan',
style: FontTheme.blackTextBold(),
),
),
),
SizedBox(
width: 400,
height: 200,
child: SingleChildScrollView(
child:
Html(data: state.quizExercise.question.content),
SizedBox(
width: 400,
height: 200,
child: SingleChildScrollView(
child:
Html(data: state.quizExercise.question.content),
),
),
),
...state.quizExercise.question.options
.map((e) => RadioListTile(
title: Text(e.content),
value: e.id,
groupValue: state.selectedAnswer,
onChanged: (value) {
context
.read<QuizExerciseCubit>()
.selectAnswer(e.id);
})),
Text(state.modalErrorMessage),
],
),
),
actions: [
SizedBox(
width: 100,
height: 50,
child: Button(
buttonType: ButtonType.secondary,
text: 'Cancel',
onTap: () {
Navigator.pop(context);
},
...state.quizExercise.question.options!
.map((e) => RadioListTile(
title: Text(e.content),
value: e.id,
groupValue: state.selectedAnswer,
onChanged: (value) {
context
.read<QuizExerciseCubit>()
.selectAnswer(e.id);
})),
state.quizExercise.type == 'SHORT_ANSWER'
? Container(
padding: const EdgeInsets.only(top: 20),
child: CustomTextField('Jawaban anda', (value) {
context
.read<QuizExerciseCubit>()
.fillAnswer(value);
}, (p0) => null, ''),
)
: Container(),
Text(state.modalErrorMessage),
],
),
),
SizedBox(
width: 100,
height: 50,
child: Button(
buttonType: ButtonType.tertiary,
text: 'OK',
onTap: () {
context.read<QuizExerciseCubit>().submitAnswer();
if (state.selectedAnswer != '') {
actions: [
SizedBox(
width: 100,
height: 50,
child: Button(
buttonType: ButtonType.secondary,
text: 'Cancel',
onTap: () {
Navigator.pop(context);
}
},
},
),
),
),
],
SizedBox(
width: 100,
height: 50,
child: Button(
buttonType: ButtonType.tertiary,
text: 'OK',
onTap: () {
var error = '';

if (state.quizExercise.type == 'SHORT_ANSWER') {
if (state.shortAnswer == '') {
error = 'Isi jawaban anda';
}
} else {
if (state.selectedAnswer == '') {
error = 'Pilih salah satu jawaban';
}
}

if (error != '') {
final snackBar = SnackBar(
backgroundColor: Colors.red,
duration: const Duration(seconds: 1),
behavior: SnackBarBehavior.floating,
margin: const EdgeInsets.only(
bottom: 50, left: 10, right: 10),
content: Text(error),
);

ScaffoldMessenger.of(context)
.showSnackBar(snackBar);

error = '';
return;
}

context.read<QuizExerciseCubit>().submitAnswer();
Navigator.pop(context);
},
),
),
],
),
);
}
return const SizedBox(
Expand Down

0 comments on commit 1e2c0fe

Please sign in to comment.