Action uncanny-automator-pro

tutor_course_complete_before

Fires before a course is marked as complete for a student in Tutor LMS.

add_action( 'tutor_course_complete_before', $callback, 10, 1 );

Description

Fires before a course is marked as completed in Tutor LMS. This action hook allows developers to perform custom actions or modify data just before the course completion status is updated for a user. It provides the course ID as a parameter.


Usage

add_action( 'tutor_course_complete_before', 'your_function_name', 10, 1 );

Parameters

$course_id (mixed)
The ID of the course that the user is about to complete.

Examples

<?php
/**
 * Example function to hook into the tutor_course_complete_before action.
 * This example will check if the course being completed is marked as "premium"
 * and if so, it will log a message to the WordPress debug log.
 */
function my_custom_tutor_course_completion_logic( $course_id ) {

	// Check if the course is marked as premium.
	// This assumes there's a meta key '_tutor_is_premium' that Tutor LMS might use or that you've added.
	// You'll need to adapt this based on how you identify premium courses in your setup.
	$is_premium_course = get_post_meta( $course_id, '_tutor_is_premium', true );

	if ( $is_premium_course && $is_premium_course === 'yes' ) {
		// Log a message indicating a premium course was completed.
		error_log( sprintf( 'Premium course with ID %d has been completed. Performing additional actions...', $course_id ) );

		// Here you could add further logic, such as:
		// - Triggering a custom email notification.
		// - Granting specific user roles.
		// - Updating a user's progress in another system.
		// - Enrolling the user in a follow-up course.
	}
}

// Add the action hook.
// We are accepting only one argument ($course_id), so the third parameter is 1.
add_action( 'tutor_course_complete_before', 'my_custom_tutor_course_completion_logic', 10, 1 );
?>

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:53

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 );

	}


Scroll to Top