Comparing user max_score achieved to game pass score

Hello, 

I am coding out an online games portal in WordPress using h5p and am looking to show a game progress indicator that shows if the user:

1. Has attempted the game
2. Has passed the game

I can see in the h5p_results table the 'score' and 'max_score' entires for the user's attempt at the game, which is excellent - and solves point 1 above. But for point 2 I need to know what the possible max score of the game is so that I can compare the user's max_score and determine if they have passed or not.

Is this possible? I am assuming the max possible score for a game must be stored somewhere to work out whether the user passes or not whilst playing the game. 

Please can someone point me to the right place?

Many thanks in advance, 
Adam

otacke's picture

Hi Adam!

The max score information is computed at runtime and can be retrieved by calling getMaxScore() on the instance object (and then passing it to the server using AJAX). In short: you'd need to retrieve it when the content type is viewed. Alternatively, you could listen for the xAPI result statement that holds the same information plus info on passing if the content type allows to have specific settings for passing.

If you need some sample code to deal with scores (in WordPress in particular), you might want to have a look at https://github.com/otacke/h5p-user-score

Best,

Oliver 

Hi Oliver, 

Thank you so much for your swift and detailed reply - I installed the plugin you linked to and was thrilled that it was doing what I wanted. However, I looked back at the wp_h5p_results table and saw the same max_score column. My initial thoughts about this column was that it was the user's max score that they had achieved when playing the game, but is it actually just the max available score for the game? Which is what I was after all along?

Thanks for any clarification you can provide on this!

Cheers,
Adam 

otacke's picture

Hi Adam!

The maximum score per user is not scored inside the database, but in the browser's local storage. That's what the sponsor wanted. It happens in https://github.com/otacke/h5p-user-score/blob/master/js/h5p-user-score.js#L59-L67 and you could simply replace it with an AJAX call that sends the value to the server for storing inside the database if that's what you need.

Best,
Oliver

Hey Oliver, 

I'm a bit confused - what does this column refer to in the database?

Thanks again, 
Adam

otacke's picture

It should be the maximum score that was reported by the content type when it was answered by the user. You cannot retrieve it however before at least one person completed the exercise, so I made sure it's retrieved once the content type is opened.

Hello Oliver, 

Your reply was so helpful, and I would love to be able to store the user's best score. So for example they complete the game:

First time: get 9
Second time: get 5

Their best score would be recorded as 9. Currently as I see it their last attempted score is recorded which in the above example would be 5.

I do appreciate you pointing me to the bit of code in which I could make this happen, but it is at the limit of my limited JS knowledge. Please could I ask you to show me what I would need to insert to get this best attempt recorded into the database?

I take it I will need to add a new column in the h5p_results table called best_score and send it the value from this line: scoreMax: xapi.result.score.max|| null,

Anything after that part I get stuck!
Thanks again,
Adam

 

otacke's picture

Hi Adam!

If you have already added a column to the database, all you have to do is

  1. listen to xAPI statements
  2. pick up statements that contain results
  3. use AJAX to pass whatever you need to the server
  4. update your database fields

(1) and (2) are already done in https://github.com/otacke/h5p-user-score/blob/76e4b4e4f65c8b82b602fc05c90ba99b230c1c86/js/h5p-user-score.js#L41 where the xAPI is the parameter holding the result.

Instead of writing the results to the local storage, here you can call the sendAJAX function (3) in https://github.com/otacke/h5p-user-score/blob/76e4b4e4f65c8b82b602fc05c90ba99b230c1c86/js/h5p-user-score.js#L20 similar to how it's done in https://github.com/otacke/h5p-user-score/blob/76e4b4e4f65c8b82b602fc05c90ba99b230c1c86/js/h5p-user-score.js#L192-L203 

(4) You could either amend the AJAX endpoint in https://github.com/otacke/h5p-user-score/blob/master/h5p-user-score.php#L104-L129 to also update your field, so you could use the set_max_score action for AJAX, or you create your own endpoint function (e.g. set_best_score), register it as in https://github.com/otacke/h5p-user-score/blob/master/h5p-user-score.php#L178-L179 and call that one instead.

Cheers,
Oliver