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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use std::collections::HashMap;

use sqlx::{Postgres, QueryBuilder, Row};

use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Type)]
#[sqlx(type_name = "peer_review_question_type", rename_all = "snake_case")]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub enum PeerOrSelfReviewQuestionType {
    Essay,
    Scale,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct CmsPeerOrSelfReviewQuestion {
    pub id: Uuid,
    pub peer_or_self_review_config_id: Uuid,
    pub order_number: i32,
    pub question: String,
    pub question_type: PeerOrSelfReviewQuestionType,
    pub answer_required: bool,
    pub weight: f32,
}

impl From<PeerOrSelfReviewQuestion> for CmsPeerOrSelfReviewQuestion {
    fn from(prq: PeerOrSelfReviewQuestion) -> Self {
        Self {
            id: prq.id,
            peer_or_self_review_config_id: prq.peer_or_self_review_config_id,
            order_number: prq.order_number,
            question: prq.question,
            question_type: prq.question_type,
            answer_required: prq.answer_required,
            weight: prq.weight,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct PeerOrSelfReviewQuestion {
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub peer_or_self_review_config_id: Uuid,
    pub order_number: i32,
    pub question: String,
    pub question_type: PeerOrSelfReviewQuestionType,
    pub answer_required: bool,
    pub weight: f32,
}

pub async fn insert(
    conn: &mut PgConnection,
    pkey_policy: PKeyPolicy<Uuid>,
    new_peer_review_question: &CmsPeerOrSelfReviewQuestion,
) -> ModelResult<Uuid> {
    let res = sqlx::query!(
        "
INSERT INTO peer_or_self_review_questions (
    id,
    peer_or_self_review_config_id,
    order_number,
    question,
    question_type
  )
VALUES ($1, $2, $3, $4, $5)
RETURNING id
        ",
        pkey_policy.into_uuid(),
        new_peer_review_question.peer_or_self_review_config_id,
        new_peer_review_question.order_number,
        new_peer_review_question.question,
        new_peer_review_question.question_type as PeerOrSelfReviewQuestionType,
    )
    .fetch_one(conn)
    .await?;
    Ok(res.id)
}

pub async fn get_by_id(conn: &mut PgConnection, id: Uuid) -> ModelResult<PeerOrSelfReviewQuestion> {
    let res = sqlx::query_as!(
        PeerOrSelfReviewQuestion,
        r#"
SELECT id,
  created_at,
  updated_at,
  deleted_at,
  peer_or_self_review_config_id,
  order_number,
  question,
  question_type AS "question_type: _",
  answer_required,
  weight
FROM peer_or_self_review_questions
WHERE id = $1
  AND deleted_at IS NULL;
        "#,
        id,
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn get_by_ids(
    conn: &mut PgConnection,
    id: &[Uuid],
) -> ModelResult<Vec<PeerOrSelfReviewQuestion>> {
    let res = sqlx::query_as!(
        PeerOrSelfReviewQuestion,
        r#"
SELECT id,
  created_at,
  updated_at,
  deleted_at,
  peer_or_self_review_config_id,
  order_number,
  question,
  question_type AS "question_type: _",
  answer_required,
  weight
FROM peer_or_self_review_questions
WHERE id IN (
    SELECT UNNEST($1::uuid [])
  )
  AND deleted_at IS NULL;
        "#,
        id,
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}

pub async fn get_by_peer_or_self_review_configs_id(
    conn: &mut PgConnection,
    peer_review_id: Uuid,
) -> ModelResult<Vec<PeerOrSelfReviewQuestion>> {
    let res = sqlx::query_as!(
        PeerOrSelfReviewQuestion,
        r#"
SELECT id,
    created_at,
    updated_at,
    deleted_at,
    peer_or_self_review_config_id,
    order_number,
    question,
    question_type AS "question_type: _",
    answer_required,
    weight
FROM peer_or_self_review_questions
WHERE peer_or_self_review_config_id = $1
  AND deleted_at IS NULL;
        "#,
        peer_review_id,
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}

pub async fn get_all_by_peer_or_self_review_config_id(
    conn: &mut PgConnection,
    peer_or_self_review_config_id: Uuid,
) -> ModelResult<Vec<PeerOrSelfReviewQuestion>> {
    let res = sqlx::query_as!(
        PeerOrSelfReviewQuestion,
        r#"
SELECT id,
    created_at,
    updated_at,
    deleted_at,
    peer_or_self_review_config_id,
    order_number,
    question,
    question_type AS "question_type: _",
    answer_required,
    weight
FROM peer_or_self_review_questions
WHERE peer_or_self_review_config_id = $1
    AND deleted_at IS NULL;
        "#,
        peer_or_self_review_config_id
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}

pub async fn get_all_by_peer_or_self_review_config_id_as_map(
    conn: &mut PgConnection,
    peer_or_self_review_config_id: Uuid,
) -> ModelResult<HashMap<Uuid, PeerOrSelfReviewQuestion>> {
    let res = get_all_by_peer_or_self_review_config_id(conn, peer_or_self_review_config_id)
        .await?
        .into_iter()
        .map(|x| (x.id, x))
        .collect();
    Ok(res)
}

pub async fn get_by_page_id(
    conn: &mut PgConnection,
    page_id: Uuid,
) -> ModelResult<Vec<CmsPeerOrSelfReviewQuestion>> {
    let res = sqlx::query_as!(
        CmsPeerOrSelfReviewQuestion,
        r#"
SELECT prq.id as id,
  prq.peer_or_self_review_config_id as peer_or_self_review_config_id,
  prq.order_number as order_number,
  prq.question as question,
  prq.question_type AS "question_type: _",
  prq.answer_required as answer_required,
  prq.weight
from pages p
  join exercises e on p.id = e.page_id
  join peer_or_self_review_configs pr on e.id = pr.exercise_id
  join peer_or_self_review_questions prq on pr.id = prq.peer_or_self_review_config_id
where p.id = $1
  AND p.deleted_at IS NULL
  AND e.deleted_at IS NULL
  AND pr.deleted_at IS NULL
  AND prq.deleted_at IS NULL;
  "#,
        page_id
    )
    .fetch_all(conn)
    .await?;

    Ok(res)
}

pub async fn delete_peer_or_self_review_questions_by_peer_or_self_review_config_ids(
    conn: &mut PgConnection,
    peer_or_self_review_config_ids: &[Uuid],
) -> ModelResult<Vec<Uuid>> {
    let res = sqlx::query!(
        "
UPDATE peer_or_self_review_questions
SET deleted_at = now()
WHERE peer_or_self_review_config_id = ANY ($1)
AND deleted_at IS NULL
RETURNING id;
    ",
        peer_or_self_review_config_ids
    )
    .fetch_all(conn)
    .await?
    .into_iter()
    .map(|x| x.id)
    .collect();
    Ok(res)
}

pub async fn get_course_default_cms_peer_or_self_review_questions(
    conn: &mut PgConnection,
    peer_or_self_review_config_id: Uuid,
) -> ModelResult<Vec<CmsPeerOrSelfReviewQuestion>> {
    let res = sqlx::query_as!(
        CmsPeerOrSelfReviewQuestion,
        r#"
SELECT id,
  peer_or_self_review_config_id,
  order_number,
  question_type AS "question_type: _",
  question,
  answer_required,
  weight
FROM peer_or_self_review_questions
where peer_or_self_review_config_id = $1
  AND deleted_at IS NULL;
    "#,
        peer_or_self_review_config_id
    )
    .fetch_all(conn)
    .await?;

    Ok(res)
}

pub async fn upsert_multiple_peer_or_self_review_questions(
    conn: &mut PgConnection,
    peer_or_self_review_questions: &[CmsPeerOrSelfReviewQuestion],
) -> ModelResult<Vec<CmsPeerOrSelfReviewQuestion>> {
    let mut sql:QueryBuilder<Postgres> = sqlx::QueryBuilder::new("INSERT INTO peer_or_self_review_questions (id, peer_or_self_review_config_id, order_number, question_type, question, answer_required) ");

    sql.push_values(peer_or_self_review_questions, |mut x, prq| {
        x.push_bind(prq.id)
            .push_bind(prq.peer_or_self_review_config_id)
            .push_bind(prq.order_number)
            .push_bind(prq.question_type)
            .push_bind(prq.question.as_str())
            .push_bind(prq.answer_required);
    });
    sql.push(
        r#" ON CONFLICT (id) DO
UPDATE
SET peer_or_self_review_config_id = excluded.peer_or_self_review_config_id,
  order_number = excluded.order_number,
  question_type = excluded.question_type,
  question = excluded.question,
  answer_required = excluded.answer_required,
  deleted_at = NULL
RETURNING id;
"#,
    );

    let ids = sql
        .build()
        .fetch_all(&mut *conn)
        .await?
        .iter()
        .map(|x| x.get(0))
        .collect::<Vec<_>>();

    let res = sqlx::query_as!(
        CmsPeerOrSelfReviewQuestion,
        r#"
SELECT id,
  peer_or_self_review_config_id,
  order_number,
  question,
  question_type AS "question_type: _",
  answer_required,
  weight
from peer_or_self_review_questions
WHERE id IN (
    SELECT UNNEST($1::uuid [])
  )
  AND deleted_at IS NULL;
    "#,
        &ids
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}

/** Modifies the questions in memory so that the weights sum to either 0 or 1. */
pub fn normalize_cms_peer_or_self_review_questions(
    peer_or_self_review_questions: &mut [CmsPeerOrSelfReviewQuestion],
) {
    // All scales have to be answered, skipping them does not make sense.
    for question in peer_or_self_review_questions.iter_mut() {
        if question.question_type == PeerOrSelfReviewQuestionType::Scale {
            question.answer_required = true;
        }
    }
    peer_or_self_review_questions.sort_by(|a, b| a.order_number.cmp(&b.order_number));
    info!(
        "Peer review question weights before normalization: {:?}",
        peer_or_self_review_questions
            .iter()
            .map(|x| x.weight)
            .collect::<Vec<_>>()
    );
    let (mut allowed_to_have_weight, mut not_allowed_to_have_weight) =
        peer_or_self_review_questions
            .iter_mut()
            .partition::<Vec<_>, _>(|q| q.question_type == PeerOrSelfReviewQuestionType::Scale);
    let total_weight: f32 = allowed_to_have_weight.iter().map(|x| x.weight).sum();
    if total_weight == 0.0 {
        return;
    }
    for question in allowed_to_have_weight.iter_mut() {
        question.weight /= total_weight;
    }
    for question in not_allowed_to_have_weight.iter_mut() {
        question.weight = 0.0;
    }
    info!(
        "Peer review question weights after normalization: {:?}",
        peer_or_self_review_questions
            .iter()
            .map(|x| x.weight)
            .collect::<Vec<_>>()
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_normalize_cms_peer_or_self_review_questions() {
        let mut questions = vec![
            CmsPeerOrSelfReviewQuestion {
                id: Uuid::new_v4(),
                peer_or_self_review_config_id: Uuid::new_v4(),
                order_number: 1,
                question: String::from("Question 1"),
                question_type: PeerOrSelfReviewQuestionType::Scale,
                answer_required: true,
                weight: 2.0,
            },
            CmsPeerOrSelfReviewQuestion {
                id: Uuid::new_v4(),
                peer_or_self_review_config_id: Uuid::new_v4(),
                order_number: 2,
                question: String::from("Question 2"),
                question_type: PeerOrSelfReviewQuestionType::Scale,
                answer_required: true,
                weight: 3.0,
            },
            CmsPeerOrSelfReviewQuestion {
                id: Uuid::new_v4(),
                peer_or_self_review_config_id: Uuid::new_v4(),
                order_number: 3,
                question: String::from("Question 3"),
                question_type: PeerOrSelfReviewQuestionType::Essay,
                answer_required: true,
                weight: 1.0,
            },
        ];

        normalize_cms_peer_or_self_review_questions(&mut questions);

        assert_eq!(questions[0].weight, 2.0 / 5.0);
        assert_eq!(questions[1].weight, 3.0 / 5.0);
        assert_eq!(questions[2].weight, 0.0);
    }
}