• Resolved alain14773

    (@alain14773)


    Hello, I would like h5pxapikatchu to only save the results for H5p exercises for users with a WordPress account, and for all anonymous users to keep nothing saved. Is this possible? Thanks. Alain

    • This topic was modified 1 year ago by alain14773.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author otacke

    (@otacke)

    There’s no such option built in, no. But you should be able to achieve this with the plugin’s hooks/filters. Please compare https://github.com/otacke/h5pxapikatchu/pull/14

    Thread Starter alain14773

    (@alain14773)

    Hello, Thanks for the reply. Here’s how I solved my problem. I changed the insert_data() function in the H5PXAPIKATCHU.php file.

    function insert_data() {
    // Add hook ‘h5pxapikatchu_insert_data’
    do_action( ‘h5pxapikatchu_insert_data’ );

    global $wpdb;
    
    $xapi = $_REQUEST['xapi'];
    
    $xapidata = new XAPIDATA( $xapi );
    
    // Get actor array from XAPIDATA
    $actor_array = $xapidata->get_actor(); 
    
    $actor_id_to_check = ''; 
    
    // Extract the relevant actor identifier from the array
    if (is_array($actor_array)) {
        if (isset($actor_array['inverseFunctionalIdentifier'])) {
            $actor_id_to_check = $actor_array['inverseFunctionalIdentifier'];
        } elseif (isset($actor_array['mbox'])) {
            $actor_id_to_check = $actor_array['mbox'];
        } elseif (isset($actor_array['account']['name']) && isset($actor_array['account']['homePage'])) {
            $actor_id_to_check = $actor_array['account']['name'];
        } elseif (isset($actor_array['openid'])) {
            $actor_id_to_check = $actor_array['openid'];
        }
    }
    
    // Block statements if actor ID is empty or doesn't start with "email: mailto:"
    if (empty($actor_id_to_check) || strpos($actor_id_to_check, 'email: mailto:') !== 0) {
        wp_die();
    }
    
    $actor = $xapidata->get_actor();
    $actor['wpUserId'] = get_current_user_id();
    $actor['wpUserId'] = ( 0 === $actor['wpUserId'] ) ? null : $actor['wpUserId'];
    $actor = filter_insert_data_actor( $actor );
    
    $verb = $xapidata->get_verb();
    $verb = filter_insert_data_verb( $verb );
    
    $object = $xapidata->get_object();
    preg_match( '/[&|?]id=([0-9]+)/', $object['id'], $matches );
    $object['h5pContentId'] = ( sizeof( $matches ) > 0 ) ? $matches[1] : null;
    preg_match( '/[&|?]subContentId=([0-9a-f-]{36})/', $object['id'], $matches );
    $object['h5pSubContentId'] = ( sizeof( $matches ) > 0 ) ? $matches[1] : null;
    $object = filter_insert_data_object( $object );
    
    $result = $xapidata->get_result();
    $result = filter_insert_data_result( $result );
    
    if ( Options::store_complete_xapi() ) {
        $xapi = str_replace( '\"', '"', $xapi );
        $xapi = str_replace( "\'", "'", $xapi );
    } else {
        $xapi = null;
    }
    $xapi = filter_insert_data_xapi( $xapi );
    
    // Add hook 'h5pxapikatchu_insert_data_pre_database'
    do_action( 'h5pxapikatchu_insert_data_pre_database' );
    
    $main_id = Database::insert_data( $actor, $verb, $object, $result, $xapi );
    
    // Add hook 'h5pxapikatchu_insert_data_post_database'
    do_action( 'h5pxapikatchu_insert_data_post_database', $main_id );
    
    wp_die();

    }

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Insert and Save only for registered users’ is closed to new replies.