From b1a606962a1c95de0c9fcd4b5c23fd0ac6a56a70 Mon Sep 17 00:00:00 2001
From: Jordan Chase <carbon6@gmail.com>
Date: Mon, 28 Dec 2015 17:31:12 -0500
Subject: [PATCH] Implimenting hook_comment_update so if the comment status is
 changed to 'unpublished' the session cookie is set back to view unpublished
 comments`

---
 su_comments.module | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/su_comments.module b/su_comments.module
index 5fed6ae..16767b8 100644
--- a/su_comments.module
+++ b/su_comments.module
@@ -9,6 +9,16 @@ function su_comments_comment_insert($comment) {
 }
 
 /**
+ * Impliments hook_comment_update
+ * When the comment is updated to unpublished, also set the session cookie 
+ */
+function su_comments_comment_update($comment) {
+  if ($comment->status == COMMENT_NOT_PUBLISHED && !user_access('administer comments')) { 
+    $_SESSION['su_comments'][] = $comment->cid;
+  }
+}
+
+/**
  * Implements hook_query_TAG_alter().
  */
 function su_comments_query_comment_filter_alter(QueryAlterableInterface $query) {
-- 
2.6.2

From 24b65cbea15a2eddaa100a1499c146b628a74f8d Mon Sep 17 00:00:00 2001
From: Jordan Chase <carbon6@gmail.com>
Date: Mon, 28 Dec 2015 17:59:16 -0500
Subject: [PATCH] Changed mechanism for viewing unpublished comments using
 drupals 'variable_get' and 'variable_set' functions.  This allows a
 persistent view of unpublished comments regardless of whether the user
 cleared their cookies or not`

---
 su_comments.module | 71 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 10 deletions(-)

diff --git a/su_comments.module b/su_comments.module
index 16767b8..681293f 100644
--- a/su_comments.module
+++ b/su_comments.module
@@ -1,11 +1,35 @@
 <?php
 
-function su_comments_comment_insert($comment) {
+function _su_comments_fetch_global_var() {
+  // Fetched the global $su_comments or initializes it if it doesn't exist
+  global $user;
   // If this comment is not published and the user does not have the rights to
-  // see unpublished comments, store the cid in the session.
+  // see unpublished comments, store the cid in the global variable.
+  $su_comments = variable_get($su_comments);
+  if(!$su_comments) {
+    $su_comments = array();
+  }
+  if(!$su_comments[$user->uid]) {
+    $su_comments[$user->uid] = array();
+  }
+  return $su_comments;
+}
+
+function su_comments_comment_insert($comment) {
+  global $user; 
+  
+  // Fetch global var
+  $su_comments = _su_comments_fetch_global_var();
+  
   if ($comment->status == COMMENT_NOT_PUBLISHED && !user_access('administer comments')) {
-    $_SESSION['su_comments'][] = $comment->cid;
+    array_push($su_comments[$user->uid], $comment_cid);
+  }
+  else if($comment->status == COMMENT_PUBLISHED && !user_access('administer comments')) {
+    $idx = array_search($commend_cid, $su_comments[$user->uid]);
+    // Remove element from global su_comments array
+    unset($su_comments[$user->uid][$idx]);
   }
+  variable_set('su_comments', $su_comments); 
 }
 
 /**
@@ -13,18 +37,35 @@ function su_comments_comment_insert($comment) {
  * When the comment is updated to unpublished, also set the session cookie 
  */
 function su_comments_comment_update($comment) {
+  global $user;
+  
+  // Fetch global var
+  $su_comments = _su_comments_fetch_global_var();
+ 
   if ($comment->status == COMMENT_NOT_PUBLISHED && !user_access('administer comments')) { 
-    $_SESSION['su_comments'][] = $comment->cid;
+    array_push($su_comments[$user->uid], $comment_cid);
+  } 
+  else if($comment->status == COMMENT_PUBLISHED && !user_access('administer comments')) {
+    $idx = array_search($commend_cid, $su_comments[$user->uid]);
+    // Remove element from global su_comments array
+    unset($su_comments[$user->uid][$idx]);
   }
+  variable_set('su_comments', $su_comments); 
 }
 
 /**
  * Implements hook_query_TAG_alter().
  */
 function su_comments_query_comment_filter_alter(QueryAlterableInterface $query) {
+  global $user;
+  die();
   // Only alter the query if the user does not have administer comments
-  // permission and has a session.
-  if (!user_access('administer comments') && !empty($_SESSION['su_comments'])) {
+  // permission and $su_comments is not empty
+  
+  // Fetch global var
+  $su_comments = _su_comments_fetch_global_var();
+  
+  if (!user_access('administer comments') && !empty($su_comments[$user->uid])) {
     $conditions = &$query->conditions();
     // Remove status condition.
     foreach ($conditions as $key => $condition) {
@@ -38,7 +79,7 @@ function su_comments_query_comment_filter_alter(QueryAlterableInterface $query)
         // by this session.
         $or = db_or();
         $or->condition('c.status', COMMENT_PUBLISHED);
-        $or->condition('c.cid', $_SESSION['su_comments']);
+        $or->condition('c.cid', $su_comments[$user->uid]);
         $query->condition($or);
         return;
       }
@@ -56,10 +97,14 @@ function su_comments_query_comment_filter_alter(QueryAlterableInterface $query)
  * @see comment_node_page_additions()
  */
 function su_comments_node_view($node, $view_mode) {
+  global $user;
   // this first condition is copied from comment_node_view():
   // add comments only if we are on the node page, in "full" view mode
+  // Fetch global var
+  $su_comments = _su_comments_fetch_global_var();
+  
   if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
-    if (empty($node->content['comments']['comments']) && $node->comment_count == 0 && isset($_SESSION['su_comments'])) {
+    if (empty($node->content['comments']['comments']) && $node->comment_count == 0 && isset($su_comments[$user->uid])) {
       // there are no published comments (so the comment thread has not been
       // built), but there may be an unpublished comment that we need to show.
       $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
@@ -79,12 +124,18 @@ function su_comments_node_view($node, $view_mode) {
  * Implements hook_comment_view_alter().
  */
 function su_comments_comment_view_alter(&$build) {
+  global $user;
+  
   // Only act on comments which are not published and if this user does not have
   // administer comments permission, and comment is in $_SESSION
+  
+  // Fetch global var
+  $su_comments = _su_comments_fetch_global_var();
+  
   if ($build['#comment']->status == COMMENT_NOT_PUBLISHED
     && !user_access('administer comments')
-    && !empty($_SESSION['su_comments'])
-    && in_array($build['#comment']->cid, $_SESSION['su_comments'])) {
+    && !empty($su_comments[$user->uid])
+    && in_array($build['#comment']->cid, $su_comments[$user->uid])) {
     // Hide the reply link as trying to reply results in an error that there is
     // no such comment.
     unset($build['links']['comment']['#links']['comment-reply']);
-- 
2.6.2

