check_plain(variable_get(USERPOINTS_TRANS_UCPOINTS, 'Points')), '!points' => check_plain(variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')), '!Point' => check_plain(variable_get(USERPOINTS_TRANS_UCPOINT, 'Point')), '!point' => check_plain(variable_get(USERPOINTS_TRANS_LCPOINT, 'point')), '!Uncategorized' => check_plain(variable_get(USERPOINTS_TRANS_UNCAT, 'General')), ); } return $trans; } /* * Returns an array of possible transaction statuses. */ function userpoints_txn_status() { static $stati; if (empty($stati)) { $stati = array( USERPOINTS_TXN_STATUS_APPROVED => t('Approved'), USERPOINTS_TXN_STATUS_PENDING => t('Pending'), USERPOINTS_TXN_STATUS_DECLINED => t('Declined'), ); } return $stati; } /** * Implements hook_help(). */ function userpoints_help($path, $arg) { switch ($path) { case 'admin/settings/userpoints': return t('Configure userpoints moderation and branding translation'); case 'admin/help#userpoints': return t('Users earn !points as they post nodes, comments, and vote on nodes', userpoints_translation()); } } /** * Checks access for administrative functionality. * * Provides simplified access checks for the administrative permissions: * - administer userpoints * - add userpoints * - edit userpoints * - moderate userpoints * * @param $type * The access type to check. The administer permission has access to all of * them. Supported strings: * - list: Access to the userpoints list, default local task. All * administrative permissions have access to this. * - add: Permission to add new userpoint transactions. * - edit: Permission to edit existing userpoint transactions. * - moderate: Permission to approve/decline pending transactions. * - administer: Unlimited userpoints permissions, used for settings page. * * @return * TRUE if the current user has access, FALSE if not. */ function userpoints_admin_access($type = 'list') { // Administer userpoints permission has full access. if (user_access('administer userpoints')) { return TRUE; } switch ($type) { // All admin permissions have access to the list page. case 'list': return user_access('add userpoints') || user_access('edit userpoints') || user_access('moderate userpoints'); break; case 'add': return user_access('add userpoints'); break; case 'edit': return user_access('edit userpoints'); break; case 'moderate': return user_access('moderate userpoints'); break; case 'administer': // administer permission was already checked, this exists for // documentation purposes only. break; } return FALSE; } /** * Implements hook_menu(). */ function userpoints_menu() { $items = array(); $items['admin/config/people/userpoints'] = array( 'title' => '!Points', 'title arguments' => userpoints_translation(), 'description' => strtr('Manage !points', userpoints_translation()), 'page callback' => 'userpoints_admin_points', 'access callback' => 'userpoints_admin_access', 'access arguments' => array('list'), 'file' => 'userpoints.admin.inc', ); $items['admin/config/people/userpoints/list'] = array( 'title' => 'List', 'description' => strtr('List users by !points', userpoints_translation()), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('list'), 'file' => 'userpoints.admin.inc', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -2, ); $items['admin/config/people/userpoints/list/totals'] = array( 'title' => 'Totals', 'description' => strtr('List users by !points', userpoints_translation()), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('list'), 'file' => 'userpoints.admin.inc', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => 0, ); $items['admin/config/people/userpoints/list/transactions'] = array( 'title' => 'Transactions', 'title arguments' => userpoints_translation(), 'description' => 'List transactions', 'page callback' => 'userpoints_admin_transactions', 'access callback' => 'userpoints_admin_access', 'access arguments' => array('edit'), 'file' => 'userpoints.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 2, ); $items['admin/config/people/userpoints/moderate'] = array( 'title' => 'Moderation', 'title arguments' => userpoints_translation(), 'description' => strtr('Review !points in moderation', userpoints_translation()), 'page callback' => 'userpoints_admin_manage', 'access callback' => 'userpoints_admin_access', 'access arguments' => array('moderate'), 'file' => 'userpoints.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => -1, ); $items['admin/config/people/userpoints/add'] = array( 'title' => 'Add', 'description' => 'Admin add/delete userpoints', 'page callback' => 'drupal_get_form', 'page arguments' => array('userpoints_admin_txn', 4, 5), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('add'), 'file' => 'userpoints.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 0, ); $items['admin/config/people/userpoints/edit'] = array( 'title' => 'Edit', 'page callback' => 'drupal_get_form', 'page arguments' => array('userpoints_admin_txn', 4, 5), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('edit'), 'file' => 'userpoints.admin.inc', 'type' => MENU_CALLBACK ); $items['admin/config/people/userpoints/approve'] = array( 'title' => 'Approve Userpoints', 'page callback' => 'userpoints_admin_approve', 'page arguments' => array(4, 5), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('moderate'), 'file' => 'userpoints.admin.inc', 'type' => MENU_CALLBACK ); $items['admin/config/people/userpoints/decline'] = array( 'title' => 'Approve !points', 'title arguments' => userpoints_translation(), 'page callback' => 'userpoints_admin_approve', 'page arguments' => array(4, 5), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('moderate'), 'file' => 'userpoints.admin.inc', 'type' => MENU_CALLBACK ); $items['admin/config/people/userpoints/settings'] = array( 'title' => '!Points settings', 'description' => strtr('Settings for !points', userpoints_translation()), 'title arguments' => userpoints_translation(), 'page callback' => 'drupal_get_form', 'page arguments' => array('userpoints_admin_settings'), 'access callback' => 'userpoints_admin_access', 'access arguments' => array('administer'), 'file' => 'userpoints.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 10, ); $items['userpoints'] = array( 'title' => 'Users by !points', 'title arguments' => userpoints_translation(), 'page callback' => 'userpoints_list_users', 'access arguments' => array('view userpoints'), 'file' => 'userpoints.pages.inc', 'type' => MENU_NORMAL_ITEM, ); $items['userpoints/operation-autocomplete'] = array( 'title' => 'Operation autocomplete', 'page callback' => 'userpoints_operation_autocomplete', 'access callback' => 'userpoints_admin_access', 'access arguments' => array('add'), 'file' => 'userpoints.admin.inc', 'type' => MENU_CALLBACK, ); $items['userpoints/view/%userpoints_transaction'] = array( 'title' => '!Point transaction', 'title callback' => 'userpoints_view_transaction_title', 'title arguments' => array(2), 'page callback' => 'userpoints_view_transaction', 'page arguments' => array(2), 'access callback' => 'userpoints_access_view_transaction', 'access arguments' => array(2), 'file' => 'userpoints.pages.inc', 'type' => MENU_NORMAL_ITEM, ); $items['myuserpoints/%user_uid_optional'] = array( 'title' => 'My !points', 'title arguments' => userpoints_translation(), 'page callback' => 'userpoints_list_my_userpoints', 'page arguments' => array(1), 'access callback' => 'userpoints_access_my_points', 'access arguments' => array(1), 'file' => 'userpoints.pages.inc', 'type' => MENU_NORMAL_ITEM, 'menu_name' => 'user-menu', ); return $items; } /** * Checks if user can access their points - used via hook_menu(). * * @return * TRUE if user has permissions to view userpoints and if the user is logged * in. */ function userpoints_access_my_points($account = NULL) { global $user; if ($account && $user->uid != $account->uid) { return userpoints_admin_access('edit'); } return (user_access('view userpoints') && user_is_logged_in()) || user_access('view own userpoints'); } /** * Checks if a user has access to a transaction. * * @return * TRUE if the user has permissions to view the transaction. */ function userpoints_access_view_transaction($transaction) { if (empty($transaction->user)) { $account = user_load($transaction->uid); } else { $account = $transaction->user; } return userpoints_access_my_points($account); } /** * Title callback function, display transaction ID. * @param $transaction * Transaction object. * @return * Page title. */ function userpoints_view_transaction_title($transaction) { return t('View !points transaction #@id', array('@id' => $transaction->txn_id) + userpoints_translation()); } /** * Implements hook_permission(). */ function userpoints_permission() { return array( 'view own userpoints' => array( 'title' => t('View own !points', userpoints_translation()), 'description' => t('Allows to view own !points, including own !point transactions.', userpoints_translation()), ), 'view userpoints' => array( 'title' => t('View all !points', userpoints_translation()), 'description' => t('Allows to view the !points of other users, but not the transactions.', userpoints_translation()), ), 'add userpoints' => array( 'title' => t('Add new !point transactions', userpoints_translation()), 'description' => t('Allows to create new !point transactions.', userpoints_translation()), ), 'edit userpoints' => array( 'title' => t('Edit !point transactions', userpoints_translation()), 'description' => t('Allows to modify existing !point transactions, including the ability to view transaction history for all users.', userpoints_translation()), ), 'moderate userpoints' => array( 'title' => t('Moderate !point transactions', userpoints_translation()), 'description' => t('Allows to approve or disapprove !point transactions.', userpoints_translation()), ), 'administer userpoints' => array( 'title' => t('Administer Userpoints'), 'description' => t('Allows to configure the settings and includes full read and write access of all !point transactions.', userpoints_translation()), ), ); } /** * Implements hook_theme(). */ function userpoints_theme() { return array( 'userpoints_list_users' => array( 'variables' => array( 'header' => NULL, 'rows' => NULL, 'tid' => NULL, 'pager_limit' => NULL, ), 'file' => 'userpoints.theme.inc', ), 'userpoints_list_users_header' => array( 'variables' => array(), 'file' => 'userpoints.theme.inc', ), 'userpoints_view_category' => array( 'render element' => 'element', 'file' => 'userpoints.theme.inc', ), 'userpoints_view_item' => array( 'render element' => 'element', 'file' => 'userpoints.theme.inc', ), 'userpoints_list_users_row' => array( 'variables' => array( 'row' => NULL, ), 'file' => 'userpoints.theme.inc', ), ); } /** * Implements hook_tokens(). */ function userpoints_tokens($type, $tokens, array $data = array(), array $options = array()) { if ($type == 'user' && isset($data['user']) && isset($tokens['userpoints'])) { return array($tokens['userpoints'] => userpoints_get_current_points($data['user']->uid)); } } /** * Implements hook_token_list(). */ function userpoints_token_list($type = 'all') { return array( 'tokens' => array( 'user' => array( 'name' => t('User points'), 'description' => t('The number of points a user has.'), ) ) ); } /** * Get current points of a user. * * @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 = NULL, $tid = NULL) { if (!$uid) { global $user; $uid = $user->uid; } // 0 is a valid value for the Uncategorized category. if (!isset($tid)) { $tid = userpoints_get_default_tid(); } elseif ($tid === 'all') { return (int) db_query('SELECT SUM(points) FROM {userpoints} WHERE uid = :uid', array(':uid' => $uid))->fetchField(); } return (int) db_query('SELECT points FROM {userpoints} WHERE uid = :uid AND tid = :tid', array(':uid' => $uid, ':tid' => $tid))->fetchField(); } /** * Gets the number of maximal points of that user. * * @param $uid * User id of the user to get or lose the points. * * @return * Number of max points in that user's account. */ function userpoints_get_max_points($uid = NULL, $tid = NULL) { $max = drupal_static(__FUNCTION__, array()); // Check if uid is passed as a parameter. if (!$uid) { // It is not, so we use the currently logged in user's uid. global $user; $uid = $user->uid; } // Check if a term id is passed as a parameter. if (!isset($tid)) { // It is not, so get the default term id. $tid = userpoints_get_default_tid(); } // Check if we have already cached the maximum for the user/term combination on previous calls. if (!isset($max[$uid][$tid])) { // We did not cache it. if ($tid === 'all') { // There is no term id, so we use "all". $max[$uid][$tid] = db_query('SELECT SUM(max_points) FROM {userpoints} WHERE uid = :uid', array(':uid' => $uid))->fetchField(); } else { // A term ID is specified, so fetch its maximum points. $max[$uid][$tid] = db_query('SELECT max_points FROM {userpoints} WHERE uid = :uid AND tid = :tid', array(':uid' => $uid, ':tid' => $tid))->fetchField(); } } // Return the cached value. return $max[$uid][$tid]; } /** * Save userpoint changes and call hooks. * * @param $params * if (int) assumed to be points for current user * Accepts an array of keyed variables and parameters * 'points' => # of points (int) (required) * 'moderate' => TRUE/FALSE * 'uid' => $user->uid * 'time_stamp' => unix time of the points assignement date * 'operation' => 'published' 'moderated' etc. * 'tid' => 'category ID' * 'expirydate' => timestamp or 0, 0 = non-expiring; NULL = site default * 'description' => 'description' * 'reference' => reserved for module specific use * 'display' => whether or not to display "points awarded" message * 'txn_id' => Transaction ID of points, If present an UPDATE is performed * 'entity_id' => ID of an entity in the Database. ex. $node->id or $user->uid * 'entity_type' => string of the entity type. ex. 'node' or 'user' NOT 'node-content-custom' * * @return * Array with status and reason. * 'status' => FALSE when no action is take, TRUE when points are credited or debited * 'reason' => (string) error message to indicate reason for failure */ function userpoints_userpointsapi($params) { global $user; // Test for the existence of parameters and set defaults if necessary. if (!isset($params['txn_id'])) { // If a txn_id is passed in we'll do an UPDATE thus the std checks don't apply. if (is_int($params)) { $params = array('points' => $params); } if (!is_array($params)) { // Has to be an array to continue. return array( 'status' => FALSE, 'reason' => 'Parameters did not properly form as an array, this is an internal module error. ', ); } if (!isset($params['uid'])) { $params['uid'] = $user->uid; } // Check if parameters are set. $params_null_check = array('operation', 'description', 'reference', 'display', 'entity_id', 'entity_type'); foreach ($params_null_check as $param_null_check) { if (!isset($params[$param_null_check])) { $params[$param_null_check] = NULL; } } if (!isset($params['moderate'])) { // If not passed then site default is used. $params['status'] = variable_get(USERPOINTS_POINTS_MODERATION, USERPOINTS_TXN_STATUS_APPROVED); } else { $params['status'] = $params['moderate'] ? USERPOINTS_TXN_STATUS_PENDING : USERPOINTS_TXN_STATUS_APPROVED; } if (!isset($params['tid']) || !is_numeric($params['tid'])) { // If not passed then site default is used. $params['tid'] = userpoints_get_default_tid(); } // Anonymous users do not get points, and there have to be points to process. if (($params['uid'] == 0 || $params['points'] == 0)) { return array( 'status' => FALSE, 'reason' => 'uid or points = 0. Anonymous users do not get points and there must be points to process.', ); } } else { // We have a txn_id so we can look up some user information. $params['uid'] = db_query('SELECT uid from {userpoints_txn} WHERE txn_id = :txn_id', array(':txn_id' => $params['txn_id']))->fetchField(); } // If txn_id. // Load the user object that will be awarded the points. $account = user_load($params['uid']); if (!$account) { return array( 'status' => FALSE, 'reason' => 'invalid uid or user account could not be loaded', ); } // Call the _userpoints hook, and stop if one of them returns FALSE. $rc = userpoints_invoke_all('points before', $params); foreach ($rc as $key => $value) { if ($value == FALSE) { // Do not process the points. return array( 'status' => FALSE, 'reason' => t('@key returned FALSE from the hook_userpoints points before call', array('@key' => $key)), ); } } $ret = _userpoints_transaction($params); if ($ret == FALSE) { return array( 'status' => FALSE, 'reason' => 'transaction failed in _userpoints_transaction, this is an internal module error', ); } // Allow modules to define custom messages. if (!empty($params['message'])) { $message = $params['message']; } // Display message if either display property is not set and messages should // be displayed by default or display property is not FALSE. elseif (!empty($params['display']) || (!isset($params['display']) && variable_get(USERPOINTS_DISPLAY_MESSAGE, 1))) { // Prepare arguments. They are the same for all string combinations. $categories = userpoints_get_categories(); $arguments = array_merge(userpoints_translation(), array( '!username' => theme('username', array('account' => $account)), '%total' => userpoints_get_current_points($params['uid'], $params['tid']), '%category' => isset($categories[$params['tid']]) ? $categories[$params['tid']] : $categories[0], )); $view_own_points = user_access('view own userpoints') || user_access('view userpoints') || user_access('administer userpoints'); $view_all_points = user_access('view userpoints') || user_access('administer userpoints'); if ($params['status'] == USERPOINTS_TXN_STATUS_DECLINED) { // Points have been declined. if ($account->uid == $user->uid && $view_own_points) { $message = format_plural($params['points'], 'You did not receive approval for @count !point in the %category category.', 'You did not receive approval for @count !points in the %category category.', $arguments); } elseif ($view_all_points) { $message = format_plural($params['points'], '!username did not receive approval for @count !point in the %category category.', '!username did not receive approval for @count !points in the %category category.', $arguments); } } elseif (isset($params['points']) && $params['points'] < 0) { if ($params['status'] == USERPOINTS_TXN_STATUS_PENDING) { if ($account->uid == $user->uid && $view_own_points) { // Directly address the user if he is loosing points. $message = format_plural(abs($params['points']), 'You just had a !point deducted, pending administrator approval.', 'You just had @count !points deducted, pending administrator approval.', $arguments); } elseif ($view_all_points) { // Only display message about other users if user has permission to view userpoints. $message = format_plural(abs($params['points']), '!username just had a !point deducted, pending administrator approval.', '!username just had @count !points deducted, pending administrator approval.', $arguments); } } else { if ($account->uid == $user->uid && $view_own_points) { $message = format_plural(abs($params['points']), 'You just had a !point deducted and now have %total !points in the %category category.', 'You just had @count !points deducted and now have %total !points in the %category category.', $arguments); } elseif ($view_all_points) { $message = format_plural(abs($params['points']), '!username just had a !point deducted and now has %total !points in the %category category.', '!username just had @count !points deducted and now has %total !points in the %category category.', $arguments); } } } elseif (!empty($params['points'])) { if ($params['status'] == USERPOINTS_TXN_STATUS_PENDING) { if ($account->uid == $user->uid && $view_own_points) { // Directly address the user if he is loosing points. $message = format_plural(abs($params['points']), 'You just earned a !point, pending administrator approval.', 'You just earned @count !points, pending administrator approval.', $arguments); } elseif ($view_all_points) { // Only display message about other users if user has permission to view userpoints. $message = format_plural(abs($params['points']), '!username just earned a !point, pending administrator approval.', '!username just earned @count !points, pending administrator approval.', $arguments); } } else { if ($account->uid == $user->uid && $view_own_points) { $message = format_plural(abs($params['points']), 'You just earned a !point and now have %total !points in the %category category.', 'You just earned @count !points and now have %total !points in the %category category.', $arguments); } elseif ($view_all_points) { $message = format_plural(abs($params['points']), '!username just earned a !point and now has %total !points in the %category category.', '!username just earned @count !points and now has %total !points in the %category category.', $arguments); } } } if (isset($message)) { drupal_set_message($message); } } // Call the _userpoints hook to allow modules to act after points are awarded. userpoints_invoke_all('points after', $params); return array( 'status' => TRUE, 'transaction' => $params, ); } /** * Adds the points to the txn table. */ function _userpoints_transaction(&$params) { // Check, again, for a properly formed array. if (!is_array($params)) { return FALSE; } 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'])) { return FALSE; } if (!isset($params['uid'])) { global $user; $params['uid'] = $user->uid; // There must be a UID, anonymous does not receive points. if (!$params['uid'] > 0) { return FALSE; } } if (isset($params['expirydate']) && !is_numeric($params['expirydate'])) { return FALSE; } // Check if parameters are set. $params_null_check = array('operation', 'description', 'reference', 'expired', 'parent_txn_id', 'entity_id', 'entity_type'); foreach ($params_null_check as $param_null_check) { if (!isset($params[$param_null_check])) { $params[$param_null_check] = NULL; } } if (!isset($params['tid']) || !is_numeric($params['tid'])) { $params['tid'] = userpoints_get_default_tid(); } elseif ($params['tid'] == 0) { // Tid with 0 are uncategorized and are set to NULL // this is a backwards compatibilty issue. $params['tid'] = NULL; } if (!isset($params['expirydate'])) { $params['expirydate'] = userpoints_get_default_expiry_date(); } // Use current time for time_stamp if configured to always use the default, // not set, not a positive integer or in the future. if (variable_get(USERPOINTS_TRANSACTION_TIMESTAMP, 1) || !isset($params['time_stamp']) || $params['time_stamp'] <= 0 || $params['time_stamp'] > $time) { $params['time_stamp'] = REQUEST_TIME; } } // Always force changed timestamp to current REQUEST_TIME for transaction tracking. $params['changed'] = REQUEST_TIME; if (!empty($params['txn_id']) && $params['txn_id'] > 0) { // A transaction ID was passed in so we'll update the transaction. $txn = (array) userpoints_transaction_load($params['txn_id']); if (!$txn) { return FALSE; } // Don't superseed existing keys, just complete missing keys. $params += $txn; // Update existing transaction record for key txn_id. $ret = drupal_write_record('userpoints_txn', $params, array('txn_id')); // Only update if the record has been successfully updated. if ($ret != FALSE) { _userpoints_update_cache($params, $txn); } } else { // Create new transaction record. $ret = drupal_write_record('userpoints_txn', $params); if ($ret != FALSE) { _userpoints_update_cache($params); } } return TRUE; } /** * Update the caching table. * * @param $params * Array with the transaction params. * @param $txn * The original transaction, if this is an update. */ function _userpoints_update_cache($txn, $old_txn = NULL) { // Store eventual updates in this array. $updates = array(); if (!$old_txn) { // For new transactions, only update the cache for fully approved non-expired // points. if ($txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['expired'] != 1) { // Calculate the current points based upon the tid. $updates['points'] = $txn['points'] + userpoints_get_current_points($txn['uid'], $txn['tid']); $max_points = userpoints_get_max_points($txn['uid'], $txn['tid']); // If the new points are higher then the max, update the maximum. if ($updates['points'] > $max_points) { $updates['max_points'] = $updates['points']; } } } else { // For existing transactions, it is a bit more complex. // Expired transactions that were expired before can be ignored. if ($txn['expired'] == 1 && $old_txn['expired'] == 1) { return; } if ($old_txn['tid'] != $txn['tid']) { // If the category has changed, remove the points of the old transaction // from the old category. $remove_points = userpoints_get_current_points($txn['uid'], $old_txn['tid']) - $old_txn['points']; db_merge('userpoints') ->key(array( 'uid' => $txn['uid'], 'tid' => (int) $old_txn['tid'], )) ->fields(array( 'points' => $remove_points, )) ->execute(); if ($txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) { // Make sure to add the points so that they are added to the new category. $updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + $txn['points']; } } else if ($old_txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] != USERPOINTS_TXN_STATUS_APPROVED) { // If the transaction goes from approved to not approved, subtract the // points to the total. $updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) - $old_txn['points']; $max_points = userpoints_get_max_points($txn['uid'], $txn['tid']); // If the new points are higher then the max, update the maximum. if ($updates['points'] > $max_points) { $updates['max_points'] = $updates['points']; } } else if ($txn['points'] != $old_txn['points'] && $old_txn['status'] == USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) { // If the category did not change but the points and the transaction // was and still is approved, update the points difference. $updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + ($txn['points'] - $old_txn['points']); } elseif ($old_txn['status'] != USERPOINTS_TXN_STATUS_APPROVED && $txn['status'] == USERPOINTS_TXN_STATUS_APPROVED) { // Calculate the current points based upon the tid. $updates['points'] = userpoints_get_current_points($txn['uid'], $txn['tid']) + $txn['points']; } } if (empty($updates)) { return; } $max_points = userpoints_get_max_points($txn['uid'], $txn['tid']); // If the new points are higher then the maximum, update it. if ($updates['points'] > $max_points) { $updates['max_points'] = $updates['points']; } $updates['last_update'] = REQUEST_TIME; // Insert or update the userpoints caching table with the user's current // points. db_merge('userpoints') ->key(array( 'uid' => $txn['uid'], 'tid' => (int) $txn['tid'], )) ->fields($updates) ->execute(); } /** * Determines the correct default expiration date. * * @return * The default expiration date. */ function userpoints_get_default_expiry_date() { $expirydate = userpoints_date_to_timestamp(variable_get(USERPOINTS_EXPIREON_DATE, 0)); if ($expirydate < REQUEST_TIME) { $expirydate = variable_get(USERPOINTS_EXPIREAFTER_DATE, 0); if ($expirydate) { $expirydate = REQUEST_TIME + $expirydate; } } return (int) $expirydate; } /* * Checks to ensure that a user exists corresponding to a category. * * @param $uid * User ID to check for existence of points for the user. * @param $tid * taxonomy id of the category to limit to, if omitted * if the use has points in any category the return is TRUE. * @return * TRUE if user found, FALSE otherwise. */ function _userpoints_user_exists($uid, $tid = NULL) { if (is_numeric($tid)) { return (int) db_query('SELECT COUNT(uid) FROM {userpoints} WHERE uid = :uid AND tid = :tid', array(':uid' => $uid, ':tid' => $tid))->fetchField(); } else { return (int) db_query('SELECT COUNT(uid) FROM {userpoints} WHERE uid = :uid', array(':uid' => $uid))->fetchField(); } } /** * Implements hook_user_delete(). */ function userpoints_user_delete($account) { // The user is being deleted, delete all traces in userpoints and txn tables. db_delete('userpoints') ->condition('uid', $account->uid) ->execute(); db_delete('userpoints_txn') ->condition('uid', $account->uid) ->execute(); } /** * Implements hook_user_view(). */ function userpoints_user_view($account, $view_mode) { global $user; if (user_access('view userpoints') || (user_access('view own userpoints') && $user->uid == $account->uid)) { $points_list = userpoints_get_points_list($account); if (!empty($details) || $points_list) { $account->content['userpoints'] = array( 'title' => array( '#markup' => '