Ejemplo n.º 1
0
/**
 * Generates ratings display for a given item
 *
 * @param string $ext_name Module or plugin code
 * @param string $code Item identifier
 * @param string $cat Item category code (optional)
 * @param bool $readonly Display as read-only
 * @return array Rendered HTML output for ratings and average integer value as an array with 2 elements
 * @global CotDB $db
 */
function cot_ratings_display($ext_name, $code, $cat = '', $readonly = false)
{
    global $db, $db_ratings, $db_rated, $db_users, $cfg, $usr, $sys, $L, $R;
    // Check permissions
    list($auth_read, $auth_write, $auth_admin) = cot_auth('plug', 'ratings');
    $enabled = cot_ratings_enabled($ext_name, $cat, $code);
    if (!$auth_read || !$enabled && !$auth_admin) {
        return array('', 0);
    }
    // Get current rating value
    $sql = $db->query("SELECT r.*, (SELECT COUNT(*) FROM {$db_rated} WHERE rated_area = ? AND rated_code = ?) AS `cnt` FROM {$db_ratings} AS r\n\t\tWHERE rating_area = ? AND rating_code = ? LIMIT 1", array($ext_name, $code, $ext_name, $code));
    if ($row = $sql->fetch()) {
        $rating_average = $row['rating_average'];
        $item_has_rating = true;
        if ($rating_average < 1) {
            $rating_average = 1;
        } elseif ($rating_average > 10) {
            $rating_average = 10;
        }
        $rating_cntround = round($rating_average, 0);
        $rating_raters_count = $row['cnt'];
    } else {
        $item_has_rating = false;
        $rating_average = 0;
        $rating_cntround = 0;
        $rating_raters_count = 0;
    }
    // Render read-only image
    $rating_fancy = cot_rc('icon_rating_stars', array('val' => $rating_cntround));
    if (!$auth_write || $readonly) {
        return array($rating_fancy, $rating_cntround, $rating_raters_count);
    }
    // Check if the user has voted already for this item
    $already_voted = false;
    if ($usr['id'] > 0) {
        $sql1 = $db->query("SELECT rated_value FROM {$db_rated}\n\t\t\tWHERE rated_area = ? AND rated_code = ? AND rated_userid = ?", array($ext_name, $code, $usr['id']));
        if ($rated_value = $sql1->fetchColumn()) {
            $already_voted = true;
            $rating_uservote = $L['rat_alreadyvoted'] . ' (' . $rated_value . ')';
        }
    }
    if ($already_voted && !$cfg['plugin']['ratings']['ratings_allowchange']) {
        return array($rating_fancy, $rating_cntround, $rating_raters_count);
    }
    $t = new XTemplate(cot_tplfile('ratings', 'plug'));
    /* == Hook for the plugins == */
    foreach (cot_getextplugins('ratings.main') as $pl) {
        include $pl;
    }
    /* ===== */
    // Get some extra information about votes
    if ($item_has_rating) {
        $sql = $db->query("SELECT COUNT(*) FROM {$db_rated}\n\t\t\tWHERE rated_area = ? AND rated_code = ?", array($ext_name, $code));
        $rating_voters = $sql->fetchColumn();
        $rating_since = $L['rat_since'] . ' ' . cot_date('datetime_medium', $row['rating_creationdate']);
        $rating_since_stamp = $row['rating_creationdate'];
        $rating_averageimg = cot_rc('icon_rating_stars', array('val' => $rating_cntround));
    } else {
        $rating_voters = 0;
        $rating_since = '';
        $rating_since_stamp = '';
        $rating_averageimg = '';
    }
    // Assign tags
    $t->assign(array('RATINGS_CODE' => $code, 'RATINGS_AVERAGE' => round($rating_average), 'RATINGS_AVERAGEIMG' => $rating_averageimg, 'RATINGS_VOTERS' => $rating_voters, 'RATINGS_SINCE' => $rating_since, 'RATINGS_SINCE_STAMP' => $rating_since_stamp, 'RATINGS_FANCYIMG' => $rating_fancy, 'RATINGS_USERVOTE' => $rating_uservote));
    /* == Hook for the plugins == */
    foreach (cot_getextplugins('ratings.tags') as $pl) {
        include $pl;
    }
    /* ===== */
    // Render voting form
    $vote_block = $auth_write && (!$already_voted || $cfg['plugin']['ratings']['ratings_allowchange']) ? 'NOTVOTED.' : 'VOTED.';
    for ($i = 1; $i <= 10; $i++) {
        $checked = $i <= $rating_cntround ? 'checked="checked"' : '';
        $t->assign(array('RATINGS_ROW_VALUE' => $i, 'RATINGS_ROW_TITLE' => $L['rat_choice' . $i], 'RATINGS_ROW_CHECKED' => $checked));
        $t->parse('RATINGS.' . $vote_block . 'RATINGS_ROW');
    }
    if ($vote_block == 'NOTVOTED.') {
        // 'r=ratings&area=' . $ext_name . '&code=' . $code.'&inr=send'
        $t->assign('RATINGS_FORM_SEND', cot_url('plug', array('r' => 'ratings', 'inr' => 'send', 'area' => $ext_name, 'code' => $code, 'cat' => $cat)));
        $t->parse('RATINGS.NOTVOTED');
    } else {
        $t->parse('RATINGS.VOTED');
    }
    // Parse and return
    $t->parse('RATINGS');
    $res = $t->text('RATINGS');
    return array($res, round($rating_cntround), $rating_raters_count);
}
Ejemplo n.º 2
0
/**
 * AJAX handler for star ratings
 *
 * @package Ratings
 * @copyright (c) Cotonti Team
 * @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
 */
defined('COT_CODE') or die('Wrong URL');
require_once cot_incfile('ratings', 'plug');
$area = cot_import('area', 'G', 'ALP');
$code = cot_import('code', 'G', 'ALP');
$cat = cot_import('cat', 'G', 'TXT');
$inr = cot_import('inr', 'G', 'ALP');
$newrate = cot_import('rate_' . $code, 'P', 'INT');
$newrate = !empty($newrate) ? $newrate : 0;
$enabled = cot_ratings_enabled($area, $cat, $code);
list($auth_read, $auth_write, $auth_admin) = cot_auth('plug', 'ratings');
if ($inr == 'send' && $newrate >= 0 && $newrate <= 10 && $auth_write && $enabled) {
    // Get current item rating
    $sql = $db->query("SELECT * FROM {$db_ratings}\n\t\tWHERE rating_area = ? AND rating_code = ? LIMIT 1", array($area, $code));
    if ($row = $sql->fetch()) {
        $rating_average = $row['rating_average'];
        $item_has_rating = true;
        if ($rating_average < 1) {
            $rating_average = 1;
        } elseif ($rating_average > 10) {
            $rating_average = 10;
        }
        $rating_cntround = round($rating_average, 0);
    } else {
        $item_has_rating = false;