-
Notifications
You must be signed in to change notification settings - Fork 7
/
XAPIWrapper.cs
241 lines (213 loc) · 9.25 KB
/
XAPIWrapper.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace XAPI
{
/// <summary>
/// Interface into the XAPI functions contained within this wrapper.
/// </summary>
public sealed class XAPIWrapper
{
// Allow users to assign a common Actor for their statements
private static Actor commonActor;
/// <summary>
/// Check that an XAPIMessenger component exists and is ready to send requests.
/// </summary>
public static void CheckMessenger ()
{
if (XAPIMessenger.InstanceExists == false)
{
// Create an empty game object following our convention
new GameObject ("XAPI").AddComponent<XAPIMessenger> ();
}
}
/// <summary>
/// Assign an LRS configuration to this wrapper. This configuration will be used for
/// all statements sent after assignment.
/// </summary>
/// <param name="config"></param>
public static void AssignConfig (Config config)
{
// Make sure our messenger exists
XAPIWrapper.CheckMessenger ();
// Relay this to it
XAPIMessenger.AssignConfig (config);
}
/// <summary>
/// Assign a common actor that will be used for xAPI statements.
///
///
/// </summary>
/// <param name="actor"></param>
public static void AssignCommonActor(Actor actor)
{
XAPIWrapper.commonActor = actor;
}
/// <summary>
/// Send an array of statements to the configured LRS.
/// </summary>
/// <param name="statements">Statements to send.</param>
/// <param name="callback">Optional callback for once this operation ends.</param>
public static void SendStatements(Statement[] statements, Action<MultiStatementStoredResponse> callback = null)
{
XAPIWrapper.CheckMessenger();
XAPIMessenger.SendStatements(statements, callback);
}
/// <summary>
/// Send the given statement to the configured LRS.
/// </summary>
/// <param name="statements">Statement to send.</param>
/// <param name="callback">Optional callback for once this operation ends.</param>
public static void SendStatement(Statement statement, Action<StatementStoredResponse> callback = null)
{
// Make sure we have a messenger. Each overload of this function eventually
// uses this version, so we only need to check for a messenger here.
//
XAPIWrapper.CheckMessenger();
XAPIMessenger.SendStatement(statement, callback);
}
/// <summary>
/// Sends an xAPI statement to the configured LRS. This requires all necessary part of an xAPI statement.
/// </summary>
/// <param name="Actor"></param>
/// <param name="Verb"></param>
/// <param name="Object"></param>
/// <param name="Result"></param>
public static void SendStatement(Actor Actor, Verb Verb, Activity Activity, Result Result = null, Action<StatementStoredResponse> callback = null)
{
var statement = new Statement(Actor, Verb, Activity);
if (Result != null)
statement.Result = Result;
XAPIWrapper.SendStatement(statement, callback);
}
/// <summary>
/// Sends an xAPI statement to the configured LRS. This version will use the common actor.
///
/// To assign a common actor, provide an actor to XAPIWrapper.AssignCommonActor().
/// </summary>
/// <param name="Actor"></param>
/// <param name="Verb"></param>
/// <param name="Object"></param>
/// <param name="Result"></param>
public static void SendStatement(Verb Verb, Activity Activity, Result Result = null, Action<StatementStoredResponse> callback = null)
{
// Make sure we actually have a common actor
if (XAPIWrapper.HasCommonActor == false)
throw new System.NullReferenceException("XAPIWrapper.CommonActor is null. This value is required for this variation of XAPIWrapper.SendStatement");
XAPIWrapper.SendStatement(XAPIWrapper.CommonActor, Verb, Activity, Result, callback);
}
/// <summary>
/// Retrieves a single statement from the configured LRS.
///
/// As you can't overload a function with identical argument types, this is private with
/// a slightly different name.
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
private static void GetStatement(StatementQuery query, Action<Statement, UnityWebRequest> callback)
{
// Make sure we have a messenger
XAPIWrapper.CheckMessenger();
// Build the fully qualified URL
string url = XAPIMessenger.Config.StatementEndpoint + query.BuildQueryString();;
XAPIMessenger.GetStatement(url, callback);
}
/// <summary>
/// Retrieves a single statement from the configured LRS.
///
/// You must supply the statement ID as an argument
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public static void GetStatement(string statementId, Action<Statement, UnityWebRequest> callback)
{
// Build the query
StatementQuery query = new StatementQuery();
query.statementId = statementId;
XAPIWrapper.GetStatement(query, callback);
}
/// <summary>
/// Retrieves a single statement from the configured LRS.
///
/// You must supply the statement ID as an argument
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public static void GetVoidedStatement(string voidedStatementId, Action<Statement, UnityWebRequest> callback)
{
// Build the query
StatementQuery query = new StatementQuery();
query.voidedStatementId = voidedStatementId;
XAPIWrapper.GetStatement(query, callback);
}
/// <summary>
/// Retrieves statements from a fully-qualified URL pointing to the configured LRS.
///
/// This is the most granular version of the call and should only be made by the wrapper itself.
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public static void GetStatements(string url, Action<StatementResult, UnityWebRequest> callback)
{
// Make sure we have a messenger
XAPIWrapper.CheckMessenger();
XAPIMessenger.GetStatements(url, callback);
}
/// <summary>
/// Retrieves statements according to the StatementQuery values.
///
/// The StatementResult object in the callback may contain a `MoreIRL` value. If that is the case,
/// then more statements matching your query can be returned using that endpoints with your LRS.
/// </summary>
/// <param name="result"></param>
/// <param name="callback"></param>
public static void GetStatements(StatementQuery query, Action<StatementResult, UnityWebRequest> callback)
{
string url = XAPIMessenger.Config.StatementEndpoint + query.BuildQueryString();
XAPIMessenger.GetStatements(url, callback);
}
/// <summary>
/// Retrieves statements using the "more" property attached to a previous statement response.
///
/// This function will remove the first parts of that "more" property to fit your LRS endpoint.
/// </summary>
/// <param name="moreIrl"></param>
/// <param name="callback"></param>
public static void GetMoreStatements(string moreIrl, Action<StatementResult, UnityWebRequest> callback)
{
// Make sure it's formatted correctly
if (moreIrl.Contains("/more/"))
{
// Find out where it is
int index = moreIrl.IndexOf("/more/");
string more = moreIrl.Substring(index, moreIrl.Length - index);
// Build the fully qualified URL
string url = XAPIMessenger.Config.Endpoint + more;
// Make the request
XAPIMessenger.GetStatements(url, callback);
}
else
{
throw new ArgumentException("XAPIWrapper.GetMoreStatements: moreIrl argument must contain \"/more/\"");
}
}
/// <summary>
/// Gets the common actor in use by the XAPIWrapper.
/// </summary>
/// <value></value>
public static Actor CommonActor
{
get { return XAPIWrapper.commonActor; }
}
/// <summary>
/// Gets whether or not a common actor has been assigned to the wrapper.
/// </summary>
/// <value></value>
public static bool HasCommonActor
{
get { return XAPIWrapper.commonActor != null; }
}
}
}