Skip to:
Content

BuddyPress.org

Changeset 8397


Ignore:
Timestamp:
05/07/2014 08:46:32 PM (11 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

Location:
trunk
Files:
3 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}
  • trunk/src/bp-groups/bp-groups-template.php

    r8329 r8397  
    24382438            <option value="last_joined"><?php _e( 'Newest', 'buddypress' ); ?></option>
    24392439            <option value="first_joined"><?php _e( 'Oldest', 'buddypress' ); ?></option>
     2440
     2441            <?php if ( bp_is_active( 'activity' ) ) : ?>
     2442                <option value="group_activity"><?php _e( 'Group Activity', 'buddypress' ); ?></option>
     2443            <?php endif; ?>
     2444
    24402445            <option value="alphabetical"><?php _e( 'Alphabetical', 'buddypress' ); ?></option>
    24412446
  • trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php

    r8113 r8397  
    403403    /**
    404404     * @group type
     405     * @group group_activity
     406     */
     407    public function test_get_with_type_group_activity_with_activity_component_disabled() {
     408        $g = $this->factory->group->create();
     409        $u1 = $this->create_user();
     410        $u2 = $this->create_user();
     411        $u3 = $this->create_user();
     412        $c = buddypress()->groups->id;
     413        $time = time();
     414
     415        $this->add_user_to_group( $u1, $g, array(
     416            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
     417        ) );
     418
     419        $this->add_user_to_group( $u2, $g, array(
     420            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 400 ),
     421        ) );
     422
     423        $this->add_user_to_group( $u3, $g, array(
     424            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
     425        ) );
     426
     427        $this->factory->activity->create( array(
     428            'component' => $c,
     429            'type' => 'activity_update',
     430            'user_id' => $u3,
     431            'item_id' => $g,
     432            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
     433        ) );
     434
     435        $this->factory->activity->create( array(
     436            'component' => $c,
     437            'type' => 'activity_update',
     438            'user_id' => $u1,
     439            'item_id' => $g,
     440            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
     441        ) );
     442
     443        $this->factory->activity->create( array(
     444            'component' => $c,
     445            'type' => 'activity_update',
     446            'user_id' => $u2,
     447            'item_id' => $g,
     448            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     449        ) );
     450
     451        // Deactivate activity component
     452        $activity_active = isset( buddypress()->active_components['activity'] );
     453        if ( $activity_active ) {
     454            unset( buddypress()->active_components['activity'] );
     455        }
     456
     457        $query_members1 = new BP_Group_Member_Query( array(
     458            'group_id' => $g,
     459            'type' => 'group_activity',
     460        ) );
     461
     462        $query_members2 = new BP_Group_Member_Query( array(
     463            'group_id' => $g,
     464            'type' => 'last_joined',
     465        ) );
     466
     467        if ( $activity_active ) {
     468            buddypress()->active_components['activity'] = '1';
     469        }
     470
     471        $this->assertSame( wp_list_pluck( $query_members2->results, 'ID' ), wp_list_pluck( $query_members1->results, 'ID' ) );
     472    }
     473
     474    /**
     475     * @group type
     476     * @group group_activity
     477     */
     478    public function test_get_with_type_group_activity() {
     479        $g = $this->factory->group->create();
     480        $u1 = $this->create_user();
     481        $u2 = $this->create_user();
     482        $u3 = $this->create_user();
     483        $c = buddypress()->groups->id;
     484        $time = time();
     485
     486        $this->add_user_to_group( $u1, $g, array(
     487            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
     488        ) );
     489
     490        $this->add_user_to_group( $u2, $g, array(
     491            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 400 ),
     492        ) );
     493
     494        $this->add_user_to_group( $u3, $g, array(
     495            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
     496        ) );
     497
     498        $this->factory->activity->create( array(
     499            'component' => $c,
     500            'type' => 'activity_update',
     501            'user_id' => $u3,
     502            'item_id' => $g,
     503            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
     504        ) );
     505
     506        $this->factory->activity->create( array(
     507            'component' => $c,
     508            'type' => 'activity_update',
     509            'user_id' => $u1,
     510            'item_id' => $g,
     511            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
     512        ) );
     513
     514        $this->factory->activity->create( array(
     515            'component' => $c,
     516            'type' => 'activity_update',
     517            'user_id' => $u2,
     518            'item_id' => $g,
     519            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     520        ) );
     521
     522        $query_members = new BP_Group_Member_Query( array(
     523            'group_id' => $g,
     524            'type' => 'group_activity',
     525        ) );
     526
     527        $ids = wp_parse_id_list( array_keys( $query_members->results ) );
     528        $this->assertEquals( array( $u2, $u1, $u3 ), $ids );
     529    }
     530
     531    /**
     532     * @group type
     533     * @group group_activity
     534     */
     535    public function test_get_with_type_group_activity_no_dupes() {
     536        $g = $this->factory->group->create();
     537        $u1 = $this->create_user();
     538        $c = buddypress()->groups->id;
     539        $time = time();
     540
     541        $this->add_user_to_group( $u1, $g, array(
     542            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
     543        ) );
     544
     545        $this->factory->activity->create( array(
     546            'component' => $c,
     547            'type' => 'activity_update',
     548            'user_id' => $u1,
     549            'item_id' => $g,
     550            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
     551        ) );
     552
     553        $this->factory->activity->create( array(
     554            'component' => $c,
     555            'type' => 'activity_update',
     556            'user_id' => $u1,
     557            'item_id' => $g,
     558            'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
     559        ) );
     560
     561        $query_members = new BP_Group_Member_Query( array(
     562            'group_id' => $g,
     563            'type' => 'group_activity',
     564        ) );
     565
     566        $ids = wp_parse_id_list( array_keys( $query_members->results ) );
     567        $this->assertEquals( array( $u1, ), $ids );
     568    }
     569    /**
     570     * @group type
    405571     */
    406572    public function test_get_with_type_alphabetical() {
Note: See TracChangeset for help on using the changeset viewer.