get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) { $sql = "CREATE TABLE {$table_name} ( ID bigint(20) unsigned NOT NULL default '0', votes int(10) unsigned NOT NULL default '0', points int(10) unsigned NOT NULL default '0', PRIMARY KEY (ID) );"; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta($sql); if ($echo) _e("Table has been created\n"); } else { if ($echo) _e("The table has already been created\n"); } $table_name = $table_prefix . "fsr_user"; if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) { $sql = "CREATE TABLE {$table_name} ( user varchar(32) NOT NULL default '', post bigint(20) unsigned NOT NULL default '0', points int(10) unsigned NOT NULL default '0', ip char(15) NOT NULL, vote_date datetime NOT NULL, PRIMARY KEY (`user`,post), KEY vote_date (vote_date) );"; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta($sql); if ($echo) _e("Scorecard created\n"); } elseif (!$wpdb->get_row("SHOW COLUMNS FROM {$table_name} LIKE 'vote_date'")) { $wpdb->query("ALTER TABLE {$table_name} ADD ip CHAR( 15 ) NOT NULL, ADD vote_date DATETIME NOT NULL"); $wpdb->query("ALTER TABLE {$table_name} ADD INDEX (vote_date)"); if ($echo) _e("Scorecard has been updated\n"); } else { if ($echo) _e("The scorecard was already created\n"); } } /** * Get the html that shows the stars for voting * If the user has already vote then it shows stars with puntuation. No voting is allowed * * @return string */ function getVotingStars($starType) { global $bp, $wpdb, $table_prefix; $rated = false; if (isset($this->_user)) { $user = $wpdb->escape($this->_user); $table_name = $table_prefix . "fsr_user"; $rated = (bool) $wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE user='{$user}' AND post={$bp->groups->current_group->id}"); } if (($this->_points > 0) && !$rated) { $user = $wpdb->escape($this->_user); $table_name = $table_prefix . "fsr_user"; $ip = $_SERVER['REMOTE_ADDR']; $vote_date = date('Y-m-d H:i:s'); $wpdb->query("INSERT INTO {$table_name} (user, post, points, ip, vote_date) VALUES ('{$user}', {$bp->groups->current_group->id}, {$this->_points}, '{$ip}', '{$vote_date}')"); $table_name = $table_prefix . "fsr_post"; if ($wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE ID={$bp->groups->current_group->id}")) { $wpdb->query("UPDATE {$table_name} SET votes=votes+1, points=points+{$this->_points} WHERE ID={$bp->groups->current_group->id};"); } else { $wpdb->query("INSERT INTO {$table_name} (ID, votes, points) VALUES ({$bp->groups->current_group->id}, 1, {$this->_points});"); } $rated = true; // $this->_setBestsOfMoment(); } $data = $this->_getPoints(); if ($rated || !isset($_COOKIE['wp_fsr'])) { $html = $this->_drawStars($data->votes, $data->points,$starType); } else { $html = $this->_drawVotingStars($data->votes, $data->points,$starType); } return $html; } /** * Get the html that shows the stars with puntuation. * * @return string */ function getStars($starType) { $data = $this->_getPoints(); return $this->_drawStars($data->votes, $data->points,$starType); } /** * Get the points and votes of current post * * @return object */ function _getPoints() { global $bp, $wpdb, $table_prefix; $table_name = $table_prefix . "fsr_post"; return $wpdb->get_row("SELECT votes, points FROM {$table_name} WHERE ID={$bp->groups->current_group->id}"); } /** * Draw the stars * * @param int $votes * @param int $points * @return string */ function _drawStars($votes, $points, $starType) { if ($votes > 0) { $rate = $points / $votes; } else { $rate = 0; } $html = '
'; for ($i = 1; $i <= 5; ++$i) { if ($i <= $rate) { $class = 'FSR_full_' . $starType; $char = '*'; } elseif ($i <= ($rate + .5)) { $class = 'FSR_half_' . $starType; $char = '½'; } else { $class = 'FSR_no_' . $starType; $char = ' '; } $html .= '' . $char . ' '; } $html .= '' . (int) $votes . ' '; if($votes > 1) { $html .= '' . __('votes') . ''; } else { $html .= '' . __('vote') . ''; } $html .= '
'; if(get_option('fsr_show_credit') == "true") { $html .= '
powered by Five Star Rating'; } else { $html .= ''; } $html .= '
'; $html .= ''; return $html; } /** * Draw the voting stars * * @param int $votes * @param int $points * @return string */ function _drawVotingStars($votes, $points, $type) { global $bp; if ($votes > 0) { $rate = $points / $votes; } else { $rate = 0; } $html = '
'; for ($i = 1; $i <= 5; ++$i) { if ($i <= $rate) { $class = 'FSR_full_voting_' . $type; $char = '*'; } elseif ($i <= ($rate + .5)) { $class = 'FSR_half_voting_' . $type; $char = '½'; } else { $class = 'FSR_no_voting_' . $type; $char = ' '; } //$html .= sprintf(' ', $id, $i, $class); $html .= sprintf(' ', $bp, $i, $class); } $html .= '' . (int) $votes . ' '; if( $votes > 1) { $html .= '' . __('votes') . ''; } else { $html .= '' . __('vote') .''; } $html .= ' ' . __('Cast your vote now!') . ''; $html .= ''; $html .= ''; $html .= ''; $html .= '
'; if(get_option('fsr_show_credit') == "true") { $html .= '
powered by Five Star Rating
'; } else { $html .= ''; } $html .= '
'; $html .= ''; return $html; } function getBestOfMonth($star_type = 'star') { global $wpdb, $table_prefix; $month = date('m'); $limit = 10; $table_name = $table_prefix . "fsr_user"; $sql = "SELECT post, COUNT(*) AS votes, SUM(points) AS points, AVG(points) FROM {$table_name} WHERE MONTH(vote_date)={$month} AND YEAR(vote_date)=YEAR(NOW()) GROUP BY 1 ORDER BY 4 DESC, 2 DESC LIMIT {$limit}"; $data = $wpdb->get_results($sql); if (is_array($data)) { $html = ''; return $html; } } /** * Initialize the values. * Get the puntuation from url and the user from the cookies. * If no cookie exists generate a new user. * Refresh the cookie to hold the value of user for 1 year * */ function init() { if (isset($_COOKIE['wp_fsr'])) { $this->_user = $_COOKIE['wp_fsr']; } else { if (!isset($this->_user)) { srand((double)microtime()*1234567); $this->_user = md5(microtime() . rand(1000, 90000000)); } } $cookieTime = time()*60; $cookie_expiration = get_option('fsr_cookie_expiration'); $cookie_expiration_unit = get_option('fsr_cookie_expiration_unit'); switch ($cookie_expiration_unit) { case 'minute': $cookieTime = time()+60*$cookie_expiration; break; case 'hour': $cookieTime = time()+60*60*$cookie_expiration; break; case 'day': $cookieTime = time()+60*60*24*$cookie_expiration; break; } setcookie('wp_fsr', $this->_user, $cookieTime, '/'); if (isset($_REQUEST['fsr_stars'])) { $points = (int) $_REQUEST['fsr_stars']; if (($points > 0) && ($points <= 5)) { $this->_points = $points; } } } } ?>