source: trunk/include/functions_rate.inc.php @ 29636

Last change on this file since 29636 was 28587, checked in by mistic100, 10 years ago

feature 3010 : replace trigger_action/event by trigger_notify/change

  • Property svn:eol-style set to LF
File size: 6.0 KB
RevLine 
[1084]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1084]23
24/**
[25614]25 * @package functions\rate
26 */
27
28
29/**
30 * Rate a picture by the current user.
[1084]31 *
[25614]32 * @param int $image_id
33 * @param float $rate
34 * @return array as return by update_rating_score()
[1084]35 */
[1092]36function rate_picture($image_id, $rate)
[1084]37{
[1092]38  global $conf, $user;
[1084]39
[1092]40  if (!isset($rate)
41      or !$conf['rate']
42      or !in_array($rate, $conf['rate_items']))
[1084]43  {
[2435]44    return false;
[1084]45  }
[1092]46
47  $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true;
48
49  if ($user_anonymous and !$conf['rate_anonymous'])
[1084]50  {
[2435]51    return false;
[1084]52  }
[1092]53
[2100]54  $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]);
55  if (count($ip_components) > 3)
56  {
57    array_pop($ip_components);
58  }
59  $anonymous_id = implode ('.', $ip_components);
60
[1092]61  if ($user_anonymous)
[1084]62  {
[1992]63    $save_anonymous_id = pwg_get_cookie_var('anonymous_rater', $anonymous_id);
64
65    if ($anonymous_id != $save_anonymous_id)
66    { // client has changed his IP adress or he's trying to fool us
67      $query = '
[1084]68SELECT element_id
[2100]69  FROM '.RATE_TABLE.'
70  WHERE user_id = '.$user['id'].'
71    AND anonymous_id = \''.$anonymous_id.'\'
[1084]72;';
[1992]73      $already_there = array_from_query($query, 'element_id');
[1092]74
[1992]75      if (count($already_there) > 0)
76      {
77        $query = '
[1084]78DELETE
[2100]79  FROM '.RATE_TABLE.'
80  WHERE user_id = '.$user['id'].'
81    AND anonymous_id = \''.$save_anonymous_id.'\'
82    AND element_id IN ('.implode(',', $already_there).')
[1084]83;';
[1992]84         pwg_query($query);
85       }
[1084]86
[1992]87       $query = '
[2100]88UPDATE '.RATE_TABLE.'
89  SET anonymous_id = \'' .$anonymous_id.'\'
90  WHERE user_id = '.$user['id'].'
91    AND anonymous_id = \'' . $save_anonymous_id.'\'
[1084]92;';
[1992]93       pwg_query($query);
94    } // end client changed ip
[1084]95
[2100]96    pwg_set_cookie_var('anonymous_rater', $anonymous_id);
[1092]97  } // end anonymous user
[1992]98
[1092]99  $query = '
[1084]100DELETE
101  FROM '.RATE_TABLE.'
102  WHERE element_id = '.$image_id.'
[2100]103    AND user_id = '.$user['id'].'
[1084]104';
[2471]105  if ($user_anonymous)
[1092]106  {
107    $query.= ' AND anonymous_id = \''.$anonymous_id.'\'';
108  }
109  pwg_query($query);
110  $query = '
[1084]111INSERT
112  INTO '.RATE_TABLE.'
113  (user_id,anonymous_id,element_id,rate,date)
114  VALUES
115  ('
[1092]116    .$user['id'].','
[2100]117    .'\''.$anonymous_id.'\','
[1092]118    .$image_id.','
119    .$rate
120    .',NOW())
[1084]121;';
[1092]122  pwg_query($query);
123
[11827]124  return update_rating_score($image_id);
125}
126
127
[25614]128/**
129 * Update images.rating_score field.
130 * We use a bayesian average (http://en.wikipedia.org/wiki/Bayesian_average) with
131 *  C = average number of rates per item
132 *  m = global average rate (all rates)
133 *
134 * @param int|false $element_id if false applies to all
135 * @return array (score, average, count) values are null if $element_id is false
[11827]136*/
137function update_rating_score($element_id = false)
138{
[28587]139  if ( ($alt_result = trigger_change('update_rating_score', false, $element_id)) !== false)
[27294]140  {
141    return $alt_result;
142  }
143
[1092]144  $query = '
[11827]145SELECT element_id,
146    COUNT(rate) AS rcount,
147    SUM(rate) AS rsum
[1084]148  FROM '.RATE_TABLE.'
[11827]149  GROUP by element_id';
150
151  $all_rates_count = 0;
152  $all_rates_avg = 0;
153  $item_ratecount_avg = 0;
154  $by_item = array();
155
156  $result = pwg_query($query);
157  while ($row = pwg_db_fetch_assoc($result))
158  {
159    $all_rates_count += $row['rcount'];
160    $all_rates_avg += $row['rsum'];
161    $by_item[$row['element_id']] = $row;
162  }
163
[12412]164  if ($all_rates_count>0)
165  {
166    $all_rates_avg /= $all_rates_count;
167    $item_ratecount_avg = $all_rates_count / count($by_item);
168  }
[11827]169
170  $updates = array();
171  foreach ($by_item as $id => $rate_summary )
172  {
173    $score = ( $item_ratecount_avg * $all_rates_avg + $rate_summary['rsum'] ) / ($item_ratecount_avg + $rate_summary['rcount']);
174    $score = round($score,2);
175    if ($id==$element_id)
176    {
177      $return = array(
178        'score' => $score,
179        'average' => round($rate_summary['rsum'] / $rate_summary['rcount'], 2),
180        'count' => $rate_summary['rcount'],
181        );
182    }
[11893]183    $updates[] = array( 'id'=>$id, 'rating_score'=>$score );
[11827]184  }
185  mass_updates(
186    IMAGES_TABLE,
187    array(
188      'primary' => array('id'),
[11893]189      'update' => array('rating_score')
[11827]190      ),
191    $updates
192    );
193
194  //set to null all items with no rate
195  if ( !isset($by_item[$element_id]) )
196  {
197    $query='
198SELECT id FROM '.IMAGES_TABLE .'
199  LEFT JOIN '.RATE_TABLE.' ON id=element_id
[11893]200  WHERE element_id IS NULL AND rating_score IS NOT NULL';
[11827]201
202    $to_update = array_from_query( $query, 'id');
203
204    if ( !empty($to_update) )
205    {
206      $query='
207UPDATE '.IMAGES_TABLE .'
[11893]208  SET rating_score=NULL
[11827]209  WHERE id IN (' . implode(',',$to_update) . ')';
210    pwg_query($query);
211    }
212  }
213
214  return isset($return) ? $return : array('score'=>null, 'average'=>null, 'count'=>0 );
[1084]215}
216
[2100]217?>
Note: See TracBrowser for help on using the repository browser.