Has anybody tried to get the karma module working in 4.7? (It's only available through cvs.) Comment moderation is missing from 4.7 and the only thing that I could find that was similar to it was the karma module.

Besides a problem with the karma.css file the only other problem I am running into is that trusted users can't set the comment viewing option to hide low-rated comments. It sets itself back to showing low-rated comments no matter what is submitted. Untrusted users have low-rated comments hidden, though.

Comments

ericwagner’s picture

So, this is such a hack I am not even going to submit it as an official patch. Also, I haven't done this before here so I may be making one more mistakes. In any case, the following patches the drupal 4.7 comments.module to allow the "Show low-rated comments" / "Hide low-rated comments" comment control to work.

This has only been lightly tested so use at your own risk:

--- comment.module 2006-05-10 18:32:30.000000000 -0700
+++ comment.module.new      2006-05-12 18:51:04.000000000 -0700
@@ -829,7 +829,7 @@ function comment_render($node, $cid = 0)
       }

       // Start a form, for use with comment control.
-       $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
+      $result = pager_query(db_rewrite_sql($query, 'c', 'cid'), $comments_per_page, 0, db_rewrite_sql(
"SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d AND c.status = %d", 'c'), $nid, COMMENT_PUBLISHED);
       if (db_num_rows($result) && (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMEN
T_CONTROLS_ABOVE || variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE
_BELOW)) {
         $output .= comment_controls($mode, $order, $comments_per_page);
       }
@@ -1519,14 +1519,16 @@ function comment_controls_submit($form_i
   $mode = $form_values['mode'];
   $order = $form_values['order'];
   $comments_per_page = $form_values['comments_per_page'];
+  $karma_show = $form_values['karma_show'];

   if ($user->uid) {
-    $user = user_save($user, array('mode' => $mode, 'sort' => $order, 'comments_per_page' => $comments
_per_page));
+    $user = user_save($user, array('mode' => $mode, 'sort' => $order, 'comments_per_page' => $comments
_per_page, 'karma_show' => $karma_show));
   }
   else {
     $_SESSION['comment_mode'] = $mode;
     $_SESSION['comment_sort'] = $order;
     $_SESSION['comment_comments_per_page'] = $comments_per_page;
+    $_SESSION['karma_show'] = $karma_show;
   }
 }
ericwagner’s picture

Also, I had to change this (in the karma.css file):

html.js form.karma-rate-comment input.form-submit {
  display: none;
}

To this:

html.js form.karma-rate-comment input.form-submit {
  display: inline;
}

Note sure why, however, the rate button for comment ratings wouldn't display without the change (this was true in any theme I chose.)

skor’s picture

I should have some time tomorrow to give it a shot on my dev server. I'll let you know.

skor’s picture

I've got the CVS version installed, updated my database, enabled the module, and granted access to it. There was no submit capability to go with the comment ratings, so I applied your CSS change, and that got fixed.

So now I can submit comment rating, and the stats seem to get calculated OK. Although the submission doesn't seem to update on the first try. I think it's actually getting applied to the database, but just not immediately reflected on the page with the comment. If I follow the link to the individual ratings, I see the update. However, sometimes I have to re-submit the rating, or re-fresh the page to see it there.

Under comment viewing options, I don't seem to be able to change the threaded/flat setting , the oldest/newest setting, or the Hide Low Rated setting.

Also, sometimes I only get 3 choices for ratings, sometimes 4, and sometimes 5. I can't see any pattern to it.

I'll give the rest of your patch a try later tonight, or tomorrow, and follow up.

miriaschka’s picture

Originally, we would not need such a patch, because Mr Drumm had built in some AJAX functionality, which submitted the rating upon selection in the dropdown. Unfortunately, it does not work anymore. Have a look at the JavaScript console...

ericwagner’s picture

Note: This has been updated.

The following patches to karma.js and karma.module make the rating changes display immediately after they are submitted. The patches also enable AJAX (which makes the change in karma.css mentioned above unecessary.)

karma.js patch (this isn't necessary with the latest karma.js file)

--- karma.js.orig       2006-05-19 12:32:26.000000000 -0700
+++ karma.js    2006-05-19 12:27:58.000000000 -0700
@@ -5,10 +5,10 @@
 function karmaAutoAttach() {
   var selects = document.getElementsByTagName('select');
   for (i = 0; select = selects[i]; i++) {
-    if (select && hasClass(select, 'karma-rating-select')) {
+    if (select && hasClass(select, 'form-select karma-rating-select')) {
       select.form.onsubmit = function () {
         select = $(this.id.replace('karma_rate_comment_', 'edit-rating-'));
-        uri = $(this.id.replace('karma_rate_comment_', 'uri-rating-')).value;
+        uri = $(this.id.replace('karma_rate_comment_', 'rating-uri-')).value;
         ratingText = $(this.id.replace('karma_rate_comment_', 'karma-rating-text-'));
         throbber = document.createElement('span');
         throbber.className = 'throbber';

karma.module patch
Note: This patch includes the fix for the missing rating option 5 mentioned below.

--- karma.module.orig	2006-07-13 15:46:44.000000000 -0700
+++ karma.module	2006-07-27 18:20:11.000000000 -0700
@@ -108,7 +108,7 @@ function karma_get_user_rating($type, $o
         break;
     }
     while ($rating = db_fetch_object($result)) {
-      $ratings[$type][$object->nid][$id] = $rating->rating;
+      $ratings[$type][$object->nid][$rating->id] = $rating->rating;
     }
   }
 
@@ -226,20 +226,24 @@ function karma_nodeapi(&$node, $op, $tea
 
   switch ($op) {
     case 'view':
-      $rating_form = _karma_rating_form('node', $node);
-      if (!is_null($rating_form) && !$css_added) {
-        theme('add_style', drupal_get_path('module', 'karma') .'/karma.css');
-        drupal_add_js(drupal_get_path('module', 'karma') .'/karma.js');
-        $css_added = TRUE;
-      }
+      $karma_enabled = variable_get('karma_enabled_'. $node->type, 0);
 
-      // Display current rating.
-      list($rating, $count) = karma_get_rating('node', $node);
-      if (variable_get('karma_selector_type', 'select') == 'stars') {
-        $node = theme('karma_node_rating_stars', $node, (float) $rating, $count, 'karma/node/'. $node->nid, $rating_form);
-      }
-      else {
-        $node = theme('karma_node_rating', $node, (float) $rating, $count, 'karma/node/'. $node->nid, $rating_form);
+      if ($karma_enabled) {
+        $rating_form = _karma_rating_form('node', $node);
+        if (!is_null($rating_form) && !$css_added) {
+          theme('add_style', drupal_get_path('module', 'karma') .'/karma.css');
+          drupal_add_js(drupal_get_path('module', 'karma') .'/karma.js');
+          $css_added = TRUE;
+        }
+
+        // Display current rating.
+        list($rating, $count) = karma_get_rating('node', $node);
+        if (variable_get('karma_selector_type', 'select') == 'stars') {
+           $node = theme('karma_node_rating_stars', $node, (float) $rating, $count, 'karma/node/'. $node->nid, $rating_form);
+        }
+        else {
+          $node = theme('karma_node_rating', $node, (float) $rating, $count, 'karma/node/'. $node->nid, $rating_form);
+        }
       }
       break;
   }
@@ -349,7 +353,7 @@ function _karma_rating_form($type, $obje
     $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, 5) as $rating) {
       if (isset($rating_titles[$rating]) && strlen($rating_titles[$rating]) > 0) {
         $rating_options[$rating] = $rating .': '. $rating_titles[$rating];
       }
@@ -417,6 +421,7 @@ function _karma_rating_form($type, $obje
       '#type' => 'hidden',
       '#value' => url('karma/rate_js/'. $type .'/'. $id, NULL, NULL, TRUE),
       '#id' => 'uri-rating-'. $type .'-'. $id,
+      '#attributes' => array('class' => 'rating_uri'. $id),
     );
     $form['op'] = array(
       '#type' => 'submit',
@@ -469,6 +474,7 @@ function karma_form_alter($form_id, &$fo
       '#type' => 'select',
       '#default_value' => (isset($user->karma_show) ? $user->karma_show : TRUE),
       '#options' => array(TRUE => t('Show low-rated comments'), FALSE => t('Hide low-rated comments')),
+      '#attributes' => array('class' => 'karma-show'),
       '#weight' => 5,
     );
     $form['#execute'][] = 'karma_comment_controls_submit';
@@ -479,8 +485,8 @@ function karma_form_alter($form_id, &$fo
     if ($form['type']['#value'] .'_node_settings' == $form_id) {
       $form['workflow']['karma_enabled_'. $form['type']['#value']] = array(
         '#type' => 'checkbox',
-        '#title' => t('Enable karma ratings'),
-        '#default_value' => t('karma_enabled_'. $form['type']['#value']),
+	'#title' => t('Enable karma ratings'),
+	'#default_value' => variable_get('karma_enabled_'. $form['type']['#value'], array('status', 'promote')),
       );
     }
   }

Unfortunately, the patch a few comments above to comment.module is still needed as there is apparently no AJAX functionality included for the "Show|Hide low-rated comments" Comment viewing options setting. This wouldn't be too difficult to include (maybe I will work on it if I have time.)

ericwagner’s picture

I think that the number of rating choices you get has to do with your current karma rating (I could be wrong, though.) Create several users with different karma levels (by rating each other) and see what happens.

miriaschka’s picture

Only trusted users may give the 0-rating. I believe there is a slight bug in the cvs version of karma.module; just set the upper bound of the array to 5 instead of 4 to make the 5-rating accessible.

skor’s picture

I'm having trouble getting the GNUWin32 patch tool to take those patches. Any chance you could send me complete files?

Thanks
Sean

fyi - I'm also going to look at the Vote up/down Package. You might want to do the same. I like the binary like it/don't like it rating scheme. 5 choices takes too much thought. Also like the potential for integration with the actions module, so I can trigger things off the number of votes.

skor’s picture

Just downloaded the Vote up/down Package and at least from the readme, it doesn't support rating comments.

EDIT: nevermind. I tried out the demo, and it does support comment ratings :D

skor’s picture

I uploaded the files, and wanted to disable the vote-up/down module, so they wouldn't interfere. But when I went to Admin>>Modules, I got:

Fatal error: Cannot redeclare comment_help() (previously declared in /home/userid/public_html/drupal/modules/comment.module:70) in /home/userid/public_html/drupal/modules/karma/comment.module on line 70

I'll mess with it some over the next few days and post some more.

Edit: Duh- must be tired. got rid of the drupal/modules/comment.module file and it's all good. also changed karma.css back, to get rid of the 'Rate' button, since it applies the rating right away now.

drumm’s picture

The karma module isn't finished enough for release as it requires a core patch to be able to add a comment viewing option which adds a rating threshold. It is use at your own risk.

If someone wants to officially maintain it, please contact me. FOr now it is considered unmaintained.

jherteng’s picture

What is the core patch that needs to be applied? How much work remains for the comment filtering to be functional?

Ignas’s picture

Try MediumVote?
http://drupal.org/node/49767
it have nice ajax coment voting capability.

ericwagner’s picture

Unfortunately, MediumVote doesn't quite fit my needs. I need something that tracks/creates "karma" or "mojo" scores, can automatically hide low rated comments, as well as include trusted/untrusted users.

I do have the karma module working after jumping through the various hoops above. I might possibly be able to commit to maintaining karma, however, I can't make the commitment for several months.

Ideally, I think that the karma, voting, and user points modules should either all work together or be one module.

Ignas’s picture

I'm looking for the same solution. Mediumvote is not complicated to modify for this task.
But you are right - this is not a right way :) I'll try to use karma module. Thanks for the patch!

Ignas’s picture

Tried 3 modules for this karma task:
Karma - bugy, no Ajax
MediumVote - bugy, Ajax
Vote up/down Package - works fine, nice code, Ajaxed.
So, pached this package. Result is there: http://www.photoart.lt/images/drupal.png - voting example and User statistics (status, voting). Users role (User/Editor/Admin) and status (Spamer/User/Expert/Guru) displayed.
Rather easy work for 1-2 days. Even for me (bad programmer). But I can't give my code, sorry - code is terrible and close related to my theme.
The workflow is: Install Voting API module, take vote_up_down.module and related files (png, js, css), patch this module. I recomend to crate separate table to store karma results. Something like that:

CREATE TABLE karmavote (
  uid int(10) NOT NULL default '0',
  voted int(8) default '0',
  votedcount int(8) default '0',
  votes int(8) default '0',
  votescount int(8) default '0',
  role tinyint(1) default '2',
  status tinyint(1) default '0',
  PRIMARY KEY (uid, uid)
) TYPE=MyISAM;

From data stored in Voting API tables calculate karma resuls (by using cron or during karma result view) and store in that table. Pass results to comment by using this function in template.php file:

function  _phptemplate_variables($hook, $vars) {
global $karmavote_role,$karmavote_status;
  switch($hook) {
    case 'comment' :
		$uid = $vars['comment']->uid;
		$karmavote = db_fetch_object(db_query('SELECT role, status FROM {karmavote} WHERE uid = %d', $uid));
		$vars['picture'] = "<div class='karmavote'>".$karmavote_role[$karmavote->role].":".$karmavote_status[$karmavote->status]."</div>".$vars['picture'];
        break;
  }
  return $vars;
}

Hiding low rated comments feature is not needed for my, but it is very easy to implement in this state.
Sorry for this heresys, but I hope this information could be usefull ;)

skor’s picture

From the comment above, in order to get the #5 rating, I did have to change one line in karma.module, function _karma_rating_form:
Was:

   foreach (range(karma_user_is_trusted() ? 0 : 1, 4) as $rating) {

Is:

   foreach (range(karma_user_is_trusted() ? 0 : 1, 5) as $rating) {

All seems to be working well as far as it goes. User karma ratings are updated properly. Seems like there's room for quite a bit of customization to grant priveledges based on karma level though.

Great job.