Skip to main content

headless_lms_server/programs/seed/seed_courses/
seed_switching_course_instances_course.rs

1use crate::{
2    prelude::*,
3    programs::seed::{
4        builder::{
5            chapter::ChapterBuilder,
6            context::SeedContext,
7            course::{CourseBuilder, CourseInstanceConfig},
8            exercise::{ExerciseBuilder, ExerciseIds},
9            module::ModuleBuilder,
10            page::PageBuilder,
11        },
12        seed_courses::CommonCourseData,
13        seed_helpers::paragraph,
14        seed_users::SeedUsersResult,
15    },
16};
17use anyhow::Result;
18use chrono::{TimeZone, Utc};
19use headless_lms_models::roles::UserRole;
20use serde_json::json;
21
22pub async fn seed_switching_course_instances_course(
23    app_config: &ApplicationConfiguration,
24    course_id: Uuid,
25    course_name: &str,
26    course_slug: &str,
27    common_course_data: CommonCourseData,
28    can_add_chatbot: bool,
29    seed_users_result: SeedUsersResult,
30) -> Result<Uuid> {
31    let CommonCourseData {
32        db_pool,
33        organization_id: org,
34        teacher_user_id,
35        student_user_id: _student,
36        langs_user_id: _langs_user_id,
37        example_normal_user_ids: _users,
38        jwt_key: _jwt_key,
39        base_url: _base_url,
40    } = common_course_data;
41
42    let mut conn = db_pool.acquire().await?;
43    let cx = SeedContext {
44        teacher: teacher_user_id,
45        org,
46        base_course_ns: course_id,
47    };
48
49    info!(
50        "Inserting switching course instances course {}",
51        course_name
52    );
53
54    let course = CourseBuilder::new(course_name, course_slug)
55        .desc("Sample course.")
56        .chatbot(can_add_chatbot)
57        .course_id(course_id)
58        .instance(CourseInstanceConfig {
59            name: None,
60            description: None,
61            support_email: None,
62            teacher_in_charge_name: "admin".to_string(),
63            teacher_in_charge_email: "admin@example.com".to_string(),
64            opening_time: None,
65            closing_time: None,
66            instance_id: Some(cx.v5(b"instance:default")),
67        })
68        .instance(CourseInstanceConfig {
69            name: Some("Non-default instance".to_string()),
70            description: Some("This is a non-default instance".to_string()),
71            support_email: Some("contact@example.com".to_string()),
72            teacher_in_charge_name: "admin".to_string(),
73            teacher_in_charge_email: "admin@example.com".to_string(),
74            opening_time: None,
75            closing_time: None,
76            instance_id: Some(cx.v5(b"instance:non-default")),
77        })
78        .role(seed_users_result.teacher_user_id, UserRole::Teacher)
79        .module(
80            ModuleBuilder::new()
81                .order(0)
82                .automatic_completion(Some(1), Some(1), false)
83                .chapter(
84                    ChapterBuilder::new(1, "The Basics")
85                        .opens(Utc::now())
86                        .deadline(Utc.with_ymd_and_hms(2225, 1, 1, 23, 59, 59).unwrap())
87                        .fixed_ids(cx.v5(b"chapter:1"), cx.v5(b"chapter:1:instance"))
88                        .page(
89                            PageBuilder::new("/chapter-1/page-1", "Page One")
90                                .block(paragraph(
91                                    "This is a simple introduction to the basics.",
92                                    cx.v5(b"page:1:1:block:intro"),
93                                ))
94                                .exercise(ExerciseBuilder::example_exercise(
95                                    "Simple multiple choice",
96                                    ExerciseIds {
97                                        exercise_id: cx.v5(b"exercise:1:1:e"),
98                                        slide_id: cx.v5(b"exercise:1:1:s"),
99                                        task_id: cx.v5(b"exercise:1:1:t"),
100                                        block_id: cx.v5(b"exercise:1:1:b"),
101                                    },
102                                    vec![paragraph(
103                                        "What is 2 + 2?",
104                                        cx.v5(b"exercise:1:1:prompt"),
105                                    )],
106                                    json!([
107                                        {
108                                            "name": "3",
109                                            "correct": false,
110                                            "id": cx.v5(b"exercise:1:1:option:1")
111                                        },
112                                        {
113                                            "name": "4",
114                                            "correct": true,
115                                            "id": cx.v5(b"exercise:1:1:option:2")
116                                        },
117                                        {
118                                            "name": "5",
119                                            "correct": false,
120                                            "id": cx.v5(b"exercise:1:1:option:3")
121                                        }
122                                    ]),
123                                    false,
124                                    None,
125                                    None,
126                                )),
127                        ),
128                ),
129        )
130        .module(
131            ModuleBuilder::new()
132                .order(1)
133                .name("Another module")
134                .automatic_completion(Some(1), Some(1), false)
135                .ects(5.0)
136                .chapter(
137                    ChapterBuilder::new(2, "Another chapter")
138                        .fixed_ids(cx.v5(b"chapter:2"), cx.v5(b"chapter:2:instance"))
139                        .page(
140                            PageBuilder::new("/chapter-2/page-1", "Simple Page")
141                                .block(paragraph(
142                                    "This is another simple page with basic content.",
143                                    cx.v5(b"page:2:1:block:intro"),
144                                ))
145                                .exercise(ExerciseBuilder::example_exercise(
146                                    "Simple question",
147                                    ExerciseIds {
148                                        exercise_id: cx.v5(b"exercise:2:1:e"),
149                                        slide_id: cx.v5(b"exercise:2:1:s"),
150                                        task_id: cx.v5(b"exercise:2:1:t"),
151                                        block_id: cx.v5(b"exercise:2:1:b"),
152                                    },
153                                    vec![paragraph(
154                                        "What color is the sky?",
155                                        cx.v5(b"exercise:2:1:prompt"),
156                                    )],
157                                    json!([
158                                        {
159                                            "name": "Red",
160                                            "correct": false,
161                                            "id": cx.v5(b"exercise:2:1:option:1")
162                                        },
163                                        {
164                                            "name": "Blue",
165                                            "correct": true,
166                                            "id": cx.v5(b"exercise:2:1:option:2")
167                                        },
168                                        {
169                                            "name": "Green",
170                                            "correct": false,
171                                            "id": cx.v5(b"exercise:2:1:option:3")
172                                        }
173                                    ]),
174                                    false,
175                                    None,
176                                    None,
177                                )),
178                        ),
179                ),
180        )
181        .module(
182            ModuleBuilder::new()
183                .order(2)
184                .name("Bonus module")
185                .automatic_completion(None, Some(1), false)
186                .chapter(
187                    ChapterBuilder::new(3, "Bonus chapter")
188                        .fixed_ids(cx.v5(b"chapter:3"), cx.v5(b"chapter:3:instance"))
189                        .page(
190                            PageBuilder::new("/chapter-3/page-1", "Bonus Page")
191                                .block(paragraph(
192                                    "This is a bonus page with simple content.",
193                                    cx.v5(b"page:3:1:block:intro"),
194                                ))
195                                .exercise(ExerciseBuilder::example_exercise(
196                                    "Bonus question",
197                                    ExerciseIds {
198                                        exercise_id: cx.v5(b"exercise:3:1:e"),
199                                        slide_id: cx.v5(b"exercise:3:1:s"),
200                                        task_id: cx.v5(b"exercise:3:1:t"),
201                                        block_id: cx.v5(b"exercise:3:1:b"),
202                                    },
203                                    vec![paragraph(
204                                        "What is the capital of France?",
205                                        cx.v5(b"exercise:3:1:assignment"),
206                                    )],
207                                    json!([
208                                        {
209                                            "name": "London",
210                                            "correct": false,
211                                            "id": cx.v5(b"exercise:3:1:option:1")
212                                        },
213                                        {
214                                            "name": "Paris",
215                                            "correct": true,
216                                            "id": cx.v5(b"exercise:3:1:option:2")
217                                        },
218                                        {
219                                            "name": "Berlin",
220                                            "correct": false,
221                                            "id": cx.v5(b"exercise:3:1:option:3")
222                                        }
223                                    ]),
224                                    false,
225                                    None,
226                                    None,
227                                )),
228                        ),
229                ),
230        );
231
232    let (course, _default_instance, _last_module) = course.seed(&mut conn, app_config, &cx).await?;
233
234    Ok(course.id)
235}