source: trunk/admin/rating.php @ 11844

Last change on this file since 11844 was 11827, checked in by rvelices, 13 years ago

feature 2384: improve average rating calculation (still need to update language files)

  • Property svn:eol-style set to LF
File size: 7.7 KB
RevLine 
[1056]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1056]23
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die ("Hacking attempt!");
27}
28
[1072]29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1056]30
31// +-----------------------------------------------------------------------+
[1072]32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36// +-----------------------------------------------------------------------+
[1056]37// |                            initialization                             |
38// +-----------------------------------------------------------------------+
39if (isset($_GET['start']) and is_numeric($_GET['start']))
40{
41  $start = $_GET['start'];
42}
43else
44{
45  $start = 0;
46}
47
48$elements_per_page=10;
49if (isset($_GET['display']) and is_numeric($_GET['display']))
50{
51  $elements_per_page = $_GET['display'];
52}
53
54$order_by_index=0;
55if (isset($_GET['order_by']) and is_numeric($_GET['order_by']))
56{
57  $order_by_index = $_GET['order_by'];
58}
59
[1211]60$page['user_filter'] = '';
61if (isset($_GET['users']))
[1056]62{
[1211]63  if ($_GET['users'] == 'user')
[1056]64  {
[1211]65    $page['user_filter'] = ' AND r.user_id <> '.$conf['guest_id'];
[1056]66  }
[1211]67  elseif ($_GET['users'] == 'guest')
[1056]68  {
[1211]69    $page['user_filter'] = ' AND r.user_id = '.$conf['guest_id'];
[1056]70  }
71}
72
[8126]73if (isset($_GET['del']))
[1056]74{
75  $del_params = urldecode( $_GET['del'] );
76  parse_str($del_params, $vars);
77  if ( !is_numeric($vars['e']) or !is_numeric($vars['u']) )
78  {
79    die('Hacking attempt');
80  }
81  $query = '
82DELETE FROM '. RATE_TABLE .'
83WHERE element_id=' . $vars['e'] . '
84AND user_id=' . $vars['u'] . '
85AND anonymous_id=\'' . $vars['a'] . '\'
86;';
87  pwg_query($query);
[11827]88  update_rating_score( $vars['e'] );
[1056]89}
90
91$users = array();
92$query = '
93SELECT '.$conf['user_fields']['username'].' as username, '.$conf['user_fields']['id'].' as id
94  FROM '.USERS_TABLE.'
95;';
96$result = pwg_query($query);
[4325]97while ($row = pwg_db_fetch_assoc($result))
[1056]98{
[4304]99  $users[$row['id']]=stripslashes($row['username']);
[1056]100}
101
102
[11827]103$query = 'SELECT COUNT(DISTINCT(r.element_id))
104FROM '.RATE_TABLE.' AS r
105WHERE 1=1'. $page['user_filter'];
[4325]106list($nb_images) = pwg_db_fetch_row(pwg_query($query));
[1056]107
108
109// +-----------------------------------------------------------------------+
110// |                             template init                             |
111// +-----------------------------------------------------------------------+
112
[2530]113$template->set_filename('rating', 'rating.tpl');
[1056]114
[2223]115$template->assign(
[1084]116  array(
[3172]117    'navbar' => create_navigation_bar(
[1084]118      PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start','del')),
119      $nb_images,
120      $start,
[1090]121      $elements_per_page
[2223]122      ),
[1056]123    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php',
124    'DISPLAY' => $elements_per_page,
[2223]125    'NB_ELEMENTS' => $nb_images,
[1056]126    )
127  );
128
[2223]129
130
[1056]131$available_order_by= array(
132    array(l10n('Rate date'), 'recently_rated DESC'),
[11827]133                array(l10n('Rating score'), 'score DESC'),
134    array(l10n('Average rate'), 'avg_rates DESC'),
[1059]135    array(l10n('Number of rates'), 'nb_rates DESC'),
136    array(l10n('Sum of rates'), 'sum_rates DESC'),
137    array(l10n('File name'), 'file DESC'),
[1056]138    array(l10n('Creation date'), 'date_creation DESC'),
[1059]139    array(l10n('Post date'), 'date_available DESC'),
[1056]140  );
141
142for ($i=0; $i<count($available_order_by); $i++)
143{
[2223]144  $template->append(
145    'order_by_options',
146    $available_order_by[$i][0]
[1056]147    );
148}
[2223]149$template->assign('order_by_options_selected', array($order_by_index) );
[1056]150
[2223]151
[1211]152$user_options = array(
[2223]153  'all'   => l10n('all'),
154  'user'  => l10n('Users'),
155  'guest' => l10n('Guests'),
[1211]156  );
157
[2223]158$template->assign('user_options', $user_options );
159$template->assign('user_options_selected', array(@$_GET['users']) );
[1211]160
[2223]161
[1064]162$query = '
163SELECT i.id,
164       i.path,
165       i.file,
166       i.tn_ext,
[11827]167       i.average_rate                           AS score,
[1064]168       MAX(r.date)          AS recently_rated,
[11827]169                         ROUND(AVG(r.rate),2) AS avg_rates,
[1064]170       COUNT(r.rate)        AS nb_rates,
[6125]171       SUM(r.rate)          AS sum_rates
[1064]172  FROM '.RATE_TABLE.' AS r
173    LEFT JOIN '.IMAGES_TABLE.' AS i ON r.element_id = i.id
[1211]174  WHERE 1 = 1 ' . $page['user_filter'] . '
[6607]175  GROUP BY i.id,
176        i.path,
177        i.file,
178        i.tn_ext,
179        i.average_rate,
180        r.element_id
[1064]181  ORDER BY ' . $available_order_by[$order_by_index][1] .'
[4334]182  LIMIT '.$elements_per_page.' OFFSET '.$start.'
[1064]183;';
[1056]184
185$images = array();
186$result = pwg_query($query);
[4325]187while ($row = pwg_db_fetch_assoc($result))
[1056]188{
189  array_push($images, $row);
190}
191
[2223]192$template->assign( 'images', array() );
[1056]193foreach ($images as $image)
194{
[1604]195  $thumbnail_src = get_thumbnail_url($image);
[1056]196
[1548]197  $image_url = PHPWG_ROOT_PATH.'admin.php?page=picture_modify'.
198            '&amp;image_id='.$image['id'];
[1059]199
[1056]200  $query = 'SELECT *
201FROM '.RATE_TABLE.' AS r
202WHERE r.element_id='.$image['id'] . '
203ORDER BY date DESC;';
204  $result = pwg_query($query);
[4325]205  $nb_rates = pwg_db_num_rows($result);
[1056]206
[2223]207  $tpl_image = 
[1056]208     array(
209       'U_THUMB' => $thumbnail_src,
210       'U_URL' => $image_url,
[11827]211                         'SCORE_RATE' => $image['score'],
212       'AVG_RATE' => $image['avg_rates'],
[1056]213       'SUM_RATE' => $image['sum_rates'],
[2223]214       'NB_RATES' => (int)$image['nb_rates'],
215       'NB_RATES_TOTAL' => (int)$nb_rates,
[1056]216       'FILE' => $image['file'],
[2223]217       'rates'  => array()
[1056]218   );
219
[4325]220  while ($row = pwg_db_fetch_assoc($result))
[1056]221  {
222
223    $url_del = PHPWG_ROOT_PATH.'admin.php'.
224                get_query_string_diff(array('del'));
[1059]225
[1056]226    $del_param = 'e='.$image['id'].
227                 '&u='.$row['user_id'].
228                 '&a='.$row['anonymous_id'];
[1059]229
[1056]230    $url_del .= '&amp;del='.urlencode(urlencode($del_param));
231
232    if ( isset($users[$row['user_id']]) )
233    {
[2475]234      $user_rate = $users[$row['user_id']];
[1056]235    }
236    else
237    {
[2475]238      $user_rate = '? '. $row['user_id'];
[1056]239    }
240    if ( strlen($row['anonymous_id'])>0 )
241    {
[2475]242      $user_rate .= '('.$row['anonymous_id'].')';
[1056]243    }
[1059]244
[2223]245    $tpl_image['rates'][] =
[1056]246       array(
247         'DATE' => format_date($row['date']),
248         'RATE' => $row['rate'],
[2475]249         'USER' => $user_rate,
[1056]250         'U_DELETE' => $url_del
251     );
252  }
[2223]253  $template->append( 'images', $tpl_image );
[1056]254}
[2223]255
[1056]256// +-----------------------------------------------------------------------+
257// |                           sending html code                           |
258// +-----------------------------------------------------------------------+
259$template->assign_var_from_handle('ADMIN_CONTENT', 'rating');
[6402]260?>
Note: See TracBrowser for help on using the repository browser.