<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][7948] trunk: Introduce support for a 'type' sorting parameter in BP_Group_Member_Query</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7948">7948</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-21 13:50:30 +0000 (Fri, 21 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/921">#921</a>

Props imath, boonebgorges</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsfunctionsphp">trunk/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatephp">trunk/bp-groups/bp-groups-template.php</a></li>
<li><a href="#trunkteststestcasesgroupsclassbpgroupmemberqueryphp">trunk/tests/testcases/groups/class-bp-group-member-query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-classes.php (7947 => 7948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php    2014-02-21 13:28:13 UTC (rev 7947)
+++ trunk/bp-groups/bp-groups-classes.php       2014-02-21 13:50:30 UTC (rev 7948)
</span><span class="lines">@@ -1436,10 +1436,10 @@
</span><span class="cx">   */
</span><span class="cx">  public function setup_hooks() {
</span><span class="cx">          // Take this early opportunity to set the default 'type' param
</span><del>-               // to 'last_modified', which will ensure that BP_User_Query
</del><ins>+                // to 'last_joined', which will ensure that BP_User_Query
</ins><span class="cx">           // trusts our order and does not try to apply its own
</span><span class="cx">          if ( empty( $this->query_vars_raw['type'] ) ) {
</span><del>-                       $this->query_vars_raw['type'] = 'last_modified';
</del><ins>+                        $this->query_vars_raw['type'] = 'last_joined';
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Set the sort order
</span><span class="lines">@@ -1471,6 +1471,7 @@
</span><span class="cx">                  'group_id'     => 0,
</span><span class="cx">                  'group_role'   => array( 'member' ),
</span><span class="cx">                  'is_confirmed' => true,
</span><ins>+                       'type'         => 'last_joined',
</ins><span class="cx">           ) );
</span><span class="cx"> 
</span><span class="cx">          $group_member_ids = $this->get_group_member_ids();
</span><span class="lines">@@ -1508,7 +1509,6 @@
</span><span class="cx">                  'where'   => array(),
</span><span class="cx">                  'orderby' => '',
</span><span class="cx">                  'order'   => '',
</span><del>-                       'limit'   => '',
</del><span class="cx">           );
</span><span class="cx"> 
</span><span class="cx">          /** WHERE clauses *****************************************************/
</span><span class="lines">@@ -1571,25 +1571,29 @@
</span><span class="cx"> 
</span><span class="cx">          $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : '';
</span><span class="cx"> 
</span><del>-               /** ORDER BY clause ***************************************************/
-
-               // @todo For now, mimicking legacy behavior of
-               // bp_group_has_members(), which has us order by date_modified
-               // only. Should abstract it in the future
</del><ins>+                // We fetch group members in order of last_joined, regardless
+               // of 'type'. If the 'type' value is not 'last_joined' or
+               // 'first_joined', the order will be overridden in
+               // BP_Group_Member_Query::set_orderby()
</ins><span class="cx">           $sql['orderby'] = "ORDER BY date_modified";
</span><del>-               $sql['order']   = "DESC";
</del><ins>+                $sql['order']   = 'first_joined' === $this->query_vars['type'] ? 'ASC' : 'DESC';
</ins><span class="cx"> 
</span><del>-               /** LIMIT clause ******************************************************/
-               $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
</del><ins>+                $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" );
</ins><span class="cx"> 
</span><ins>+               /**
+                * Use this filter to build a custom query (such as when you've
+                * defined a custom 'type').
+                */
+               $this->group_member_ids = apply_filters( 'bp_group_member_query_group_member_ids', $this->group_member_ids, $this );
+
</ins><span class="cx">           return $this->group_member_ids;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * Tell BP_User_Query to order by the order of our query results.
</span><span class="cx">   *
</span><del>-        * This implementation assumes the 'last_modified' sort order
-        * hardcoded in BP_Group_Member_Query::get_group_member_ids().
</del><ins>+         * We only override BP_User_Query's native ordering in case of the
+        * 'last_joined' and 'first_joined' $type parameters.
</ins><span class="cx">    *
</span><span class="cx">   * @param BP_User_Query $query BP_User_Query object.
</span><span class="cx">   */
</span><span class="lines">@@ -1599,12 +1603,19 @@
</span><span class="cx">                  $gm_ids = array( 0 );
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // The first param in the FIELD() clause is the sort column id
-               $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
-               $gm_ids_sql = implode( ',', $gm_ids );
</del><ins>+                // For 'last_joined' and 'first_joined' types, we force
+               // the order according to the query performed in
+               // BP_Group_Member_Query::get_group_members(). Otherwise, fall
+               // through and let BP_User_Query do its own ordering.
+               if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined' ) ) ) {
</ins><span class="cx"> 
</span><del>-               $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
</del><ins>+                        // The first param in the FIELD() clause is the sort column id
+                       $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
+                       $gm_ids_sql = implode( ',', $gm_ids );
</ins><span class="cx"> 
</span><ins>+                       $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
+               }
+
</ins><span class="cx">           // Prevent this filter from running on future BP_User_Query
</span><span class="cx">          // instances on the same page
</span><span class="cx">          remove_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-functions.php (7947 => 7948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-functions.php  2014-02-21 13:28:13 UTC (rev 7947)
+++ trunk/bp-groups/bp-groups-functions.php     2014-02-21 13:50:30 UTC (rev 7948)
</span><span class="lines">@@ -384,14 +384,12 @@
</span><span class="cx">  *     @type bool|int True (or 1) to exclude banned users from results.
</span><span class="cx">  *           Default: 1.
</span><span class="cx">  *     @type array $group_role Optional. Array of group roles to include.
</span><ins>+ *     @type string $search_terms Optional. Filter results by a search string.
+ *     @type string $type Optional. Sort the order of results. 'last_joined',
+ *           'first_joined', or any of the $type params available in
+ *           {@link BP_User_Query}. Default: 'last_joined'.
</ins><span class="cx">  * }
</span><del>- * @param int $group_id
- * @param int $limit Maximum members to return
- * @param int $page The page of results to return (requires $limit)
- * @param bool $exclude_admins_mods Whether to exclude admins and mods
- * @param bool $exclude_banned Whether to exclude banned users
- * @param array|string $exclude Array or comma-sep list of users to exclude
- * @return array Multi-d array of 'members' list and 'count'
</del><ins>+ * @return array Multi-d array of 'members' list and 'count'.
</ins><span class="cx">  */
</span><span class="cx"> function groups_get_group_members( $args = array() ) {
</span><span class="cx"> 
</span><span class="lines">@@ -422,6 +420,7 @@
</span><span class="cx">          'exclude'             => false,
</span><span class="cx">          'group_role'          => array(),
</span><span class="cx">          'search_terms'        => false,
</span><ins>+               'type'                => 'last_joined',
</ins><span class="cx">   ) );
</span><span class="cx"> 
</span><span class="cx">  // For legacy users. Use of BP_Groups_Member::get_all_for_group()
</span><span class="lines">@@ -455,7 +454,7 @@
</span><span class="cx">                  'group_role'     => $r['group_role'],
</span><span class="cx">                  'exclude'        => $r['exclude'],
</span><span class="cx">                  'search_terms'   => $r['search_terms'],
</span><del>-                       'type'           => 'last_modified',
</del><ins>+                        'type'           => $r['type'],
</ins><span class="cx">           ) );
</span><span class="cx"> 
</span><span class="cx">          // Structure the return value as expected by the template functions
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (7947 => 7948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php   2014-02-21 13:28:13 UTC (rev 7947)
+++ trunk/bp-groups/bp-groups-template.php      2014-02-21 13:50:30 UTC (rev 7948)
</span><span class="lines">@@ -2019,6 +2019,7 @@
</span><span class="cx">                  'exclude_banned'      => 1,
</span><span class="cx">                  'group_role'          => false,
</span><span class="cx">                  'search_terms'        => false,
</span><ins>+                       'type'                => 'last_joined',
</ins><span class="cx">           ) );
</span><span class="cx"> 
</span><span class="cx">          // @todo No
</span><span class="lines">@@ -2122,6 +2123,9 @@
</span><span class="cx">  *     @type bool|int True (or 1) to exclude banned users from results.
</span><span class="cx">  *           Default: 1.
</span><span class="cx">  *     @type array $group_role Optional. Array of group roles to include.
</span><ins>+ *     @type string $type Optional. Sort order of results. 'last_joined',
+ *           'first_joined', or any of the $type params available in
+ *           {@link BP_User_Query}. Default: 'last_joined'.
</ins><span class="cx">  *     @type string $search_terms Optional. Search terms to match.
</span><span class="cx">  * }
</span><span class="cx">  */
</span><span class="lines">@@ -2138,6 +2142,7 @@
</span><span class="cx">          'exclude_banned'      => 1,
</span><span class="cx">          'group_role'          => false,
</span><span class="cx">          'search_terms'        => false,
</span><ins>+               'type'                => 'last_joined',
</ins><span class="cx">   ) );
</span><span class="cx"> 
</span><span class="cx">  if ( empty( $r['search_terms'] ) && ! empty( $_REQUEST['s'] ) )
</span></span></pre></div>
<a id="trunkteststestcasesgroupsclassbpgroupmemberqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/groups/class-bp-group-member-query.php (7947 => 7948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/class-bp-group-member-query.php     2014-02-21 13:28:13 UTC (rev 7947)
+++ trunk/tests/testcases/groups/class-bp-group-member-query.php        2014-02-21 13:50:30 UTC (rev 7948)
</span><span class="lines">@@ -334,4 +334,92 @@
</span><span class="cx">          $this->assertEquals( array( $u2 ), $ids );
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       /**
+        * @group type
+        */
+       public function test_get_with_type_last_joined() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user();
+               $u2 = $this->create_user();
+               $time = time();
+
+               $this->add_user_to_group( $u1, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
+               ) );
+
+               $this->add_user_to_group( $u2, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+               ) );
+
+               $query_members = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'last_joined',
+               ) );
+
+               $ids = wp_parse_id_list( array_keys( $query_members->results ) );
+               $this->assertEquals( array( $u2, $u1 ), $ids );
+       }
+
+       /**
+        * @group type
+        */
+       public function test_get_with_type_first_joined() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user();
+               $u2 = $this->create_user();
+               $time = time();
+
+               $this->add_user_to_group( $u1, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
+               ) );
+
+               $this->add_user_to_group( $u2, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+               ) );
+
+               $query_members = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'first_joined',
+               ) );
+
+               $ids = wp_parse_id_list( array_keys( $query_members->results ) );
+               $this->assertEquals( array( $u1, $u2 ), $ids );
+       }
+
+       /**
+        * @group type
+        */
+       public function test_get_with_type_alphabetical() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user( array(
+                       'display_name' => 'AAA',
+               ) );
+               $u2 = $this->create_user( array(
+                       'display_name' => 'CCC',
+               ) );
+               $u3 = $this->create_user( array(
+                       'display_name' => 'BBB',
+               ) );
+               $time = time();
+
+               $this->add_user_to_group( $u1, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+               ) );
+
+               $this->add_user_to_group( $u2, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
+               ) );
+
+               $this->add_user_to_group( $u3, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
+               ) );
+
+               $query_members = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'alphabetical',
+               ) );
+
+               $ids = wp_parse_id_list( array_keys( $query_members->results ) );
+               $this->assertEquals( array( $u1, $u3, $u2 ), $ids );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>