[1084] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1903] | 5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[1084] | 6 | // +-----------------------------------------------------------------------+ |
---|
[1992] | 7 | // | file : $Id: functions_rate.inc.php 1992 2007-05-01 13:57:52Z rub $ |
---|
[1092] | 8 | // | last update : $Date: 2007-05-01 13:57:52 +0000 (Tue, 01 May 2007) $ |
---|
| 9 | // | last modifier : $Author: rub $ |
---|
| 10 | // | revision : $Revision: 1992 $ |
---|
[1084] | 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 | */ |
---|
[1092] | 34 | function rate_picture($image_id, $rate) |
---|
[1084] | 35 | { |
---|
[1092] | 36 | global $conf, $user; |
---|
[1084] | 37 | |
---|
[1092] | 38 | if (!isset($rate) |
---|
| 39 | or !$conf['rate'] |
---|
| 40 | or !in_array($rate, $conf['rate_items'])) |
---|
[1084] | 41 | { |
---|
[1092] | 42 | return; |
---|
[1084] | 43 | } |
---|
[1092] | 44 | |
---|
| 45 | $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true; |
---|
| 46 | |
---|
| 47 | if ($user_anonymous and !$conf['rate_anonymous']) |
---|
[1084] | 48 | { |
---|
[1092] | 49 | return; |
---|
[1084] | 50 | } |
---|
[1092] | 51 | |
---|
| 52 | if ($user_anonymous) |
---|
[1084] | 53 | { |
---|
[1092] | 54 | $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]); |
---|
| 55 | if (count($ip_components) > 3) |
---|
[1084] | 56 | { |
---|
[1092] | 57 | array_pop($ip_components); |
---|
| 58 | } |
---|
| 59 | $anonymous_id = implode ('.', $ip_components); |
---|
| 60 | |
---|
[1992] | 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 = ' |
---|
[1084] | 66 | SELECT element_id |
---|
[1992] | 67 | FROM '.RATE_TABLE.' |
---|
| 68 | WHERE user_id = '.$user['id'].' |
---|
| 69 | AND anonymous_id = \''.$anonymous_id.'\' |
---|
[1084] | 70 | ;'; |
---|
[1992] | 71 | $already_there = array_from_query($query, 'element_id'); |
---|
[1092] | 72 | |
---|
[1992] | 73 | if (count($already_there) > 0) |
---|
| 74 | { |
---|
| 75 | $query = ' |
---|
[1084] | 76 | DELETE |
---|
[1992] | 77 | FROM '.RATE_TABLE.' |
---|
| 78 | WHERE user_id = '.$user['id'].' |
---|
| 79 | AND anonymous_id = \''.$save_anonymous_id.'\' |
---|
| 80 | AND element_id NOT IN ('.implode(',', $already_there).') |
---|
[1084] | 81 | ;'; |
---|
[1992] | 82 | pwg_query($query); |
---|
| 83 | } |
---|
[1084] | 84 | |
---|
[1992] | 85 | $query = ' |
---|
[1084] | 86 | UPDATE |
---|
[1992] | 87 | '.RATE_TABLE.' |
---|
| 88 | SET anonymous_id = \'' .$anonymous_id.'\' |
---|
| 89 | WHERE user_id = '.$user['id'].' |
---|
| 90 | AND anonymous_id = \'' . $save_anonymous_id.'\' |
---|
[1084] | 91 | ;'; |
---|
[1992] | 92 | pwg_query($query); |
---|
| 93 | } // end client changed ip |
---|
[1084] | 94 | |
---|
[1992] | 95 | pwg_get_cookie_var('anonymous_rater', $anonymous_id); |
---|
[1092] | 96 | } // end anonymous user |
---|
[1992] | 97 | |
---|
[1092] | 98 | $query = ' |
---|
[1084] | 99 | DELETE |
---|
| 100 | FROM '.RATE_TABLE.' |
---|
| 101 | WHERE element_id = '.$image_id.' |
---|
[1092] | 102 | AND user_id = '.$user['id'].' |
---|
[1084] | 103 | '; |
---|
[1092] | 104 | if (isset($anonymous_id)) |
---|
| 105 | { |
---|
| 106 | $query.= ' AND anonymous_id = \''.$anonymous_id.'\''; |
---|
| 107 | } |
---|
| 108 | pwg_query($query); |
---|
| 109 | $query = ' |
---|
[1084] | 110 | INSERT |
---|
| 111 | INTO '.RATE_TABLE.' |
---|
| 112 | (user_id,anonymous_id,element_id,rate,date) |
---|
| 113 | VALUES |
---|
| 114 | (' |
---|
[1092] | 115 | .$user['id'].',' |
---|
| 116 | .(isset($anonymous_id) ? '\''.$anonymous_id.'\'' : "''").',' |
---|
| 117 | .$image_id.',' |
---|
| 118 | .$rate |
---|
| 119 | .',NOW()) |
---|
[1084] | 120 | ;'; |
---|
[1092] | 121 | pwg_query($query); |
---|
| 122 | |
---|
| 123 | // update of images.average_rate field |
---|
| 124 | $query = ' |
---|
[1084] | 125 | SELECT ROUND(AVG(rate),2) AS average_rate |
---|
| 126 | FROM '.RATE_TABLE.' |
---|
| 127 | WHERE element_id = '.$image_id.' |
---|
| 128 | ;'; |
---|
[1092] | 129 | $row = mysql_fetch_array(pwg_query($query)); |
---|
| 130 | $query = ' |
---|
[1084] | 131 | UPDATE '.IMAGES_TABLE.' |
---|
| 132 | SET average_rate = '.$row['average_rate'].' |
---|
| 133 | WHERE id = '.$image_id.' |
---|
| 134 | ;'; |
---|
[1092] | 135 | pwg_query($query); |
---|
[1084] | 136 | } |
---|
| 137 | |
---|
[1903] | 138 | ?> |
---|