Advertisement
Guest User

Untitled

a guest
Sep 30th, 2010
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. <?php
  2. /**
  3. * Class that manages all the features of Five Star Rating Wordpress plugin
  4. *
  5. */
  6.  
  7. function group_rate() {
  8. global $bp;
  9. $group_rate = $bp->groups->current_group->id;
  10. }
  11.  
  12. class FSR {
  13. var $_points = 0;
  14. var $_user;
  15. var $_momentLimit = 10;
  16.  
  17. /**
  18. * Create the database tables to support plugin behaviour.
  19. *
  20. * @param boolean $echo If true echoes messages to user
  21. */
  22. function install($echo = false) {
  23. global $table_prefix, $wpdb;
  24.  
  25. $table_name = $table_prefix . "fsr_post";
  26. if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) {
  27. $sql = "CREATE TABLE {$table_name} (
  28. ID bigint(20) unsigned NOT NULL default '0',
  29. votes int(10) unsigned NOT NULL default '0',
  30. points int(10) unsigned NOT NULL default '0',
  31. PRIMARY KEY (ID)
  32. );";
  33.  
  34. require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
  35. dbDelta($sql);
  36. if ($echo) _e("Table has been created\n");
  37. } else {
  38. if ($echo) _e("The table has already been created\n");
  39. }
  40.  
  41. $table_name = $table_prefix . "fsr_user";
  42. if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) {
  43. $sql = "CREATE TABLE {$table_name} (
  44. user varchar(32) NOT NULL default '',
  45. post bigint(20) unsigned NOT NULL default '0',
  46. points int(10) unsigned NOT NULL default '0',
  47. ip char(15) NOT NULL,
  48. vote_date datetime NOT NULL,
  49. PRIMARY KEY (`user`,post),
  50. KEY vote_date (vote_date)
  51. );";
  52. require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
  53. dbDelta($sql);
  54. if ($echo) _e("Scorecard created\n");
  55. } elseif (!$wpdb->get_row("SHOW COLUMNS FROM {$table_name} LIKE 'vote_date'")) {
  56. $wpdb->query("ALTER TABLE {$table_name} ADD ip CHAR( 15 ) NOT NULL, ADD vote_date DATETIME NOT NULL");
  57. $wpdb->query("ALTER TABLE {$table_name} ADD INDEX (vote_date)");
  58. if ($echo) _e("Scorecard has been updated\n");
  59. } else {
  60. if ($echo) _e("The scorecard was already created\n");
  61. }
  62. }
  63.  
  64. /**
  65. * Get the html that shows the stars for voting
  66. * If the user has already vote then it shows stars with puntuation. No voting is allowed
  67. *
  68. * @return string
  69. */
  70. function getVotingStars($starType) {
  71. global $group_rate, $wpdb, $table_prefix;
  72. $rated = false;
  73. if (isset($this->_user)) {
  74. $user = $wpdb->escape($this->_user);
  75. $table_name = $table_prefix . "fsr_user";
  76. $rated = (bool) $wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE user='{$user}' AND post={$group_rate}");
  77. }
  78. if (($this->_points > 0) && !$rated) {
  79. $user = $wpdb->escape($this->_user);
  80. $table_name = $table_prefix . "fsr_user";
  81. $ip = $_SERVER['REMOTE_ADDR'];
  82. $vote_date = date('Y-m-d H:i:s');
  83. $wpdb->query("INSERT INTO {$table_name} (user, post, points, ip, vote_date) VALUES ('{$user}', {$group_rate}, {$this->_points}, '{$ip}', '{$vote_date}')");
  84. $table_name = $table_prefix . "fsr_post";
  85. if ($wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE ID={$group_rate}")) {
  86. $wpdb->query("UPDATE {$table_name} SET votes=votes+1, points=points+{$this->_points} WHERE ID={$group_rate};");
  87. } else {
  88. $wpdb->query("INSERT INTO {$table_name} (ID, votes, points) VALUES ({$group_rate}, 1, {$this->_points});");
  89. }
  90. $rated = true;
  91. // $this->_setBestsOfMoment();
  92. }
  93. $data = $this->_getPoints();
  94. if ($rated || !isset($_COOKIE['wp_fsr'])) {
  95. $html = $this->_drawStars($data->votes, $data->points,$starType);
  96. } else {
  97. $html = $this->_drawVotingStars($data->votes, $data->points,$starType);
  98. }
  99. return $html;
  100. }
  101.  
  102. /**
  103. * Get the html that shows the stars with puntuation.
  104. *
  105. * @return string
  106. */
  107. function getStars($starType) {
  108. $data = $this->_getPoints();
  109. return $this->_drawStars($data->votes, $data->points,$starType);
  110. }
  111.  
  112. /**
  113. * Get the points and votes of current post
  114. *
  115. * @return object
  116. */
  117. function _getPoints() {
  118. global $group_rate, $wpdb, $table_prefix;
  119. $table_name = $table_prefix . "fsr_post";
  120. return $wpdb->get_row("SELECT votes, points FROM {$table_name} WHERE ID={$group_rate}");
  121. }
  122.  
  123. /**
  124. * Draw the stars
  125. *
  126. * @param int $votes
  127. * @param int $points
  128. * @return string
  129. */
  130. function _drawStars($votes, $points, $starType) {
  131. if ($votes > 0) {
  132. $rate = $points / $votes;
  133. } else {
  134. $rate = 0;
  135. }
  136. $html = '<div class="FSR_container"><div class="FSR_stars"> ';
  137. for ($i = 1; $i <= 5; ++$i) {
  138. if ($i <= $rate) {
  139. $class = 'FSR_full_' . $starType;
  140. $char = '*';
  141. } elseif ($i <= ($rate + .5)) {
  142. $class = 'FSR_half_' . $starType;
  143. $char = '&frac12;';
  144. } else {
  145. $class = 'FSR_no_' . $starType;
  146. $char = '&nbsp;';
  147. }
  148. $html .= '<span class="' . $class . '">' . $char . '</span> ';
  149. }
  150. $html .= '<span class="FSR_votes">' . (int) $votes . '</span> ';
  151. if($votes > 1) {
  152. $html .= '<span class="FSR_tvotes">' . __('votes') . '</span>';
  153. } else {
  154. $html .= '<span class="FSR_tvotes">' . __('vote') . '</span>';
  155. }
  156. $html .= '</div>';
  157. if(get_option('fsr_show_credit') == "true") {
  158. $html .= '<div class="fsr_credits">powered by <a href="http://wordpress-plug.in/featured/five-star-rating/" title="Five Star Rating">Five Star Rating</a></span>';
  159. }
  160. else {
  161. $html .= '<!-- powered by Five Star Rating- http://wordpress-plug.in/featured/five-star-rating/ -->';
  162. }
  163. $html .= '</div>';
  164. $html .= '<!-- cookie: ' . $_COOKIE['wp_fsr'] . ' -->';
  165. return $html;
  166. }
  167.  
  168. /**
  169. * Draw the voting stars
  170. *
  171. * @param int $votes
  172. * @param int $points
  173. * @return string
  174. */
  175. function _drawVotingStars($votes, $points, $type) {
  176. global $group_rate;
  177. if ($votes > 0) {
  178. $rate = $points / $votes;
  179. } else {
  180. $rate = 0;
  181. }
  182. $html = '<div class="FSR_container"><form id="FSR_form_' . $group_rate . '" action="' . WP_PLUGIN_URL . '/five-star-rating/fsr-ajax-stars.php" method="post" class="FSR_stars" onmouseout="FSR_star_out(this)"> ';
  183. for ($i = 1; $i <= 5; ++$i) {
  184. if ($i <= $rate) {
  185. $class = 'FSR_full_voting_' . $type;
  186. $char = '*';
  187. } elseif ($i <= ($rate + .5)) {
  188. $class = 'FSR_half_voting_' . $type;
  189. $char = '&frac12;';
  190. } else {
  191. $class = 'FSR_no_voting_' . $type;
  192. $char = '&nbsp;';
  193. }
  194. //$html .= sprintf('<input type="radio" id="fsr_star_%1$d_%2$d" class="star" name="fsr_stars" value="%2$d"/><label class="%3$s" for="fsr_star_%1$d_%2$d" onmouseover="FSR_star_over(this, %2$d)">%2$d</label> ', $group_rate, $i, $class);
  195. $html .= sprintf('<input type="radio" id="fsr_star_%1$d_%2$d" class="star" name="fsr_stars" value="%2$d"/><label class="%3$s" for="fsr_star_%1$d_%2$d">%2$d</label> ', $group_rate, $i, $class);
  196. }
  197. $html .= '<span class="FSR_votes">' . (int) $votes . '</span> ';
  198. if( $votes > 1) {
  199. $html .= '<span class="FSR_tvotes">' . __('votes') . '</span>';
  200. }
  201. else {
  202. $html .= '<span class="FSR_tvotes">' . __('vote') .'</span>';
  203. }
  204. $html .= '<span class="FSR_tvote FSR_important"> ' . __('Cast your vote now!') . '</span>';
  205. $html .= '<input type="hidden" name="p" value="' . $group_rate . '" />';
  206. $html .= '<input type="hidden" name="starType" value="' . $type . '" />';
  207. $html .= '<input type="submit" name="vote" value="' . __('Voting') . '" />';
  208. $html .= '</form>';
  209. if(get_option('fsr_show_credit') == "true") {
  210. $html .= '<div class="fsr_credits">powered by <a href="http://wordpress-plug.in/featured/five-star-rating/" title="Five Star Rating">Five Star Rating</a></div>';
  211. }
  212. else {
  213. $html .= '<!-- powered by Five Star Rating- http://wordpress-plug.in/featured/five-star-rating/ -->';
  214. }
  215. $html .= '</div>';
  216. $html .= '<!-- cookie: ' . $_COOKIE['wp_fsr'] . ' -->';
  217. return $html;
  218. }
  219.  
  220. function getBestOfMonth($star_type = 'star') {
  221. global $wpdb, $table_prefix;
  222. $month = date('m');
  223. $limit = 10;
  224. $table_name = $table_prefix . "fsr_user";
  225. $sql = "SELECT post, COUNT(*) AS votes, SUM(points) AS points, AVG(points)
  226. FROM {$table_name}
  227. WHERE MONTH(vote_date)={$month} AND YEAR(vote_date)=YEAR(NOW())
  228. GROUP BY 1
  229. ORDER BY 4 DESC, 2 DESC
  230. LIMIT {$limit}";
  231. $data = $wpdb->get_results($sql);
  232. if (is_array($data)) {
  233. $html = '<ul class="FSR_month_scores">';
  234. foreach ($data AS $row) {
  235. $title = get_the_title($row->post);
  236. $html .= '<li><a class="post_title" href="' . get_permalink($row->post) . '" title="' . $title . '">' . $title . '</a> ' . $this->_drawStars($row->votes, $row->points,$star_type) . '</li>';
  237. }
  238. $html .= '</ul>';
  239. return $html;
  240. }
  241. }
  242.  
  243. /**
  244. * Initialize the values.
  245. * Get the puntuation from url and the user from the cookies.
  246. * If no cookie exists generate a new user.
  247. * Refresh the cookie to hold the value of user for 1 year
  248. *
  249. */
  250. function init() {
  251. if (isset($_COOKIE['wp_fsr'])) {
  252. $this->_user = $_COOKIE['wp_fsr'];
  253. }
  254. else {
  255. if (!isset($this->_user)) {
  256. srand((double)microtime()*1234567);
  257. $this->_user = md5(microtime() . rand(1000, 90000000));
  258. }
  259. }
  260. $cookieTime = time()*60;
  261. $cookie_expiration = get_option('fsr_cookie_expiration');
  262. $cookie_expiration_unit = get_option('fsr_cookie_expiration_unit');
  263. switch ($cookie_expiration_unit) {
  264. case 'minute':
  265. $cookieTime = time()+60*$cookie_expiration;
  266. break;
  267. case 'hour':
  268. $cookieTime = time()+60*60*$cookie_expiration;
  269. break;
  270. case 'day':
  271. $cookieTime = time()+60*60*24*$cookie_expiration;
  272. break;
  273. }
  274. setcookie('wp_fsr', $this->_user, $cookieTime, '/');
  275. if (isset($_REQUEST['fsr_stars'])) {
  276. $points = (int) $_REQUEST['fsr_stars'];
  277. if (($points > 0) && ($points <= 5)) {
  278. $this->_points = $points;
  279. }
  280. }
  281. }
  282. }
  283. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement