1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use rand::Rng;

use crate::{exercise_slide_submissions::ExerciseSlideSubmission, prelude::*};

/// Returns an exercise slide submission id that has been given to be reviewed by the student within the hour.
/// Does not return submissions that no longer need peer review.
pub async fn try_to_restore_previously_given_exercise_slide_submission(
    conn: &mut PgConnection,
    exercise_id: Uuid,
    user_id: Uuid,
    course_instance_id: Uuid,
) -> ModelResult<Option<ExerciseSlideSubmission>> {
    let res = sqlx::query!(
        "
SELECT exercise_slide_submission_id
FROM offered_answers_to_peer_review_temporary
WHERE exercise_id = $1
  AND user_id = $2
  AND course_instance_id = $3
  AND created_at > now() - '1 hour'::interval
  ",
        exercise_id,
        user_id,
        course_instance_id,
    )
    .fetch_optional(&mut *conn)
    .await?;
    if rand::thread_rng().gen_range(0..10) == 0 {
        // Sometimes clean up the table
        delete_expired_records(&mut *conn).await?;
    }
    if let Some(res) = res {
        // In order to return the saved submission, it needs to have a peer review queue entry and the entry must not have received enough peer reviews.
        if let Some(peer_review_queue_entry) = crate::peer_review_queue_entries::try_to_get_by_receiving_submission_and_course_instance_ids(&mut *conn, res.exercise_slide_submission_id, course_instance_id).await? {
          if peer_review_queue_entry.received_enough_peer_reviews {
            return Ok(None);
          }
        } else {
          return Ok(None)
        }

        let ess = crate::exercise_slide_submissions::get_by_id(
            &mut *conn,
            res.exercise_slide_submission_id,
        )
        .await?;
        return Ok(Some(ess));
    }
    Ok(None)
}

/// Returns an exercise slide submission id that has been given to be reviewed by the student within the hour.
pub async fn save_given_exercise_slide_submission(
    conn: &mut PgConnection,
    exercise_slide_submission_id: Uuid,
    exercise_id: Uuid,
    user_id: Uuid,
    course_instance_id: Uuid,
) -> ModelResult<()> {
    let _res = sqlx::query!(
        "
INSERT INTO offered_answers_to_peer_review_temporary (
    exercise_slide_submission_id,
    user_id,
    course_instance_id,
    exercise_id
  )
VALUES ($1, $2, $3, $4) ON CONFLICT ON CONSTRAINT offered_answers_to_peer_review_temporary_pkey DO
UPDATE
SET exercise_slide_submission_id = $1,
  user_id = $2,
  course_instance_id = $3,
  exercise_id = $4,
  created_at = now()
",
        exercise_slide_submission_id,
        user_id,
        course_instance_id,
        exercise_id,
    )
    .execute(&mut *conn)
    .await?;

    Ok(())
}

/// For clearing the table after the user has given a peer review so that they can receive a new submission to be reviewed
pub async fn delete_saved_submissions_for_user(
    conn: &mut PgConnection,
    exercise_id: Uuid,
    user_id: Uuid,
    course_instance_id: Uuid,
) -> ModelResult<()> {
    info!("Deleting expired records from offered_answers_to_peer_review_temporary");
    let _res = sqlx::query!(
        "
DELETE FROM offered_answers_to_peer_review_temporary
WHERE exercise_id = $1 AND user_id = $2 AND course_instance_id = $3
",
        exercise_id,
        user_id,
        course_instance_id
    )
    .execute(&mut *conn)
    .await?;
    Ok(())
}

/// Deletes entries older than 1 hour -- for keeping the table small and fast
async fn delete_expired_records(conn: &mut PgConnection) -> ModelResult<()> {
    info!("Deleting expired records from offered_answers_to_peer_review_temporary");
    let _res = sqlx::query!(
        "
DELETE FROM offered_answers_to_peer_review_temporary
WHERE created_at < now() - '1 hour'::interval
"
    )
    .execute(&mut *conn)
    .await?;
    Ok(())
}