uo_tincanny_reporting_questions_get_times_question_asked
Filters the times a question was asked for reporting within the Tincanny reporting system.
add_filter( 'uo_tincanny_reporting_questions_get_times_question_asked', $callback, 10, 1 );
Description
Fires when retrieving the count of how many times a specific question was asked within a quiz. Developers can filter the SQL query or modify the resulting count. This hook is essential for customizing how question usage statistics are calculated and displayed.
Usage
add_filter( 'uo_tincanny_reporting_questions_get_times_question_asked', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of how to filter the 'uo_tincanny_reporting_questions_get_times_question_asked' hook.
* This example adds an extra condition to the SQL query to only count instances
* where the question was answered correctly.
*
* @param string $sql_query The original SQL query string.
* @param int $question_id The ID of the question being queried.
* @param string $quiz_statistic_table The name of the quiz_statistic database table.
* @param int $pro_quiz_id The ID of the Uncanny Toolkit Pro quiz.
*
* @return string The modified SQL query string.
*/
add_filter( 'uo_tincanny_reporting_questions_get_times_question_asked', function( $sql_query, $question_id, $quiz_statistic_table, $pro_quiz_id ) {
// Assuming there's another table related to question answers or results
// that can indicate correctness. Let's pretend it's 'quiz_answers' and
// it has a 'correct' column (1 for correct, 0 for incorrect).
// This is a hypothetical table name and column for demonstration.
// In a real scenario, you'd inspect the Uncanny Toolkit Pro database schema
// or documentation to find the correct table and column.
// First, let's get the table name for quiz answers.
// This might involve another LDLMS_DB call if available, or you might know it.
// For this example, let's assume a hypothetical function or a known table name.
// If LDLMS_DB doesn't provide it directly, you'd use $wpdb->prefix . 'your_answers_table_name'
// Let's assume a hypothetical function for demonstration:
$quiz_answers_table = LDLMS_DB::get_table_name( 'quiz_answers' ); // Hypothetical
// We need to join this new table to check for correctness.
// The join condition would depend on how quiz_statistic and quiz_answers are related.
// Assuming they share a common ID or can be joined via the statistic_ref_id.
// Let's assume quiz_answers has a 'statistic_ref_id' column.
// Find the WHERE clause in the original query and insert our JOIN and additional WHERE condition.
$where_pos = strripos( $sql_query, 'WHERE' );
if ( $where_pos !== false ) {
// Extract the existing WHERE clause content
$existing_where_content = substr( $sql_query, $where_pos + 5 ); // +5 to skip 'WHERE'
$sql_query = substr( $sql_query, 0, $where_pos + 5 ); // Keep up to 'WHERE'
// Add the new JOIN
$sql_query .= "
JOIN {$quiz_answers_table} qa
ON s.statistic_ref_id = qa.statistic_ref_id AND qa.question_id = {$question_id} AND qa.correct = 1"; // Hypothetical join condition and correctness check
// Append the original WHERE content, adjusted if necessary
$sql_query .= " AND {$existing_where_content}";
} else {
// If there's no WHERE clause yet, simply add our conditions
$sql_query .= " AND qa.question_id = {$question_id} AND qa.correct = 1"; // Hypothetical correctness check
}
// Append the JOIN to the query if it wasn't already there
// This is a fallback in case the string manipulation above was too simple.
// A more robust approach would involve parsing the SQL.
if ( strpos( $sql_query, "{$quiz_answers_table} qa ON s.statistic_ref_id = qa.statistic_ref_id" ) === false ) {
// This is a simplified approach. In a real scenario, you'd want to be more careful
// about where you insert the JOIN.
$sql_query = str_replace( "FROM {$quiz_statistic_table} s", "FROM {$quiz_statistic_table} s JOIN {$quiz_answers_table} qa ON s.statistic_ref_id = qa.statistic_ref_id", $sql_query );
$sql_query = str_replace( "AND ref.quiz_id = {$pro_quiz_id}", "AND ref.quiz_id = {$pro_quiz_id} AND qa.question_id = {$question_id} AND qa.correct = 1", $sql_query );
}
return $sql_query;
}, 10, 4 ); // Priority 10, accepts 4 arguments
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-question-analysis-report/question-analysis-report.php:708
private static function get_times_question_asked( $question_id ) {
global $wpdb;
$table = LDLMS_DB::get_table_name( 'quiz_statistic' );
$table_ref = LDLMS_DB::get_table_name( 'quiz_statistic_ref' );
$pro_quiz_id = self::$pro_quiz_id;
$date_ranges = self::validate_date_range();
$qry = apply_filters(
'uo_tincanny_reporting_questions_get_times_question_asked',
"SELECT COUNT(*)
FROM $table s
JOIN $table_ref ref
ON s.statistic_ref_id = ref.statistic_ref_id AND ref.quiz_id = $pro_quiz_id
WHERE s.question_id = $question_id
$date_ranges",
$question_id,
$table,
$pro_quiz_id
);
return $wpdb->get_var( $qry ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}