Flag no-shows as absent once window closes
Browse files at this commit094486136c4aa2a1f03f89969c29c1c52d010915Parent: 3375565- feat
- flag no-shows as absent once window closes
3 changed files +162 -10
- README.md +6 -2
- src/web/exams.rs +24 -8
- tests/integration.rs +132 -0
README.mdmodified+6 -2
| @@ -460,10 +460,14 @@ deadline rules mid-sitting would be a different exam (`409`). | |||
| 460 | 460 | ||
| 461 | The course's manager (its creator, or manager+) watches it all live: | 461 | The course's manager (its creator, or manager+) watches it all live: |
| 462 | `GET /exams/{id}/live` returns one snapshot — | 462 | `GET /exams/{id}/live` returns one snapshot — |
| 463 | -the enrolled roster joined with attempts and marks (`not_started` | | 463 | +the enrolled roster joined with attempts and marks (`not_started` | `absent` | |
| 464 | `in_progress` | `submitted` | `expired`, per-student `attempt` / | 464 | `in_progress` | `submitted` | `expired`, per-student `attempt` / |
| 465 | `attempts_used` / `left_at` / `deadline` / `remaining_ms` / `mark`, always the | 465 | `attempts_used` / `left_at` / `deadline` / `remaining_ms` / `mark`, always the |
| 466 | -latest sitting) plus summary counts, all judged at a single `now`. | 466 | +latest sitting) plus summary counts, all judged at a single `now`. Once the |
| 467 | +window closes, `not_started` hardens into `absent` — the no-shows, flagged | ||
| 468 | +right in the roster (open exams have no window, so never an `absent`). The | ||
| 469 | +flag is informational: an absent student still has no mark until the teacher | ||
| 470 | +records one through `POST /exams/{id}/results`. | ||
| 467 | `GET /exams/{id}/live/stream` is the same JSON as Server-Sent Events: a | 471 | `GET /exams/{id}/live/stream` is the same JSON as Server-Sent Events: a |
| 468 | `snapshot` event immediately on connect, then every ~2 s — attendance, ticking | 472 | `snapshot` event immediately on connect, then every ~2 s — attendance, ticking |
| 469 | clocks, submissions, and marks land without polling: | 473 | clocks, submissions, and marks land without polling: |
src/web/exams.rsmodified+24 -8
| @@ -907,7 +907,10 @@ async fn finish_attempt( | |||
| 907 | #[derive(Serialize, ToSchema)] | 907 | #[derive(Serialize, ToSchema)] |
| 908 | struct LiveStudentResponse { | 908 | struct LiveStudentResponse { |
| 909 | user: PersonRef, | 909 | user: PersonRef, |
| 910 | - /// `not_started` | `in_progress` | `submitted` | `expired`. | 910 | + /// `not_started` | `absent` | `in_progress` | `submitted` | `expired`. |
| 911 | + /// `absent` is `not_started` after the window closed: the student never | ||
| 912 | + /// sat and no longer can. Open exams have no window, so nobody is ever | ||
| 913 | + /// absent from one. | ||
| 911 | #[schema(example = "in_progress")] | 914 | #[schema(example = "in_progress")] |
| 912 | status: String, | 915 | status: String, |
| 913 | /// Which sitting the shown attempt is (1, 2, …); `null` before the first. | 916 | /// Which sitting the shown attempt is (1, 2, …); `null` before the first. |
| @@ -934,7 +937,11 @@ struct LiveStudentResponse { | |||
| 934 | #[derive(Serialize, ToSchema)] | 937 | #[derive(Serialize, ToSchema)] |
| 935 | struct LiveCountsResponse { | 938 | struct LiveCountsResponse { |
| 936 | enrolled: u64, | 939 | enrolled: u64, |
| 940 | + /// Enrolled with no attempt while the window is still open (or the exam | ||
| 941 | + /// has no window). | ||
| 937 | not_started: u64, | 942 | not_started: u64, |
| 943 | + /// Enrolled with no attempt and the window closed — the no-shows. | ||
| 944 | + absent: u64, | ||
| 938 | in_progress: u64, | 945 | in_progress: u64, |
| 939 | submitted: u64, | 946 | submitted: u64, |
| 940 | expired: u64, | 947 | expired: u64, |
| @@ -1000,6 +1007,10 @@ async fn live_snapshot(exam: &Exam, db: &Database) -> Result<ExamLiveResponse, A | |||
| 1000 | } | 1007 | } |
| 1001 | let people = person_map(roster.iter().map(|e| e.get_user().clone()), db).await?; | 1008 | let people = person_map(roster.iter().map(|e| e.get_user().clone()), db).await?; |
| 1002 | 1009 | ||
| 1010 | + // Once the window closes the door is shut for good (starting answers | ||
| 1011 | + // 409), so "hasn't started" hardens into "was absent". Open exams have | ||
| 1012 | + // no window and never make that call. | ||
| 1013 | + let window_over = exam.get_ends_at().is_some_and(|ends| now >= ends); | ||
| 1003 | let mut students: Vec<LiveStudentResponse> = roster | 1014 | let mut students: Vec<LiveStudentResponse> = roster |
| 1004 | .iter() | 1015 | .iter() |
| 1005 | .map(|enrollment| { | 1016 | .map(|enrollment| { |
| @@ -1009,9 +1020,12 @@ async fn live_snapshot(exam: &Exam, db: &Database) -> Result<ExamLiveResponse, A | |||
| 1009 | let deadline = attempt.and_then(|a| a.deadline(exam)); | 1020 | let deadline = attempt.and_then(|a| a.deadline(exam)); |
| 1010 | LiveStudentResponse { | 1021 | LiveStudentResponse { |
| 1011 | user: PersonRef::resolve(&people, enrollment.get_user()), | 1022 | user: PersonRef::resolve(&people, enrollment.get_user()), |
| 1012 | - status: status | 1023 | + status: match status { |
| 1013 | - .map_or("not_started", AttemptStatus::as_str) | 1024 | + Some(status) => status.as_str(), |
| 1014 | - .to_string(), | 1025 | + None if window_over => "absent", |
| 1026 | + None => "not_started", | ||
| 1027 | + } | ||
| 1028 | + .to_string(), | ||
| 1015 | attempt: attempt.map(ExamAttempt::get_seq), | 1029 | attempt: attempt.map(ExamAttempt::get_seq), |
| 1016 | attempts_used: used.get(key).copied().unwrap_or(0), | 1030 | attempts_used: used.get(key).copied().unwrap_or(0), |
| 1017 | started_at: attempt.map(|a| a.get_started_at().as_millis()), | 1031 | started_at: attempt.map(|a| a.get_started_at().as_millis()), |
| @@ -1035,6 +1049,7 @@ async fn live_snapshot(exam: &Exam, db: &Database) -> Result<ExamLiveResponse, A | |||
| 1035 | let counts = LiveCountsResponse { | 1049 | let counts = LiveCountsResponse { |
| 1036 | enrolled: students.len() as u64, | 1050 | enrolled: students.len() as u64, |
| 1037 | not_started: count("not_started"), | 1051 | not_started: count("not_started"), |
| 1052 | + absent: count("absent"), | ||
| 1038 | in_progress: count("in_progress"), | 1053 | in_progress: count("in_progress"), |
| 1039 | submitted: count("submitted"), | 1054 | submitted: count("submitted"), |
| 1040 | expired: count("expired"), | 1055 | expired: count("expired"), |
| @@ -1050,10 +1065,11 @@ async fn live_snapshot(exam: &Exam, db: &Database) -> Result<ExamLiveResponse, A | |||
| 1050 | } | 1065 | } |
| 1051 | 1066 | ||
| 1052 | /// A one-shot live snapshot of the exam: who's in, who's still writing (and | 1067 | /// A one-shot live snapshot of the exam: who's in, who's still writing (and |
| 1053 | -/// on which sitting), who walked out of the room (`left_at`), time each | 1068 | +/// on which sitting), who walked out of the room (`left_at`), who never |
| 1054 | -/// student has left, and marks as they land. Requires teacher+ and | 1069 | +/// showed at all (`absent`, once the window is over), time each student has |
| 1055 | -/// management rights over the exam's course. For a self-updating feed of the | 1070 | +/// left, and marks as they land. Requires teacher+ and management rights |
| 1056 | -/// same shape, see `GET /exams/{id}/live/stream`. | 1071 | +/// over the exam's course. For a self-updating feed of the same shape, see |
| 1072 | +/// `GET /exams/{id}/live/stream`. | ||
| 1057 | #[utoipa::path( | 1073 | #[utoipa::path( |
| 1058 | get, | 1074 | get, |
| 1059 | path = "/{id}/live", | 1075 | path = "/{id}/live", |
tests/integration.rsmodified+132 -0
| @@ -3788,6 +3788,138 @@ async fn sync_attempt_lifecycle_feeds_the_live_monitor() { | |||
| 3788 | assert_eq!(res.status, StatusCode::FORBIDDEN); | 3788 | assert_eq!(res.status, StatusCode::FORBIDDEN); |
| 3789 | } | 3789 | } |
| 3790 | 3790 | ||
| 3791 | +/// Once the window closes, an enrolled student who never sat turns from | ||
| 3792 | +/// `not_started` into `absent` on the monitor — started-but-unsubmitted stays | ||
| 3793 | +/// `expired`, and an open exam (no window) never flags anyone. | ||
| 3794 | +#[tokio::test] | ||
| 3795 | +async fn closed_window_flags_no_shows_as_absent() { | ||
| 3796 | + let (app, db) = app_and_db().await; | ||
| 3797 | + let teacher = login_as(&app, &db, "abs_t", "teacher").await; | ||
| 3798 | + let ayse = login(&app, "ayse").await; // never shows up | ||
| 3799 | + let veli = login(&app, "veli").await; // submits | ||
| 3800 | + let cem = login(&app, "cem").await; // starts, never submits | ||
| 3801 | + let ayse_id = me_id(&app, &ayse).await; | ||
| 3802 | + let veli_id = me_id(&app, &veli).await; | ||
| 3803 | + let cem_id = me_id(&app, &cem).await; | ||
| 3804 | + let course = create_course(&app, &teacher, "history").await; | ||
| 3805 | + enroll(&app, &teacher, &course, &ayse_id).await; | ||
| 3806 | + enroll(&app, &teacher, &course, &veli_id).await; | ||
| 3807 | + enroll(&app, &teacher, &course, &cem_id).await; | ||
| 3808 | + | ||
| 3809 | + let now = Timestamp::now().as_millis(); | ||
| 3810 | + let exam = scheduled_exam( | ||
| 3811 | + &app, | ||
| 3812 | + &teacher, | ||
| 3813 | + &course, | ||
| 3814 | + json!({ "title": "midterm", "kind": "midterm", "weight": 2, | ||
| 3815 | + "mode": "sync", "starts_at": now - 50_000, "ends_at": now + 600_000 }), | ||
| 3816 | + ) | ||
| 3817 | + .await; | ||
| 3818 | + | ||
| 3819 | + for cookie in [&veli, &cem] { | ||
| 3820 | + let res = send( | ||
| 3821 | + &app, | ||
| 3822 | + "POST", | ||
| 3823 | + &format!("/exams/{exam}/attempt"), | ||
| 3824 | + Some(cookie), | ||
| 3825 | + None, | ||
| 3826 | + ) | ||
| 3827 | + .await; | ||
| 3828 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 3829 | + } | ||
| 3830 | + let res = send( | ||
| 3831 | + &app, | ||
| 3832 | + "POST", | ||
| 3833 | + &format!("/exams/{exam}/attempt/finish"), | ||
| 3834 | + Some(&veli), | ||
| 3835 | + None, | ||
| 3836 | + ) | ||
| 3837 | + .await; | ||
| 3838 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 3839 | + | ||
| 3840 | + // Window still open: the no-show is merely `not_started`. | ||
| 3841 | + let res = send( | ||
| 3842 | + &app, | ||
| 3843 | + "GET", | ||
| 3844 | + &format!("/exams/{exam}/live"), | ||
| 3845 | + Some(&teacher), | ||
| 3846 | + None, | ||
| 3847 | + ) | ||
| 3848 | + .await; | ||
| 3849 | + assert_eq!(res.status, StatusCode::OK); | ||
| 3850 | + assert_eq!(res.body["counts"]["not_started"], 1); | ||
| 3851 | + assert_eq!(res.body["counts"]["absent"], 0); | ||
| 3852 | + assert_eq!(res.body["students"][0]["user"]["username"], "ayse"); | ||
| 3853 | + assert_eq!(res.body["students"][0]["status"], "not_started"); | ||
| 3854 | + | ||
| 3855 | + // The teacher cuts the window short (inside the backdating grace): the | ||
| 3856 | + // exam is over, and the gap hardens into an absence. | ||
| 3857 | + let res = send( | ||
| 3858 | + &app, | ||
| 3859 | + "PATCH", | ||
| 3860 | + &format!("/exams/{exam}"), | ||
| 3861 | + Some(&teacher), | ||
| 3862 | + Some(json!({ "ends_at": now - 10_000 })), | ||
| 3863 | + ) | ||
| 3864 | + .await; | ||
| 3865 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 3866 | + | ||
| 3867 | + let res = send( | ||
| 3868 | + &app, | ||
| 3869 | + "GET", | ||
| 3870 | + &format!("/exams/{exam}/live"), | ||
| 3871 | + Some(&teacher), | ||
| 3872 | + None, | ||
| 3873 | + ) | ||
| 3874 | + .await; | ||
| 3875 | + assert_eq!(res.status, StatusCode::OK); | ||
| 3876 | + assert_eq!(res.body["counts"]["enrolled"], 3); | ||
| 3877 | + assert_eq!(res.body["counts"]["not_started"], 0); | ||
| 3878 | + assert_eq!(res.body["counts"]["absent"], 1); | ||
| 3879 | + assert_eq!(res.body["counts"]["submitted"], 1); | ||
| 3880 | + assert_eq!(res.body["counts"]["expired"], 1); | ||
| 3881 | + let students = res.body["students"].as_array().expect("students"); | ||
| 3882 | + assert_eq!(students[0]["user"]["username"], "ayse"); | ||
| 3883 | + assert_eq!(students[0]["status"], "absent"); | ||
| 3884 | + assert_eq!(students[1]["user"]["username"], "cem"); | ||
| 3885 | + assert_eq!(students[1]["status"], "expired"); | ||
| 3886 | + assert_eq!(students[2]["user"]["username"], "veli"); | ||
| 3887 | + assert_eq!(students[2]["status"], "submitted"); | ||
| 3888 | + | ||
| 3889 | + // Absence is informational — the mark stays a human call, and the | ||
| 3890 | + // teacher may still record one for the no-show. | ||
| 3891 | + assert!(students[0]["mark"].is_null()); | ||
| 3892 | + let res = send( | ||
| 3893 | + &app, | ||
| 3894 | + "POST", | ||
| 3895 | + &format!("/exams/{exam}/results"), | ||
| 3896 | + Some(&teacher), | ||
| 3897 | + Some(json!({ "mark": 0, "user_id": ayse_id })), | ||
| 3898 | + ) | ||
| 3899 | + .await; | ||
| 3900 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 3901 | + | ||
| 3902 | + // An open exam has no window: never started is never absent. | ||
| 3903 | + let open = scheduled_exam( | ||
| 3904 | + &app, | ||
| 3905 | + &teacher, | ||
| 3906 | + &course, | ||
| 3907 | + json!({ "title": "essay", "kind": "homework", "weight": 1, "mode": "open" }), | ||
| 3908 | + ) | ||
| 3909 | + .await; | ||
| 3910 | + let res = send( | ||
| 3911 | + &app, | ||
| 3912 | + "GET", | ||
| 3913 | + &format!("/exams/{open}/live"), | ||
| 3914 | + Some(&teacher), | ||
| 3915 | + None, | ||
| 3916 | + ) | ||
| 3917 | + .await; | ||
| 3918 | + assert_eq!(res.status, StatusCode::OK); | ||
| 3919 | + assert_eq!(res.body["counts"]["not_started"], 3); | ||
| 3920 | + assert_eq!(res.body["counts"]["absent"], 0); | ||
| 3921 | +} | ||
| 3922 | + | ||
| 3791 | #[tokio::test] | 3923 | #[tokio::test] |
| 3792 | async fn async_deadline_is_start_plus_duration_clamped_to_window() { | 3924 | async fn async_deadline_is_start_plus_duration_clamped_to_window() { |
| 3793 | let (app, db) = app_and_db().await; | 3925 | let (app, db) = app_and_db().await; |