tc_xapi_report_query
Filters the xAPI report query before it's executed, allowing modifications to the SQL query.
add_filter( 'tc_xapi_report_query', $callback, 10, 4 );
Description
Filters the XAPi report query. Developers can modify the SQL query for XAPi reports, influencing which data is retrieved. This hook fires just before the query is executed, allowing for custom filtering, joining, or column selection for advanced reporting needs.
Usage
add_filter( 'tc_xapi_report_query', 'your_function_name', 10, 4 );
Parameters
-
$query(mixed) - This parameter contains the SQL query string that will be used to retrieve xAPI reporting data from the database.
-
$this(mixed) - This parameter contains the SQL query string being built to fetch xAPI reporting data.
-
$columns(mixed) - This parameter represents the name of the quiz table used in the database query.
-
$where(mixed) - This parameter likely contains an array or string representing the columns to be selected in the SQL query.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the tc_xapi_report_query hook to include
* only reports for users who have completed a specific course.
*
* @param string $query The current SQL query string.
* @param array $filters An array of current filter parameters.
* @param string $table The name of the reporting table.
* @param array $columns An array of columns to select.
* @param string $where The WHERE clause for the query.
* @return string The modified SQL query string.
*/
add_filter( 'tc_xapi_report_query', function( $query, $filters, $table, $columns, $where ) {
// Assume we have a specific course ID we want to filter by.
$target_course_id = 123; // Replace with the actual course ID
// Check if the target course ID is provided in the filters
if ( ! isset( $filters['course_id'] ) || $filters['course_id'] != $target_course_id ) {
// If the target course is not already filtered, append a condition
// to the existing WHERE clause to ensure we only get reports for that course.
// This assumes the 'course_id' column exists in the reporting table.
$course_join_alias = 'course'; // Based on the provided source context
// Construct the new WHERE clause part
$new_where_part = " AND {$course_join_alias}.ID = " . intval( $target_course_id );
// Append the new condition to the existing WHERE clause.
// We need to be careful here not to break existing logic in $where.
// A more robust solution might involve parsing and merging WHERE clauses.
// For this example, we'll assume $where is a simple string or can be appended to.
// In a real-world scenario, you'd want to inspect $where more carefully.
if ( ! empty( $where ) ) {
$where .= $new_where_part;
} else {
$where = " WHERE reporting.ID > 0 " . $new_where_part; // Assuming 'reporting.ID > 0' is a safe base if $where is empty
}
// Reconstruct the query with the modified WHERE clause.
// This is a simplified reconstruction. A more complex query might need
// to adjust the JOIN clauses as well if they are dependent on the WHERE clause.
// We are assuming the FROM and JOIN clauses remain the same.
// We're also assuming $columns is an array of strings and $table is the base table name.
global $wpdb;
$query = "
SELECT " . implode( ', ', $columns ) . "
FROM {$wpdb->prefix}" . $table . " AS reporting
LEFT OUTER JOIN $wpdb->posts xgrouping ON xgrouping.ID = reporting.group_id
LEFT OUTER JOIN $wpdb->users user ON user.ID = reporting.user_id
LEFT OUTER JOIN $wpdb->posts course ON course.ID = reporting.course_id
LEFT OUTER JOIN $wpdb->posts lesson ON lesson.ID = reporting.lesson_id
WHERE {$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:1327
FROM {$wpdb->prefix}" . self::TABLE_QUIZ . " AS reporting
LEFT OUTER JOIN $wpdb->posts xgrouping ON xgrouping.ID = reporting.group_id
LEFT OUTER JOIN $wpdb->users user ON user.ID = reporting.user_id
LEFT OUTER JOIN $wpdb->posts course ON course.ID = reporting.course_id
LEFT OUTER JOIN $wpdb->posts lesson ON lesson.ID = reporting.lesson_id
WHERE $where";
return apply_filters( 'tc_xapi_report_query', $query, $this->filters, self::TABLE_QUIZ, $columns, $where );
}
/**
* Return List of Module for Course
*
* @access private
*