diff -rupN comment_revisions/comment_revisions.module comment_revisions_new/comment_revisions.module
--- comment_revisions/comment_revisions.module	2008-06-29 19:15:07.000000000 +1000
+++ comment_revisions.module	2010-05-26 13:44:09.000000000 +1000
@@ -3,7 +3,6 @@
 /**
  *  @file
  *  Complete module for comment_revisions
- *
  */
 
 /**
@@ -13,130 +12,136 @@ function comment_revisions_help($section
   switch ($section) {
     case 'admin/help#comment_revisions':
       // Return a line-break version of the module README
-      return filter_filter('process', 2, NULL, file_get_contents( dirname(__FILE__)."/README.txt") );
+      return filter_filter('process', 2, NULL, file_get_contents( dirname(__FILE__) . "/README.txt"));
   }
 }
 
 /**
- * Implementation of hook_perm()
+ * Implementation of hook_perm().
  */
 function comment_revisions_perm() {
-  return array("administer comment revisions", // can change settings
-               "create revisions", // can create revisions for others
-               "willful create own", // can willful create revisions for own comments
-               "revert revisions", // can revert to other revisions
-               "delete revisions", // can delete revisions
-               "view revisions", // can only view revisions
-               "view own revisions", // can only view his own revisions
-               "force revision", // forces user to create revisions
-               "except from forced", // allows exceptions
-              );
+  return array(
+    'administer comment revisions', // Can change settings
+    'force comment revision', // Forces revisions to be created with no user input
+    'create comment revisions', // Can create revisions for any comments
+    'create own comment revisions', // Can create revisions for own comments
+    'delete comment revisions', // Can delete revisions for any comments
+    'delete own comment revisions', // Can delete own comment revisions
+    'revert comment revisions', // Can revert any comment revisions
+    'revert own comment revisions', // Can revert own comment revisions
+    'view comment revisions', // Can view any comment revisions
+    'view own comment revisions', // Can view own comment revisions
+  );
 }
 
 /**
- * FAPI definition for the user login form
+ * FAPI definition for the user login form.
  *
  * @ingroup forms
  * @see comment_revisions_admin_validate()
  * @see comment_revisions_admin_submit()
  */
 function comment_revisions_admin() {
-
-  $form['comment_revisions']['enable_usr_rev'] = array(
+  $form['comment_revisions_enable_usr_rev'] = array(
     '#type' => 'checkbox',
     '#title' => t("Enable automatic creation of user revisions"),
     '#description' => t('If a user edits his own content a new revision will be automatically created.'),
-    '#default_value' => variable_get("comment_revisions_enable_usr_rev", 1)
+    '#default_value' => variable_get("comment_revisions_enable_usr_rev", 1),
   );
-  $form['comment_revisions']['max_usr_rev'] = array(
+
+  $form['comment_revisions_max_usr_revisions'] = array(
     '#type' => 'textfield',
     '#title' => t("Maximum number of automatic revisions."),
-    '#description' => t('Maximum amount of revisions automatically created when a user edits his own content.'),
+    '#description' => t('Maximum amount of revisions automatically created when a user edits his own content. 0 = infinite.'),
     '#default_value' => variable_get("comment_revisions_max_usr_revisions", 5),
     '#size' => 2,
-    '#maxlength' => 6
+    '#maxlength' => 6,
+    '#required' => TRUE,
   );
 
-  $form['comment_revisions']['max_adm_rev'] = array(
+  $form['comment_revisions_max_admin'] = array(
     '#type' => 'textfield',
     '#title' => t('Maximum number of administrator revisions'),
-    '#description' => t('Maximum amount of revisions an administrator can create. 0 = infinite'),
+    '#description' => t('Maximum amount of revisions an administrator can create. 0 = infinite.'),
     '#default_value' => variable_get('comment_revisions_max_admin', 0),
     '#size' => 2,
     '#maxlength' => 6,
-    '#required' => TRUE
+    '#required' => TRUE,
+  );
+
+  $form['comment_revisions_enable_log_message_field'] = array(
+    '#type' => 'checkbox',
+    '#title' => t("Enable the log message field"),
+    '#description' => t('The log message field allowss you to enter a log message against revisions.'),
+    '#default_value' => variable_get("comment_revisions_enable_log_message_field", TRUE),
   );
 
   return system_settings_form($form);
 }
 
 /**
- * validates the form
+ * Form validate for comment_revisions_admin.
  */
 function comment_revisions_admin_validate($form_id, $form_values) {
-  if($form_values["max_adm_rev"] < 0) {
-    form_set_error("comment_revisions][max_adm_rev", t("Maximum amount of administrator revisions has to be greater than 0."));
+  if (!is_numeric($form_values['comment_revisions_max_admin'])) {
+    form_set_error('comment_revisions_max_admin', t('Maximum amount of administrator revisions has to be a number.'));
   }
-  if($form_values["max_usr_rev"] < 1) {
-    form_set_error("comment_revisions][max_usr_rev", t("Maximum amount of user revisions has to be at least 1."));
+  if (!is_numeric($form_values['comment_revisions_max_usr_revisions'])) {
+    form_set_error('comment_revisions_max_usr_revisions', t('Maximum amount of user revisions has to be a number.'));
   }
-}
-
-/**
- * submits the form
- */
-function comment_revisions_admin_submit($form_id, $form_values) {
 
-  variable_set("comment_revisions_enable_usr_rev", $form_values["enable_usr_rev"]);
-  variable_set("comment_revisions_max_usr_revisions", $form_values["max_usr_rev"]);
-  variable_set("comment_revisions_max_admin", $form_values["max_adm_rev"]);
-  drupal_set_message(t("Comment Revision settings saved."));
+  if ($form_values['comment_revisions_max_admin'] < 0) {
+    form_set_error('comment_revisions_max_admin', t('Maximum amount of administrator revisions has to be greater than 0.'));
+  }
+  if ($form_values['comment_revisions_max_usr_revisions'] < 0) {
+    form_set_error('comment_revisions_max_usr_revisions', t('Maximum amount of user revisions has to be greater than 0.'));
+  }
 }
 
 /**
- * Implementation of hook_menu
+ * Implementation of hook_menu().
  */
 function comment_revisions_menu($may_cache) {
   $items = array();
 
-  if($may_cache) {
-
-  } else {
-    $items[] = array('path' => 'admin/settings/commentrevisions',
+  if ($may_cache) {
+    $items[] = array(
+      'path' => 'admin/settings/commentrevisions',
       'title' => t('GB Comment Revisions settings'),
       'description' => t('Settings page for the GB Comment Revisions Module'),
       'callback' => 'drupal_get_form',
       'callback arguments' => 'comment_revisions_admin',
-      'access' => user_access('administer GB Comment Revisions'),
+      'access' => user_access('administer comment revisions'),
       'type' => MENU_NORMAL_ITEM
     );
-
-    /* If permission is "view revisions" */
+  }
+  else {
     $items[] = array(
-          'path' => 'commentrevision/'.arg(1),
-          'title' => t('Revisions'),
-          'callback' => 'comment_revisions_overview',
-          'callback arguments' => arg(1),
-          'access' => user_access('view revisions') || user_access('view own revisions'),
-          'weight' => 2,
-          'type' => MENU_CALLBACK,
+      'path' => 'commentrevision/' . arg(1),
+      'title' => t('Revisions'),
+      'callback' => 'comment_revisions_overview',
+      'callback arguments' => arg(1),
+      'access' => user_access('view comment revisions') || (user_access('view own comment revisions') && comment_revisions_is_comment_author(arg(1), 'cid')),
+      'weight' => 2,
+      'type' => MENU_CALLBACK,
     );
 
     $items[] = array(
-          'path' => 'commentrevision/'.arg(1).'/revert',
-          'title' => t('Revert Revision'),
-          'callback' => 'comment_revisions_revert',
-          'callback arguments' => arg(1),
-          'access' => user_access('revert revisions'),
-          'type' => MENU_CALLBACK
+      'path' => 'commentrevision/' . arg(1) . '/revert',
+      'title' => t('Revert Revision'),
+      'callback' => 'comment_revisions_revert',
+      'callback arguments' => arg(1),
+      'access' => user_access('revert comment revisions') || (user_access('revert own comment revisions') && comment_revisions_is_comment_author(arg(1))),
+      'type' => MENU_CALLBACK,
     );
+
     $items[] = array(
-          'path' => 'commentrevision/'.arg(1).'/delete',
-          'title' => t('Delete Revision'),
-          'callback' => 'comment_revisions_delete',
-          'callback arguments' => arg(1),
-          'access' => user_access('delete revisions'),
-          'type' => MENU_CALLBACK
+      'path' => 'commentrevision/' . arg(1) . '/delete',
+      'title' => t('Delete Revision'),
+      'callback' => 'comment_revisions_delete',
+      'callback arguments' => arg(1),
+      'access' => user_access('delete comment revisions') || (user_access('delete own comment revisions') && comment_revisions_is_comment_author(arg(1))),
+      'type' => MENU_CALLBACK,
     );
   }
 
@@ -144,282 +149,314 @@ function comment_revisions_menu($may_cac
 }
 
 /**
- * Implementation of hook_comment
+ * Implementation of hook_comment().
  */
-function comment_revisions_comment($a1, $op){
+function comment_revisions_comment($a1, $op) {
   global $user;
 
-  switch($op) {
+  switch ($op) {
     case "delete":
-      db_query("DELETE FROM {comment_revisions} WHERE cid=%d", $a1->cid);
-      db_query("DELETE FROM {comment_revisions_actual} WHERE cid=%d", $a1->cid);
+      db_query("DELETE FROM {comment_revisions} WHERE cid = %d", $a1->cid);
+      db_query("DELETE FROM {comment_revisions_actual} WHERE cid = %d", $a1->cid);
       break;
     case "validate":
-      /* Comment is edited and revision should be created */
-      if($a1["cid"] != NULL && $a1["create_revision"] == 1 && $a1["op"] != t("Preview comment")) {
-        /* No revision before, that means, the original has to be saved */
-        if(!_comment_revision_cmt_has_revisions($a1["cid"])) {
-          $result = db_fetch_array(db_query("SELECT * FROM {comments} WHERE cid=%d", $a1['cid']));
+      // Comment is edited and revision should be created
+      if ($a1["cid"] != NULL && $a1["create_revision"] == 1 && $a1["op"] != t("Preview comment")) {
+        // No revision before, that means, the original has to be saved
+        if (!_comment_revision_cmt_has_revisions($a1["cid"])) {
+          $result = db_fetch_array(db_query("SELECT * FROM {comments} WHERE cid = %d", $a1['cid']));
           db_query("INSERT INTO {comment_revisions} (vid, cid, comment, subject, logMessage, createdOn, createdBy, isUsrRev)
-                          VALUES(%d, %d, '%s', '%s', '%s', %d, %d, '%s')",
-                          db_next_id("{comment_revisions}_vid"),
-                          $result["cid"],
-                          $result["comment"],
-                          $result["subject"],
-                          t("Original comment"),
-                          $result["timestamp"],
-                          $result["uid"],
-                          '0');
+                    VALUES(%d, %d, '%s', '%s', '%s', %d, %d, '%s')",
+                   db_next_id("{comment_revisions}_vid"),
+                   $result["cid"],
+                   $result["comment"],
+                   $result["subject"],
+                   t("Original comment"),
+                   $result["timestamp"],
+                   $result["uid"],
+                   '0');
         }
       }
       break;
     case 'update':
-      if($user->uid == $a1['uid'] and !user_access('willful create own')) {
-          $isUserRevision = TRUE;
-        } else {
-          $isUserRevision = FALSE;
-        }
-        /* Prepare query to insert revision */
-        if($a1['create_revision']) {
-
-          $queryInfo = array();
-          $queryInfo['cid'] =                     $a1['cid'];
-          $queryInfo['comment'] =            $a1['comment'];
-          $queryInfo['subject'] =               $a1['subject'];
-          $queryInfo['log_message'] =      $a1['log_message'];
-          $queryInfo['createdOn'] = time();
-          $queryInfo['createdBy'] = $user->uid;
-          $queryInfo['isUsrRev'] = $isUserRevision ? '1' : '0';
-          $queryInfo['vid'] = db_next_id("{comment_revisions}_vid");
+      if ($user->uid == $a1['uid'] && !user_access('create owncomment revisions')) {
+          $is_user_revision = TRUE;
+      }
+      else {
+        $is_user_revision = FALSE;
+      }
+      // Prepare query to insert revision
+      if ($a1['create_revision']) {
+        $query_info = array();
+        $query_info['cid'] = $a1['cid'];
+        $query_info['comment'] = $a1['comment'];
+        $query_info['subject'] = $a1['subject'];
+        $query_info['log_message'] = $a1['log_message'];
+        $query_info['createdOn'] = time();
+        $query_info['createdBy'] = $user->uid;
+        $query_info['isUsrRev'] = $is_user_revision ? '1' : '0';
+        $query_info['vid'] = db_next_id("{comment_revisions}_vid");
 
-          db_query("INSERT INTO {comment_revisions}
-                  (vid, cid, comment, subject, logMessage, createdOn, createdBy, isUsrRev)
+        db_query("INSERT INTO {comment_revisions} (vid, cid, comment, subject, logMessage, createdOn, createdBy, isUsrRev)
                   VALUES (%d, %d,'%s','%s','%s', %d, %d, '%s')",
-          $queryInfo['vid'],
-          $queryInfo["cid"],
-          $queryInfo["comment"],
-          $queryInfo["subject"],
-          $queryInfo["log_message"],
-          $queryInfo["createdOn"],
-          $queryInfo["createdBy"],
-          $queryInfo["isUsrRev"]);
-
-          if($isUserRevision) {
-            $amountOfRevisions = db_result(db_query("SELECT COUNT(*) FROM {comment_revisions} WHERE cid=%d AND isUsrRev=1",$queryInfo['cid']));
-            $maxUsrRevisions = variable_get("comment_revisions_max_usr_revisions", 5);
-            if($amountOfRevisions > $maxUsrRevisions) {
-              $deleteRevID = db_result(db_query_range("SELECT vid FROM {comment_revisions} WHERE cid=%d AND isUsrRev ORDER BY createdOn ASC",$queryInfo['cid'],0,1));
-              db_query("DELETE FROM {comment_revisions} WHERE vid=%d", $deleteRevID);
-              drupal_set_message(t("One expired revision was deleted."));
-            }
-          }
-          if(!_comment_revision_cmt_has_revisions($queryInfo["cid"])) {
-            db_query("INSERT INTO {comment_revisions_actual} (cid, vid) VALUES (%d, %d)", $queryInfo["cid"], $queryInfo["vid"]);
-            drupal_set_message("Comment set to RevisionId:".$queryInfo["vid"]);
-          } else {
-            db_query("UPDATE {comment_revisions_actual} SET vid=%d WHERE cid=%d", $queryInfo["vid"], $queryInfo["cid"]);
-            drupal_set_message("Comment updated to RevisionId:". $queryInfo["vid"]);
+                 $query_info['vid'],
+                 $query_info["cid"],
+                 $query_info["comment"],
+                 $query_info["subject"],
+                 $query_info["log_message"],
+                 $query_info["createdOn"],
+                 $query_info["createdBy"],
+                 $query_info["isUsrRev"]);
+
+        $max_user_revisions = variable_get('comment_revisions_max_usr_revisions', 5);
+        if ($is_user_revision && $max_user_revisions != 0) {
+          $amount_of_revisions = db_result(db_query("SELECT COUNT(*) FROM {comment_revisions} WHERE cid = %d AND isUsrRev = 1",$query_info['cid']));
+          if ($amount_of_revisions > $max_user_revisions) {
+            $delete_rev_id = db_result(db_query_range("SELECT vid FROM {comment_revisions} WHERE cid = %d AND isUsrRev = 1 ORDER BY createdOn", $query_info['cid'], 0, 1));
+            db_query("DELETE FROM {comment_revisions} WHERE vid = %d", $delete_rev_id);
+            drupal_set_message(t("One expired revision was deleted."));
           }
-          drupal_set_message("One revision created.");
         }
-
+        if (!_comment_revision_cmt_has_revisions($query_info["cid"])) {
+          db_query("INSERT INTO {comment_revisions_actual} (cid, vid) VALUES (%d, %d)", $query_info["cid"], $query_info["vid"]);
+          drupal_set_message("Comment set to RevisionId:" . $query_info["vid"]);
+        }
+        else {
+          db_query("UPDATE {comment_revisions_actual} SET vid = %d WHERE cid = %d", $query_info["vid"], $query_info["cid"]);
+          drupal_set_message("Comment updated to RevisionId:". $query_info["vid"]);
+        }
+        drupal_set_message("One revision created.");
+      }
       break;
   }
 }
 
 /**
- * Implementation of hook_form_alter
+ * Implementation of hook_form_alter().
  */
-function comment_revisions_form_alter($form_id, &$form){
+function comment_revisions_form_alter($form_id, &$form) {
   global $user;
-  if($form["form_id"]["#id"] == "edit-comment-form" && $form["cid"]["#value"] != NULL) {
-    /* is comment editing form */
-
-    $isUserCmt = FALSE;
-    if($form['uid']['#value'] == $user->uid) {
-      $isUserCmt = TRUE;
+  if ($form['form_id']['#id'] == 'edit-comment-form' && $form['cid']['#value'] != NULL) {
+    // Is comment editing form
+    $is_user_comment = FALSE;
+    if ($form['uid']['#value'] == $user->uid) {
+      $is_user_comment = TRUE;
+    }
+    if (user_access('force comment revision')) {
+      // If user is forced to create a revision on edit.
+      $form['revisions']['create_revision'] = array(
+        '#type' => 'value',
+        '#value' => TRUE,
+      );
+      $form['revisions']['log_message'] = array(
+        '#type' => 'value',
+        '#value' => t('User revision'),
+      );
     }
-    if((user_access('create revisions') && !$isUserCmt) || (user_access('willful create own') && $isUserCmt)) {
-      /* if user can create revisions and its not his own, or it is his own and he is allowed to create his own revision */
-      /*$form["revisions"] = array(
+    else if (user_access('create comment revisions') || (user_access('create own comment revisions') && $is_user_comment)) {
+      // If user can create revisions and its not his own, or it is his own and he is allowed to create his own revision
+      $form['revisions'] = array(
         '#type' => 'fieldset',
         '#title' => t('Revision'),
-        '#weight' => -1,
+        '#weight' => 1,
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
-      ); */
-      $form["create_revision"] = array(
+      );
+      $form['revisions']['create_revision'] = array(
         '#type'=>'checkbox',
         '#title'=>t('Create Revision'),
         '#default_value' => TRUE,
       );
-      $form["log_message"] = array(
+      $form['revisions']['log_message'] = array(
         '#type' => 'textfield',
         '#title' => t('Log Message'),
         '#maxlength' => 256,
         '#default_value' => t('Edit'),
       );
-    } else if(user_access('force revision') && $isUserCmt && !user_access('except from force')){
-      /* if user is forced to create a revision on edit, and it is his own comment and not excepted from beeing forced */
-      $form["revisions"]["create_revision"] = array(
-        '#type' => 'value',
-        '#value' => TRUE,
-      );
-      $form["revisions"]["log_message"] = array(
-        '#type' => 'value',
-        '#value' => t('User revision'),
-      );
     }
   }
 }
 
 /**
- * Implementation of hook_link
+ * Implementation of hook_link().
  */
 function comment_revisions_link($type, $post = 0, $main = 0) {
   global $user;
   $links = array();
-  if($type == 'comment' && _comment_revision_cmt_has_revisions($post->cid) && user_access('view revisions')) {
-    $links["revisions"] = array(
-      'title' => t('View revisions'),
-      'href' => 'commentrevision/'. $post->cid
-    );
-  } else if ($user->uid == $post->uid && user_access('view own revisions') && _comment_revision_cmt_has_revisions($post->cid)) {
+  if ($type == 'comment' && _comment_revision_cmt_has_revisions($post->cid) && (user_access('view comment revisions') || ($user->uid == $post->uid && user_access('view own comment revisions')))) {
     $links["revisions"] = array(
       'title' => t('View revisions'),
-      'href' => 'commentrevision/'. $post->cid
+      'href' => 'commentrevision/' . $post->cid
     );
   }
   return $links;
 }
 
 /**
- * creates an overview of revisions for cid
+ * Creates an overview of revisions for cid.
  */
 function comment_revisions_overview($cid) {
   global $user;
   $creator = FALSE;
-  if(user_access('view own revisions')) {
+  if (user_access('view own comment revisions') && !user_access('view comment revisions')) {
     $creator = $user->uid;
   }
-  if(user_access('view revisions')) {
-    $creator = FALSE;
-  }
-  $revisions=  comment_revisions_list($cid, $creator);
-  $actualvid = db_result(db_query("SELECT vid FROM {comment_revisions_actual} WHERE cid=%d",$cid));
-  $rows = array();
 
-  foreach($revisions as $revision) {
+  $revisions = comment_revisions_list($cid, $creator);
+
+  $actualvid = db_result(db_query("SELECT vid FROM {comment_revisions_actual} WHERE cid = %d", $cid));
+  return theme('comment_revisions_overview', $revisions, $actualvid);
+}
+
+/**
+ * Theme the comment_revisions_overview form.
+ * @ingroup themable
+ */
+function theme_comment_revisions_overview($revisions, $actualvid) {
+  $rows = array();
+  foreach ($revisions as $revision) {
     $row = array();
     $operations = array();
-    $revisionIsUsrRev = ($revision->isUsrRev == 1) ? '<p class="revision_isUsrRev">'.t('Is a user-revision').'</p>':FALSE;
-    $revisionIsActual = ($revision->vid == $actualvid) ? TRUE : FALSE;
-    $tempar = array('data' => t('!date by !username', array('!date' => l(format_date($revision->createdOn, 'small'), "node/$revision->nid/",array(),NULL,"comment-$revision->cid"), '!username' => theme('username', $revision)))
-    . (($revision->logMessage != '') ? '<p class="revision-log">'. filter_xss($revision->logMessage) .'</p><p class="comment">'.filter_xss($revision->comment).'</p>' : '<p class="comment">'.filter_xss($revision->comment).'</p>') . "" . $revisionIsUsrRev);
-    if($revisionIsActual) {
-      $tempar['class'] = 'revision-current';
-      $operations[] = array('data' => theme('placeholder', t('current revision')) .'<br />'. l(t('delete'), "commentrevision/$revision->vid/delete"), 'class' => 'revision-current', 'colspan' => 2);
-
-    } else {
-      $operations[] = l(t('revert'), "commentrevision/$revision->vid/revert") .'<br />'. l(t('delete'), "commentrevision/$revision->vid/delete");
-      //$operations[] = l(t('delete'), "commentrevision/$revision->vid/delete");
+    $revision_is_user_rev = ($revision->isUsrRev == 1) ? '<p class="revision_isUsrRev">' . t('Is a user-revision') . '</p>' : FALSE;
+    $revision_is_actual = ($revision->vid == $actualvid) ? TRUE : FALSE;
+    $cell_class = $revision_is_actual ? 'revision-current' : '';
+
+    // Revision col.
+    $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->createdOn, 'small'), "node/{$revision->nid}/", array(), NULL, "comment-{$revision->cid}"), '!username' => theme('username', $revision)))
+    . (($revision->logMessage != '') ? '<p class="revision-log">' . filter_xss($revision->logMessage) . '</p><p class="comment">' . filter_xss($revision->comment) . '</p>' : '<p class="comment">' . filter_xss($revision->comment) . '</p>') . $revision_is_user_rev, 'class' => $cell_class);
+
+    // Operations col
+    if ($revision_is_actual) {
+      $operations[] = theme('placeholder', t('current revision'));
     }
+    foreach ($revision->current_user_ops as $op) {
+      if ($op == 'revert' && $revision_is_actual) {
+        continue;
+      }
+      $operations[] = l(t($op), "commentrevision/{$revision->vid}/$op");
+    }
+    $row[] = array('data' => implode('<br />', $operations), 'class' => $cell_class);
 
-    $row[] = $tempar;
-    $rows[] = array_merge($row, $operations);
+    $rows[] = $row;
   }
 
-  $header = array(t('Revision'), array('data' => t('Operations'), 'colspan' => 2));
-  /* create back link */
-  $path = "node/" ._comment_revisions_getnid($cid);
-  $pathfragment = "comment-". $cid;
-  $attributes = array();
-  $query = NULL;
-  $output = l(t("Back to comment"), $path , $attributes, $query, $pathfragment);
+  $header = array(t('Revision'), t('Operations'));
+  // Create back link.
+  $path = "node/" . _comment_revisions_getnid($cid);
+  $pathfragment = "comment-" . $cid;
+  $output = l(t("Back to comment"), $path, array(), NULL, $pathfragment);
   $output .= theme('table', $header, $rows);
   return $output;
 }
 
 /**
- * revert a comment to another revision
+ * Revert a comment to another revision.
  */
 function comment_revisions_revert($vid) {
   $revision = _comment_revisions_load_revision($vid);
-
   return drupal_get_form('comment_revisions_revert_confirm', $revision);
-
 }
 
 /**
- * confirm reverting
+ * Confirm revert form.
  */
 function comment_revisions_revert_confirm($revision) {
-  $form['revision'] = array('#type' => 'value', '#value' => $revision);
-  return confirm_form($form, t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($revision->createdOn))), 'commentrevisions/'. $revision->cid, '', t('Revert'), t('Cancel'));
+  $form['revision'] = array(
+    '#type' => 'value',
+    '#value' => $revision,
+  );
+  return confirm_form($form, t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($revision->createdOn))), 'commentrevision/'. $revision->cid, '', t('Revert'), t('Cancel'));
 }
 
 /**
- * submit reverting
+ * Submit revert form.
  */
 function comment_revisions_revert_confirm_submit($form_id, $form_values) {
   $revision = $form_values['revision'];
 
-  db_query("UPDATE {comment_revisions_actual} SET vid=%d WHERE cid=%d",$revision->vid, $revision->cid);
-  db_query("UPDATE {comments} SET comment='%s' WHERE cid=%d", $revision->comment, $revision->cid);
+  db_query("UPDATE {comment_revisions_actual} SET vid = %d WHERE cid = %d", $revision->vid, $revision->cid);
+  db_query("UPDATE {comments} SET comment = '%s' WHERE cid = %d", $revision->comment, $revision->cid);
+
   drupal_set_message(t('Comment Id: %cid has been reverted back to the revision from %revision-date', array('%revision-date' => format_date($revision->createdOn), '%cid' => $revision->cid)));
-  watchdog('comment',t('Reverted %cid to revision from %date.', array('%cid' => $revision->cid, '%date' => format_date($revision->createdOn))),WATCHDOG_NOTICE,l(t('view'), "commentrevision/".$revision->cid));
 
-  return 'commentrevision/'. $revision->cid;
+  watchdog('comment', t('Reverted %cid to revision from %date.', array('%cid' => $revision->cid, '%date' => format_date($revision->createdOn))), WATCHDOG_NOTICE, l(t('view'), "commentrevision/" . $revision->cid));
+
+  return 'commentrevision/' . $revision->cid;
 }
 
 /**
- * delete a revision
+ * Delete a revision.
  */
-function comment_revisions_delete($vid){
+function comment_revisions_delete($vid) {
   $revision = _comment_revisions_load_revision($vid);
-
   return drupal_get_form('comment_revisions_delete_confirm', $revision);
-
-  drupal_goto('commentrevision/'. $revision->cid);
 }
 
 /**
- * confirm deletion
+ * Confirm delete form.
  */
 function comment_revisions_delete_confirm($revision) {
-  $actualRevision = _comment_revision_get_actual_revision($revision->cid);
-  $extraInfo = "";
-  if($actualRevision == $revision->vid) {
-    $extraInfo = t("This is the actual revision of the comment. Deleting this, will revert the comment to the newest revision after deletion");
-  }
-  $form['revision'] = array('#type' => 'value', '#value' => $revision, '#description' => $extraInfo);
-  return confirm_form($form, t('Are you sure you want to delete the revision from %revision-date?', array('%revision-date' => format_date($revision->createdOn))), 'commentrevisions/'. $revision->cid, '', t('Delete'), t('Cancel'));
+  $actual_revision = _comment_revision_get_actual_revision($revision->cid);
+  $extra_info = "";
+  if ($actual_revision == $revision->vid) {
+    $extra_info = t("This is the actual revision of the comment. Deleting this, will revert the comment to the newest revision after deletion");
+  }
+  $form['revision'] = array(
+    '#type' => 'value',
+    '#value' => $revision,
+   '#description' => $extra_info,
+  );
+
+  return confirm_form($form, t('Are you sure you want to delete the revision from %revision-date?', array('%revision-date' => format_date($revision->createdOn))), 'commentrevisions/' . $revision->cid, '', t('Delete'), t('Cancel'));
 }
 
 /**
- * submit deletion
+ * Submit delete form.
  */
 function comment_revisions_delete_confirm_submit($form_id, $form_values) {
   $revision = $form_values['revision'];
 
-  db_query("DELETE FROM {comment_revisions} WHERE vid=%d", $revision->vid);
+  db_query("DELETE FROM {comment_revisions} WHERE vid = %d", $revision->vid);
+
   drupal_set_message(t('Revision for Comment Id: %cid from %revision-date has been deleted', array('%cid' => $revision->cid, '%revision-date' => format_date($revision->createdOn))));
-  watchdog('comment', t('Deleted Revision Id: %vid from %revision-date for Comment Id: %cid', array('%vid' => $revision->vid, '%revision-date' => format_date($revision->createdOn), '%cid' => $revision->cid)), WATCHDOG_NOTICE, l(t('view'), "commentrevision/".$revision->cid));
 
-  return 'commentrevision/'. $revision->cid;
+  watchdog('comment', t('Deleted Revision Id: %vid from %revision-date for Comment Id: %cid', array('%vid' => $revision->vid, '%revision-date' => format_date($revision->createdOn), '%cid' => $revision->cid)), WATCHDOG_NOTICE, l(t('view'), "commentrevision/" . $revision->cid));
+
+  return 'commentrevision/' . $revision->cid;
 }
 
 /**
- * create a list of revisions for cid
+ * Create a list of revisions for cid.
  */
 function comment_revisions_list($cid, $creator = FALSE) {
-  $revisions=  array();
-  if(!$creator){
-    $result = db_query('SELECT users.uid, users.name, revision.vid, revision.cid, comments.nid, revision.createdOn, revision.comment, revision.logMessage, revision.isUsrRev FROM {comment_revisions} revision INNER JOIN {comments} comments ON comments.cid = revision.cid INNER JOIN {users} users ON revision.createdBy = users.uid WHERE revision.cid = %d ORDER BY revision.createdOn DESC', $cid);
-  } else {
-    $result = db_query('SELECT users.uid, users.name, revision.vid, revision.cid, comments.nid, revision.createdOn, revision.comment, revision.logMessage, revision.isUsrRev FROM {comment_revisions} revision INNER JOIN {comments} comments ON comments.cid = revision.cid INNER JOIN {users} users ON revision.createdBy = users.uid WHERE revision.cid = %d AND revision.createdBy = "%s" ORDER BY revision.createdOn DESC', $cid, $creator);
-  }
+  $revisions = array();
+
+  $args = array($cid);
+  $and = '';
+  if ($creator) {
+    $args[] = $creator;
+    $and = " AND revision.createdBy = '%s' ";
+  }
+
+  $sql = "SELECT users.uid,
+                 users.name,
+                 revision.vid,
+                 revision.cid,
+                 comments.nid,
+                 revision.createdOn,
+                 revision.comment,
+                 revision.logMessage,
+                 revision.isUsrRev
+            FROM {comment_revisions} revision
+      INNER JOIN {comments} comments ON comments.cid = revision.cid
+      INNER JOIN {users} users ON revision.createdBy = users.uid
+           WHERE revision.cid = %d
+           $and
+        ORDER BY revision.createdOn DESC";
+
+  $result = db_query($sql, $args);
   while ($revision = db_fetch_object($result)) {
+    $revision->current_user_ops = comment_revisions_get_current_user_ops($revision);
     $revisions[] = $revision;
   }
 
@@ -427,34 +464,84 @@ function comment_revisions_list($cid, $c
 }
 
 /**
- * TRUE if comment has at least one revision
+ * Get the operations available to the current user for the given revision.
+ *
+ * @param $revision
+ *   A comment revision object
+ *
+ * @return
+ *   An array of the available operations.
+ */
+function comment_revisions_get_current_user_ops($revision) {
+  global $user;
+  $ops = array();
+  $own_comment = ($user->uid == $revision->uid) ? TRUE : FALSE;
+
+  if (user_access('revert comment revisions') || ($own_comment && user_access('revert own comment revisions'))) {
+    $ops[] = 'revert';
+  }
+  if (user_access('delete comment revisions') || ($own_comment && user_access('delete own comment revisions'))) {
+    $ops[] = 'delete';
+  }
+
+  return $ops;
+}
+
+/**
+ * Return whether or not the given comment has any revisions.
  */
 function _comment_revision_cmt_has_revisions($cid) {
-  $result = db_result(db_query("SELECT COUNT(*) FROM {comment_revisions_actual} WHERE cid=%d",$cid));
-  if($result < 1) {
+  $result = db_result(db_query("SELECT COUNT(*) FROM {comment_revisions_actual} WHERE cid = %d", $cid));
+  if ($result < 1) {
     return FALSE;
-  } else {
+  }
+  else {
     return TRUE;
   }
 }
 
 /**
- * loads a single revision
+ * Loads a single comment revision.
  */
 function _comment_revisions_load_revision($vid) {
-  return db_fetch_object(db_query('SELECT * FROM {comment_revisions} WHERE vid=%d', $vid));
+  return db_fetch_object(db_query('SELECT * FROM {comment_revisions} WHERE vid = %d', $vid));
 }
 
 /**
- * returns the actual vid
+ * Returns the actual vid of a cid.
  */
 function _comment_revision_get_actual_revision($cid) {
-  return db_result(db_query("SELECT vid FROM {comment_revisions_actual} WHERE cid=%d", $cid));
+  return db_result(db_query("SELECT vid FROM {comment_revisions_actual} WHERE cid = %d", $cid));
 }
 
 /**
- * returns nid of cid
+ * Returns nid of a cid.
  */
 function _comment_revisions_getnid($cid) {
-  return db_result(db_query("SELECT nid FROM {comments} WHERE cid=%d", $cid));
-}
\ No newline at end of file
+  return db_result(db_query("SELECT nid FROM {comments} WHERE cid = %d", $cid));
+}
+
+/**
+ * Check that the current user is the author of the comment
+ * of the given revision.
+ *
+ * @param $id
+ *   Revision id or comment id of the comment being acted on.
+ */
+function comment_revisions_is_comment_author($id, $field = 'vid') {
+  if ((!$id || !is_numeric($id)) ||($field != 'vid' && $field != 'cid')) {
+    return FALSE;
+  }
+
+  global $user;
+  $comment = db_fetch_object(db_query("SELECT c.*
+                                           FROM {comments} c,
+                                                {comment_revisions} r
+                                          WHERE c.cid = r.cid
+                                            AND r.$field = %d", $id));
+
+  if ($comment && $comment->uid == $user->uid) {
+    return TRUE;
+  }
+  return FALSE;
+}
