PATCH: Addition to views section -- User Relationship Count

alakon - January 31, 2008 - 21:29
Project:User Relationships
Version:5.x-2.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I needed this functionality for my own site, and I thought I would contribute it back to the community.

I've attached a small patch that adds a view field with a count of the number of relationships for a particular user.

This module can be used for a variety of situations -- to count the number of employees under a manager, or protégés under a mentor, or just to see how connected a particular user is in the social fabric of a community.

It works well for those using the usernode module, and with a slight tweak can accommodate other profile solutions.

I'm just learning and this is my first code contribution, so please take a hard look and see if anything in the patch could be done better.

AttachmentSize
user_relationship_views.patch2.7 KB

#1

sprsquish - January 31, 2008 - 23:11

@alakon

Hey, thanks for the contribution. I'm going to work on integrating it with the module, but I wanted to comment on your code a little. I hope you wont mind some constructive criticism.

Here's your main function:

<?php

function user_relationship_views_number_relationships_field_handler($field_info, $field_data, $value, $data) {
  global
$user;
 
//query to lookup UID (likely can be improved)
 
if (module_exists('usernode')) {
 
$uidlookup = db_result(db_query("SELECT `uid` FROM `usernode` WHERE `nid` =".$data->nid, $data->nid));
  } else {
 
//module has not be tested without usernode module
 
$uidlookup = $data->nid;
  }

 
//query to count number of relationships
  //both
 
if ($field_data['options'] = 'both') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." OR `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requestee
 
if ($field_data['options'] = '1') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requester
 
if ($field_data['options'] = '0') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//I might have done a very poor job with the following line. This is my first time using arrays. Please look at it before you use this code.
   
return in_array($data->nid) ? (0) : $relcount;
}
?>

So the first issue is that you have three separate "if" statements in there. We already know that the value of $field_data['options'] is going to be one of three different values, but never a combination. Right now each condition is checked creating extra work that doesn't need to be done. So those three statements should be turned into if/else if/else:

<?php
 
if ($field_data['options'] = 'both') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." OR `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requestee
 
else if ($field_data['options'] = '1') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requester
 
else {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
?>

Now the max number of comparisons have been cut to two. Also, if the first condition evaluates to TRUE none of the others run. This may be small, but it cuts out unnecessary steps.

In the same code, none of your conditional tests are actual conditions. "=" is an assignment operator, what you're looking for is actually "==" to do the comparisons.

<?php
 
if ($field_data['options'] == 'both') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." OR `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requestee
 
else if ($field_data['options'] == '1') {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requestee_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
 
//requester
 
else {
     
$relcount = db_num_rows(db_query("SELECT * FROM `user_relationships` WHERE `requester_id` =".$uidlookup." AND `approved` =1", $data->nid));
  }
?>

Those are my basic PHP criticisms. When I finish the patch, I'll post it and show you what I did and why.

Thanks again for the contribution.

#2

sprsquish - January 31, 2008 - 23:44

Okay, here's my version.

Here's the relevant parts of the patch:

<?php
     
'number_relationships_of_author' => array(
       
'name'      => t("User Relationships: Number of Relationships held by Particular User"),
       
'handler'   => 'user_relationship_views_number_relationships_field_handler',
       
'option'    => array(
         
'#type'     => 'select',
         
'#options'  => array(
           
'user'          => t('Any'),
           
'requester_id'  => t('User is Requestee'),
           
'requestee_id'  => t('User is Requester')
          ),
        ),
       
'notafield' => TRUE,
       
'help'      => t('This will display the total number of relationships held by a particular user.'),
      ),

/**
* handler for displaying the number of relationships held by an author
*/
function user_relationship_views_number_relationships_field_handler($field_info, $field_data, $value, $data) {
 
$node = node_load($data->nid);

  return
user_relationships_load(array($field_data['options'] => $node->uid), TRUE);
}
?>

Here I've set the values of the select to be param names that will later be passed to user_relationships_load. Down in the code, I've loaded the node in order to reach the UID it belongs to. Then using user_relationships_load we grab the count.

AttachmentSize
user_relationship_views.patch1.36 KB

#3

alakon - February 1, 2008 - 05:26

Awesome! Thanks for improving the code.

I'm excited to make my first modest contribution to Drupal.

#4

sprsquish - February 22, 2008 - 15:36
Status:patch (code needs review)» closed
 
 

Drupal is a registered trademark of Dries Buytaert.