Filter tin-canny-learndash-reporting

h5p-xapi-pre-save

Filters the xAPI statement before it is saved to the database, allowing for modifications.

add_filter( 'h5p-xapi-pre-save', $callback, 10, 1 );

Description

Allows modification of an xAPI statement before it's saved to the database. Developers can filter the statement array to alter or add data, ensuring compliance or custom integrations with learning record stores. This hook fires just before the statement is processed for saving.


Usage

add_filter( 'h5p-xapi-pre-save', 'your_function_name', 10, 1 );

Parameters

$statement (mixed)
The `$statement` parameter contains the xAPI statement object that is about to be saved.

Return Value

The filtered value.


Examples

add_filter( 'h5p-xapi-pre-save', 'my_h5p_xapi_modify_statement', 10, 1 );

/**
 * Example filter to modify H5P xAPI statements before saving.
 *
 * This function demonstrates how to access and potentially alter the xAPI statement
 * data received from H5P. In this example, we'll add a custom extension if one
 * doesn't already exist, or modify an existing one.
 *
 * @param array $statement The xAPI statement data.
 * @return array The modified xAPI statement data.
 */
function my_h5p_xapi_modify_statement( $statement ) {
	// Ensure $statement is an array, as expected.
	if ( ! is_array( $statement ) ) {
		return $statement;
	}

	// Define our custom extension key and value.
	$custom_extension_key = 'http://example.com/h5p/extensions/user-tracking';
	$custom_extension_value = array(
		'user_id' => get_current_user_id(),
		'timestamp' => time(),
	);

	// Ensure the 'context' and 'extensions' keys exist.
	if ( ! isset( $statement['context'] ) || ! is_array( $statement['context'] ) ) {
		$statement['context'] = array();
	}
	if ( ! isset( $statement['context']['extensions'] ) || ! is_array( $statement['context']['extensions'] ) ) {
		$statement['context']['extensions'] = array();
	}

	// Add or update our custom extension.
	// We're adding a simple user ID and timestamp for demonstration.
	$statement['context']['extensions'][$custom_extension_key] = $custom_extension_value;

	// Example of potentially removing an unwanted extension (if it exists).
	// For instance, if you want to ensure no sensitive data is sent in a specific extension.
	$sensitive_extension_key = 'http://example.com/h5p/extensions/sensitive-data';
	if ( isset( $statement['context']['extensions'][$sensitive_extension_key] ) ) {
		unset( $statement['context']['extensions'][$sensitive_extension_key] );
	}

	// Always return the (potentially modified) statement.
	return $statement;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/uncanny-tincan/classes/Init.php:384
src/h5p-xapi/process-xapi-statement.php:60

public function process_xapi_statement() {
		// Get Request Type.
		$request_type = ultc_current_request_type();

		// Get Statement.
		$statement = ultc_filter_has_var( 'statement', $request_type ) ? ultc_filter_input( 'statement', $request_type ) : array();
		$statement = ! empty( $statement ) && $this->is_json( $statement ) ? json_decode( stripslashes( $statement ), true ) : array();

		if ( isset( $statement['context'] ) && isset( $statement['context']['extensions'] ) && ! $statement['context']['extensions'] ) {
			unset( $statement['context']['extensions'] );
		}

		if ( has_filter( 'h5p-xapi-pre-save' ) ) {
			$statement = apply_filters( 'h5p-xapi-pre-save', $statement ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

			if ( ! $statement ) {
				echo wp_json_encode(
					array(
						'ok'      => 1,
						'message' => null,
					)
				);
				exit;
			}
		}

		$tin_can_h5p = new UCTINCANTinCanRequestH5P( $statement );
		$res         = $tin_can_h5p->get_completion();
		if ( $res ) {
			$response = array(
				'ok'      => 1,
				'message' => 'true',
				'code'    => 200,
			);
		} else {
			$response = array(
				'ok'      => 1,
				'message' => 'false',
				'code'    => 200,
			);
		}

		echo wp_json_encode( $response );
		exit();
	}

Scroll to Top