-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
Open
rtibblesbot
wants to merge
4
commits into
learningequality:unstable
Choose a base branch
from
rtibblesbot:issue-6101-e6feda
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7ffee14
Add associate interaction parsing, serialization, and validation
rtibblesbot e5c745f
Add associate interaction descriptor and composable
rtibblesbot 75c0209
Add associate interaction editor and register the plugin
rtibblesbot 366bf77
Add an associate item to the QTI demo page
rtibblesbot 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
135 changes: 135 additions & 0 deletions
135
...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,135 @@ | ||
| import { ref } from 'vue'; | ||
| import { useAssociateInteraction } from '../useAssociateInteraction'; | ||
| import { ASSOCIATE_XML, ASSOCIATE_DECL_XML } from '../../utils/testingFixtures'; | ||
| import { parseXML } from '../../serialization/parseItem'; | ||
| import { QuestionType } from '../../constants'; | ||
|
|
||
| const GENERATED_ID = /^choice_[a-zA-Z0-9]{8}$/; | ||
|
|
||
| const contentsOf = pairs => pairs.map(pair => pair.map(choice => choice.content)); | ||
|
|
||
| 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); | ||
| }); | ||
| }); | ||
|
|
||
| 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>']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('max-associations XML output', () => { | ||
| const maxAssociations = bodyXml => | ||
| parseXML(bodyXml).documentElement.getAttribute('max-associations'); | ||
|
|
||
| it('matches the number of pairs in state', () => { | ||
| const { bodyXml } = setup(); | ||
| expect(maxAssociations(bodyXml.value)).toBe('2'); | ||
| }); | ||
|
|
||
| it('follows a pair being added', () => { | ||
| const { bodyXml, addPair } = setup(); | ||
| addPair(); | ||
| expect(maxAssociations(bodyXml.value)).toBe('3'); | ||
| }); | ||
|
|
||
| it('follows a pair being removed', () => { | ||
| const { bodyXml, removePair } = setup(); | ||
| removePair(0); | ||
| expect(maxAssociations(bodyXml.value)).toBe('1'); | ||
| }); | ||
| }); | ||
| }); |
76 changes: 76 additions & 0 deletions
76
...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,76 @@ | ||
| import { readonly } from 'vue'; | ||
| import { generateRandomSlug } from '../utils/generateRandomSlug'; | ||
| import { associateInteractionDescriptor } from '../interactions/associate/AssociateInteractionDescriptor'; | ||
| import { useInteraction } from './useInteraction'; | ||
|
|
||
| const blankChoice = () => ({ id: generateRandomSlug('choice'), content: '' }); | ||
|
|
||
| /** | ||
| * 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, [blankChoice(), blankChoice()]] }; | ||
| } | ||
|
|
||
| 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, { ...blankChoice(), 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.