Filter tin-canny-learndash-reporting

uo_tincanny_reporting_get_state_fallback_query

Filters the fallback query used when retrieving Tincanny reporting state by module ID.

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

Description

Allows modification of the fallback query used when retrieving a learner's state for a specific module. Developers can alter the query arguments, such as the user ID or module ID, to customize the data fetched or to implement alternative retrieval logic. This hook fires before the database query is executed.


Usage

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

Parameters

$module_id_value (mixed)
This parameter represents the initial state query or data being considered by the filter.

Return Value

The filtered value.


Examples

/**
 * Filters the fallback query for retrieving a user's state.
 *
 * This function allows developers to modify the fallback SQL query used when
 * a user's state cannot be found with the most specific query (including
 * course and lesson IDs). It can be used to adjust the query to retrieve
 * state data even if course and lesson context is missing.
 *
 * @param bool   $fallback_query   The default value, which is true, indicating that a fallback query should be executed.
 *                                 Setting this to false will prevent the fallback query from running.
 * @param string $module_id_value  The ID of the module or lesson the user is currently interacting with.
 *
 * @return mixed The modified fallback query condition or a false value to disable the fallback.
 */
add_filter(
	'uo_tincanny_reporting_get_state_fallback_query',
	function( $fallback_query, $module_id_value ) {
		// If the initial query (with course and lesson IDs) failed,
		// this filter is called. By default, it tries a simpler query
		// that doesn't include course and lesson IDs.
		//
		// This example shows how to further modify the fallback behavior.
		// For instance, if you wanted to prevent the fallback query from
		// running under specific conditions, you could do so here.

		// Example: If the module ID value is empty, we might not want to
		// attempt a fallback query as it's unlikely to yield meaningful results.
		if ( empty( $module_id_value ) ) {
			return false; // Disable the fallback query if module_id_value is empty.
		}

		// In most cases, you'd likely want to keep the default fallback logic.
		// If you don't want to change anything, simply return the original value.
		return $fallback_query;
	},
	10, // Priority: default is 10
	2   // Accepted arguments: the filter receives $fallback_query and $module_id_value
);

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

public function get_state( $url, $state_id ) {
		global $wpdb;

		if ( ! $this->set_user_id() ) {
			return false;
		}

		// Sync table columns
		$this->add_course_lesson_columns();
		// Get course and lesson id from request
		list( $course_id, $lesson_id ) = $this->fetch_course_lesson_ids();

		$module_id       = $this->get_slide_id_from_url( $url );
		$table_name      = $wpdb->prefix . self::TABLE_RESUME;
		$module_id_value = isset( $module_id[1] ) ? $module_id[1] : '';

		// SELECT with course and lesson ids
		$query = $wpdb->prepare(
			"SELECT `value` FROM {$table_name}
				WHERE
					`user_id`   = %s AND
					`module_id` = %s AND
					`course_id` = %s AND
					`lesson_id` = %s AND
					`state`     = %s
			LIMIT 1
			",
			self::$user_id,
			$module_id_value,
			$course_id,
			$lesson_id,
			$state_id
		);

		$return = $wpdb->get_var( $query );

		if ( ! empty( $return ) ) {
			if ( 'suspend_data' === $state_id ) {
				$return = apply_filters( 'uo_tincanny_reporting_sanitize_suspend_data', $return, $module_id_value, 'GET' );
			}
			return $return;
		}

		$fallback_query = apply_filters( 'uo_tincanny_reporting_get_state_fallback_query', true, $module_id_value );

		if ( true === $fallback_query ) {
			$query = $wpdb->prepare(
				"SELECT `value` FROM {$table_name}
					WHERE
						`user_id`   = %s AND
						`module_id` = %s AND
						`state`     = %s
				LIMIT 1
				",
				self::$user_id,
				$module_id_value,
				$state_id
			);

			$return = $wpdb->get_var( $query );

			if ( ! empty( $return ) ) {
				if ( 'suspend_data' === $state_id ) {
					$return = apply_filters( 'uo_tincanny_reporting_sanitize_suspend_data', $return, $module_id_value, 'GET' );
				}
				return $return;
			}
		}

		return null;
	}


Scroll to Top