variable_get(USERPOINTS_TRANS_UCPOINTS, 'Points'), '!points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'), '!point' => variable_get(USERPOINTS_TRANS_LCPOINT, 'point') ); } return $trans; } function userpoints_help($section) { switch ($section) { case 'admin/settings/userpoints': $output = t('Users earn !points as they post nodes, comments, and vote on nodes', userpoints_translation()); break; } return $output; } function userpoints_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'userpoints', 'callback' => 'userpoints_list_users', 'title' => t('Users by !Points', userpoints_translation()), 'access' => user_access(USERPOINTS_PERM_VIEW), 'type' => MENU_NORMAL_ITEM); $items[] = array( 'path' => 'admin/settings/userpoints', 'title' => t('Userpoints'), 'description' => t('Userpoints settings.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'userpoints_settings', 'access' => user_access(USERPOINTS_PERM_ADMIN), 'type' => MENU_NORMAL_ITEM,); } return $items; } function userpoints_perm() { return array (USERPOINTS_PERM_VIEW, USERPOINTS_PERM_USE, USERPOINTS_PERM_ADMIN); } function userpoints_settings() { $group = 'points'; $form[$group] = array( '#type' => 'fieldset', '#collapsible' => true, '#title' => t('!Points for each event', userpoints_translation()), ); foreach(node_get_types() as $type => $name) { $form[$group][USERPOINTS_POST. $type] = array( '#type' => 'textfield', '#title' => t('!Points for posting a !node-name', array_merge(userpoints_translation(), array('!node-name' => $name->name))), '#default_value' => variable_get(USERPOINTS_POST. $type, '0'), '#size' => 5, '#maxlength' => 5, ); } $form[$group][USERPOINTS_POST_COMMENT] = array( '#type' => 'textfield', '#title' => t('!Points for posting a Comment', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_POST_COMMENT, 0), '#size' => 5, '#maxlength' => 5, ); $form[$group][USERPOINTS_MODERATE_COMMENT] = array( '#type' => 'textfield', '#title' => t('!Points for moderating a Comment', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_MODERATE_COMMENT, 0), '#size' => 5, '#maxlength' => 5, ); if (module_exists('nodevote')) { $form[$group][USERPOINTS_NODEVOTE] = array( '#type' => 'textfield', '#title' => t('!Points for voting on a node (requires nodevote module)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_NODEVOTE, 0), '#size' => 5, '#maxlength' => 5, ); } if (module_exists('referral')) { $form[$group][USERPOINTS_REFERRAL] = array( '#type' => 'textfield', '#title' => t('!Points for referring a user (requires the referral module)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_REFERRAL, 0), '#size' => 5, '#maxlength' => 5, ); } if (module_exists('invite')) { $form[$group][USERPOINTS_INVITE_INVITE] = array( '#type' => 'textfield', '#title' => t('!Points for inviting a user (requires the invite module)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_INVITE_INVITE, 0), '#size' => 5, '#maxlength' => 5, ); } if (module_exists('invite')) { $form[$group][USERPOINTS_INVITE_REGISTER] = array( '#type' => 'textfield', '#title' => t('!Points when invited user registers (requires the invite module)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_INVITE_REGISTER, 0), '#size' => 5, '#maxlength' => 5, ); } if (module_exists('cart') && module_exists('payment')) { $group = 'ecommerce'; $form[$group] = array( '#type' => 'fieldset', '#collapsible' => true, '#collapsed' => true, '#title' => t('Ecommerce Options'), ); $form[$group][USERPOINTS_EC_EARN] = array( '#type' => 'textfield', '#title' => t('!Points awarded for buying (multiplied by price)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_EC_EARN, 0), '#size' => 5, '#maxlength' => 5, ); $form[$group][USERPOINTS_MIN_PURCHASE] = array( '#type' => 'textfield', '#title' => t('Smallest purchase for which !points are awarded', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_MIN_PURCHASE, '0.00'), '#size' => 5, '#maxlength' => 5, ); $form[$group][USERPOINTS_EC_PAYMENT] = array( '#type' => 'radios', '#title' => t('Accept user !points as payment?', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_EC_PAYMENT, 0), '#options' => array(t('Disable'), t('Enable')), ); $form[$group][USERPOINTS_EC_SPEND] = array( '#type' => 'textfield', '#title' => t('!Points used in payment (for every dollar)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_EC_SPEND, 1), '#size' => 5, '#maxlength' => 5, ); $form[$group][USERPOINTS_MIN_SPEND] = array( '#type' => 'textfield', '#title' => t('Number of !points that must be spent per purchase', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_MIN_SPEND, 0), '#size' => 5, '#maxlength' => 5, ); } $group = 'advanced'; $form[$group] = array( '#type' => 'fieldset', '#collapsible' => true, '#collapsed' => true, '#title' => t('Advanced Options'), ); $form[$group][USERPOINTS_TRANS_UCPOINTS] = array( '#type' => 'textfield', '#title' => t('Word to use in the interface for the upper case plural word !Points', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_TRANS_UCPOINTS, 'Points'), '#size' => 20, '#maxlength' => 20, ); $form[$group][USERPOINTS_TRANS_LCPOINTS] = array( '#type' => 'textfield', '#title' => t('Word to use in the interface for the lower case plural word !points', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'), '#size' => 20, '#maxlength' => 20, ); $form[$group][USERPOINTS_TRANS_LCPOINT] = array( '#type' => 'textfield', '#title' => t('Word to use in the interface for the lower case singular word !point', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_TRANS_LCPOINT, 'point'), '#size' => 20, '#maxlength' => 20, ); return system_settings_form($form); } function userpoints_user($op, &$edit, &$user, $category = '') { switch($op) { case 'delete': db_query('DELETE FROM {userpoints} WHERE uid = %d', $user->uid); break; case 'view': $points = (int) db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d', $user->uid)); $disp_points[] = array( 'title' => t('User !points', userpoints_translation()), 'value' => $points, ); return array(t('!Points', userpoints_translation()) => $disp_points); break; } } function userpoints_list_users() { $sql = "SELECT p.uid, u.name, p.points FROM {userpoints} p INNER JOIN {users} u USING (uid) GROUP BY p.uid,u.name,p.points"; $sql_cnt = "SELECT COUNT(DISTINCT(uid)) FROM {userpoints}"; $header = array( array('data' => t('User'), 'field' => 'u.name'), array('data' => t('!Points', userpoints_translation()), 'field' => 'p.points', 'sort' => 'desc'), ); $sql .= tablesort_sql($header); $result = pager_query($sql, 30, 0, $sql_cnt); while ($data = db_fetch_object($result)) { $rows[] = array( array('data' => theme('username', $data)), array('data' => $data->points, 'align' => 'right'), ); } $output = theme('table', $header, $rows); $output .= theme('pager', NULL, 30, 0); return $output; } function userpoints_block($op = 'list', $delta = 0, $edit = array()) { global $user; $num = 5; $block_title = array(); $block_title[] = t('%user\'s !points', array_merge(array('%user' => $user->name), userpoints_translation())); $block_title[] = t('Highest Users'); switch ($op) { case 'list': $blocks[0]['info'] = $block_title[0]; $blocks[1]['info'] = $block_title[1]; return $blocks; case 'view': if (user_access(USERPOINTS_PERM_VIEW)) { switch ($delta) { case 0: $title = $block_title[$delta]; if ($user->uid) { $points = (int) db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d', $user->uid)); $singular = t('!point', userpoints_translation()); $plural = t('!points', userpoints_translation()); $content = t('You have %p @c', array('%p' => $points, '@c' => format_plural($points, $singular, $plural))); } else { $content = t('!Points are visible to logged in users only',userpoints_translation()); } break; case 1: $title = $block_title[$delta]; $result = db_query_range('SELECT p.uid, u.name, p.points FROM {userpoints} p INNER JOIN {users} u USING (uid) GROUP BY p.uid ORDER BY p.points DESC', 0, $num); while ($data = db_fetch_object($result)) { $rows[] = array( array('data' => theme('username', $data)), array('data' => $data->points, 'align' => 'right')); } $header = array(t('User'), t('!Points', userpoints_translation())); $content = theme('table', $header, $rows); $content .= ''; break; } $block['subject'] = $title; $block['content'] = $content; return $block; } } } function userpoints_page() { $edit = $_POST['edit']; print theme('page', ''); } function userpoints_nodeapi(&$node, $op, $teaser, $page) { $points = variable_get(USERPOINTS_POST . $node->type, 0); switch($op) { case 'insert': $points = $points; _userpoints_update_points($points, $node->uid, "op=$op type=".$node->type); break; case 'delete': $points = -$points; _userpoints_update_points($points, $node->uid, "op=$op type=".$node->type); break; case 'update': $orig_node = node_load(array('nid' => $node->nid)); if ($node->uid != $orig_node->uid) { // subtract from the original node owner $points = -$points; _userpoints_update_points($points, $orig_node->uid, "op=$op type=".$node->type); // Add to the new node owner $points = -$points; _userpoints_update_points($points, $node->uid, "op=$op type=".$node->type); } break; } } function userpoints_comment($comment, $op) { global $user; $points = variable_get(USERPOINTS_POST_COMMENT, 0); switch($op) { case 'insert': _userpoints_update_points($points, $user->uid, 'comment '.$op); break; case 'delete': $points = -$points; _userpoints_update_points($points, $comment->uid, 'comment '.$op); break; case 'moderate': $points = variable_get(USERPOINTS_MODERATE_COMMENT, 0); _userpoints_update_points($points, $comment->uid, 'comment '.$op); break; } } function userpoints_nodevote($nid) { global $user; $points = variable_get(USERPOINTS_NODEVOTE, 0); _userpoints_update_points($points, $user->uid, 'nodevote'); } function userpoints_invite($op, $args) { switch($op) { case 'invite': $points = variable_get(USERPOINTS_INVITE_INVITE, 0); break; case 'escalate': $points = variable_get(USERPOINTS_INVITE_REGISTER, 0); break; } _userpoints_update_points($points, $args['inviter']->uid, $op); } /** * @param uid: user id of the user to get or lose the points * * @return number of current points in that user's account */ function userpoints_get_current_points($uid) { return (int)db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d', $uid)); } /** * @param points: number of points to add (if positive) or subtract (if negative) * @param uid: user id of the user to get or lose the points * @param info: optional description * * @return false when no action is take, true when points are credited or debited */ function userpoints_userpoints($points = 0, $uid = 0, $info = '') { return _userpoints_update_points($points, $uid, $info); } function _userpoints_update_points($new_points = 0, $uid = 0, $info = '') { // anonymous users do not get points, and there have to be points to process if ($uid == 0 || $new_points == 0) { return false; } $user = user_load(array('uid'=>$uid)); if ($new_points <= 0) { $msg = t('lost'); } else { $msg = t('earned'); } $points = (int)$new_points + (int)userpoints_get_current_points($uid); $singular = t('!point', userpoints_translation()); $plural = t('!points', userpoints_translation()); drupal_set_message(t('User %uname %op !pointsvalue @points! Total now is %total @tpoints.', array_merge(userpoints_translation(), array( '@points' => format_plural(abs($new_points), $singular, $plural), '%uname' => $user->name, '%op' => $msg, '!pointsvalue' => abs($new_points), '%total' => $points, '@tpoints' => format_plural($points, $singular, $plural)) ))); if (_userpoints_user_exists($uid)) { db_query("UPDATE {userpoints} SET points = '%d', last_update = '%d' WHERE uid = %d", $points, time(), $uid); } else { $result = db_query("INSERT INTO {userpoints} VALUES ('%d', '%d', '%d')", $uid, $points, time()); } return true; } function _userpoints_user_exists($uid) { return (int)db_result(db_query('SELECT COUNT(*) FROM {userpoints} WHERE uid = %d', $uid)); } function userpoints_ecommerceapi($data, $op) { switch($op) { case 'on payment completion': $multiplier = (int)variable_get(USERPOINTS_EC_EARN, 0); $min = variable_get(USERPOINTS_MIN_PURCHASE, '0.00'); $txnid = $data['txnid']; $total = 0; $uid = $data['uid']; $result = db_query('SELECT price, qty FROM {ec_transaction_product} WHERE txnid = %d', $txnid); while ($item = db_fetch_array($result)) { $total = $total + $item['price'] * $item['qty']; } if ($total < $min) { $points = 0; } else { $points = $multiplier * $total; } $payment_status = $data['payment_status']; if ($payment_status == payment_get_status_id('completed')) { // Payment completed if ($uid) { // User id from the transaction if ($points) { // if ($data['payment_method'] != 'userpoints') { // We cannot use points to purchase points. It does not make sense! _userpoints_update_points($points, $uid); } } } } break; } } function userpoints_paymentapi(&$txn, $op, $arg = '') { global $user; $uid = $user->uid; if (!variable_get(USERPOINTS_EC_PAYMENT, 0)) { return; } $multiplier = (int)variable_get(USERPOINTS_EC_SPEND, 1); $min = variable_get(USERPOINTS_MIN_SPEND, 0); switch ($op) { case 'display name': return t('User !Points', userpoints_translation()); case 'on checkout': $gross = store_transaction_calc_gross($txn); $points = ($gross * $multiplier); if ($points < $min) { form_set_error('gross', t('You must spend at least %min !points', array_merge(array('%min' => $min),userpoints_translation()))); } $balance = userpoints_get_current_points($uid); if ($balance < $points) { form_set_error('gross', t('You do not have enough !points for this purchase', userpoints_translation())); } drupal_set_message(t('This purchase will cost you !point_value !points', array_merge(array('!point_value' => $points), userpoints_translation()))); break; case 'payment page': $gross = $txn->gross; $points = ($gross * $multiplier); // Check the user's balance $balance = userpoints_get_current_points($uid); if ($balance >= $points) { $txnid = $txn->txnid; // Check if we have a transaction ID if ($txnid) { // User has sufficient balance _userpoints_update_points(-$points, $uid); // Check if we have shippable items $has_shippable_item = false; foreach($txn->items as $key => $value) { if (product_is_shippable($key['nid'])) { $has_shippable_item = true; } } // If there are no shippable items, then push workflow to completion if (!$has_shippable_item) { $txn->workflow = 6; } // Setup payment to completed $txn->payment_status = payment_get_status_id('completed'); // Update the transaction $result = store_transaction_save($txn); if ($result) { // Send an email notification store_send_invoice_email($txnid); } drupal_set_message(t('Your purchase using !pointsvalue !points has been completed', array_merge(array('!pointsvalue' => $points), userpoints_translation()))); } } break; } } function userpoints_update_1() { return _system_update_utf8(array('userpoints')); }