We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50903
    • 6 Posts
    Hello,

    I have been working my own rating plugin so that I can store a custom rating against a user and then they can edit/adjust and see what the average rating among users is, It is almost there now the main issue I am having now is I cannot work out how to get an average rating of a certain resource. I think Migx is the answer and I have been using it for lots of other things but can't quite do this.

    I have created some custom tables (schema below) and Basically whenever a user rates a story it stores the story id and the user id. I can then get that information out no problem, but is there an easy way to get an average if the user has not yet rated?

    I was trying to use the below and then output to a Json placeholder but that wouldn't give me any values at all, and having all the separate values is difficult to explode into an array. Any help in the right direction would be much appreciated.

    Current Call for average:
    [[!migxLoopCollection? 
    &packageName=`rating`
    &classname=`Userr`
    &where=`{"storyr_id":"[[+id]]"}`
    &tpl=`avrating_snip`]]


    Schema:
    <?xml version="1.0" encoding="UTF-8"?>
    <model package="rating" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
        <object class="Userr" table="rating" extends="xPDOSimpleObject">
            <field key="userr_id" dbtype="varchar" precision="250" phptype="string" null="false" default="" />
    <field key="storyr_id" dbtype="varchar" precision="250" phptype="string" null="false" default="" />
    <field key="rating" dbtype="varchar" precision="350" phptype="string" null="false" default="" />
           
            
            </object>
        </model>

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      try a snippet like that:

      $packageName = 'rating';
      $classname = 'Userr';
      $where = $modx->getOption('where',$scriptProperties,'');
      $where = $modx->fromJson($where);
      
      $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
      $modelpath = $packagepath . 'model/';
      $modx->addPackage($packageName, $modelpath);
      
      $c = $modx->newQuery($classname);
      $c->select('AVG(rating) as avg');
      if (is_array($where)){
          $c->where($where);
      }
      $avg = 0;
      if ($object = $modx->getObject($classname,$c)){
          $avg = $object->get('avg');
      }
      return $avg;
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 50903
        • 6 Posts
        HI Bruno,

        Thanks again for helping this script was exactly what I needed!

        I have adjusted slightly and this is working for me which I thought I would post in case it's useful to anyone else but thought I would also ask if you can see any issues with this.

        <?php
        $packageName = 'rating';
        $classname = 'Userr';
        
         
        $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
        $modelpath = $packagepath . 'model/';
        $modx->addPackage($packageName, $modelpath);
         
        $c = $modx->newQuery($classname);
        $c->where(array('storyr_id' => $story_id));
        $c->select('AVG(rating) as avg');
        
        
        if ($object = $modx->getObject($classname,$c)){
            $avg = $object->get('avg');
        }
        
        $output = $modx->getChunk('avrating_template',array(
        'storyid' => $story_id,
          'rating' => round($avg),
        ));
        
        return $output;


        Quote from: Bruno17 at Dec 13, 2015, 09:32 AM
        try a snippet like that:

        $packageName = 'rating';
        $classname = 'Userr';
        $where = $modx->getOption('where',$scriptProperties,'');
        $where = $modx->fromJson($where);
        
        $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
        $modelpath = $packagepath . 'model/';
        $modx->addPackage($packageName, $modelpath);
        
        $c = $modx->newQuery($classname);
        $c->select('AVG(rating) as avg');
        if (is_array($where)){
            $c->where($where);
        }
        $avg = 0;
        if ($object = $modx->getObject($classname,$c)){
            $avg = $object->get('avg');
        }
        return $avg;