/Your Code/Web App-Code/wp-content/plugins/gd-star-rating/code/blg/db.php

https://bitbucket.org/midasinc/your-submission · PHP · 516 lines · 444 code · 64 blank · 8 comment · 34 complexity · 4ba8e1767e3dab94ce7cdcd86e46eb86 MD5 · raw file

  1. <?php
  2. class gdsrBlgDB {
  3. function get_rss_multi_data($post_id) {
  4. global $wpdb, $table_prefix;
  5. $sql = sprintf("select * from %sgdsr_multis_data where post_id = %s order by (total_votes_users + total_votes_visitors) desc limit 0, 1", $table_prefix, $post_id);
  6. return $wpdb->get_row($sql);
  7. }
  8. function get_rss_multi_data_review($post_id) {
  9. global $wpdb, $table_prefix;
  10. $sql = sprintf("select * from %sgdsr_multis_data where post_id = %s order by average_review desc limit 0, 1", $table_prefix, $post_id);
  11. return $wpdb->get_row($sql);
  12. }
  13. function add_new_view($post_id) {
  14. if (intval($post_id) > 0) {
  15. global $wpdb, $table_prefix;
  16. $dbt_data_article = $table_prefix.'gdsr_data_article';
  17. $sql = sprintf("update %s set views = views + 1 where post_id = %s", $dbt_data_article, $post_id);
  18. $wpdb->query($sql);
  19. }
  20. }
  21. function get_comments_aggregation($post_id, $filter_show = "total") {
  22. global $wpdb, $table_prefix;
  23. $where = "";
  24. switch ($filter_show) {
  25. default:
  26. case "total":
  27. $where = " user_voters + visitor_voters > 0";
  28. break;
  29. case "users":
  30. $where = " user_voters > 0";
  31. break;
  32. case "visitors":
  33. $where = " visitor_voters > 0";
  34. break;
  35. }
  36. $sql = sprintf("SELECT * FROM %sgdsr_data_comment where post_id = %s and %s", $table_prefix, $post_id, $where);
  37. return $wpdb->get_results($sql);
  38. }
  39. function lock_post($post_id, $rules_articles = "N") {
  40. global $wpdb, $table_prefix;
  41. $wpdb->query(sprintf("update %sgdsr_data_article set rules_articles = '%s' where post_id = %s",
  42. $table_prefix, $rules_articles, $post_id));
  43. }
  44. // ip
  45. function check_ip_single($ip) {
  46. global $wpdb, $table_prefix;
  47. $sql = sprintf("select count(*) from %sgdsr_ips where `status` = 'B' and `mode` = 'S' and `ip` = '%s'", $table_prefix, $ip);
  48. return $wpdb->get_var($sql) > 0;
  49. }
  50. function check_ip_range($ip) {
  51. global $wpdb, $table_prefix;
  52. $sql = sprintf("select count(*) from %sgdsr_ips where `status` = 'B' and `mode` = 'R' and inet_aton(substring_index(ip, '|', 1)) <= inet_aton('%s') and inet_aton(substring_index(ip, '|', -1)) >= inet_aton('%s')", $table_prefix, $ip, $ip);
  53. return $wpdb->get_var($sql) > 0;
  54. }
  55. function check_ip_mask($ip) {
  56. global $wpdb, $table_prefix;
  57. $sql = sprintf("select ip from %sgdsr_ips where `status` = 'B' and `mode` = 'M'", $table_prefix);
  58. $ips = $wpdb->get_results($sql);
  59. foreach ($ips as $i) {
  60. $mask = explode('.', $i->ip);
  61. $ip = explode('.', $ip);
  62. for ($i = 0; $i < 4; $i++) {
  63. if (is_numeric($mask[$i])) {
  64. if ($ip[$i] != $mask[$i]) return false;
  65. }
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. // ip
  72. // check vote
  73. function check_vote_table($table, $id, $user, $type, $ip, $mixed = false) {
  74. global $wpdb, $table_prefix;
  75. if ($user > 0) {
  76. $votes_sql = sprintf("SELECT count(*) FROM %s WHERE vote_type = '%s' and id = %s and user_id = %s", $table_prefix.$table, $type, $id, $user);
  77. wp_gdsr_dump("CHECK_VOTE_USER", $votes_sql);
  78. $votes = $wpdb->get_var($votes_sql);
  79. return $votes == 0;
  80. } else {
  81. $votes_sql = sprintf("SELECT count(*) FROM %s WHERE vote_type = '%s' and id = %s and ip = '%s'", $table_prefix.$table, $type, $id, $ip);
  82. wp_gdsr_dump("CHECK_VOTE", $votes_sql);
  83. $votes = $wpdb->get_var($votes_sql);
  84. if ($votes > 0 && $mixed) {
  85. $votes_sql = sprintf("SELECT count(*) FROM %s WHERE vote_type = '%s' and user_id > 0 and id = %s and ip = '%s'", $table_prefix.$table, $type, $id, $ip);
  86. wp_gdsr_dump("CHECK_VOTE_MIX", $votes_sql);
  87. $votes_mixed = $wpdb->get_var($votes_sql);
  88. if ($votes_mixed > 0) $votes = 0;
  89. }
  90. return $votes == 0;
  91. }
  92. }
  93. function check_vote($id, $user, $type, $ip, $mod_only = false, $mixed = false) {
  94. $result = true;
  95. if (!$mod_only) $result = gdsrBlgDB::check_vote_logged($id, $user, $type, $ip, $mixed);
  96. if ($result) $result = gdsrBlgDB::check_vote_moderated($id, $user, $type, $ip, $mixed);
  97. return $result;
  98. }
  99. function check_vote_logged($id, $user, $type, $ip, $mixed = false) {
  100. return gdsrBlgDB::check_vote_table('gdsr_votes_log', $id, $user, $type, $ip, $mixed);
  101. }
  102. function check_vote_moderated($id, $user, $type, $ip, $mixed = false) {
  103. return gdsrBlgDB::check_vote_table('gdsr_moderate', $id, $user, $type, $ip, $mixed);
  104. }
  105. // check vote
  106. // save thumb votes
  107. function save_vote_comment_thumb($id, $user, $ip, $ua, $vote) {
  108. global $wpdb, $table_prefix;
  109. $ua = str_replace("'", "''", $ua);
  110. $ua = substr($ua, 0, 250);
  111. $post = $wpdb->get_row("select comment_post_ID from $wpdb->comments where comment_ID = ".$id);
  112. $post_id = $post->comment_post_ID;
  113. $sql = sprintf("SELECT * FROM %sgdsr_data_article WHERE post_id = %s", $table_prefix, $post_id);
  114. $post_data = $wpdb->get_row($sql);
  115. if ($post_data->recc_moderate_comments == "" || $post_data->recc_moderate_comments == "N" || ($post_data->recc_moderate_comments == "V" && $user > 0) || ($post_data->recc_moderate_comments == "U" && $user == 0)) {
  116. gdsrBlgDB::add_vote_comment_thumb($id, $user, $ip, $ua, $vote);
  117. } else {
  118. $modsql = sprintf("INSERT INTO %sgdsr_moderate (id, vote_type, user_id, vote, voted, ip, user_agent) VALUES (%s, 'cmmthumb', %s, %s, '%s', '%s', '%s')",
  119. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua);
  120. $wpdb->query($modsql);
  121. }
  122. }
  123. function save_vote_thumb($id, $user, $ip, $ua, $vote, $comment_id = 0) {
  124. global $wpdb, $table_prefix;
  125. $ua = str_replace("'", "''", $ua);
  126. $ua = substr($ua, 0, 250);
  127. $sql = sprintf("SELECT * FROM %sgdsr_data_article WHERE post_id = %s", $table_prefix, $id);
  128. $post_data = $wpdb->get_row($sql);
  129. if (count($post_data) == 0) {
  130. GDSRDatabase::add_default_vote($id);
  131. $post_data = $wpdb->get_row($sql);
  132. }
  133. if ($post_data->recc_moderate_articles == "" || $post_data->recc_moderate_articles == "N" || ($post_data->recc_moderate_articles == "V" && $user > 0) || ($post_data->recc_moderate_articles == "U" && $user == 0)) {
  134. gdsrBlgDB::add_vote_thumb($id, $user, $ip, $ua, $vote, $comment_id);
  135. } else {
  136. $modsql = sprintf("INSERT INTO %sgdsr_moderate (id, vote_type, user_id, vote, voted, ip, user_agent, comment_id) VALUES (%s, 'artthumb', %s, %s, '%s', '%s', '%s', %s)",
  137. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua, $comment_id);
  138. $wpdb->query($modsql);
  139. }
  140. }
  141. function add_vote_comment_thumb($id, $user, $ip, $ua, $vote) {
  142. global $wpdb, $table_prefix;
  143. $trend_date = date("Y-m-d");
  144. $sql_trend = sprintf("SELECT count(*) FROM %sgdsr_votes_trend WHERE vote_date = '%s' and vote_type = 'cmmthumb' and id = %s", $table_prefix, $trend_date, $id);
  145. $trend_data = $wpdb->get_var($sql_trend);
  146. $trend_added = false;
  147. if ($trend_data == 0) {
  148. $trend_added = true;
  149. if ($user > 0) {
  150. $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, user_voters, user_votes, vote_date) VALUES (%s, 'cmmthumb', 1, %s, '%s')",
  151. $table_prefix, $id, $vote, $trend_date);
  152. $wpdb->query($sql);
  153. } else {
  154. $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, visitor_voters, visitor_votes, vote_date) VALUES (%s, 'cmmthumb', 1, %s, '%s')",
  155. $table_prefix, $id, $vote, $trend_date);
  156. $wpdb->query($sql);
  157. }
  158. }
  159. if ($user > 0) {
  160. $part = $vote == 1 ? "user_recc_plus = user_recc_plus + 1" : "user_recc_minus = user_recc_minus + 1";
  161. if (!$trend_added) {
  162. $sql = sprintf("UPDATE %sgdsr_votes_trend SET user_voters = user_voters + 1, user_votes = user_votes + %s WHERE id = %s and vote_type = 'cmmthumb' and vote_date = '%s'",
  163. $table_prefix, $vote, $id, $trend_date);
  164. $wpdb->query($sql);
  165. }
  166. } else {
  167. $part = $vote == 1 ? "visitor_recc_plus = visitor_recc_plus + 1" : "visitor_recc_minus = visitor_recc_minus + 1";
  168. if (!$trend_added) {
  169. $sql = sprintf("UPDATE %sgdsr_votes_trend SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s WHERE id = %s and vote_type = 'cmmthumb' and vote_date = '%s'",
  170. $table_prefix, $vote, $id, $trend_date);
  171. $wpdb->query($sql);
  172. }
  173. }
  174. $sql = sprintf("UPDATE %sgdsr_data_comment SET %s, last_voted_recc = CURRENT_TIMESTAMP WHERE comment_id = %s",
  175. $table_prefix, $part, $id);
  176. $wpdb->query($sql);
  177. wp_gdsr_dump("SAVE_THUMB_VOTE", $sql);
  178. $logsql = sprintf("INSERT INTO %sgdsr_votes_log (id, vote_type, user_id, vote, object, voted, ip, user_agent) VALUES (%s, 'cmmthumb', %s, %s, '', '%s', '%s', '%s')",
  179. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua);
  180. $wpdb->query($logsql);
  181. wp_gdsr_dump("SAVE_THUMB_LOG", $logsql);
  182. }
  183. function add_vote_thumb($id, $user, $ip, $ua, $vote, $comment_id = 0) {
  184. global $wpdb, $table_prefix;
  185. $trend_date = date("Y-m-d");
  186. $sql_trend = sprintf("SELECT count(*) FROM %sgdsr_votes_trend WHERE vote_date = '%s' and vote_type = 'artthumb' and id = %s", $table_prefix, $trend_date, $id);
  187. $trend_data = $wpdb->get_var($sql_trend);
  188. $trend_added = false;
  189. if ($trend_data == 0) {
  190. $trend_added = true;
  191. if ($user > 0) {
  192. $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, user_voters, user_votes, vote_date) VALUES (%s, 'artthumb', 1, %s, '%s')",
  193. $table_prefix, $id, $vote, $trend_date);
  194. $wpdb->query($sql);
  195. } else {
  196. $sql = sprintf("INSERT INTO %sgdsr_votes_trend (id, vote_type, visitor_voters, visitor_votes, vote_date) VALUES (%s, 'artthumb', 1, %s, '%s')",
  197. $table_prefix, $id, $vote, $trend_date);
  198. $wpdb->query($sql);
  199. }
  200. }
  201. if ($user > 0) {
  202. $part = $vote == 1 ? "user_recc_plus = user_recc_plus + 1" : "user_recc_minus = user_recc_minus + 1";
  203. if (!$trend_added) {
  204. $sql = sprintf("UPDATE %sgdsr_votes_trend SET user_voters = user_voters + 1, user_votes = user_votes + %s WHERE id = %s and vote_type = 'artthumb' and vote_date = '%s'",
  205. $table_prefix, $vote, $id, $trend_date);
  206. $wpdb->query($sql);
  207. }
  208. } else {
  209. $part = $vote == 1 ? "visitor_recc_plus = visitor_recc_plus + 1" : "visitor_recc_minus = visitor_recc_minus + 1";
  210. if (!$trend_added) {
  211. $sql = sprintf("UPDATE %sgdsr_votes_trend SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s WHERE id = %s and vote_type = 'artthumb' and vote_date = '%s'",
  212. $table_prefix, $vote, $id, $trend_date);
  213. $wpdb->query($sql);
  214. }
  215. }
  216. $sql = sprintf("UPDATE %sgdsr_data_article SET %s, last_voted_recc = CURRENT_TIMESTAMP WHERE post_id = %s",
  217. $table_prefix, $part, $id);
  218. $wpdb->query($sql);
  219. wp_gdsr_dump("SAVE_THUMB_VOTE", $sql);
  220. $logsql = sprintf("INSERT INTO %sgdsr_votes_log (id, vote_type, user_id, vote, object, voted, ip, user_agent, comment_id) VALUES (%s, 'artthumb', %s, %s, '', '%s', '%s', '%s', %s)",
  221. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua, $comment_id);
  222. $wpdb->query($logsql);
  223. wp_gdsr_dump("SAVE_THUMB_LOG", $logsql);
  224. }
  225. // save thumb votes
  226. // save stars votes
  227. function save_vote($id, $user, $ip, $ua, $vote, $comment_id = 0) {
  228. global $wpdb, $table_prefix;
  229. $ua = str_replace("'", "''", $ua);
  230. $ua = substr($ua, 0, 250);
  231. $sql = sprintf("SELECT * FROM %sgdsr_data_article WHERE post_id = %s", $table_prefix, $id);
  232. $post_data = $wpdb->get_row($sql);
  233. if (count($post_data) == 0) {
  234. GDSRDatabase::add_default_vote($id);
  235. $post_data = $wpdb->get_row($sql);
  236. }
  237. if ($post_data->moderate_articles == "" || $post_data->moderate_articles == "N" || ($post_data->moderate_articles == "V" && $user > 0) || ($post_data->moderate_articles == "U" && $user == 0)) {
  238. gdsrBlgDB::add_vote($id, $user, $ip, $ua, $vote, $comment_id);
  239. } else {
  240. $modsql = sprintf("INSERT INTO %sgdsr_moderate (id, vote_type, user_id, vote, voted, ip, user_agent, comment_id) VALUES (%s, 'article', %s, %s, '%s', '%s', '%s', %s)",
  241. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua, $comment_id);
  242. $wpdb->query($modsql);
  243. }
  244. }
  245. function save_vote_comment($id, $user, $ip, $ua, $vote) {
  246. global $wpdb, $table_prefix;
  247. $ua = str_replace("'", "''", $ua);
  248. $ua = substr($ua, 0, 250);
  249. $post = $wpdb->get_row("select comment_post_ID from $wpdb->comments where comment_ID = ".$id);
  250. $post_id = $post->comment_post_ID;
  251. $sql = sprintf("SELECT * FROM %sgdsr_data_article WHERE post_id = %s", $table_prefix, $post_id);
  252. $post_data = $wpdb->get_row($sql);
  253. if ($post_data->moderate_comments == "" || $post_data->moderate_comments == "N" || ($post_data->moderate_comments == "V" && $user > 0) || ($post_data->moderate_comments == "U" && $user == 0)) {
  254. gdsrBlgDB::add_vote_comment($id, $user, $ip, $ua, $vote);
  255. } else {
  256. $modsql = sprintf("INSERT INTO %sgdsr_moderate (id, vote_type, user_id, vote, voted, ip, user_agent) VALUES (%s, 'comment', %s, %s, '%s', '%s', '%s')",
  257. $table_prefix, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua);
  258. $wpdb->query($modsql);
  259. }
  260. }
  261. function add_vote_comment($id, $user, $ip, $ua, $vote) {
  262. global $wpdb, $table_prefix;
  263. $comments = $table_prefix.'gdsr_data_comment';
  264. $stats = $table_prefix.'gdsr_votes_log';
  265. $trend = $table_prefix.'gdsr_votes_trend';
  266. $trend_date = date("Y-m-d");
  267. $sql_trend = sprintf("SELECT count(*) FROM %s WHERE vote_date = '%s' and vote_type = 'comment' and id = %s", $trend, $trend_date, $id);
  268. $trend_data = $wpdb->get_var($sql_trend);
  269. wp_gdsr_dump("SAVEVOTE_CMM_trend_check_sql", $sql_trend);
  270. wp_gdsr_dump("SAVEVOTE_CMM_trend_check_error", $wpdb->last_error);
  271. $trend_added = false;
  272. if ($trend_data == 0) {
  273. $trend_added = true;
  274. if ($user > 0) {
  275. $sql = sprintf("INSERT INTO %s (id, vote_type, user_voters, user_votes, vote_date) VALUES (%s, 'comment', 1, %s, '%s')",
  276. $trend, $id, $vote, $trend_date);
  277. $wpdb->query($sql);
  278. } else {
  279. $sql = sprintf("INSERT INTO %s (id, vote_type, visitor_voters, visitor_votes, vote_date) VALUES (%s, 'comment', 1, %s, '%s')",
  280. $trend, $id, $vote, $trend_date);
  281. $wpdb->query($sql);
  282. }
  283. wp_gdsr_dump("SAVEVOTE_CMM_trend_insert_sql", $sql);
  284. wp_gdsr_dump("SAVEVOTE_CMM_trend_insert_error", $wpdb->last_error);
  285. }
  286. if ($user > 0) {
  287. $sql = sprintf("UPDATE %s SET user_voters = user_voters + 1, user_votes = user_votes + %s, last_voted = CURRENT_TIMESTAMP WHERE comment_id = %s",
  288. $comments, $vote, $id);
  289. $wpdb->query($sql);
  290. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_user_sql", $sql);
  291. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_user_error", $wpdb->last_error);
  292. if (!$trend_added) {
  293. $sql = sprintf("UPDATE %s SET user_voters = user_voters + 1, user_votes = user_votes + %s WHERE id = %s and vote_type = 'comment' and vote_date = '%s'",
  294. $trend, $vote, $id, $trend_date);
  295. $wpdb->query($sql);
  296. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_user_sql", $sql);
  297. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_user_error", $wpdb->last_error);
  298. }
  299. } else {
  300. $sql = sprintf("UPDATE %s SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s, last_voted = CURRENT_TIMESTAMP WHERE comment_id = %s",
  301. $comments, $vote, $id);
  302. $wpdb->query($sql);
  303. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_visitor_sql", $sql);
  304. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_visitor_error", $wpdb->last_error);
  305. if (!$trend_added) {
  306. $sql = sprintf("UPDATE %s SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s WHERE id = %s and vote_type = 'comment' and vote_date = '%s'",
  307. $trend, $vote, $id, $trend_date);
  308. $wpdb->query($sql);
  309. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_visitor_sql", $sql);
  310. wp_gdsr_dump("SAVEVOTE_CMM_trend_update_visitor_error", $wpdb->last_error);
  311. }
  312. }
  313. $logsql = sprintf("INSERT INTO %s (id, vote_type, user_id, vote, voted, ip, user_agent) VALUES (%s, 'comment', %s, %s, '%s', '%s', '%s')",
  314. $stats, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua);
  315. $wpdb->query($logsql);
  316. wp_gdsr_dump("SAVEVOTE_CMM_insert_stats_sql", $sql);
  317. wp_gdsr_dump("SAVEVOTE_CMM_insert_stats_id", $wpdb->insert_id);
  318. wp_gdsr_dump("SAVEVOTE_CMM_insert_stats_error", $wpdb->last_error);
  319. }
  320. function add_vote($id, $user, $ip, $ua, $vote, $comment_id = 0) {
  321. global $wpdb, $table_prefix;
  322. $articles = $table_prefix.'gdsr_data_article';
  323. $stats = $table_prefix.'gdsr_votes_log';
  324. $trend = $table_prefix.'gdsr_votes_trend';
  325. $trend_date = date("Y-m-d");
  326. $sql_trend = sprintf("SELECT count(*) FROM %s WHERE vote_date = '%s' and vote_type = 'article' and id = %s", $trend, $trend_date, $id);
  327. $trend_data = $wpdb->get_var($sql_trend);
  328. wp_gdsr_dump("SAVEVOTE_trend_check_sql", $sql_trend);
  329. wp_gdsr_dump("SAVEVOTE_trend_check_error", $wpdb->last_error);
  330. $trend_added = false;
  331. if ($trend_data == 0) {
  332. $trend_added = true;
  333. if ($user > 0) {
  334. $sql = sprintf("INSERT INTO %s (id, vote_type, user_voters, user_votes, vote_date) VALUES (%s, 'article', 1, %s, '%s')",
  335. $trend, $id, $vote, $trend_date);
  336. $wpdb->query($sql);
  337. } else {
  338. $sql = sprintf("INSERT INTO %s (id, vote_type, visitor_voters, visitor_votes, vote_date) VALUES (%s, 'article', 1, %s, '%s')",
  339. $trend, $id, $vote, $trend_date);
  340. $wpdb->query($sql);
  341. }
  342. wp_gdsr_dump("SAVEVOTE_trend_insert_sql", $sql);
  343. wp_gdsr_dump("SAVEVOTE_trend_insert_error", $wpdb->last_error);
  344. }
  345. if ($user > 0) {
  346. $sql = sprintf("UPDATE %s SET user_voters = user_voters + 1, user_votes = user_votes + %s, last_voted = CURRENT_TIMESTAMP WHERE post_id = %s",
  347. $articles, $vote, $id);
  348. $wpdb->query($sql);
  349. wp_gdsr_dump("SAVEVOTE_update_user_sql", $sql);
  350. wp_gdsr_dump("SAVEVOTE_update_user", $wpdb->last_error);
  351. if (!$trend_added) {
  352. $sql = sprintf("UPDATE %s SET user_voters = user_voters + 1, user_votes = user_votes + %s WHERE id = %s and vote_type = 'article' and vote_date = '%s'",
  353. $trend, $vote, $id, $trend_date);
  354. $wpdb->query($sql);
  355. wp_gdsr_dump("SAVEVOTE_trend_added_user_sql", $sql);
  356. wp_gdsr_dump("SAVEVOTE_trend_added_user_error", $wpdb->last_error);
  357. }
  358. } else {
  359. $sql = sprintf("UPDATE %s SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s, last_voted = CURRENT_TIMESTAMP WHERE post_id = %s",
  360. $articles, $vote, $id);
  361. $wpdb->query($sql);
  362. wp_gdsr_dump("SAVEVOTE_update_visitor_sql", $sql);
  363. wp_gdsr_dump("SAVEVOTE_update_visitor_error", $wpdb->last_error);
  364. if (!$trend_added) {
  365. $sql = sprintf("UPDATE %s SET visitor_voters = visitor_voters + 1, visitor_votes = visitor_votes + %s WHERE id = %s and vote_type = 'article' and vote_date = '%s'",
  366. $trend, $vote, $id, $trend_date);
  367. $wpdb->query($sql);
  368. }
  369. wp_gdsr_dump("SAVEVOTE_trend_added_visitor_sql", $sql);
  370. wp_gdsr_dump("SAVEVOTE_trend_added_visitor_error", $wpdb->last_error);
  371. }
  372. $logsql = sprintf("INSERT INTO %s (id, vote_type, user_id, vote, object, voted, ip, user_agent, comment_id) VALUES (%s, 'article', %s, %s, '', '%s', '%s', '%s', %s)",
  373. $stats, $id, $user, $vote, str_replace("'", "''", current_time('mysql')), $ip, $ua, $comment_id);
  374. $wpdb->query($logsql);
  375. wp_gdsr_dump("SAVEVOTE_insert_stats_sql", $sql);
  376. wp_gdsr_dump("SAVEVOTE_insert_stats_id", $wpdb->insert_id);
  377. wp_gdsr_dump("SAVEVOTE_insert_stats_error", $wpdb->last_error);
  378. }
  379. // save stars votes
  380. function taxonomy_multi_ratings_data($taxonomy = "category", $terms = array(), $multi_id = 0, $by = "name") {
  381. global $wpdb, $table_prefix;
  382. $select = "d.id as mdid, v.source, x.name as title, t.term_id, v.item_id, sum(v.user_voters) as user_voters";
  383. $select.= ", sum(v.user_votes) as user_votes, sum(v.visitor_voters) as visitor_voters, sum(v.visitor_votes) as visitor_votes";
  384. $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, %sterms x, %sgdsr_multis_values v, ", $table_prefix, $table_prefix, $table_prefix, $table_prefix);
  385. $where = array("d.id = v.id", "t.term_taxonomy_id = r.term_taxonomy_id", "r.object_id = p.id", "t.term_id = x.term_id", "p.id = d.post_id", "p.post_status = 'publish'", "d.multi_id = ".$multi_id, sprintf("t.taxonomy = '%s'", $taxonomy));
  386. if (count($terms) > 0) {
  387. $clean_terms = array();
  388. foreach ($terms as $t) $clean_terms[] = "'".str_replace("'", "''", $t)."'";
  389. $where[] = sprintf("x.%s in (%s)", $by, join(", ", $clean_terms));
  390. }
  391. $sql = sprintf("select distinct %s from %s%sposts p, %sgdsr_multis_data d where %s group by x.term_id, v.source, v.item_id order by x.term_id, d.id, v.source, v.item_id",
  392. $select, $from, $table_prefix, $table_prefix, join(" and ", $where));
  393. return $wpdb->get_results($sql);
  394. }
  395. function taxonomy_multi_ratings($taxonomy = "category", $terms = array(), $multi_id = 0, $by = "name") {
  396. global $wpdb, $table_prefix, $wp_taxonomies;
  397. $select = "d.id as mdid, x.name as title, t.term_id, count(*) as counter, sum(d.average_rating_users * d.total_votes_users) as user_votes, sum(d.average_rating_visitors * d.total_votes_visitors) as visitor_votes, sum(d.total_votes_users) as user_voters, sum(d.total_votes_visitors) as visitor_voters, sum(d.average_review)/count(*) as review, 0 as votes, 0 as voters";
  398. $select.= ", 0 as rating, 0 as bayesian, '' as rating_stars, '' as bayesian_stars, '' as review_stars, '' as review_block, '' as rating_block";
  399. $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, %sterms x, ", $table_prefix, $table_prefix, $table_prefix);
  400. $where = array("t.term_taxonomy_id = r.term_taxonomy_id", "r.object_id = p.id", "t.term_id = x.term_id", "p.id = d.post_id", "p.post_status = 'publish'", "d.multi_id = ".$multi_id, sprintf("t.taxonomy = '%s'", $taxonomy));
  401. if (count($terms) > 0) {
  402. $clean_terms = array();
  403. foreach ($terms as $t) $clean_terms[] = "'".str_replace("'", "''", $t)."'";
  404. $where[] = sprintf("x.%s in (%s)", $by, join(", ", $clean_terms));
  405. }
  406. $sql = sprintf("select distinct %s from %s%sposts p, %sgdsr_multis_data d where %s group by t.term_id",
  407. $select, $from, $table_prefix, $table_prefix, join(" and ", $where));
  408. return $wpdb->get_results($sql);
  409. }
  410. }
  411. ?>