Views Total?

paulgrimshaw - September 19, 2009 - 17:31
Project:User Points
Version:6.x-1.x-dev
Component:Code: userpoints_views
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

Hi,

I am using the Userpoints:points in Views to show on a user profile page how many points they have, but rather than show Total as I assumed it would, it shows each category total instead.

Is it possible to show the full total or at least what the categories are? e.g. It just lists all the totals without any labels.

Thanks,
Paul

#1

dash - October 29, 2009 - 22:36

Paul,

I believe the issue is because the userpoints table sums up the userpoints by category. A simple solution would be to have a single category, but I appreciate this would be limiting. In my case, I faced the same issue and this is what I did as a work around.

Note: I have updated the userpoints code directly, but you may want to consider creating a mini module to have a clean implementation. When I get down to cleaning my code, I will post it here.

Step 1: In your mysql database create a view as follows:

CREATE VIEW userpoints_total SELECT uid, sum(points) total_points FROM `userpoints` GROUP BY uid

Step 2: Update the userpoints_views_data hook code in userpoints.views.inc to the following:

<?php
function userpoints_views_data() {
 
// ----------------------------------------------------------------
  // userpoints table

  // Describe the userpoints table.
  // Define the base group of this table. Fields that don't
  // have a group defined will go into this field by default.
 
$data['userpoints']['table']['group']  = t('Userpoints');
 
$data['userpoints_total']['table']['group']  = t('Userpoints');

 
$data['userpoints']['table']['base'] = array(
   
'field' => 'uid',
   
'title' => t('Userpoints'),
   
'help' => t('!Points accumulated by users on your site.', userpoints_translation()),
  );

 
$data['userpoints']['table']['join'] = array(
   
'users' => array(
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
   
'node' => array(
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
   
// This goes to the node so that we have consistent authorship.
   
'node_revisions' => array(
     
'left_table' => 'node',
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
  );

 
// Describe the points column of the userpoints table.
 
$data['userpoints']['points'] = array(
   
'title' => t('!Points', userpoints_translation()),
   
'help' => t("A User's current !points.", userpoints_translation()), // The help that appears on the UI,
   
'field' => array(
     
'handler' => 'views_handler_field_numeric',
     
'click sortable' => TRUE,
    ),
   
'argument' => array(
     
'handler' => 'views_handler_argument_numeric',
     
'numeric' => TRUE,
     
'name field' => 'points', // display this field in the summary
   
),
   
'filter' => array(
     
'handler' => 'views_handler_filter_numeric',
    ),
   
'sort' => array(
     
'handler' => 'views_handler_sort',
    ),
  );

 
// Add relationship to user table.
 
$data['userpoints']['uid'] = array(
   
'title' => t('User'),
   
'help' => t('Relate the userpoints table to the user table.'),
   
'relationship' => array(
     
'base' => 'users',
     
'field' => 'uid',
     
'label' => t('Users'),
     
'handler' => 'views_handler_relationship',
    ),
  );

 
//CUSTOMIZATION STARTS HERE

  //User points total

 
$data['userpoints_total']['table']['base'] = array(
   
'field' => 'uid',
   
'title' => t('Userpoints Total'),
   
'help' => t('Total !Points accumulated by users on your site.', userpoints_translation()),
  );

 
$data['userpoints_total']['table']['join'] = array(
   
'users' => array(
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
   
'node' => array(
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
   
// This goes to the node so that we have consistent authorship.
   
'node_revisions' => array(
     
'left_table' => 'node',
     
'left_field' => 'uid',
     
'field' => 'uid',
    ),
  );

 
// Describe the total_points column of the userpoints_total table.
 
$data['userpoints_total']['total_points'] = array(
   
'title' => t('Total !Points', userpoints_translation()),
   
'help' => t("A User's current total !points.", userpoints_translation()), // The help that appears on the UI,
   
'field' => array(
     
'handler' => 'views_handler_field_numeric',
     
'click sortable' => TRUE,
    ),
   
'argument' => array(
     
'handler' => 'views_handler_argument_numeric',
     
'numeric' => TRUE,
     
'name field' => 'total_points', // display this field in the summary
   
),
   
'filter' => array(
     
'handler' => 'views_handler_filter_numeric',
    ),
   
'sort' => array(
     
'handler' => 'views_handler_sort',
    ),
  );

 
// Add relationship to user table.
 
$data['userpoints_total']['uid'] = array(
   
'title' => t('User'),
   
'help' => t('Relate the total userpoints table to the user table.'),
   
'relationship' => array(
     
'base' => 'users',
     
'field' => 'uid',
     
'label' => t('Users'),
     
'handler' => 'views_handler_relationship',
    ),
  );

 
//CUSTOMIZATION ENDS HERE

 
return $data;
}
?>

Step 3: Clear the cache from admin/settings/performance

You should now be able to see an additional field called "Total ..." under Userpoints Group in the view

Hope this helps.

Cheers,
Dash

 
 

Drupal is a registered trademark of Dries Buytaert.