| 1 | use crate::constant::MAX_ANSWER_TEXT_LEN; |
| 2 | use crate::domain::exam::ExamId; |
| 3 | use crate::domain::exam_question::{ChoiceId, ExamQuestion, ExamQuestionId}; |
| 4 | use crate::domain::key; |
| 5 | use crate::domain::timestamp::Timestamp; |
| 6 | use crate::domain::user::UserId; |
| 7 | use crate::error::ValidationError; |
| 8 | use crate::validate::validate_optional; |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 17 | pub struct ExamAnswerId { |
| 18 | pub(crate) question: ExamQuestionId, |
| 19 | pub(crate) user: UserId, |
| 20 | pub(crate) seq: i64, |
| 21 | } |
| 22 | |
| 23 | impl ExamAnswerId { |
| 24 | pub fn composite(question: &ExamQuestionId, user: &UserId, seq: i64) -> Self { |
| 25 | Self { |
| 26 | question: question.clone(), |
| 27 | user: *user, |
| 28 | seq, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | pub fn key(&self) -> String { |
| 34 | key::sitting( |
| 35 | self.question.key().as_str(), |
| 36 | self.user.key().as_str(), |
| 37 | self.seq, |
| 38 | ) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 45 | #[sqlx(transparent)] |
| 46 | pub struct AnswerText(String); |
| 47 | |
| 48 | impl AnswerText { |
| 49 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 50 | validate_optional("text", value, MAX_ANSWER_TEXT_LEN)?; |
| 51 | Ok(Self(value.to_string())) |
| 52 | } |
| 53 | |
| 54 | pub fn as_str(&self) -> &str { |
| 55 | &self.0 |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 63 | pub struct ExamAnswer { |
| 64 | pub(crate) exam: ExamId, |
| 65 | pub(crate) question: ExamQuestionId, |
| 66 | #[sqlx(rename = "app_user")] |
| 67 | pub(crate) user: UserId, |
| 68 | pub(crate) seq: i64, |
| 69 | pub(crate) selected: Option<ChoiceId>, |
| 70 | pub(crate) text: Option<AnswerText>, |
| 71 | pub(crate) updated_at: Timestamp, |
| 72 | } |
| 73 | |
| 74 | impl ExamAnswer { |
| 75 | |
| 76 | pub fn get_id(&self) -> ExamAnswerId { |
| 77 | ExamAnswerId::composite(&self.question, &self.user, self.seq) |
| 78 | } |
| 79 | |
| 80 | pub fn get_exam(&self) -> &ExamId { |
| 81 | &self.exam |
| 82 | } |
| 83 | |
| 84 | pub fn get_question(&self) -> &ExamQuestionId { |
| 85 | &self.question |
| 86 | } |
| 87 | |
| 88 | pub fn get_user(&self) -> &UserId { |
| 89 | &self.user |
| 90 | } |
| 91 | |
| 92 | |
| 93 | pub fn get_seq(&self) -> i64 { |
| 94 | self.seq |
| 95 | } |
| 96 | |
| 97 | pub fn get_selected(&self) -> Option<&ChoiceId> { |
| 98 | self.selected.as_ref() |
| 99 | } |
| 100 | |
| 101 | pub fn get_text(&self) -> Option<&AnswerText> { |
| 102 | self.text.as_ref() |
| 103 | } |
| 104 | |
| 105 | pub fn get_updated_at(&self) -> Timestamp { |
| 106 | self.updated_at |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | pub fn is_correct(&self, question: &ExamQuestion) -> Option<bool> { |
| 112 | let correct = question.get_correct()?; |
| 113 | Some(self.selected.as_ref() == Some(correct)) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | pub fn auto_score(questions: &[ExamQuestion], answers: &[ExamAnswer]) -> (i64, i64) { |
| 122 | let mut earned = 0; |
| 123 | let mut possible = 0; |
| 124 | for question in questions { |
| 125 | let Some(correct) = question.get_correct() else { |
| 126 | continue; |
| 127 | }; |
| 128 | possible += question.get_points().as_i64(); |
| 129 | if answers |
| 130 | .iter() |
| 131 | .any(|a| a.get_question() == question.get_id() && a.get_selected() == Some(correct)) |
| 132 | { |
| 133 | earned += question.get_points().as_i64(); |
| 134 | } |
| 135 | } |
| 136 | (earned, possible) |
| 137 | } |
| 138 | |
| 139 | #[cfg(test)] |
| 140 | mod tests { |
| 141 | use super::*; |
| 142 | use crate::domain::exam_question::{ChoiceInput, QuestionKind, QuestionSpec}; |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | fn choice_question(exam: &ExamId, points: i64, correct: usize) -> ExamQuestion { |
| 148 | let labels = ["a", "b", "c"]; |
| 149 | let spec = QuestionSpec::try_new( |
| 150 | QuestionKind::try_new("choice").unwrap(), |
| 151 | Some( |
| 152 | labels |
| 153 | .iter() |
| 154 | .map(|l| ChoiceInput { |
| 155 | id: Some((*l).into()), |
| 156 | text: (*l).into(), |
| 157 | }) |
| 158 | .collect(), |
| 159 | ), |
| 160 | Some(labels[correct].into()), |
| 161 | &[], |
| 162 | ) |
| 163 | .unwrap(); |
| 164 | ExamQuestion::test_new(exam, "pick one", points, spec) |
| 165 | } |
| 166 | |
| 167 | fn text_question(exam: &ExamId, points: i64) -> ExamQuestion { |
| 168 | let spec = |
| 169 | QuestionSpec::try_new(QuestionKind::try_new("text").unwrap(), None, None, &[]).unwrap(); |
| 170 | ExamQuestion::test_new(exam, "explain", points, spec) |
| 171 | } |
| 172 | |
| 173 | |
| 174 | fn choice_id(question: &ExamQuestion, index: usize) -> ChoiceId { |
| 175 | question.get_choices().unwrap()[index].get_id().clone() |
| 176 | } |
| 177 | |
| 178 | fn answer(question: &ExamQuestion, user: &UserId, selected: Option<usize>) -> ExamAnswer { |
| 179 | let selected = selected.map(|index| choice_id(question, index)); |
| 180 | ExamAnswer { |
| 181 | exam: question.get_exam().clone(), |
| 182 | question: question.get_id().clone(), |
| 183 | user: *user, |
| 184 | seq: 1, |
| 185 | text: selected |
| 186 | .is_none() |
| 187 | .then(|| AnswerText::try_new("essay").unwrap()), |
| 188 | selected, |
| 189 | updated_at: Timestamp::from_millis(1), |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | fn student() -> UserId { |
| 194 | UserId::from_key("0198f1a2-3b4c-7d5e-8f90-aa2b3c4d5e6f") |
| 195 | } |
| 196 | |
| 197 | #[tokio::test] |
| 198 | async fn seqs_key_distinct_rows_but_seq_one_keeps_the_bare_key() { |
| 199 | let question = ExamQuestionId::from_key("0198f1a2-3b4c-7d5e-8f90-1a2b3c4d5e6f"); |
| 200 | let user = student(); |
| 201 | |
| 202 | let first = ExamAnswerId::composite(&question, &user, 1); |
| 203 | assert_eq!( |
| 204 | first.key(), |
| 205 | format!("{}_{}", question.key(), user.key()), |
| 206 | "seq 1 stays addressable at its historical key" |
| 207 | ); |
| 208 | |
| 209 | let second = ExamAnswerId::composite(&question, &user, 2); |
| 210 | assert_ne!(first, second, "two seqs must not collide onto one row"); |
| 211 | assert_eq!(second.key(), format!("{}_{}_2", question.key(), user.key())); |
| 212 | } |
| 213 | |
| 214 | #[tokio::test] |
| 215 | async fn answer_text_may_be_empty_but_bounded() { |
| 216 | assert!(AnswerText::try_new("").is_ok()); |
| 217 | assert!(AnswerText::try_new(&"x".repeat(10_000)).is_ok()); |
| 218 | assert!(AnswerText::try_new(&"x".repeat(10_001)).is_err()); |
| 219 | } |
| 220 | |
| 221 | #[tokio::test] |
| 222 | async fn auto_score_counts_correct_choices_only() { |
| 223 | let exam = ExamId::from_key("0198f1a2-3b4c-7d5e-8f90-be2b3c4d5e6f"); |
| 224 | let user = student(); |
| 225 | let q1 = choice_question(&exam, 10, 0); |
| 226 | let q2 = choice_question(&exam, 20, 1); |
| 227 | let q3 = choice_question(&exam, 30, 2); |
| 228 | let q4 = text_question(&exam, 40); |
| 229 | let questions = vec![q1.clone(), q2.clone(), q3, q4.clone()]; |
| 230 | let answers = vec![ |
| 231 | answer(&q1, &user, Some(0)), |
| 232 | answer(&q2, &user, Some(0)), |
| 233 | answer(&q4, &user, None), |
| 234 | ]; |
| 235 | assert_eq!(auto_score(&questions, &answers), (10, 60)); |
| 236 | } |
| 237 | |
| 238 | #[tokio::test] |
| 239 | async fn auto_score_of_nothing_is_zero() { |
| 240 | assert_eq!(auto_score(&[], &[]), (0, 0)); |
| 241 | let exam = ExamId::from_key("0198f1a2-3b4c-7d5e-8f90-be2b3c4d5e6f"); |
| 242 | let questions = vec![text_question(&exam, 50)]; |
| 243 | assert_eq!(auto_score(&questions, &[]), (0, 0)); |
| 244 | } |
| 245 | |
| 246 | #[tokio::test] |
| 247 | async fn is_correct_judges_choice_questions_only() { |
| 248 | let exam = ExamId::from_key("0198f1a2-3b4c-7d5e-8f90-be2b3c4d5e6f"); |
| 249 | let user = student(); |
| 250 | let choice = choice_question(&exam, 10, 1); |
| 251 | assert_eq!( |
| 252 | answer(&choice, &user, Some(1)).is_correct(&choice), |
| 253 | Some(true) |
| 254 | ); |
| 255 | assert_eq!( |
| 256 | answer(&choice, &user, Some(0)).is_correct(&choice), |
| 257 | Some(false) |
| 258 | ); |
| 259 | let text = text_question(&exam, 10); |
| 260 | assert_eq!(answer(&text, &user, None).is_correct(&text), None); |
| 261 | } |
| 262 | } |