Tutor LMS: Recent Course Completions Dashboard Widget
Keeps a log of the latest Tutor LMS course completions and shows them in a dashboard widget with student names and dates.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
add_action( 'tutor_course_complete_after', function ( $course_id, $user_id = 0 ) {
$log = (array) get_option( 'wpide_tutor_completions', array() );
array_unshift( $log, array( 'user' => $user_id ? (int) $user_id : get_current_user_id(), 'course' => (int) $course_id, 'time' => time() ) );
update_option( 'wpide_tutor_completions', array_slice( $log, 0, 15 ), false );
}, 10, 2 );
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_tutor_completions', 'Recent course completions', function () {
$log = array_filter( (array) get_option( 'wpide_tutor_completions', array() ), 'is_array' );
if ( ! $log ) {
echo '<p>No completions recorded yet.</p>';
return;
}
echo '<ul>';
foreach ( $log as $row ) {
$user = get_userdata( $row['user'] );
printf(
'<li><strong>%1$s</strong> completed %2$s <span style="color:#646970">%3$s</span></li>',
esc_html( $user ? $user->display_name : 'Deleted user' ),
esc_html( get_the_title( $row['course'] ) ),
esc_html( wp_date( get_option( 'date_format' ), (int) $row['time'] ) )
);
}
echo '</ul>';
} );
} );
Notes
Keeps the 15 most recent completions, starting from activation.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.