UOG_REMOVESEATSFROMGROUP::remove_seats_from_a_group( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the trigger action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)


Source Source

File: src/integrations/uncanny-groups/actions/uog-removeseatsfromgroup.php

	public function remove_seats_from_a_group( $user_id, $action_data, $recipe_id, $args ) {
		global $wpdb;

		$uo_group_id = Automator()->parse->text( $action_data['meta']['UNCANNYGROUP'], $recipe_id, $user_id, $args );
		$check_group = learndash_validate_groups( array( $uo_group_id ) );
		if ( empty( $check_group ) || ! is_array( $check_group ) ) {
			$error_message                       = esc_html__( 'The selected group is not found.', 'uncanny-automator' );
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}
		$uo_remove_seats = absint( Automator()->parse->text( $action_data['meta']['NUMOFSEATS'], $recipe_id, $user_id, $args ) );

		$code_group_id = ulgm()->group_management->seat->get_code_group_id( $uo_group_id );
		if ( empty( $code_group_id ) ) {
			$error_message                       = __( 'Group management is not enabled on the selected group.', 'uncanny-automator' );
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}
		$existing_seats = ulgm()->group_management->seat->total_seats( $uo_group_id );
		$empty_seats    = ulgm()->group_management->seat->available_seats( $uo_group_id );
		if ( empty( $empty_seats ) ) {
			$error_message                       = __( 'No empty seats in the selected group.', 'uncanny-automator' );
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}
		// Seats removed
		$tbl = SharedFunctions::$db_group_codes_tbl;

		// If seats to remove are less than empty seats
		if ( $uo_remove_seats < $empty_seats ) {
			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->prefix{$tbl} WHERE group_id = %d AND student_id IS NULL LIMIT %d", $code_group_id, $uo_remove_seats ) ); //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			update_post_meta( $uo_group_id, '_ulgm_total_seats', $empty_seats );
			Automator()->complete_action( $user_id, $action_data, $recipe_id );

			return;
		}
		// if seats to remove are more than empty seats
		if ( $uo_remove_seats >= $empty_seats ) {
			$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->prefix{$tbl} WHERE group_id = %d AND student_id IS NULL", $code_group_id ) );  //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			update_post_meta( $uo_group_id, '_ulgm_total_seats', $existing_seats - $uo_remove_seats );
		}

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