We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21255
    • 215 Posts
    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.
      • 10487 MODX Staff
      • 1,535 Posts
      Great snippet Timon! grin I’ll give it a blast later on.
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 2912
        • 315 Posts
        Great snippet and works a treat.

        I changed the way it rounded to match the displayed star rating.

        Awesome!
          BBloke
          • 25547
          • 61 Posts
          for me it only shows four of them?
            • 2912
            • 315 Posts
            What about the &limit parameter?
              BBloke
              • 25547
              • 61 Posts
              Quote from: BBloke at Apr 23, 2008, 01:04 PM

              What about the &limit parameter?

              I am using

              [!HighestRated? &limit=`10`!]

              but it only shows four

              Anyone??
                • 25547
                • 61 Posts
                Quote from: Ross1234 at Apr 23, 2008, 01:27 PM

                Quote from: BBloke at Apr 23, 2008, 01:04 PM

                What about the &limit parameter?

                I am using

                [!HighestRated? &limit=`10`!]

                but it only shows four

                Anyone??

                bump, Please help :’(
                  • 2912
                  • 315 Posts
                  Do you have a link to your page. Is there more than 4 rated documents?
                    BBloke
                    • 25547
                    • 61 Posts
                    yes,

                    http://game4free.co.uk/rated.html

                    and it gets the games from the all flash games section.

                      • 2912
                      • 315 Posts
                      I’ll be honest. I’m a little lost so here’s what I have:

                      Most Rated page snippet call:
                      [!HighestRated? &tvName=`MovieStarRating` &limit=`10` &chunk=`HighestRated`!]
                      


                      Most Rated chunk:
                      <div class="nl_summaryPost2" style="text-align:center;">
                        <h4><a href="{alias}" title="{description}">{longtitle}</a></h4>
                        <div class="nl_info" >{MovieImage}</div>
                      </div>
                      <div style="margin-left: auto;margin-right: auto;width: 10em;">[[star_rating? &docID=`{id}` &tvName=`MovieStarRating` &viewOnly=`true` &starTpl=`MovieStarRatingTPL_Star`]]</div>
                      <hr class="movie">
                      


                      MovieStarRatingTPL_Star chunk:
                      [+starRating+]
                      


                      Hope it helps.
                        BBloke