Index: tests/userpoints_api.test =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/tests/Attic/userpoints_api.test,v retrieving revision 1.1.2.13 diff -u -p -r1.1.2.13 userpoints_api.test --- tests/userpoints_api.test 31 Jan 2011 22:36:54 -0000 1.1.2.13 +++ tests/userpoints_api.test 20 Feb 2011 17:23:11 -0000 @@ -38,7 +38,7 @@ class UserpointsTestCase extends DrupalW */ function testBasicCall() { global $user; - $points = (int) rand(1, 500); + $points = rand(1, 500); $sumpoints = $points; //NOTE: increment max points with all positive point changes, tests userpoints_get_max_points @@ -49,15 +49,15 @@ class UserpointsTestCase extends DrupalW $this->assertTrue($return['status'] == TRUE , t('API responded with successful grant of points')); //Check the database to ensure the point were properly saved - $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points)); - $this->assertTrue($db_points === $points, t('Successfully verified points in the txn table')); + $sql = "SELECT points from {userpoints_txn} WHERE uid = %d ORDER BY time_stamp"; + $db_points = (float) db_result(db_query($sql, $user->uid)); + $this->assertTrue(round($db_points) == round($points), t('Successfully verified points in the txn table')); //Check that the transaction table and the summary table match $sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d"; - $txn_points = (int) db_result(db_query($sql, $user->uid)); + $txn_points = (float) db_result(db_query($sql, $user->uid)); $sql = "SELECT SUM(points) from {userpoints} WHERE uid = %d"; - $up_points = (int) db_result(db_query($sql, $user->uid)); + $up_points = (float) db_result(db_query($sql, $user->uid)); $this->assertTrue($txn_points === $up_points, t('Sum of transactions match total points for user')); //Add negative points to the initial value and check the values @@ -66,22 +66,18 @@ class UserpointsTestCase extends DrupalW userpoints_userpointsapi($points); //Check the database to ensure the negative point value was properly saved - $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points)); - $this->assertTrue($db_points === $points, t('Successfully verified negative points in the txn table')); + $sql = "SELECT points from {userpoints_txn} WHERE uid = %d ORDER BY txn_id DESC"; + $db_points = (float) db_result(db_query($sql, $user->uid)); + $this->assertTrue(round($db_points) == round($points), t('Successfully verified negative points in the txn table')); //Now test to make sure the transaction and and caching table stay in sync. //Also test userpoints_get_max_points and userpoints_get_current_points - for ($i = 0; $i <= rand(1, 50); $i++) { - $points = rand(1, 500); - if (rand() & 1) { - $points = - $points; + for ($i = 0; $i < 10; $i++) { + $points = rand(-500, 500); + if ($points > 0) { + $maxpoints = $maxpoints + $points; } - - if ($points > 0) { - $maxpoints = $maxpoints + $points; - } $sumpoints = $sumpoints + $points; userpoints_userpointsapi($points); } @@ -89,20 +85,20 @@ class UserpointsTestCase extends DrupalW //Check the summary table to make sure everything is still kosher. $sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d"; - $txn_points = (int) db_result(db_query($sql, $user->uid)); + $txn_points = (float) db_result(db_query($sql, $user->uid)); $sql = "SELECT SUM(points) from {userpoints} WHERE uid = %d"; - $up_points = (int) db_result(db_query($sql, $user->uid)); + $up_points = (float) db_result(db_query($sql, $user->uid)); - $this->assertTrue($txn_points === $up_points, t('Sum of transactions matches the caching table')); - $this->assertTrue($up_points === $sumpoints, + $this->assertTrue(round($txn_points) === round($up_points), t('Sum of transactions matches the caching table')); + $this->assertTrue(round($up_points) === round($sumpoints), t('Caching table matches testing code after !recs point records totaling !points points', array('!recs' => $i, '!points' => $sumpoints )) ); - $this->assertTrue($sumpoints == userpoints_get_current_points(), + $this->assertTrue(round($sumpoints) == round(userpoints_get_current_points()), t("userpoints_get_current_points() returned correct point value") ); - $this->assertTrue($maxpoints == userpoints_get_max_points(), + $this->assertTrue(round($maxpoints) == round(userpoints_get_max_points()), t("userpoints_get_max_points() returned correct point value") ); @@ -116,7 +112,7 @@ class UserpointsTestCase extends DrupalW global $user; global $admin_user; - $points = rand(); + $points = rand(1,500); //Assert that the use of a params array with simply points in it works. $params = array( @@ -128,9 +124,9 @@ class UserpointsTestCase extends DrupalW ); //Check the Database to make sure the points made it there - $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d"; - $db_points = db_result(db_query($sql, $user->uid, $points)); - $this->assertTrue($db_points == $points, t('Successfully verified points in the txn table')); + $sql = "SELECT points from {userpoints_txn} WHERE uid = %d ORDER BY txn_id DESC"; + $db_points = db_result(db_query($sql, $user->uid)); + $this->assertTrue((int)$db_points == (int) $points, t('Successfully verified points in the txn table')); //Test to ensure that calling it with no points returns FALSE indicating an error $params = array( @@ -161,9 +157,9 @@ class UserpointsTestCase extends DrupalW ); //Check the Database to make sure the points made it there - $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d"; - $db_points = db_result(db_query($sql, $admin_user->uid, $points)); - $this->assertTrue($db_points == $points, t('Successfully verified points in the txn table')); + $sql = "SELECT points from {userpoints_txn} WHERE uid = %d ORDER BY txn_id DESC"; + $db_points = db_result(db_query($sql, $admin_user->uid)); + $this->assertTrue(round($db_points) == round($points), t('Successfully verified points in the txn table')); //Attempt to award points to a non-existent user @@ -249,9 +245,9 @@ class UserpointsTestCase extends DrupalW $this->assertTrue($return['status'] == TRUE, t($time['string'] ." API responded with a successful grant of points")); /* Check the database to ensure the points were properly saved */ - $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND expirydate = %d"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points, $time['time'])); - $this->assertTrue($db_points == $points, + $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %f AND expirydate = %d"; + $db_points = (float) db_result(db_query($sql, $user->uid, $points, $time['time'])); + $this->assertTrue(round($db_points) == round($points), t($time['string'] ."Successfully verified points in the txn table.") ); if ($db_points == $points) { $sum_points = $sum_points + $points; @@ -259,8 +255,8 @@ class UserpointsTestCase extends DrupalW /* Check update point to 'userpoints' table */ $sql1 = "SELECT points FROM {userpoints} WHERE uid=%d"; - $db_points = (int) db_result(db_query($sql1, $user->uid)); - $this->assertTrue($db_points == $sum_points, + $db_points = (float) db_result(db_query($sql1, $user->uid)); + $this->assertTrue(round($db_points) == round($sum_points), t($time['string'] ."Successfully verified that the summary table was updated") ); } @@ -303,12 +299,12 @@ class UserpointsTestCase extends DrupalW /* Check the user points removed or not if the point was expiration */ $sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d"; - $db_points = (int) db_result(db_query($sql, $user->uid)); + $db_points = (float) db_result(db_query($sql, $user->uid)); $this->assertTrue($db_points == $keep_points, t("Successfully removed expired points from the txn table.") ); $sql = "SELECT points FROM {userpoints} WHERE uid=%d"; - $db_points = (int) db_result(db_query($sql, $user->uid)); - $this->assertTrue($db_points == $keep_points, t("Successfully removed expired points from the summary table.") ); + $db_points = (float) db_result(db_query($sql, $user->uid)); + $this->assertTrue(round($db_points) == round($keep_points), t("Successfully removed expired points from the summary table.") ); // Load the expiry transaction from the database and verify that it does // not expire. @@ -414,8 +410,8 @@ class UserpointsTestCase extends DrupalW $this->drupalPost('admin/user/userpoints/add', $edit, 'Save'); /* check database */ - $sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%d"; - $db_points = (int)db_result(db_query($sql, $admin_user->uid, $points)); + $sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%f"; + $db_points = (float)db_result(db_query($sql, $admin_user->uid, $points)); $this->assertTrue($db_points == $points, t("Successful verified that points were added into database.")); /* logout and change user */ @@ -445,8 +441,8 @@ class UserpointsTestCase extends DrupalW */ /* check database */ - $sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%d"; - $db_points = (int)db_result(db_query($sql, $view_user->uid, $points)); + $sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%f"; + $db_points = (float)db_result(db_query($sql, $view_user->uid, $points)); $this->assertTrue($db_points != $points, t("Successful verified that points do not add into database.")); $this->drupalLogout(); } @@ -466,13 +462,13 @@ class UserpointsTestCase extends DrupalW $this->assertTrue($return['status'] == TRUE , t("1. (moderate=TRUE) API responded with successful grant of points")); /* Check the database to ensure the point were properly saved */ - $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND status = 1"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points)); + $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %f AND status = 1"; + $db_points = (float) db_result(db_query($sql, $user->uid, $points)); $this->assertTrue($db_points == $points, t("1. (moderate=TRUE) Successfully verified points in the txn table and waiting moderation.") ); /* Check do not update point to 'userpoints' table */ $sql1 = "SELECT points FROM {userpoints} WHERE uid=%d"; - $db_points = (int) db_result(db_query($sql1, $user->uid)); + $db_points = (float) db_result(db_query($sql1, $user->uid)); $this->assertTrue($db_points == 0, t("1. (moderate=TRUE) Successfully verified that points were added and the summary table was not updated.") ); /* @@ -485,12 +481,12 @@ class UserpointsTestCase extends DrupalW $return = userpoints_userpointsapi($params); $this->assertTrue($return['status'] == TRUE , t("6. (moderate=NULL) API responded with successful grant of points")); - $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND status = 1"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points)); + $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %f AND status = 1"; + $db_points = (float) db_result(db_query($sql, $user->uid, $points)); $this->assertTrue($db_points == $points,t("6. (moderate=NULL) Successfully verified points in the txn table and waiting moder.") ); $sql1 = "SELECT points FROM {userpoints} WHERE uid=%d"; - $db_points = (int) db_result(db_query($sql1, $user->uid)); + $db_points = (float) db_result(db_query($sql1, $user->uid)); $this->assertTrue($db_points == 0,t("6. (moderate=NULL) Successfully, Points added and does not modify summary table.") ); */ @@ -506,8 +502,8 @@ class UserpointsTestCase extends DrupalW $this->assertTrue($return['status'] == TRUE , t("7. (moderate=FALSE) API responded with successful grant of points")); /* Check the database to ensure the point were properly saved */ - $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND status = 0"; - $db_points = (int) db_result(db_query($sql, $user->uid, $points)); + $sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %f AND status = 0"; + $db_points = (float) db_result(db_query($sql, $user->uid, $points)); $this->assertTrue($db_points == $points, t("7. (moderate=FALSE) Successfully verified points in the txn table and NOT waiting moderation.") ); } Index: userpoints.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/userpoints.install,v retrieving revision 1.15.2.11 diff -u -p -r1.15.2.11 userpoints.install --- userpoints.install 16 Feb 2011 09:50:56 -0000 1.15.2.11 +++ userpoints.install 20 Feb 2011 17:23:11 -0000 @@ -22,13 +22,13 @@ function userpoints_schema() { ), 'points' => array( 'description' => 'Current Points', - 'type' => 'int', + 'type' => 'float', 'not null' => TRUE, 'default' => 0, ), 'max_points' => array( 'description' => 'Out of a maximum points', - 'type' => 'int', + 'type' => 'float', 'not null' => TRUE, 'default' => 0, ), @@ -75,7 +75,7 @@ function userpoints_schema() { ), 'points' => array( 'description' => 'Points', - 'type' => 'int', + 'type' => 'float', 'not null' => TRUE, 'default' => 0, ), @@ -297,3 +297,14 @@ function userpoints_update_6013() { db_add_index($ret, 'userpoints_txn', 'points', array('points')); return $ret; } + +/** + * Change points from int to float. + */ +function userpoints_update_6014() { + $ret = array(); + db_change_field($ret, 'userpoints_txn', 'points', 'points', array('type' => 'float', 'not null' => true, 'default' => 0)); + db_change_field($ret, 'userpoints', 'points', 'points', array('type' => 'float', 'not null' => true, 'default' => 0)); + db_change_field($ret, 'userpoints', 'max_points', 'max_points', array('type' => 'float', 'not null' => true, 'default' => 0)); + return $ret; +} Index: userpoints.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/userpoints.module,v retrieving revision 1.67.2.70 diff -u -p -r1.67.2.70 userpoints.module --- userpoints.module 16 Feb 2011 10:01:10 -0000 1.67.2.70 +++ userpoints.module 20 Feb 2011 17:23:11 -0000 @@ -34,6 +34,8 @@ define('USERPOINTS_REPORT_USERCOUNT', 'u define('USERPOINTS_REPORT_LIMIT', 'userpoints_report_limit'); define('USERPOINTS_REPORT_DISPLAYZERO', 'userpoints_report_displayzero'); +define('USERPOINTS_PRECISION_NUM', 'userpoints_precision_num'); + define('USERPOINTS_CATEGORY_NAME', 'Userpoints'); define('USERPOINTS_CATEGORY_DEFAULT_VID', 'userpoints_category_default_vid'); define('USERPOINTS_CATEGORY_DEFAULT_TID', 'userpoints_category_default_tid'); @@ -216,6 +218,11 @@ function userpoints_theme() { 'rows' => NULL, ), ), + 'userpoints_points' => array( + 'arguments' => array( + 'points' => NULL, + ), + ), ); //return array } @@ -292,6 +299,14 @@ function userpoints_admin_settings() { '#size' => 20, '#maxlength' => 20, ); + // Number of decimals to display + $form[$group][USERPOINTS_PRECISION_NUM] = array( + '#type' => 'textfield', + '#title' => t('Number of decimals to use when displaying and calculating !points', userpoints_translation()), + '#default_value' => variable_get(USERPOINTS_PRECISION_NUM, 0), + '#size' => 2, + '#maxlength' => 2, + ); $group = 'status'; $form[$group] = array( @@ -479,9 +494,9 @@ function userpoints_get_current_points($ $tid = userpoints_get_default_tid(); } elseif ($tid == 'all') { - return (int)db_result(db_query('SELECT SUM(points) FROM {userpoints} WHERE uid = %d', $uid)); + return (float)db_result(db_query('SELECT SUM(points) FROM {userpoints} WHERE uid = %d', $uid)); } - return (int)db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid)); + return (float)db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid)); } /** @@ -522,10 +537,10 @@ function userpoints_get_max_points($uid } /** - * @param $params(array) or (int) - * if (int) assumed to be points for current user + * @param $params(array) or (float) + * if (float) assumed to be points for current user * Accepts an array of keyed variables and parameters - * 'points' => # of points (int) (required) + * 'points' => # of points (int/float) (required) * 'moderate' => TRUE/FALSE * 'uid' => $user->uid * 'time_stamp' => unix time of the points assignement date @@ -649,7 +664,7 @@ function userpoints_userpointsapi($param array_merge(userpoints_translation(), array( '%uname' => $name, '%op' => $msg, - '%pointsvalue' => abs($params['points']), + '%pointsvalue' => theme('userpoints_points', abs($params['points'])), '%total' => userpoints_get_current_points($params['uid'], $params['tid']), )) )); @@ -659,7 +674,7 @@ function userpoints_userpointsapi($param array_merge(userpoints_translation(), array( '%uname' => $name, '%op' => $msg, - '%pointsvalue' => abs($params['points']), + '%pointsvalue' => theme('userpoints_points', abs($params['points'])), '%total' => userpoints_get_current_points($params['uid'], $params['tid']) )))); } //if $params['status'] @@ -774,19 +789,19 @@ function _userpoints_update_cache(&$para } // Calculate the current points based upon the tid - $current_points = (int)$params['points'] + userpoints_get_current_points($params['uid'], $params['tid']); + $current_points = (float)$params['points'] + userpoints_get_current_points($params['uid'], $params['tid']); //Grab the user's maximum points to preserve it $max_points = db_result(db_query('SELECT max_points FROM {userpoints} WHERE uid = %d AND tid = %d', $params['uid'], $params['tid'])); if ($params['points'] > 0) { //points are greater than zero, update their max_points - $max_points = (int)$params['points'] + (int)$max_points; + $max_points = (float)$params['points'] + (float)$max_points; } // insert or update the userpoints caching table with the user's current points if (_userpoints_user_exists($params['uid'], $params['tid'])) { db_query("UPDATE {userpoints} - SET points = %d, max_points = %d, last_update = %d + SET points = %f, max_points = %f, last_update = %d WHERE uid = %d AND tid = %d", $current_points, $max_points, @@ -798,7 +813,7 @@ function _userpoints_update_cache(&$para else { $result = db_query("INSERT INTO {userpoints} (uid, points, max_points, last_update, tid) - VALUES (%d, %d, %d, %d, %d )", + VALUES (%d, %f, %f, %d, %d )", $params['uid'], $current_points, $max_points, @@ -970,7 +985,7 @@ function userpoints_admin_manage() { $rows[] = array( array('data' => theme('username', $user)), array('data' => format_date($data->time_stamp, 'custom', 'Y-m-d H:i')), - array('data' => $data->points, 'align' => 'right'), + array('data' => theme('userpoints_points', $data->points), 'align' => 'right'), array('data' => $operation), array('data' => $data->cat), array('data' => l('approve', "admin/user/userpoints/approve/$data->txn_id") . @@ -1329,7 +1344,7 @@ function userpoints_admin_points() { $rows[] = array( array('data' => theme('username', $data) ."  ". l(t("(details)"), "myuserpoints/$data->uid")), array('data' => $data->cat, 'align' => 'right'), - array('data' => $data->points, 'align' => 'right'), + array('data' => theme('userpoints_points', $data->points), 'align' => 'right'), ); } @@ -1454,7 +1469,7 @@ function theme_userpoints_list_users_row return array( array('data' => theme('username', $row) . $details), array('data' => $row->cat, 'align' => 'right'), - array('data' => $row->points, 'align' => 'right'), + array('data' => theme('userpoints_points', $row->points), 'align' => 'right'), ); } @@ -1695,11 +1710,11 @@ function userpoints_list_my_userpoints() //maintain a grand total $grand_total += $result['total']; } - $args['approved_total'] = $grand_total; + $args['approved_total'] = theme('userpoints_points', $grand_total); //Grab the unmoderated point total - $args['unapproved_total'] = (int)db_result(db_query("SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d AND status = 1", $uid)); - $args['overall_total'] = ($args['approved_total'] + $args['unapproved_total']); + $args['unapproved_total'] = theme('userpoints_points', (float)db_result(db_query("SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d AND status = 1", $uid))); + $args['overall_total'] = theme('userpoints_points', ($args['approved_total'] + $args['unapproved_total'])); $header = array( array('data' => t('!Points', userpoints_translation()), 'field' => 'points'), @@ -1766,7 +1781,7 @@ function userpoints_list_my_userpoints() } } $rows[] = array( - array('data' => $row->points, 'align' => 'center'), + array('data' => theme('userpoints_points', $row->points), 'align' => 'center'), array('data' => $status, 'align' => 'center'), array('data' => format_date($row->time_stamp, 'small'), 'align' => 'center'), array('data' => $operation), @@ -1875,3 +1890,11 @@ function userpoints_views_api() { 'api' => 2.0, ); } + +/** + * Function to make points appear in decimal format + */ +function theme_userpoints_points($points) { + $decimal_points = number_format($points, variable_get(USERPOINTS_PRECISION_NUM, 2)); + return $decimal_points; +}