/** * Rate a picture by the current user. * * @param int $image_id * @param float $rate * @return array as return by update_rating_score() */ functionrate_picture($image_id, $rate) { global $conf, $user;
if (!isset($rate) or !$conf['rate'] or !in_array($rate, $conf['rate_items'])) { returnfalse; }
if ($user_anonymous) { $save_anonymous_id = pwg_get_cookie_var('anonymous_rater', $anonymous_id);
if ($anonymous_id != $save_anonymous_id) { // client has changed his IP adress or he's trying to fool us $query = ' SELECT element_id FROM '.RATE_TABLE.' WHERE user_id = '.$user['id'].' AND anonymous_id = \''.$anonymous_id.'\' ;'; $already_there = array_from_query($query, 'element_id');
if (count($already_there) > 0) { $query = ' DELETE FROM '.RATE_TABLE.' WHERE user_id = '.$user['id'].' AND anonymous_id = \''.$save_anonymous_id.'\' AND element_id IN ('.implode(',', $already_there).') ;'; pwg_query($query); }
$query = ' UPDATE '.RATE_TABLE.' SET anonymous_id = \'' .$anonymous_id.'\' WHERE user_id = '.$user['id'].' AND anonymous_id = \'' . $save_anonymous_id.'\' ;'; pwg_query($query); } // end client changed ip
pwg_set_cookie_var('anonymous_rater', $anonymous_id); } // end anonymous user
$query = ' DELETE FROM '.RATE_TABLE.' WHERE element_id = '.$image_id.' AND user_id = '.$user['id'].' '; if ($user_anonymous) { $query.= ' AND anonymous_id = \''.$anonymous_id.'\''; } pwg_query($query); $query = ' INSERT INTO '.RATE_TABLE.' (user_id,anonymous_id,element_id,rate,date) VALUES (' .$user['id'].',' .'\''.$anonymous_id.'\',' .$image_id.',' .$rate .',NOW()) ;'; pwg_query($query);
return update_rating_score($image_id); }
该函数用于给图片评分,因此限制了分数$rate只能是$conf[‘rate_items’]中的值
1 2 3
//include/config_default.inc.php // rate_items: available rates for a picture $conf['rate_items'] = array(0,1,2,3,4,5);
1 2 3 4 5 6 7
//include/functions_rate.inc.php if (!isset($rate) or !$conf['rate'] or !in_array($rate, $conf['rate_items'])) { returnfalse; }