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

Way for study registeries to filter completions, exam instruction fixes #1197

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 14 additions & 4 deletions services/course-material/src/components/exams/ExamStartBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { css } from "@emotion/css"
import React, { useState } from "react"
import { useTranslation } from "react-i18next"

import { ExamEnrollmentData } from "../../shared-module/bindings"
import Button from "../../shared-module/components/Button"
import { baseTheme } from "../../shared-module/styles"

export interface ExamInstructionsProps {
onStart: () => Promise<void>
canStartExam: boolean
examHasStarted: boolean
examHasEnded: boolean
timeMinutes: number
examEnrollmentData: ExamEnrollmentData
}

const ExamStartBanner: React.FC<React.PropsWithChildren<ExamInstructionsProps>> = ({
onStart,
canStartExam,
examEnrollmentData,
examHasStarted,
examHasEnded,
timeMinutes,
Expand Down Expand Up @@ -69,15 +70,24 @@ const ExamStartBanner: React.FC<React.PropsWithChildren<ExamInstructionsProps>>
>
{children}
</p>
{!canStartExam && <p>{t("you-are-not-eligible-for-taking-this-exam")}</p>}
{examEnrollmentData.tag === "NotEnrolled" && !examEnrollmentData.can_enroll && (
<p>{t("message-you-have-not-met-the-requirements-for-taking-this-exam")}</p>
)}
{!examHasStarted && !examHasEnded && <p>{t("message-the-exam-has-not-started-yet")}</p>}
<div
className={css`
text-align: center;
margin-top: 1rem;
`}
>
<Button
onClick={handleStart}
disabled={!examHasStarted || examHasEnded || !canStartExam || disabled}
disabled={
!examHasStarted ||
examHasEnded ||
(examEnrollmentData.tag === "NotEnrolled" && !examEnrollmentData.can_enroll) ||
disabled
}
variant="primary"
size="medium"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface ExamProps {
}

const Exam: React.FC<React.PropsWithChildren<ExamProps>> = ({ query }) => {
const { t } = useTranslation()
const { t, i18n } = useTranslation()
const examId = query.id
const [pageState, pageStateDispatch] = useReducer(
pageStateReducer,
Expand Down Expand Up @@ -65,6 +65,15 @@ const Exam: React.FC<React.PropsWithChildren<ExamProps>> = ({ query }) => {
}
}, [exam.isError, exam.isSuccess, exam.data, exam.error])

useEffect(() => {
if (!exam.data) {
return
}
if (i18n.language !== exam.data.language) {
i18n.changeLanguage(exam.data.language)
}
})

const layoutContext = useContext(LayoutContext)
useEffect(() => {
layoutContext.setOrganizationSlug(query.organizationSlug)
Expand Down Expand Up @@ -172,9 +181,6 @@ const Exam: React.FC<React.PropsWithChildren<ExamProps>> = ({ query }) => {
</BreakFromCentered>
)

const canStartExam =
exam.data.enrollment_data.tag === "NotEnrolled" ? exam.data.enrollment_data.can_enroll : false

if (
exam.data.enrollment_data.tag === "NotEnrolled" ||
exam.data.enrollment_data.tag === "NotYetStarted"
Expand All @@ -188,7 +194,7 @@ const Exam: React.FC<React.PropsWithChildren<ExamProps>> = ({ query }) => {
await enrollInExam(examId)
exam.refetch()
}}
canStartExam={canStartExam}
examEnrollmentData={exam.data.enrollment_data}
examHasStarted={exam.data.starts_at ? isPast(exam.data.starts_at) : false}
examHasEnded={exam.data.ends_at ? isPast(exam.data.ends_at) : false}
timeMinutes={exam.data.time_minutes}
Expand Down

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.

29 changes: 23 additions & 6 deletions services/headless-lms/models/src/course_module_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use futures::Stream;

use crate::prelude::*;
use crate::{prelude::*, study_registry_registrars::StudyRegistryRegistrar};

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
Expand Down Expand Up @@ -541,25 +541,42 @@ impl StudyRegistryGrade {
}
}
}

/// Streams completions.
///
/// If no_completions_registered_by_this_study_registry_registrar is None, then all completions are streamed.
pub fn stream_by_course_module_id<'a>(
conn: &'a mut PgConnection,
course_module_ids: &'a [Uuid],
) -> impl Stream<Item = sqlx::Result<StudyRegistryCompletion>> + 'a {
sqlx::query_as!(
no_completions_registered_by_this_study_registry_registrar: &'a Option<StudyRegistryRegistrar>,
) -> impl Stream<Item = sqlx::Result<StudyRegistryCompletion>> + Send + 'a {
// If this is none, we're using a null uuid, which will never match anything. Therefore, no completions will be filtered out.
let study_module_registrar_id = no_completions_registered_by_this_study_registry_registrar
.clone()
.map(|o| o.id)
.unwrap_or(Uuid::nil());
let res = sqlx::query_as!(
CourseModuleCompletion,
r#"
SELECT *
FROM course_module_completions
WHERE course_module_id = ANY($1)
AND prerequisite_modules_completed
AND eligible_for_ects
AND eligible_for_ects IS TRUE
AND deleted_at IS NULL
AND id NOT IN (
SELECT course_module_completion_id
FROM course_module_completion_registered_to_study_registries
WHERE course_module_id = ANY($1)
AND study_registry_registrar_id = $2
AND deleted_at IS NULL
)
"#,
course_module_ids,
study_module_registrar_id,
)
.map(StudyRegistryCompletion::from)
.fetch(conn)
.fetch(conn);
res
}

pub async fn delete(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
Expand Down
6 changes: 5 additions & 1 deletion services/headless-lms/models/src/exams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ pub struct Exam {
pub id: Uuid,
pub name: String,
pub instructions: serde_json::Value,
// TODO: page_id is not in the exams table, prevents from using select * with query_as!
pub page_id: Uuid,
pub courses: Vec<Course>,
pub starts_at: Option<DateTime<Utc>>,
pub ends_at: Option<DateTime<Utc>>,
pub time_minutes: i32,
pub minimum_points_treshold: i32,
pub language: String,
}

impl Exam {
Expand Down Expand Up @@ -60,7 +62,8 @@ SELECT exams.id,
exams.starts_at,
exams.ends_at,
exams.time_minutes,
exams.minimum_points_treshold
exams.minimum_points_treshold,
exams.language
FROM exams
JOIN pages ON pages.exam_id = exams.id
WHERE exams.id = $1
Expand Down Expand Up @@ -109,6 +112,7 @@ WHERE course_exams.exam_id = $1
time_minutes: exam.time_minutes,
courses,
minimum_points_treshold: exam.minimum_points_treshold,
language: exam.language.unwrap_or("en-US".to_string()),
})
}

Expand Down
1 change: 1 addition & 0 deletions services/headless-lms/models/src/library/copying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ WHERE id = $2;
time_minutes: copied_exam.time_minutes,
page_id: get_page_id.page_id,
minimum_points_treshold: copied_exam.minimum_points_treshold,
language: copied_exam.language.unwrap_or("en-US".to_string()),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,24 @@ pub struct ExamData {
pub ended: bool,
pub time_minutes: i32,
pub enrollment_data: ExamEnrollmentData,
pub language: String,
}

#[derive(Debug, Serialize)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
#[serde(tag = "tag")]
pub enum ExamEnrollmentData {
/// The student has enrolled to the exam and started it.
EnrolledAndStarted {
page_id: Uuid,
page: Box<Page>,
enrollment: ExamEnrollment,
},
NotEnrolled {
can_enroll: bool,
},
/// The student has not enrolled to the exam yet. However, the the exam is open.
NotEnrolled { can_enroll: bool },
/// The exam's start time is in the future, no one can enroll yet.
NotYetStarted,
/// The exam is still open but the student has run out of time.
StudentTimeUp,
}

Expand Down Expand Up @@ -145,6 +148,7 @@ pub async fn fetch_exam_for_user(
ended,
time_minutes: exam.time_minutes,
enrollment_data: ExamEnrollmentData::NotYetStarted,
language: exam.language,
}));
}

Expand All @@ -166,6 +170,7 @@ pub async fn fetch_exam_for_user(
ended,
time_minutes: exam.time_minutes,
enrollment_data: ExamEnrollmentData::StudentTimeUp,
language: exam.language,
}));
}
enrollment
Expand All @@ -183,6 +188,7 @@ pub async fn fetch_exam_for_user(
ended,
time_minutes: exam.time_minutes,
enrollment_data: ExamEnrollmentData::NotEnrolled { can_enroll },
language: exam.language,
}));
};

Expand All @@ -202,6 +208,7 @@ pub async fn fetch_exam_for_user(
page: Box::new(page),
enrollment,
},
language: exam.language,
}))
}

Expand Down
Loading
Loading