UOTC_USERATTAINSSCORE::tincan_module_result_processed( $module_id,  $user_id,  $score )


Parameters Parameters

$module_id

(Required)

$user_id

(Required)

$verb

(Required)

$data

(Required)


Source Source

File: src/integrations/uncanny-tincanny/triggers/uotc-userattainsscore.php

	public function tincan_module_result_processed( $module_id, $user_id, $score ) {

		if ( absint( $user_id ) === 0 ) {
			return;
		}

		if ( absint( $score ) < 0 ) {
			return;
		}

		if ( ! empty( $module_id ) ) {
			if ( ! absint( $module_id ) ) {
				return;
			}
		}

		$recipes             = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$module_ids          = Automator()->get->meta_from_recipes( $recipes, 'TCMODULEINTERACTION' );
		$required_conditions = Automator()->get->meta_from_recipes( $recipes, 'NUMBERCOND' );
		$required_scores     = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );

		$matched_recipe_ids = array();

		foreach ( $recipes as $recipe_id => $recipe ) {

			foreach ( $recipe['triggers'] as $trigger ) {

				$trigger_id = $trigger['ID'];

				if ( ( (int) $module_ids[ $recipe_id ][ $trigger_id ] === (int) $module_id || '-1' == $module_ids[ $recipe_id ][ $trigger_id ] ) && Automator()->utilities->match_condition_vs_number( $required_conditions[ $recipe_id ][ $trigger_id ], $required_scores[ $recipe_id ][ $trigger_id ], $score ) ) {
					$matched_recipe_ids[] = array(
						'recipe_id'          => $recipe_id,
						'trigger_id'         => $trigger_id,
						'required_condition' => $required_conditions[ $recipe_id ][ $trigger_id ],
					);
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {
				// Custom check for duplicate recipe run within 10 second window. Similar to Tin Canny Plugin
				global $wpdb;
				$results = $wpdb->get_row(
					$wpdb->prepare(
						"SELECT date_time, CURRENT_TIMESTAMP as current_mysql_time
						FROM {$wpdb->prefix}uap_recipe_log
						WHERE 1=1
						AND user_id = %d
						AND automator_recipe_id = %d
						AND completed = 1 ORDER BY ID DESC ",
						$user_id,
						$matched_recipe_id['recipe_id']
					)
				);

				$can_run = true;
				if ( ! empty( $results ) ) {
					$last_time    = strtotime( $results->date_time );
					$current_time = strtotime( $results->current_mysql_time );

					if ( ( $current_time - $last_time ) <= 10 ) {
						$can_run = false;
					}
				}

				if ( ! Automator()->is_recipe_completed( $matched_recipe_id['recipe_id'], $user_id ) && $can_run ) {
					$args = array(
						'code'             => $this->trigger_code,
						'meta'             => $this->trigger_meta,
						'user_id'          => $user_id,
						'recipe_to_match'  => $matched_recipe_id['recipe_id'],
						'trigger_to_match' => $matched_recipe_id['trigger_id'],
						'ignore_post_id'   => true,
						'is_signed_in'     => true,
						'post_id'          => $module_id,
					);

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

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

								Automator()->db->trigger->add_meta(
									$result['args']['trigger_id'],
									$result['args']['get_trigger_id'],
									$result['args']['run_number'],
									array(
										'user_id'        => $user_id,
										'trigger_id'     => $result['args']['trigger_id'],
										'meta_key'       => $this->trigger_meta,
										'meta_value'     => $score,
										'trigger_log_id' => $result['args']['get_trigger_id'],
										'run_number'     => $result['args']['run_number'],
									)
								);
								Automator()->db->trigger->add_meta(
									$result['args']['trigger_id'],
									$result['args']['get_trigger_id'],
									$result['args']['run_number'],
									array(
										'user_id'        => $user_id,
										'trigger_id'     => $result['args']['trigger_id'],
										'meta_key'       => 'TCMODULEINTERACTION',
										'meta_value'     => $module_id,
										'trigger_log_id' => $result['args']['get_trigger_id'],
										'run_number'     => $result['args']['run_number'],
									)
								);
								Automator()->db->trigger->add_meta(
									$result['args']['trigger_id'],
									$result['args']['get_trigger_id'],
									$result['args']['run_number'],
									array(
										'user_id'        => $user_id,
										'trigger_id'     => $result['args']['trigger_id'],
										'meta_key'       => 'NUMBERCOND',
										'meta_value'     => $matched_recipe_id['required_condition'],
										'trigger_log_id' => $result['args']['get_trigger_id'],
										'run_number'     => $result['args']['run_number'],
									)
								);

								Automator()->process->user->maybe_trigger_complete( $result['args'] );
							}
						}
					}
				}
			}
		}
	}