diff quotedb.tcl @ 439:ac66900fd066

quotedb: Add display of current quote rating when voting for it.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Apr 2017 15:20:11 +0300
parents 86f39989df4b
children 6acd372d36da
line wrap: on
line diff
--- a/quotedb.tcl	Tue Feb 07 10:35:11 2017 +0200
+++ b/quotedb.tcl	Wed Apr 26 15:20:11 2017 +0300
@@ -226,6 +226,15 @@
 
 
 #-------------------------------------------------------------------------
+proc qdb_get_rating_for_id {utable uid} {
+  set usql "SELECT total(${utable}_votes.vote) AS qrating FROM ${utable}_votes WHERE urlid=${uid}"
+  set qrating 0
+  qdb_handle eval $usql { set urating $qrating }
+  return $qrating
+}
+
+
+#-------------------------------------------------------------------------
 proc qdb_vote {ucmd utable unick uhand uchan utext upublic} {
   global qdb_handle qdb_msg_you_voted qdb_msg_quote_does_not_exist qdb_msg_vote_updated
 
@@ -259,8 +268,9 @@
     # Yes, update the previous vote
     set usql "UPDATE ${utable}_votes SET vote=$uvote WHERE id=$qid"
     if {[qdb_sql_exec $upublic $unick $uchan $usql]} {
-      qdb_log "$uhand changed vote to $uvote on #$uid / $utable"
-      qdb_msg $upublic $unick $uchan $qdb_msg_vote_updated [list $uid $uvote]
+      set urating [qdb_get_rating_for_id $utable $uid]
+      qdb_log "$uhand changed vote to $uvote on #$uid / $utable, total $urating"
+      qdb_msg $upublic $unick $uchan $qdb_msg_vote_updated [list $uid $uvote $urating]
       return 1
     } else {
       return 0
@@ -270,8 +280,9 @@
   # No previous votes, insert new
   set usql "INSERT INTO ${utable}_votes (user,urlid,vote) VALUES ('[utl_escape $uhand]',$uid,$uvote)"
   if {[qdb_sql_exec $upublic $unick $uchan $usql]} {
-    qdb_log "$uhand voted $uvote on #$uid / $utable"
-    qdb_msg $upublic $unick $uchan $qdb_msg_you_voted [list $uid $uvote]
+    set urating [qdb_get_rating_for_id $utable $uid]
+    qdb_log "$uhand voted $uvote on #$uid / $utable, total $urating"
+    qdb_msg $upublic $unick $uchan $qdb_msg_you_voted [list $uid $uvote $urating]
   }
 }