forked from adlnet/xAPIWrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxapi-util.js
More file actions
157 lines (132 loc) · 4.83 KB
/
xapi-util.js
File metadata and controls
157 lines (132 loc) · 4.83 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(function (obj) {
var ADL = obj;
var onBrowser = false;
if (typeof window !== 'undefined') {
ADL = window.ADL = obj.ADL || {};
onBrowser = true;
}
var getObjDefName = function (o) {
if (o.definition && o.definition.name) {
return ADL.xapiutil.getLangVal(o.definition.name);
}
return undefined;
};
var getSubStatementDisplay = function (o) {
if(o.objectType !== "SubStatement") return;
if(o.object.objectType === "SubStatement") return;
if(o.id || o.stored || o.version || o.authority) return;
var disp = ADL.xapiutil.getActorId(o.actor) + ":" + ADL.xapiutil.getVerbDisplay(o.verb) + ":" + ADL.xapiutil.getObjectId(o.object);
return disp;
};
ADL.xapiutil = {};
ADL.xapiutil.getLang = function () {
var lang;
if (typeof navigator !== 'undefined')
lang = navigator.language || navigator.browserLanguage ||
navigator.systemLanguage || navigator.userLanguage;
else if (typeof process !== 'undefined' && typeof process.env !== 'undefined' && typeof process.env.LANG !== 'undefined') {
var str = process.env.LANG;
lang = str.slice(0, str.indexOf('.'));
lang = lang.replace(/_/, '-')
}
return lang || "en-US";
};
ADL.xapiutil.getLangVal = function (langprop) {
if (!langprop) return;
var options = Object.keys(langprop);
// test that langprop is a dict (obj)
// skips if not a dict(obj) and returns
if (options.length == 0) return undefined;
var lang = ADL.xapiutil.getLang(),
ret,
dispGotten = false;
do { //test and retest
if (langprop[lang])
{
ret = langprop[lang];
dispGotten = true;
}
else if (lang.indexOf('-'))
{
lang = lang.substring(0, lang.lastIndexOf('-'));
}
} while (!dispGotten && lang !=="");
return ret;
};
ADL.xapiutil.getMoreStatements = function (iterations, callback, searchParams) {
if (!onBrowser) throw new Error("Node not supported.");
var stmts = [];
ADL.XAPIWrapper.getStatements(searchParams, null, function getMore(r) {
if (! (r && r.response) ) return;
var res = JSON.parse(r.response);
if (! res.statements) return;
stmts = stmts.concat(res.statements);
if (iterations-- <= 0) {
callback(stmts);
}
else {
if (res.more && res.more !== "")
{
ADL.XAPIWrapper.getStatements(searchParams, res.more, getMore);
}
else if (res.more === "")
{
callback(stmts);
}
}
});
};
ADL.xapiutil.getActorId = function (a) {
return a.mbox || a.openid || a.mbox_sha1sum || a.account;
};
ADL.xapiutil.getActorIdString = function (a) {
var id = a.mbox || a.openid || a.mbox_sha1sum;
if (! id) {
if (a.account) id = a.account.homePage + ":" + a.account.name;
else if (a.member) id = "Anon Group " + a.member;
else id = 'unknown';
}
return id;
};
ADL.xapiutil.getActorDisplay = function (a) {
return a.name || ADL.xapiutil.getActorIdString(a);
};
ADL.xapiutil.getVerbDisplay = function (v) {
if (!v) return;
if (v.display) {
return ADL.xapiutil.getLangVal(v.display) || v.id;
}
return v.id;
};
ADL.xapiutil.getObjectType = function (o) {
return o.objectType || ((o.id) ? "Activity" : "Agent");
};
ADL.xapiutil.getObjectId = function (o) {
if (o.id) return o.id;
var type = ADL.xapiutil.getObjectType(o);
if (type === "Agent" || type === "Group") return ADL.xapiutil.getActorId(o);
return undefined;
};
ADL.xapiutil.getObjectIdString = function (o) {
if (!o) return 'unknown'
if (o.id) return o.id;
var type = ADL.xapiutil.getObjectType(o);
if (type === "Agent" || type === "Group") return ADL.xapiutil.getActorIdString(o);
else if (type == "SubStatement") {
return getSubStatementDisplay(o);
}
return 'unknown';
};
ADL.xapiutil.getObjectDisplay = function (o) {
if (!o) return "unknown"
var disp = getObjDefName(o) || o.name || o.id;
if (! disp) {
var type = ADL.xapiutil.getObjectType(o);
if (type === "Agent" || type == "Group") disp = ADL.xapiutil.getActorDisplay(o);
else if (type == "SubStatement") {
disp = getSubStatementDisplay(o);
}
}
return disp;
};
})(this);