Preserve the "sub-step" status in Gravity Flow when one or more assignees are changed in GravityView

When using GravityView with Gravity Flow, you may encounter a situation where you edit an assignee field on an entry with GravityView which does not reflect in the Gravity Flow workflow "User Input" step. 

The snippet below (kindly provided by Rob Davis) extends Gravity Flow user input steps so that when there is more than one assignee, the "sub-step" status may be preserved when one or more assignees are changed in GravityView.

Here's an example of the issue in question:

1. A user (such as a manager) assigns three site users (such as reviewers) to evaluate an entry in a Gravity Flow user input step.

2. Two of the assigned users complete the task quickly, but the third is slow or does not respond in a timely manner. 

3. The manager no longer has the entry in their inbox since they assigned it to the three reviewers in the user input step. Fortunately, with this snippet, they may edit the entry in GravityView, change the third assignee to someone else, and update the entry.

4. Without the snippet, the assignee would change in the Gravity Forms entry, but it would not affect the Gravity Flow workflow. The manager or a site admin would need to restart the step, and even after doing so, the fact that the first two reviewers already completed their portion of the user input step would not be preserved. Their status would go from complete back to pending, and they would need to resubmit their user input even if no changes were needed.

With the below snippet, editing and updating the entry in GravityView preserves the completed status of the first two reviewers, and refreshes the entry to recognize the new assignee! 

/** 
 * Purpose: When a Gravity Flow user input step has > 1 assignee, it is sometimes necessary to change one or more assignees.
 */
 
add_filter( 'gravityflow_step_settings_fields', 'custom_filter_example_notification_fields', 10, 2 );
function custom_filter_example_notification_fields( $settings, $current_step_id ) {
	foreach ( $settings as $key => $setting ) {
		if ( isset( $setting['id'] ) && $setting['id'] == 'gravityflow-step-settings-user_input' ) {
			$settings[ $key ]['fields'][] = array(
				'name'    => 'enable_users_previous_status_fields',
				'label'   => esc_html__( 'Keep User Previous Status (Complete)', 'gravityflow' ),
				'type'    => 'checkbox',
				'choices' => array(
					array(
						'label'         => esc_html__( 'Enable field to keep previous status', 'gravityflow' ),
						'name'          => 'enable_users_previous_status_fields',
						'default_value' => '0',
					),
				),
			);
		}
	}

	return $settings;
}

add_filter( 'gravityflow_settings_user_input', 'custom_gravityflow_settings_user_input', 10, 2 );
function custom_gravityflow_settings_user_input( $settings, $settings_api ) {

	$settings['fields'][] = array(
		'name'    => 'enable_users_previous_status_fields',
		'label'   => esc_html__( 'Keep User Previous Status (Complete)', 'gravityflow' ),
		'type'    => 'checkbox',
		'choices' => array(
			array(
				'label'         => esc_html__( 'Enable field to keep previous status', 'gravityflow' ),
				'name'          => 'enable_users_previous_status_fields',
				'default_value' => '0',
			),
		),
	);

	return $settings;
}

add_action( 'gform_after_update_entry', 'gf_gform_after_update_entry_custom_status', 10, 2 );
function gf_gform_after_update_entry_custom_status( $form, $entry_id ) {

	$cssClass = $form['cssClass'];

	if ( $cssClass == "entry_reviews" ) {

		$api   = new Gravity_Flow_API( $form['id'] );
		$entry = GFAPI::get_entry( $entry_id );

		$step = $api->get_current_step( $entry );


		if ( $step && $step->get_type() == "user_input" && ( isset( $step->enable_users_previous_status_fields ) && $step->enable_users_previous_status_fields === '1' ) ) {


			$previous_assignees = $step && $step instanceof Gravity_Flow_Step ?
				$step->get_assignees() : array();

			$previous_data = [];
			foreach ( $previous_assignees as $assignee ) {
				if ( $assignee->get_status() == "complete" ) {
					$previous_data[ $assignee->get_id() ] = $assignee->get_status();
				} else {
					$previous_data[ $assignee->get_id() ] = 'pending';
				}
			}

			$api->send_to_step( $entry, $step->get_id() );

			$step               = $api->get_current_step( $entry );
			$previous_assignees = $step && $step instanceof Gravity_Flow_Step ?
				$step->get_assignees() : array();
			$counter            = 0;
			foreach ( $previous_assignees as $assignee ) {
				if ( isset( $previous_data[ $assignee->get_id() ] ) ) {
					$assignee->process_status( $previous_data[ $assignee->get_id() ] );
				}

			}

			$step->refresh_entry();
		}
	}

}

Read here how to add these code samples to your website: Where to put code samples.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us