Listeners and callbacks

Created on 3 June 2024, 23 days ago
Updated 5 June 2024, 21 days ago

Support request

I am creating an app using Anu LMS, its a small opinionated LMS in Drupal with a semi decoupled React interface.
I created a lesson activity type using the H5P module as you can see in the screen shot provided.
I am digging in to the code, but there are two issues I have at the moment and would appreciate some pointers to docs or code.

1. Initialize method
In Anu LMS, if I use the H5P component in it's JS multi-step interface (it could be the same with accordions or tabs), and it is not on the first step (is hidden on page load) it does not get initialized. Is there a JS function/method I can call to initialize it manually on step change ?

2. Score event listener
In Anu LMS (in the screen shot) I want to disable the Next step button when you are on an H5P lesson activity and re-enable is when you complete the activity. Is there a JS event I can listen for in my custom JS to detect when the H5P component has been completed ? or when the scores change.

When I've got the Anu LMS H5P Component module done, I'll publish it on Drupal, I think they work pretty well together :) and thanks for the module :)

πŸ’¬ Support request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom 8bitplateau

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @8bitplateau
  • πŸ‡¬πŸ‡§United Kingdom 8bitplateau
  • πŸ‡¬πŸ‡§United Kingdom 8bitplateau

    So, in answer to the first question H5P is in the window object (yay) so I can init() it from wherever.
    In react I am doing it as follows, listening for history change. It's not quite there yet but it's a start and now I know how to init().
    The key is H5P.init('.h5p-iframe-wrapper');

    const CustomParagraph = (props) => {
    
      let history = useHistory();
    
      useEffect(() => {
        try {
          let H5P = window.H5P;
          setTimeout(() => {
            H5P.init('.h5p-iframe-wrapper');
          }, "10");
        }
        catch (err) {
          console.error('Missing H5P.');
        }
      }, [history]);
    
      const StyledBox = withStyles((theme) => ({
        root: {
          marginBottom: theme.spacing(4),
        },
      }))(Box);
    
      return (
        <StyledBox>
          <LessonGrid>
            <div className='h5p-iframe-wrapper'>
              <iframe
                id={`h5p-iframe-${props.componentId}`}
                className='h5p-iframe'
                data-content-id={props.componentId}
                frameBorder='0'
                scrolling='no'
                lang='en'
                title={`Component frame ${props.componentId}`}>
              </iframe>
            </div>
          </LessonGrid>
        </StyledBox>
      );
    };
  • πŸ‡¬πŸ‡§United Kingdom 8bitplateau

    And in answer to the seconds, is this ? using xAPI

            H5P.externalDispatcher.on('xAPI', function(event){
              const max = event.getMaxScore();
              const score = event.getScore();
              const verb = event.getVerb();
              if (score === max && max > 0){
                console.error(verb);
                console.error('Complete');
              }
            });
    

    Thank you Docs - https://h5p.org/documentation/developers/x-api-event#setScoredResult

Production build 0.69.0 2024