Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/07/2014 08:46:32 PM (12 years ago)
Author:
boonebgorges
Message:

Introduce 'group_activity' sort order for group member queries

Adds a new 'Group Activity' option to the 'Order By' dropdown on group member
listings, so that you can see who has most recently been active in that
specific group.

Fixes #3430

Props imath

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-classes.php

    r8200 r8397  
    17081708        }
    17091709
    1710         // For 'last_joined' and 'first_joined' types, we force
    1711         // the order according to the query performed in
    1712         // BP_Group_Member_Query::get_group_members(). Otherwise, fall
    1713         // through and let BP_User_Query do its own ordering.
    1714         if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined' ) ) ) {
     1710        // For 'last_joined', 'first_joined', and 'group_activity'
     1711        // types, we override the default orderby clause of
     1712        // BP_User_Query. In the case of 'group_activity', we perform
     1713        // a separate query to get the necessary order. In the case of
     1714        // 'last_joined' and 'first_joined', we can trust the order of
     1715        // results from  BP_Group_Member_Query::get_group_members().
     1716        // In all other cases, we fall through and let BP_User_Query
     1717        // do its own (non-group-specific) ordering.
     1718        if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined', 'group_activity' ) ) ) {
     1719
     1720            // Group Activity DESC
     1721            if ( 'group_activity' == $query->query_vars['type'] ) {
     1722                $gm_ids = $this->get_gm_ids_ordered_by_activity( $query, $gm_ids );
     1723            }
    17151724
    17161725            // The first param in the FIELD() clause is the sort column id
     
    17661775        // Don't filter other BP_User_Query objects on the same page
    17671776        remove_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
     1777    }
     1778
     1779    /**
     1780     * Sort user IDs by how recently they have generated activity within a given group.
     1781     *
     1782     * @since  BuddyPress (2.1.0)
     1783     *
     1784     * @param BP_User_Query $query BP_User_Query object.
     1785     * @param array $gm_ids array of group member ids.
     1786     * @return array
     1787     */
     1788    public function get_gm_ids_ordered_by_activity( $query, $gm_ids = array() ) {
     1789        global $wpdb;
     1790
     1791        if ( empty( $gm_ids ) ) {
     1792            return $gm_ids;
     1793        }
     1794
     1795        if ( ! bp_is_active( 'activity' ) ) {
     1796            return $gm_ids;
     1797        }
     1798
     1799        $activity_table = buddypress()->activity->table_name;
     1800
     1801        $sql = array(
     1802            'select'  => "SELECT user_id, max( date_recorded ) as date_recorded FROM {$activity_table}",
     1803            'where'   => array(),
     1804            'groupby' => 'GROUP BY user_id',
     1805            'orderby' => 'ORDER BY date_recorded',
     1806            'order'   => 'DESC',
     1807        );
     1808
     1809        $sql['where'] = array(
     1810            'user_id IN (' . implode( ',', wp_parse_id_list( $gm_ids ) ) . ')',
     1811            'item_id = ' . absint( $query->query_vars['group_id'] ),
     1812            $wpdb->prepare( "component = %s", buddypress()->groups->id ),
     1813        );
     1814
     1815        $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] );
     1816
     1817        $group_user_ids = $wpdb->get_results( "{$sql['select']} {$sql['where']} {$sql['groupby']} {$sql['orderby']} {$sql['order']}" );
     1818
     1819        return wp_list_pluck( $group_user_ids, 'user_id' );
    17681820    }
    17691821}
Note: See TracChangeset for help on using the changeset viewer.