[12624] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based photo gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
[12624] | 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(); |
---|
[16925] | 28 | $tabsheet->set_id('rating'); |
---|
[12624] | 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 | |
---|
[26900] | 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 | |
---|
[12650] | 44 | // build users |
---|
[12624] | 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 | { |
---|
[12650] | 57 | $users_by_id[(int)$row['id']] = array( |
---|
[12624] | 58 | 'name' => $row['name'], |
---|
| 59 | 'anon' => is_autorize_status(ACCESS_CLASSIC, $row['status']) ? false : true |
---|
| 60 | ); |
---|
| 61 | } |
---|
| 62 | |
---|
[12650] | 63 | $by_user_rating_model = array( 'rates' => array() ); |
---|
[12624] | 64 | foreach($conf['rate_items'] as $rate) |
---|
| 65 | { |
---|
[12650] | 66 | $by_user_rating_model['rates'][$rate] = array(); |
---|
[12624] | 67 | } |
---|
| 68 | |
---|
[26900] | 69 | // by user aggregation |
---|
[12624] | 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 | } |
---|
[12650] | 81 | $usr = $users_by_id[$row['user_id']]; |
---|
| 82 | if ($usr['anon']) |
---|
[12624] | 83 | { |
---|
[12650] | 84 | $user_key = $usr['name'].'('.$row['anonymous_id'].')'; |
---|
[12624] | 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
[12650] | 88 | $user_key = $usr['name']; |
---|
[12624] | 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']; |
---|
[12650] | 95 | $rating['aid'] = $usr['anon'] ? $row['anonymous_id'] : ''; |
---|
[29053] | 96 | $rating['last_date'] = $rating['first_date'] = $row['date']; |
---|
[12624] | 97 | } |
---|
[29053] | 98 | else |
---|
| 99 | $rating['first_date'] = $row['date']; |
---|
| 100 | |
---|
[12624] | 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 | |
---|
[12650] | 109 | // get image tn urls |
---|
[12624] | 110 | $image_urls = array(); |
---|
| 111 | if (count($image_ids) > 0 ) |
---|
| 112 | { |
---|
[29053] | 113 | $query = 'SELECT id, name, file, path, representative_ext, level |
---|
[12624] | 114 | FROM '.IMAGES_TABLE.' |
---|
| 115 | WHERE id IN ('.implode(',', array_keys($image_ids)).')'; |
---|
| 116 | $result = pwg_query($query); |
---|
[29053] | 117 | $params = ImageStdParams::get_by_type(IMG_SQUARE); |
---|
[12624] | 118 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 119 | { |
---|
| 120 | $image_urls[ $row['id'] ] = array( |
---|
[29053] | 121 | 'tn' => DerivativeImage::url($params, $row), |
---|
[12624] | 122 | 'page' => make_picture_url( array('image_id'=>$row['id'], 'image_file'=>$row['file']) ), |
---|
| 123 | ); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[26900] | 127 | //all image averages |
---|
[12650] | 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 | |
---|
[26900] | 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 |
---|
[12624] | 146 | foreach($by_user_ratings as $id => &$rating) |
---|
| 147 | { |
---|
[26900] | 148 | $c=0; $s=0; $ss=0; $consensus_dev=0; $consensus_dev_top=0; $consensus_dev_top_count=0; |
---|
[12624] | 149 | foreach($rating['rates'] as $rate => $rates) |
---|
| 150 | { |
---|
| 151 | $ct = count($rates); |
---|
| 152 | $c += $ct; |
---|
| 153 | $s += $ct * $rate; |
---|
| 154 | $ss += $ct * $rate * $rate; |
---|
[12650] | 155 | foreach($rates as $id_date) |
---|
| 156 | { |
---|
[26900] | 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 | } |
---|
[12650] | 164 | } |
---|
[12624] | 165 | } |
---|
| 166 | |
---|
[12650] | 167 | $consensus_dev /= $c; |
---|
[26900] | 168 | if ($consensus_dev_top_count) |
---|
| 169 | $consensus_dev_top /= $consensus_dev_top_count; |
---|
[12650] | 170 | |
---|
[12624] | 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 |
---|
[26900] | 177 | 'cd' => $consensus_dev, |
---|
[29053] | 178 | 'cdtop' => $consensus_dev_top_count ? $consensus_dev_top : '', |
---|
[12624] | 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 | |
---|
[12650] | 205 | function cv_compare($a, $b) |
---|
[12624] | 206 | { |
---|
[12650] | 207 | $d = $b['cv'] - $a['cv']; //desc |
---|
[12624] | 208 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
| 209 | } |
---|
| 210 | |
---|
[12650] | 211 | function consensus_dev_compare($a, $b) |
---|
[12624] | 212 | { |
---|
[12650] | 213 | $d = $b['cd'] - $a['cd']; //desc |
---|
[12624] | 214 | return ($d==0) ? 0 : ($d<0 ? -1 : 1); |
---|
| 215 | } |
---|
| 216 | |
---|
[29053] | 217 | function last_rate_compare($a, $b) |
---|
| 218 | { |
---|
| 219 | return -strcmp( $a['last_date'], $b['last_date']); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | $order_by_index=4; |
---|
[12624] | 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'), |
---|
[12650] | 231 | array(l10n('Variation'), 'cv_compare'), |
---|
| 232 | array(l10n('Consensus deviation'), 'consensus_dev_compare'), |
---|
[29053] | 233 | array(l10n('Last'), 'last_rate_compare'), |
---|
[12624] | 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, |
---|
[26900] | 250 | 'CONSENSUS_TOP_NUMBER' => $consensus_top_number, |
---|
[12624] | 251 | 'available_rates' => $conf['rate_items'], |
---|
| 252 | 'ratings' => $by_user_ratings, |
---|
| 253 | 'image_urls' => $image_urls, |
---|
[29053] | 254 | 'TN_WIDTH' => ImageStdParams::get_by_type(IMG_SQUARE)->sizing->ideal_size[0], |
---|
[12624] | 255 | ) ); |
---|
| 256 | $template->set_filename('rating', 'rating_user.tpl'); |
---|
| 257 | $template->assign_var_from_handle('ADMIN_CONTENT', 'rating'); |
---|
| 258 | |
---|
| 259 | ?> |
---|