Filter tin-canny-learndash-reporting

tincanny_process_content

Filters the processed content before it is displayed, allowing for modification of the output.

add_filter( 'tincanny_process_content', $callback, 10, 1 );

Description

Filters the content being processed by the Tin Can API. Developers can modify the content before it's sent to the LRS, allowing for custom data manipulation or enrichment. Use with caution to avoid disrupting the core Tin Can API workflow.


Usage

add_filter( 'tincanny_process_content', 'your_function_name', 10, 1 );

Parameters

$content (mixed)
This parameter is a boolean value that likely controls whether the content should be processed or not.

Return Value

The filtered value.


Examples

add_filter( 'tincanny_process_content', 'my_custom_tincanny_process_content', 10, 2 );

/**
 * Example of filtering the 'tincanny_process_content' hook.
 * This function conditionally prevents processing of Tincanny content
 * based on specific conditions.
 *
 * @param bool $process_content Whether to process the Tincanny content. Default is true.
 * @param array $data An array containing 'content' and 'client' details.
 *                    - 'content': The decoded Tincanny content.
 *                    - 'client': The client application sending the Tincanny data.
 *
 * @return bool Returns false to prevent processing, or true to allow processing.
 */
function my_custom_tincanny_process_content( $process_content, $data ) {
	// Ensure we have the expected data structure.
	if ( ! isset( $data['content'] ) || ! isset( $data['client'] ) ) {
		return $process_content; // Return original value if data is incomplete.
	}

	$content = $data['content'];
	$client  = $data['client'];

	// Example: Prevent processing if the client is "SomeSpecificApp"
	// and the content indicates a "training_module" type.
	if ( 'SomeSpecificApp' === $client && isset( $content['type'] ) && 'training_module' === $content['type'] ) {
		// Log this event for debugging purposes if needed.
		// error_log( 'Tincanny content processing skipped for SomeSpecificApp training module.' );
		return false; // Prevent processing.
	}

	// Example: Only process if the content contains a 'completionStatement' key.
	if ( ! isset( $content['completionStatement'] ) ) {
		// error_log( 'Tincanny content processing skipped: completionStatement missing.' );
		return false; // Prevent processing.
	}

	// If none of the above conditions are met, allow processing.
	return $process_content;
}

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/Server.php:81

private function process_request() {
		//error_reporting( 0 );

		do_action( 'tincanny_before_process_request' );

		$this->modify_header();
		$content = $this->get_decoded();

		// Get Resume
		$this->by_request( $content );

		// Get Client and Contents
		$client = $this->get_client();

		$is_app = strstr( $_SERVER['HTTP_USER_AGENT'], 'AdobeAIR/' );

		// Check Nonce
		if ( 'H5P' !== $client && ! $this->get_nonce() && ! $is_app ) {
			header( 'Status: 403 Forbidden' );
			header( 'HTTP/1.1 403 Forbidden' );
			exit();
		}

		if ( isset( $content['content'] ) && ! is_array( $content['content'] ) ) {
			$content = json_decode( $content['content'], true );
		}

		// Should we process this request?
		$tincanny_process_content = apply_filters( 'tincanny_process_content', true, [
			'content' => $content,
			'client'  => $client
		] );

		if ( true !== $tincanny_process_content ) {
			$this->print_guid( false );
			return;
		}

		// Get the First Key
		$keys      = array_keys( $content );
		$first_key = array_shift( $keys );

		// Multiple Request
		if ( is_numeric( $first_key ) || 0 === $first_key ) {
			for ( $i = ( count( $content ) - 1 ); $i >= 0; $i -- ) {
				$completion = $this->create_tincan_record( $client, $content[ $i ] );
			}

			// Single Request
		} else {
			$completion = $this->create_tincan_record( $client, $content );
		}

		$this->print_guid( $completion );
	}


Scroll to Top