|
1 | 1 | import { Err, Result } from "ts-results"; |
2 | 2 |
|
3 | 3 | import { Logger } from "../utilities"; |
4 | | -import { combineApiExerciseData } from "../utilities/apiData"; |
| 4 | +import { combineTmcApiExerciseData } from "../utilities/apiData"; |
5 | 5 |
|
6 | 6 | import { refreshLocalExercises } from "./refreshLocalExercises"; |
7 | 7 | import { ActionContext } from "./types"; |
8 | | -import { |
9 | | - CourseIdentifier, |
10 | | - LocalCourseData, |
11 | | - LocalTmcCourseData, |
12 | | - makeMoocKind, |
13 | | - makeTmcKind, |
14 | | - match, |
15 | | -} from "../shared/shared"; |
| 8 | +import { CourseIdentifier, LocalMoocCourseData, LocalTmcCourseData, match } from "../shared/shared"; |
16 | 9 |
|
17 | 10 | /** |
18 | | - * Adds a new TMC course to user's courses. |
| 11 | + * Adds a new course to user's courses. |
19 | 12 | */ |
20 | 13 | export async function addNewCourse( |
21 | 14 | actionContext: ActionContext, |
22 | | - organization: string, |
| 15 | + organizationSlug: string, |
23 | 16 | course: CourseIdentifier, |
24 | 17 | ): Promise<Result<void, Error>> { |
25 | | - const { tmc, ui, userData, workspaceManager } = actionContext; |
26 | | - if (!(tmc.ok && userData.ok && workspaceManager.ok)) { |
| 18 | + const { langs, ui, userData, workspaceManager } = actionContext; |
| 19 | + if (!(langs.ok && userData.ok && workspaceManager.ok)) { |
27 | 20 | return new Err(new Error("Extension was not initialized properly")); |
28 | 21 | } |
29 | 22 | Logger.info("Adding new course"); |
30 | 23 |
|
31 | | - const courseDataResult = await tmc.val.getCourseData(course); |
32 | | - if (courseDataResult.err) { |
33 | | - return courseDataResult; |
34 | | - } |
35 | | - const courseData = courseDataResult.val; |
| 24 | + return match( |
| 25 | + course, |
| 26 | + async (tmcCourse) => { |
| 27 | + const courseDataResult = await langs.val.getTmcCourseData(tmcCourse.courseId); |
| 28 | + if (courseDataResult.err) { |
| 29 | + return courseDataResult; |
| 30 | + } |
| 31 | + const courseData = courseDataResult.val; |
| 32 | + |
| 33 | + let availablePoints = 0; |
| 34 | + let awardedPoints = 0; |
| 35 | + courseData.exercises.forEach((x) => { |
| 36 | + availablePoints += x.available_points.length; |
| 37 | + awardedPoints += x.awarded_points.length; |
| 38 | + }); |
36 | 39 |
|
37 | | - let availablePoints = 0; |
38 | | - let awardedPoints = 0; |
39 | | - courseData.exercises.forEach((x) => { |
40 | | - availablePoints += x.available_points.length; |
41 | | - awardedPoints += x.awarded_points.length; |
42 | | - }); |
| 40 | + const localData: LocalTmcCourseData = { |
| 41 | + description: courseData.details.description || "", |
| 42 | + exercises: combineTmcApiExerciseData( |
| 43 | + courseData.details.exercises, |
| 44 | + courseData.exercises, |
| 45 | + ), |
| 46 | + id: courseData.details.id, |
| 47 | + name: courseData.details.name, |
| 48 | + title: courseData.details.title, |
| 49 | + organization: organizationSlug, |
| 50 | + availablePoints: availablePoints, |
| 51 | + awardedPoints: awardedPoints, |
| 52 | + perhapsExamMode: courseData.settings.hide_submission_results, |
| 53 | + newExercises: [], |
| 54 | + notifyAfter: 0, |
| 55 | + disabled: courseData.settings.disabled_status === "enabled" ? false : true, |
| 56 | + materialUrl: courseData.settings.material_url, |
| 57 | + }; |
| 58 | + userData.val.addCourse({ kind: "tmc", data: localData }); |
| 59 | + ui.treeDP.addChildWithId("myCourses", localData.id, localData.title, { |
| 60 | + command: "tmc.courseDetails", |
| 61 | + title: "Go To Course Details", |
| 62 | + arguments: [CourseIdentifier.from(localData.id)], |
| 63 | + }); |
| 64 | + workspaceManager.val.createWorkspaceFile(courseData.details.name); |
| 65 | + //await displayUserCourses(actionContext); |
| 66 | + return refreshLocalExercises(actionContext); |
| 67 | + }, |
| 68 | + async (mooc) => { |
| 69 | + const courseInstanceRes = await langs.val.getMoocCourseInstanceData(mooc.instanceId); |
| 70 | + if (courseInstanceRes.err) { |
| 71 | + return courseInstanceRes; |
| 72 | + } |
| 73 | + const [courseInstance, _] = courseInstanceRes.val; |
43 | 74 |
|
44 | | - const localData: LocalTmcCourseData = { |
45 | | - description: courseData.details.description || "", |
46 | | - exercises: combineApiExerciseData(courseData.details.exercises, courseData.exercises) |
47 | | - .filter((ex) => ex.kind === "tmc") |
48 | | - .map((ex) => ex.data), |
49 | | - id: courseData.details.id, |
50 | | - name: courseData.details.name, |
51 | | - title: courseData.details.title, |
52 | | - organization: organization, |
53 | | - availablePoints: availablePoints, |
54 | | - awardedPoints: awardedPoints, |
55 | | - perhapsExamMode: courseData.settings.hide_submission_results, |
56 | | - newExercises: [], |
57 | | - notifyAfter: 0, |
58 | | - disabled: courseData.settings.disabled_status === "enabled" ? false : true, |
59 | | - materialUrl: courseData.settings.material_url, |
60 | | - }; |
61 | | - userData.val.addCourse({ kind: "tmc", data: localData }); |
62 | | - ui.treeDP.addChildWithId("myCourses", localData.id, localData.title, { |
63 | | - command: "tmc.courseDetails", |
64 | | - title: "Go To Course Details", |
65 | | - arguments: [localData.id], |
66 | | - }); |
67 | | - workspaceManager.val.createWorkspaceFile(courseData.details.name); |
68 | | - //await displayUserCourses(actionContext); |
69 | | - return refreshLocalExercises(actionContext); |
| 75 | + const localData: LocalMoocCourseData = { |
| 76 | + courseId: courseInstance.course_id, |
| 77 | + instanceId: courseInstance.id, |
| 78 | + courseName: courseInstance.course_name, |
| 79 | + instanceName: courseInstance.instance_name, |
| 80 | + courseDescription: courseInstance.course_description, |
| 81 | + instanceDescription: courseInstance.instance_description, |
| 82 | + awardedPoints: 0, |
| 83 | + availablePoints: 0, |
| 84 | + disabled: false, |
| 85 | + materialUrl: null, |
| 86 | + exercises: [], |
| 87 | + newExercises: [], |
| 88 | + notifyAfter: 0, |
| 89 | + perhapsExamMode: false, |
| 90 | + }; |
| 91 | + userData.val.addCourse({ kind: "mooc", data: localData }); |
| 92 | + ui.treeDP.addChildWithId("myCourses", localData.instanceId, localData.courseName, { |
| 93 | + command: "tmc.courseDetails", |
| 94 | + title: "Go To Course Details", |
| 95 | + arguments: [CourseIdentifier.from(localData.instanceId)], |
| 96 | + }); |
| 97 | + workspaceManager.val.createWorkspaceFile(courseInstance.course_slug); |
| 98 | + return refreshLocalExercises(actionContext); |
| 99 | + }, |
| 100 | + ); |
70 | 101 | } |
0 commit comments