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
use anyhow::Result;
use bytes::Bytes;
use headless_lms_models::course_instances;

use async_trait::async_trait;
use itertools::Itertools;
use models::library::progressing;

use crate::domain::csv_export::CsvWriter;

use sqlx::PgConnection;
use std::io::Write;
use tokio::sync::mpsc::UnboundedSender;

use uuid::Uuid;

use crate::prelude::*;

use super::{
    super::authorization::{AuthorizationToken, AuthorizedResponse},
    course_module_completion_info_to_grade_string, CSVExportAdapter, CsvExportDataLoader,
};

pub struct CompletionsExportOperation {
    pub course_instance_id: Uuid,
}

#[async_trait]
impl CsvExportDataLoader for CompletionsExportOperation {
    async fn load_data(
        &self,
        sender: UnboundedSender<Result<AuthorizedResponse<Bytes>, ControllerError>>,
        conn: &mut PgConnection,
        token: AuthorizationToken,
    ) -> anyhow::Result<CSVExportAdapter> {
        export_completions(
            &mut *conn,
            self.course_instance_id,
            CSVExportAdapter {
                sender,
                authorization_token: token,
            },
        )
        .await
    }
}

/// Writes the completions as csv into the writer
pub async fn export_completions<W>(
    conn: &mut PgConnection,
    course_instance_id: Uuid,
    writer: W,
) -> Result<W>
where
    W: Write + Send + 'static,
{
    // fetch summary
    let course_instance = course_instances::get_course_instance(conn, course_instance_id).await?;
    let summary =
        progressing::get_course_instance_completion_summary(conn, &course_instance).await?;

    // sort modules
    let mut modules = summary.course_modules;
    modules.sort_by_key(|m| m.order_number);

    // prepare headers
    let mut headers = vec![
        "user_id".to_string(),
        "first_name".to_string(),
        "last_name".to_string(),
        "email".to_string(),
    ];
    for module in &modules {
        let module_name = module.name.as_deref().unwrap_or("default_module");
        headers.push(format!("{module_name}_grade"));
        headers.push(format!("{module_name}_registered"));
        headers.push(format!("{module_name}_completion_date"));
    }

    // write rows
    let writer = CsvWriter::new_with_initialized_headers(writer, headers).await?;
    for user in summary.users_with_course_module_completions {
        let mut has_completed_some_module = false;

        let mut csv_row = vec![
            user.user_id.to_string(),
            user.first_name.unwrap_or_default(),
            user.last_name.unwrap_or_default(),
            user.email,
        ];
        for module in &modules {
            let user_completion = user
                .completed_modules
                .iter()
                // sort by created at, latest timestamp first
                .sorted_by(|a, b| b.created_at.cmp(&a.created_at))
                .find(|cm| cm.course_module_id == module.id);
            if user_completion.is_some() {
                has_completed_some_module = true;
            }
            let grade = course_module_completion_info_to_grade_string(user_completion);
            csv_row.push(grade);
            let registered = user_completion
                .map(|cm| cm.registered.to_string())
                .unwrap_or_default();
            csv_row.push(registered);
            csv_row.push(
                user_completion
                    .map(|uc| uc.completion_date.to_rfc3339())
                    .unwrap_or_default(),
            )
        }
        // To avoid confusion with some people potentially not understanding that '-' means not completed,
        // we'll skip the users that don't have any completions from any modules. The confusion is less likely in cases where there are more than one module, and only in those cases the teachers would see the '-' entries in this file.
        if has_completed_some_module {
            writer.write_record(csv_row);
        }
    }
    let writer = writer.finish().await?;
    Ok(writer)
}

pub struct CourseInstancesExportOperation {
    pub course_id: Uuid,
}

#[async_trait]
impl CsvExportDataLoader for CourseInstancesExportOperation {
    async fn load_data(
        &self,
        sender: UnboundedSender<Result<AuthorizedResponse<Bytes>, ControllerError>>,
        conn: &mut PgConnection,
        token: AuthorizationToken,
    ) -> anyhow::Result<CSVExportAdapter> {
        export_course_instances(
            &mut *conn,
            self.course_id,
            CSVExportAdapter {
                sender,
                authorization_token: token,
            },
        )
        .await
    }
}

/// Writes the course instances as csv into the writer
pub async fn export_course_instances<W>(
    conn: &mut PgConnection,
    course_id: Uuid,
    writer: W,
) -> Result<W>
where
    W: Write + Send + 'static,
{
    let course_instances =
        course_instances::get_course_instances_for_course(conn, course_id).await?;

    let headers = IntoIterator::into_iter([
        "id".to_string(),
        "created_at".to_string(),
        "updated_at".to_string(),
        "name".to_string(),
    ]);
    let writer = CsvWriter::new_with_initialized_headers(writer, headers).await?;

    for next in course_instances.into_iter() {
        let csv_row = vec![
            next.id.to_string(),
            next.created_at.to_rfc3339(),
            next.updated_at.to_rfc3339(),
            next.name.unwrap_or_default(),
        ];
        writer.write_record(csv_row);
    }
    let writer = writer.finish().await?;
    Ok(writer)
}