Automator_Functions::are_recipes_completed( null $user_id = null,  $recipe_ids = array() )

Check if the recipe was completed


Parameters Parameters

$user_id

(null) (Optional)

Default value: null

$recipe_ids

(Optional)

Default value: array()


Top ↑

Return Return

(array)


Source Source

File: src/core/lib/class-automator-functions.php

	public function are_recipes_completed( $user_id = null, $recipe_ids = array() ) {

		if ( empty( $recipe_ids ) ) {
			Automator()->error->trigger( 'You are trying to check if a recipe is completed without providing a recipe_ids.' );

			return null;
		}

		// Set user ID
		if ( null === $user_id ) {
			$user_id = get_current_user_id();
		}

		// No user id is available.
		if ( 0 === $user_id ) {
			Automator()->error->trigger( 'You are trying to check if a recipe is completed when a there is no logged in user.' );

			return null;
		}

		$completed = array();
		global $wpdb;
		$results = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT COUNT(completed) AS completed, automator_recipe_id
					FROM {$wpdb->prefix}uap_recipe_log
WHERE user_id = %d AND automator_recipe_id IN (" . join( ',', $recipe_ids ) . ")
  AND completed = 1
  GROUP BY automator_recipe_id", $user_id )
		);

		if ( $results ) {
			foreach ( $recipe_ids as $recipe_id ) {
				$complete = 0;
				$found    = false;
				foreach ( $results as $r ) {
					if ( $recipe_id === (int) $r->automator_recipe_id ) {
						$found    = true;
						$complete = $r->completed;
						break;
					} else {
						$found = false;
					}
				}

				if ( $found ) {
					$completed[ $recipe_id ] = $complete;
				} else {
					$completed[ $recipe_id ] = 0;
				}
			}
		} else {
			//Fallback to mark every recipe incomplete
			foreach ( $recipe_ids as $recipe_id ) {
				$completed[ $recipe_id ] = 0;
			}
		}

		return $this->utilities->recipes_number_times_completed( $recipe_ids, $completed );
	}