<?php
	//	топ пользователей
	
function users_top($options){
global $MAIN_USER,$MAIN_DB;
 	if ( $options=='week' ){
		$time = (time()-(60*60*24*7));
 	}else{
 		$time = (time()-(60*60*24*31));
 	}

		
	$sql = "SELECT
			 	SUM(points) AS points,
			 	u.u_interests,
			 	u.u_login 	AS profile_url,
			 	u.u_avatar 	AS avatar_url,
			 	u.u_points  AS rank_img,
			 	u.u_login,u.u_name,u.u_points 
			FROM 
		  	".$MAIN_DB -> prefix("users_points_log")." l
		  	LEFT JOIN
		  	".$MAIN_DB -> prefix("users")." u
		  	USING(u_id)
		 	WHERE u_active='1' AND g_id=2 AND l.time>$time AND points>0 GROUP BY u_id ORDER BY SUM(points) DESC LIMIT 0,3";
	$u = new u_query('u');
	$u -> set_sql($sql);
	$u -> get(false,false);
  	$rows = array();
 	while( $row =  $u -> row() ){
		if ( $row['points']<=0 ){
			continue;
		}
		$row['u_interests']= sys_in_html($row['u_interests'] );
		$rows[]= arr_to_obj($row);
  	}
  	
  	
   	$t = new PHPTAL( false );
  	if ( $options=='week' ){
   		$t -> setSource(TMPL_USER_TOP_WEEK);
  	}else{
  		$t -> setSource(TMPL_USER_TOP_MONTH);
  	}
  	$t -> rows = $rows;
  	
  	 		
  	return  $t -> execute();
}

define("TMPL_USER_TOP_WEEK",
<<<TMPL_USER_TOP_WEEK
<h2>Нужно дописать шаблон</h2>
<h2><a class="h_link" href="/users/top.html"><span></span></a></h2>
<ul class="top_users">
<tal:block tal:condition="exists:rows" tal:repeat="r rows" >
	<li>
		<div class="tu_content_wrap">
			<a href="/show-user/{r users.u_login}">
				<img src="{r users.u_avatar}" title="{r users.u_login}" width="88" height="88" alt="{r users.u_login}">
			</a>
			<div class="tu_content">
				<a class="b_link" href="/show-user/{r users.u_login}">{r users.u_name}</a>
				{if exists_u_interests}<p>	{u_interests}</p>{endif}
			</div>
			<div class="stars_rate">
				<img src="/source/modules/ajax/tmp/{r users.u_rank_img}"  width="86" height="15">
				<strong tal:content="row/points">+123</strong>
			</div>
		</div>
	</li>
</tal:block>
</ul>
TMPL_USER_TOP_WEEK
);

define("TMPL_USER_TOP_MONTH",
<<<TMPL_USER_TOP_MONTH
<div class="top_users_wrap">
	<h2><a class="h_link" href="/users/top.html"><span></span></a></h2>
	<table class="top_users" cellpadding="0" cellspacing="0">
	<tr>
		<tal:block tal:condition="exists:rows" tal:repeat="r rows" >
		<th width="32%">
			<div class="tu_content_wrap">
				<a tal:attributes="href r/profile_url" >
					<img 
						tal:attributes="title r/u_login;src r/avatar_url;alt r/u_login"  
						
						
						width="88" height="88"
					 />
				</a>
				<div class="tu_content">
					<a class="b_link" tal:attributes="href r/profile_url" tal:content="r/u_name"></a>
					<p tal:condition="r/u_interests" tal:content="r/u_interests"></p>
				</div>
				<div class="stars_rate">
					<img tal:attributes="src r/rank_img"  width="86" height="15" />
					<strong tal:content="r/u_points">+10</strong>
				</div>
			</div>
		</th>
		<td>&nbsp;</td>
	</tal:block>
</tr>
</table>
</div>
TMPL_USER_TOP_MONTH
);
?>
