Changeset 11827 for trunk/include


Ignore:
Timestamp:
Jul 25, 2011, 8:04:50 PM (13 years ago)
Author:
rvelices
Message:

feature 2384: improve average rating calculation (still need to update language files)

Location:
trunk/include
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_category.inc.php

    r11512 r11827  
    291291    array(
    292292    array(l10n('Default'), '', true),
    293     array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
     293    array(l10n('Rating score'), 'average_rate DESC', $conf['rate']),
    294294    array(l10n('Most visited'), 'hit DESC', true),
    295295    array(l10n('Creation date'), 'date_creation DESC', true),
  • trunk/include/functions_rate.inc.php

    r8728 r11827  
    117117  pwg_query($query);
    118118
    119   // update of images.average_rate field
     119  return update_rating_score($image_id);
     120}
     121
     122
     123/* update images.average_rate field
     124  * we use a bayesian average (http://en.wikipedia.org/wiki/Bayesian_average) with
     125C = average number of rates per item
     126m = global average rate (all rates)
     127
     128 * param int $element_id optional, otherwise applies to all
     129 * @return array(average_rate, count) if element_id is specified
     130*/
     131function update_rating_score($element_id = false)
     132{
    120133  $query = '
    121 SELECT COUNT(rate) AS count
    122      , ROUND(AVG(rate),2) AS average
    123   FROM '.RATE_TABLE.'
    124   WHERE element_id = '.$image_id.'
    125 ;';
    126   $row = pwg_db_fetch_assoc(pwg_query($query));
    127   $query = '
    128 UPDATE '.IMAGES_TABLE.'
    129   SET average_rate = '.$row['average'].'
    130   WHERE id = '.$image_id.'
    131 ;';
    132   pwg_query($query);
    133   return $row;
     134SELECT element_id,
     135    COUNT(rate) AS rcount,
     136    SUM(rate) AS rsum
     137  FROM '.RATE_TABLE.'
     138  GROUP by element_id';
     139
     140  $all_rates_count = 0;
     141  $all_rates_avg = 0;
     142  $item_ratecount_avg = 0;
     143  $by_item = array();
     144
     145  $result = pwg_query($query);
     146  while ($row = pwg_db_fetch_assoc($result))
     147  {
     148    $all_rates_count += $row['rcount'];
     149    $all_rates_avg += $row['rsum'];
     150    $by_item[$row['element_id']] = $row;
     151  }
     152
     153  $all_rates_avg /= $all_rates_count;
     154  $item_ratecount_avg = $all_rates_count / count($by_item);
     155
     156  $updates = array();
     157  foreach ($by_item as $id => $rate_summary )
     158  {
     159    $score = ( $item_ratecount_avg * $all_rates_avg + $rate_summary['rsum'] ) / ($item_ratecount_avg + $rate_summary['rcount']);
     160    $score = round($score,2);
     161    if ($id==$element_id)
     162    {
     163      $return = array(
     164        'score' => $score,
     165        'average' => round($rate_summary['rsum'] / $rate_summary['rcount'], 2),
     166        'count' => $rate_summary['rcount'],
     167        );
     168    }
     169    $updates[] = array( 'id'=>$id, 'average_rate'=>$score );
     170  }
     171  mass_updates(
     172    IMAGES_TABLE,
     173    array(
     174      'primary' => array('id'),
     175      'update' => array('average_rate')
     176      ),
     177    $updates
     178    );
     179
     180  //set to null all items with no rate
     181  if ( !isset($by_item[$element_id]) )
     182  {
     183    $query='
     184SELECT id FROM '.IMAGES_TABLE .'
     185  LEFT JOIN '.RATE_TABLE.' ON id=element_id
     186  WHERE element_id IS NULL AND average_rate IS NOT NULL';
     187
     188    $to_update = array_from_query( $query, 'id');
     189
     190    if ( !empty($to_update) )
     191    {
     192      $query='
     193UPDATE '.IMAGES_TABLE .'
     194  SET average_rate=NULL
     195  WHERE id IN (' . implode(',',$to_update) . ')';
     196    pwg_query($query);
     197    }
     198  }
     199
     200  return isset($return) ? $return : array('score'=>null, 'average'=>null, 'count'=>0 );
    134201}
    135202
  • trunk/include/picture_rate.inc.php

    r8728 r11827  
    2929if ($conf['rate'])
    3030{
    31   if ( NULL != $picture['current']['average_rate'] )
     31  $rate_summary = array( 'count'=>0, 'score'=>$picture['current']['average_rate'], 'average'=>null );
     32  if ( NULL != $rate_summary['score'] )
    3233  {
    3334    $query = '
     
    3738  WHERE element_id = '.$picture['current']['id'].'
    3839;';
    39     $row = pwg_db_fetch_assoc(pwg_query($query));
     40                list($rate_summary['count'], $rate_summary['average']) = pwg_db_fetch_row(pwg_query($query));
    4041  }
    41   else
    42   { // avg rate null -> no rate -> no need to query db
    43     $row = array( 'count'=>0, 'average'=>NULL );
    44   }
    45   $template->assign('rate_summary', $row);
     42  $template->assign('rate_summary', $rate_summary);
    4643
    4744  $user_rate = null;
    4845  if ($conf['rate_anonymous'] or is_autorize_status(ACCESS_CLASSIC) )
    4946  {
    50     if ($row['count']>0)
     47    if ($rate_summary['count']>0)
    5148    {
    5249      $query = 'SELECT rate
  • trunk/include/ws_functions.inc.php

    r11756 r11827  
    782782  }
    783783  //------------------------------------------------------------- related rates
    784   $query = '
     784        $rating = array('score'=>$image_row['average_rate'], 'count'=>0, 'average'=>null);
     785        if (isset($rating['score']))
     786        {
     787                $query = '
    785788SELECT COUNT(rate) AS count
    786789     , ROUND(AVG(rate),2) AS average
     
    788791  WHERE element_id = '.$image_row['id'].'
    789792;';
    790   $rating = pwg_db_fetch_assoc(pwg_query($query));
    791   $rating['count'] = (int)$rating['count'];
     793                $row = pwg_db_fetch_assoc(pwg_query($query));
     794                $rating['score'] = (float)$rating['score'];
     795                $rating['average'] = (float)$row['average'];
     796                $rating['count'] = (int)$row['count'];
     797        }
    792798
    793799  //---------------------------------------------------------- related comments
Note: See TracChangeset for help on using the changeset viewer.