XAPI iframe query

Hello all,

I have a  Wordpress site hosting H5P but I want to embed content activities on a separate HTML site on the same domain and I want to be able to capture the score and whether a user got 100% on the activity to then either insert into a SQL database against a user or create a PHP session with the information.

I have tried using info from this previous post https://h5p.org/node/56148. I took the same 2 below scripts:

H5P.externalDispatcher.on('xAPI', function (event) {
  window.parent.postMessage(event.data.statement, '*');
});
window.addEventListener('message', function receiveMessage(event) {
  if (event.data.result === undefined) {
    return; // Only handle messages with result
  }
 
  console.log(event.data.actor.name + ' just scored ' + (event.data.result.score.scaled * 100) + ' %');
} , false);

and this works in my console, but if i put both of these at the bottom of my page containing the iframe then nothing seems to happen.

Can anyone advise how I would use the similar code to work on a page that has the iframe content embedeed to then create a PHP session with the updated progress within it?

Many thanks in advance.

otacke's picture

Hi ojw!

if i put both of these at the bottom of my page containing the iframe then nothing seems to happen

As mentioned in the post that you referenced: The first script is supposed to be included inside the iframe (added to the H5P content on the site that's hosting H5P), the second script is supposed to be included outside the iframe (the site the content is embedded into).

Cheers,
Oliver

Thank you, how would I include inside iFrame, what page on H5P or wordpress would I add it on?

otacke's picture

You should use the alter_scripts hook as described at https://h5p.org/wordpress-customization and in the sample code provided in the extra plugin that's referenced there. You should not hard code it into some of H5P's files if you don't know what you are doing.

Do you take on private work to be able to assist me with what I need to acheive please?

otacke's picture

Hi ojw!

Hiring me for such a tiny job is in no way profitable for me. Too much bureaucratic overhead. Sorry.

But as on stack overflow, I am willing to help for free if I see that people are putting effort into something and are willing to learn instead of just copying and pasting code snippets.

Best,

Oliver