adlnet / xAPIWrapper

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

How to store state value(or etag) with getState() #104

Closed wzhonggo closed 6 years ago

wzhonggo commented 6 years ago

Hi

GetState function not store state value or etag. It there a simple way to store state value or etag?

FlorianTolk commented 6 years ago

Hello! Just making sure, you have seen the documentation in the js file, right?

      Get activity state from the LRS
      @param {string} activityid   the id of the Activity this state is about
      @param {object} agent   the agent this Activity state is related to
      @param {string} [stateid]    the id of the state, if not included, the response will be a list of stateids
                 associated with the activity and agent)
      @param {string} [registration]   the registraton id associated with this state
      @param {object} [since]    date object or date string telling the LRS to return objects newer than the date supplied
      @param {function} [callback]   function to be called after the LRS responds
                 to this request (makes the call asynchronous)
                 the function will be passed the XMLHttpRequest object
      @return {object} xhr response object or null if 404
      @example
      ADL.XAPIWrapper.getState("http://adlnet.gov/expapi/activities/question",
                       {"mbox":"mailto:tom@example.com"}, "questionstate");
      >> {info: "the state info"}

You should just need to include the stateid in the call if I am not mistaken.

Also, if you are trying to save the sate info, you should be using sendState which sends the information to your LRS.

vbhayden commented 6 years ago

sendState() is what you want.

The documentation for the function itself is a bit slim, admittedly.

wzhonggo commented 6 years ago

Hi FlorianTolk

I've read document with xAPIWrapper before i create this issue and I know how to send state with sendState. But i am not know how to store the state when use getState. Does it need to be store old state in localStorage by myself.

Example : I want to change old state if old state is {"info":"1"}, but not a simple way to store old state etag.

  1. send state id questionstate is {"info":"1"}
  2. get state id questionstate , LRS server will return {"info":"1"}.
  3. I want to change questionstate, so i will send state id questionstate is {"info":"2"} and old state sha1 with {"info":"1"}.
    var stateval = {"info":"2"};
    var oldstateval = {"info":"1"};
    var oldstatevalHash = ADL.XAPIWrapper.hash(JSON.stringify(oldstateval));
    ADL.XAPIWrapper.sendState("http://adlnet.gov/expapi/activities/question", 
                   {"mbox":"mailto:tom@example.com"}, 
                   "questionstate", null, stateval,oldstatevalHash);
vbhayden commented 6 years ago

You can store something locally or keep everything on the LRS -- it's up to you, really.

If you need assistance storing values on a local machine, then there are plenty of resources online for how to do that. :-)

I'll be closing this issue at the end of the week if there aren't any issues related to functionality.

vbhayden commented 6 years ago

After reading your question again: No there isn't any out-of-the-box functionality to save a history of state values.

But, there's nothing stopping you from saving your prior state as another state itself, or just attaching the history to your object itself (not recommended for performance reasons).

// The objects you pass can be anything serializable
//
var stateval = {"info":"2", "version": 1};
var oldstateval = {"info":"1", "version": 0};
var oldstatevalHash = ADL.XAPIWrapper.hash(JSON.stringify(oldstateval));

// Send the most recent one, which has its version number
//
ADL.XAPIWrapper.sendState("http://adlnet.gov/expapi/activities/question", 
                   {"mbox":"mailto:tom@example.com"}, 
                   "questionstate", null, stateval);

// Send the previous one, which also has its version number, but send it to a unique 
// IRL based on that version and the state ID
//
ADL.XAPIWrapper.sendState("http://adlnet.gov/expapi/activities/question", 
                   {"mbox":"mailto:tom@example.com"}, 
                   "questionstate/" + oldStateVal["version"], null, oldstateval );
wzhonggo commented 6 years ago

OK, I will customize the implementation of the old state of storage.

Thank you very much.