ulgm_rest_api_get_quiz_data
Filters the quiz data retrieved for the REST API before it is returned.
add_filter( 'ulgm_rest_api_get_quiz_data', $callback, 10, 2 );
Description
Filters the data retrieved for a group quiz report before it's returned via the REST API. Developers can modify the `$quiz_table` and `$_POST` data to customize report content or filtering. This hook fires after initial data validation but before data is fetched.
Usage
add_filter( 'ulgm_rest_api_get_quiz_data', 'your_function_name', 10, 2 );
Parameters
-
$quiz_table(mixed) - This parameter contains the data for the quiz that will be retrieved.
-
$_POST(mixed) - This parameter represents the data fetched for the quiz report, likely in a tabular format.
Return Value
The filtered value.
Examples
/**
* Example filter function to modify quiz data before it's returned.
*
* This function demonstrates how to hook into the 'ulgm_rest_api_get_quiz_data'
* filter to add custom logic or alter the existing quiz data.
*
* @param array $quiz_table The original quiz data table generated by the plugin.
* @param array $post_data The $_POST data received from the request, containing quizId, scoreType, etc.
* @return array The modified quiz data table.
*/
add_filter( 'ulgm_rest_api_get_quiz_data', 'my_custom_ulgm_quiz_data_modifier', 10, 2 );
function my_custom_ulgm_quiz_data_modifier( $quiz_table, $post_data ) {
// Ensure we have valid data to work with.
if ( empty( $quiz_table ) || ! is_array( $quiz_table ) || empty( $post_data ) || ! is_array( $post_data ) ) {
return $quiz_table; // Return original data if invalid.
}
// Example: Add a custom column to the quiz data if a specific score type is requested.
// Let's assume 'scoreType' is 'raw' and we want to add a 'bonus_points' column.
if ( isset( $post_data['scoreType'] ) && 'raw' === sanitize_text_field( $post_data['scoreType'] ) ) {
// We need to iterate through the existing rows and add the new column.
// This is a hypothetical addition. In a real scenario, you'd fetch
// or calculate this 'bonus_points' value based on your plugin's logic
// and potentially the $post_data.
foreach ( $quiz_table['rows'] as &$row ) {
// For demonstration, let's assign a static value or a simple calculation.
// Replace this with your actual logic to determine bonus points.
$row['bonus_points'] = ( isset( $row['score'] ) ) ? absint( $row['score'] ) * 0.1 : 0;
}
unset( $row ); // Unset the reference to avoid potential issues.
// Update table headers if they exist
if ( isset( $quiz_table['headers'] ) && is_array( $quiz_table['headers'] ) ) {
$quiz_table['headers'][] = array(
'key' => 'bonus_points',
'label' => __( 'Bonus Points', 'my-text-domain' ),
);
}
}
// Example: Filter out rows where a specific user (identified by a hypothetical 'exclude_user_id' in POST) scored below a certain threshold.
if ( isset( $post_data['exclude_user_id'] ) && ! empty( $post_data['exclude_user_id'] ) ) {
$exclude_user_id = absint( $post_data['exclude_user_id'] );
$min_score_threshold = 50; // Hypothetical minimum score for inclusion
$filtered_rows = array();
foreach ( $quiz_table['rows'] as $row ) {
// Assuming each row has a 'user_id' and 'score' field.
if ( isset( $row['user_id'] ) && $row['user_id'] == $exclude_user_id ) {
// Skip this user if they match the exclusion ID.
continue;
}
if ( isset( $row['score'] ) && $row['score'] < $min_score_threshold ) {
// Optionally, you could also filter out low scores.
continue;
}
$filtered_rows[] = $row;
}
$quiz_table['rows'] = $filtered_rows;
}
return $quiz_table;
}
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/classes/reports/group-quiz-report.php:758
public function get_quiz_data() {
$data = $_POST;
// validate inputs
$quiz_ID = absint( $data['quizId'] );
$score_type = isset( $data['scoreType'] ) ? sanitize_text_field( $data['scoreType'] ) : 'percent';
// if any of the values are 0 then they didn't validate
if ( 0 === $quiz_ID ) {
$return_object['message'] = sprintf( __( 'invalid %s id supplied', 'uncanny-learndash-groups' ), LearnDash_Custom_Label::get_label( 'course' ) );
$return_object['groupId'] = $data['courseId'];
return $return_object;
}
// validate inputs
$group_ID = absint( $data['groupId'] );
// if any of the values are 0 then they didn't validate
if ( 0 === $group_ID ) {
$return_object['message'] = __( 'invalid group id supplied', 'uncanny-learndash-groups' );
$return_object['groupId'] = $data['groupId'];
return $return_object;
}
$quiz_table = self::quiz_table( $quiz_ID, $group_ID, $score_type );
$quiz_table = apply_filters( 'ulgm_rest_api_get_quiz_data', $quiz_table, $_POST );
return $quiz_table;
}