function thank() {
global $userdata, $lang;
$mode = (string) $this->request['m'];
$attach_id = (int) $this->request['a'];
if(is_numeric($attach_id)) {
$result = array('attach_id' => $attach_id, 'error' => false);
// Thank!
if($mode == 'thank') {
$sql = "INSERT INTO ". BB_ATTACHMENTS_RATING ." (attach_id,user_id,thanked) VALUES ("
. $attach_id .",". $userdata['user_id'] .",1)on duplicate key update thanked=1";
if( DB()->sql_query($sql) ) {
$sql = "select sum(thanked) as c from ". BB_ATTACHMENTS_RATING ." where attach_id=". $attach_id;
if( $res = DB()->sql_query($sql) ) {
if ($row = DB()->sql_fetchrow($res)) {
$result['thanked'] = $row['c'];
$result['mode'] = $mode;
$result['list_button']=' (<span id="VL'.$attach_id.'"><a href="#torrent" onClick="thank(\'list\','.$attach_id.');">'. $lang['THANK_LIST'] .'</a></span>)';
$sql = "UPDATE ". BB_ATTACHMENTS_DESC ." SET thanks=". $row['c'] ." where attach_id=". $attach_id;
DB()->sql_query($sql);
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
}
// Thanks list
elseif($mode == 'list')
{
$sql = DB()->fetch_rowset("SELECT u.user_id, u.username, u.user_rank FROM ". BB_ATTACHMENTS_RATING ." r join ". BB_USERS ." u on u.user_id=r.user_id where r.thanked=1 and r.attach_id=". $attach_id);
$html = '';
foreach ($sql as $row)
{
if( $html ) $html .= ', ';
$html .= profile_url($row);
}
$result['list'] = $html;
$result['mode'] = $mode;
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
$this->response['message'] = $result;
}
function rate() {
global $userdata, $lang;
$attach_id = (int) $this->request['a'];
$rating = (int) $this->request['v'];
$result['error'] = false;
if(is_numeric($rating) && is_numeric($attach_id) && $rating>=1 && $rating<=5) {
$sql = "insert into ". BB_ATTACHMENTS_RATING ."(attach_id,user_id,rating)values("
. $attach_id .",". $userdata['user_id'] .",". $rating .")on duplicate key update rating=values(rating)";
if(DB()->sql_query($sql)) {
$sql = "select sum(rating) as r, count(*) as c from ". BB_ATTACHMENTS_RATING ." where rating>0 and attach_id=". $attach_id;
if($res = DB()->sql_query($sql)) {
if ($row = DB()->sql_fetchrow($res)) {
$result['attach_id'] = $attach_id;
$result['rating'] = round($row['r']/$row['c'],1);
$result['rating_count'] = $row['c'];
$result['your_rating'] = $lang['YOUR_VOTE'] .' '. $lang['RATING_'.$rating] .' '. $lang['VOTE_COUNTED'];
$sql = "update ". BB_ATTACHMENTS_DESC ." set rating_sum=". $row['r'] .", rating_count=". $row['c'] ." where attach_id=". $attach_id;
DB()->sql_query($sql);
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
}
else {
$result['error'] = true;
}
$this->response['message'] = $result;
}