groups->current_group->id; } class FSR { var $_points = 0; var $_user; var $_momentLimit = 10; /** * Create the database tables to support plugin behaviour. * * @param boolean $echo If true echoes messages to user */ function install($echo = false) { global $table_prefix, $wpdb; $table_name = $table_prefix . "fsr_post"; if ($wpdb->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 $group_rate, $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={$group_rate}"); } 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}', {$group_rate}, {$this->_points}, '{$ip}', '{$vote_date}')"); $table_name = $table_prefix . "fsr_post"; if ($wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE ID={$group_rate}")) { $wpdb->query("UPDATE {$table_name} SET votes=votes+1, points=points+{$this->_points} WHERE ID={$group_rate};"); } else { $wpdb->query("INSERT INTO {$table_name} (ID, votes, points) VALUES ({$group_rate}, 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 $group_rate, $wpdb, $table_prefix; $table_name = $table_prefix . "fsr_post"; return $wpdb->get_row("SELECT votes, points FROM {$table_name} WHERE ID={$group_rate}"); } /** * 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 = '