source: trunk/admin/rating_user.php @ 16925

Last change on this file since 16925 was 16925, checked in by mistic100, 12 years ago

feature 2703: make it easy for plugins to add tabs in admin screens
add a trigger a give an id to each core tabsheets

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