uo_ceu_historical_course_recorded
Fires after a historical course recording has been successfully inserted into the database.
add_action( 'uo_ceu_historical_course_recorded', $callback, 10, 1 );
Description
Fires after a user's historical course completion data has been recorded. Developers can use this hook to perform additional actions when a course is marked as completed for a user, such as updating user meta or triggering custom notifications. The `$insert_data` parameter contains the data being inserted into the database.
Usage
add_action( 'uo_ceu_historical_course_recorded', 'your_function_name', 10, 1 );
Parameters
-
$insert_data(mixed) - This parameter contains data related to a completed course, including user, course, progress, and completion date information, intended for historical recording.
Examples
// Example of how to hook into the 'uo_ceu_historical_course_recorded' action.
// This function will be executed after a user's historical course data has been recorded.
function my_handle_historical_course_recorded( $insert_data ) {
// Check if $insert_data is an array and has the expected keys.
if ( ! is_array( $insert_data ) || ! isset( $insert_data['user_id'] ) || ! isset( $insert_data['course_id'] ) ) {
// Log an error or handle the unexpected data format.
error_log( 'uo_ceu_historical_course_recorded received unexpected data: ' . print_r( $insert_data, true ) );
return;
}
$user_id = intval( $insert_data['user_id'] );
$course_id = intval( $insert_data['course_id'] );
// Example: Award a badge to the user for completing this course.
// This is a hypothetical example; you'd need a badge system plugin or custom logic.
if ( function_exists( 'my_award_badge_to_user' ) ) {
$badge_id = my_get_badge_for_course( $course_id ); // Hypothetical function
if ( $badge_id ) {
my_award_badge_to_user( $user_id, $badge_id );
// Optionally log that a badge was awarded.
error_log( "User ID {$user_id} awarded badge ID {$badge_id} for completing Course ID {$course_id}." );
}
}
// Example: Send a notification to an administrator about a new course completion.
$user_info = get_userdata( $user_id );
$course_info = get_post( $course_id );
if ( $user_info && $course_info ) {
$admin_email = get_option( 'admin_email' );
$subject = sprintf( 'New Course Completion: %s by %s', $course_info->post_title, $user_info->display_name );
$message = sprintf(
"A user has completed a course:nn" .
"User: %s (ID: %d)n" .
"Course: %s (ID: %d)n" .
"Completed Date: %sn",
$user_info->display_name,
$user_id,
$course_info->post_title,
$course_id,
isset( $insert_data['date_completed'] ) ? $insert_data['date_completed'] : 'N/A'
);
// Consider using wp_mail() for sending emails.
// wp_mail( $admin_email, $subject, $message );
error_log( "New course completion notification for Admin: {$subject}" );
}
// You can also use the $insert_data to update custom user meta, trigger other actions, etc.
}
add_action( 'uo_ceu_historical_course_recorded', 'my_handle_historical_course_recorded', 10, 1 );
// The '10' is the priority, and '1' indicates that the function accepts one argument ($insert_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/classes/ceu-store-historical-data.php:113
src/classes/ceu-migrate-user-records.php:439
public function learndash_course_completed_func( $data ) {
$user = $data['user'];
$course = $data['course'];
$progress = $data['progress'];
$date = $data['course_completed'];
$since = ld_course_access_from( $course->ID, $user->ID );
if ( empty( $since ) ) {
$since = learndash_user_group_enrolled_to_course_from( $user->ID, $course->ID );
}
global $wpdb;
$insert_data = array(
'user_id' => $user->ID,
'course_id' => $course->ID,
'progress' => maybe_serialize( $progress[ $course->ID ] ),
'certificate' => '',
'date_enrolled' => $since,
'date_completed' => $date,
);
$wpdb->insert(
$wpdb->prefix . self::$historical_course_table,
$insert_data,
array(
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
)
);
do_action( 'uo_ceu_historical_course_recorded', $insert_data );
}