Trying to parse xAPI statements from an LRS

584 Views Asked by At

When I try to return records from an Learning record store I am successful but it comes in as an unparsed array. Figure 1 below shows the code that is successful for me at bringing in this unparsed data.

When I use the code in Figure 2 to bring in just three parsed fields it doesn't work. After executing, nothing is displayed in the browser. Can someone help me with the code in Figure 2 ? Thanks very much.

Figure 1:

<!DOCTYPE  html> 
<html lang='en'> 
    <head> 
        <meta charset='UTF-8'> 
        <title>Get statements  101</title> 
        <script type="text/javascript" src="tincan.js"></script> 
    </head> 
    <body> 
        <h1>Pull info from the LRS</h1> 
        <div id='response'></div>
        <script>
       var tincan = new TinCan (
    {
        recordStores: [
            {
                endpoint: "https://lrs.adlnet.gov/xapi/",
                username: "xapi-tools",
                password: "xapi-tools",
                allowFail: false
            }
        ]
    }
);

        var container =  document.getElementById('response');
        tincan.getStatements({
            'callback': function (err, result) {
                container.innerHTML = (err !== null ? 'ERROR' : JSON.stringify(result.statements));
            }
        });
        </script> 
    </body> 
</html>

Figure 2:

<!DOCTYPE  html> 
<html lang='en'> 
    <head> 
        <meta charset='UTF-8'> 
        <title>Pulling Data from the LRS</title> 
        <script type="text/javascript" src="tincan.js"></script> 
    </head> 
    <body> 
        <h1>Get statements</h1> 

        <script>
       var tincan = new TinCan (
    {
        recordStores: [
            {
                endpoint: "https://lrs.adlnet.gov/xapi/",
                username: "xapi-tools",
                password: "xapi-tools",
                allowFail: false
            }
        ]
    }
);


        tincan.getStatements({
    'callback': function (err, result) {
        container.innerHTML = (err !== null ? 'ERROR' : parseMyData(result));
    }
});
parseMyData = function(result) {
            var statements = result.statements;
            var output = '';
            var name,verb,activity;
            for(var i=0;i<statements.length;i++){
                // check the statement for a usable name value
                // (priority = actor.name, actor.mbox, actor.account.name)
                if(statements[i].actor.name != null && statements[i].actor.name != "") {
                    name = statements[i].actor.name
                }else if(statements[i].actor.mbox != null && statements[i].actor.mbox != "") {
                    name = statements[i].actor.mbox
                }else{
                    name = statements[i].actor.account.name
                }
                // check the statement for a usable verb value
                // (priority = verb.display['en-US'], verb.id)
                try{
                    verb = statements[i].verb.display['en-US'];
                }catch(e){
                    verb = statements[i].verb.id;
                }
                // check the activity for a usable value
                // (priority = definition.name['en-US'], id)
                try{
                    activity = statements[i].target.definition.name['en-US'];
                }catch(e){
                    activity = statements[i].target.id;
                }
                output +=   name + ' - ' +
                            verb + ' - ' + 
                            activity +
                            '<br>' 
            }
            return output;

        </script> 
    </body> 
</html>
0

There are 0 best solutions below