Filter tin-canny-learndash-reporting

uo_tincanny_reporting_questions_get_times_this_answer_selected

Filters the data used to determine how many times a specific answer has been selected for a given question and quiz.

add_filter( 'uo_tincanny_reporting_questions_get_times_this_answer_selected', $callback, 10, 5 );

Description

Filters the SQL query used to retrieve the count of times a specific answer was selected for a quiz question. Developers can modify the query to change how answer selection counts are retrieved, offering flexibility in reporting and analysis.


Usage

add_filter( 'uo_tincanny_reporting_questions_get_times_this_answer_selected', 'your_function_name', 10, 5 );

Parameters

$table (mixed)
This parameter contains the database table name for quiz statistics.
$question_id (mixed)
This parameter holds the name of the database table used for storing quiz statistics.
$question_position (mixed)
This parameter contains the unique identifier for the question whose answer selection counts are being retrieved.
$table (mixed)
This parameter represents the position of the question within the quiz.
$pro_quiz_id (mixed)
This parameter provides the name of the database table used to store quiz statistics.

Return Value

The filtered value.


Examples

/**
 * Modify the SQL query to also count incorrect answers for a specific question and answer.
 *
 * @param string $query The original SQL query.
 * @param int    $question_id The ID of the question.
 * @param string $question_position The position of the answer (e.g., 'a', 'b').
 * @param string $table The name of the quiz_statistic table.
 * @param int    $pro_quiz_id The ID of the pro quiz.
 *
 * @return string The modified SQL query.
 */
add_filter( 'uo_tincanny_reporting_questions_get_times_this_answer_selected', function( $query, $question_id, $question_position, $table, $pro_quiz_id ) {
	// Example: Let's say we want to also include the count of when an answer was selected but not correct,
	// even if it wasn't the *only* incorrect attempt. This could be useful for understanding partial correctness.

	// The original query likely targets cases where the answer was *exactly* the selected option and incorrect.
	// We'll modify the WHERE clause to be more inclusive.

	// Assuming the original query looks something like this:
	// "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.answer_data LIKE '{$question_position}' AND s.incorrect_count = 1 {$date_ranges}"

	// We'll add a condition to include cases where incorrect_count is NOT 0, and the answer_data matches.
	// Note: The exact structure of `answer_data` will depend on how Uncanny Automator stores it.
	// We're assuming it's a string that can be compared using LIKE for simplicity here.

	// Parse the existing query to find and modify the WHERE clause.
	$where_pos = strpos( $query, 'WHERE ' );
	if ( $where_pos !== false ) {
		$where_clause = substr( $query, $where_pos + 6 ); // Get the WHERE clause content

		// Find the `AND s.incorrect_count = 1` part and replace it or add to it.
		$incorrect_count_pos = strpos( $where_clause, 'AND s.incorrect_count = 1' );

		if ( $incorrect_count_pos !== false ) {
			// Replace 'AND s.incorrect_count = 1' with 'AND s.incorrect_count != 0' to include all incorrect attempts.
			// Or, if we want to be more specific about how the answer was selected, we might need to adjust 'answer_data' handling.
			// For this example, let's assume we want to count any selection of this answer, regardless of correctness,
			// but only if it contributes to an incorrect attempt or is an attempt itself.
			// A simpler modification: include cases where `incorrect_count` is not 0.
			$modified_where_clause = str_replace( 'AND s.incorrect_count = 1', 'AND s.incorrect_count != 0', $where_clause );
			$query = substr( $query, 0, $where_pos + 6 ) . $modified_where_clause;
		} else {
			// If 'AND s.incorrect_count = 1' isn't found, it might mean the original query is structured differently
			// or doesn't have this specific condition. In a real-world scenario, you'd want to inspect the original query more carefully.
			// For this example, let's assume a basic structure and try to append a condition if it's missing.
			// This might be less robust.
			$query .= ' AND s.incorrect_count != 0';
		}

		// Additionally, let's ensure that 'answer_data' is correctly handled.
		// If 'answer_data' stores a JSON array of selected answers, the LIKE operator might not be appropriate.
		// For this example, we'll stick to LIKE as it's present in the original snippet, but a more robust solution
		// might involve JSON functions if the data format requires it.

	} else {
		// If there's no WHERE clause, we need to construct it. This is less likely given the source context.
		// For demonstration, let's assume a basic WHERE clause structure is always present.
		// A safer approach would involve re-querying and building the SQL from scratch if needed.
		$query .= " WHERE s.question_id = {$question_id} AND s.answer_data LIKE '{$question_position}' AND s.incorrect_count != 0";
	}


	// Example: Also, let's prepend a condition to explicitly check for correct answers for comparison,
	// if that's something the user might want to observe side-by-side.
	// This is just an illustration of how you could add complexity.
	// $query = str_replace(
	// 	"SELECT COUNT(*)",
	// 	"SELECT COUNT(*) AS total_attempts, SUM(CASE WHEN s.incorrect_count = 0 THEN 1 ELSE 0 END) AS correct_attempts",
	// 	$query
	// );
	// This change would require the calling function to handle multiple return values or a different query structure.
	// For this specific filter, we'll keep it focused on modifying the original COUNT(*) query.


	return $query;
}, 10, 5 ); // Priority 10, accepting 5 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:738

private static function get_times_this_answer_selected( $question_position, $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_this_answer_selected',
			"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.answer_data LIKE '$question_position'
  AND s.incorrect_count = 1
  $date_ranges",
			$question_id,
			$question_position,
			$table,
			$pro_quiz_id
		);

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


Scroll to Top