### Eclipse Workspace Patch 1.0 #P userpoints_contrib Index: userpoints_badges/userpoints_badges.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/userpoints_badges/userpoints_badges.module,v retrieving revision 1.3 diff -u -r1.3 userpoints_badges.module --- userpoints_badges/userpoints_badges.module 9 Jan 2009 10:39:21 -0000 1.3 +++ userpoints_badges/userpoints_badges.module 23 Aug 2010 00:27:53 -0000 @@ -1,5 +1,7 @@ 10, ); $form['submit']['#weight'] = 10; - + $form['#validate'][] = 'userpoints_badges_edit_badge_validate'; $form['#submit'][] = 'userpoints_badges_edit_badge_submit'; } @@ -72,7 +74,7 @@ $form_values['weight'], $form_values['name'], $form_values['image'], 0, 1)); } db_query("DELETE FROM {userpoints_badges} WHERE bid=%d", $bid); - + if (trim($form_values['userpoints_goal']) !== '') { db_query("INSERT INTO {userpoints_badges} (bid, userpoints_goal) VALUES (%d, %d)", $bid, $form_values['userpoints_goal']); @@ -88,11 +90,11 @@ } /** - * When a userpoints badge is created or edited, a mass check is run on the users to see if they qualify for the new badge + * When a userpoints badge is created or edited, a mass check is run on the users to see if they qualify for the new badge */ function userpoints_badges_run_mass_badge_check($bid, $userpoints_goal) { db_query("DELETE FROM {user_badges_user} WHERE bid=%d", $bid); - $resultset = db_query("SELECT uid FROM {userpoints} WHERE uid > 0 GROUP BY uid HAVING SUM(points) > %d", + $resultset = db_query("SELECT uid FROM {userpoints} WHERE uid > 0 GROUP BY uid HAVING SUM(points) > %d", $userpoints_goal); while ($account = db_fetch_object($resultset)) { user_badges_user_add_badge($account->uid, $bid, strtr('Userpoints @bid', array('@bid' => $bid))); @@ -108,18 +110,40 @@ case 'setting': $group = 'userpoints_badges'; $form[$group] = array( + '#type' => 'fieldset', + '#title' => t('!Points Badges',userpoints_translation()), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form[$group]['text'] = array( '#type' => 'item', '#value' => t('Go to !link to edit userpoint badges', array('!link' => l(t('Userpoints Badges Settings'), 'admin/user/user_badges'))) ); + $options = array( 0 => t('Lifetime points earned'), 1 => t('Current points balance') ); + $form[$group][USERPOINTS_BADGES_ALLOCATION] = array( + '#type' => 'radios', + '#title' => t('Allocation Method'), + '#default_value' => variable_get(USERPOINTS_BADGES_ALLOCATION, 0), + '#options' => $options, + '#description' => t('Define how badges are calculated. This should be based on "Liftime points earned" or "Current points balance".'), + ); + return $form; case 'points after': $badges = userpoints_badges_get_badges(); - $total_points = userpoints_get_current_points($params['uid'], 'all'); + //check to see what allocation method we are using lifetime or current balance + if (variable_get(USERPOINTS_BADGES_ALLOCATION, 0) == 0){ + $total_points = userpoints_get_max_points($params['uid'], 'all'); + } else { + $total_points = userpoints_get_current_points($params['uid'], 'all'); + } + // Remove all existing badges for this user + db_query("DELETE FROM {user_badges_user} WHERE uid = %d AND type LIKE 'Userpoints%'", $params['uid']); + + // Iterate through each badge and assign any badge that passes the threshold foreach ($badges as $badge) { if ($total_points >= $badge->userpoints_goal) { - if (!db_result(db_query('SELECT uid FROM {user_badges_user} WHERE bid=%d AND uid=%d', $badge->bid, $params['uid']))) { - user_badges_user_add_badge($params['uid'], $badge->bid, t('Userpoints @bid', array('@bid' => $badge->bid))); - } + user_badges_user_add_badge($params['uid'], $badge->bid, t('Userpoints @bid', array('@bid' => $badge->bid))); } } } @@ -140,7 +164,7 @@ $lookup[$badge->bid] = $goal; } krsort($badges_by_goal, SORT_NUMERIC); - + $user_badges = user_badges_get_badges($uid); $user_badges_by_goal = array(); foreach ($user_badges as $badge) { @@ -148,7 +172,7 @@ $user_badges_by_goal[$goal] = $badge; } krsort($user_badges_by_goal, SORT_NUMERIC); - + if (count($user_badges_by_goal)) { $badge = array_shift($user_badges_by_goal); } @@ -159,4 +183,4 @@ return $badge; } return ''; -} \ No newline at end of file +}