headless_lms_models/
course_background_questions.rs

1use crate::{
2    course_background_question_answers::CourseBackgroundQuestionAnswer,
3    course_instances::CourseInstance, prelude::*,
4};
5
6#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Type)]
7#[sqlx(
8    type_name = "course_background_question_type",
9    rename_all = "snake_case"
10)]
11#[cfg_attr(feature = "ts_rs", derive(TS))]
12pub enum CourseBackgroundQuestionType {
13    Checkbox,
14    Text,
15}
16
17#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
18#[cfg_attr(feature = "ts_rs", derive(TS))]
19pub struct CourseBackgroundQuestion {
20    pub id: Uuid,
21    pub created_at: DateTime<Utc>,
22    pub updated_at: DateTime<Utc>,
23    pub deleted_at: Option<DateTime<Utc>>,
24    pub course_instance_id: Option<Uuid>,
25    pub course_id: Uuid,
26    pub question_text: String,
27    pub question_type: CourseBackgroundQuestionType,
28}
29
30#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
31#[cfg_attr(feature = "ts_rs", derive(TS))]
32pub struct CourseBackgroundQuestionsAndAnswers {
33    pub background_questions: Vec<CourseBackgroundQuestion>,
34    pub answers: Vec<CourseBackgroundQuestionAnswer>,
35}
36
37/// Return all background questions that will need to be asked when the user enrolls on a course instance. Includes both instance specific questions and course specific questions.
38pub async fn get_background_questions_for_course_instance(
39    conn: &mut PgConnection,
40    course_instance: &CourseInstance,
41) -> ModelResult<Vec<CourseBackgroundQuestion>> {
42    let res: Vec<CourseBackgroundQuestion> = sqlx::query_as!(
43        CourseBackgroundQuestion,
44        r#"
45SELECT id,
46  created_at,
47  updated_at,
48  deleted_at,
49  course_instance_id,
50  course_id,
51  question_text,
52  question_type as "question_type: CourseBackgroundQuestionType"
53FROM course_background_questions
54WHERE deleted_at IS NULL
55  AND (
56    (
57      course_instance_id IS NULL
58      AND course_id = $1
59    )
60    OR (
61      course_instance_id = $2
62      AND course_id = $1
63    )
64  )
65  "#,
66        course_instance.course_id,
67        course_instance.id,
68    )
69    .fetch_all(conn)
70    .await?;
71    Ok(res)
72}
73
74/// Return all background questions (and existing answers) that will need to be asked when the user enrolls on a course instance. Includes both instance specific questions and course specific questions.
75pub async fn get_background_questions_and_answers(
76    conn: &mut PgConnection,
77    course_instance: &CourseInstance,
78    user_id: Uuid,
79) -> ModelResult<CourseBackgroundQuestionsAndAnswers> {
80    let background_questions =
81        get_background_questions_for_course_instance(&mut *conn, course_instance).await?;
82    let answers = crate::course_background_question_answers::get_background_question_answers_for_background_questions(&mut *conn, user_id, &background_questions).await?;
83    Ok(CourseBackgroundQuestionsAndAnswers {
84        background_questions,
85        answers,
86    })
87}