Index: userpoints_votingapi.module =================================================================== --- userpoints_votingapi.module (revision 4) +++ userpoints_votingapi.module (working copy) @@ -6,53 +6,67 @@ * * Extends User Points module with Voting API settings. */ -function userpoints_votingapi_form_alter(&$form, $form_state, $form_id) { - if ($form_id == 'userpoints_admin_settings') { - // Shift system_settings_form buttons. - $weight = $form['buttons']['#weight']; - $form['buttons']['#weight'] = $weight + 1; +function userpoints_votingapi_form_userpoints_admin_settings_alter(&$form, $form_state) { + // Shift system_settings_form buttons. + $weight = $form['buttons']['#weight']; + $form['buttons']['#weight'] = $weight + 1; - $group = "votingapi"; - $form[$group] = array( - '#type' => 'fieldset', - '#title' => t('Voting API settings'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#description' => t(''), - ); + $group = "votingapi"; + $form[$group] = array( + '#type' => 'fieldset', + '#title' => t('Voting API settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t(''), + ); - $form[$group][userpoints_votingapi_moderation] = array( - '#type' => 'checkbox', - '#title' => t('Moderate votes'), - '#default_value' => variable_get(userpoints_votingapi_moderation, 0), - '#description' => t('Moderate User Points earned from votes.'), - ); + $form[$group][userpoints_votingapi_moderation] = array( + '#type' => 'checkbox', + '#title' => t('Moderate votes'), + '#default_value' => variable_get('userpoints_votingapi_moderation', 0), + '#description' => t('Moderate User Points earned from votes.'), + ); - $form[$group][userpoints_votingapi_vote] = array( - '#type' => 'textfield', - '#title' => t('!Points for voting', userpoints_translation()), - '#default_value' => variable_get(userpoints_votingapi_vote, 1), - '#size' => 5, - '#maxlength' => 5, - ); + $form[$group][userpoints_votingapi_vote] = array( + '#type' => 'textfield', + '#title' => t('!Points for voting', userpoints_translation()), + '#default_value' => variable_get('userpoints_votingapi_vote', 1), + '#size' => 5, + '#maxlength' => 5, + ); - $form[$group][userpoints_votingapi_daily_threshold] = array( - '#type' => 'select', - '#title' => t('Daily threshold'), - '#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50)), - '#default_value' => variable_get(userpoints_votingapi_daily_threshold, 0), - '#description' => t('The maximum number of votes -- that will earn User Points -- in a 24-hour period.
Select 0 to ignore.'), - ); + $form[$group][userpoints_votingapi_daily_threshold] = array( + '#type' => 'select', + '#title' => t('Daily threshold'), + '#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50)), + '#default_value' => variable_get('userpoints_votingapi_daily_threshold', 0), + '#description' => t('The maximum number of votes -- that will earn User Points -- in a 24-hour period.
Select 0 to ignore.'), + ); - $form[$group]['userpoints_votingapi_tid'] = array( - '#type' => 'select', - '#title' => t('Category'), - '#default_value' => variable_get(userpoints_votingapi_tid, 0), - '#options' => userpoints_get_categories(), - '#description' => t('Votes will be assigned to this category. You can modify what categories are available by modifying the Userpoints taxonomy.', - array('!url' => url('admin/content/taxonomy/'. variable_get(USERPOINTS_CATEGORY_DEFAULT_VID, '')))), - ); - } + $form[$group][userpoints_votingapi_vote_karma_positive] = array( + '#type' => 'textfield', + '#title' => t('!Points for author receiving positive votes', userpoints_translation()), + '#default_value' => variable_get('userpoints_votingapi_vote_karma_positive', 1), + '#size' => 5, + '#maxlength' => 5, + ); + + $form[$group][userpoints_votingapi_vote_karma_negative] = array( + '#type' => 'textfield', + '#title' => t('!Points for author receiving negative votes', userpoints_translation()), + '#default_value' => variable_get('userpoints_votingapi_vote_karma_negative', -1), + '#size' => 5, + '#maxlength' => 5, + ); + + $form[$group]['userpoints_votingapi_tid'] = array( + '#type' => 'select', + '#title' => t('Category'), + '#default_value' => variable_get(userpoints_votingapi_tid, 0), + '#options' => userpoints_get_categories(), + '#description' => t('Votes will be assigned to this category. You can modify what categories are available by modifying the Userpoints taxonomy.', + array('!url' => url('admin/content/taxonomy/'. variable_get(USERPOINTS_CATEGORY_DEFAULT_VID, '')))), + ); } /** @@ -61,21 +75,54 @@ */ function userpoints_votingapi_votingapi_insert($vote) { $vote = $vote[0]; - $threshold = variable_get(userpoints_votingapi_daily_threshold, 0); + $threshold = variable_get('userpoints_votingapi_daily_threshold', 0); if (($threshold == '0') OR (userpoints_votingapi_within_threshold($vote['uid'], $threshold))) { - userpoints_userpointsapi(array( - 'uid' => $vote['uid'], - 'points' => variable_get(userpoints_votingapi_vote, 1), - 'moderate' => variable_get(userpoints_votingapi_moderation, 0), - 'event' => $vote['tag'], - 'entity_id' => $vote['content_id'], - 'entity_type' => $vote['content_type'], - 'operation' => t('vote'), - 'tid' => variable_get('userpoints_votingapi_tid', 0), - 'description' => t('Vote cast: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), - ) - ); - } + userpoints_userpointsapi(array( + 'uid' => $vote['uid'], + 'points' => variable_get('userpoints_votingapi_vote', 1), + 'moderate' => variable_get('userpoints_votingapi_moderation', 0), + 'event' => $vote['tag'], + 'entity_id' => $vote['content_id'], + 'entity_type' => $vote['content_type'], + 'operation' => t('vote'), + 'tid' => variable_get('userpoints_votingapi_tid', 0), + 'description' => t('Vote cast: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), + ) + ); + } + + if ( ($vote['value'] >= 1 && ($points = variable_get('userpoints_votingapi_vote_karma_positive', 1))) || + ($vote['value'] <= -1 && ($points = variable_get('userpoints_votingapi_vote_karma_negative', -1))) ) { + $uid = FALSE; + switch ($vote['content_type']) { + case 'node': + $uid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", $vote['content_id'])); + break; + + case 'comment': + $uid = db_result(db_query("SELECT uid FROM {comments} WHERE cid = %d", $vote['content_id'])); + break; + + case 'user': + $uid = $vote['content_id']; + break; + } + + if ($uid) { + userpoints_userpointsapi(array( + 'uid' => $uid, + 'points' => $points, + 'moderate' => variable_get('userpoints_votingapi_moderation', 0), + 'event' => $vote['tag'], + 'entity_id' => $vote['content_id'], + 'entity_type' => $vote['content_type'], + 'operation' => t('vote'), + 'tid' => variable_get('userpoints_votingapi_tid', 0), + 'description' => t('Vote cast: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), + ) + ); + } + } } /** @@ -83,25 +130,59 @@ * */ function userpoints_votingapi_votingapi_delete($vote) { - $vote = $vote[0]; - $threshold = variable_get(userpoints_votingapi_daily_threshold, 0); - if (($threshold == '0') OR (userpoints_votingapi_within_threshold($vote['uid'], $threshold))) { - userpoints_userpointsapi(array( - 'uid' => $vote['uid'], - 'points' => -1 * variable_get(userpoints_votingapi_vote, 1), - 'moderate' => variable_get(userpoints_votingapi_moderation, 0), - 'event' => $vote['tag'], - 'entity_id' => $vote['content_id'], - 'entity_type' => $vote['content_type'], - 'operation' => t('vote'), - 'tid' => variable_get('userpoints_votingapi_tid', 0), - 'description' => t('Vote cancelled: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), - ) - ); - } + $vote = $vote[0]; + + $threshold = variable_get('userpoints_votingapi_daily_threshold', 0); + if (($threshold == '0') || (userpoints_votingapi_within_threshold($vote['uid'], $threshold))) { + userpoints_userpointsapi(array( + 'uid' => $vote['uid'], + 'points' => -1 * variable_get('userpoints_votingapi_vote', 1), + 'moderate' => variable_get('userpoints_votingapi_moderation', 0), + 'event' => $vote['tag'], + 'entity_id' => $vote['content_id'], + 'entity_type' => $vote['content_type'], + 'operation' => t('vote'), + 'tid' => variable_get('userpoints_votingapi_tid', 0), + 'description' => t('Vote cancelled: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), + ) + ); + } + // If the deleted vote affected the author's userpoints score, nullify the affect + if ( ($vote['value'] >= 1 && ($points = variable_get('userpoints_votingapi_vote_karma_positive', 1))) || + ($vote['value'] <= -1 && ($points = variable_get('userpoints_votingapi_vote_karma_negative', -1))) ) { + $uid = FALSE; + switch ($vote['content_type']) { + case 'node': + $uid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", $vote['content_id'])); + break; + + case 'comment': + $uid = db_result(db_query("SELECT uid FROM {comments} WHERE cid = %d", $vote['content_id'])); + break; + + case 'user': + $uid = $vote['content_id']; + break; + } + + if ($uid) { + userpoints_userpointsapi(array( + 'uid' => $uid, + 'points' => -1 * $points, + 'moderate' => variable_get('userpoints_votingapi_moderation', 0), + 'event' => $vote['tag'], + 'entity_id' => $vote['content_id'], + 'entity_type' => $vote['content_type'], + 'operation' => t('vote'), + 'tid' => variable_get('userpoints_votingapi_tid', 0), + 'description' => t('Vote cancelled: !content_type !content_id.',array('!content_type'=> $vote['content_type'], '!content_id'=> $vote['content_id'])), + ) + ); + } + } } function userpoints_votingapi_within_threshold($uid, $threshold) { $number = db_result(db_query("SELECT COUNT(*) FROM {votingapi_vote} WHERE uid = %d AND timestamp > %d", $uid, time() - 86400)); return ($number <= $threshold ? TRUE : FALSE); -} \ No newline at end of file +}