source: trunk/include/functions_rate.inc.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: functions_rate.inc.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48/**
49 * rate a picture by a user
50 *
51 * @param int image identifier
52 * @param int rate
53 * @return void
54 */
55function rate_picture($image_id, $rate)
56{
57  global $conf, $user;
58
59  if (!isset($rate)
60      or !$conf['rate']
61      or !in_array($rate, $conf['rate_items']))
62  {
63    return;
64  }
65
66  $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true;
67
68  if ($user_anonymous and !$conf['rate_anonymous'])
69  {
70    return;
71  }
72
73  $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]);
74  if (count($ip_components) > 3)
75  {
76    array_pop($ip_components);
77  }
78  $anonymous_id = implode ('.', $ip_components);
79
80  if ($user_anonymous)
81  {
82    $save_anonymous_id = pwg_get_cookie_var('anonymous_rater', $anonymous_id);
83
84    if ($anonymous_id != $save_anonymous_id)
85    { // client has changed his IP adress or he's trying to fool us
86      $query = '
87SELECT element_id
88  FROM '.RATE_TABLE.'
89  WHERE user_id = '.$user['id'].'
90    AND anonymous_id = \''.$anonymous_id.'\'
91;';
92      $already_there = array_from_query($query, 'element_id');
93
94      if (count($already_there) > 0)
95      {
96        $query = '
97DELETE
98  FROM '.RATE_TABLE.'
99  WHERE user_id = '.$user['id'].'
100    AND anonymous_id = \''.$save_anonymous_id.'\'
101    AND element_id IN ('.implode(',', $already_there).')
102;';
103         pwg_query($query);
104       }
105
106       $query = '
107UPDATE '.RATE_TABLE.'
108  SET anonymous_id = \'' .$anonymous_id.'\'
109  WHERE user_id = '.$user['id'].'
110    AND anonymous_id = \'' . $save_anonymous_id.'\'
111;';
112       pwg_query($query);
113    } // end client changed ip
114
115    pwg_set_cookie_var('anonymous_rater', $anonymous_id);
116  } // end anonymous user
117
118  $query = '
119DELETE
120  FROM '.RATE_TABLE.'
121  WHERE element_id = '.$image_id.'
122    AND user_id = '.$user['id'].'
123';
124  if (isset($user_anonymous))
125  {
126    $query.= ' AND anonymous_id = \''.$anonymous_id.'\'';
127  }
128  pwg_query($query);
129  $query = '
130INSERT
131  INTO '.RATE_TABLE.'
132  (user_id,anonymous_id,element_id,rate,date)
133  VALUES
134  ('
135    .$user['id'].','
136    .'\''.$anonymous_id.'\','
137    .$image_id.','
138    .$rate
139    .',NOW())
140;';
141  pwg_query($query);
142
143  // update of images.average_rate field
144  $query = '
145SELECT ROUND(AVG(rate),2) AS average_rate
146  FROM '.RATE_TABLE.'
147  WHERE element_id = '.$image_id.'
148;';
149  $row = mysql_fetch_array(pwg_query($query));
150  $query = '
151UPDATE '.IMAGES_TABLE.'
152  SET average_rate = '.$row['average_rate'].'
153  WHERE id = '.$image_id.'
154;';
155  pwg_query($query);
156}
157
158?>
Note: See TracBrowser for help on using the repository browser.