From db181f42b5f65cca2d62e9d4a60c358f485fbbdf Mon Sep 17 00:00:00 2001 From: Tristan Olive Date: Fri, 12 Jun 2015 14:06:40 -0400 Subject: [PATCH] Established a function to get the first unread comment for a given user and node (DBOINCP-167) --- .../boincuser/includes/boincuser.helpers.inc | 22 +++++++++++++++++++ .../default/boinc/themes/boinc/template.php | 18 ++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/drupal/sites/default/boinc/modules/boincuser/includes/boincuser.helpers.inc b/drupal/sites/default/boinc/modules/boincuser/includes/boincuser.helpers.inc index 8dc29c2f24..46b66293ba 100644 --- a/drupal/sites/default/boinc/modules/boincuser/includes/boincuser.helpers.inc +++ b/drupal/sites/default/boinc/modules/boincuser/includes/boincuser.helpers.inc @@ -94,6 +94,28 @@ function boincuser_check_credit_requirements() { } } +/** + * Get the cid of the first comment the user has not seen on a given node + */ +function boincuser_get_first_unread_comment_id($nid, $uid = NULL) { + if (!$uid) { + global $user; + $uid = $user->uid; + } + return db_result(db_query(" + SELECT c.cid + FROM {node} n + INNER JOIN {comments} c ON c.nid = n.nid + LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d + WHERE n.nid = %d + AND n.status = 1 + AND c.timestamp > h.timestamp + ORDER BY c.timestamp ASC + LIMIT 1", + $uid, $nid + )); +} + /** * Determine the first unique name from a given base */ diff --git a/drupal/sites/default/boinc/themes/boinc/template.php b/drupal/sites/default/boinc/themes/boinc/template.php index a0e4847bfe..ab78892aee 100644 --- a/drupal/sites/default/boinc/themes/boinc/template.php +++ b/drupal/sites/default/boinc/themes/boinc/template.php @@ -472,24 +472,10 @@ function boinc_preprocess_comment(&$vars, $hook) { */ function boinc_preprocess_forum_topic_list(&$variables) { if (!empty($variables['topics'])) { - global $user; foreach ($variables['topics'] as $id => $topic) { if ($topic->new_replies) { - $cid = db_result(db_query(" - SELECT c.cid - FROM {node} n - INNER JOIN {comments} c ON c.nid = n.nid - LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d - WHERE n.nid = %d - AND n.status = 1 - AND c.timestamp > h.timestamp - ORDER BY c.timestamp ASC - LIMIT 1", - $user->uid, $topic->nid - )); - if ($cid) { - $variables['topics'][$id]->new_url = url("goto/comment/{$cid}"); - } + $cid = boincuser_get_first_unread_comment_id($topic->nid); + $variables['topics'][$id]->new_url = url("goto/comment/{$cid}"); } } }