-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add QtiAreaMapping and QtiMapResponsePoint classes for area-bas…
…ed response mapping
- Loading branch information
1 parent
052303f
commit 84e79ff
Showing
6 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ti-components/qti-response-processing/qti-expression/qti-area-mapping/qti-area-mapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { property } from 'lit/decorators.js'; | ||
import { LitElement } from 'lit'; | ||
|
||
export class QtiAreaMapping extends LitElement { | ||
@property({ attribute: 'default-value', type: Number }) defaultValue: number = 0; | ||
@property({ attribute: 'lower-bound', type: Number }) lowerBound: number; | ||
@property({ attribute: 'upper-bound', type: Number }) upperBound: number; | ||
|
||
public get mapEntries() { | ||
return Array.from(this.querySelectorAll('qti-area-map-entry')).map(el => { | ||
return { | ||
shape: el.getAttribute('shape'), | ||
coords: el.getAttribute('coords'), | ||
mappedValue: +el.getAttribute('mapped-value'), | ||
defaultValue: el.getAttribute('default-value') ? +el.getAttribute('default-value') : 0 | ||
} as { | ||
shape: 'default' | 'circle' | 'rect' | 'ellipse' | 'poly'; | ||
coords: string; | ||
mappedValue: number; | ||
defaultValue: number; | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
customElements.define('qti-area-mapping', QtiAreaMapping); |
71 changes: 71 additions & 0 deletions
71
...s/qti-response-processing/qti-expression/qti-map-response-point/qti-map-response-point.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { property } from 'lit/decorators.js'; | ||
|
||
import { QtiExpression } from '../../../internal/qti-expression'; | ||
import { ScoringHelper } from '../../utilities/scoring-helper'; | ||
|
||
import type { ResponseVariable } from '../../../../exports/variables'; | ||
import type { QtiAreaMapping } from '../qti-area-mapping/qti-area-mapping'; | ||
|
||
export class QtiMapResponsePoint extends QtiExpression<number> { | ||
@property({ type: String }) identifier: string; | ||
|
||
public override getResult(): number { | ||
const response: ResponseVariable = this.context.variables.find(r => r.identifier === this.identifier); | ||
if (!response) { | ||
console.warn(`Response ${this.identifier} cannot be found`); | ||
return null; | ||
} | ||
|
||
const areaMapping: QtiAreaMapping = response.areaMapping; | ||
if (!areaMapping) { | ||
console.warn(`Area mapping not found for response ${this.identifier}`); | ||
return null; | ||
} | ||
|
||
const candidateResponses = !Array.isArray(response.value) ? [response.value] : response.value; | ||
if (!candidateResponses || candidateResponses.length === 0) { | ||
console.warn(`No candidate responses for response ${this.identifier}`); | ||
return null; | ||
} | ||
|
||
let result = 0; | ||
|
||
// Keep track of areas that have already been matched | ||
const mappedAreas = new Set<string>(); | ||
|
||
for (const candidateResponse of candidateResponses) { | ||
for (const entry of areaMapping.mapEntries) { | ||
if (mappedAreas.has(entry.coords)) { | ||
continue; // Skip areas that have already been mapped | ||
} | ||
|
||
const isPointInArea = ScoringHelper.isPointInArea( | ||
candidateResponse, | ||
`${entry.shape},${entry.coords}`, | ||
response.baseType | ||
); | ||
if (isPointInArea) { | ||
result += entry.mappedValue ?? 0; | ||
mappedAreas.add(entry.coords); | ||
} | ||
} | ||
} | ||
|
||
// Add default value for unmatched candidate responses | ||
if (mappedAreas.size < candidateResponses.length) { | ||
result += areaMapping.defaultValue; | ||
} | ||
|
||
// Apply bounds if defined | ||
if (areaMapping.lowerBound != null) { | ||
result = Math.max(areaMapping.lowerBound, result); | ||
} | ||
if (areaMapping.upperBound != null) { | ||
result = Math.min(areaMapping.upperBound, result); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
customElements.define('qti-map-response-point', QtiMapResponsePoint); |
94 changes: 93 additions & 1 deletion
94
src/lib/qti-components/qti-response-processing/utilities/scoring-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters