Index: userpoints_role/userpoints_role.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/userpoints_role/userpoints_role.info,v
retrieving revision 1.3.2.2
diff -u -p -r1.3.2.2 userpoints_role.info
--- userpoints_role/userpoints_role.info	11 Jan 2009 00:51:42 -0000	1.3.2.2
+++ userpoints_role/userpoints_role.info	10 Jan 2011 10:27:48 -0000
@@ -3,4 +3,5 @@ name = Userpoints Role
 description = Users join/leave roles as they earn/lose certain points threshold, and get an email.
 package = Userpoints
 dependencies[] = userpoints
-core = 6.x
+core = 7.x
+files[]=userpoints_role.module
Index: userpoints_role/userpoints_role.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/userpoints_role/userpoints_role.install,v
retrieving revision 1.1.2.4
diff -u -p -r1.1.2.4 userpoints_role.install
--- userpoints_role/userpoints_role.install	29 Dec 2008 17:19:40 -0000	1.1.2.4
+++ userpoints_role/userpoints_role.install	10 Jan 2011 10:27:48 -0000
@@ -1,8 +1,12 @@
 <?php
 
-// $Id: userpoints_role.install,v 1.1.2.4 2008/12/29 17:19:40 kbahey Exp $
+// $Id: userpoints_role.install,v 1.1.2.4 2008-12-29 17:19:40 kbahey Exp $
 
 function userpoints_role_uninstall() {
-  db_query("DELETE FROM {variable} WHERE name LIKE 'userpoints_role%%'");
-  db_query("DELETE FROM {variable} WHERE name LIKE 'userpoints_email_role%%'");
+  db_delete('variable')
+    ->condition(db_or()
+      ->condition('name', 'userpoints_role%', 'LIKE')
+      ->condition('name', 'userpoints_email_role%', 'LIKE')
+    )
+    ->execute();
 }
Index: userpoints_role/userpoints_role.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/userpoints_role/userpoints_role.module,v
retrieving revision 1.4.2.5
diff -u -p -r1.4.2.5 userpoints_role.module
--- userpoints_role/userpoints_role.module	3 Feb 2009 17:00:01 -0000	1.4.2.5
+++ userpoints_role/userpoints_role.module	10 Jan 2011 10:27:48 -0000
@@ -1,5 +1,4 @@
 <?php
-
 //$Id: userpoints_role.module,v 1.4.2.5 2009/02/03 17:00:01 kbahey Exp $
 
 // Copyright 2005-2008 Khalid Baheyeldin http://2bits.com
@@ -35,12 +34,12 @@ function userpoints_role_help($path, $ar
 function userpoints_role_menu() {
   $items = array();
 
-  $items['admin/settings/userpoints/role/%'] = array(
+  $items['admin/config/people/userpoints/role/%'] = array(
     'title'            => t('!Points roles settings', userpoints_translation()),
     'description'      => t('Settings for role'),
     'page callback'    => 'drupal_get_form',
     'page arguments'   => array('userpoints_role_admin_settings', 4),
-    'access arguments' => array(USERPOINTS_PERM_ADMIN),
+    'access arguments' => array('administer userpoints'),
     'type'             => MENU_NORMAL_ITEM
   );
 
@@ -97,32 +96,25 @@ function userpoints_role_admin_settings(
 function userpoints_role_userpoints($op, $params = array()) {
   switch($op) {
     case 'setting':
-      $group = 'role';
-      $form[$group] = array(
-        '#type'        => 'fieldset',
-        '#title'       => t('!Points for Roles', userpoints_translation()),
-        '#description' => t('Users join/leave to roles as they gain/lose points'),
-        '#collapsible' => TRUE,
-        '#collapsed'   => TRUE,
-      );
-
-      $subgroup = 'default_email';
+      $group = 'default_email';
 
-      $form[$group][$subgroup] = array(
+      $form[$group] = array(
         '#type'        => 'fieldset',
-        '#title'       => t('Default email settings', userpoints_translation()),
+        '#title'       => t('Roles email settings', userpoints_translation()),
         '#description' => t('Email subject and body for joining and leaving roles. You can modify that further for each role if you wish, using the links below each role. If you a role does not have its own subject and body settings, then the ones below will apply.'),
         '#collapsible' => TRUE,
         '#collapsed'   => TRUE,
+        '#group' => 'settings_additional',
+        '#weight' => 10,
       );
 
-      $form[$group][$subgroup][USERPOINTS_ROLE_SUBJECT_JOIN] = array(
+      $form[$group][USERPOINTS_ROLE_SUBJECT_JOIN] = array(
         '#type'          => 'textfield',
         '#title'         => t('Subject of email to sent upon joining a role'),
         '#default_value' => variable_get(USERPOINTS_ROLE_SUBJECT_JOIN, t(userpoints_role_defaults('join_subject'))),
       );
 
-      $form[$group][$subgroup][USERPOINTS_ROLE_TEXT_JOIN] = array(
+      $form[$group][USERPOINTS_ROLE_TEXT_JOIN] = array(
         '#type'          => 'textarea',
         '#title'         => t('Text of email to sent upon joining a role'),
         '#default_value' => variable_get(USERPOINTS_ROLE_TEXT_JOIN, t(userpoints_role_defaults('join_text'))),
@@ -130,13 +122,13 @@ function userpoints_role_userpoints($op,
         '#lines'         => 5,
         );
 
-      $form[$group][$subgroup][USERPOINTS_ROLE_SUBJECT_LEAVE] = array(
+      $form[$group][USERPOINTS_ROLE_SUBJECT_LEAVE] = array(
         '#type'          => 'textfield',
         '#title'         => t('Subject of email to sent upon leaving a role'),
         '#default_value' => variable_get(USERPOINTS_ROLE_SUBJECT_LEAVE, t(userpoints_role_defaults('leave_subject'))),
       );
 
-      $form[$group][$subgroup][USERPOINTS_ROLE_TEXT_LEAVE] = array(
+      $form[$group][USERPOINTS_ROLE_TEXT_LEAVE] = array(
         '#type'          => 'textarea',
         '#title'         => t('Text of email to sent upon leaving a role'),
         '#default_value' => variable_get(USERPOINTS_ROLE_TEXT_LEAVE, t(userpoints_role_defaults('leave_text'))),
@@ -146,18 +138,20 @@ function userpoints_role_userpoints($op,
 
       $role_list = userpoints_role_load_role_points(TRUE);
 
-      $subgroup = 'role_details';
+      $group = 'roles';
 
-      $form[$group][$subgroup] = array(
+      $form[$group] = array(
         '#type'        => 'fieldset',
-        '#title'       => t('Role settings', userpoints_translation()),
-        '#description' => t('Points required to join each role. Enter 0 to ignore that role.'),
+        '#title'       => t('Roles', userpoints_translation()),
+        '#description' => t('Users join/leave to roles as they gain/lose points. Points required to join each role. Enter 0 to ignore that role.'),
         '#collapsible' => TRUE,
         '#collapsed'   => TRUE,
+        '#group' => 'settings_additional',
+        '#weight' => 9,
       );
 
       if (empty($role_list)) {
-        $form[$group][$subgroup]['no_roles'] = array(
+        $form[$group]['no_roles'] = array(
           '#type '  => 'markup',
           '#prefix' => '<div>',
           '#suffix' => '</div>',
@@ -170,19 +164,19 @@ function userpoints_role_userpoints($op,
         $rid  = $role_point['rid'];
         $role = USERPOINTS_ROLE . $rid;
 
-        $form[$group][$subgroup][$role] = array(
+        $form[$group][$role] = array(
           '#type'          => 'textfield',
           '#title'         => t('Points for role :') . $name,
           '#default_value' => variable_get($role, 0),
         );
-        $form[$group][$subgroup][$role .'_tid'] = array(
+        $form[$group][$role .'_tid'] = array(
           '#type'          => 'select',
           '#title'         => t('Category'),
           '#default_value' => variable_get($role .'_tid', 0),
           '#options'       => userpoints_get_categories(),
           '#description'   => t('Category of these points'),
         );
-        $form[$group][$subgroup][$role . 'links'] = array(
+        $form[$group][$role . 'links'] = array(
           '#type '  => 'markup',
           '#prefix' => '<div>',
           '#suffix' => '</div>',
@@ -194,58 +188,65 @@ function userpoints_role_userpoints($op,
       break;
 
     case 'points after':
-      $uid        = $params['uid'];
-      $new_points = $params['points'];
-
       // Check the roles the user belong to, and if they are exempted from userpoints
-      $points = userpoints_get_current_points($uid);
-      _userpoints_role_update_roles($uid, $points, $new_points);
+      _userpoints_role_update_roles($params);
       break;
   }
 }
 
-function _userpoints_role_update_roles($uid, $points, $new_points) {
+/**
+ * Check if the user needs to be added or removed from roles.
+ *
+ * @param array $params
+ *   Userpoints transaction.
+ */
+function _userpoints_role_update_roles($transaction) {
+  $points = userpoints_get_current_points($transaction['uid'], $transaction['tid']);
+
   $role_points = userpoints_role_load_role_points();
-  if ($new_points < 0) {
-    // we may need to demote the user in roles
+  if ($transaction['points'] < 0) {
+    // Negative transaction point amount, we may need to remove the user for
+    // certain roles.
     foreach($role_points as $role_point) {
-      if ($role_point['points'] > 0) {
-        // Ignore roles that have 0 in them, since the admin does not want
-        // users to join those roles
-        if ($role_point['tid'] > 0) {
-          $cat_points = userpoints_get_current_points($uid, $role_point['tid']);
-          if ($cat_points < $role_point['points']) {
-            userpoints_role_leave($uid, $role_point);
-          }
-        }
-        else if ($points < $role_point['points']) {
-          userpoints_role_leave($uid, $role_point);
+      // Only continue if the role has points assigned and the category
+      // equals the category of the points transaction.
+      if ($role_point['points'] > 0 && $role_point['tid'] == $transaction['tid']) {
+        if ($cat_points < $role_point['points']) {
+          // Remove user from role if he has not enough points.
+          userpoints_role_leave($transaction['uid'], $role_point);
         }
       }
     }
   }
   else {
-    // we need to promote the user to new roles, if they have the threshold
+    // Positive transaction point amount, check if user needs to have roles
+    // assigned.
     foreach($role_points as $role_point) {
-      if ($role_point['points'] > 0) {
-        // Ignore roles that have 0 in them, since the admin does not want
-        // users to join those roles
-        if ($role_point['tid'] > 0) {
-          $cat_points = userpoints_get_current_points($uid, $role_point['tid']);
-          if ($cat_points < $role_point['points']) {
-            userpoints_role_join($uid, $role_point);
-          }
-        }
-        else if ($points >= $role_point['points']) {
-          userpoints_role_join($uid, $role_point);
+      // Only continue if the role has points assigned and the category
+      // equals the category of the points transaction.
+      if ($role_point['points'] > 0 && $role_point['tid'] == $transaction['tid']) {
+        if ($cat_points < $role_point['points']) {
+          // Assign the role to the user.
+          userpoints_role_join($transaction['uid'], $role_point);
         }
       }
     }
   }
 }
 
+/**
+ * Check if user is in a certain role.
+ *
+ * @param int $uid
+ *   User ID.
+ * @param int $rid
+ *   Role ID.
+ *
+ * @return boolean
+ *   TRUE if the user has the given role, FALSE if not.
+ */
 function userpoints_role_check_user_role($uid, $rid) {
-  return (int)db_result(db_query("SELECT COUNT(*) FROM {users_roles} WHERE rid = %d AND uid = %d", $rid, $uid));
+  return (bool)db_query_range("SELECT 1 FROM {users_roles} WHERE rid = :rid AND uid = :uid", 0, 1, array(':uid' => $uid, ':rid' => $rid))->fetchField();;
 }
 
 function userpoints_role_join($uid, $role_point) {
@@ -271,7 +272,9 @@ function userpoints_role_leave($uid, $ro
 }
 
 function userpoints_role_send_mail($op = 'join', $uid, $role_point) {
-  $account = user_load(array('uid' => $uid));
+  global $language;
+
+  $account = user_load($uid);
   $vars = array(
     '!username' => $account->name,
     '!rolename' => $role_point['name'],
@@ -302,7 +305,7 @@ function userpoints_role_email($op = 'jo
 }
 
 /**
- * Implementation of hook_mail().
+ * Implements hook_mail().
  */
 function userpoints_role_mail($key, &$message, $params) {
   $message['subject'] = $params['subject'];
@@ -340,38 +343,3 @@ function _userpoints_role_load_role_data
     'tid'    => variable_get(USERPOINTS_ROLE . $rid .'_tid', 0),
   );
 }
-
-/**
- * Public function to return the name of the highest * role a user has reached.
- *
- * @param $uid
- *  Option user ID. Current logged in user if not specified.
- *
- * @return 
- *  name of their role
- *
- */
-function userpoints_role_highest_role($uid = 0) {
-  global $user;
-
-  // If no user specified, then it is for the current user
-  if (!$uid) {
-    $uid = $user->uid;
-  }
-
-  // Get the points the user has
-  $current_points = userpoints_get_current_points($uid);
-
-  // Get a list of roles
-  $role_points = userpoints_role_load_role_points();
-  $roles_within = array();
-  foreach($role_points as $role) {
-    //put all the roles user is in into an array.
-    if ($current_points > $role['points']){
-      $roles_within[$role['points']] = $role['name'];
-    }
-  }
-  // return the maximum role name
-  // max() returns the value of the largest key
-  return max($roles_within);
-}
