Hi, I’m using the CSS Star Rating Snippet 2.0-rc1 with Revo 2.04-pl2 and I’m trying to list the top 3 rated documents for my site. I came across the following snippet which looks pretty old and doesn’t work with Revo 2 as it is. Would it be a big job to modify it for Revo 2 or is there a simpler way I can list the top rated docs perhaps with getResources? Not being a PHP developer by any means, I have no idea what would be required. Any suggestions much appreciated.
<?php
/**
* @name HighestRated
* @author Timon Reinhard <[email protected]>
* @copyright Copyright (c) 2006 Netnoise Media GbR
* @license GNU General Public License 2.0
* @version 0.1
*
* Lists documents ordered by their star_rating.
* Depends on Garry Nutting's star_rating snippet.
*
* Usage
* @param string $tvName which star_rating uses to store it's data
* @param string $chunk to render each result into
* @param integer $limit top n results
*/
isset($tvName) or $tvName = 'star_rating';
isset($chunk) or $chunk = 'HighestRated';
$html = '';
$pages = array();
/* Collect all rated documents */
$tmplvarid = (int)$modx->db->getValue($modx->db->select('id', $modx->getFullTableName('site_tmplvars'), "name='{$tvName}'"));
$result = $modx->db->select('contentid, value', $modx->getFullTableName('site_tmplvar_contentvalues'), "tmplvarid={$tmplvarid}");
while($row = $modx->db->getRow($result)) {
/* Get score value */
list($rValue, $rCount) = explode(':', $row['value']);
if(!(int)$rCount>0) break;
$score = round(($rValue/$rCount),2) * 100;
/* Get page info and rendered TVar output of scored document */
if(($tvars = $modx->getTemplateVars('*', 'name', $row['contentid'], 1)) && is_array($tvars)) {
foreach($tvars as $tvar) {
$tvnames[] = $tvar['name'];
}
$pages[$score] = $modx->getTemplateVarOutput($tvnames, $row['contentid']);
unset($tvnames);
}
}
/* Display sorted results */
krsort($pages, SORT_NUMERIC);
(isset($limit) && ($limit < count($pages))) or $limit = count($pages);
for ($i = 1; $i <= $limit; $i++) {
list($score, $page) = each($pages);
$page['rank'] = $i;
$page['stars'] = intval($score/100);
$html.= $modx->parseChunk($chunk, $page);
}
return $html;
?>
- cheers