LD_QUIZPOINT::learndash_quiz_completed( $data,  $current_user )

Validation function when the trigger action is hit


Parameters Parameters

$data

(Required)

$current_user

(Required)


Source Source

File: src/integrations/learndash/triggers/ld-quizpoint.php

	public function learndash_quiz_completed( $data, $current_user ) {
		if ( empty( $data ) ) {
			return;
		}

		$quiz                = $data['quiz'];
		$quiz_id             = is_object( $quiz ) ? $quiz->ID : $quiz;
		$points              = $data['points'];
		$recipes             = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_points     = Automator()->get->meta_from_recipes( $recipes, 'QUIZPOINT' );
		$required_quiz       = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$required_conditions = Automator()->get->meta_from_recipes( $recipes, 'NUMBERCOND' );
		$matched_recipe_ids  = array();

		if ( empty( $recipes ) ) {
			return;
		}

		foreach ( $recipes as $recipe_id => $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				$trigger_id = $trigger['ID'];
				if ( Automator()->utilities->match_condition_vs_number( $required_conditions[ $recipe_id ][ $trigger_id ], $required_points[ $recipe_id ][ $trigger_id ], $points ) ) {
					$matched_recipe_ids[] = array(
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					);
				}
			}
		}

		if ( empty( $matched_recipe_ids ) ) {
			return;
		}

		foreach ( $matched_recipe_ids as $matched_recipe_id ) {
			//Any Quiz OR a specific quiz
			$r_quiz = (int) $required_quiz[ $matched_recipe_id['recipe_id'] ][ $matched_recipe_id['trigger_id'] ];
			if ( intval( '-1' ) !== intval( $r_quiz ) && (int) $r_quiz !== (int) $quiz_id ) {
				continue;
			}
			$args = array(
				'code'             => $this->trigger_code,
				'meta'             => $this->trigger_meta,
				'user_id'          => $current_user->ID,
				'recipe_to_match'  => $matched_recipe_id['recipe_id'],
				'trigger_to_match' => $matched_recipe_id['trigger_id'],
				'ignore_post_id'   => true,
				'post_id'          => $quiz_id,
			);

			$result = Automator()->maybe_add_trigger_entry( $args, false );
			if ( empty( $result ) ) {
				continue;
			}
			foreach ( $result as $r ) {
				if ( false === $r['result'] ) {
					continue;
				}
				$trigger_id     = (int) $r['args']['trigger_id'];
				$user_id        = (int) $r['args']['user_id'];
				$trigger_log_id = (int) $r['args']['trigger_log_id'];
				$run_number     = (int) $r['args']['run_number'];

				$insert = array(
					'user_id'        => $user_id,
					'trigger_id'     => $trigger_id,
					'trigger_log_id' => $trigger_log_id,
					'meta_key'       => 'LDQUIZ_achieved_points',
					'meta_value'     => $points,
					'run_number'     => $run_number,
				);
				Automator()->insert_trigger_meta( $insert );

				$insert['meta_key']   = 'quiz_id';
				$insert['meta_value'] = $quiz_id;
				Automator()->insert_trigger_meta( $insert );

				Automator()->maybe_trigger_complete( $r['args'] );
			}
		}
	}