1<?php
2/***************************************************************************
3* copyright            : (C) 2001-2003 Advanced Internet Designs Inc.
4* email                : forum@prohost.org
5* $Id: ratethread.php.t 21069 2010-03-25 22:47:48Z Caeies $
6*
7* This program is free software; you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the
9* Free Software Foundation; either version 2 of the License, or
10* (at your option) any later version.
11***************************************************************************/
12
13/*{PRE_HTML_PHP}*/
14/*{POST_HTML_PHP}*/
15
16	if (isset($_POST['rate_thread_id'], $_POST['sel_vote'])) {
17		$th = (int) $_POST['rate_thread_id'];
18		$rt = (int) $_POST['sel_vote'];
19
20		/* determine if the user has permission to rate the thread */
21		if (!q_singleval('SELECT t.id
22				FROM {SQL_TABLE_PREFIX}thread t
23				LEFT JOIN {SQL_TABLE_PREFIX}mod m ON t.forum_id=m.forum_id AND m.user_id='._uid.'
24				INNER JOIN {SQL_TABLE_PREFIX}group_cache g1 ON g1.user_id='.(_uid ? 2147483647 : 0).' AND g1.resource_id=t.forum_id
25				'.(_uid ? ' LEFT JOIN {SQL_TABLE_PREFIX}group_cache g2 ON g2.user_id='._uid.' AND g2.resource_id=t.forum_id ' : '').'
26				WHERE t.id='.$th.($usr->users_opt & 1048576 ? '' : ' AND (m.id IS NOT NULL OR ((CASE WHEN g1.id IS NULL THEN g2.group_cache_opt ELSE g1.group_cache_opt END) & 1024) > 0)')  . ' LIMIT 1')) {
27			std_error('access');
28		}
29
30		if (db_li('INSERT INTO {SQL_TABLE_PREFIX}thread_rate_track (thread_id, user_id, stamp, rating) VALUES('.$th.', '._uid.', '.__request_timestamp__.', '.$rt.')', $ef)) {
31			$rt = db_saq('SELECT count(*), ROUND(AVG(rating)) FROM {SQL_TABLE_PREFIX}thread_rate_track WHERE thread_id='.$th);
32			q('UPDATE {SQL_TABLE_PREFIX}thread SET rating='.(int)$rt[1].', n_rating='.(int)$rt[0].' WHERE id='.$th);
33		}
34	}
35	check_return($usr->returnto);
36?>