javascriptlrsxapi

How to query LRS by agent via javascript


I feel like I've tried everything, but I keep coming up short. I am working on a course in Storyline 360, and I am able to return statements just fine when using verbs and object IDs, but no matter what I do to try and return statements for a specific Agent, I cannot get a query to go through.

Here's my code as it stands now - where I do return plenty of statements...what I need to know is how to have it query the current learner's statements for matches. I'm able to pull in their name or mbox, but trying to pass those through in my params fails on me every time.

Any help is very much appreciated!

var lrs;
var statementFound = false;
var player = GetPlayer();
try {
    lrs = new TinCan.LRS(
        {
            endpoint: "https://cloud.scorm.com/lrs/MYINFO/",
            username: "MYINFO",
            password: "MYINFO",
            allowFail: false
        }
    );
}
catch (ex) {
    console.log("Failed to setup LRS object: ", ex);
    // TODO: do something with error, can't communicate with LRS
};

var myObj = JSON.parse(getParameterByName('actor'));
lrs.queryStatements(
    {
        params: {
            verb: new TinCan.Verb(
                { 
                   id:  "http://adlnet.gov/expapi/verbs/answered"
                }
            )
    },
        callback: function (err, sr) {
            if (err !== null) {
                console.log("Failed to query statements: " + err);
                // TODO: do something with error, didn't get statements
                return;
             }

            if (sr.more !== null) {
                // TODO: additional page(s) of statements should be fetched
             }

           if (sr.statements.length > 0) {
            statementFound = true;
             console.log(sr.statements);
             player.SetVar("sf",statementFound);
            }
        }
    }
);

var myObj is able to pull in the necessary info to ID the learner if needed - but again, I just can't figure out how to get it passed in the query.


Solution

  • You need to set an agent property in the params object passed in the first argument. Assuming the Agent is the actor in statements.

    lrs.queryStatements(
        {
            params: {
                agent: TinCan.Agent.fromJSON(getParameterByName('actor'))
            }
        },
        ...
    );