Filter tin-canny-learndash-reporting

uo_tincanny_reporting_questions_get_answered_correctly

Filters the query arguments for retrieving correctly answered quiz questions.

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

Description

Filters the SQL query used to count how many times a specific question was answered correctly. Developers can modify this query to alter the logic for determining correct answers or to include/exclude specific data points in the count. This hook fires before the database query is executed.


Usage

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

Parameters

$table (mixed)
This parameter represents the name of the database table that stores quiz statistics.
$question_id (mixed)
This parameter represents the name of the database table used to store quiz statistics.
$table (mixed)
This parameter contains the ID of the question for which you want to retrieve answered correctly statistics.
$pro_quiz_id (mixed)
This parameter is a reference to the name of the database table that stores quiz statistic references.

Return Value

The filtered value.


Examples

/**
 * Filters the SQL query to count how many times a specific question was answered correctly.
 *
 * This example adds a condition to only include results from quizzes taken within the last 30 days.
 *
 * @param string $qry          The original SQL query string.
 * @param int    $question_id  The ID of the question.
 * @param string $table        The name of the quiz statistic table.
 * @param int    $pro_quiz_id  The ID of the professional quiz.
 *
 * @return string The modified SQL query string.
 */
add_filter(
	'uo_tincanny_reporting_questions_get_answered_correctly',
	function ( $qry, $question_id, $table, $pro_quiz_id ) {
		global $wpdb;
		$thirty_days_ago = date( 'Y-m-d H:i:s', strtotime( '-30 days' ) );

		// Appends a condition to the WHERE clause to filter by date.
		// Note: In a real-world scenario, ensure proper sanitization and escaping if $thirty_days_ago
		// were dynamic based on user input or other variables. For this specific example, it's static.
		$qry .= $wpdb->prepare( " AND ref.quiz_date > '%s'", $thirty_days_ago );

		return $qry;
	},
	10,
	4 // The filter accepts 4 arguments: $qry, $question_id, $table, $pro_quiz_id
);

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:679

private static function get_answered_correctly( $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_answered_correctly',
			"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
  AND s.correct_count = 1 $date_ranges",
			$question_id,
			$table,
			$pro_quiz_id
		);

		return $wpdb->get_var( $qry ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}


Scroll to Top