From f3ba47eb30f270ec82569619282ec19e7a5505a8 Mon Sep 17 00:00:00 2001 From: Gerald Schmittinger Date: Wed, 14 May 2025 08:45:44 +0200 Subject: [PATCH] fix(backend): Vooruitgang wordt nu enkel op basis van effectief bereikbare leerobjecten berekend. --- .../database-learning-path-provider.ts | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/backend/src/services/learning-paths/database-learning-path-provider.ts b/backend/src/services/learning-paths/database-learning-path-provider.ts index fe05dda1..7dbc3168 100644 --- a/backend/src/services/learning-paths/database-learning-path-provider.ts +++ b/backend/src/services/learning-paths/database-learning-path-provider.ts @@ -62,6 +62,8 @@ async function convertLearningPath(learningPath: LearningPathEntity, order: numb // Convert the learning object notes as retrieved from the database into the expected response format- const convertedNodes = await convertNodes(nodesToLearningObjects, personalizedFor); + const nodesActuallyOnPath = traverseLearningPath(convertedNodes); + return { _id: `${learningPath.hruid}/${learningPath.language}`, // For backwards compatibility with the original Dwengo API. __order: order, @@ -71,8 +73,8 @@ async function convertLearningPath(learningPath: LearningPathEntity, order: numb image: image, title: learningPath.title, nodes: convertedNodes, - num_nodes: learningPath.nodes.length, - num_nodes_left: convertedNodes.filter((it) => !it.done).length, + num_nodes: nodesActuallyOnPath.length, + num_nodes_left: nodesActuallyOnPath.filter((it) => !it.done).length, keywords: keywords.join(' '), target_ages: targetAges, max_age: Math.max(...targetAges), @@ -103,6 +105,7 @@ async function convertNode( isTransitionPossible(trans, optionalJsonStringToObject(lastSubmission?.content)) // Otherwise remove all transitions that aren't possible. ) .map((trans, i) => convertTransition(trans, i, nodesToLearningObjects)); + return { _id: learningObject.uuid, language: learningObject.language, @@ -174,6 +177,31 @@ function convertTransition( } } +/** + * Start from the start node and then always take the first transition until there are no transitions anymore. + * Returns the traversed nodes as an array. (This effectively filters outs nodes that cannot be reached.) + */ +function traverseLearningPath(nodes: LearningObjectNode[]): LearningObjectNode[] { + const traversedNodes: LearningObjectNode[] = []; + let currentNode = nodes.find(it => it.start_node); + + while (currentNode) { + traversedNodes.push(currentNode); + + const next = currentNode.transitions[0]?.next; + + if (next) { + currentNode = nodes.find(it => + it.learningobject_hruid === next.hruid && it.language === next.language && it.version === next.version + ); + } else { + currentNode = undefined; + } + } + + return traversedNodes; +} + /** * Service providing access to data about learning paths from the database. */