Skip to content

Commit 59d2da4

Browse files
1 parent cb505cd commit 59d2da4

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { get, has } from 'lodash';
2+
3+
import ActivityInteractionType from '../../../../models/ActivityInteractionType';
4+
5+
import Statement from '../../../../models/Statement';
6+
7+
export const getChoiceQuestionMetadata = (statement: Statement)
8+
: {readonly [key: string]: any} => {
9+
if (
10+
!(
11+
get(statement.object, ['definition', 'interactionType']) === ActivityInteractionType.CHOICE
12+
&& has(statement, ['result', 'response'])
13+
)
14+
) {
15+
return {};
16+
}
17+
18+
const choicesString = get(statement, ['result', 'response']);
19+
const choices = choicesString.split('[,]');
20+
21+
return { 'https://learninglocker&46;net/choice-response': choices };
22+
};

src/apps/statements/service/storeStatements/queriables/getMetadataFromStatement/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import Statement from '../../../../models/Statement';
22
import { getBooleanMetadata } from './getBooleanMetadata';
3+
import { getChoiceQuestionMetadata } from './getChoiceQuestionMetadata';
34
import { getDurationMetadata } from './getDurationMetadata';
45
import { getMatchingQuestionsMetadata } from './getMatchingQuestionsMetadata';
56
import { getSequencingMetadata } from './getSequencingMetadata';
67

78
export default (statement: Statement): { readonly [key: string]: any } => {
89
const durationMetadata = getDurationMetadata(statement);
910
const sequencingMetadata = getSequencingMetadata(statement);
11+
const choicesMetadata = getChoiceQuestionMetadata(statement);
1012
const matchingMetadata = getMatchingQuestionsMetadata(statement);
1113
const booleanMetadata = getBooleanMetadata(statement);
1214

1315
return {
1416
...durationMetadata,
1517
...sequencingMetadata,
18+
...choicesMetadata,
1619
...matchingMetadata,
1720
...booleanMetadata,
1821
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { merge } from 'lodash';
2+
import ActivityInteractionType from '../../../../../models/ActivityInteractionType';
3+
4+
import InteractionActivityDefinition from '../../../../../models/InteractionActivityDefinition';
5+
import Statement from '../../../../../models/Statement';
6+
import SubStatementObject from '../../../../../models/SubStatementObject';
7+
import { statementDefaults } from './statements.fixture';
8+
9+
const singleChoice: Statement = {
10+
...statementDefaults,
11+
...{
12+
result: {
13+
success: true,
14+
duration: 'PT0H0M3S',
15+
response: 'golf',
16+
},
17+
object: {
18+
id: 'http://www.example.com/tincan/activities/uyheHUJd76s/question2',
19+
objectType: 'Activity',
20+
definition: {
21+
name: { 'en-US': 'Question 2' },
22+
description: { 'en-US': 'Which of these prototypes are available at the beta site?' },
23+
type: 'http://adlnet.gov/expapi/activities/cmi.interaction',
24+
interactionType: ActivityInteractionType.CHOICE,
25+
correctResponsesPattern: ['golf[,]tetris'],
26+
choices: [
27+
{ id: 'golf', description: { 'en-US': 'Golf Example' } },
28+
{ id: 'facebook', description: { 'en-US': 'Facebook App' } },
29+
{ id: 'tetris', description: { 'en-US': 'Tetris Example' } },
30+
{ id: 'scrabble', description: { 'en-US': 'Scrabble Example' } },
31+
],
32+
} as Partial<InteractionActivityDefinition>,
33+
} as SubStatementObject,
34+
} as Partial<Statement>,
35+
};
36+
37+
const multipleChoices: Statement = merge(
38+
{},
39+
singleChoice,
40+
{
41+
result: {
42+
response: 'golf[,]tetris',
43+
},
44+
} as Partial<Statement>,
45+
);
46+
47+
export {
48+
singleChoice,
49+
multipleChoices,
50+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as assert from 'assert';
2+
3+
import { getChoiceQuestionMetadata } from '../../../../service/storeStatements/queriables/getMetadataFromStatement/getChoiceQuestionMetadata';
4+
import { multipleChoices, singleChoice } from './fixtures/choice-interaction.fixture';
5+
6+
describe('Retrieve choices metadata from statement', () => {
7+
it('should return choices metadata from statement', () => {
8+
const expectedEmptyMetadata = {};
9+
10+
const actualEmptyMetadataFromEmptyResult = getChoiceQuestionMetadata(
11+
{
12+
...singleChoice,
13+
...{
14+
result: {},
15+
},
16+
},
17+
);
18+
19+
assert.deepEqual(actualEmptyMetadataFromEmptyResult, expectedEmptyMetadata);
20+
});
21+
22+
it('should retrieve metadata from statement with one choice', () => {
23+
const actualSingleChoiceMetadata = getChoiceQuestionMetadata(singleChoice);
24+
const expectedSingleChoiceMetadata = {
25+
'https://learninglocker&46;net/choice-response': ['golf'],
26+
};
27+
28+
assert.deepEqual(actualSingleChoiceMetadata, expectedSingleChoiceMetadata);
29+
});
30+
31+
it('should retrieve metadata from statement with multiple choices', () => {
32+
const actualMultipleChoicesMetadata = getChoiceQuestionMetadata(multipleChoices);
33+
const expectedMultipleChoicesMetadata = {
34+
'https://learninglocker&46;net/choice-response': ['golf', 'tetris'],
35+
};
36+
37+
assert
38+
.deepEqual(
39+
actualMultipleChoicesMetadata,
40+
expectedMultipleChoicesMetadata,
41+
);
42+
});
43+
});

src/apps/statements/tests/storingStatements/queriables/getMetadataFromStatement/getMetadataFromStatement.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { merge } from 'lodash';
33

44
import getMetadataFromStatement
55
from '../../../../service/storeStatements/queriables/getMetadataFromStatement';
6+
import { multipleChoices } from './fixtures/choice-interaction.fixture';
67
import { multipleMatchingQuestions } from './fixtures/matching-interaction.fixture';
78
import {
89
sequencingInteractionActivityStatement,
@@ -49,6 +50,24 @@ describe('Retrieve metadata from statement', () => {
4950
assert.deepEqual(actualMetadata, expectedMetadata);
5051
});
5152

53+
it('should return choices metadata from statement', () => {
54+
const actualMetadata = getMetadataFromStatement(
55+
{
56+
...multipleChoices,
57+
...{
58+
result: {
59+
response: multipleChoices.result?.response,
60+
},
61+
},
62+
},
63+
);
64+
const expectedMetadata = {
65+
'https://learninglocker&46;net/choice-response': ['golf', 'tetris'],
66+
};
67+
68+
assert.deepEqual(actualMetadata, expectedMetadata);
69+
});
70+
5271
it('should return matching questions metadata', () => {
5372
const actualMatchingQuestionsMetadata = getMetadataFromStatement(multipleMatchingQuestions);
5473
const expectedMatchingQuestionsMetadata = {

0 commit comments

Comments
 (0)