-
Notifications
You must be signed in to change notification settings - Fork 4
/
TinCanCan.js
574 lines (442 loc) · 15.5 KB
/
TinCanCan.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
=============COPYRIGHT============
Tin Can-Can - A Tin Can API wrapper for Adobe Captivate
Copyright (C) 2012 Andrew Downes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<http://www.gnu.org/licenses/>.
*/
/*============SWF EMBED VARIABLES==============*/
var strURLParams = "?SCORM_API=1.0&SCORM_TYPE=0",
flashvars = {},
params = { wmode: "opaque", allowScriptAccess: "always", bgcolor: "#f5f4f1", menu: "false" },
attributes = { id: "Captivate", name: "Captivate" };
//Handler for the Captivate swf element
/*============END SWF EMBED VARIABLES==============*/
/*============OTHER GLOBAL VARIABLES==============*/
var value_store = {}, //stores values such as score so that data is only sent to the LMS when it has chnaged.
attemptInProgress = false, //value currently sent to LRS
attemptCompleted = false, //value currently sent to LRS
completionChanged = false, //used to track if the completion status has been chnaged but not yet reported.
startDuration, //the attempt duration at the start of the session
offsetDuration; //used to account for session time that happeend prior to the current attempt.
/*============END OTHER GLOBAL VARIABLES==============*/
TinCan.DEBUG = true;
//Create an instance of the Tin Can Library
var myTinCan = new TinCan();
/*============CREATE LRS OBJECT==============*/
//get the array of LRSes from the query string
var LRSArray = getObjectFromQueryString('lrs');
//For each LRS in that array...
$.each(LRSArray,function(index){
//...make a new LRS object (some reformatting required)...
var myLRS = new TinCan.LRS({
endpoint: LRSArray[index].endpoint,
version: "0.95",
auth: 'Basic ' + Base64.encode(LRSArray[index].login + ':' + LRSArray[index].pass)
});
//...and add it to the Tin Can Object's library of record stores
myTinCan.recordStores[index] = myLRS;
});
/*============END CREATE LRS OBJECT==============*/
/*============CREATE ACTOR OBJECT==============*/
//Get the actor object from the querystring and use it to define a TinCan Agent
var myActor= new TinCan.Agent(getObjectFromQueryString('actor'));
//Tell the Tin Can driver to use this agent (TODO: test what this actually does i.e. does it affect authority or is it just a default?)
myTinCan.actor = myActor;
/*============END CREATE ACTOR OBJECT==============*/
/*============CREATE THE ACTIVTY OBJECT==============*/
//TODO: PULL THESE VALUES FROM XML OR SOMETHING
//TODO: Add support for multiple languages
var courseName = "Name of Captivate Course in trans-Atlantic English";
var courseDesc = "Description of Captivate Course in trans-Atlantic English";
var courseId = "http://example.com/exampleCaptivate";
//END Pull for XML
//Create the activity definition
var captivateActivityDefinition = new TinCan.ActivityDefinition({
type : "http://adlnet.gov/expapi/activities/course",
name: {
"en-US" : courseName,
"en-GB" : courseName
},
description: {
"en-US" : courseDesc,
"en-GB" : courseDesc
},
});
//Create the activity
var myActivity = new TinCan.Activity({
id : courseId,
definition : captivateActivityDefinition
});
//Tell the Tin Can driver to use this activity as default
myTinCan.activity = myActivity;
/*============END CREATE THE ACTIVTY OBJECT==============*/
//TODO: ADD CONTEXT TO INCLUDE REVISION AND REGISTRATION AS A MINIMUM
/*============CREATE PREDEFINED STATEMENT TEMPLATES==============*/
//Declare statement Collection as a new Object to hold our template statements
var statementsCollection = new Object();
//For each verb, create an instance of the Tin Can Verb Object and add it to the collection for later use.
//attempted - an attempt has happened but the session ended before it was completed.
statementsCollection.attempted = makeTemplateStatement();
statementsCollection.attempted.verb = getVerb("attempted", "http://adlnet.gov/expapi/verbs/");
statementsCollection.attempted.result=getResult(false);
//completed - an attempted has been completed but we are making no assertions as to whether it was successful or not
statementsCollection.completed = makeTemplateStatement();
statementsCollection.completed.verb = getVerb("completed", "http://adlnet.gov/expapi/verbs/");
statementsCollection.completed.result =getResult(true);
//passed - an attempt has been completed successfully
statementsCollection.passed = makeTemplateStatement();
statementsCollection.passed.verb = getVerb("passed", "http://adlnet.gov/expapi/verbs/");
statementsCollection.passed.result = getResult(true, true);
//failed - an attempt has been completed but it was not sucecssful
statementsCollection.failed = makeTemplateStatement();
statementsCollection.failed.verb = getVerb("failed", "http://adlnet.gov/expapi/verbs/");
statementsCollection.failed.result = getResult(true, false);
//started - A session started
statementsCollection.started = makeTemplateStatement();
statementsCollection.started.verb = getVerb("initialized", "http://adlnet.gov/expapi/verbs/");
//stopped - A session ended
statementsCollection.stopped = makeTemplateStatement();
statementsCollection.stopped.verb = getVerb("terminated", "http://adlnet.gov/expapi/verbs/");
/*============END=============*/
/*============SET STATE SETTINGS==============*/
var stateCfg = {
actor: myActor,
activity: myActivity,
registration: '55da1b40-4181-11e2-a25f-0800200c9a66'
//Note this unique UUID was generated by a UUID gentator so is and always will be unique. What? You wanted a DIFFERRENT unique number every time?
//TODO: put registration UUID in querystring and in Statement Context
}
TCCGetState();
/*============END=============*/
/*============CREATE CMI CACHE==============*/
var cmiCacheResultChanged = false; //if true, we have new data to send to the LRS
var cmiCache = function(property, value){
//Ensure we have a valid property to work with
if(typeof property === "undefined"){ return false; }
//Replace all periods in CMI property names so we don't run into JS errors
property = property.replace(/\./g,'_');
//If cached value exists, return it
if(typeof value_store[property] !== "undefined"){
return value_store[property];
}
//Otherwise add to cache
if(typeof value !== "undefined"){
value_store[property] = value;
}
return false;
};
/*============END CREATE CMI CACHE==============*/
$(function(){
/*============LAUNCH CODE==============*/
//Get the duration from the state if it exists (default "" which we interpret as "PT0H0M0S") and save it for later comparision.
startDuration = value_store["cmi.total_time"]; //must be called AFTER the LRS object has been created
//Send a started statement
var stmtToSend;
stmtToSend = statementsCollection.started;
stmtToSend.result = new TinCan.Result
stmtToSend.result.duration = "PT0S";
//Send the statement, no callback
myTinCan.sendStatement(stmtToSend, function() {});
/*============END LAUNCH CODE==============*/
});
/*===========================================FUNCTIONS ONLY PAST THIS POINT========================================================*/
function makeTemplateStatement()
{
//create a base statement
return new TinCan.Statement({
actor : myActor,
target : myActivity
},true);
}
function getResult(completion,success)
{
if (typeof success == 'undefined')
{
return new TinCan.Result({
completion : completion,
});
}
else
{
return new TinCan.Result({
completion : completion,
success : success
});
}
}
function getVerb(verb, library, display)
{
display = typeof display == 'undefined' ? verb : display;
return new TinCan.Verb({
id : library + verb,
display : {
"en-US" : display,
"en-GB" : display
}
});
}
function getObjectFromQueryString(objectToGet)
{
var qString = $.getUrlVar(objectToGet);
if (!(qString == undefined))
{
var objectToReturn = JSON.parse(urldecode(qString));
return objectToReturn;
}
else
{
return;
}
}
/*============DURATION FUNCTIONS==============*/
function convertCMITimespanToSeconds(CMITimespan)
{
var isValueNegative = (CMITimespan.indexOf('-') >= 0);
var indexOfT = CMITimespan.indexOf("T");
var indexOfH = CMITimespan.indexOf("H");
var indexOfM = CMITimespan.indexOf("M");
var indexOfS = CMITimespan.indexOf("S");
var hours;
var minutes;
var seconds;
if (indexOfH == -1) {
indexOfH = indexOfT;
hours = 0;
}
else {
hours = parseInt(CMITimespan.slice(indexOfT + 1, indexOfH));
};
if (indexOfM == -1) {
indexOfM = indexOfPT
minutes = 0;
}
else
{
minutes = parseInt(CMITimespan.slice(indexOfH + 1, indexOfM));
};
seconds = parseInt(CMITimespan.slice(indexOfM + 1, indexOfS));
var timespanInSeconds = parseInt((((hours * 60) + minutes) * 60) + seconds);
if (isNaN(timespanInSeconds)){
timespanInSeconds=0
};
if (isValueNegative) {
timespanInSeconds = timespanInSeconds * -1;
};
return timespanInSeconds;
}
function convertSecondsToCMITimespan(inputSeconds)
{
var hours, minutes, seconds,
i_inputSeconds = parseInt(inputSeconds);
var inputIsNegative = "";
if (i_inputSeconds < 0)
{
inputIsNegative = "-";
i_inputSeconds = i_inputSeconds * -1;
}
hours = parseInt((i_inputSeconds) / 3600);
minutes = parseInt(((i_inputSeconds) % 3600) / 60);
seconds = parseInt(((i_inputSeconds) % 3600) % 60);
var rtnStr = inputIsNegative + "PT";
if (hours > 0)
{
rtnStr += hours +"H";
}
if (minutes > 0)
{
rtnStr += minutes +"M";
}
return rtnStr + seconds +"S";
}
function resetAttemptDuration() //reset the attempt duration timer to zero
{
compareWithCacheAndSetState("cmi.total_time","PT0S",false)
startDuration = "PT0S";
offsetDuration
if (value_store["cmi.session_time"] =='')
{
offsetDuration = "PT0S";
}
else
{
offsetDuration = "-" + value_store["cmi.session_time"];
}
};
/*============END DURATION FUNCTIONS==============*/
function TCCSendLessonData()
{
//This function is called whenever completion status changes
//Send completed, passed, failed or attempted statement at end of attempt
if (!(attemptCompleted))
{
endAttempt();
attemptCompleted = true; //Only send this success data once.
//reset the clock. Note: this means slides after the completed attempt but before learner restarts are counted towards next attempt
resetAttemptDuration();
}
}
function endAttempt()
{
var stmtToSend;
if (value_store["cmi.success_status"] == "passed")
{
stmtToSend = statementsCollection.passed;
}
else if (value_store["cmi.success_status"] == "failed")
{
stmtToSend = statementsCollection.failed;
}
else
{
if (value_store["cmi.completion_status"] == "completed")
{
stmtToSend = statementsCollection.completed;
}
else
{
stmtToSend = statementsCollection.attempted;
}
}
//Set the score, if present
stmtToSend.result.score = new TinCan.Score;
stmtToSend.result.score.scaled = value_store["cmi.score.scaled"];
stmtToSend.result.score.raw = value_store["cmi.score.raw"];
stmtToSend.result.score.min = value_store["cmi.score.min"];
stmtToSend.result.score.max = value_store["cmi.score.max"];
stmtToSend.result.score = deleteEmptyProperties(stmtToSend.result.score);
//Set the Duration
stmtToSend.result.duration = value_store["cmi.total_time"];
//Send the statement, no callback
myTinCan.sendStatement(stmtToSend, function() {});
}
function sessionStoppedStatement()
{
var stmtToSend;
var sessionDuration = value_store["cmi.session_time"];
if (sessionDuration == "")
{sessionDuration= "PT0S"}
stmtToSend = statementsCollection.stopped;
stmtToSend.result = new TinCan.Result
stmtToSend.result.duration = sessionDuration;
//Send the statement, no callback
myTinCan.sendStatement(stmtToSend, function() {});
}
function TCCSendInteractionData(interactionIndex)
{
//Captivate doesn't report a desciption
//TODO: get these from somewhere clever e.g. some xml file
var enInterationDescription = "Captivate Question";
var questionName = "Captivate Question";
//verb
var interactionVerb = getVerb("answered", "http://adlnet.gov/expapi/verbs/");
var correctResponsesPattern = new Array();
var correctResponsesIndex = 0;
while(typeof value_store["cmi.interactions." + interactionIndex + ".correct_responses." + correctResponsesIndex + ".pattern"] !== "undefined")
{
correctResponsesPattern[correctResponsesIndex] = value_store["cmi.interactions." + interactionIndex + ".correct_responses." + correctResponsesIndex + ".pattern"];
correctResponsesIndex++;
}
//Create the activity definition
var interactionDefinition = new TinCan.ActivityDefinition({
type : "http://adlnet.gov/expapi/activities/cmi.interaction",
name: {
"en-US" : questionName,
"en-GB" : questionName
},
description: {
"en-US" : enInterationDescription,
"en-GB" : enInterationDescription
},
"interactionType": value_store["cmi.interactions." + interactionIndex + ".type"],
"correctResponsesPattern": correctResponsesPattern
});
//Create the activity
var myInteraction = new TinCan.Activity({
id : courseId + ':interactions#' + value_store["cmi.interactions." + interactionIndex + ".id"],
definition : interactionDefinition
});
//Result
var interactionResult = getResult(true);
interactionResult.response = value_store["cmi.interactions." + interactionIndex + ".learner_response"];
interactionResult.duration = value_store["cmi.interactions." + interactionIndex + ".latency"]
//Score
interactionResult.score = new TinCan.Score;
interactionResult.score.min = 0;
interactionResult.score.max = parseInt(value_store["cmi.interactions." + interactionIndex + ".weighting"]);
switch (value_store["cmi.interactions." + interactionIndex + ".result"])
{
case "correct":
interactionResult.success = true;
interactionResult.score.scaled = 1;
interactionResult.score.raw = interactionResult.score.max;
break;
case "incorrect":
interactionResult.success = false;
interactionResult.score.scaled = 0;
interactionResult.score.raw = interactionResult.score.min;
break;
default:
break;
}
//TODO: add context, particularly parent.
//Statement
var interactionStmt = new TinCan.Statement({
actor : myTinCan.actor,
verb: interactionVerb,
target : myInteraction,
result: interactionResult,
timestamp: value_store["cmi.interactions." + interactionIndex + ".timestamp"] + '.000Z'
},true);
myTinCan.sendStatement(interactionStmt, function() {});
}
function compareWithCacheAndSetState(parameter,value,sendStateNow)
{
var cached_value = cmiCache(parameter, value);
if(!cached_value || cached_value !== value) {
//Save to the value store
value_store[parameter] = value;
if (sendStateNow)
{
var stateJSON = JSON.stringify(value_store);
myTinCan.setState('SCORMValues', stateJSON, stateCfg);
}
return true;
}
else
{
return false;
}
}
/*============END SEND DATA TO LRS==============*/
/*============GET DATA FROM LRS==============*/
function TCCGetState()
{
var rtnState = myTinCan.getState('SCORMValues', stateCfg).state;
if (rtnState)
{
var rtnString = rtnState.contents;
var rtnArray = JSON.parse(rtnString);
$.each(rtnArray, function(key, value){
value_store[key] = value;
});
}
}
function TCCGetFromCache(parameter)
{
if (value_store[parameter])
{
return value_store[parameter];
}
else
{
return "";
}
}
/*============END GET DATA FROM LRS==============*/