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 | defined('PHPWG_ROOT_PATH') or die ("Hacking attempt!"); |
---|
25 | |
---|
26 | include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); |
---|
27 | $tabsheet = new tabsheet(); |
---|
28 | $tabsheet->set_id('rating'); |
---|
29 | $tabsheet->select('rating_user'); |
---|
30 | $tabsheet->assign(); |
---|
31 | |
---|
32 | $filter_min_rates = 2; |
---|
33 | if (isset($_GET['f_min_rates'])) |
---|
34 | { |
---|
35 | $filter_min_rates = (int)$_GET['f_min_rates']; |
---|
36 | } |
---|
37 | |
---|
38 | $consensus_top_number = $conf['top_number']; |
---|
39 | if (isset($_GET['consensus_top_number'])) |
---|
40 | { |
---|
41 | $consensus_top_number = (int)$_GET['consensus_top_number']; |
---|
42 | } |
---|
43 | |
---|
44 | // build users |
---|
45 | global $conf; |
---|
46 | $query = 'SELECT DISTINCT |
---|
47 | u.'.$conf['user_fields']['id'].' AS id, |
---|
48 | u.'.$conf['user_fields']['username'].' AS name, |
---|
49 | ui.status |
---|
50 | FROM '.USERS_TABLE.' AS u INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
51 | ON u.'.$conf['user_fields']['id'].' = ui.user_id'; |
---|
52 | |
---|
53 | $users_by_id = array(); |
---|
54 | $result = pwg_query($query); |
---|
55 | while ($row = pwg_db_fetch_assoc($result)) |
---|
56 | { |
---|
57 | $users_by_id[(int)$row['id']] = array( |
---|
58 | 'name' => $row['name'], |
---|
59 | 'anon' => is_autorize_status(ACCESS_CLASSIC, $row['status']) ? false : true |
---|
60 | ); |
---|
61 | } |
---|
62 | |
---|
63 | $by_user_rating_model = array( 'rates' => array() ); |
---|
64 | foreach($conf['rate_items'] as $rate) |
---|
65 | { |
---|
66 | $by_user_rating_model['rates'][$rate] = array(); |
---|
67 | } |
---|
68 | |
---|
69 | // by user aggregation |
---|
70 | $image_ids = array(); |
---|
71 | $by_user_ratings = array(); |
---|
72 | $query = ' |
---|
73 | SELECT * FROM '.RATE_TABLE.' ORDER by date DESC'; |
---|
74 | $result = pwg_query($query); |
---|
75 | while ($row = pwg_db_fetch_assoc($result)) |
---|
76 | { |
---|
77 | if (!isset($users_by_id[$row['user_id']])) |
---|
78 | { |
---|
79 | $users_by_id[$row['user_id']] = array('name' => '???'.$row['user_id'], 'anon' => false); |
---|
80 | } |
---|
81 | $usr = $users_by_id[$row['user_id']]; |
---|
82 | if ($usr['anon']) |
---|
83 | { |
---|
84 | $user_key = $usr['name'].'('.$row['anonymous_id'].')'; |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | $user_key = $usr['name']; |
---|
89 | } |
---|
90 | $rating = & $by_user_ratings[$user_key]; |
---|
91 | if ( is_null($rating) ) |
---|
92 | { |
---|
93 | $rating = $by_user_rating_model; |
---|
94 | $rating['uid'] = (int)$row['user_id']; |
---|
95 | $rating['aid'] = $usr['anon'] ? $row['anonymous_id'] : ''; |
---|
96 | $rating['last_date'] = $rating['first_date'] = $row['date']; |
---|
97 | } |
---|
98 | else |
---|
99 | $rating['first_date'] = $row['date']; |
---|
100 | |
---|
101 | $rating['rates'][$row['rate']][] = array( |
---|
102 | 'id' => $row['element_id'], |
---|
103 | 'date' => $row['date'], |
---|
104 | ); |
---|
105 | $image_ids[$row['element_id']] = 1; |
---|
106 | unset($rating); |
---|
107 | } |
---|
108 | |
---|
109 | // get image tn urls |
---|
110 | $image_urls = array(); |
---|
111 | if (count($image_ids) > 0 ) |
---|
112 | { |
---|
113 | $query = 'SELECT id, name, file, path, representative_ext, level |
---|
114 | FROM '.IMAGES_TABLE.' |
---|
115 | WHERE id IN ('.implode(',', array_keys($image_ids)).')'; |
---|
116 | $result = pwg_query($query); |
---|
117 | $params = ImageStdParams::get_by_type(IMG_SQUARE); |
---|
118 | while ($row = pwg_db_fetch_assoc($result)) |
---|
119 | { |
---|
120 | $image_urls[ $row['id'] ] = array( |
---|
121 | 'tn' => DerivativeImage::url($params, $row), |
---|
122 | 'page' => make_picture_url( array('image_id'=>$row['id'], 'image_file'=>$row['file']) ), |
---|
123 | ); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | //all image averages |
---|
128 | $query='SELECT element_id, |
---|
129 | AVG(rate) AS avg |
---|
130 | FROM '.RATE_TABLE.' |
---|
131 | GROUP BY element_id'; |
---|
132 | $all_img_sum = array(); |
---|
133 | $result = pwg_query($query); |
---|
134 | while ($row = pwg_db_fetch_assoc($result)) |
---|
135 | { |
---|
136 | $all_img_sum[(int)$row['element_id']] = array( 'avg'=>(float)$row['avg'] ); |
---|
137 | } |
---|
138 | |
---|
139 | $query='SELECT id |
---|
140 | FROM '.IMAGES_TABLE.' |
---|
141 | ORDER by rating_score DESC |
---|
142 | LIMIT '.$consensus_top_number; |
---|
143 | $best_rated = array_flip( array_from_query($query, 'id')); |
---|
144 | |
---|
145 | // by user stats |
---|
146 | foreach($by_user_ratings as $id => &$rating) |
---|
147 | { |
---|
148 | $c=0; $s=0; $ss=0; $consensus_dev=0; $consensus_dev_top=0; $consensus_dev_top_count=0; |
---|
149 | foreach($rating['rates'] as $rate => $rates) |
---|
150 | { |
---|
151 | $ct = count($rates); |
---|
152 | $c += $ct; |
---|
153 | $s += $ct * $rate; |
---|
154 | $ss += $ct * $rate * $rate; |
---|
155 | foreach($rates as $id_date) |
---|
156 | { |
---|
157 | $dev = abs($rate - $all_img_sum[$id_date['id']]['avg']); |
---|
158 | $consensus_dev += $dev; |
---|
159 | if (isset($best_rated[$id_date['id']])) |
---|
160 | { |
---|
161 | $consensus_dev_top += $dev; |
---|
162 | $consensus_dev_top_count++; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | $consensus_dev /= $c; |
---|
168 | if ($consensus_dev_top_count) |
---|
169 | $consensus_dev_top /= $consensus_dev_top_count; |
---|
170 | |
---|
171 | $var = ($ss - $s*$s/$c)/$c; |
---|
172 | $rating += array( |
---|
173 | 'id' => $id, |
---|
174 | 'count' => $c, |
---|
175 | 'avg' => $s/$c, |
---|
176 | 'cv' => $s==0 ? -1 : sqrt($var)/($s/$c), // http://en.wikipedia.org/wiki/Coefficient_of_variation |
---|
177 | 'cd' => $consensus_dev, |
---|
178 | 'cdtop' => $consensus_dev_top_count ? $consensus_dev_top : '', |
---|
179 | ); |
---|
180 | } |
---|
181 | unset($rating); |
---|
182 | |
---|
183 | // filter |
---|
184 | foreach($by_user_ratings as $id => $rating) |
---|
185 | { |
---|
186 | if ($rating['count'] <= $filter_min_rates) |
---|
187 | { |
---|
188 | unset($by_user_ratings[$id]); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | function avg_compare($a, $b) |
---|
194 | { |
---|
195 | $d = $a['avg'] - $b['avg']; |
---|
196 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
197 | } |
---|
198 | |
---|
199 | function count_compare($a, $b) |
---|
200 | { |
---|
201 | $d = $a['count'] - $b['count']; |
---|
202 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
203 | } |
---|
204 | |
---|
205 | function cv_compare($a, $b) |
---|
206 | { |
---|
207 | $d = $b['cv'] - $a['cv']; //desc |
---|
208 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
209 | } |
---|
210 | |
---|
211 | function consensus_dev_compare($a, $b) |
---|
212 | { |
---|
213 | $d = $b['cd'] - $a['cd']; //desc |
---|
214 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
215 | } |
---|
216 | |
---|
217 | function last_rate_compare($a, $b) |
---|
218 | { |
---|
219 | return -strcmp( $a['last_date'], $b['last_date']); |
---|
220 | } |
---|
221 | |
---|
222 | $order_by_index=4; |
---|
223 | if (isset($_GET['order_by']) and is_numeric($_GET['order_by'])) |
---|
224 | { |
---|
225 | $order_by_index = $_GET['order_by']; |
---|
226 | } |
---|
227 | |
---|
228 | $available_order_by= array( |
---|
229 | array(l10n('Average rate'), 'avg_compare'), |
---|
230 | array(l10n('Number of rates'), 'count_compare'), |
---|
231 | array(l10n('Variation'), 'cv_compare'), |
---|
232 | array(l10n('Consensus deviation'), 'consensus_dev_compare'), |
---|
233 | array(l10n('Last'), 'last_rate_compare'), |
---|
234 | ); |
---|
235 | |
---|
236 | for ($i=0; $i<count($available_order_by); $i++) |
---|
237 | { |
---|
238 | $template->append( |
---|
239 | 'order_by_options', |
---|
240 | $available_order_by[$i][0] |
---|
241 | ); |
---|
242 | } |
---|
243 | $template->assign('order_by_options_selected', array($order_by_index) ); |
---|
244 | |
---|
245 | $x = uasort($by_user_ratings, $available_order_by[$order_by_index][1] ); |
---|
246 | |
---|
247 | $template->assign( array( |
---|
248 | 'F_ACTION' => get_root_url().'admin.php', |
---|
249 | 'F_MIN_RATES' => $filter_min_rates, |
---|
250 | 'CONSENSUS_TOP_NUMBER' => $consensus_top_number, |
---|
251 | 'available_rates' => $conf['rate_items'], |
---|
252 | 'ratings' => $by_user_ratings, |
---|
253 | 'image_urls' => $image_urls, |
---|
254 | 'TN_WIDTH' => ImageStdParams::get_by_type(IMG_SQUARE)->sizing->ideal_size[0], |
---|
255 | ) ); |
---|
256 | $template->set_filename('rating', 'rating_user.tpl'); |
---|
257 | $template->assign_var_from_handle('ADMIN_CONTENT', 'rating'); |
---|
258 | |
---|
259 | ?> |
---|