Action
uncanny-automator-pro
tutor_course_complete_after
Fires after a student successfully completes a course, providing the course and user IDs.
add_action( 'tutor_course_complete_after', $callback, 10, 2 );
Description
Fires after a user successfully completes a course in Tutor LMS. Developers can use this hook to trigger custom actions, such as awarding badges, sending notifications, or updating user meta, immediately after course completion. It provides the course ID and user ID for context.
Usage
add_action( 'tutor_course_complete_after', 'your_function_name', 10, 2 );
Parameters
-
$course_id(mixed) - The ID of the course that has just been completed.
-
$user_id(mixed) - This parameter represents the unique identifier of the course that has just been completed.
Examples
add_action( 'tutor_course_complete_after', 'my_custom_course_completion_logic', 10, 2 );
/**
* Processes custom logic when a course is completed in Tutor LMS.
*
* This function can be used to trigger various actions after a user successfully
* completes a course, such as sending an email notification, granting a badge,
* or updating user meta.
*
* @param int $course_id The ID of the completed course.
* @param int $user_id The ID of the user who completed the course.
*/
function my_custom_course_completion_logic( $course_id, $user_id ) {
// Example: Log the course completion to a custom log file.
$log_message = sprintf(
'User ID: %1$d completed Course ID: %2$d on %3$s',
$user_id,
$course_id,
current_time( 'mysql' )
);
error_log( $log_message, 3, WP_CONTENT_DIR . '/tutor-course-completions.log' );
// Example: Grant a custom achievement or badge (requires additional plugins/custom code).
// This is a placeholder and would involve interacting with a badge system.
// For instance, if you have a custom function to grant badges:
// if ( function_exists( 'grant_custom_badge' ) ) {
// grant_custom_badge( $user_id, 'course_complete_' . $course_id );
// }
// Example: Update user meta to track completed courses.
$completed_courses = get_user_meta( $user_id, 'tutor_completed_courses', true );
if ( ! is_array( $completed_courses ) ) {
$completed_courses = array();
}
if ( ! in_array( $course_id, $completed_courses, true ) ) {
$completed_courses[] = $course_id;
update_user_meta( $user_id, 'tutor_completed_courses', $completed_courses );
}
// Example: Send an email notification to the user.
$user_info = get_userdata( $user_id );
if ( $user_info ) {
$to_email = $user_info->user_email;
$course = get_post( $course_id );
$subject = sprintf( 'Congratulations on completing "%s"!', $course->post_title );
$message = sprintf( 'Dear %s, we are thrilled to inform you that you have successfully completed the course "%s". Well done!', $user_info->display_name, $course->post_title );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
// wp_mail( $to_email, $subject, $message, $headers );
}
}
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
uncanny-automator-pro/src/integrations/tutorlms/helpers/tutorlms-pro-helpers.php:79
public function complete_course( $course_id, $user_id ) {
do_action( 'tutor_course_complete_before', $course_id );
global $wpdb;
$date = date( 'Y-m-d H:i:s', tutor_time() );
// Making sure that hash is unique.
do {
$hash = substr( md5( wp_generate_password( 32 ) . $date . $course_id . $user_id ), 0, 16 );
$has_hash = (int) $wpdb->get_var( "SELECT COUNT(comment_ID) from {$wpdb->comments} WHERE comment_agent = 'TutorLMSPlugin' AND comment_type = 'course_completed' AND comment_content = '{$hash}' " );
} while ( $has_hash > 0 );
$data = array(
'comment_post_ID' => $course_id,
'comment_author' => $user_id,
'comment_date' => $date,
'comment_date_gmt' => get_gmt_from_date( $date ),
'comment_content' => $hash, //Identification Hash.
'comment_approved' => 'approved',
'comment_agent' => 'TutorLMSPlugin',
'comment_type' => 'course_completed',
'user_id' => $user_id,
);
$wpdb->insert( $wpdb->comments, $data );
do_action( 'tutor_course_complete_after', $course_id, $user_id );
}