Filter tin-canny-learndash-reporting

uo_tincanny_reporting_questions_get_avg_time

Filters the average time data for reporting questions before it's retrieved from the database.

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

Description

Filters the calculated average time spent on a specific quiz question. Developers can modify the average time, or the query used to calculate it. This hook fires before the average time is returned by the `get_avg_time` function. The `$table` parameter is duplicated and its description is missing.


Usage

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

Parameters

$table (mixed)
This parameter refers to the database table name for quiz statistics.
$question_id (mixed)
This parameter is used to specify the database table that stores quiz statistics.
$table (mixed)
This parameter represents the ID of the specific question for which the average time is being calculated.
$pro_quiz_id (mixed)
This parameter likely contains a reference to the `quiz_statistic` table, which is used to store quiz statistics.

Return Value

The filtered value.


Examples

/**
 * Modifies the SQL query to include a condition to only consider questions answered within a specific time frame.
 * This can be useful for analyzing performance during peak usage or specific testing periods.
 *
 * @param string $sql The original SQL query string.
 * @param int    $question_id The ID of the question being queried.
 * @param string $table The name of the 'quiz_statistic' database table.
 * @param int    $pro_quiz_id The ID of the pro quiz.
 *
 * @return string The modified SQL query string.
 */
add_filter(
	'uo_tincanny_reporting_questions_get_avg_time',
	function ( $sql, $question_id, $table, $pro_quiz_id ) {
		// Assuming $date_ranges is defined in the scope of the original function
		// and contains a WHERE clause like "AND DATE(ref.statistic_date) BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD'"
		// For demonstration, let's assume we want to filter for questions answered in the last 7 days.
		$seven_days_ago = date( 'Y-m-d', strtotime( '-7 days' ) );
		$today          = date( 'Y-m-d' );

		// Modify the SQL query to add a date range condition.
		// It's crucial to handle cases where $date_ranges might already contain a WHERE clause.
		// A more robust solution would involve parsing the existing $date_ranges.
		// For this example, we'll append it directly, assuming it's structured correctly.
		$date_condition = " AND DATE(ref.statistic_date) BETWEEN '$seven_days_ago' AND '$today'";

		// Find the correct insertion point for the date condition.
		// This is a simplified approach; a real-world scenario might require more sophisticated parsing.
		$modified_sql = str_replace(
			"WHERE s.question_id = $question_id",
			"WHERE s.question_id = $question_id $date_condition",
			$sql
		);

		return $modified_sql;
	},
	10,
	4 // Accepted arguments: $sql, $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:770

private static function get_avg_time( $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_avg_time',
			"SELECT
    AVG(s.question_time)
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 number_format( $wpdb->get_var( $qry ), 2 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}

Scroll to Top