uo_tincanny_reporting_questions_query
Filters the query arguments used to retrieve reporting questions for a specific quiz.
add_filter( 'uo_tincanny_reporting_questions_query', $callback, 10, 4 );
Description
This filter hook `uo_tincanny_reporting_questions_query` allows developers to modify the SQL query used to retrieve quiz questions for reporting. It fires just before the query is executed, enabling customization of question selection based on `$quiz_id`, `$pro_quiz_id`, and `$question_pro_ids`. Developers can alter the `$wpdb` object to change the query logic.
Usage
add_filter( 'uo_tincanny_reporting_questions_query', 'your_function_name', 10, 4 );
Parameters
-
$wpdb(mixed) - This parameter contains a global object representing the WordPress database access.
-
$quiz_id(mixed) - This parameter is an instance of the WordPress `$wpdb` object, used for interacting with the database.
-
$pro_quiz_id(mixed) - This parameter represents the ID of the quiz for which questions are being retrieved.
-
$question_pro_ids(mixed) - This parameter contains the ID of the pro quiz being queried.
Return Value
The filtered value.
Examples
// Filter to modify the SQL query for fetching quiz questions in the Uncanny Tincanny reporting.
// This example demonstrates how to add an additional condition to exclude questions
// that have been marked as "deprecated" in a custom post meta.
add_filter(
'uo_tincanny_reporting_questions_query',
function ( $query, $quiz_id, $pro_quiz_id, $question_pro_ids ) {
global $wpdb;
// Ensure $question_pro_ids is not empty before proceeding.
if ( empty( $question_pro_ids ) ) {
return $query;
}
// Add a condition to exclude deprecated questions.
// Assumes a custom meta key 'uo_is_deprecated_question' exists and a value of '1' marks it as deprecated.
$query .= $wpdb->prepare(
" AND NOT EXISTS (
SELECT 1
FROM {$wpdb->postmeta} pm_deprecated
WHERE pm_deprecated.post_id = p.ID
AND pm_deprecated.meta_key = %s
AND pm_deprecated.meta_value = %s
)",
'uo_is_deprecated_question',
'1'
);
return $query;
},
10,
4 // The filter callback accepts 4 arguments: $query, $quiz_id, $pro_quiz_id, $question_pro_ids
);
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:434
private static function get_quiz_questions( $quiz_id, $pro_quiz_id ) {
global $wpdb;
$ld_question_tbl = LDLMS_DB::get_table_name( 'quiz_question' );
// phpcs:disable WordPress.DB.PreparedSQL
$question_post_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT p.ID as question_post_ID
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta pm
ON pm.post_id = p.ID AND (pm.meta_key = %s OR ( pm.meta_key = %s AND pm.meta_value = %d ))
WHERE p.post_type = %s
AND p.post_status = %s",
'ld_quiz_' . $quiz_id,
'quiz_id',
$quiz_id,
'sfwd-question',
'publish'
)
);
if ( empty( $question_post_ids ) ) {
return array();
}
$question_pro_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT meta_value FROM $wpdb->postmeta WHERE post_id IN (" . join( ', ', $question_post_ids ) . ')
AND meta_key = %s',
'question_pro_id'
)
);
$where = apply_filters(
'uo_tincanny_reporting_questions_query_where',
'',
$pro_quiz_id,
$question_pro_ids
);
$qry = apply_filters(
'uo_tincanny_reporting_questions_query',
$wpdb->prepare(
"SELECT p.ID AS posts_question_id, p.post_title AS post_question,
q.id AS question_id, q.title AS question_title, q.question AS question, q.answer_data
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta pm
ON pm.post_id = p.ID AND pm.meta_key = 'question_pro_id'
JOIN $ld_question_tbl q
ON q.id = pm.meta_value AND q.answer_type = %s
WHERE p.post_type = %s
AND p.post_status = %s
AND q.id IN (" . join( ',', $question_pro_ids ) . ")
$where
ORDER BY q.sort ASC",
'single',
'sfwd-question',
'publish'
),
$quiz_id,
$pro_quiz_id,
$question_pro_ids
);
$questions = $wpdb->get_results( $qry );
// phpcs:enable WordPress.DB.PreparedSQL
self::get_distractors( $questions );
self::cache_set( 'distractor_data_' . $quiz_id, self::$distractors );
self::cache_set( 'questions_data_' . $quiz_id, $questions );
self::cache_set( 'total_distractors_' . $quiz_id, self::$total_distractors );
self::cache_set( 'column_headings_' . $quiz_id, self::$columns );
return $questions;
}