Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion requirements by chapter #1295

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Add down migration script here
ALTER TABLE course_modules DROP COLUMN IF EXISTS is_completion_requirement_by_chapter;
----
DROP TABLE chapter_completion_requirements;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Add up migration script here
ALTER TABLE course_modules
ADD COLUMN is_completion_requirement_by_chapter BOOLEAN NOT NULL DEFAULT FALSE;
COMMENT ON COLUMN course_modules.is_completion_requirement_by_chapter IS 'Determine if a chapter needs to specify its completion_requirement or not';
CREATE TABLE chapter_completion_requirements (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
course_instance_id UUID NOT NULL REFERENCES course_instances,
chapter_id UUID NOT NULL REFERENCES chapters,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
deleted_at TIMESTAMP WITH TIME ZONE,
completion_points_threshold INTEGER,
completion_number_of_exercises_attempted_threshold INTEGER NOT NULL
);
CREATE TRIGGER set_timestamp BEFORE
UPDATE ON chapter_completion_requirements FOR EACH ROW EXECUTE PROCEDURE trigger_set_timestamp();
COMMENT ON TABLE chapter_completion_requirements IS 'Contains completion requirement for a chapter when is_completion_require_by_chapter flag is set to true in course_modules';
COMMENT ON COLUMN chapter_completion_requirements.id IS 'A unique, stable identifier for the record.';
COMMENT ON COLUMN chapter_completion_requirements.course_instance_id IS 'The course_instance_id which this chapter is a part of.';
COMMENT ON COLUMN chapter_completion_requirements.chapter_id IS 'The chapter_id of the chapter.';
COMMENT ON COLUMN chapter_completion_requirements.created_at IS 'Timestamp when the record was created.';
COMMENT ON COLUMN chapter_completion_requirements.updated_at IS 'Timestamp when the record was updated.';
COMMENT ON COLUMN chapter_completion_requirements.deleted_at IS 'Timestamp when the record was deleted. If null, the record is not deleted.';
COMMENT ON COLUMN chapter_completion_requirements.completion_points_threshold IS 'The point threshold set for this chapter by the instructor.';
COMMENT ON COLUMN chapter_completion_requirements.completion_number_of_exercises_attempted_threshold IS 'The number of exercises attempted threshold set for this chapter by the instructor.';

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

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

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

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

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

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

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

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

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

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

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

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

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
@@ -0,0 +1,83 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ChapterCompletionRequirements {
pub id: Uuid,
pub course_instance_id: Uuid,
pub chapter_id: Uuid,
pub created_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
pub completion_points_threshold: Option<i32>,
pub completion_number_of_exercises_attempted_threshold: Option<i32>,
}

pub async fn insert_chapter_completion_requirements(
conn: &mut PgConnection,
course_instance_id: Uuid,
chapter_id: Uuid,
completion_points_threshold: Option<i32>,
completion_number_of_exercises_attempted_threshold: Option<i32>,
) -> ModelResult<Uuid> {
let res = sqlx::query!(
"
INSERT INTO chapter_completion_requirements(
course_instance_id,
chapter_id,
completion_points_threshold,
completion_number_of_exercises_attempted_threshold
)
VALUES(
$1,
$2,
$3,
$4
)
RETURNING id
",
course_instance_id,
chapter_id,
completion_points_threshold,
completion_number_of_exercises_attempted_threshold,
)
.fetch_one(conn)
.await?;
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,
) -> ModelResult<()> {
sqlx::query!(
"
UPDATE chapter_completion_requirements
SET deleted_at = now()
WHERE chapter_id = $1
",
id
)
.execute(conn)
.await?;
Ok(())
}
Loading
Loading