Advertisement
Guest User

Untitled

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