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