source: trunk/admin/rating_user.php @ 26461

Last change on this file since 26461 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

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