Filter uncanny-automator

user_answers__course_id

Filters the course ID associated with user answers, allowing modification before it's saved or processed.

add_filter( 'user_answers__course_id', $callback, 10, 2 );

Description

Fires after the course ID is determined for MasterStudy LMS quiz-related automations. Developers can filter the `$course_id` to modify which course is associated with the trigger, useful for custom quiz assignments or conditional logic based on course. The `$source` parameter indicates the origin of the course ID.


Usage

add_filter( 'user_answers__course_id', 'your_function_name', 10, 2 );

Parameters

$course_id (mixed)
This parameter contains the ID of the course associated with the user's answer.
$source (mixed)
This parameter represents the ID of the course associated with the user's quiz attempt.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'user_answers__course_id' filter hook.
 *
 * This filter allows you to modify the course ID associated with a user's quiz answer.
 * For instance, you might want to append a prefix or perform a lookup based on the source.
 */
add_filter( 'user_answers__course_id', 'my_custom_course_id_filter', 10, 2 );

function my_custom_course_id_filter( $course_id, $source ) {

    // If the source is from a specific integration and we have a course ID,
    // let's say we want to append a special identifier.
    if ( ! empty( $course_id ) && 'some_specific_source_identifier' === $source ) {
        // In a real scenario, you might have a custom mapping or logic here.
        // For this example, we'll just append '-custom'.
        return $course_id . '-custom';
    }

    // If no special logic is needed, return the original course ID.
    return $course_id;
}
?>

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/triggers/masterstudy-quizpassed.php:137
src/integrations/masterstudy-lms/triggers/masterstudy-quizfailed.php:135
uncanny-automator-pro/src/integrations/masterstudy-lms/triggers/masterstudy-quiz-percentage.php:214

public function quiz_passed( $user_id, $quiz_id, $user_quiz_progress ) {

		$args = array(
			'code'    => $this->trigger_code,
			'meta'    => $this->trigger_meta,
			'post_id' => $quiz_id,
			'user_id' => $user_id,
		);

		$args = Automator()->maybe_add_trigger_entry( $args, false );

		if ( $args ) {
			foreach ( $args as $result ) {
				if ( true === $result['result'] ) {

					$source    = ( ! empty( automator_filter_input( 'source', INPUT_POST ) ) ) ? intval( automator_filter_input( 'source', INPUT_POST ) ) : '';
					$course_id = ( ! empty( automator_filter_input( 'course_id', INPUT_POST ) ) ) ? intval( automator_filter_input( 'course_id', INPUT_POST ) ) : '';
					$course_id = apply_filters( 'user_answers__course_id', $course_id, $source );

					Automator()->insert_trigger_meta(
						array(
							'user_id'        => $user_id,
							'trigger_id'     => $result['args']['trigger_id'],
							'meta_key'       => 'MSLMSCOURSE',
							'meta_value'     => $course_id,
							'trigger_log_id' => $result['args']['get_trigger_id'],
							'run_number'     => $result['args']['run_number'],
						)
					);

					Automator()->insert_trigger_meta(
						array(
							'user_id'        => $user_id,
							'trigger_id'     => $result['args']['trigger_id'],
							'meta_key'       => $this->trigger_meta . '_SCORE',
							'meta_value'     => $user_quiz_progress . '%',
							'trigger_log_id' => $result['args']['get_trigger_id'],
							'run_number'     => $result['args']['run_number'],
						)
					);

					Automator()->insert_trigger_meta(
						array(
							'user_id'        => $user_id,
							'trigger_id'     => $result['args']['trigger_id'],
							'meta_key'       => $this->trigger_meta,
							'meta_value'     => $quiz_id,
							'trigger_log_id' => $result['args']['get_trigger_id'],
							'run_number'     => $result['args']['run_number'],
						)
					);
					Automator()->maybe_trigger_complete( $result['args'] );
				}
			}
		}
	}

Scroll to Top