Filter tin-canny-learndash-reporting

tc_tincan_report_query

Filters the Tincan report query arguments before data is retrieved from the database.

add_filter( 'tc_tincan_report_query', $callback, 10, 4 );

Description

Filters the SQL query used to retrieve Tincan report data. Developers can modify the query to customize report results, such as adding custom joins or filtering conditions, before the data is fetched from the database.


Usage

add_filter( 'tc_tincan_report_query', 'your_function_name', 10, 4 );

Parameters

$query (mixed)
This parameter contains the SQL query string being built for retrieving reporting data.
$this (mixed)
This parameter represents the SQL query string being built for retrieving Tincan reporting data.
$columns (mixed)
This parameter represents the specific database table used for storing reporting data.
$where (mixed)
This parameter contains an array of column names that will be selected for the report.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the Tincan report query to exclude specific verbs or users.
 *
 * This example demonstrates how to hook into 'tc_tincan_report_query'
 * to alter the SQL query for Tincan reports. In this case, we're
 * adding a condition to exclude reports where the verb is 'completed'
 * and the user ID is a specific value (e.g., a test user).
 *
 * @param string $query   The SQL query string.
 * @param array  $filters The current filters applied to the report.
 * @param string $table   The name of the reporting table.
 * @param array  $columns The columns being selected in the query.
 * @param string $where   The WHERE clause of the query.
 * @return string The modified SQL query string.
 */
add_filter( 'tc_tincan_report_query', function( $query, $filters, $table, $columns, $where ) {

    // Example: Exclude reports with the verb 'completed' for a specific user ID.
    // This might be useful for excluding automated test results or specific scenarios.
    $excluded_verb = 'completed';
    $excluded_user_id = 123; // Replace with an actual user ID if needed.

    // Check if the excluded verb and user ID are present in the filters.
    // This is a simplified check. A more robust solution might involve
    // checking the actual query string or how the filters are structured.
    if ( isset( $filters['verb'] ) && $filters['verb'] === $excluded_verb &&
         isset( $filters['user_id'] ) && $filters['user_id'] === (string) $excluded_user_id ) {

        // Append a condition to the WHERE clause to exclude the specific record.
        // We need to be careful about how we append to the WHERE clause,
        // ensuring we don't break existing conditions.
        if ( ! empty( $where ) ) {
            $where .= ' AND ';
        } else {
            $where = ' WHERE ';
        }
        $where .= " {$table}.verb != '" . esc_sql( $excluded_verb ) . "' OR {$table}.user_id != " . intval( $excluded_user_id );

        // Rebuild the query with the modified WHERE clause.
        // This assumes the $query variable is constructible by piecing together
        // the parts. In a real scenario, you might need to parse the $query
        // string more intelligently or have access to the components used to build it.
        // For this example, we'll assume a common structure.
        $query_parts = explode( ' WHERE ', $query );
        if ( count( $query_parts ) > 1 ) {
            $query = $query_parts[0] . $where;
        } else {
            // If there was no WHERE clause originally, just add it.
            $query .= $where;
        }
    }

    return $query;
}, 10, 5 );
?>

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/Admin.php:1207

case when reporting.verb = 'passed' then reporting.user_id else reporting.user_id end
		), (
		    case when reporting.verb = 'passed' then reporting.module else reporting.module end
		), (
		    case when reporting.verb = 'passed' then reporting.course_id else reporting.course_id end
		) ";

		$query = apply_filters( 'tc_tincan_report_query', $query, $this->filters, self::TABLE_REPORTING, $columns, $where );

		return $query;
	}

	/**
	 * Check the Current User is a Group Leader and Return Assigned Groups Query String
	 *

Scroll to Top