-
-
Notifications
You must be signed in to change notification settings - Fork 304
feat: add Ordering Interaction Editor #6089
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
Abhishek-Punhani
wants to merge
1
commit into
learningequality:unstable
Choose a base branch
from
Abhishek-Punhani:ordering-interaction
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
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
124 changes: 124 additions & 0 deletions
124
...tion/frontend/shared/views/QTIEditor/composables/__tests__/useOrderingInteraction.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,124 @@ | ||
| import { ref } from 'vue'; | ||
| import { useOrderingInteraction } from '../useOrderingInteraction'; | ||
| import { ORDERING_XML, ORDERING_DECL_XML } from '../../utils/testingFixtures'; | ||
| import { QuestionType, ValidationError, Orientation } from '../../constants'; | ||
|
|
||
| function makeInteractionBlock(bodyXml = ORDERING_XML, declarationXml = ORDERING_DECL_XML) { | ||
| return { bodyXml, responseDeclarations: [declarationXml] }; | ||
| } | ||
|
|
||
| describe('useOrderingInteraction', () => { | ||
| function setup(bodyXml, declarationXml) { | ||
| const questionType = ref(QuestionType.ORDERING); | ||
| return useOrderingInteraction(makeInteractionBlock(bodyXml, declarationXml), questionType); | ||
| } | ||
|
|
||
| describe('initial state', () => { | ||
| it('parses items correctly from the fixture XML', () => { | ||
| const { state } = setup(); | ||
| expect(state.value.items).toHaveLength(3); | ||
| expect(state.value.items[0].id).toBe('order_aaa11111'); | ||
| }); | ||
|
|
||
| it('starts with an empty errors array', () => { | ||
| const { errors } = setup(); | ||
| expect(errors.value).toEqual([]); | ||
| }); | ||
|
|
||
| it('orientation defaults to vertical', () => { | ||
| const { state } = setup(); | ||
| expect(state.value.orientation).toBe(Orientation.VERTICAL); | ||
| }); | ||
| }); | ||
|
|
||
| describe('addItem()', () => { | ||
| it('appends a new item with a generated order_ identifier', () => { | ||
| const { state, addItem } = setup(); | ||
| const before = state.value.items.length; | ||
| addItem(); | ||
| expect(state.value.items).toHaveLength(before + 1); | ||
| expect(state.value.items[before].id).toMatch(/^order_/); | ||
| }); | ||
|
|
||
| it('new item starts with empty content', () => { | ||
| const { state, addItem } = setup(); | ||
| addItem(); | ||
| const last = state.value.items[state.value.items.length - 1]; | ||
| expect(last.content).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removeItem()', () => { | ||
| it('removes the item with the given id', () => { | ||
| const { state, removeItem } = setup(); | ||
| const idToRemove = state.value.items[0].id; | ||
| removeItem(idToRemove); | ||
| expect(state.value.items.find(i => i.id === idToRemove)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('is a no-op when only one item remains', () => { | ||
| const { state, removeItem } = setup(); | ||
| // Remove until one left | ||
| while (state.value.items.length > 1) { | ||
| removeItem(state.value.items[0].id); | ||
| } | ||
| const lastId = state.value.items[0].id; | ||
| removeItem(lastId); | ||
| expect(state.value.items).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('moveItemUp()', () => { | ||
| it('swaps the item at index N with the one at index N-1', () => { | ||
| const { state, moveItemUp } = setup(); | ||
| const [firstId, secondId] = state.value.items.map(i => i.id); | ||
| moveItemUp(secondId); | ||
| expect(state.value.items[0].id).toBe(secondId); | ||
| expect(state.value.items[1].id).toBe(firstId); | ||
| }); | ||
|
|
||
| it('is a no-op when the item is already at the top', () => { | ||
| const { state, moveItemUp } = setup(); | ||
| const firstId = state.value.items[0].id; | ||
| moveItemUp(firstId); | ||
| expect(state.value.items[0].id).toBe(firstId); | ||
| }); | ||
| }); | ||
|
|
||
| describe('moveItemDown()', () => { | ||
| it('swaps the item at index N with the one at index N+1', () => { | ||
| const { state, moveItemDown } = setup(); | ||
| const [firstId, secondId] = state.value.items.map(i => i.id); | ||
| moveItemDown(firstId); | ||
| expect(state.value.items[0].id).toBe(secondId); | ||
| expect(state.value.items[1].id).toBe(firstId); | ||
| }); | ||
|
|
||
| it('is a no-op when the item is already at the bottom', () => { | ||
| const { state, moveItemDown } = setup(); | ||
| const lastId = state.value.items[state.value.items.length - 1].id; | ||
| moveItemDown(lastId); | ||
| expect(state.value.items[state.value.items.length - 1].id).toBe(lastId); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setItemContent()', () => { | ||
| it('updates only the targeted item content', () => { | ||
| const { state, setItemContent } = setup(); | ||
| const targetId = state.value.items[1].id; | ||
| setItemContent(targetId, '<p>Updated</p>'); | ||
| expect(state.value.items[1].content).toBe('<p>Updated</p>'); | ||
| // Other items untouched | ||
| expect(state.value.items[0].content).toBe(state.value.items[0].content); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: compares a value to itself, so it can never fail — including if |
||
| }); | ||
| }); | ||
|
|
||
| describe('runValidation()', () => { | ||
| it('populates errors for an invalid state', () => { | ||
| const { runValidation, errors, setPrompt } = setup(); | ||
| setPrompt(''); | ||
| runValidation(); | ||
| expect(errors.value.some(e => e.code === ValidationError.PROMPT_REQUIRED)).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
68 changes: 68 additions & 0 deletions
68
...ion/contentcuration/frontend/shared/views/QTIEditor/composables/useOrderingInteraction.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,68 @@ | ||
| import { readonly } from 'vue'; | ||
| import { generateRandomSlug } from '../utils/generateRandomSlug'; | ||
| import { orderingInteractionDescriptor } from '../interactions/ordering/OrderingInteractionDescriptor'; | ||
| import { useInteraction } from './useInteraction'; | ||
|
|
||
| /** | ||
| * Composable for the ordering interaction editor. | ||
| * | ||
| * @param {{ bodyXml: string, responseDeclarations: string[] }} interactionBlock | ||
| * @param {import('vue').Ref<string|null>} questionType | ||
| */ | ||
| export function useOrderingInteraction(interactionBlock, questionType) { | ||
| const base = useInteraction(orderingInteractionDescriptor, interactionBlock, questionType); | ||
| const { state } = base; | ||
|
|
||
| function addItem() { | ||
| state.value = { | ||
| ...state.value, | ||
| items: [...state.value.items, { id: generateRandomSlug('order'), content: '', fixed: false }], | ||
| }; | ||
| } | ||
|
|
||
| function removeItem(id) { | ||
| if (state.value.items.length <= 1) return; | ||
| state.value = { | ||
| ...state.value, | ||
| items: state.value.items.filter(item => item.id !== id), | ||
| }; | ||
| } | ||
|
|
||
| function moveItemUp(id) { | ||
| const items = [...state.value.items]; | ||
| const idx = items.findIndex(item => item.id === id); | ||
| if (idx <= 0) return; | ||
| [items[idx - 1], items[idx]] = [items[idx], items[idx - 1]]; | ||
| state.value = { ...state.value, items }; | ||
| } | ||
|
|
||
| function moveItemDown(id) { | ||
| const items = [...state.value.items]; | ||
| const idx = items.findIndex(item => item.id === id); | ||
| if (idx === -1 || idx >= items.length - 1) return; | ||
| [items[idx], items[idx + 1]] = [items[idx + 1], items[idx]]; | ||
| state.value = { ...state.value, items }; | ||
| } | ||
|
|
||
| function setItemContent(id, html) { | ||
| state.value = { | ||
| ...state.value, | ||
| items: state.value.items.map(item => (item.id === id ? { ...item, content: html } : item)), | ||
| }; | ||
| } | ||
|
|
||
| function setPrompt(html) { | ||
| state.value = { ...state.value, prompt: html }; | ||
| } | ||
|
|
||
| return { | ||
| ...base, | ||
| state: readonly(state), | ||
| addItem, | ||
| removeItem, | ||
| moveItemUp, | ||
| moveItemDown, | ||
| setItemContent, | ||
| 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
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
85 changes: 85 additions & 0 deletions
85
...on/frontend/shared/views/QTIEditor/interactions/ordering/OrderingInteractionDescriptor.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,85 @@ | ||
| import { QtiInteraction, QuestionType, BaseType, Cardinality } from '../../constants'; | ||
| import { parseOrderingInteraction, buildOrderingInteractionXML } from './parse'; | ||
| import { validateOrderingInteraction } from './validate'; | ||
|
|
||
| /** | ||
| * Owns all ordering-specific interaction logic: schema, parse, buildXML, and validate. | ||
| */ | ||
| export class OrderingInteractionDescriptor { | ||
| constructor({ editorComponent = null } = {}) { | ||
| this.type = QtiInteraction.ORDER; | ||
| this.placement = 'block'; | ||
| this.questionTypes = [QuestionType.ORDERING]; | ||
| this.editorComponent = editorComponent; | ||
| this.convertsFrom = []; | ||
| } | ||
|
|
||
| getTypeOptions(tr) { | ||
| return [ | ||
| { | ||
| value: QuestionType.ORDERING, | ||
| label: tr.orderingLabel$(), | ||
| description: tr.orderingDescription$(), | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| /** @param {Element} el */ | ||
| matches(el) { | ||
| return el.tagName.toLowerCase() === QtiInteraction.ORDER; | ||
| } | ||
|
|
||
| /** | ||
| * Ordering always has exactly one question type. | ||
| * | ||
| * @returns {string} | ||
| */ | ||
| getQuestionType() { | ||
| return QuestionType.ORDERING; | ||
| } | ||
|
|
||
| /** | ||
| * @returns {{ baseType: string, cardinality: string }} | ||
| */ | ||
| getResponseDeclarationSchema() { | ||
| return { | ||
| baseType: BaseType.IDENTIFIER, | ||
| cardinality: Cardinality.ORDERED, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Parse <qti-order-interaction> body XML + response declarations → OrderingState. | ||
| * | ||
| * @param {string} bodyXml | ||
| * @param {string[]} responseDeclarations | ||
| * @returns {object} OrderingState | ||
| */ | ||
| parse(bodyXml, responseDeclarations) { | ||
| return parseOrderingInteraction(bodyXml, responseDeclarations); | ||
| } | ||
|
|
||
| /** | ||
| * Serialize OrderingState → { bodyXml, responseDeclarations }. | ||
| * | ||
| * @param {object} state - OrderingState | ||
| * @param {string} questionType | ||
| * @returns {{ bodyXml: string, responseDeclarations: string[] }} | ||
| */ | ||
| buildXML(state, questionType) { | ||
| return buildOrderingInteractionXML(state, questionType, this.getResponseDeclarationSchema()); | ||
| } | ||
|
|
||
| /** | ||
| * Validate OrderingState → ValidationError[]. | ||
| * | ||
| * @param {object} state - OrderingState | ||
| * @returns {Array<{ code: string, id?: string }>} | ||
| */ | ||
| validate(state) { | ||
| return validateOrderingInteraction(state); | ||
| } | ||
| } | ||
|
|
||
| /** Singleton — safe to import from any file in the ordering module tree. */ | ||
| export const orderingInteractionDescriptor = new OrderingInteractionDescriptor(); |
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.
Uh oh!
There was an error while loading. Please reload this page.