--- userpoints.install Mon Jan 19 23:26:36 1970 +++ userpoints.install Mon Jan 19 23:26:36 1970 @@ -26,6 +26,12 @@ 'not null' => TRUE, 'default' => 0, ), + 'points_dec' => array( + 'description' => 'Current decimal Points', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), 'max_points' => array( 'description' => 'Out of a maximum points', 'type' => 'int', @@ -78,6 +84,12 @@ 'not null' => TRUE, 'default' => 0, ), + 'points_dec' => array( + 'description' => 'Decimal Points', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), 'time_stamp' => array( 'description' => 'Timestamp', 'type' => 'int', @@ -279,5 +291,27 @@ $ret = array(); db_drop_index($ret, 'userpoints_txn', 'status'); db_add_index($ret, 'userpoints_txn', 'status_expired_expiry', array('status', 'expired', 'expirydate')); + return $ret; +} + +/** + * Adding decimal points. + */ +function userpoints_update_6013() { + $ret = array(); + db_add_field($ret, 'userpoints', 'points_dec', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'Current decimal Points', + ) + ); + db_add_field($ret, 'userpoints_txn', 'points_dec', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'Decimal Points', + ) + ); return $ret; } --- userpoints.module Mon Jan 19 23:26:36 1970 +++ userpoints.module Mon Jan 19 23:26:36 1970 @@ -39,7 +39,9 @@ define('USERPOINTS_CATEGORY_DEFAULT_TID', 'userpoints_category_default_tid'); define('USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID', 'userpoints_category_profile_display_tid'); define('USERPOINTS_TRANSACTION_TIMESTAMP', 'userpoints_transaction_timestamp'); - +define('USERPOINTS_DECIMAL_POINT', 'userpoints_decimal_point'); +define('USERPOINTS_THOUSANDS_SEPARATOR', 'userpoints_thousands_separator'); +define('USERPOINTS_DECIMAL_PLACES', 'userpoints_decimal_places'); /** * Purpose: Returns an array of common translation placeholders @@ -227,7 +229,7 @@ function userpoints_token_values($type, $object = NULL, $options = array()) { if ($type == 'user') { $tokens = array( - 'userpoints' => userpoints_get_current_points($object->uid), + 'userpoints' => userpoints_catenate(userpoints_get_current_points($object->uid), userpoints_get_current_points($object->uid, NULL, TRUE)), ); return $tokens; } @@ -462,6 +464,35 @@ '#default_value' => variable_get(USERPOINTS_TRANSACTION_TIMESTAMP, 1), '#description' => t('Sets if the transaction timestamp should obey current time, or can be modified by the API operations. Unchecking this option will allow customization of timetamp for the transactions.'), ); + // New configuration options to overide current timestamp + $group = "decimal"; + $form[$group] = array( + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#title' => t('Decimal settings'), + ); + $form[$group][USERPOINTS_DECIMAL_PLACES] = array( + '#type' => 'select', + '#title' => t('Decimal places'), + '#default_value' => variable_get(USERPOINTS_DECIMAL_PLACES, 0), + '#options' => array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8), + '#description' => t('Set the number of decimal places that will be used.'), + ); + $form[$group][USERPOINTS_DECIMAL_POINT] = array( + '#type' => 'textfield', + '#size' => 1, + '#title' => t('Decimal point'), + '#default_value' => variable_get(USERPOINTS_DECIMAL_POINT, '.'), + '#description' => t('Set the decimal point that will be used.'), + ); + $form[$group][USERPOINTS_THOUSANDS_SEPARATOR] = array( + '#type' => 'textfield', + '#size' => 1, + '#title' => t('Thousands separator'), + '#default_value' => variable_get(USERPOINTS_THOUSANDS_SEPARATOR, ','), + '#description' => t('Set the thousands separator that will be used.'), + ); $form['setting'] = module_invoke_all('userpoints', 'setting'); return system_settings_form($form); @@ -472,7 +503,7 @@ * * @return number of current points in that user's account */ -function userpoints_get_current_points($uid = NULL, $tid = NULL) { +function userpoints_get_current_points($uid = NULL, $tid = NULL, $decimal = FALSE) { if (!$uid) { global $user; $uid = $user->uid; @@ -481,8 +512,14 @@ $tid = userpoints_get_default_tid(); } elseif ($tid == 'all') { + if ($decimal) { + return (int)db_result(db_query('SELECT SUM(points_dec) FROM {userpoints} WHERE uid = %d', $uid)); + } return (int)db_result(db_query('SELECT SUM(points) FROM {userpoints} WHERE uid = %d', $uid)); } + if ($decimal) { + return (int)db_result(db_query('SELECT points_dec FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid)); + } return (int)db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d AND tid = %d', $uid, $tid)); } @@ -524,10 +561,10 @@ } /** - * @param $params(array) or (int) - * if (int) assumed to be points for current user + * @param $params(array) or (float) or (int) + * if (float) or (int) assumed to be points for current user * Accepts an array of keyed variables and parameters - * 'points' => # of points (int) (required) + * 'points' => # of points (float) or (int) (required) * 'moderate' => TRUE/FALSE * 'uid' => $user->uid * 'time_stamp' => unix time of the points assignement date @@ -612,6 +649,11 @@ ); } + // Split the full points from the decimals + $points = userpoints_split_floats($params['points']); + $params['points'] = $points['points']; + $params['points_dec'] = $points['points_dec']; + // Call the _userpoints hook, and stop if one of them returns FALSE $rc = module_invoke_all('userpoints', 'points before', $params); @@ -624,7 +666,7 @@ ); } } - if (isset($params['points']) && $params['points'] < 0) { + if ((isset($params['points']) || isset($params['points_dec'])) && ($params['points'] < 0 || $params['points_dec'] < 0)) { $msg = t('lost'); } elseif (isset($params['status']) && $params['status'] == USERPOINTS_TXN_STATUS_DECLINED) { @@ -652,7 +694,7 @@ '%uname' => $name, '%op' => $msg, '%pointsvalue' => abs($params['points']), - '%total' => userpoints_get_current_points($params['uid'], $params['tid']), + '%total' => userpoints_catenate(userpoints_get_current_points($params['uid'], $params['tid']), userpoints_get_current_points($params['uid'], $params['tid'], TRUE)), )) )); } @@ -662,7 +704,7 @@ '%uname' => $name, '%op' => $msg, '%pointsvalue' => abs($params['points']), - '%total' => userpoints_get_current_points($params['uid'], $params['tid']) + '%total' => userpoints_catenate(userpoints_get_current_points($params['uid'], $params['tid']), userpoints_get_current_points($params['uid'], $params['tid'], TRUE)), )))); } //if $params['status'] @@ -689,7 +731,7 @@ if (!isset($params['txn_id'])) { //If a txn_id is preset we UPDATE the record instead of adding one //the standard checks don't apply - if (!is_numeric($params['points'])) { + if (!is_numeric($params['points']) || !is_numeric($params['points_dec'])) { return FALSE; } if (!isset($params['uid'])) { @@ -735,7 +777,7 @@ if (!empty($params['txn_id']) && is_numeric($params['txn_id'])) { //A transaction ID was passed in so we'll update the transaction - $result = db_query("SELECT txn_id, uid, approver_uid, points, + $result = db_query("SELECT txn_id, uid, approver_uid, points, points_dec, time_stamp, status, operation, description, reference, expirydate, expired, parent_txn_id, tid, entity_id, entity_type FROM {userpoints_txn} @@ -776,7 +818,17 @@ } // Calculate the current points based upon the tid + $current_points_dec = (int)$params['points_dec'] + userpoints_get_current_points($params['uid'], $params['tid'], TRUE); + if ($current_points_dec < 0) { + $params['points']--; + $current_points_dec += 100000000; + } + else if ($current_points_dec > 99999999) { + $params['points']++; + $current_points_dec -= 100000000; + } $current_points = (int)$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'])); @@ -788,9 +840,10 @@ // 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 = %d, points_dec = %d, max_points = %d, last_update = %d WHERE uid = %d AND tid = %d", $current_points, + $current_points_dec, $max_points, time(), $params['uid'], @@ -799,10 +852,11 @@ } else { $result = db_query("INSERT INTO {userpoints} - (uid, points, max_points, last_update, tid) - VALUES (%d, %d, %d, %d, %d )", + (uid, points, points_dec, max_points, last_update, tid) + VALUES (%d, %d, %d, %d, %d, %d )", $params['uid'], $current_points, + $current_points_dec, $max_points, time(), $params['tid'] @@ -879,6 +933,8 @@ //Which points are we display? $points = userpoints_get_current_points($account->uid, variable_get(USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID, 0)); + $points_dec = userpoints_get_current_points($account->uid, variable_get(USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID, 0), TRUE); + $points = userpoints_catenate($points, $points_dec); $account->content['userpoints'] = array( '#type' => 'user_profile_category', @@ -907,7 +963,7 @@ array('data' => t('Operation')), ); - $sql = "SELECT p.txn_id, p.uid, p.time_stamp, p.points, p.operation, p.status, + $sql = "SELECT p.txn_id, p.uid, p.time_stamp, p.points, p.points_dec, p.operation, p.status, p.entity_type, p.entity_id, t.name as cat FROM {userpoints_txn} p LEFT JOIN {term_data} t ON p.tid = t.tid @@ -972,7 +1028,7 @@ $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' => userpoints_catenate($data->points, $data->points_dec), 'align' => 'right'), array('data' => $operation), array('data' => $data->cat), array('data' => l('approve', "admin/user/userpoints/approve/$data->txn_id") . @@ -1090,8 +1146,8 @@ '#type' => 'textfield', '#title' => t('Points'), '#size' => 10, - '#maxlength' => 10, - '#default_value' => isset($txn->points) ? $txn->points : 0, + '#maxlength' => 21, + '#default_value' => isset($txn->points) || isset($txn->points_dec) ? userpoints_catenate($txn->points, $txn->points_dec) : 0, '#description' => t('Number of !points to add/subtract from the user. For example, 25 (to add !points) or -25 (to subtract !points).', userpoints_translation()), ); @@ -1284,7 +1340,7 @@ $tid = arg(3); $cat_count = count(userpoints_get_categories()); - $sql = "SELECT p.uid, u.name, p.points, p.tid, t.name as cat + $sql = "SELECT p.uid, u.name, p.points, p.points_dec, p.tid, t.name as cat FROM {userpoints} p INNER JOIN {users} u USING (uid) LEFT JOIN {term_data} t ON p.tid = t.tid "; @@ -1308,12 +1364,12 @@ WHERE tid = %d "; - if (variable_get(USERPOINTS_REPORT_DISPLAYZERO, 1) == 0 ) { + if (variable_get(USERPOINTS_REPORT_DISPLAYZERO, 1) == 0) { //The user would NOT like to see users with zero points - $sql .= " AND p.points <> 0"; - $sql_cnt .= " AND points <> 0"; + $sql .= " AND (p.points <> 0 OR p.points_dec <> 0)"; + $sql_cnt .= " AND (points <> 0 OR points_dec <> 0)"; } - $sql .= " GROUP BY p.uid, u.name, p.points, p.tid, t.name"; + $sql .= " GROUP BY p.uid, u.name, p.points, p.points_dec, p.tid, t.name"; $header = array( array('data' => t('User'), 'field' => 'u.name'), @@ -1331,7 +1387,7 @@ $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' => userpoints_catenate($data->points, $data->points_dec), 'align' => 'right'), ); } @@ -1356,7 +1412,7 @@ function userpoints_list_users() { $tid = arg(1); - $sql = "SELECT p.uid, u.name, p.points, p.tid, t.name as cat + $sql = "SELECT p.uid, u.name, p.points, p.points_dec, p.tid, t.name as cat FROM {userpoints} p INNER JOIN {users} u USING (uid) LEFT JOIN {term_data} t ON p.tid = t.tid "; @@ -1381,12 +1437,12 @@ WHERE tid = %d "; - if (variable_get(USERPOINTS_REPORT_DISPLAYZERO, 1) == 0 ) { + if (variable_get(USERPOINTS_REPORT_DISPLAYZERO, 1) == 0) { //The user would NOT like to see users with zero points - $sql .= " AND p.points <> 0"; - $sql_cnt .= " AND points <> 0"; + $sql .= " AND (p.points <> 0 OR p.points_dec <> 0)"; + $sql_cnt .= " AND (points <> 0 OR points_dec <> 0)"; } - $sql .= " GROUP BY p.uid, u.name, p.points, p.tid, t.name"; + $sql .= " GROUP BY p.uid, u.name, p.points, p.points_dec, p.tid, t.name"; $header = theme('userpoints_list_users_header'); $sql .= tablesort_sql($header); $pager_limit = variable_get(USERPOINTS_REPORT_USERCOUNT, 30); @@ -1456,7 +1512,7 @@ return array( array('data' => theme('username', $row) . $details), array('data' => $row->cat, 'align' => 'right'), - array('data' => $row->points, 'align' => 'right'), + array('data' => userpoints_catenate($row->points, $row->points_dec), 'align' => 'right'), ); } @@ -1647,10 +1703,6 @@ * no URL argument returns current user otherwise accepts uid. */ function userpoints_list_my_userpoints() { - $overall_total = 0; - $unapproved_total = 0; - $approved_total = 0; - global $user; // User which are displaying points for @@ -1686,22 +1738,29 @@ GROUP BY p.tid, t.name"; $results = db_query($sql, $uid); $grand_total = 0; + $grand_total_dec = 0; while ($result = db_fetch_array($results)) { if ($result['name'] == NULL) { $result['name'] = t('!Uncategorized', userpoints_translation()); } //pull the sum from the caching table for resource reason and b/c the $result['total'] = userpoints_get_current_points($uid, $result['tid']); + $result['total_dec'] = userpoints_get_current_points($uid, $result['tid'], TRUE); $args['subtotals'][$result['tid']] = $result; //maintain a grand total $grand_total += $result['total']; + $grand_total_dec += $result['total_dec']; } - $args['approved_total'] = $grand_total; + $args['approved_total'] = userpoints_catenate($grand_total, $grand_total_dec); //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']); + $unapproved_total = (int)db_result(db_query("SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d AND status = 1", $uid)); + $unapproved_total_dec = (int)db_result(db_query("SELECT SUM(points_dec) FROM {userpoints_txn} WHERE uid = %d AND status = 1", $uid)); + $args['unapproved_total'] = userpoints_catenate($unapproved_total, $unapproved_total_dec); + + // Calculate overall total + $args['overall_total'] = userpoints_catenate($grand_total + $unapproved_total, $grand_total_dec + $unapproved_total_dec); $header = array( array('data' => t('!Points', userpoints_translation()), 'field' => 'points'), @@ -1711,7 +1770,7 @@ array('data' => t('Category'), 'field' => 'cat'), array('data' => t('Description'), 'field' => 'description'), ); - $sql = "SELECT p.points, p.time_stamp, p.operation, p.description, p.status, p.tid, t.name as cat , p.entity_id, p.entity_type + $sql = "SELECT p.points, p.points_dec, p.time_stamp, p.operation, p.description, p.status, p.tid, t.name as cat , p.entity_id, p.entity_type FROM {userpoints_txn} p LEFT JOIN {term_data} t on p.tid = t.tid WHERE p.uid = %d"; @@ -1765,7 +1824,7 @@ } } $rows[] = array( - array('data' => $row->points, 'align' => 'center'), + array('data' => userpoints_catenate($row->points, $row->points_dec), 'align' => 'center'), array('data' => $status, 'align' => 'center'), array('data' => format_date($row->time_stamp, 'small'), 'align' => 'center'), array('data' => $operation), @@ -1852,7 +1911,7 @@ $output .= "
";
if (isset($args) && isset($args['subtotals'])) {
foreach ($args['subtotals'] as $tid => $data) {
- $output .= ''. $data['name'] .' '. t('!points Balance', userpoints_translation()) .': '. $data['total'] .'
';
+ $output .= ''. $data['name'] .' '. t('!points Balance', userpoints_translation()) .': '. userpoints_catenate($data['total'], $data['total_dec']) .'
';
}
}
$output .= "