Как вывести в TV среднее значение Star Rating?

Для сортировки по рейтингу нужно вывести среднее значение оценки в [[*middlerate]] . Кто знает, есть ли такая функция или что (и куда) дописать в сниппет?

На данный момент не понятно: что у вас за рейтинг, что за мифический сниппет… Я бы внес чуть больше конкретики, а то телепаты в отпуске

Стандартный Star Rating, он единственный в установщике Revo. http://modx.com/extras/package/starrating#__utmzi__1__=1

Это его файл, куда нужно добавить вывод средней оценки в TV <php

/**

  • The Star Rating class

  • @package star_rating

*/

class starRating extends xPDOSimpleObject {

public $vote = null;

public $startDate = null;

public $endDate = null;

public $config = array();

public $output = '';

/**

 * Initialize default config settings

 */

public function initialize() {

    $this->config = array(

        'useSession' => false,

        'sessionName' => 'starrating',

        'useCookie' => false,

        'cookieName' => 'starrating',

        'cookieExpiry' => 608400,

        'theme' => 'default',

        'maxStars' => 5,

        'imgWidth' => 25,

        'cssFile' => 'star',

        'urlPrefix' => '',

        'starTpl' => 'starTpl',

    );

}

/**

 * Setup custom config variables

 *

 * @param array $array An array of configuration properties

 */

public function setConfig($config = array()) {

    $this->config = array_merge($this->config,$config);

}

/**

 * Render the star rating

 */

public function renderVote() {

    if (($this->startDate == null || date('Y-m-d H:i:s',time()) > $this->startDate) && ($this->endDate == null || date('Y-m-d H:i:s', time()) < $this->endDate)) {

        $voteAllowed = $this->allowVote();

        $voteStats = $this->getVoteStats();

        /* TODO: replace with lexicon */

        $currentText = round($voteStats['average'].'/'.$this->config['maxStars'], 2);

        $listItems = '';

        for($i = 0; $i config['maxStars']; $i++) {

            if ($i == 0) {

                $listItems .= ''.$currentText.'

';

            } else {

                $starWidth = floor(100 / $this->config['maxStars'] * $i);

                $starIndex = ($this->config['maxStars'] - $i) + 2;

                if ($voteAllowed) {

                    $prefix = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

                    if (!empty($this->config['urlPrefix'])) {

                        $prefix = $this->config['urlPrefix'].'&'.$prefix;

                    }

                    $urlParams = $prefix . '&vote='.$i.'&star_id=' . $this->get('star_id');

                    $listItems .= ''. $i . '

';

                } else {

                    $listItems .= ''. $i .'

';

                }

            }

        }

        $listItems .= '';

    }

    $ph['rating'] = $listItems;

    $ph = array_merge($ph, $voteStats);

    $chunk = $this->xpdo->getObject('modChunk',array(

        'name' => $this->config['starTpl'],

    ));

    if ($chunk) {

        $this->output = $chunk->getContent();

    } else {

        $this->output = '[[+rating]]Votes: [[+vote_count]]';

    }

    foreach ($ph as $key => $value) {

        $this->output = str_replace('[[+' . $key . ']]', $value, $this->output);

    }

    return $this->output;

}

/**

 * Returns an array of the current vote stats

 *

 * @return array

 */

public function getVoteStats() {

    $row = $this->toArray();

    $row['average'] = ($row['vote_count'] != 0) ? $row['vote_total'] / $row['vote_count'] : 0;

    $row['percentage'] = floor(($row['average']/$this->config['maxStars'])*100);

    return $row;

}

/**

 * Add vote if necessary permission and validation checks are okay

 *

 * @return boolean

 */

public function addVote() {

    $added = false;

    if (is_numeric($this->vote) && $this->allowVote() && $this->validateVote()) {

        $vt = $this->get('vote_total') + $this->vote;

        $vc = $this->get('vote_count') + 1;

        $this->set('vote_total', $vt);

        $this->set('vote_count', $vc);

        if ($this->save()) {

            $this->setVoteRestrictions();

            $added = true;

        }

    }

    return false;

}

/**

 * Set the vote to a certain value

 *

 * @param integer $vote

 */

public function setVote($vote) {

    $this->vote = $vote;

}

/**

 * Check vote is within limits of the star rating

 *

 * @return boolean

 */

public function validateVote() {

    if (!empty($this->vote) && intval($this->vote) > 0 && intval($this->vote) config['maxStars']) {

        return true;

    }

    return false;

}

/**

 * Check that the vote is allowed (cookie, session, user permissions)

 *

 * @return boolean

 */

public function allowVote() {

    $uniqueId = ($this->get('group_id') != '' ? $this->get('group_id').'-' : '') . $this->get('star_id');

    /* session */

    if ($this->config['useSession'] == true

        && array_key_exists($this->config['sessionName'],$_SESSION)) {

        $sessionArray = $_SESSION[$this->config['sessionName']];

        if (!empty($sessionArray) && in_array($uniqueId, $sessionArray)) {

            return false;

        }

    }

    /* cookie */

    if ($this->config['useCookie'] == true) {

        if (isset ($_COOKIE[$this->config['cookieName'] . $uniqueId])) {

            return false;

        }

    }

    return true;

}

/**

 * Prevent user from voting twice

 *

 * @return boolean

 */

public function setVoteRestrictions() {

    $uniqueId = ($this->get('group_id') != '' ? $this->get('group_id').'-' : '') . $this->get('star_id');

    /* session */

    if ($this->config['useSession'] == true) {

        if (!array_key_exists($this->config['sessionName'], $_SESSION)) $_SESSION[$this->config['sessionName']] = array();

        if (!in_array($uniqueId, $_SESSION[$this->config['sessionName']])) {

            array_push($_SESSION[$this->config['sessionName']], $uniqueId);

        }

    }

    /* cookie */

    if ($this->config['useCookie'] == true) {

        setcookie($this->config['cookieName'] . $uniqueId, 'novote', time() + $this->config['cookieExpiry']);

    }

    return true;

}

/**

 * Load custom theme

 *

 * @return boolean

 */

public function loadTheme() {

    $loaded = false;

    $f = $this->xpdo->getOption('base_path').'assets/components/star_rating/themes/'.$this->config['theme'].'/'.$this->config['cssFile'].'.css';

    if (file_exists($f)) {

        $url = $this->xpdo->getOption('base_url') . 'assets/components/star_rating/themes/' . $this->config['theme'] . '/' . $this->config['cssFile'] . '.css';

        $this->xpdo->regClientCSS($url);

        $loaded = true;

    }

    return $loaded;

}

public function getRedirectUrl() {

    $qs = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

    $qsCleansed = array();

    if (strlen($qs) > 0) {

        $qsParts = explode('&', $qs);

        foreach ($qsParts as $key => $part) {

            $param = explode('=', $part);

            if (!in_array($param[0], array('vote', 'star_id', 'group_id'))) {

                $qsCleansed[] = $param[0] . '=' . $param[1];

            }

        }

    }

    return $this->xpdo->makeUrl($this->xpdo->resource->get('id'), '', implode('&', $qsCleansed));

}

}

В прошлой версии мне помогли сделать это так: http://community.modx-cms.ru/blog/solutions/7924.html После обновления, точнее этот рейтинг другого издателя, такой трюк не проходит. Помогите, плиз, дописать корректно код!

Стандартный Star Rating, он единственный в установщике Revo. Вообще-то вы имеете ввиду CSS Star Rating. А есть еще starRating, и по запросу Star Rating они выдаются оба. http://joxi.ru/KAgK9pXIEMVkAl Поэтому указать точное наименование модуля никогда не лишнее. Ваш топик вообще лоуконтекстный, просьба больше такие не писать (а то что-то зачастили). Уже который раз говорится, что здесь нет телепатов. Хотите чтобы вам помогли (то есть потратили свои калории на помощь вам) - потрудитесь и вы оформить топик как положено, описав четче задачу, предоставив листинги как и что у вас сделано и где что вообще требуется. А то скоро просто в игнор отправлять будем. По вашей теме: особо копать не буду. Приведу только xPDO-запрос, который позволит отсортировать как хочется. А вы уже смотрите куда и как его применить. <php

$snippetPath = $modx->getOption('core_path').'components/star_rating/';

$modx->addPackage('star_rating',$snippetPath.'model/');

$q = $modx->newQuery('starRating');

$q->select(array(

"starRating.*",

"starRating.vote_total/starRating.vote_count as average",

));

$q->sortby("average", "DESC");

foreach($modx->getCollection('starRating', $q) as $o){

print_r($o->toArray());

}

Спасибо, что отозвались. Не знаю, чем я вас так разозлила, я абсолютно точно написала название (второе по-другому пишется) и сформулировала задачу. Я не знаю куда именно вставлять этот код и что он делает (в PHP полнейший ноль), на этом сайте, я так понимаю, таких вопросов нельзя задавать? Вы бы где-то указали, что сайт только для профи, новичкам здесь делать нечего. Тогда бы и не тратил никто ни своего, ни вашего времени.

Если здесь все такие умные, готова заплатить, кто может помочь это сделать. Очень нужно. (artnet6 sobaka yandex.ru)

Не стоит обижаться. Рабочее решение я вам написал. Но согласен, интеграция этого может потребовать скилов. Направлю к вам человечка, если никто вам вперед не сделает, то завтра вечером он вам поможет.

Не за что. Не знаю, чем я вас так разозлила Нет, еще не разозлили. я абсолютно точно написала название Извините, мы программисты, "Star Rating" != "CSS Star Rating". Отличается на 4 символа (пробел - тоже символ). я так понимаю, таких вопросов нельзя задавать? Вы бы где-то указали, что сайт только для профи, новичкам здесь делать нечего. Тогда бы и не тратил никто ни своего, ни вашего времени. Можно. Но как раз на этот счет очередное обновление на сайте.

Буду благодарна.

Здравствуйте, помогите записать произвольное в TV "$row['percentage']"