[wp-hackers] A Basic Activity Feed - Is This Efficient?

BenderisGreat greglancaster71 at gmail.com
Tue Oct 29 11:32:20 UTC 2013


Creating a basic activity feed for users. First wrote a simple function that
takes args as user_id, activity_type, and source_page.  Here is the
function:

/function activity_tracker($user_id, $activity_type, $source_id ) {
global $wpdb, $current_user;
      get_currentuserinfo();

	$time = current_time('mysql');

	$wpdb->insert( 'wp_jo_activity_feed', array( 
	'user_id' => $current_user->ID, 
	'activity_type' => $activity_type, 
	'source_id' => $source_id, 
	'time' => $time
	));
	/

Simple enough, then I drop this function call all over the place: 

/	$runme = activity_tracker($_current_member->ID, 'update_info', 'prof');/

So everytime someone submits a form, or deletes an entry, or changes profile
information, friends another user, likes a post, etc... they each have a
specific activity name (that I enter manually) and is logged this way.

Then, I query the DB to show the most recent 10-50 activities, on the
activity feed page, like so:

/        <?php //activity call
	$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
		$i = 0;
	$limit = 15;
	$count = count($activity_calls);
	while ($i < $limit && $i < $count) 
	{
	$row = $activity_calls[$i];
	$get_row_user_id = $row->user_id;
	$convert_name = get_userdata( $get_row_user_id ); 
	$time = $row->time;
	
	if ($row->activity_type == 'new_record') {
		
		$message = ' created a new log entry at '; 
		$points = '+10';} elseif 
		
		( $row->activity_type == 'deleted_record') {
		
		$message = ' deleted an existing log entry at ';
		$points = 0; } else {
		
		$message = ' updated his profile information at ';
		$points = 5; }
	
	echo '<li class="list-group-item">'.$points.''.$convert_name->user_login.
$message .$time. '</li>';
		++$i; }
		?>
        </ul>
};/

You get the idea, it's obviously very simple - but it's also VERY DB
intensive (is that the correct terminology?).  I am making a ton of DB
calls, which isnt a problem yet, but I would like to know if anyone has a
more efficient method to achieve the same result.  

Additionally, for the message being returned in the activity feed, I am
using if, elseif, and else statements, which limits me.  I would prefer to
have a more streamlined way to return the message.  If I were to write a
function that selected the response based on the activity_type entered- what
would something like that look like?  

Before it's recommended, I am aware buddypress is an option for activity
feeds.  I want to do this so I can learn more, and create something unique
for myself.  



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/A-Basic-Activity-Feed-Is-This-Efficient-tp42662.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.


More information about the wp-hackers mailing list