diff --git a/services/frontend/src/components/CustomPopulation/CourseMatrixExport.tsx b/services/frontend/src/components/CustomPopulation/CourseMatrixExport.tsx index 7bb6e8750..99ee91d0c 100644 --- a/services/frontend/src/components/CustomPopulation/CourseMatrixExport.tsx +++ b/services/frontend/src/components/CustomPopulation/CourseMatrixExport.tsx @@ -20,10 +20,10 @@ export const CourseMatrixExport = ({ const { getTextIn } = useLanguage() const getXlsx = () => { - const courseInfoById = new Map( - courses.map(({ course }) => [course.id, { code: course.code, name: getTextIn(course.name) ?? '' }]) + const courseInfoByGroupId = new Map( + courses.map(({ course }) => [course.groupId, { code: course.code, name: getTextIn(course.name) ?? '' }]) ) - const { completedCoursesRows, courseCounterRows } = calculateExcelData(students, courseInfoById) + const { completedCoursesRows, courseCounterRows } = calculateExcelData(students, courseInfoByGroupId) // Columns: Student number, Name, student's attainments in the format: "course name (course code)" const completedCoursesSheet = utils.aoa_to_sheet([['Student number', 'Name'], ...completedCoursesRows]) diff --git a/services/frontend/src/components/CustomPopulation/courseMatrix.ts b/services/frontend/src/components/CustomPopulation/courseMatrix.ts index 4f324e54a..cf09e243b 100644 --- a/services/frontend/src/components/CustomPopulation/courseMatrix.ts +++ b/services/frontend/src/components/CustomPopulation/courseMatrix.ts @@ -21,39 +21,39 @@ export type CourseMatrixAggregate = { /** * Passed completions of the student that are included in one of the student's study plans (HOPS). - * Returns the latest completion per course id, sorted by course id. + * Returns the latest completion per course group, sorted by course group id. */ export const getHopsCourses = (student: FormattedStudent): StudentCourse[] => { const hopsCourseIds = new Set(student.studyplans?.flatMap(plan => plan.included_courses ?? []) ?? []) const passedCoursesInHops = student.courses.filter(course => course.passed && hopsCourseIds.has(course.course_id)) - const latestById = new Map() + const latestByGroupId = new Map() for (const course of passedCoursesInHops) { - if (!course.course_id) continue + if (!course.courseGroupId) continue - const existing = latestById.get(course.course_id) + const existing = latestByGroupId.get(course.courseGroupId) if (!existing || new Date(course.date).getTime() > new Date(existing.date).getTime()) { - latestById.set(course.course_id, course) + latestByGroupId.set(course.courseGroupId, course) } } - return [...latestById.values()].sort((a, b) => a.course_id.localeCompare(b.course_id)) + return [...latestByGroupId.values()].sort((a, b) => a.courseGroupId.localeCompare(b.courseGroupId)) } export const calculateExcelData = ( students: FormattedStudent[], - courseInfoById: Map + courseInfoByGroupId: Map ) => { - // Keyed by course_id internally so different courses are never accidentally merged; - // code/name are only for display. + // Keyed by course groupId internally so different courses are never accidentally merged, + // while different versions of the same course are; code/name are only for display. const counters = new Map }>() const rows = students.map(student => { const courses = getHopsCourses(student).map(course => { - const id = course.course_id - const { code, name } = courseInfoById.get(id) ?? { code: id, name: '' } - const counter = counters.get(id) ?? { code, name, credits: 0, students: new Set() } - counters.set(id, counter) + const groupId = course.courseGroupId + const { code, name } = courseInfoByGroupId.get(groupId) ?? { code: course.course_code, name: '' } + const counter = counters.get(groupId) ?? { code, name, credits: 0, students: new Set() } + counters.set(groupId, counter) counter.credits += course.credits ?? 0 counter.students.add(student.studentNumber)