-
Notifications
You must be signed in to change notification settings - Fork 10
/
use-handle-learner-event.tsx
98 lines (89 loc) · 2.54 KB
/
use-handle-learner-event.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { LearnerEventData } from '@editor/plugin/helpers/editor-learner-event'
import XAPI from '@xapi/xapi'
import { useAuthentication } from '@/auth/use-authentication'
import { useEntityData } from '@/contexts/uuids-context'
const endpoint = 'https://watershedlrs.com/api/organizations/25975/lrs'
const key = '97c9dcdcc2b2d8' //
const secret = '44ac953d3f72a4'
const auth = XAPI.toBasicAuth(key, secret)
const xapi = new XAPI({
endpoint,
auth,
})
/**
* Quick example of how a xAPI implementation could look like
*/
export function useSerloHandleLearnerEvent() {
const auth = useAuthentication()
const { entityId } = useEntityData()
function handleLearnerEvent(data: LearnerEventData) {
// eslint-disable-next-line no-console
console.log(data)
const result =
data.correct !== undefined ? { success: data.correct } : undefined
const shortId = data.pluginId?.split('-')[0]
void xapi.sendStatement({
statement: {
actor: {
objectType: 'Agent',
name: auth?.username ?? '___testuser___',
account: {
homePage: 'https://serlo.org',
name: String(auth?.id ?? '___testuser___'),
},
},
verb: verbMap[data.verb],
object: {
objectType: 'Activity',
id: `https://serlo.org/${entityId}#${shortId}`,
definition: {
name: {
'en-US': data.contentType,
},
interactionType: interactionTypeMap[data.contentType],
type: 'http://adlnet.gov/expapi/activities/cmi.interaction',
},
},
result,
},
})
}
return handleLearnerEvent
}
const verbMap = {
opened: {
id: 'http://activitystrea.ms/schema/1.0/open',
display: {
'en-US': 'opened',
},
},
attempted: {
id: 'http://adlnet.gov/expapi/verbs/attempted',
display: {
'en-US': 'attempted',
},
},
interacted: {
id: 'http://adlnet.gov/expapi/verbs/interacted',
display: {
'en-US': 'interacted',
},
},
answered: {
id: 'http://adlnet.gov/expapi/verbs/answered',
display: {
'en-US': 'answered',
},
},
} as const
// https:github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#interaction-types
//true-false, choice, fill-in, long-fill-in, matching, performance, sequencing, likert, numeric, other
const interactionTypeMap = {
'input-exercise': 'fill-in',
'sc-exercise': 'choice',
'mc-exercise': 'choice',
'blanks-exercise': 'fill-in',
'h5p-exercise': 'other',
spoiler: 'other',
solution: 'other',
} as const