<!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][8397] trunk: Introduce 'group_activity' sort order for group member queries</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/8397">8397</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-05-07 20:46:32 +0000 (Wed, 07 May 2014)</dd>
</dl>

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

Props imath</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpgroupsbpgroupsclassesphp">trunk/src/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunksrcbpgroupsbpgroupstemplatephp">trunk/src/bp-groups/bp-groups-template.php</a></li>
<li><a href="#trunktestsphpunittestcasesgroupsclassbpgroupmemberqueryphp">trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-groups/bp-groups-classes.php (8396 => 8397)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-groups/bp-groups-classes.php        2014-05-07 13:32:35 UTC (rev 8396)
+++ trunk/src/bp-groups/bp-groups-classes.php   2014-05-07 20:46:32 UTC (rev 8397)
</span><span class="lines">@@ -1707,12 +1707,21 @@
</span><span class="cx">                  $gm_ids = array( 0 );
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // 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' ) ) ) {
</del><ins>+                // For 'last_joined', 'first_joined', and 'group_activity'
+               // types, we override the default orderby clause of
+               // BP_User_Query. In the case of 'group_activity', we perform
+               // a separate query to get the necessary order. In the case of
+               // 'last_joined' and 'first_joined', we can trust the order of
+               // results from  BP_Group_Member_Query::get_group_members().
+               // In all other cases, we fall through and let BP_User_Query
+               // do its own (non-group-specific) ordering.
+               if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined', 'group_activity' ) ) ) {
</ins><span class="cx"> 
</span><ins>+                       // Group Activity DESC
+                       if ( 'group_activity' == $query->query_vars['type'] ) {
+                               $gm_ids = $this->get_gm_ids_ordered_by_activity( $query, $gm_ids );
+                       }
+
</ins><span class="cx">                   // The first param in the FIELD() clause is the sort column id
</span><span class="cx">                  $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
</span><span class="cx">                  $gm_ids_sql = implode( ',', $gm_ids );
</span><span class="lines">@@ -1766,6 +1775,49 @@
</span><span class="cx">          // Don't filter other BP_User_Query objects on the same page
</span><span class="cx">          remove_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
</span><span class="cx">  }
</span><ins>+
+       /**
+        * Sort user IDs by how recently they have generated activity within a given group.
+        *
+        * @since  BuddyPress (2.1.0)
+        *
+        * @param BP_User_Query $query BP_User_Query object.
+        * @param array $gm_ids array of group member ids.
+        * @return array
+        */
+       public function get_gm_ids_ordered_by_activity( $query, $gm_ids = array() ) {
+               global $wpdb;
+
+               if ( empty( $gm_ids ) ) {
+                       return $gm_ids;
+               }
+
+               if ( ! bp_is_active( 'activity' ) ) {
+                       return $gm_ids;
+               }
+
+               $activity_table = buddypress()->activity->table_name;
+
+               $sql = array(
+                       'select'  => "SELECT user_id, max( date_recorded ) as date_recorded FROM {$activity_table}",
+                       'where'   => array(),
+                       'groupby' => 'GROUP BY user_id',
+                       'orderby' => 'ORDER BY date_recorded',
+                       'order'   => 'DESC',
+               );
+
+               $sql['where'] = array(
+                       'user_id IN (' . implode( ',', wp_parse_id_list( $gm_ids ) ) . ')',
+                       'item_id = ' . absint( $query->query_vars['group_id'] ),
+                       $wpdb->prepare( "component = %s", buddypress()->groups->id ),
+               );
+
+               $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] );
+
+               $group_user_ids = $wpdb->get_results( "{$sql['select']} {$sql['where']} {$sql['groupby']} {$sql['orderby']} {$sql['order']}" );
+
+               return wp_list_pluck( $group_user_ids, 'user_id' );
+       }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunksrcbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-groups/bp-groups-template.php (8396 => 8397)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-groups/bp-groups-template.php       2014-05-07 13:32:35 UTC (rev 8396)
+++ trunk/src/bp-groups/bp-groups-template.php  2014-05-07 20:46:32 UTC (rev 8397)
</span><span class="lines">@@ -2437,6 +2437,11 @@
</span><span class="cx">          <select id="group_members-order-by">
</span><span class="cx">                  <option value="last_joined"><?php _e( 'Newest', 'buddypress' ); ?></option>
</span><span class="cx">                  <option value="first_joined"><?php _e( 'Oldest', 'buddypress' ); ?></option>
</span><ins>+
+                       <?php if ( bp_is_active( 'activity' ) ) : ?>
+                               <option value="group_activity"><?php _e( 'Group Activity', 'buddypress' ); ?></option>
+                       <?php endif; ?>
+
</ins><span class="cx">                   <option value="alphabetical"><?php _e( 'Alphabetical', 'buddypress' ); ?></option>
</span><span class="cx"> 
</span><span class="cx">                  <?php do_action( 'bp_groups_members_order_options' ); ?>
</span></span></pre></div>
<a id="trunktestsphpunittestcasesgroupsclassbpgroupmemberqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php (8396 => 8397)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php     2014-05-07 13:32:35 UTC (rev 8396)
+++ trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php        2014-05-07 20:46:32 UTC (rev 8397)
</span><span class="lines">@@ -402,7 +402,173 @@
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * @group type
</span><ins>+        * @group group_activity
</ins><span class="cx">    */
</span><ins>+       public function test_get_with_type_group_activity_with_activity_component_disabled() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user();
+               $u2 = $this->create_user();
+               $u3 = $this->create_user();
+               $c = buddypress()->groups->id;
+               $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 - 400 ),
+               ) );
+
+               $this->add_user_to_group( $u3, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u3,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u1,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u2,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+               ) );
+
+               // Deactivate activity component
+               $activity_active = isset( buddypress()->active_components['activity'] );
+               if ( $activity_active ) {
+                       unset( buddypress()->active_components['activity'] );
+               }
+
+               $query_members1 = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'group_activity',
+               ) );
+
+               $query_members2 = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'last_joined',
+               ) );
+
+               if ( $activity_active ) {
+                       buddypress()->active_components['activity'] = '1';
+               }
+
+               $this->assertSame( wp_list_pluck( $query_members2->results, 'ID' ), wp_list_pluck( $query_members1->results, 'ID' ) );
+       }
+
+       /**
+        * @group type
+        * @group group_activity
+        */
+       public function test_get_with_type_group_activity() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user();
+               $u2 = $this->create_user();
+               $u3 = $this->create_user();
+               $c = buddypress()->groups->id;
+               $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 - 400 ),
+               ) );
+
+               $this->add_user_to_group( $u3, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u3,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u1,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u2,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+               ) );
+
+               $query_members = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'group_activity',
+               ) );
+
+               $ids = wp_parse_id_list( array_keys( $query_members->results ) );
+               $this->assertEquals( array( $u2, $u1, $u3 ), $ids );
+       }
+
+       /**
+        * @group type
+        * @group group_activity
+        */
+       public function test_get_with_type_group_activity_no_dupes() {
+               $g = $this->factory->group->create();
+               $u1 = $this->create_user();
+               $c = buddypress()->groups->id;
+               $time = time();
+
+               $this->add_user_to_group( $u1, $g, array(
+                       'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u1,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 250 ),
+               ) );
+
+               $this->factory->activity->create( array(
+                       'component' => $c,
+                       'type' => 'activity_update',
+                       'user_id' => $u1,
+                       'item_id' => $g,
+                       'recorded_time' => gmdate( 'Y-m-d H:i:s', $time - 200 ),
+               ) );
+
+               $query_members = new BP_Group_Member_Query( array(
+                       'group_id' => $g,
+                       'type' => 'group_activity',
+               ) );
+
+               $ids = wp_parse_id_list( array_keys( $query_members->results ) );
+               $this->assertEquals( array( $u1, ), $ids );
+       }
+       /**
+        * @group type
+        */
</ins><span class="cx">   public function test_get_with_type_alphabetical() {
</span><span class="cx">          $g = $this->factory->group->create();
</span><span class="cx">          $u1 = $this->create_user( array(
</span></span></pre>
</div>
</div>

</body>
</html>