uo_tincanny_reporting_questions_query_where
Filters the WHERE clause for querying reporting questions, allowing modification of the database query conditions.
add_filter( 'uo_tincanny_reporting_questions_query_where', $callback, 10, 2 );
Description
Filters the `WHERE` clause for querying reporting questions related to a specific quiz. Developers can use this hook to modify the query logic, for example, to include or exclude questions based on custom criteria, or to alter how quiz IDs are matched.
Usage
add_filter( 'uo_tincanny_reporting_questions_query_where', 'your_function_name', 10, 2 );
Parameters
-
$pro_quiz_id(mixed) - This parameter is a placeholder for potential future arguments and is not currently used by the function.
-
$question_pro_ids(mixed) - This parameter contains the ID of the pro quiz associated with the report.
Return Value
The filtered value.
Examples
// Add a filter to modify the WHERE clause for the Uncanny Owl Tincanny reporting questions query.
// This example demonstrates how to exclude questions associated with a specific quiz ID from the report.
add_filter(
'uo_tincanny_reporting_questions_query_where',
function ( $where, $pro_quiz_id, $question_pro_ids ) {
// Check if we have a quiz ID and question IDs to process.
if ( ! empty( $pro_quiz_id ) && ! empty( $question_pro_ids ) ) {
global $wpdb;
// Get the meta value for 'quiz_id' for the given question post IDs.
$question_quiz_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT pm.meta_value
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE p.post_type = %s
AND pm.meta_key = %s
AND pm.post_id IN (" . implode( ',', $question_pro_ids ) . ')',
'sfwd-question',
'quiz_id'
)
);
// If we found quiz IDs associated with the questions, filter them out.
if ( ! empty( $question_quiz_ids ) ) {
// Convert the array of quiz IDs to a comma-separated string for the SQL IN clause.
$quiz_ids_to_exclude = implode( ',', array_map( 'absint', $question_quiz_ids ) );
// Append to the existing WHERE clause to exclude these questions.
if ( ! empty( $where ) ) {
$where .= " AND q.id NOT IN (SELECT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.meta_key = 'quiz_id' AND pm.meta_value IN ($quiz_ids_to_exclude))";
} else {
$where = " AND q.id NOT IN (SELECT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.meta_key = 'quiz_id' AND pm.meta_value IN ($quiz_ids_to_exclude))";
}
}
}
return $where;
},
10,
3
);
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:427
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;
}