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

Last change on this file since 2333 was 2299, checked in by plg, 17 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 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/**
25 * rate a picture by a user
26 *
27 * @param int image identifier
28 * @param int rate
29 * @return void
30 */
31function rate_picture($image_id, $rate)
32{
33  global $conf, $user;
34
35  if (!isset($rate)
36      or !$conf['rate']
37      or !in_array($rate, $conf['rate_items']))
38  {
39    return;
40  }
41
42  $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true;
43
44  if ($user_anonymous and !$conf['rate_anonymous'])
45  {
46    return;
47  }
48
49  $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]);
50  if (count($ip_components) > 3)
51  {
52    array_pop($ip_components);
53  }
54  $anonymous_id = implode ('.', $ip_components);
55
56  if ($user_anonymous)
57  {
58    $save_anonymous_id = pwg_get_cookie_var('anonymous_rater', $anonymous_id);
59
60    if ($anonymous_id != $save_anonymous_id)
61    { // client has changed his IP adress or he's trying to fool us
62      $query = '
63SELECT element_id
64  FROM '.RATE_TABLE.'
65  WHERE user_id = '.$user['id'].'
66    AND anonymous_id = \''.$anonymous_id.'\'
67;';
68      $already_there = array_from_query($query, 'element_id');
69
70      if (count($already_there) > 0)
71      {
72        $query = '
73DELETE
74  FROM '.RATE_TABLE.'
75  WHERE user_id = '.$user['id'].'
76    AND anonymous_id = \''.$save_anonymous_id.'\'
77    AND element_id IN ('.implode(',', $already_there).')
78;';
79         pwg_query($query);
80       }
81
82       $query = '
83UPDATE '.RATE_TABLE.'
84  SET anonymous_id = \'' .$anonymous_id.'\'
85  WHERE user_id = '.$user['id'].'
86    AND anonymous_id = \'' . $save_anonymous_id.'\'
87;';
88       pwg_query($query);
89    } // end client changed ip
90
91    pwg_set_cookie_var('anonymous_rater', $anonymous_id);
92  } // end anonymous user
93
94  $query = '
95DELETE
96  FROM '.RATE_TABLE.'
97  WHERE element_id = '.$image_id.'
98    AND user_id = '.$user['id'].'
99';
100  if (isset($user_anonymous))
101  {
102    $query.= ' AND anonymous_id = \''.$anonymous_id.'\'';
103  }
104  pwg_query($query);
105  $query = '
106INSERT
107  INTO '.RATE_TABLE.'
108  (user_id,anonymous_id,element_id,rate,date)
109  VALUES
110  ('
111    .$user['id'].','
112    .'\''.$anonymous_id.'\','
113    .$image_id.','
114    .$rate
115    .',NOW())
116;';
117  pwg_query($query);
118
119  // update of images.average_rate field
120  $query = '
121SELECT ROUND(AVG(rate),2) AS average_rate
122  FROM '.RATE_TABLE.'
123  WHERE element_id = '.$image_id.'
124;';
125  $row = mysql_fetch_array(pwg_query($query));
126  $query = '
127UPDATE '.IMAGES_TABLE.'
128  SET average_rate = '.$row['average_rate'].'
129  WHERE id = '.$image_id.'
130;';
131  pwg_query($query);
132}
133
134?>
Note: See TracBrowser for help on using the repository browser.