source: trunk/admin/rating_user.php @ 28012

Last change on this file since 28012 was 26900, checked in by rvelices, 11 years ago

admin rating user improvements

  • add last rate date for user
  • add consensus deviation but only based on the best rated photos (e.g. how much this user tries to change the best rated photos)
  • Property svn:eol-style set to LF
File size: 7.3 KB
Line 
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
24defined('PHPWG_ROOT_PATH') or die ("Hacking attempt!");
25
26include_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;
33if (isset($_GET['f_min_rates']))
34{
35  $filter_min_rates = (int)$_GET['f_min_rates'];
36}
37
38$consensus_top_number = $conf['top_number'];
39if (isset($_GET['consensus_top_number']))
40{
41  $consensus_top_number = (int)$_GET['consensus_top_number'];
42}
43
44// build users
45global $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);
55while ($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() );
64foreach($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 = '
73SELECT * FROM '.RATE_TABLE.' ORDER by date DESC';
74$result = pwg_query($query);
75while ($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'] = $row['date'];
97  }
98  $rating['rates'][$row['rate']][] = array(
99    'id' => $row['element_id'],
100    'date' => $row['date'],
101  );
102  $image_ids[$row['element_id']] = 1;
103  unset($rating);
104}
105
106// get image tn urls
107$image_urls = array();
108if (count($image_ids) > 0 )
109{
110  $query = 'SELECT id, name, file, path, representative_ext
111  FROM '.IMAGES_TABLE.'
112  WHERE id IN ('.implode(',', array_keys($image_ids)).')';
113  $result = pwg_query($query);
114  while ($row = pwg_db_fetch_assoc($result))
115  {
116    $image_urls[ $row['id'] ] = array(
117      'tn' => DerivativeImage::thumb_url($row),
118      'page' => make_picture_url( array('image_id'=>$row['id'], 'image_file'=>$row['file']) ),
119    );
120  }
121}
122
123//all image averages
124$query='SELECT element_id,
125    AVG(rate) AS avg
126  FROM '.RATE_TABLE.'
127  GROUP BY element_id';
128$all_img_sum = array();
129$result = pwg_query($query);
130while ($row = pwg_db_fetch_assoc($result))
131{
132  $all_img_sum[(int)$row['element_id']] = array( 'avg'=>(float)$row['avg'] );
133}
134
135$query='SELECT id
136  FROM '.IMAGES_TABLE.'
137  ORDER by rating_score DESC
138  LIMIT '.$consensus_top_number;
139$best_rated = array_flip( array_from_query($query, 'id'));
140
141// by user stats
142foreach($by_user_ratings as $id => &$rating)
143{
144  $c=0; $s=0; $ss=0; $consensus_dev=0; $consensus_dev_top=0; $consensus_dev_top_count=0;
145  foreach($rating['rates'] as $rate => $rates)
146  {
147    $ct = count($rates);
148    $c += $ct;
149    $s += $ct * $rate;
150    $ss += $ct * $rate * $rate;
151    foreach($rates as $id_date)
152    {
153      $dev = abs($rate - $all_img_sum[$id_date['id']]['avg']);
154      $consensus_dev += $dev;
155      if (isset($best_rated[$id_date['id']]))
156      {
157        $consensus_dev_top += $dev;
158        $consensus_dev_top_count++;
159      }
160    }
161  }
162
163  $consensus_dev /= $c;
164  if ($consensus_dev_top_count)
165    $consensus_dev_top /= $consensus_dev_top_count;
166
167  $var = ($ss - $s*$s/$c)/$c;
168  $rating += array(
169    'id' => $id,
170    'count' => $c,
171    'avg' => $s/$c,
172    'cv'  => $s==0 ? -1 : sqrt($var)/($s/$c), // http://en.wikipedia.org/wiki/Coefficient_of_variation
173    'cd'  => $consensus_dev,
174    'cdtop'  => $consensus_dev_top_count ? $consensus_dev_top : ''
175  );
176}
177unset($rating);
178
179// filter
180foreach($by_user_ratings as $id => $rating)
181{
182  if ($rating['count'] <= $filter_min_rates)
183  {
184    unset($by_user_ratings[$id]);
185  }
186}
187
188
189function avg_compare($a, $b)
190{
191  $d = $a['avg'] - $b['avg'];
192  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
193}
194
195function count_compare($a, $b)
196{
197  $d = $a['count'] - $b['count'];
198  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
199}
200
201function cv_compare($a, $b)
202{
203  $d = $b['cv'] - $a['cv']; //desc
204  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
205}
206
207function consensus_dev_compare($a, $b)
208{
209  $d = $b['cd'] - $a['cd']; //desc
210  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
211}
212
213$order_by_index=3;
214if (isset($_GET['order_by']) and is_numeric($_GET['order_by']))
215{
216  $order_by_index = $_GET['order_by'];
217}
218
219$available_order_by= array(
220    array(l10n('Average rate'), 'avg_compare'),
221    array(l10n('Number of rates'), 'count_compare'),
222    array(l10n('Variation'), 'cv_compare'),
223    array(l10n('Consensus deviation'), 'consensus_dev_compare'),
224  );
225
226for ($i=0; $i<count($available_order_by); $i++)
227{
228  $template->append(
229    'order_by_options',
230    $available_order_by[$i][0]
231    );
232}
233$template->assign('order_by_options_selected', array($order_by_index) );
234
235$x = uasort($by_user_ratings, $available_order_by[$order_by_index][1] );
236
237$template->assign( array(
238  'F_ACTION' => get_root_url().'admin.php',
239  'F_MIN_RATES' => $filter_min_rates,
240  'CONSENSUS_TOP_NUMBER' => $consensus_top_number,
241  'available_rates' => $conf['rate_items'],
242  'ratings' => $by_user_ratings,
243  'image_urls' => $image_urls,
244  'TN_WIDTH' => 28+2*ImageStdParams::get_by_type(IMG_THUMB)->sizing->ideal_size[0],
245  ) );
246$template->set_filename('rating', 'rating_user.tpl');
247$template->assign_var_from_handle('ADMIN_CONTENT', 'rating');
248
249?>
Note: See TracBrowser for help on using the repository browser.