diff -urNp karma/karma.css karma-mine/karma.css --- karma/karma.css 2006-06-28 00:21:33.000000000 -0700 +++ karma-mine/karma.css 2007-01-15 17:49:08.000000000 -0800 @@ -58,11 +58,13 @@ ul.karma-stars a.rating-1 { left: 0px; ul.karma-stars a.rating-2 { left: 17px; } ul.karma-stars a.rating-3 { left: 34px; } ul.karma-stars a.rating-4 { left: 51px; } +ul.karma-stars a.rating-5 { left: 68px; } ul.karma-stars a.rating-1:hover { width: 17px; } ul.karma-stars a.rating-2:hover { width: 34px; } ul.karma-stars a.rating-3:hover { width: 51px; } ul.karma-stars a.rating-4:hover { width: 68px; } +ul.karma-stars a.rating-5:hover { width: 85px; } ul.karma-stars li.current-rating { background: url(stars.png) left top; diff -urNp karma/karma.module karma-mine/karma.module --- karma/karma.module 2006-12-07 18:09:02.000000000 -0800 +++ karma-mine/karma.module 2007-01-19 09:42:45.000000000 -0800 @@ -170,12 +170,13 @@ function karma_get_rating($type, $object * @param $uid * The user's uid. */ -function karma_update_user_karma($uid) { +function karma_update_user_karma($uid, $orig_karma) { if (!$uid) { // The anonymous user does not have karma. return; } + $min = (float) variable_get('karma_minimum_rating', '0.0'); $ratings = db_query_range("SELECT k.rating, k.count, GREATEST(c.timestamp, n.created) AS timestamp FROM {karma_objects} k LEFT JOIN {comments} c ON k.type = 'cid' AND k.id = c.cid AND c.uid = %d LEFT JOIN {node} n ON k.type = 'nid' AND k.id = n.nid AND n.uid = %d WHERE c.timestamp IS NOT NULL OR n.created IS NOT NULL ORDER BY timestamp DESC, k.rating", $uid, $uid, 0, 30); $comment_num = 1; $sum = 0; @@ -197,6 +198,14 @@ function karma_update_user_karma($uid) { // First delete any existing entry, then add the new entry db_query('DELETE FROM {karma_users} WHERE uid = %d', $uid); db_query('INSERT INTO {karma_users} (uid, karma) VALUES (%d, %f)', $uid, $karma); + + // Update all comment karma counts that involve this user if user crosses the minimum rating threshold in either direction + if ((is_null($orig_karma) && !is_null($karma)) || ($orig_karma > $min && $karma <= $min) || ($orig_karma <= $min && $karma > $min)) { + $nids = db_query('SELECT DISTINCT(nid) FROM comments WHERE uid = %d', $uid); + while ($nid = db_fetch_object($nids)) { + karma_update_comment_statistics($nid->nid); + } + } } function karma_update_comment_statistics($nid) { @@ -221,6 +230,9 @@ function karma_comment(&$comment, $op) { $comment = theme('karma_comment_rating', $comment, (float) $rating, $count, 'karma/comment/'. $comment->cid, $rating_form); } } + if ($op == 'insert') { + karma_update_comment_statistics($comment['nid']); + } } function karma_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { @@ -247,7 +259,9 @@ function karma_nodeapi(&$node, $op, $tea function karma_rate_validate($form_id, $form_values) { global $user; - if (!is_numeric($form_values['rating']) || $form_values['rating'] > 5 || $form_values['rating'] < -1) { + $max_rating = variable_get('karma_rating_max', 5); + + if (!is_numeric($form_values['rating']) || $form_values['rating'] > $max_rating || $form_values['rating'] < -1) { form_set_error('rating', t('Invalid rating.')); } if ($form_values['rating'] == 0 && !karma_user_is_trusted()) { @@ -261,13 +275,18 @@ function karma_rate_submit($form_id, $fo switch ($form_values['type']) { case 'comment': $id_type = 'cid'; + $object = db_fetch_object(db_query('SELECT uid, nid FROM {comments} WHERE cid = %d', $form_values['id'])); break; case 'node': $id_type = 'nid'; + $object = db_fetch_object(db_query('SELECT uid, nid FROM {node} WHERE nid = %d', $form_values['id'])); break; } + // Store the comment owner's original karma + $orig_karma = db_result(db_query('SELECT karma FROM {karma_users} WHERE uid = %d', $object->uid)); + // Remove any existing rating and insert the new rating db_query("DELETE FROM {karma_ratings} WHERE uid = %d AND id = %d AND type = '%s'", $user->uid, $form_values['id'], $id_type); if ($form_values['rating'] != -1) { @@ -280,16 +299,7 @@ function karma_rate_submit($form_id, $fo db_query("INSERT INTO {karma_objects} (id, type, rating, count) SELECT id, '%s', avg(rating), count(uid) FROM {karma_ratings} WHERE id = %d AND type = '%s' GROUP BY id", $id_type, $form_values['id'], $id_type); // Update the comment owner's karma - switch ($form_values['type']) { - case 'comment': - $object = db_fetch_object(db_query('SELECT uid, nid FROM {comments} WHERE cid = %d', $form_values['id'])); - break; - - case 'node': - $object = db_fetch_object(db_query('SELECT uid, nid FROM {node} WHERE nid = %d', $form_values['id'])); - break; - } - karma_update_user_karma($object->uid); + karma_update_user_karma($object->uid, $orig_karma); if ($id_type == 'cid') { karma_update_comment_statistics($object->nid); @@ -352,13 +362,15 @@ function _karma_rating_form($type, $obje break; } + $max_rating = variable_get('karma_rating_max', 5); + // Rating form. The user must be logged in, have permission to rate // comments, and not be the author of the comment. if (isset($user->uid) && $object->uid != $user->uid) { $form = array('#attributes' => array('class' => 'karma-rate-comment')); $rating_options = array(-1 => t('Not rated')); $rating_titles = variable_get('karma_rating_titles', array()); - foreach (range(karma_user_is_trusted() ? 0 : 1, 4) as $rating) { + foreach (range(karma_user_is_trusted() ? 0 : 1, $max_rating) as $rating) { if (isset($rating_titles[$rating]) && strlen($rating_titles[$rating]) > 0) { $rating_options[$rating] = $rating .': '. $rating_titles[$rating]; } @@ -414,7 +426,7 @@ function _karma_rating_form($type, $obje break; } $output .= '
  • '; - foreach (range(1, 4) as $rating) { + foreach (range(1, $max_rating) as $rating) { $output .= '
  • '; } $output .= ''; @@ -575,15 +587,27 @@ function karma_settings_page() { '#default_value' => variable_get('karma_minimum_rating', '0.0'), '#description' => t('Trusted users may choose to view comments with low ratings.'), ); + $form['karma_trusted_karma'] = array( '#type' => 'select', '#title' => t('Minimum rating for trusted users'), - '#options' => drupal_map_assoc(array('2.5', '3.0', '3.5')), + '#options' => drupal_map_assoc(array('1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5')), '#default_value' => variable_get('karma_trusted_karma', '3.5'), - '#description' => t('Trusted users may view low-rated comments.'), + '#description' => t('Trusted users may view low-rated comments. If you set this higher than the maximum rating, you are preventing users from achieving trusted status.'), + ); + + $form['karma_rating_max'] = array( + '#type' => 'select', + '#title' => t('Maximum rating of comments'), + '#options' => drupal_map_assoc(array('3', '4', '5')), + '#default_value' => variable_get('karma_rating_max', '4'), + '#description' => t('Sets the maximum rating a comment may receive.'), ); $karma_rating_titles = variable_get('karma_rating_titles', array()); + + $rating_max = variable_get('karma_rating_max', 5); + $form['karma_rating_titles'] = array( '#type' => 'fieldset', '#title' => t('Rating titles'), @@ -594,6 +618,7 @@ function karma_settings_page() { '#type' => 'textfield', '#title' => t('Title for %rating', array('%rating' => $rating)), '#default_value' => $karma_rating_titles[$rating], + '#description' => ($rating > $rating_max) ? t('Rating title above maximum rating.') : '', ); } @@ -602,6 +627,7 @@ function karma_settings_page() { '#title' => t('Selector type'), '#options' => array('select' => t('Drop down selection'), 'stars' => t('Stars')), '#default_value' => variable_get('karma_selector_type', 'select'), + '#description' => t('At the moment stars work only when maximum rating is set to 4.'), ); $form['karma_merge_widgets'] = array( @@ -646,6 +672,7 @@ The following code will work if you over } Have fun with the hackishness and use Drupal 5! +*/ function karma_link($type, $node = NULL, $main = NULL) { global $user; @@ -693,4 +720,4 @@ function karma_num_new($nid) { return 0; } } -*/ + diff -urNp karma/karma.theme karma-mine/karma.theme --- karma/karma.theme 2006-06-28 00:21:33.000000000 -0700 +++ karma-mine/karma.theme 2007-01-15 11:39:21.000000000 -0800 @@ -41,13 +41,14 @@ function theme_karma_comment_rating_star } function theme_karma_comment_rating_stars_content($rating, $users, $more_path) { + $max_rating = variable_get('karma_rating_max', 5); if ($users > 0) { $output .= t('Average (%votes):', array('%votes' => format_plural($users, '1 vote', '%count votes'))); } else { $output = t('no votes'); } - $output .= '
    '; + $output .= '
    '; $output .= l(t('see individual ratings'), $more_path); return $output; @@ -77,13 +78,14 @@ function theme_karma_node_rating_stars($ } function theme_karma_node_rating_stars_content($rating, $users, $more_path) { + $max_rating = variable_get('karma_rating_max', 5); if ($users > 0) { $output .= t('Average (%votes):', array('%votes' => format_plural($users, '1 vote', '%count votes'))); } else { $output = t('no votes'); } - $output .= '
    '; + $output .= '
    '; $output .= l(t('see individual ratings'), $more_path); return $output;