| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows users to purchase certain products with a userpoints discount.
|
| 7 |
*
|
| 8 |
* Module sponsored by http://www.horseracegame.com
|
| 9 |
* Developed by Ryan (rszrama) of Ubercart
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu().
|
| 14 |
*/
|
| 15 |
function uc_userpoints_discount_menu($may_cache) {
|
| 16 |
if ($may_cache) {
|
| 17 |
$items[] = array(
|
| 18 |
'path' => 'admin/store/customers/userpoints-discounts',
|
| 19 |
'title' => t('Userpoints discounts'),
|
| 20 |
'description' => t('Administer the userpoints discounts given to customers.'),
|
| 21 |
'callback' => 'uc_userpoints_discount_admin',
|
| 22 |
'access' => user_access('administer userpoints discounts'),
|
| 23 |
'type' => MENU_NORMAL_ITEM,
|
| 24 |
);
|
| 25 |
$items[] = array(
|
| 26 |
'path' => 'admin/store/settings/userpoints-discount',
|
| 27 |
'title' => t('Userpoints discount'),
|
| 28 |
'description' => t('Administer settings to provide userpoints discounts.'),
|
| 29 |
'callback' => 'drupal_get_form',
|
| 30 |
'callback arguments' => array('uc_userpoints_discount_admin_form'),
|
| 31 |
'access' => user_access('administer userpoints discounts'),
|
| 32 |
'type' => MENU_NORMAL_ITEM,
|
| 33 |
);
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
$items[] = array(
|
| 37 |
'path' => 'admin/store/customers/userpoints-discounts/'. arg(4) .'/delete',
|
| 38 |
'title' => t('Delete userpoints discount?'),
|
| 39 |
'description' => t('Deletes the specified userpoints discount.'),
|
| 40 |
'callback' => 'drupal_get_form',
|
| 41 |
'callback arguments' => array('uc_userpoints_discount_delete_form', arg(4)),
|
| 42 |
'access' => user_access('administer userpoints discounts'),
|
| 43 |
'type' => MENU_CALLBACK,
|
| 44 |
);
|
| 45 |
}
|
| 46 |
|
| 47 |
return $items;
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_access().
|
| 52 |
*/
|
| 53 |
function uc_userpoints_discount_perm() {
|
| 54 |
return array('administer userpoints discounts');
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Implementation of hook_cron().
|
| 59 |
*/
|
| 60 |
function uc_userpoints_discount_cron() {
|
| 61 |
// Remove any holds that expire with cart items.
|
| 62 |
db_query("DELETE FROM {uc_userpoints_discount_txns} WHERE expires > 0 AND expires <= %d", time());
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_nodeapi().
|
| 67 |
*/
|
| 68 |
function uc_userpoints_discount_nodeapi(&$node, $op, $arg3 = NULL, $arg4 = NULL) {
|
| 69 |
if (in_array($node->type, module_invoke_all('product_types'))) {
|
| 70 |
switch ($op) {
|
| 71 |
case 'insert':
|
| 72 |
case 'update':
|
| 73 |
db_query("DELETE FROM {uc_product_userpoints_discount} WHERE nid = %d", $node->nid);
|
| 74 |
if ($node->userpoints_discount_status > 0) {
|
| 75 |
db_query("INSERT INTO {uc_product_userpoints_discount} (nid, discount_status) VALUES (%d, %d)", $node->nid, $node->userpoints_discount_status);
|
| 76 |
}
|
| 77 |
break;
|
| 78 |
case 'load':
|
| 79 |
return array('userpoints_discount_status' => _product_discount_status($node->nid));
|
| 80 |
case 'delete':
|
| 81 |
db_query("DELETE FROM {uc_product_userpoints_discount} WHERE nid = %d", $node->nid);
|
| 82 |
break;
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Implementation of hook_form_alter().
|
| 89 |
*/
|
| 90 |
function uc_userpoints_discount_form_alter($form_id, &$form) {
|
| 91 |
// Add the checkbox to the product node edit form for the userpoints discount.
|
| 92 |
if (isset($form['#node'])) {
|
| 93 |
$node_type = $form['#node']->type;
|
| 94 |
|
| 95 |
if ($form_id == $node_type .'_node_form' && in_array($node_type, module_invoke_all('product_types'))) {
|
| 96 |
$form['userpoints_discount'] = array(
|
| 97 |
'#type' => 'fieldset',
|
| 98 |
'#title' => t('Userpoints discount'),
|
| 99 |
'#collapsed' => FALSE,
|
| 100 |
);
|
| 101 |
$form['userpoints_discount']['userpoints_discount_status'] = array(
|
| 102 |
'#type' => 'radios',
|
| 103 |
'#title' => t('Userpoints discount status'),
|
| 104 |
'#options' => array(
|
| 105 |
0 => t('This product must be bought with cold hard cash.'),
|
| 106 |
1 => t('Allow this product to be purchased with a userpoints discount.'),
|
| 107 |
2 => t('This product can only be purchased with userpoints.'),
|
| 108 |
),
|
| 109 |
'#default_value' => $form['#node']->userpoints_discount_status,
|
| 110 |
);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
// Add the button to purchase a product with a userpoints discount to the add
|
| 115 |
// to cart form on applicable products.
|
| 116 |
if (strpos($form_id, 'uc_product_add_to_cart_form') === 0) {
|
| 117 |
if (($status = _product_discount_status($form['nid']['#value'])) > 0) {
|
| 118 |
// Get the customer's total available userpoints.
|
| 119 |
$points = _customer_available_points();
|
| 120 |
|
| 121 |
if ($points > 0) {
|
| 122 |
// Figure out the value of the product in points.
|
| 123 |
$price = db_result(db_query("SELECT sell_price FROM {uc_products} WHERE nid = %d", $form['nid']['#value']));
|
| 124 |
$rate = _userpoints_discount_rate();
|
| 125 |
$max_points = $price * $rate;
|
| 126 |
|
| 127 |
if ($points > $max_points) {
|
| 128 |
$points = $max_points;
|
| 129 |
}
|
| 130 |
|
| 131 |
if ($status == 1 || ($status == 2 && $points == $max_points)) {
|
| 132 |
$form['submit']['#attributes']['class'] = 'add-to-cart';
|
| 133 |
|
| 134 |
// Add the special add to cart button.
|
| 135 |
$form['userpoints_add_to_cart'] = array(
|
| 136 |
'#type' => 'submit',
|
| 137 |
'#value' => t('Add to cart using @points points, @amount Discount', array('@points' => $points, '@amount' => uc_currency_format(round($points / $rate, 2)))),
|
| 138 |
'#attributes' => array('class' => 'points-add-to-cart'),
|
| 139 |
);
|
| 140 |
$form['discount_points'] = array(
|
| 141 |
'#type' => 'value',
|
| 142 |
'#value' => $points,
|
| 143 |
);
|
| 144 |
$form['#validate']['uc_userpoints_discount_add_to_cart_validate'] = array();
|
| 145 |
}
|
| 146 |
|
| 147 |
if ($status == 2) {
|
| 148 |
unset($form['submit']);
|
| 149 |
|
| 150 |
if ($points < $max_points) {
|
| 151 |
$form['points_message'] = array(
|
| 152 |
'#value' => '<div class="need-more-points"></div>',
|
| 153 |
);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
}
|
| 158 |
}
|
| 159 |
|
| 160 |
// Disable the appropriate Qty. fields on the cart view form.
|
| 161 |
if ($form_id == 'uc_cart_view_form') {
|
| 162 |
static $messaged = 0;
|
| 163 |
|
| 164 |
$rate = _userpoints_discount_rate();
|
| 165 |
$items = array('#tree' => TRUE);
|
| 166 |
$count = 0;
|
| 167 |
|
| 168 |
unset($form['items']['#tree']);
|
| 169 |
|
| 170 |
foreach ($form['items'] as $key => $value) {
|
| 171 |
// Add the item to the new items array.
|
| 172 |
$items[$count] = $value;
|
| 173 |
$count++;
|
| 174 |
$saved = 0;
|
| 175 |
$points = 0;
|
| 176 |
|
| 177 |
$data = unserialize($value['data']['#value']);
|
| 178 |
|
| 179 |
// If it's a discounted item, add another item row for the discount.
|
| 180 |
if (isset($data['discounted'])) {
|
| 181 |
$items[$count - 1]['qty']['#discount'] = $data['discounted'];
|
| 182 |
$items[$count - 1]['qty']['#theme'] = 'testing_theme_func';
|
| 183 |
|
| 184 |
$items[$count]['remove'] = array('#value' => '-');
|
| 185 |
$items[$count]['title'] = array('#value' => t('Points discount (@points pts)', array('@points' => number_format($data['discounted']))));
|
| 186 |
$items[$count]['#total'] = -round($data['discounted'] / $rate, 2);
|
| 187 |
|
| 188 |
$saved -= $items[$count]['#total'];
|
| 189 |
$points += $data['discounted'];
|
| 190 |
|
| 191 |
$messaged++;
|
| 192 |
$count++;
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
$form['items'] = $items;
|
| 197 |
|
| 198 |
if ($messaged == 1 && empty($form['#post'])) {
|
| 199 |
drupal_set_message(t('You will save a total of @saved using @points points!', array('@saved' => uc_currency_format($saved), '@points' => number_format($points))));
|
| 200 |
$messaged++;
|
| 201 |
}
|
| 202 |
|
| 203 |
// Add a submit function to remove holds when necessary.
|
| 204 |
$form['#submit'] = array_merge(array('uc_userpoints_discount_cart_view_submit' => array()), $form['#submit']);
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
// Validation function for the add to cart form.
|
| 209 |
function uc_userpoints_discount_add_to_cart_validate($form_id, $form_values) {
|
| 210 |
if (isset($form_values['discount_points'])) {
|
| 211 |
if (_customer_available_points() < $form_values['discount_points']) {
|
| 212 |
form_set_error('userpoints_add_to_cart', t('You do not have enough points for this discount.'));
|
| 213 |
}
|
| 214 |
}
|
| 215 |
}
|
| 216 |
|
| 217 |
// Submit function for the cart view form.
|
| 218 |
function uc_userpoints_discount_cart_view_submit($form_id, $form_values) {
|
| 219 |
if (is_array($form_values['items'])) {
|
| 220 |
foreach ($form_values['items'] as $item) {
|
| 221 |
if ($item['remove']) {
|
| 222 |
$data = unserialize($item['data']);
|
| 223 |
if (intval($data['discounted']) > 0) {
|
| 224 |
uc_userpoints_discount_remove_hold($data['txn_id']);
|
| 225 |
}
|
| 226 |
}
|
| 227 |
}
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Implementation of hook_add_to_cart_data().
|
| 233 |
*/
|
| 234 |
function uc_userpoints_discount_add_to_cart_data($form_values) {
|
| 235 |
if (isset($form_values['discount_points']) && $form_values['op'] == $form_values['userpoints_add_to_cart']) {
|
| 236 |
// Fail if we don't have enough available points.
|
| 237 |
if ($form_values['discount_points'] > _customer_available_points()) {
|
| 238 |
drupal_set_message(t('Your account did not have enough points for that discount.'));
|
| 239 |
return array();
|
| 240 |
}
|
| 241 |
|
| 242 |
// Otherwise add the discount to the product and put it in the cart.
|
| 243 |
$txn_id = uc_userpoints_discount_add_hold($form_values['discount_points']);
|
| 244 |
|
| 245 |
return array('discounted' => $form_values['discount_points'], 'txn_id' => $txn_id);
|
| 246 |
}
|
| 247 |
}
|
| 248 |
|
| 249 |
/**
|
| 250 |
* Implementation of hook_order().
|
| 251 |
*/
|
| 252 |
function uc_userpoints_discount_order($op, &$arg1, $arg2) {
|
| 253 |
switch ($op) {
|
| 254 |
case 'save':
|
| 255 |
if ($arg1->order_status == 'in_checkout') {
|
| 256 |
// Delete any existing userpoints discount line items for this order.
|
| 257 |
db_query("DELETE FROM {uc_order_line_items} WHERE order_id = %d AND type = 'userpoints_discount'", $arg1->order_id);
|
| 258 |
|
| 259 |
foreach ($arg1->products as $product) {
|
| 260 |
if (intval($product->data['discounted']) > 0) {
|
| 261 |
// Add a line item for each product with a discount.
|
| 262 |
uc_order_line_item_add($arg1->order_id, 'userpoints_discount', t('Points discount'), -round($product->data['discounted'] / _userpoints_discount_rate(), 2));
|
| 263 |
|
| 264 |
// Update the transactions with the order ID.
|
| 265 |
db_query("UPDATE {uc_userpoints_discount_txns} SET order_id = %d WHERE txn_id = %d", $arg1->order_id, $product->data['txn_id']);
|
| 266 |
}
|
| 267 |
}
|
| 268 |
}
|
| 269 |
break;
|
| 270 |
case 'update':
|
| 271 |
foreach (uc_order_status_list('general') as $data) {
|
| 272 |
$statuses[] = $data['id'];
|
| 273 |
}
|
| 274 |
if (uc_order_status_data($arg1->order_status, 'state') == 'in_checkout' && in_array($arg2, $statuses)) {
|
| 275 |
uc_userpoints_discount_complete_txns($arg1->order_id);
|
| 276 |
}
|
| 277 |
break;
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
/**
|
| 282 |
* Implementation of hook_line_item().
|
| 283 |
*/
|
| 284 |
function uc_userpoints_discount_line_item() {
|
| 285 |
$items[] = array(
|
| 286 |
'id' => 'userpoints_discount',
|
| 287 |
'title' => t('Points discount'),
|
| 288 |
'weight' => 6,
|
| 289 |
'stored' => TRUE,
|
| 290 |
'add_list' => TRUE,
|
| 291 |
'calculated' => TRUE,
|
| 292 |
'callback' => 'uc_line_item_userpoints_discount',
|
| 293 |
);
|
| 294 |
|
| 295 |
return $items;
|
| 296 |
}
|
| 297 |
|
| 298 |
// Handles the gift card line item.
|
| 299 |
function uc_line_item_userpoints_discount($op, $arg1) {
|
| 300 |
global $user;
|
| 301 |
|
| 302 |
switch ($op) {
|
| 303 |
case 'cart-preview':
|
| 304 |
if (module_exists('uc_payment') && variable_get('uc_pane_payment_enabled', TRUE)) {
|
| 305 |
$count = 1;
|
| 306 |
|
| 307 |
// Loop through the items in the cart to find discounts.
|
| 308 |
foreach (uc_cart_get_contents() as $item) {
|
| 309 |
if (intval($item->data['discounted']) > 0) {
|
| 310 |
// Calculate the amount of the discount and display a message.
|
| 311 |
$amount = round($item->data['discounted'] / _userpoints_discount_rate(), 2);
|
| 312 |
if (empty($_POST)) {
|
| 313 |
drupal_set_message(t('@amount discount added for %product using @points points.', array('@amount' => uc_currency_format($amount), '%product' => $item->title, '@points' => number_format($item->data['discounted']))));
|
| 314 |
}
|
| 315 |
|
| 316 |
// Add the discount line item to the order total preview.
|
| 317 |
drupal_add_js("\$(document).ready( function() { set_line_item('userpoints_discount_". $count ."', '". t('Points discount (@points pts)', array('@points' => number_format($item->data['discounted']))) ."', ". -$amount .", 15); } );", 'inline');
|
| 318 |
|
| 319 |
// Increment our counter so we have unique line items.
|
| 320 |
$count++;
|
| 321 |
}
|
| 322 |
}
|
| 323 |
}
|
| 324 |
break;
|
| 325 |
}
|
| 326 |
}
|
| 327 |
|
| 328 |
// Admin page for userpoints discounts granted to customers.
|
| 329 |
function uc_userpoints_discount_admin() {
|
| 330 |
$header = array(
|
| 331 |
array('data' => t('Discount ID'), 'field' => 'txn_id', 'sort' => 'desc'),
|
| 332 |
array('data' => t('User ID'), 'field' => 'uid'),
|
| 333 |
array('data' => t('Order ID'), 'field' => 'order_id'),
|
| 334 |
array('data' => t('Points'), 'field' => 'amount'),
|
| 335 |
array('data' => t('Status'), 'field' => 'txn_status'),
|
| 336 |
t('Operations'),
|
| 337 |
);
|
| 338 |
|
| 339 |
$rows = array();
|
| 340 |
|
| 341 |
$sql = 'SELECT t.*, u.name FROM {uc_userpoints_discount_txns} AS t LEFT JOIN {users} AS u ON t.uid = u.uid'. tablesort_sql($header);
|
| 342 |
$result = pager_query($sql, 30);
|
| 343 |
while ($txn = db_fetch_array($result)) {
|
| 344 |
$ops = array();
|
| 345 |
|
| 346 |
$ops[] = l(t('delete'), 'admin/store/customers/userpoints-discounts/'. $txn['txn_id'] .'/delete');
|
| 347 |
|
| 348 |
$rows[] = array(
|
| 349 |
$txn['txn_id'],
|
| 350 |
l($txn['name'], 'user/'. $txn['uid']),
|
| 351 |
$txn['order_id'] > 0 ? l($txn['order_id'], 'admin/store/orders/'. $txn['order_id']) : '-',
|
| 352 |
$txn['amount'],
|
| 353 |
$txn['txn_status'] == 0 ? t('On hold') : t('Completed'),
|
| 354 |
implode(' ', $ops),
|
| 355 |
);
|
| 356 |
}
|
| 357 |
|
| 358 |
return theme('table', $header, $rows) . theme_pager();
|
| 359 |
}
|
| 360 |
|
| 361 |
// Displays a confirmation form for deleting a userpoints discount.
|
| 362 |
function uc_userpoints_discount_delete_form($txn_id) {
|
| 363 |
if (intval($txn_id) == 0) {
|
| 364 |
drupal_goto('admin/store/customers/userpoints-discounts');
|
| 365 |
}
|
| 366 |
|
| 367 |
$name = db_result(db_query("SELECT u.name FROM {users} AS u LEFT JOIN {uc_userpoints_discount_txns} AS t ON u.uid = t.uid WHERE t.txn_id = %d", $txn_id));
|
| 368 |
|
| 369 |
$form = confirm_form(array(), t('Are you sure you wish to delete discount %txn_id for user %name?', array('%txn_id' => $txn_id, '%name' => $name)), 'admin/store/customers/userpoints-discounts');
|
| 370 |
|
| 371 |
$form['txn_id'] = array(
|
| 372 |
'#type' => 'value',
|
| 373 |
'#value' => $txn_id,
|
| 374 |
);
|
| 375 |
|
| 376 |
return $form;
|
| 377 |
}
|
| 378 |
|
| 379 |
function uc_userpoints_discount_delete_form_submit($form_id, $form_values) {
|
| 380 |
uc_userpoints_discount_delete($form_values['txn_id']);
|
| 381 |
|
| 382 |
return 'admin/store/customers/userpoints-discounts';
|
| 383 |
}
|
| 384 |
|
| 385 |
// Form builder for the module settings form.
|
| 386 |
function uc_userpoints_discount_admin_form() {
|
| 387 |
$form['uc_userpoints_discount_rate'] = array(
|
| 388 |
'#type' => 'textfield',
|
| 389 |
'#title' => t('Userpoints exchange rate'),
|
| 390 |
'#description' => t('Specify the exchange rate used for discounts on purchases.'),
|
| 391 |
'#default_value' => variable_get('uc_userpoints_discount_rate', 1000),
|
| 392 |
'#size' => 10,
|
| 393 |
'#field_suffix' => ' points = '. uc_currency_format(1),
|
| 394 |
);
|
| 395 |
|
| 396 |
return system_settings_form($form);
|
| 397 |
}
|
| 398 |
|
| 399 |
// Themes discounted item Qty. boxes so they can't be changed on the cart form.
|
| 400 |
function theme_testing_theme_func($element) {
|
| 401 |
return check_plain($element['#value']);
|
| 402 |
}
|
| 403 |
|
| 404 |
// Adds a hold on userpoints and returns the transaction ID.
|
| 405 |
function uc_userpoints_discount_add_hold($points, $uid = NULL) {
|
| 406 |
global $user;
|
| 407 |
|
| 408 |
if (empty($uid)) {
|
| 409 |
$uid = $user->uid;
|
| 410 |
}
|
| 411 |
|
| 412 |
if ($uid == 0) {
|
| 413 |
return 0;
|
| 414 |
}
|
| 415 |
|
| 416 |
$time = strtotime('+'. variable_get('uc_cart_auth_duration', '1') .' '. variable_get('uc_cart_auth_unit', 'years'));
|
| 417 |
|
| 418 |
$txn_id = db_next_id('{uc_userpoints_discount_txns}_txn_id');
|
| 419 |
db_query("INSERT INTO {uc_userpoints_discount_txns} (txn_id, uid, order_id, amount, txn_status, expires) VALUES (%d, %d, 0, %d, 0, %d)", $txn_id, $uid, $points, $time);
|
| 420 |
|
| 421 |
return $txn_id;
|
| 422 |
}
|
| 423 |
|
| 424 |
// Removes a hold when an item gets tossed from the cart.
|
| 425 |
function uc_userpoints_discount_remove_hold($txn_id) {
|
| 426 |
db_query("DELETE FROM {uc_userpoints_discount_txns} WHERE txn_id = %d", $txn_id);
|
| 427 |
}
|
| 428 |
|
| 429 |
// Updates transactions to a completed status.
|
| 430 |
function uc_userpoints_discount_complete_txns($order_id) {
|
| 431 |
$result = db_query("SELECT * FROM {uc_userpoints_discount_txns} WHERE order_id = %d", $order_id);
|
| 432 |
while ($txn = db_fetch_array($result)) {
|
| 433 |
// Add an order comment about the deduction.
|
| 434 |
uc_order_comment_save($order_id, 0, t('@points points deducted for a points discount.', array('@points' => $txn['amount'])), 'order', 'pending');
|
| 435 |
|
| 436 |
// Deduct the userpoints from the user's account.
|
| 437 |
$return = userpoints_userpointsapi(array('points' => -$txn['amount'], 'description' => t('Points discount on <a href="!url">Order @order_id</a>.', array('!url' => url('user/'. $txn['uid'] .'/order/'. $order_id), '@order_id' => $order_id))));
|
| 438 |
|
| 439 |
// Update the transaction status.
|
| 440 |
db_query("UPDATE {uc_userpoints_discount_txns} SET txn_status = 1, expires = 0 WHERE txn_id = %d", $txn['txn_id']);
|
| 441 |
}
|
| 442 |
}
|
| 443 |
|
| 444 |
// Deletes a userpoints discount.
|
| 445 |
function uc_userpoints_discount_delete($txn_id) {
|
| 446 |
db_query("DELETE FROM {uc_userpoints_discount_txns} WHERE txn_id = %d", $txn_id);
|
| 447 |
}
|
| 448 |
|
| 449 |
/**
|
| 450 |
* Returns the userpoints discount status of the product:
|
| 451 |
* 0 = Cannot be purchased with userpoints.
|
| 452 |
* 1 = Can be purchased with a userpoints discount.
|
| 453 |
* 2 = Can only be purchased with userpoints.
|
| 454 |
*/
|
| 455 |
function _product_discount_status($nid) {
|
| 456 |
$status = db_result(db_query("SELECT discount_status FROM {uc_product_userpoints_discount} WHERE nid = %d", $nid));
|
| 457 |
|
| 458 |
return $status >= 1 ? $status : 0;
|
| 459 |
}
|
| 460 |
|
| 461 |
// Returns the number of available userpoints for a customer.
|
| 462 |
function _customer_available_points($uid = NULL) {
|
| 463 |
global $user;
|
| 464 |
|
| 465 |
if (empty($uid)) {
|
| 466 |
$uid = $user->uid;
|
| 467 |
}
|
| 468 |
|
| 469 |
if ($uid == 0) {
|
| 470 |
return 0;
|
| 471 |
}
|
| 472 |
|
| 473 |
// Get the current userpoints total.
|
| 474 |
$points = userpoints_get_current_points($uid);
|
| 475 |
|
| 476 |
// Get the current number of held userpoints from the transaction table.
|
| 477 |
$held = db_result(db_query("SELECT SUM(amount) FROM {uc_userpoints_discount_txns} WHERE uid = %d AND txn_status = 0", $uid));
|
| 478 |
|
| 479 |
return $points - $held;
|
| 480 |
}
|
| 481 |
|
| 482 |
// Returns the exchange rate as userpoints per $1.
|
| 483 |
function _userpoints_discount_rate() {
|
| 484 |
return intval(variable_get('uc_userpoints_discount_rate', 1000));
|
| 485 |
}
|