feat(backend): Gepersonaliseerde leerpaden via API

Mogelijkheid toegevoegd om via de API optioneel een gepersonaliseerde variant van een leerpad voor een student of groep aan te vragen.
This commit is contained in:
Gerald Schmittinger 2025-03-11 12:18:07 +01:00
parent 31f994167d
commit 3f04d2fd4d
2 changed files with 77 additions and 4 deletions

View file

@ -2,7 +2,12 @@ import { Request, Response } from 'express';
import { themes } from '../data/themes.js';
import { FALLBACK_LANG } from '../config.js';
import learningPathService from '../services/learning-paths/learning-path-service';
import { NotFoundException } from '../exceptions';
import {BadRequestException, NotFoundException} from '../exceptions';
import {Language} from "../entities/content/language";
import {
PersonalizationTarget, personalizedForGroup,
personalizedForStudent
} from "../services/learning-paths/learning-path-personalization-util";
/**
* Fetch learning paths based on query parameters.
@ -13,6 +18,22 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi
const searchQuery = req.query.search as string;
const language = (req.query.language as string) || FALLBACK_LANG;
const forStudent = req.query.forStudent as string;
const forGroupNo = req.query.forGroup as string;
const assignmentNo = req.query.assignmentNo as string;
const classId = req.query.classId as string;
let personalizationTarget: PersonalizationTarget | undefined;
if (forStudent) {
personalizationTarget = await personalizedForStudent(forStudent)
} else if (forGroupNo) {
if (!assignmentNo || !classId) {
throw new BadRequestException("If forGroupNo is specified, assignmentNo and classId must also be specified.");
}
personalizationTarget = await personalizedForGroup(classId, parseInt(assignmentNo), parseInt(forGroupNo));
}
let hruidList;
if (hruids) {
@ -25,13 +46,13 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi
throw new NotFoundException(`Theme "${themeKey}" not found.`);
}
} else if (searchQuery) {
const searchResults = await learningPathService.searchLearningPaths(searchQuery, language);
const searchResults = await learningPathService.searchLearningPaths(searchQuery, language as Language, personalizationTarget);
res.json(searchResults);
return;
} else {
hruidList = themes.flatMap((theme) => theme.hruids);
}
const learningPaths = await learningPathService.fetchLearningPaths(hruidList, language, `HRUIDs: ${hruidList.join(', ')}`);
const learningPaths = await learningPathService.fetchLearningPaths(hruidList, language as Language, `HRUIDs: ${hruidList.join(', ')}`, personalizationTarget);
res.json(learningPaths.data);
}

View file

@ -2,12 +2,64 @@ import { LearningPathNode } from '../../entities/content/learning-path-node.enti
import { Student } from '../../entities/users/student.entity';
import { Group } from '../../entities/assignments/group.entity';
import { Submission } from '../../entities/assignments/submission.entity';
import { getSubmissionRepository } from '../../data/repositories';
import {
getClassRepository,
getGroupRepository,
getStudentRepository,
getSubmissionRepository
} from '../../data/repositories';
import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier';
import { LearningPathTransition } from '../../entities/content/learning-path-transition.entity';
import { JSONPath } from 'jsonpath-plus';
export type PersonalizationTarget = { type: 'student'; student: Student } | { type: 'group'; group: Group };
/**
* Shortcut function to easily create a PersonalizationTarget object for a student by his/her username.
* @param username Username of the student we want to generate a personalized learning path for.
* If there is no student with this username, return undefined.
*/
export async function personalizedForStudent(username: string): Promise<PersonalizationTarget | undefined> {
const student = await getStudentRepository().findByUsername(username);
if (student) {
return {
type: "student",
student: student
}
} else {
return undefined;
}
}
/**
* Shortcut function to easily create a PersonalizationTarget object for a group by class name, assignment number and
* group number.
* @param classId Id of the class in which this group was created
* @param assignmentNumber Number of the assignment for which this group was created
* @param groupNumber Number of the group for which we want to personalize the learning path.
*/
export async function personalizedForGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<PersonalizationTarget | undefined> {
const clazz = await getClassRepository().findById(classId);
if (!clazz) {
return undefined;
}
const group = await getGroupRepository().findOne({
assignment: {
within: clazz,
id: assignmentNumber,
},
groupNumber: groupNumber
})
if (group) {
return {
type: "group",
group: group
}
} else {
return undefined;
}
}
/**
* Returns the last submission for the learning object associated with the given node and for the student or group
*/