ssr_algorithms/leitner_system/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use level::Level;
use serde::{Deserialize, Serialize};
use ssr_core::task::{level::TaskLevel, Task};
use std::time::SystemTime;

mod level;

#[derive(Serialize, Deserialize)]
pub struct WriteAnswer {
    level: Level,
    input_blocks: s_text_input_f::Blocks,
    correct_answer: s_text_input_f::Response,
}

impl WriteAnswer {
    pub fn new(
        input_blocks: s_text_input_f::Blocks,
        correct_answer: s_text_input_f::Response,
    ) -> Self {
        Self {
            level: Default::default(),
            input_blocks,
            correct_answer,
        }
    }
}

impl<'a> Task<'a> for WriteAnswer {
    type SharedState = ();

    fn next_repetition(&self, _: f64) -> SystemTime {
        self.level.next_repetition(0.)
    }

    fn complete(
        &mut self,
        _: &mut (),
        interaction: &mut impl FnMut(
            s_text_input_f::Blocks,
        ) -> std::io::Result<s_text_input_f::Response>,
    ) -> std::io::Result<()> {
        let user_answer = interaction(self.input_blocks.clone())?;
        match s_text_input_f::eq_response(&user_answer, &self.correct_answer, true, false) {
            false => {
                let mut feedback = s_text_input_f::to_answered(
                    self.input_blocks.clone(),
                    user_answer,
                    self.correct_answer.clone(),
                );
                feedback.push(s_text_input_f::Block::Paragraph(vec![]));
                feedback.push(s_text_input_f::Block::OneOf(vec!["OK".to_string()]));
                interaction(feedback)?;
                self.level.update(&mut (), (SystemTime::now(), false));
            }
            true => {
                let feedback = vec![
                    s_text_input_f::Block::Paragraph(vec![s_text_input_f::ParagraphItem::Text(
                        "All answers correct!".to_string(),
                    )]),
                    s_text_input_f::Block::OneOf(vec!["OK".to_string()]),
                ];
                interaction(feedback)?;
                self.level.update(&mut (), (SystemTime::now(), true));
                todo!()
            }
        }
        Ok(())
    }
}