[Custom pop course export] fix code/name incorrect in exported file

This commit is contained in:
joonas-a
2026-09-30 15:41:58 +03:00
parent 89dd5f1322
commit a77c70150c
2 changed files with 16 additions and 16 deletions
@@ -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])
@@ -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<string, StudentCourse>()
const latestByGroupId = new Map<string, StudentCourse>()
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<string, { code: string; name: string }>
courseInfoByGroupId: Map<string, { code: string; name: string }>
) => {
// 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<string, { code: string; name: string; credits: number; students: Set<string> }>()
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)