uo_tin_can_filter_courses
Filters the list of courses before they are displayed, allowing modification of the course data.
add_filter( 'uo_tin_can_filter_courses', $callback, 10, 1 );
Description
Filters the list of courses displayed in the Tin Can filter options. Developers can use this hook to modify or replace the `$courses` array to customize which courses are available for filtering. This filter fires when the Tin Can filter template is rendered, providing access to the course data before it's displayed to the user.
Usage
add_filter( 'uo_tin_can_filter_courses', 'your_function_name', 10, 1 );
Parameters
-
$courses(mixed) - This parameter contains a mixed data type representing the courses to be filtered, potentially a WP_Query object or an array of course data.
Return Value
The filtered value.
Examples
<?php
/**
* Filter the list of courses for the Tin Can filter.
*
* This example demonstrates how to remove courses that have not yet been completed
* by the current user from the filter options.
*
* @param array $courses An array of course data, typically containing 'course_id' and 'course_name'.
* @return array The filtered array of course data.
*/
add_filter( 'uo_tin_can_filter_courses', 'my_plugin_filter_tin_can_courses', 10, 1 );
function my_plugin_filter_tin_can_courses( $courses ) {
// Ensure we are working with an array
if ( ! is_array( $courses ) ) {
return $courses;
}
// Get the current user ID
$current_user_id = get_current_user_id();
// If no user is logged in, return all courses
if ( ! $current_user_id ) {
return $courses;
}
// Prepare an array to hold courses that the user has at least started
$completed_courses = array();
// Fetch all courses the user has interacted with
$user_courses = learndash_get_users_courses( $current_user_id );
if ( ! empty( $user_courses ) ) {
foreach ( $user_courses as $course_id ) {
// For simplicity, we'll consider any course in the user's list as "available"
// In a more complex scenario, you might check completion status more strictly.
$completed_courses[] = $course_id;
}
}
// Filter the provided $courses array to only include those the user has interacted with
$filtered_courses = array_filter( $courses, function( $course ) use ( $completed_courses ) {
// Ensure the course has a valid 'course_id' key and that it exists in the user's completed list
return isset( $course['course_id'] ) && in_array( (int) $course['course_id'], $completed_courses, true );
} );
// Return the filtered courses
return array_values( $filtered_courses ); // Re-index array after filtering
}
?>
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/reporting/xapi-quiz/templates/xapi-quiz-filter.php:159
src/reporting/tin-can/templates/tc-tincan-filter.php:163
/* translators: %s: Course label */
__( 'All %s', 'uncanny-learndash-reporting' ),
apply_filters( 'uo_tin_can_filter_course_label', esc_html_x( 'Course', 'Tin Can Filter Group name', 'uncanny-learndash-reporting' ), true )
)
);
?>
</option>
<?php $courses = apply_filters( 'uo_tin_can_filter_courses', $courses ); ?>
<?php if ( ! empty( $courses ) ) { ?>
<?php foreach ( $courses as $course ) { ?>
<?php $tc_course_selected = ! empty( $tc_course_filter ) && $tc_course_filter === (int) $course['course_id'] ? ' selected="selected"' : ''; ?>
<option value="<?php echo esc_attr( $course['course_id'] ); ?>"<?php echo esc_attr( $tc_course_selected ); ?>>
<?php echo esc_html( $course['course_name'] ); ?>
</option>
<?php } ?>