1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
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 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | /** |
---|
25 | * @package functions\rate |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * Rate a picture by the current user. |
---|
31 | * |
---|
32 | * @param int $image_id |
---|
33 | * @param float $rate |
---|
34 | * @return array as return by update_rating_score() |
---|
35 | */ |
---|
36 | function rate_picture($image_id, $rate) |
---|
37 | { |
---|
38 | global $conf, $user; |
---|
39 | |
---|
40 | if (!isset($rate) |
---|
41 | or !$conf['rate'] |
---|
42 | or !in_array($rate, $conf['rate_items'])) |
---|
43 | { |
---|
44 | return false; |
---|
45 | } |
---|
46 | |
---|
47 | $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true; |
---|
48 | |
---|
49 | if ($user_anonymous and !$conf['rate_anonymous']) |
---|
50 | { |
---|
51 | return false; |
---|
52 | } |
---|
53 | |
---|
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 | |
---|
61 | if ($user_anonymous) |
---|
62 | { |
---|
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 = ' |
---|
68 | SELECT element_id |
---|
69 | FROM '.RATE_TABLE.' |
---|
70 | WHERE user_id = '.$user['id'].' |
---|
71 | AND anonymous_id = \''.$anonymous_id.'\' |
---|
72 | ;'; |
---|
73 | $already_there = array_from_query($query, 'element_id'); |
---|
74 | |
---|
75 | if (count($already_there) > 0) |
---|
76 | { |
---|
77 | $query = ' |
---|
78 | DELETE |
---|
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).') |
---|
83 | ;'; |
---|
84 | pwg_query($query); |
---|
85 | } |
---|
86 | |
---|
87 | $query = ' |
---|
88 | UPDATE '.RATE_TABLE.' |
---|
89 | SET anonymous_id = \'' .$anonymous_id.'\' |
---|
90 | WHERE user_id = '.$user['id'].' |
---|
91 | AND anonymous_id = \'' . $save_anonymous_id.'\' |
---|
92 | ;'; |
---|
93 | pwg_query($query); |
---|
94 | } // end client changed ip |
---|
95 | |
---|
96 | pwg_set_cookie_var('anonymous_rater', $anonymous_id); |
---|
97 | } // end anonymous user |
---|
98 | |
---|
99 | $query = ' |
---|
100 | DELETE |
---|
101 | FROM '.RATE_TABLE.' |
---|
102 | WHERE element_id = '.$image_id.' |
---|
103 | AND user_id = '.$user['id'].' |
---|
104 | '; |
---|
105 | if ($user_anonymous) |
---|
106 | { |
---|
107 | $query.= ' AND anonymous_id = \''.$anonymous_id.'\''; |
---|
108 | } |
---|
109 | pwg_query($query); |
---|
110 | $query = ' |
---|
111 | INSERT |
---|
112 | INTO '.RATE_TABLE.' |
---|
113 | (user_id,anonymous_id,element_id,rate,date) |
---|
114 | VALUES |
---|
115 | (' |
---|
116 | .$user['id'].',' |
---|
117 | .'\''.$anonymous_id.'\',' |
---|
118 | .$image_id.',' |
---|
119 | .$rate |
---|
120 | .',NOW()) |
---|
121 | ;'; |
---|
122 | pwg_query($query); |
---|
123 | |
---|
124 | return update_rating_score($image_id); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
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 |
---|
136 | */ |
---|
137 | function update_rating_score($element_id = false) |
---|
138 | { |
---|
139 | $query = ' |
---|
140 | SELECT element_id, |
---|
141 | COUNT(rate) AS rcount, |
---|
142 | SUM(rate) AS rsum |
---|
143 | FROM '.RATE_TABLE.' |
---|
144 | GROUP by element_id'; |
---|
145 | |
---|
146 | $all_rates_count = 0; |
---|
147 | $all_rates_avg = 0; |
---|
148 | $item_ratecount_avg = 0; |
---|
149 | $by_item = array(); |
---|
150 | |
---|
151 | $result = pwg_query($query); |
---|
152 | while ($row = pwg_db_fetch_assoc($result)) |
---|
153 | { |
---|
154 | $all_rates_count += $row['rcount']; |
---|
155 | $all_rates_avg += $row['rsum']; |
---|
156 | $by_item[$row['element_id']] = $row; |
---|
157 | } |
---|
158 | |
---|
159 | if ($all_rates_count>0) |
---|
160 | { |
---|
161 | $all_rates_avg /= $all_rates_count; |
---|
162 | $item_ratecount_avg = $all_rates_count / count($by_item); |
---|
163 | } |
---|
164 | |
---|
165 | $updates = array(); |
---|
166 | foreach ($by_item as $id => $rate_summary ) |
---|
167 | { |
---|
168 | $score = ( $item_ratecount_avg * $all_rates_avg + $rate_summary['rsum'] ) / ($item_ratecount_avg + $rate_summary['rcount']); |
---|
169 | $score = round($score,2); |
---|
170 | if ($id==$element_id) |
---|
171 | { |
---|
172 | $return = array( |
---|
173 | 'score' => $score, |
---|
174 | 'average' => round($rate_summary['rsum'] / $rate_summary['rcount'], 2), |
---|
175 | 'count' => $rate_summary['rcount'], |
---|
176 | ); |
---|
177 | } |
---|
178 | $updates[] = array( 'id'=>$id, 'rating_score'=>$score ); |
---|
179 | } |
---|
180 | mass_updates( |
---|
181 | IMAGES_TABLE, |
---|
182 | array( |
---|
183 | 'primary' => array('id'), |
---|
184 | 'update' => array('rating_score') |
---|
185 | ), |
---|
186 | $updates |
---|
187 | ); |
---|
188 | |
---|
189 | //set to null all items with no rate |
---|
190 | if ( !isset($by_item[$element_id]) ) |
---|
191 | { |
---|
192 | $query=' |
---|
193 | SELECT id FROM '.IMAGES_TABLE .' |
---|
194 | LEFT JOIN '.RATE_TABLE.' ON id=element_id |
---|
195 | WHERE element_id IS NULL AND rating_score IS NOT NULL'; |
---|
196 | |
---|
197 | $to_update = array_from_query( $query, 'id'); |
---|
198 | |
---|
199 | if ( !empty($to_update) ) |
---|
200 | { |
---|
201 | $query=' |
---|
202 | UPDATE '.IMAGES_TABLE .' |
---|
203 | SET rating_score=NULL |
---|
204 | WHERE id IN (' . implode(',',$to_update) . ')'; |
---|
205 | pwg_query($query); |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | return isset($return) ? $return : array('score'=>null, 'average'=>null, 'count'=>0 ); |
---|
210 | } |
---|
211 | |
---|
212 | ?> |
---|