adlnet / xAPIWrapper

Wrapper to simplify communication to an LRS
https://adlnet.gov/projects/xapi/
Apache License 2.0
219 stars 114 forks source link

having issue working with get more statements #76

Closed sharadmaheshwari closed 7 years ago

sharadmaheshwari commented 7 years ago

I am facing issue processing more statements from XAPI. In my code below, I am using XAPI Wrapper and I am getting around 900 statements from lrs in rek, however, when I convert them into a collection and try to use the where clause, it does not give me any result.

Please Note: I can get the same thing done using fetchAllStatementes and it also works fine with getStatements (for only the first 100 records), however, I wish to do it manually using getMoreStatements.

Is there an example of merging statements using get more statements, creating a collection on top of it and then using the where query to filter the data?

My code: var stmt1 = JSON.stringify({ "mbox": actor.mbox , "name": actor.name });

var stmt2 = JSON.stringify({
    "mbox": "mailto:abc@xyz.xom"  
});

var stmtVerb = JSON.stringify({
    "id": "http://adlnet.gov/expapi/verbs/started"
});

var stmtCtx = JSON.stringify({
    "contextActivities": {
        "grouping": [
            {
                "id": competency.iri
            }
        ]
    }
});

var search = ADL.XAPIWrapper.searchParams();
search['agent'] = stmt1;
search['authority'] = stmt2;
search['context'] = stmtCtx;
var rek = []
ADL.XAPIWrapper.getStatements(search, null,
   function getmore(r){
      var res = JSON.parse(r.response);
      $.each(res.statements, function(x,y){
        rek.push(y)  ;
      });

      if (res.more && res.more !== ""){
         ADL.XAPIWrapper.getStatements(search, res.more, getmore);
      }
   });

console.log(rek);

//var ret = ADL.XAPIWrapper.getStatements(search);  //works fine
//var statements = new ADL.Collection(ret.statements);  

var stmt = new ADL.Collection(rek);
var filtered_data = stmt.where('actor.name = "ccazabon"').exec(function(data){
                    console.log(data);//no output-empty array, however, matching data does exists
                });

var p = [{'name': 'Steven', 'age': 25, 'dob':{ 'mm': 'jan', 'dd': '18' }}, {'name': 'John', 'age': 35, 'dob':{ 'mm': 'feb', 'dd': '19' }}]; //console.log(p); var a = new ADL.Collection(p); //console.log(a); var d = a.where('dob.mm = "jan"').exec(function(data){console.log(data)});//works as expected

I think there is some issue with merging all the statements and creating the ADL.Collection, however, I am not getting any online help to resolve the same. Your help is greatly appreciated.

creighton commented 7 years ago

First try fixing your search params. 'authority' and 'context' are not support keys. See the parameter table in Section 2.1.3 Get Statements.

For authority, you would set the 'agent' parameter to your agent (stmt2 in your example) and set 'related_agents' to true. This is a problem because you are already setting 'agent' to another agent (stmt1). A GET /statements query in xAPI can only have one agent specified. You should query the LRS for which ever agent will get you all of the statements you want, then filter via Javascript to remove any unwanted statements client side.

For context, you would set the 'object' parameter to your context (stmtCtx) and set 'related_activities' to true.

Try something like this..

var search = ADL.XAPIWrapper.searchParams();
search['agent'] = stmt1;
search['related_agents'] = true;
search['activity'] = competency.iri;
search['related_activities'] = true;
sharadmaheshwari commented 7 years ago

As mentioned before, I am able to fetch all the 900 statements from LRS. Fetching statements based on search parameter is not an issue (you can even ignore the search param for now, as the issue is related to merging more than 100 statements and using adl coleltion to perform sql-like query)

I am able to get the required data in an array based on my search criteria. Everything works if I just fetch the first 100 statements with only getStatements. However, the issue is when I try to merge more than 100 statements and filter them using ADL collection.

creighton commented 7 years ago

getStatements with a callback is async. Try setting the collection w/in the callback

sharadmaheshwari commented 7 years ago

Thank you, it worked!

creighton commented 7 years ago

nice. glad it's working for you.