-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSMoodleConnect.lsl
290 lines (232 loc) · 8.81 KB
/
OSMoodleConnect.lsl
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Moodle LMS / Opensimulator Web Services
// Author: Josh Garrett - 2022
// Authorized for reuse under MIT License
// Latest Update: 4/27/23
// Enable Debugging
integer debug=0;
//Moodle Config - From Notecard
string configurationNotecardName = "";
key notecardQueryId;
integer line;
string AvatarName;
string Fullname;
string Email;
string Username;
string Password;
string userID;
// Web Service Access Token
string accessToken = "";
key req_id;
key touch_req_id;
string name;
// Listen Config
integer listenHandle;
remove_listen_handles()
{
llListenRemove(listenHandle);
}
init()
{
listenHandle = llListen(777, "", NULL_KEY, "");
name = llGetOwner();
AvatarName = llKey2Name(name);
if(llGetInventoryType(configurationNotecardName) != INVENTORY_NOTECARD)
{
llOwnerSay("Missing inventory notecard: " + configurationNotecardName);
return;
}
line = 0;
notecardQueryId = llGetNotecardLine(configurationNotecardName, line);
}
processConfiguration(string data)
{
if(data == EOF)
{
// Check Access Token
if (accessToken == "") {
if (debug==1) {
llOwnerSay("Debug: Requesting Access Token");
}
state getAccessToken;
}
llOwnerSay(AvatarName + " is now authorized as " + Username);
return;
}
if(data != "")
{
if(llSubStringIndex(data, "#") != 0)
{
// find first equal sign
integer i = llSubStringIndex(data, "=");
// if line contains equal sign
if(i != -1)
{
string name = llGetSubString(data, 0, i - 1);
string value = llGetSubString(data, i + 1, -1);
list temp = llParseString2List(name, [" "], []);
name = llDumpList2String(temp, " ");
name = llToLower(name);
temp = llParseString2List(value, [" "], []);
value = llDumpList2String(temp, " ");
if(name == "username")
Username = value;
else if(name == "password")
Password = value;
else
llOwnerSay("Unknown configuration value: " + name + " on line " + (string)line);
}
// line does not contain equal sign
else
{
llOwnerSay("Configuration could not be read on line " + (string)line);
}
}
}
// read the next line
notecardQueryId = llGetNotecardLine(configurationNotecardName, ++line);
}
// Send Activity Update to LMS
sendLMSUpdate(string data)
{
string regionname = llDumpList2String(llParseString2List(llGetRegionName(),[" "],[]),"%20");
req_id = llHTTPRequest("https://SERVERNAME/webservice/rest/server.php?wstoken=" + accessToken + "&wsfunction=mod_data_add_entry&databaseid=1&data[0][fieldid]=1&data[0][value]=\"" + activityTitle + "\"&data[1][fieldid]=4&data[1][value]=\"" + AvatarName + "\"&data[2][fieldid]=6&data[2][value]=\"" + activityContent + "\"&data[3][fieldid]=7&data[3][value]=\"" + activityURL + "\"&moodlewsrestformat=" + wsformat, [HTTP_METHOD, "POST"],"");
}
http_response(key request_id, integer status, list metadata, string body) {
if (req_id == request_id) {
string object = llJsonGetValue(body,[0]);
string data = llJsonGetValue( object, ["id"] );
userID = data;
if (debug==1) {
llOwnerSay("Debug body: " + body );
llOwnerSay("Debug: " + object );
llOwnerSay("Debug: " + data);
}
// state default;
}
}
// Gain Access Token based on credentials
state getAccessToken
{
state_entry()
{
req_id = llHTTPRequest("https://SERVERNAME/login/token.php?username=" + Username + "&password=" + Password + "&service=moodle_mobile_app", [HTTP_METHOD, "POST"],"");
}
http_response(key request_id, integer status, list metadata, string body) {
if (req_id == request_id) {
string data = llJsonGetValue( body, ["token"] );
accessToken = data;
if (debug==1) {
llOwnerSay("Debug: " + body );
llOwnerSay("Debug: Got Access Token: " + accessToken );
}
state getUserData;
}
}
}
// Get User details based on Access Token
state getUserData
{
state_entry()
{
req_id = llHTTPRequest("https://SERVERNAME/webservice/rest/server.php?wstoken=" + accessToken + "&wsfunction=core_user_get_users_by_field&field=username&values[0]=" + Username + "&moodlewsrestformat=" + wsformat, [HTTP_METHOD, "POST"],"");
}
http_response(key request_id, integer status, list metadata, string body) {
if (req_id == request_id) {
string object = llJsonGetValue(body,[0]);
userID = llJsonGetValue( object, ["id"] );
Fullname = llJsonGetValue( object, ["fullname"] );
Email = llJsonGetValue( object, ["email"] );
//userID = dataID;
if (debug==1) {
llOwnerSay("Debug: " + object );
llOwnerSay("Debug: Got User ID: " + userID);
llOwnerSay("Debug: Got Fullname: " + Fullname);
}
state userLogin;
}
}
}
// Get User details based on Access Token
state userLogin
{
state_entry()
{
string regionname = llDumpList2String(llParseString2List(llGetRegionName(),[" "],[]),"%20");
req_id = llHTTPRequest("https://SERVERNAME/webservice/rest/server.php?wstoken=" + accessToken + "&wsfunction=mod_data_add_entry&databaseid=1&data[0][fieldid]=1&data[0][value]=\"" + activityTitle + "\"&data[1][fieldid]=4&data[1][value]=\"" + AvatarName + "\"&data[2][fieldid]=6&data[2][value]=\"" + activityURL + "\"&data[3][fieldid]=7&data[3][value]=\"" + activityURL + "\"&moodlewsrestformat=" + wsformat, [HTTP_METHOD, "POST"],"");
}
http_response(key request_id, integer status, list metadata, string body) {
if (req_id == request_id) {
string object = llJsonGetValue(body,[0]);
string data = llJsonGetValue( object, ["id"] );
userID = data;
if (debug==1) {
llOwnerSay("Debug body: " + body );
llOwnerSay("Debug: " + object );
llOwnerSay("Debug: Got User Login: " + data);
}
state default;
}
}
}
default
{
on_rez(integer start_param)
{
accessToken = "";
init();
}
changed(integer change)
{
if(change & (CHANGED_OWNER | CHANGED_INVENTORY))
init();
}
state_entry()
{
init();
}
touch_start(integer total_number)
{
if (debug==1) {
llSay(0, AvatarName + " is making request with access token: " + accessToken);
}
touch_req_id = llHTTPRequest("https://SERVERNAME/webservice/rest/server.php?wstoken=" + accessToken + "&field=id&value=2&moodlewsrestformat=" + wsformat + "&wsfunction=" + wsfunction, [HTTP_METHOD, "POST"],"");
}
http_response(key touch_request_id, integer status, list metadata, string body) {
if (touch_req_id == touch_request_id) {
if (debug==1) {
if (llJsonValueType(body, []) == JSON_OBJECT)
{
llOwnerSay("The supplied string is a Json object.");
} else {
llOwnerSay("The supplied string is not a Json object."); // TRUE
}
if (llJsonValueType(body, [1]) == JSON_OBJECT)
{
llOwnerSay("The second element of the array is a Json object."); // TRUE
} else {
llOwnerSay("The second element of the array is not a Json object.");
}
}
string data = llJsonGetValue( body, ["courses"] );
list courses = llJson2List( data );
string studentdata = llJsonGetValue( courses, ["contacts"] );
list students = llJson2List( studentdata );
//llSay(0, courses);
string courseName = llJsonGetValue( courses, ["fullname"] );
string studentName = llJsonGetValue( students, ["fullname"] );
if (debug==1) {
llSay(0, "Courses: " + data );
}
llOwnerSay("Touched");
llOwnerSay("Course Name: " + courseName );
llOwnerSay("Student Name: " + Fullname );
llOwnerSay("Student Email: " + Email );
llSetText("",<1,0,0>,1);
}
}
dataserver(key request_id, string data)
{
if(request_id == notecardQueryId)
processConfiguration(data);
}
}