Reconfiguring a StarRating snippet - php help needed!

I’m looking to reconfigure a StarRating snippet to suit a website redesign which I’m currently embarking on.

The snippet displays a list on my home page of the top 10 best rated circuits on RacingCircuits.info. However, I’ve spotted a couple of issues:

  1. It’s currently configured to count results only from resources using template 17, when it should really show from resources which use templates 13 or 17;
  2. I’d like to additionally like to set placeholder values for the pagetitle and a further template variable (named circuit_countrycode) of the parent resource.

I’m not well versed in php and didn’t write the original, so would appreciate any help!

Current snippet code below:

<?php
    $showhidden = isset($showhidden) ? $showhidden : 0;
    $limit = isset($limit) ? $limit : 10;

    $snippetPath = $modx->getOption('core_path').'components/star_rating/';
    $modx->addPackage('star_rating',$snippetPath.'model/');
    $output='';
    //$manager = $modx->getManager();
    //$manager->createObjectContainer('starRating');

    $c = $modx->newQuery('modResource');
    $c->leftJoin('starRating','starRating', array("modResource.id = starRating.star_id"));


    $c->select('modResource.id, vote_total/vote_count AS rating');
    $where = array(
         'modResource.published' => '1',
         'modResource.template' => '17',
         'modResource.hidemenu' => $showhidden
        );
    if (isset($parent)) {
     $where['modResource.parent']=$parent;
    }
    if (isset($template)) {
     $where['modResource.template']=$template;
    }
    $c->where($where);
    $c->sortby('rating','DESC');
    $c->sortby('id','DESC');
    $c->limit($limit,0);
    //$c->prepare();echo $c->toSQL();
    $ratings = $modx->getCollection('modResource', $c);
    $idx=1;
    foreach ($ratings as $rating) {
    	$modx->setPlaceholder('pagetitle',$rating->get('pagetitle'));
    	$modx->setPlaceholder('id',$rating->get('id'));
    	$modx->setPlaceholder('idx',$idx);
    	$output.=$modx->getChunk('tplHPCircuitRating');
    	$idx++;
    }
    return $output;

I hope you don’t mind – I added three back-ticks above and below your code to preserve the format.

For starters, back up the snippet, then see if this works to get resources with the other template:

Replace:

'modResource.template' => '17',

with:

'modResource.template:IN' => array('13','17'),
1 Like

Add these lines to the foreach loop to load the parent resource and read the TV.

//existing foreach loop
foreach ($ratings as $rating) {
    //new code
    $parent = $modx->getObject('modResource', $rating->get('parent'));
    if ($parent) {
        $countrycode = $parent->getTVValue('circuit_countrycode');
        $modx->setPlaceholder('circuit_countrycode',$countrycode);
        $modx->setPlaceholder('parent_pagetitle',$parent->get('pagetitle'));
    }
    ...
}

Many thanks - I was wondering how to do that. Has been a while since I posted on the Modx forums and the first since the new format launched!

Thanks Bob - that does indeed work.

Brilliant - that works a treat. Thanks very much for your help!