Advertisement
Guest User

Untitled

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