gravityflow_step_status_evaluation_approval

The hook gravityflow_step_status_evaluation_approval allows the step status for the approval to be customised. For situations where  the approval policy options of Any (only one approval required) or All (every approver must approve) do not meet your use case, you can use this filter to perform custom approval evaluation logic. 

Parameters

ParameterTypeDetails
$step_statusStringThe status of the step
$approversArrayThe array of Gravity_Flow_Assignee objects
$stepStepThe current step

Examples

Change the approval threshold

This example would change the approval threshold for a specific step to require a minimum number of approvals (2) or a single rejection to complete the step. The step assignee policy should be set to ‘all’ for the logic to apply.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
add_filter( 'gravityflow_step_status_evaluation_approval', 'approval_status_evaluation', 10, 3 );
function approval_status_evaluation( $step_status, $approvers, $step ) {
 
    //Ensure you are only adjusting the desired step
    if ( $step->get_id() !== 58 ) {
        return $step_status;
    }
 
    //Modify this for your minimum number of approvals
    $approval_threshold = 2;
 
    $approval_count = 0;
    $pending_count = 0;
 
    $step_status = 'pending';
 
    foreach ( $approvers as $approver ) {
 
        $approver_status = $approver->get_status();
 
        if ( $approver_status == 'rejected' ) {
            $step_status = 'rejected';
            break;
        }
        if ( $step->assignee_policy == 'all' ) {
            if ( $approver_status == 'approved' ) {
                $approval_count++;
            } else {
                $pending_count++;
            }
        } else if ( empty( $approver_status ) || $approver_status == 'pending' ) {
            $step_status = 'pending';
        }
    }
 
    if( $step_status !== 'rejected') {
        gravity_flow()->log_debug( __METHOD__ . '(): Custom Evaluation Check: ' . $approval_count . ' approved with ' . $pending_count . ' outstanding.');
        if( $approval_count >= $approval_threshold ) {
            $step_status = 'approved';
            gravity_flow()->log_debug( __METHOD__ . '(): Does meet threshold criteria. Step approved.');
        } else {
            $step_status = 'pending';
            gravity_flow()->log_debug( __METHOD__ . '(): Does not meet threshold criteria.');
        }
    }
 
    return $step_status;
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?