[1084] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 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 | /** |
---|
| 25 | * rate a picture by a user |
---|
| 26 | * |
---|
| 27 | * @param int image identifier |
---|
| 28 | * @param int rate |
---|
| 29 | * @return void |
---|
| 30 | */ |
---|
[1092] | 31 | function rate_picture($image_id, $rate) |
---|
[1084] | 32 | { |
---|
[1092] | 33 | global $conf, $user; |
---|
[1084] | 34 | |
---|
[1092] | 35 | if (!isset($rate) |
---|
| 36 | or !$conf['rate'] |
---|
| 37 | or !in_array($rate, $conf['rate_items'])) |
---|
[1084] | 38 | { |
---|
[2435] | 39 | return false; |
---|
[1084] | 40 | } |
---|
[1092] | 41 | |
---|
| 42 | $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true; |
---|
| 43 | |
---|
| 44 | if ($user_anonymous and !$conf['rate_anonymous']) |
---|
[1084] | 45 | { |
---|
[2435] | 46 | return false; |
---|
[1084] | 47 | } |
---|
[1092] | 48 | |
---|
[2100] | 49 | $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]); |
---|
| 50 | if (count($ip_components) > 3) |
---|
| 51 | { |
---|
| 52 | array_pop($ip_components); |
---|
| 53 | } |
---|
| 54 | $anonymous_id = implode ('.', $ip_components); |
---|
| 55 | |
---|
[1092] | 56 | if ($user_anonymous) |
---|
[1084] | 57 | { |
---|
[1992] | 58 | $save_anonymous_id = pwg_get_cookie_var('anonymous_rater', $anonymous_id); |
---|
| 59 | |
---|
| 60 | if ($anonymous_id != $save_anonymous_id) |
---|
| 61 | { // client has changed his IP adress or he's trying to fool us |
---|
| 62 | $query = ' |
---|
[1084] | 63 | SELECT element_id |
---|
[2100] | 64 | FROM '.RATE_TABLE.' |
---|
| 65 | WHERE user_id = '.$user['id'].' |
---|
| 66 | AND anonymous_id = \''.$anonymous_id.'\' |
---|
[1084] | 67 | ;'; |
---|
[1992] | 68 | $already_there = array_from_query($query, 'element_id'); |
---|
[1092] | 69 | |
---|
[1992] | 70 | if (count($already_there) > 0) |
---|
| 71 | { |
---|
| 72 | $query = ' |
---|
[1084] | 73 | DELETE |
---|
[2100] | 74 | FROM '.RATE_TABLE.' |
---|
| 75 | WHERE user_id = '.$user['id'].' |
---|
| 76 | AND anonymous_id = \''.$save_anonymous_id.'\' |
---|
| 77 | AND element_id IN ('.implode(',', $already_there).') |
---|
[1084] | 78 | ;'; |
---|
[1992] | 79 | pwg_query($query); |
---|
| 80 | } |
---|
[1084] | 81 | |
---|
[1992] | 82 | $query = ' |
---|
[2100] | 83 | UPDATE '.RATE_TABLE.' |
---|
| 84 | SET anonymous_id = \'' .$anonymous_id.'\' |
---|
| 85 | WHERE user_id = '.$user['id'].' |
---|
| 86 | AND anonymous_id = \'' . $save_anonymous_id.'\' |
---|
[1084] | 87 | ;'; |
---|
[1992] | 88 | pwg_query($query); |
---|
| 89 | } // end client changed ip |
---|
[1084] | 90 | |
---|
[2100] | 91 | pwg_set_cookie_var('anonymous_rater', $anonymous_id); |
---|
[1092] | 92 | } // end anonymous user |
---|
[1992] | 93 | |
---|
[1092] | 94 | $query = ' |
---|
[1084] | 95 | DELETE |
---|
| 96 | FROM '.RATE_TABLE.' |
---|
| 97 | WHERE element_id = '.$image_id.' |
---|
[2100] | 98 | AND user_id = '.$user['id'].' |
---|
[1084] | 99 | '; |
---|
[2471] | 100 | if ($user_anonymous) |
---|
[1092] | 101 | { |
---|
| 102 | $query.= ' AND anonymous_id = \''.$anonymous_id.'\''; |
---|
| 103 | } |
---|
| 104 | pwg_query($query); |
---|
| 105 | $query = ' |
---|
[1084] | 106 | INSERT |
---|
| 107 | INTO '.RATE_TABLE.' |
---|
| 108 | (user_id,anonymous_id,element_id,rate,date) |
---|
| 109 | VALUES |
---|
| 110 | (' |
---|
[1092] | 111 | .$user['id'].',' |
---|
[2100] | 112 | .'\''.$anonymous_id.'\',' |
---|
[1092] | 113 | .$image_id.',' |
---|
| 114 | .$rate |
---|
| 115 | .',NOW()) |
---|
[1084] | 116 | ;'; |
---|
[1092] | 117 | pwg_query($query); |
---|
| 118 | |
---|
[11827] | 119 | return update_rating_score($image_id); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[11893] | 123 | /* update images.rating_score field |
---|
[11827] | 124 | * we use a bayesian average (http://en.wikipedia.org/wiki/Bayesian_average) with |
---|
| 125 | C = average number of rates per item |
---|
| 126 | m = global average rate (all rates) |
---|
| 127 | |
---|
| 128 | * param int $element_id optional, otherwise applies to all |
---|
[11893] | 129 | * @return array(rating_score, count) if element_id is specified |
---|
[11827] | 130 | */ |
---|
| 131 | function update_rating_score($element_id = false) |
---|
| 132 | { |
---|
[1092] | 133 | $query = ' |
---|
[11827] | 134 | SELECT element_id, |
---|
| 135 | COUNT(rate) AS rcount, |
---|
| 136 | SUM(rate) AS rsum |
---|
[1084] | 137 | FROM '.RATE_TABLE.' |
---|
[11827] | 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 | |
---|
[12412] | 153 | if ($all_rates_count>0) |
---|
| 154 | { |
---|
| 155 | $all_rates_avg /= $all_rates_count; |
---|
| 156 | $item_ratecount_avg = $all_rates_count / count($by_item); |
---|
| 157 | } |
---|
[11827] | 158 | |
---|
| 159 | $updates = array(); |
---|
| 160 | foreach ($by_item as $id => $rate_summary ) |
---|
| 161 | { |
---|
| 162 | $score = ( $item_ratecount_avg * $all_rates_avg + $rate_summary['rsum'] ) / ($item_ratecount_avg + $rate_summary['rcount']); |
---|
| 163 | $score = round($score,2); |
---|
| 164 | if ($id==$element_id) |
---|
| 165 | { |
---|
| 166 | $return = array( |
---|
| 167 | 'score' => $score, |
---|
| 168 | 'average' => round($rate_summary['rsum'] / $rate_summary['rcount'], 2), |
---|
| 169 | 'count' => $rate_summary['rcount'], |
---|
| 170 | ); |
---|
| 171 | } |
---|
[11893] | 172 | $updates[] = array( 'id'=>$id, 'rating_score'=>$score ); |
---|
[11827] | 173 | } |
---|
| 174 | mass_updates( |
---|
| 175 | IMAGES_TABLE, |
---|
| 176 | array( |
---|
| 177 | 'primary' => array('id'), |
---|
[11893] | 178 | 'update' => array('rating_score') |
---|
[11827] | 179 | ), |
---|
| 180 | $updates |
---|
| 181 | ); |
---|
| 182 | |
---|
| 183 | //set to null all items with no rate |
---|
| 184 | if ( !isset($by_item[$element_id]) ) |
---|
| 185 | { |
---|
| 186 | $query=' |
---|
| 187 | SELECT id FROM '.IMAGES_TABLE .' |
---|
| 188 | LEFT JOIN '.RATE_TABLE.' ON id=element_id |
---|
[11893] | 189 | WHERE element_id IS NULL AND rating_score IS NOT NULL'; |
---|
[11827] | 190 | |
---|
| 191 | $to_update = array_from_query( $query, 'id'); |
---|
| 192 | |
---|
| 193 | if ( !empty($to_update) ) |
---|
| 194 | { |
---|
| 195 | $query=' |
---|
| 196 | UPDATE '.IMAGES_TABLE .' |
---|
[11893] | 197 | SET rating_score=NULL |
---|
[11827] | 198 | WHERE id IN (' . implode(',',$to_update) . ')'; |
---|
| 199 | pwg_query($query); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | return isset($return) ? $return : array('score'=>null, 'average'=>null, 'count'=>0 ); |
---|
[1084] | 204 | } |
---|
| 205 | |
---|
[2100] | 206 | ?> |
---|