You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.3 KiB
89 lines
2.3 KiB
import 'question.dart';
|
|
|
|
/// Status change for a question after an attempt.
|
|
enum QuestionStatusChange {
|
|
improved,
|
|
regressed,
|
|
unchanged,
|
|
}
|
|
|
|
/// Result of a single answer in an attempt.
|
|
class AnswerResult {
|
|
/// The question that was answered.
|
|
final Question question;
|
|
|
|
/// The indices of answers the user selected.
|
|
final List<int> userAnswerIndices;
|
|
|
|
/// Deprecated: Use [userAnswerIndices] instead. Returns the first selected answer index.
|
|
@Deprecated('Use userAnswerIndices instead')
|
|
int get userAnswerIndex => userAnswerIndices.isNotEmpty ? userAnswerIndices.first : -1;
|
|
|
|
/// Whether the answer was correct.
|
|
final bool isCorrect;
|
|
|
|
/// The status change for this question.
|
|
final QuestionStatusChange statusChange;
|
|
|
|
const AnswerResult({
|
|
required this.question,
|
|
required this.userAnswerIndices,
|
|
required this.isCorrect,
|
|
required this.statusChange,
|
|
});
|
|
}
|
|
|
|
/// Result of a completed attempt.
|
|
class AttemptResult {
|
|
/// Total number of questions in the attempt.
|
|
final int totalQuestions;
|
|
|
|
/// Number of correct answers.
|
|
final int correctCount;
|
|
|
|
/// Percentage of correct answers.
|
|
final double percentageCorrect;
|
|
|
|
/// Time spent on the attempt in milliseconds.
|
|
final int timeSpent;
|
|
|
|
/// List of questions that were answered incorrectly.
|
|
final List<AnswerResult> incorrectQuestions;
|
|
|
|
/// List of all answer results.
|
|
final List<AnswerResult> allResults;
|
|
|
|
const AttemptResult({
|
|
required this.totalQuestions,
|
|
required this.correctCount,
|
|
required this.percentageCorrect,
|
|
required this.timeSpent,
|
|
required this.incorrectQuestions,
|
|
required this.allResults,
|
|
});
|
|
|
|
/// Creates an attempt result from answer results.
|
|
factory AttemptResult.fromAnswers({
|
|
required List<AnswerResult> results,
|
|
required int timeSpent,
|
|
}) {
|
|
final correctCount = results.where((r) => r.isCorrect).length;
|
|
final totalQuestions = results.length;
|
|
final percentageCorrect = totalQuestions > 0
|
|
? (correctCount / totalQuestions) * 100.0
|
|
: 0.0;
|
|
final incorrectQuestions =
|
|
results.where((r) => !r.isCorrect).toList();
|
|
|
|
return AttemptResult(
|
|
totalQuestions: totalQuestions,
|
|
correctCount: correctCount,
|
|
percentageCorrect: percentageCorrect,
|
|
timeSpent: timeSpent,
|
|
incorrectQuestions: incorrectQuestions,
|
|
allResults: results,
|
|
);
|
|
}
|
|
}
|
|
|