Drupal 6: user_badges_taxonomy Module

Submitted by nigel on Sunday 15th January 2012

The motivation for this module came from a shortcoming of the functionality of the user_badges module and the thread http://drupal.org/node/1144296 questioning the usage of the vocabulary feature in the user_badges module. It appears that this enables the assignment of badges to users for arbitrary achievements. For instance, a client of mine is a political campaigning organisation and it would be cool to assign badges to those activists who attend campaigns. Similarly, badges could be assigned according to a geographic location (as in my example below) or indeed anything else an administrator can think of. This can be linked to Drupal's taxonomy system - or to be more precise, the terms within a selected vocabulary.

The problem with user_badges is there is no mechanism for assigning badges to individuals once a vocabulary has been selected in user_badge settings. The module I have created below, user_badges_taxonomy addresses this. The screenshots will guide you through the process, then the source code is listed, and then the tar file is available for download.

Drupal Taxonomy List

Firstly, create a new taxonomy vocabulary at admin/content/taxonomy/add/vocabulary. Then create individual taxonomy terms which will reflect the arbitrary badges you intend assigning to your users. The image shows I have chosen geographical location for staff offices, but of course it could be anything and is only limited by your imagination. The list can be seen at admin/content/taxonomy/{tid}


This vocabulary now needs to be set in user_badges. So go to admin/user/user_badges/settings and click the radio button of the vocabulary you wish to use


Create the arbitrary badges for achievements by going to admin/user/user_badges/add. There is a drop down selection box to pick the taxonomy term you wish to assign this particular badge

Assign 2

Check that you have permissions to use my user_badges_taxonomy module by going to admin/user/permissions. Providing you have permission you will be able to navigate to the profile of any user at user/{uid}/badge_taxomony where you will see a list of all the arbitrary achievement badges you have created. These can be selected / deselected as appropriate

London Assigned

You can now navigate to user/{uid}/badges (again if you have the correct permissions) and see the fruit of your labour

To Do

It would be good to assign a userweight to these achievement badges. There is a column in the database table to support this, so that functionality should be added to my code at some stage. In addition it would be cool to show a picture of the badge along with the checkbox to aid those responsible for assigning the badges.

Source Code
name = User Badges Taxonomy
description = Assign taxonomy terms to a badge in a user's profile. Useful for arbitrary assignment of badges for achievements
dependencies[] = user_badges
core = 6.x

// Copyright @badzillacouk <a href="http://www.badzilla.co.uk
//" title="www.badzilla.co.uk
//</a> Licence GPL. This program may be distributed as per the terms of GPL and all credits
// must be retained
// If you find this script useful, please consider a donation to help me fund my web presence
// and encourage me to develop more products to be placed under the terms of GPL
// To donate, go to <a href="http://www.badzilla.co.uk" title="http://www.badzilla.co.uk">http://www.badzilla.co.uk</a> and click on the donation button
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of

* Implementation of hook_menu().
function user_badges_taxonomy_menu() {

$items = array();
$items['user/%/badge_taxomony'] = array(
'title'                 => 'Assign Achievement Badges',
'access callback'       => 'user_access',
'access arguments'      => array('create taxonomy user badges'),
'page arguments'        => array(1),
'page callback'         => 'user_badges_taxonomy_pre_form',
'type'                  => MENU_LOCAL_TASK,
'weight'                => 3,


* Implementation of hook_perm().
function user_badges_taxonomy_perm() {

    return array(
'create taxonomy user badges');

user_badges_taxonomy_pre_form($uid) {

$nothing t('There are currently no achievement badges available');

$vid variable_get('user_badges_vid'NULL);

// Are there any badges with taxonomy terms to display or not?
if (!db_result(db_query("SELECT COUNT(*) FROM {user_badges_badges} WHERE tid <> 0")))
$out $nothing;
    elseif (!
$vid or !count($tree taxonomy_get_tree($vid0, -11)))
$out $nothing;
$out drupal_get_form('user_badges_taxonomy_form'$tree$uid);


* Implementation of hook_form().
function user_badges_taxonomy_form($form_state$tree$uid) {

$form = array();
$options = array();
$defaults = array();

// create the checkbox list by looping through the tree
foreach($tree as $branch) {
            if (
$obj db_fetch_object(db_query("SELECT bid, name FROM {user_badges_badges} WHERE tid = %d"$branch->tid))) {
$options[$obj->bid] = $obj->name;

// set default after checking db
if (db_result(db_query("SELECT COUNT(*) FROM {user_badges_badges} 
                                        INNER JOIN {user_badges_user} USING (bid)
                                        WHERE bid = %d AND uid = %d"
$defaults[$obj->bid] = $obj->bid;

$form['terms'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#title' => t('Achievement Badges'),
'#default_value' => $defaults,

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),

$form['#submit'][] = 'user_badges_taxonomy_form_submit';

user_badges_taxonomy_form_submit($form, &$form_state) {

// loop through all the badges and set / unset as applicable
foreach($form_state['values']['terms'] as $key => $value) {
// Determine whether currently exists
$count db_result(db_query("SELECT COUNT(*) FROM {user_badges_user} WHERE uid = %d AND bid = %d",
$form['#parameters'][3], $key));

// four possible situations:
        // 1. no db row and not checked = do nothing
        // 2. no db row and checked = add record
        // 3. db row and not checked = delete record
        // 4. db row and checked = do nothing
if (!$count and $value)
db_query("INSERT INTO {user_badges_user} SET uid = %d, bid = %d, type = 'Vocabulary'"$form['#parameters'][3], $key);

        if (
$count and !$value)
db_query("DELETE FROM {user_badges_user} WHERE uid = %d AND bid = %d"$form['#parameters'][3], $key);

drupal_set_message(t('Changes made to the achievement badges'));
blog terms
Drupal Drupal 6 PHP