Skip to:
Content

BuddyPress.org

Changeset 7948


Ignore:
Timestamp:
02/21/2014 01:50:30 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce support for a 'type' sorting parameter in BP_Group_Member_Query

The 'type' parameter supports any of the arguments ('newest', 'active', etc)
supported by the parent class BP_User_Query. In addition, it's possible to
sort by type 'last_joined' and 'first_joined'.

This changeset also introduces the 'type' parameter through the
bp_group_has_members() function stack.

See #921

Props imath, boonebgorges

Location:
trunk
Files:
4 edited

Legend:

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

    r7939 r7948  
    14371437    public function setup_hooks() {
    14381438        // Take this early opportunity to set the default 'type' param
    1439         // to 'last_modified', which will ensure that BP_User_Query
     1439        // to 'last_joined', which will ensure that BP_User_Query
    14401440        // trusts our order and does not try to apply its own
    14411441        if ( empty( $this->query_vars_raw['type'] ) ) {
    1442             $this->query_vars_raw['type'] = 'last_modified';
     1442            $this->query_vars_raw['type'] = 'last_joined';
    14431443        }
    14441444
     
    14721472            'group_role'   => array( 'member' ),
    14731473            'is_confirmed' => true,
     1474            'type'         => 'last_joined',
    14741475        ) );
    14751476
     
    15091510            'orderby' => '',
    15101511            'order'   => '',
    1511             'limit'   => '',
    15121512        );
    15131513
     
    15721572        $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : '';
    15731573
    1574         /** ORDER BY clause ***************************************************/
    1575 
    1576         // @todo For now, mimicking legacy behavior of
    1577         // bp_group_has_members(), which has us order by date_modified
    1578         // only. Should abstract it in the future
     1574        // We fetch group members in order of last_joined, regardless
     1575        // of 'type'. If the 'type' value is not 'last_joined' or
     1576        // 'first_joined', the order will be overridden in
     1577        // BP_Group_Member_Query::set_orderby()
    15791578        $sql['orderby'] = "ORDER BY date_modified";
    1580         $sql['order']   = "DESC";
    1581 
    1582         /** LIMIT clause ******************************************************/
    1583         $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
     1579        $sql['order']   = 'first_joined' === $this->query_vars['type'] ? 'ASC' : 'DESC';
     1580
     1581        $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" );
     1582
     1583        /**
     1584         * Use this filter to build a custom query (such as when you've
     1585         * defined a custom 'type').
     1586         */
     1587        $this->group_member_ids = apply_filters( 'bp_group_member_query_group_member_ids', $this->group_member_ids, $this );
    15841588
    15851589        return $this->group_member_ids;
     
    15891593     * Tell BP_User_Query to order by the order of our query results.
    15901594     *
    1591      * This implementation assumes the 'last_modified' sort order
    1592      * hardcoded in BP_Group_Member_Query::get_group_member_ids().
     1595     * We only override BP_User_Query's native ordering in case of the
     1596     * 'last_joined' and 'first_joined' $type parameters.
    15931597     *
    15941598     * @param BP_User_Query $query BP_User_Query object.
     
    16001604        }
    16011605
    1602         // The first param in the FIELD() clause is the sort column id
    1603         $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
    1604         $gm_ids_sql = implode( ',', $gm_ids );
    1605 
    1606         $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
     1606        // For 'last_joined' and 'first_joined' types, we force
     1607        // the order according to the query performed in
     1608        // BP_Group_Member_Query::get_group_members(). Otherwise, fall
     1609        // through and let BP_User_Query do its own ordering.
     1610        if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined' ) ) ) {
     1611
     1612            // The first param in the FIELD() clause is the sort column id
     1613            $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
     1614            $gm_ids_sql = implode( ',', $gm_ids );
     1615
     1616            $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
     1617        }
    16071618
    16081619        // Prevent this filter from running on future BP_User_Query
  • trunk/bp-groups/bp-groups-functions.php

    r7932 r7948  
    385385 *           Default: 1.
    386386 *     @type array $group_role Optional. Array of group roles to include.
     387 *     @type string $search_terms Optional. Filter results by a search string.
     388 *     @type string $type Optional. Sort the order of results. 'last_joined',
     389 *           'first_joined', or any of the $type params available in
     390 *           {@link BP_User_Query}. Default: 'last_joined'.
    387391 * }
    388  * @param int $group_id
    389  * @param int $limit Maximum members to return
    390  * @param int $page The page of results to return (requires $limit)
    391  * @param bool $exclude_admins_mods Whether to exclude admins and mods
    392  * @param bool $exclude_banned Whether to exclude banned users
    393  * @param array|string $exclude Array or comma-sep list of users to exclude
    394  * @return array Multi-d array of 'members' list and 'count'
     392 * @return array Multi-d array of 'members' list and 'count'.
    395393 */
    396394function groups_get_group_members( $args = array() ) {
     
    423421        'group_role'          => array(),
    424422        'search_terms'        => false,
     423        'type'                => 'last_joined',
    425424    ) );
    426425
     
    456455            'exclude'        => $r['exclude'],
    457456            'search_terms'   => $r['search_terms'],
    458             'type'           => 'last_modified',
     457            'type'           => $r['type'],
    459458        ) );
    460459
  • trunk/bp-groups/bp-groups-template.php

    r7947 r7948  
    20202020            'group_role'          => false,
    20212021            'search_terms'        => false,
     2022            'type'                => 'last_joined',
    20222023        ) );
    20232024
     
    21232124 *           Default: 1.
    21242125 *     @type array $group_role Optional. Array of group roles to include.
     2126 *     @type string $type Optional. Sort order of results. 'last_joined',
     2127 *           'first_joined', or any of the $type params available in
     2128 *           {@link BP_User_Query}. Default: 'last_joined'.
    21252129 *     @type string $search_terms Optional. Search terms to match.
    21262130 * }
     
    21392143        'group_role'          => false,
    21402144        'search_terms'        => false,
     2145        'type'                => 'last_joined',
    21412146    ) );
    21422147
  • trunk/tests/testcases/groups/class-bp-group-member-query.php

    r7505 r7948  
    335335    }
    336336
     337    /**
     338     * @group type
     339     */
     340    public function test_get_with_type_last_joined() {
     341        $g = $this->factory->group->create();
     342        $u1 = $this->create_user();
     343        $u2 = $this->create_user();
     344        $time = time();
     345
     346        $this->add_user_to_group( $u1, $g, array(
     347            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
     348        ) );
     349
     350        $this->add_user_to_group( $u2, $g, array(
     351            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     352        ) );
     353
     354        $query_members = new BP_Group_Member_Query( array(
     355            'group_id' => $g,
     356            'type' => 'last_joined',
     357        ) );
     358
     359        $ids = wp_parse_id_list( array_keys( $query_members->results ) );
     360        $this->assertEquals( array( $u2, $u1 ), $ids );
     361    }
     362
     363    /**
     364     * @group type
     365     */
     366    public function test_get_with_type_first_joined() {
     367        $g = $this->factory->group->create();
     368        $u1 = $this->create_user();
     369        $u2 = $this->create_user();
     370        $time = time();
     371
     372        $this->add_user_to_group( $u1, $g, array(
     373            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
     374        ) );
     375
     376        $this->add_user_to_group( $u2, $g, array(
     377            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     378        ) );
     379
     380        $query_members = new BP_Group_Member_Query( array(
     381            'group_id' => $g,
     382            'type' => 'first_joined',
     383        ) );
     384
     385        $ids = wp_parse_id_list( array_keys( $query_members->results ) );
     386        $this->assertEquals( array( $u1, $u2 ), $ids );
     387    }
     388
     389    /**
     390     * @group type
     391     */
     392    public function test_get_with_type_alphabetical() {
     393        $g = $this->factory->group->create();
     394        $u1 = $this->create_user( array(
     395            'display_name' => 'AAA',
     396        ) );
     397        $u2 = $this->create_user( array(
     398            'display_name' => 'CCC',
     399        ) );
     400        $u3 = $this->create_user( array(
     401            'display_name' => 'BBB',
     402        ) );
     403        $time = time();
     404
     405        $this->add_user_to_group( $u1, $g, array(
     406            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     407        ) );
     408
     409        $this->add_user_to_group( $u2, $g, array(
     410            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
     411        ) );
     412
     413        $this->add_user_to_group( $u3, $g, array(
     414            'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
     415        ) );
     416
     417        $query_members = new BP_Group_Member_Query( array(
     418            'group_id' => $g,
     419            'type' => 'alphabetical',
     420        ) );
     421
     422        $ids = wp_parse_id_list( array_keys( $query_members->results ) );
     423        $this->assertEquals( array( $u1, $u3, $u2 ), $ids );
     424    }
    337425}
Note: See TracChangeset for help on using the changeset viewer.