ulgm_quiz_report_table_columns
Filters the default columns displayed in the user quiz report table.
add_filter( 'ulgm_quiz_report_table_columns', $callback, 10, 1 );
Description
Customize the columns displayed in the group quiz report table. This filter allows you to add, remove, or reorder columns by modifying the `$default_columns` array. It's applied after the default columns are defined but before they are used to render the table.
Usage
add_filter( 'ulgm_quiz_report_table_columns', 'your_function_name', 10, 1 );
Parameters
-
$default_columns(mixed) - This parameter contains an array of default columns configured for the quiz report table.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'ulgm_quiz_report_table_columns' filter to
* add a new custom column to the quiz report table and reorder existing ones.
*
* The default columns might include things like 'quiz_title', 'user_email',
* 'score', 'completion_time', etc.
*
* @param array $default_columns An array of default column configurations.
* @return array Modified array of column configurations.
*/
function my_custom_quiz_report_columns( $default_columns ) {
// Define a new custom column.
// The keys in this array will become the column IDs used internally.
// The 'title' key is what will be displayed in the table header.
$custom_columns = array(
'quiz_results_summary' => array(
'title' => __( 'Results Summary', 'my-text-domain' ),
'callback' => 'my_render_quiz_results_summary_column', // A callback function to render the cell content.
'priority' => 5, // Controls the order of the column. Lower numbers appear earlier.
),
);
// Reorder existing columns and add the new custom column.
$new_column_order = array();
// Example: Move 'score' column to be after 'quiz_title'.
if ( isset( $default_columns['quiz_title'] ) ) {
$new_column_order['quiz_title'] = $default_columns['quiz_title'];
}
if ( isset( $default_columns['score'] ) ) {
// Add our custom column before the original score column.
$new_column_order['quiz_results_summary'] = $custom_columns['quiz_results_summary'];
$new_column_order['score'] = $default_columns['score'];
} else {
// If score column doesn't exist for some reason, just add our custom one.
$new_column_order['quiz_results_summary'] = $custom_columns['quiz_results_summary'];
}
// Add any other default columns that weren't explicitly handled above.
foreach ( $default_columns as $column_id => $column_config ) {
if ( ! array_key_exists( $column_id, $new_column_order ) ) {
$new_column_order[ $column_id ] = $column_config;
}
}
// Ensure our custom column is present if it wasn't added in the reordering logic.
if ( ! isset( $new_column_order['quiz_results_summary'] ) ) {
$new_column_order['quiz_results_summary'] = $custom_columns['quiz_results_summary'];
}
return $new_column_order;
}
// Add the filter to WordPress.
// The second argument '10' is the priority.
// The third argument '1' indicates that our callback function accepts one argument ($default_columns).
add_filter( 'ulgm_quiz_report_table_columns', 'my_custom_quiz_report_columns', 10, 1 );
/**
* Callback function to render the content for the custom 'Results Summary' column.
* This function would typically reside in your theme's functions.php or a custom plugin.
*
* @param array $item The data for the current quiz report row.
* @return string The HTML output for the column cell.
*/
function my_render_quiz_results_summary_column( $item ) {
// $item is an array containing the data for a single quiz report row.
// The structure of $item depends on how the plugin generates report data.
// For example, it might contain keys like 'score', 'total_questions', 'correct_answers', etc.
// This is a placeholder. You'd need to inspect the $item structure
// provided by the 'ulgm_quiz_report' plugin to build this logic.
$score = isset( $item['score'] ) ? $item['score'] : 'N/A';
$total_questions = isset( $item['total_questions'] ) ? $item['total_questions'] : 'N/A';
$correct_answers = isset( $item['correct_answers'] ) ? $item['correct_answers'] : 'N/A';
$output = '';
if ( 'N/A' !== $score ) {
$output .= sprintf( __( 'Score: %s', 'my-text-domain' ), esc_html( $score ) );
if ( 'N/A' !== $total_questions ) {
$output .= sprintf( ' (%d/%d)', esc_html( $correct_answers ), esc_html( $total_questions ) );
}
} else {
$output = __( 'No data', 'my-text-domain' );
}
return $output;
}
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:130
* @param $request
*
* @return false|string|void
*/
public function display_quiz_report( $request ) {
$default_columns = self::table_columns_config();
$columns_config = apply_filters( 'ulgm_quiz_report_table_columns', $default_columns );
$columns_config = ! is_array( $columns_config ) || empty( $columns_config ) ? $default_columns : $columns_config;
// Fix the params keys and set the default values.
$request = Utilities::fix_params_key( $request, 'csv-export-button', 'csv_export_button' );
$request = Utilities::fix_params_key( $request, 'excel-export-button', 'excel_export_button' );
$request = shortcode_atts(
array(