source: trunk/admin/rating_user.php @ 12624

Last change on this file since 12624 was 12624, checked in by rvelices, 12 years ago

feature 2486: Add an admin view for rates by user

File size: 6.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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->add('rating', l10n('Photos'), get_root_url().'admin.php?page=rating');
29$tabsheet->add('rating_user', l10n('Users'), get_root_url().'admin.php?page=rating_user');
30$tabsheet->select('rating_user');
31$tabsheet->assign();
32
33$filter_min_rates = 2;
34if (isset($_GET['f_min_rates']))
35{
36  $filter_min_rates = (int)$_GET['f_min_rates'];
37}
38
39global $conf;
40$query = 'SELECT DISTINCT
41  u.'.$conf['user_fields']['id'].' AS id,
42  u.'.$conf['user_fields']['username'].' AS name,
43  ui.status
44  FROM '.USERS_TABLE.' AS u INNER JOIN '.USER_INFOS_TABLE.' AS ui
45    ON u.'.$conf['user_fields']['id'].' = ui.user_id';
46
47$users_by_id = array();
48$result = pwg_query($query);
49while ($row = pwg_db_fetch_assoc($result))
50{
51  $user = array(
52    'name' => $row['name'],
53    'anon' => is_autorize_status(ACCESS_CLASSIC, $row['status']) ? false : true
54  );
55  $users_by_id[(int)$row['id']] = $user;
56}
57
58$by_rate_model = array();
59foreach($conf['rate_items'] as $rate)
60{
61  $by_rate_model[$rate] = array();
62}
63
64
65$by_user_rating_model = array( 'rates' => $by_rate_model);
66
67$image_ids = array();
68$by_user_ratings = array();
69$query = '
70SELECT * FROM '.RATE_TABLE.' ORDER by date DESC';
71$result = pwg_query($query);
72while ($row = pwg_db_fetch_assoc($result))
73{
74
75  if (!isset($users_by_id[$row['user_id']]))
76  {
77    $users_by_id[$row['user_id']] = array('name' => '???'.$row['user_id'], 'anon' => false);
78  }
79  $user = $users_by_id[$row['user_id']];
80  if ($user['anon'])
81  {
82    $user_key = $user['name'].'('.$row['anonymous_id'].')';
83  }
84  else
85  {
86    $user_key = $user['name'];
87  }
88  $rating = & $by_user_ratings[$user_key];
89  if ( is_null($rating) )
90  {
91    $rating = $by_user_rating_model;
92    $rating['uid'] = (int)$row['user_id'];
93    $rating['aid'] = $user['anon'] ? $row['anonymous_id'] : '';
94  }
95  $rating['rates'][$row['rate']][] = array(
96    'id' => $row['element_id'],
97    'date' => $row['date'],
98  );
99  $image_ids[$row['element_id']] = 1;
100  unset($rating);
101}
102
103
104
105$image_urls = array();
106if (count($image_ids) > 0 )
107{
108  $query = 'SELECT id, name, file, path, tn_ext
109  FROM '.IMAGES_TABLE.'
110  WHERE id IN ('.implode(',', array_keys($image_ids)).')';
111  $result = pwg_query($query);
112  while ($row = pwg_db_fetch_assoc($result))
113  {
114    $image_urls[ $row['id'] ] = array(
115      'tn' => get_thumbnail_url($row),
116      'page' => make_picture_url( array('image_id'=>$row['id'], 'image_file'=>$row['file']) ),
117    );
118  }
119}
120
121foreach($by_user_ratings as $id => &$rating)
122{
123  $c=0; $s=0; $ss=0;
124  foreach($rating['rates'] as $rate => $rates)
125  {
126    $ct = count($rates);
127    $c += $ct;
128    $s += $ct * $rate;
129    $ss += $ct * $rate * $rate;
130  }
131
132  $var = ($ss - $s*$s/$c)/$c;
133  $rating += array(
134    'id' => $id,
135    'count' => $c,
136    'avg' => $s/$c,
137    'std' => sqrt($var),
138    'cv'  => $s==0 ? -1 : sqrt($var)/($s/$c), // http://en.wikipedia.org/wiki/Coefficient_of_variation
139  );
140}
141unset($rating);
142
143// filter
144foreach($by_user_ratings as $id => $rating)
145{
146  if ($rating['count'] <= $filter_min_rates)
147  {
148    unset($by_user_ratings[$id]);
149  }
150}
151
152
153function avg_compare($a, $b)
154{
155  $d = $a['avg'] - $b['avg'];
156  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
157}
158
159function count_compare($a, $b)
160{
161  $d = $a['count'] - $b['count'];
162  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
163}
164
165function std_compare($a, $b)
166{
167  $d = $a['std'] - $b['std'];
168  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
169}
170
171function cv_compare($a, $b)
172{
173  $d = $a['cv'] - $b['cv'];
174  return ($d==0) ? 0 : ($d<0 ? -1 : 1);
175}
176
177
178$order_by_index=3;
179if (isset($_GET['order_by']) and is_numeric($_GET['order_by']))
180{
181  $order_by_index = $_GET['order_by'];
182}
183
184$available_order_by= array(
185    array(l10n('Average rate'), 'avg_compare'),
186    array(l10n('Number of rates'), 'count_compare'),
187    array('StDev', 'std_compare'),
188    array('Coeff of Variation', 'cv_compare'),
189  );
190
191for ($i=0; $i<count($available_order_by); $i++)
192{
193  $template->append(
194    'order_by_options',
195    $available_order_by[$i][0]
196    );
197}
198$template->assign('order_by_options_selected', array($order_by_index) );
199
200$x = uasort($by_user_ratings, $available_order_by[$order_by_index][1] );
201
202$template->assign( array(
203  'F_ACTION' => get_root_url().'admin.php',
204  'F_MIN_RATES' => $filter_min_rates,
205  'available_rates' => $conf['rate_items'],
206  'ratings' => $by_user_ratings,
207  'image_urls' => $image_urls,
208  ) );
209$template->set_filename('rating', 'rating_user.tpl');
210$template->assign_var_from_handle('ADMIN_CONTENT', 'rating');
211
212?>
Note: See TracBrowser for help on using the repository browser.