Skip to content

Commit

Permalink
add more sql functions
Browse files Browse the repository at this point in the history
  • Loading branch information
george-misan committed Jul 18, 2024
1 parent c6dcae5 commit 555bc1a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ RETURNING id
Ok(res.id)
}

pub async fn get_requirements_by_chapter_id(
conn: &mut PgConnection,
chapter_id: Uuid,
) -> ModelResult<ChapterCompletionRequirements> {
let completion_requirements = sqlx::query_as!(
ChapterCompletionRequirements,
"
SELECT *
FROM chapter_completion_requirements
WHERE chapter_id = $1
AND deleted_at IS NULL;
",
chapter_id
)
.fetch_one(conn)
.await?;
Ok(completion_requirements)
}

pub async fn delete_chapter_completion_requirements(
conn: &mut PgConnection,
id: Uuid,
Expand Down
51 changes: 29 additions & 22 deletions services/headless-lms/models/src/library/progressing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
user_course_settings,
user_details::UserDetail,
user_exercise_states,
user_exercise_states::UserCourseInstanceChapterExerciseProgress,
users::{self, User},
};
use headless_lms_utils::numbers::option_f32_to_f32_two_decimals_with_none_as_zero;
Expand Down Expand Up @@ -113,16 +112,25 @@ pub async fn check_and_insert_suspected_cheaters(
Ok(())
}

pub struct TotalScoreInChapter {
pub total_score: f32,
}

pub struct TotalAttemptsInChapter {
pub total_attempts: i32,
}

// Get the total score given to a student in a chapter
pub async fn get_total_score_in_a_chapter(
conn: &mut PgConnection,
user_id: Uuid,
course_instance_id: Uuid,
chapter_id: Uuid,
) -> ModelResult<Vec<UserCourseInstanceChapterExerciseProgress>> {
) -> ModelResult<TotalScoreInChapter> {
let chapter_exercises = exercises::get_exercises_by_chapter_id(conn, chapter_id).await?;
let exercise_ids: Vec<Uuid> = chapter_exercises.into_iter().map(|e| e.id).collect();

// Fetch user exercise progress
let user_course_instance_exercise_progress =
user_exercise_states::get_user_course_instance_chapter_exercises_progress(
conn,
Expand All @@ -131,15 +139,15 @@ pub async fn get_total_score_in_a_chapter(
user_id,
)
.await?;
let rounded_score_given_instances: Vec<UserCourseInstanceChapterExerciseProgress> =
user_course_instance_exercise_progress
.into_iter()
.map(|i| UserCourseInstanceChapterExerciseProgress {
score_given: option_f32_to_f32_two_decimals_with_none_as_zero(i.score_given),
exercise_id: i.exercise_id,
})
.collect();
Ok(rounded_score_given_instances)

// Calculate the total score
let total_score: f32 = user_course_instance_exercise_progress
.into_iter()
.map(|i| option_f32_to_f32_two_decimals_with_none_as_zero(i.score_given))
.sum();

// Return the total score
Ok(TotalScoreInChapter { total_score })
}

// Get the total score given to a student in a chapter
Expand All @@ -148,7 +156,7 @@ pub async fn get_total_exercise_attempt_in_a_chapter(
user_id: Uuid,
course_instance_id: Uuid,
chapter_id: Uuid,
) -> ModelResult<()> {
) -> ModelResult<TotalAttemptsInChapter> {
let chapter_exercises = exercises::get_exercises_by_chapter_id(conn, chapter_id).await?;
let exercise_ids: Vec<Uuid> = chapter_exercises.into_iter().map(|e| e.id).collect();

Expand All @@ -160,16 +168,15 @@ pub async fn get_total_exercise_attempt_in_a_chapter(
user_id,
)
.await?;
// let rounded_score_given_instances: Vec<UserCourseInstanceChapterExerciseProgress> =
// user_course_instance_exercise_progress
// .into_iter()
// .map(|i| UserCourseInstanceChapterExerciseProgress {
// score_given: option_f32_to_f32_two_decimals_with_none_as_zero(i.score_given),
// exercise_id: i.exercise_id,
// })
// .collect();
// Ok(rounded_score_given_instances)
Ok(())

// Calculate the exercise attempts in a chapter
let total_attempts: i32 = user_course_instance_exercises_attempted
.into_iter()
.map(|i| i.exercise_attempts.unwrap_or(0))
.sum();

// Return the total score
Ok(TotalAttemptsInChapter { total_attempts })
}

/// Creates completion for the user if eligible and previous one doesn't exist. Returns an Option containing
Expand Down
8 changes: 4 additions & 4 deletions services/headless-lms/models/src/user_exercise_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ WHERE ues.deleted_at IS NULL
}

pub struct ChapterExerciseAttempts {
pub exercise_attempt: Option<bool>,
pub exercise_attempts: Option<i32>,
}

pub async fn get_user_course_instance_chapter_exercises_attempts(
Expand All @@ -481,9 +481,9 @@ pub async fn get_user_course_instance_chapter_exercises_attempts(
r#"
SELECT
CASE
WHEN ues.score_given IS NULL THEN FALSE
ELSE TRUE
END AS exercise_attempt
WHEN ues.score_given IS NULL THEN 0
ELSE 1
END AS exercise_attempts
FROM
user_exercise_states AS ues
WHERE ues.deleted_at IS NULL
Expand Down

0 comments on commit 555bc1a

Please sign in to comment.