tincanny_csv_rows_data
Filters the data array for CSV rows before it's processed or exported.
add_filter( 'tincanny_csv_rows_data', $callback, 10, 1 );
Description
Fires after the CSV data array is prepared but before it's outputted. Developers can modify or filter the `$data` array to alter the CSV content, add custom columns, or remove unwanted rows. This hook is crucial for dynamic CSV generation.
Usage
add_filter( 'tincanny_csv_rows_data', 'your_function_name', 10, 1 );
Parameters
-
$data(mixed) - This parameter contains the data that will be processed and potentially modified for output in a CSV format.
Return Value
The filtered value.
Examples
/**
* Example of how to filter the data passed to the tincanny_csv_rows_data hook.
* This example adds an extra column to the CSV output, showing the course title
* for each row of activity data.
*
* @param array $data The original data array for the CSV.
* @return array The modified data array with the added column.
*/
add_filter( 'tincanny_csv_rows_data', 'my_tincanny_add_course_title_to_csv', 10, 1 );
function my_tincanny_add_course_title_to_csv( $data ) {
// Ensure we're working with an array
if ( ! is_array( $data ) ) {
return $data;
}
// Assume the original data has columns like 'user_id', 'activity_id', 'completed_at', etc.
// We'll add a 'course_title' column.
// Prepare the header row (if it exists and is the first element)
// We need to find the correct index to insert our new column name.
$header_key = -1;
$new_header_row = $data; // Start with a copy
if ( ! empty( $data ) ) {
// Check if the first element looks like a header row (e.g., all strings)
// This is a heuristic and might need adjustment based on the actual structure of $data.
$is_header = true;
foreach ( $data[0] as $column_value ) {
if ( ! is_string( $column_value ) ) {
$is_header = false;
break;
}
}
if ( $is_header ) {
// Find a suitable place to insert the new column name.
// For simplicity, let's assume we want it near the beginning, or at the end.
// Let's append it for this example.
$new_header_row[0][] = 'Course Title';
$header_key = 0; // Mark that we've modified the header
}
}
// Iterate through the data rows and add the course title
foreach ( $data as $index => $row ) {
// Skip the header row if we've already processed it
if ( $index === $header_key ) {
continue;
}
// The structure of each row will depend on how Uncanny Tin Can formats its data.
// Assuming each $row is an associative array or an object with keys like 'user_id', 'activity_id'.
// We'll need to retrieve the course title based on the activity or user context.
// This is a placeholder; a real implementation would involve querying the WordPress database
// or using Uncanny Tin Can's internal methods to get the course title.
// For demonstration purposes, let's assume the row contains 'activity_id'
// and we have a function `get_course_title_for_activity( $activity_id )` available.
$course_title = 'N/A'; // Default if not found
// This is a crucial part that depends on the $data structure.
// You'll need to inspect $data to know what keys or properties are available to find the course.
// Example: if $row['activity_id'] exists
if ( isset( $row['activity_id'] ) && ! empty( $row['activity_id'] ) ) {
// This is a mock function call. In a real scenario, you'd get this from Uncanny Tin Can API or WP DB.
// For example, if activity ID maps directly to a course ID, or if the activity itself has a course association.
// Example: $course_id = get_course_id_from_activity( $row['activity_id'] );
// $course_title = get_the_title( $course_id );
// Or, if Uncanny Tin Can provides a direct lookup:
// $course_title = UncannyTinCanAPIActivity::get_course_title( $row['activity_id'] );
// Mocking a lookup:
$mock_activity_to_course_map = [
'act_123' => 'The Amazing Course',
'act_456' => 'Advanced Techniques',
'act_789' => 'Beginner's Guide',
];
if ( isset( $mock_activity_to_course_map[ $row['activity_id'] ] ) ) {
$course_title = $mock_activity_to_course_map[ $row['activity_id'] ];
}
} elseif ( isset( $row['course_id'] ) && ! empty( $row['course_id'] ) ) {
// Another potential scenario if course ID is directly available
$course_title = get_the_title( $row['course_id'] );
}
// Add the course title to the row.
// If we've modified the header, we need to ensure the row has the correct number of columns.
if ( $header_key === 0 ) {
// Append the course title to the end of the row array.
$data[ $index ][] = $course_title;
} else {
// If no header was detected/modified, append it anyway to the data row.
// This assumes the CSV output can handle an appended column.
$data[ $index ]['course_title'] = $course_title; // If it's an associative array
// Or $data[ $index ][] = $course_title; // If it's a numerically indexed array
}
}
// If we modified the header row, update the first element of $data.
if ( $header_key === 0 ) {
$data[0] = $new_header_row[0];
}
return $data;
}
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-tincan/classes/Admin/CSV.php:32
public function __construct( $data ) {
$data = apply_filters( 'tincanny_csv_rows_data', $data );
$this->data = $data;
if ( !empty( $this->data ) ){
if ( !headers_sent() ) $this->header();
$this->output = fopen('php://output', 'w');
$this->print_table_header();
$this->print_csv();
fclose( $this->output );
die;
}
}