source: trunk/admin/rating_user.php @ 13018

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

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

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