-
-
Notifications
You must be signed in to change notification settings - Fork 307
Add the QTI associate interaction plugin #6113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexVelezLl
merged 7 commits into
learningequality:unstable
from
rtibblesbot:issue-6101-e6feda
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f6468e
Add associate interaction parsing, serialization, and validation
rtibblesbot de9d9f7
Add associate interaction descriptor and composable
rtibblesbot 2c9b477
Flush editor content up before emitting minimize
rtibblesbot bdc2f62
Add associate interaction editor and register the plugin
rtibblesbot 179cc2a
Add an associate item to the QTI demo page
rtibblesbot f531292
Clip overflowing editor content inside TipTapEditor itself
rtibblesbot c741964
Fix chip min width and pair border colors
AlexVelezLl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
122 changes: 122 additions & 0 deletions
122
...ion/frontend/shared/views/QTIEditor/composables/__tests__/useAssociateInteraction.spec.js
This file contains hidden or 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,122 @@ | ||
| import { ref } from 'vue'; | ||
| import { useAssociateInteraction } from '../useAssociateInteraction'; | ||
| import { ASSOCIATE_XML, ASSOCIATE_DECL_XML } from '../../utils/testingFixtures'; | ||
| import { parseXML } from '../../serialization/xml'; | ||
| import { QuestionType } from '../../constants'; | ||
|
|
||
| const GENERATED_ID = /^choice_[a-z0-9]{8}$/; | ||
|
|
||
| const contentsOf = pairs => pairs.map(pair => pair.map(choice => choice.content)); | ||
|
|
||
| const maxAssociations = bodyXml => | ||
| parseXML(bodyXml).documentElement.getAttribute('max-associations'); | ||
|
|
||
| describe('useAssociateInteraction', () => { | ||
| function setup(bodyXml = ASSOCIATE_XML, declarationXml = ASSOCIATE_DECL_XML) { | ||
| const questionType = ref(QuestionType.ASSOCIATE); | ||
| return useAssociateInteraction( | ||
| { bodyXml, responseDeclarations: [declarationXml] }, | ||
| questionType, | ||
| ); | ||
| } | ||
|
|
||
| describe('initial state', () => { | ||
| it('parses pairs and distractors from the fixture XML', () => { | ||
| const { state } = setup(); | ||
| expect(contentsOf(state.value.pairs)).toEqual([ | ||
| ['Antonio', 'Prospero'], | ||
| ['Capulet', 'Montague'], | ||
| ]); | ||
| expect(state.value.distractors.map(d => d.content)).toEqual(['Lysander']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('addPair()', () => { | ||
| it('appends a pair of two blank choices with distinct generated ids', () => { | ||
| const { state, addPair } = setup(); | ||
| addPair(); | ||
| expect(contentsOf(state.value.pairs)).toEqual([ | ||
| ['Antonio', 'Prospero'], | ||
| ['Capulet', 'Montague'], | ||
| ['', ''], | ||
| ]); | ||
| const [first, second] = state.value.pairs[2]; | ||
| expect(first.id).toMatch(GENERATED_ID); | ||
| expect(second.id).toMatch(GENERATED_ID); | ||
| expect(first.id).not.toBe(second.id); | ||
| }); | ||
|
|
||
| it('rebuilds bodyXml with the new max-associations', () => { | ||
| const { bodyXml, addPair } = setup(); | ||
| addPair(); | ||
| expect(maxAssociations(bodyXml.value)).toBe('3'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removePair()', () => { | ||
| it('drops the pair at the given index and keeps the rest in order', () => { | ||
| const { state, removePair } = setup(); | ||
| removePair(0); | ||
| expect(contentsOf(state.value.pairs)).toEqual([['Capulet', 'Montague']]); | ||
| }); | ||
|
|
||
| it('is a no-op when only one pair remains', () => { | ||
| const { state, removePair } = setup(); | ||
| removePair(0); | ||
| removePair(0); | ||
| expect(contentsOf(state.value.pairs)).toEqual([['Capulet', 'Montague']]); | ||
| }); | ||
|
|
||
| it('drops the pair from the emitted correct response', () => { | ||
| const { responseDeclarations, removePair } = setup(); | ||
| removePair(0); | ||
| expect(responseDeclarations.value[0]).not.toContain('choice_aaa11111'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setPair()', () => { | ||
| it('replaces only the pair at the given index', () => { | ||
| const { state, setPair } = setup(); | ||
| const [first, second] = state.value.pairs[0]; | ||
| setPair(0, [{ ...first, content: '<p>Updated</p>' }, second]); | ||
| expect(contentsOf(state.value.pairs)).toEqual([ | ||
| ['<p>Updated</p>', 'Prospero'], | ||
| ['Capulet', 'Montague'], | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('addDistractor()', () => { | ||
| it('appends one blank choice with a generated id', () => { | ||
| const { state, addDistractor } = setup(); | ||
| addDistractor(); | ||
| expect(state.value.distractors).toHaveLength(2); | ||
| expect(state.value.distractors[1].content).toBe(''); | ||
| expect(state.value.distractors[1].id).toMatch(GENERATED_ID); | ||
| }); | ||
|
|
||
| it('appends the given content when the distractor is written before it is added', () => { | ||
| const { state, addDistractor } = setup(); | ||
| addDistractor('<p>Demetrius</p>'); | ||
| expect(state.value.distractors[1].content).toBe('<p>Demetrius</p>'); | ||
| expect(state.value.distractors[1].id).toMatch(GENERATED_ID); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removeDistractor()', () => { | ||
| it('drops the distractor at the given index', () => { | ||
| const { state, removeDistractor } = setup(); | ||
| removeDistractor(0); | ||
| expect(state.value.distractors).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setDistractorContent()', () => { | ||
| it('updates only the targeted distractor', () => { | ||
| const { state, addDistractor, setDistractorContent } = setup(); | ||
| addDistractor(); | ||
| setDistractorContent(1, '<p>Updated</p>'); | ||
| expect(state.value.distractors.map(d => d.content)).toEqual(['Lysander', '<p>Updated</p>']); | ||
| }); | ||
| }); | ||
| }); |
74 changes: 74 additions & 0 deletions
74
...on/contentcuration/frontend/shared/views/QTIEditor/composables/useAssociateInteraction.js
This file contains hidden or 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,74 @@ | ||
| import { readonly } from 'vue'; | ||
| import { associateInteractionDescriptor } from '../interactions/associate/Descriptor'; | ||
| import { newChoice } from '../interactions/associate/parse'; | ||
| import { useInteraction } from './useInteraction'; | ||
|
|
||
| /** | ||
| * Composable for the associate interaction editor. | ||
| * | ||
| * @param {{ bodyXml: string, responseDeclarations: string[] }} interactionBlock | ||
| * @param {import('vue').Ref<string|null>} questionType | ||
| */ | ||
| export function useAssociateInteraction(interactionBlock, questionType) { | ||
| const base = useInteraction(associateInteractionDescriptor, interactionBlock, questionType); | ||
| const { state } = base; | ||
|
|
||
| function addPair() { | ||
| state.value = { ...state.value, pairs: [...state.value.pairs, [newChoice(), newChoice()]] }; | ||
| } | ||
|
|
||
| function removePair(index) { | ||
| // An associate question is meaningless without a pair to associate. | ||
| if (state.value.pairs.length <= 1) return; | ||
| state.value = { | ||
| ...state.value, | ||
| pairs: state.value.pairs.filter((_, i) => i !== index), | ||
| }; | ||
| } | ||
|
|
||
| function setPair(index, newPair) { | ||
| state.value = { | ||
| ...state.value, | ||
| pairs: state.value.pairs.map((pair, i) => (i === index ? newPair : pair)), | ||
| }; | ||
| } | ||
|
|
||
| function addDistractor(content = '') { | ||
| state.value = { | ||
| ...state.value, | ||
| distractors: [...state.value.distractors, newChoice(content)], | ||
| }; | ||
| } | ||
|
|
||
| function removeDistractor(index) { | ||
| state.value = { | ||
| ...state.value, | ||
| distractors: state.value.distractors.filter((_, i) => i !== index), | ||
| }; | ||
| } | ||
|
|
||
| function setDistractorContent(index, html) { | ||
| state.value = { | ||
| ...state.value, | ||
| distractors: state.value.distractors.map((choice, i) => | ||
| i === index ? { ...choice, content: html } : choice, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| function setPrompt(html) { | ||
| state.value = { ...state.value, prompt: html }; | ||
| } | ||
|
|
||
| return { | ||
| ...base, | ||
| state: readonly(state), | ||
| addPair, | ||
| removePair, | ||
| setPair, | ||
| addDistractor, | ||
| removeDistractor, | ||
| setDistractorContent, | ||
| setPrompt, | ||
| }; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add what we added for choice interaction, and let's prevent the removal of the last pair, also let's disable the remove button if its the last pair.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removePairis a no-op at one pair, matchingremoveChoice, and the delete button is disabled there. Spec gained that case plus amax-associationsblock mirroring choice'smax-choicesone. Checked the branch's other list mutators:removeDistractoris the only sibling, and distractors are optional, so it keeps no floor.