Here’s one that is intended for displaying documents ordered by their star rating. To use it, you need to have
CSS Star Rating installed.
Sample Chunk: HighestRated
Document '{pagetitle}' is on rank {rank} rated with {stars} stars.<br />
Note: You *can* as well use Template Variables in your Chunk. Just write them in curly braces.
Snippet code
<?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;
?>
Since this snippet isn’t really fast you should better use it from chache only.