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

Make course instance joinable with a special code #1314

Merged
merged 9 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE course_instances DROP COLUMN join_code;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE course_instances
ADD COLUMN join_code varchar(1024);
COMMENT ON COLUMN course_instances.join_code IS 'Regeneratable code that is used to join the course';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the join_code_uses table.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE course_accesses;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE course_accesses (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

join_code_uses

id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
course_instance_id UUID NOT NULL REFERENCES course_instances(id),
user_id UUID NOT NULL REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMP WITH TIME ZONE
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the updated_at column and then the trigger to update it automatically.

COMMENT ON TABLE course_accesses IS 'This table is used to check if user has access to a course that is only joinable by a special code';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this comment explain how this table is related to the course_instances.join_code.

COMMENT ON COLUMN course_accesses.id IS 'A unique, stable identifier for the record.';
COMMENT ON COLUMN course_accesses.course_instance_id IS 'Course instance that the user has access to.';
COMMENT ON COLUMN course_accesses.user_id IS 'User who has the access to the course.';
COMMENT ON COLUMN course_accesses.created_at IS 'Timestamp of when the record was created';
COMMENT ON COLUMN course_accesses.deleted_at IS 'Timestamp when the record was deleted. If null, the record is not deleted.';

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.

38 changes: 32 additions & 6 deletions services/headless-lms/models/src/course_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct CourseInstance {
pub teacher_in_charge_name: String,
pub teacher_in_charge_email: String,
pub support_email: Option<String>,
pub join_code: Option<String>,
}

impl CourseInstance {
Expand Down Expand Up @@ -85,7 +86,8 @@ RETURNING id,
description,
teacher_in_charge_name,
teacher_in_charge_email,
support_email
support_email,
join_code
"#,
pkey_policy.into_uuid(),
new_course_instance.course_id,
Expand Down Expand Up @@ -118,7 +120,8 @@ SELECT id,
description,
teacher_in_charge_name,
teacher_in_charge_email,
support_email
support_email,
join_code
FROM course_instances
WHERE id = $1
AND deleted_at IS NULL;
Expand Down Expand Up @@ -187,7 +190,8 @@ SELECT i.id,
i.description,
i.teacher_in_charge_name,
i.teacher_in_charge_email,
i.support_email
i.support_email,
i.join_code
FROM user_course_settings ucs
JOIN course_instances i ON (ucs.current_course_instance_id = i.id)
WHERE ucs.user_id = $1
Expand Down Expand Up @@ -221,7 +225,8 @@ SELECT i.id,
i.description,
i.teacher_in_charge_name,
i.teacher_in_charge_email,
i.support_email
i.support_email,
i.join_code
FROM course_instances i
JOIN course_instance_enrollments ie ON (i.id = ie.course_id)
WHERE i.course_id = $1
Expand Down Expand Up @@ -253,7 +258,8 @@ SELECT id,
description,
teacher_in_charge_name,
teacher_in_charge_email,
support_email
support_email,
join_code
FROM course_instances
WHERE deleted_at IS NULL
"#
Expand Down Expand Up @@ -281,7 +287,8 @@ SELECT id,
description,
teacher_in_charge_name,
teacher_in_charge_email,
support_email
support_email,
join_code
FROM course_instances
WHERE course_id = $1
AND deleted_at IS NULL;
Expand Down Expand Up @@ -852,6 +859,25 @@ WHERE ce.course_instance_id = $1
Ok(res.map(|r| r.student_duration_seconds).unwrap_or_default())
}

pub async fn generate_join_code_for_course_instance(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better function name would be set_join_code_for_course_instance

conn: &mut PgConnection,
course_instance_id: Uuid,
join_code: String,
) -> ModelResult<()> {
sqlx::query!(
"
UPDATE course_instances
SET join_code = $2
WHERE id = $1
",
course_instance_id,
join_code
)
.execute(conn)
.await?;
Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,28 @@ async fn get_user_progress_for_course_instance(
token.authorized_ok(web::Json(user_course_instance_progress))
}

/**
POST /api/v0/main-frontend/course-instance/:course_instance_id/generate-join-code - Generates a code that is used as a part of URL to join course
*/
#[instrument(skip(pool))]
async fn generate_join_code_for_course_instance(
id: web::Path<Uuid>,
pool: web::Data<PgPool>,
user: AuthUser,
) -> ControllerResult<HttpResponse> {
let mut conn = pool.acquire().await?;
let token = authorize(
&mut conn,
Act::Edit,
Some(user.id),
Res::CourseInstance(*id),
)
.await?;
let code = Uuid::new_v4().to_string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a random string would be better. Also, we have to keep in mind that these strings should be long enough that no one can guess them, and we have to keep them easy to type.
You could for example generate a random string that excludes look-alike characters. (https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-user-defined-characters)


models::course_instances::generate_join_code_for_course_instance(&mut conn, *id, code).await?;
token.authorized_ok(HttpResponse::Ok().finish())
}
/**
Add a route for each controller in this module.

Expand Down Expand Up @@ -502,5 +524,9 @@ pub fn _add_routes(cfg: &mut ServiceConfig) {
.route(
"/{course_instance_id}/default-certificate-configurations",
web::get().to(certificate_configurations),
)
.route(
"/{course_instance_id}/generate-join-code",
web::post().to(generate_join_code_for_course_instance),
);
}
Loading
Loading