From 9ea44532c2a2da9519ca377ef2c82a88df040efa Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 24 Oct 2019 15:31:15 -0700 Subject: [PATCH] Plugin: Add functions for handling xprofile data --- plugins/wporg-5ftf/includes/xprofile.php | 84 ++++++++++++++++++++++++ plugins/wporg-5ftf/index.php | 1 + 2 files changed, 85 insertions(+) create mode 100644 plugins/wporg-5ftf/includes/xprofile.php diff --git a/plugins/wporg-5ftf/includes/xprofile.php b/plugins/wporg-5ftf/includes/xprofile.php new file mode 100644 index 0000000..449e494 --- /dev/null +++ b/plugins/wporg-5ftf/includes/xprofile.php @@ -0,0 +1,84 @@ +prepare( + ' + SELECT user_id, field_id, value + FROM bpmain_bp_xprofile_data + WHERE user_id IN ( %1$s ) + AND field_id IN ( %2$s ) + ', + implode( ', ', array_map( 'absint', $user_ids ) ), + implode( ', ', array_map( 'absint', $field_ids ) ) + ); + + return $wpdb->get_results( $sql, ARRAY_A ); +} + +/** + * Aggregate the raw xprofile data for all contributors linked to a given pledge. + * + * @param int $pledge_id + * + * @return array + */ +function get_aggregate_contributor_data_for_pledge( $pledge_id ) { + $contributors = Contributor\get_contributor_user_objects( + Contributor\get_pledge_contributors( $pledge_id, 'pending' ) // TODO set to 'publish' when finished testing + ); + $user_ids = wp_list_pluck( $contributors, 'ID' ); + + $data = get_xprofile_contribution_data( $user_ids ); + + $initial = array( + 'contributors' => count( $user_ids ), + 'hours' => 0, + 'teams' => array(), + ); + + $aggregate_data = array_reduce( $data, function( $carry, $item ) { + switch( $item['field_id'] ) { + case 29: // Hours. + $carry['hours'] += absint( $item['value'] ); + break; + + case 30: // Teams. + $value = maybe_unserialize( $item['value'] ); + $carry['teams'] = array_merge( $carry['teams'], $value ); + break; + } + + return $carry; + }, $initial ); + + $aggregate_data['teams'] = array_unique( $aggregate_data['teams'] ); + sort( $aggregate_data['teams'] ); + + return $aggregate_data; +} diff --git a/plugins/wporg-5ftf/index.php b/plugins/wporg-5ftf/index.php index 69a239e..9a776ee 100755 --- a/plugins/wporg-5ftf/index.php +++ b/plugins/wporg-5ftf/index.php @@ -28,6 +28,7 @@ function load() { require_once get_includes_path() . 'pledge-meta.php'; require_once get_includes_path() . 'pledge-form.php'; require_once get_includes_path() . 'directory.php'; + require_once get_includes_path() . 'xprofile.php'; } /**