Action uncanny-automator

stm_lms_lesson_passed

Fires when a student successfully passes a lesson, passing the user and lesson IDs.

add_action( 'stm_lms_lesson_passed', $callback, 10, 2 );

Description

Fires after a user successfully completes a MasterStudy LMS lesson. Developers can use this hook to trigger custom actions, such as awarding badges, sending notifications, or updating external systems, based on lesson completion. It provides the user ID and lesson ID for context.


Usage

add_action( 'stm_lms_lesson_passed', 'your_function_name', 10, 2 );

Parameters

$user_id (mixed)
The ID of the user who has passed the lesson.
$lesson_id (mixed)
The user ID of the student who has passed the lesson.

Examples

/**
 * Fires when a user has successfully passed a lesson.
 *
 * This function can be used to trigger custom actions when a lesson is passed,
 * such as awarding points, sending notifications, or unlocking further content.
 *
 * @param int $user_id   The ID of the user who passed the lesson.
 * @param int $lesson_id The ID of the lesson that was passed.
 */
add_action( 'stm_lms_lesson_passed', function( $user_id, $lesson_id ) {
    // Example: Award bonus points to the user for passing the lesson.
    // This is a hypothetical function, you would replace it with your actual point system integration.
    if ( function_exists( 'your_custom_award_points_function' ) ) {
        $points_to_award = 10; // Define the number of points to award
        your_custom_award_points_function( $user_id, $points_to_award );
    }

    // Example: Log the lesson completion for reporting purposes.
    // This assumes you have a custom logging mechanism or a plugin that handles this.
    if ( function_exists( 'your_custom_log_activity_function' ) ) {
        $log_message = sprintf( 'User %d passed lesson %d.', $user_id, $lesson_id );
        your_custom_log_activity_function( $user_id, 'lesson_passed', $log_message );
    }

    // Example: Check if this lesson completion unlocks a specific badge.
    // You would integrate with your badge system here.
    if ( function_exists( 'your_custom_check_badge_unlock_function' ) ) {
        your_custom_check_badge_unlock_function( $user_id, $lesson_id );
    }

}, 10, 2 );

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/integrations/masterstudy-lms/actions/masterstudy-markcoursecomplete.php:130
uncanny-automator-pro/src/integrations/masterstudy-lms/actions/masterstudy-mark-lesson-complete.php:144

public function mark_course_complete( $user_id, $action_data, $recipe_id, $args ) {

		$course_id  = $action_data['meta'][ $this->action_meta ];
		$helpers    = new Masterstudy_Helpers( false );
		$curriculum = $helpers->get_course_curriculum_materials( $course_id );

		if ( empty( $curriculum ) ) {
			$action_data['complete_with_errors'] = true;
			$error                               = _x( 'Course does not have any lessons or quizzes to complete.', 'Masterstudy LMS - Complete course action', 'uncanny-automator' );
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error );
			return;
		}

		// Enroll the user is the course if they are not already enrolled
		$course = stm_lms_get_user_course( $user_id, $course_id, array( 'user_course_id' ) );
		if ( ! count( $course ) ) {
			STM_LMS_Course::add_user_course( $course_id, $user_id, STM_LMS_Course::item_url( $course_id, '' ), 0 );
			STM_LMS_Course::add_student( $course_id );
		}

		foreach ( $curriculum as $post ) {

			if ( 'stm-lessons' === $post['post_type'] ) {

				// Complete Lesson
				$lesson_id = $post['post_id'];

				if ( STM_LMS_Lesson::is_lesson_completed( $user_id, $course_id, $lesson_id ) ) {
					continue;
				};

				$end_time   = time();
				$start_time = get_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}", true );

				stm_lms_add_user_lesson( compact( 'user_id', 'course_id', 'lesson_id', 'start_time', 'end_time' ) );
				STM_LMS_Course::update_course_progress( $user_id, $course_id );

				do_action( 'stm_lms_lesson_passed', $user_id, $lesson_id );

				delete_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}" );
			}

			if ( 'stm-quizzes' === $post['post_type'] ) {

				// Complete quiz
				$quiz_id = $post['post_id'];

				if ( STM_LMS_Quiz::quiz_passed( $quiz_id, $user_id ) ) {
					continue;
				}

				$progress  = 100;
				$status    = 'passed';
				$user_quiz = compact( 'user_id', 'course_id', 'quiz_id', 'progress', 'status' );
				stm_lms_add_user_quiz( $user_quiz );
				stm_lms_get_delete_user_quiz_time( $user_id, $quiz_id );

				STM_LMS_Course::update_course_progress( $user_id, $course_id );

				$user_quiz['progress'] = round( $user_quiz['progress'], 1 );
				do_action( 'stm_lms_quiz_' . $status, $user_id, $quiz_id, $user_quiz['progress'] );

			}
		}

		Automator()->complete_action( $user_id, $action_data, $recipe_id );
	}


Scroll to Top