Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Allows users to to rate posts
- *
- * @copyright Copyright (C) 2009 crurf.info
- * @license http://creativecommons.org/licenses/by-sa/3.0/ Creative Commons License version 3.0 or higher
- * @package ajax_rating
- */
- function AR_loadRating($postid) {
- global $forum_db;
- $retvalue = array(
- 'postid' => $postid,
- 'yescnt' => 0,
- 'nocnt' => 0,
- 'rating' => 0,
- 'cnt' => 0,
- );
- $query = array(
- 'SELECT' => '*',
- 'FROM' => 'postrating',
- 'WHERE' => 'postid = ' . $postid,
- );
- $result = $forum_db->query_build($query);
- if ((int)$forum_db->num_rows($result)) {
- list($retvalue['postid'], $retvalue['yescnt'], $retvalue['nocnt'], $retvalue['rating'], $retvalue['cnt']) = $forum_db->fetch_row($result);
- }
- return $retvalue;
- }
- function AR_getRating($postid, &$opts, $is_guest) {
- global $lang_ajax_rating, $forum_config, $forum_user;
- if (!(int)$postid) return;
- $info = AR_loadRating($postid);
- $active = (boolean)(!$forum_user['is_guest'] || ($forum_user['is_guest'] && (int)$forum_config['p_ajax_rating_allow_guest']));
- $html = '<div class="post_rating">';
- if ((int)$forum_config['p_ajax_rating_type']) {
- $html .= '
- <ul id="star' . $postid . '" class="star" ' . ($active ? 'onmousedown="star.update(event,this)" onmousemove="star.mouse(event,this)"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . ' title="' . $lang_ajax_rating['Rate This Post'] . '">
- <li id="starCur' . $postid . '" class="curr" title="' . (int)$info['rating'] . '" style="width: ' . (int)$info['rating'] . '%;"></li>
- </ul>
- <div id="starUser' . $postid . '" class="user">' . (int)$info['rating'] . '%</div>
- ';
- } else {
- $html .= '<a class="rate_yes" title="' . $lang_ajax_rating['Approve'] . '" href="javascript: void(0);" ' . ($active ? 'onclick="AR_rate(this, ' . (int)$postid . ', 0, 1);"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . '>' . $info['yescnt'] . '</a>';
- $html .= '<a class="rate_no" title="' . $lang_ajax_rating['Decline'] . '" href="javascript: void(0);" ' . ($active ? 'onclick="AR_rate(this, ' . (int)$postid . ', 0, -1);"' : 'onclick="alert(\'' . $lang_ajax_rating['Login to rate'] . '\');"') . '>' . $info['nocnt'] . '</a>';
- }
- $html .= '</div>';
- if (!is_array($opts)) $opts = array();
- $opts['rating'] = $html;
- }
- function AR_processRequest($action) {
- global $forum_db, $forum_config, $forum_user;
- if ($action == 'ajax_rating') {
- $postid = (int)$_POST['postid'];
- $type = (int)$_POST['type'];
- $rate = (float)$_POST['rate'];
- $content = '{postid:' . $postid . '}';
- $active = (boolean)(!$forum_user['is_guest'] || ($forum_user['is_guest'] && (int)$forum_config['p_ajax_rating_allow_guest']));
- if (!$postid || !$active) {
- header('X-json: "' . md5($content) . '"');
- echo $content;
- exit;
- }
- $to_cancel_vote = 0;
- if (!(int)$forum_config['p_ajax_rating_allow_multiple']) {
- $query = array(
- 'SELECT' => '*',
- 'FROM' => 'postrating_det',
- 'WHERE' => 'postid = ' . $postid . ' AND ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'',
- );
- $result = $forum_db->query_build($query);
- $to_cancel_vote = (int)$forum_db->num_rows($result);
- }
- if ($to_cancel_vote) {
- header('X-json: "' . md5($content) . '"');
- echo $content;
- exit;
- }
- $query = array(
- 'SELECT' => '*',
- 'FROM' => 'postrating',
- 'WHERE' => 'postid = ' . $postid,
- );
- $result = $forum_db->query_build($query);
- $to_update = (int)$forum_db->num_rows($result);
- $info = AR_loadRating($postid);
- if ($type == 0) {
- $newcnt = ((int)$info[($rate > 0 ? 'yescnt' : 'nocnt')] + 1);
- $newrate = (((int)$info['yescnt'] + ($rate > 0 ? 1 : 0)) * 100) / ((int)$info['cnt'] + 1);
- $newrate = (float)round($newrate);
- // Yes/No rate
- if ($to_update) {
- $forum_db->query('UPDATE ' . $forum_db->prefix . 'postrating
- SET ' . ($rate > 0 ? 'yescnt' : 'nocnt') . ' = ' . $newcnt . ',
- rating = ' . $newrate . ', cnt = (cnt + 1) WHERE postid = ' . $postid
- );
- } else {
- $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating
- (postid, cnt, ' . ($rate > 0 ? 'yescnt' : 'nocnt') . ', rating)
- VALUES (' . $postid . ', 1, ' . $newcnt . ', ' . $newrate . ')'
- );
- }
- $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
- (postid, ip, rating, tstamp) VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', ' . ($rate > 0 ? '1' : '0') . ', ' . mktime() . ')'
- );
- $info = AR_loadRating($postid);
- $content = '{postid:' . $postid . ', newvalue:' . $info[($rate > 0 ? 'yescnt' : 'nocnt')] . '}';
- } elseif ($type == 1) {
- $old_yescnt = ceil(((int)$info['cnt'] * (float)$info['rating']) / 100);
- $old_nocnt = (int)$info['cnt'] - $old_yescnt;
- $new_yescnt = ceil(5 * $rate);
- $new_nocnt = 5 - $new_yescnt;
- $newrate = (float)((($old_yescnt + $new_yescnt) * 100) / ((int)$info['cnt'] + 5));
- if ($to_update) {
- $forum_db->query('UPDATE ' . $forum_db->prefix . 'postrating
- SET yescnt = ' . ($old_yescnt + $new_yescnt) . ', nocnt = ' . ($old_nocnt + $new_nocnt) . ',
- rating = ' . $newrate . ', cnt = (cnt + 5) WHERE postid = ' . $postid
- );
- } else {
- $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating
- (postid, cnt, yescnt, nocnt, rating)
- VALUES (' . $postid . ', 5, ' . ($old_yescnt + $new_yescnt) . ', ' . ($old_nocnt + $new_nocnt) . ', ' . $newrate . ')'
- );
- }
- $posttime = mktime();
- for ($i = 0; $i < $new_yescnt; $i ++) {
- $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
- (postid, ip, rating, tstamp)
- VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', 1, ' . $posttime . ')'
- );
- }
- for ($i = 0; $i < $new_nocnt; $i ++) {
- $forum_db->query('INSERT INTO ' . $forum_db->prefix . 'postrating_det
- (postid, ip, rating, tstamp)
- VALUES (' . $postid . ', \'' . $_SERVER['REMOTE_ADDR'] . '\', 0, ' . $posttime . ')'
- );
- }
- $info = AR_loadRating($postid);
- $content = '{postid:' . $postid . ', newvalue:' . $info['rating'] . '}';
- }
- header('X-json: "' . md5($content) . '"');
- echo $content;
- exit;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement