<?php
?><h2>Top Voters - Nexora</h2><?php
$realm=class_exists('NexoraRealm')?NexoraRealm::active():'legacy';
$database=Connection::Database('Platform',$realm);
$currentMonth=(int)date('m'); $currentYear=(int)date('Y');
$ts1=strtotime(sprintf('%04d-%02d-01 00:00:00',$currentYear,$currentMonth));
$ts2=strtotime('+1 month',$ts1);
$voteLogs=$database->query_fetch('SELECT TOP 100 user_id,MAX(account_username) AS account_username,COUNT(*) AS totalvotes,SUM(ISNULL(points_reward,0)) AS points FROM '.WEBENGINE_VOTE_LOGS.' WHERE realm=? AND timestamp BETWEEN ? AND ? GROUP BY user_id ORDER BY totalvotes DESC',array($realm,$ts1,$ts2));
echo '<p><strong>Contexto:</strong> '.htmlspecialchars(ucfirst($realm),ENT_QUOTES,'UTF-8').'</p>';
if($voteLogs&&is_array($voteLogs)) {
 echo '<table class="table table-condensed table-hover"><tr><th>#</th><th>Cuenta</th><th>Votos</th><th>Vote Points</th><th>Creditos convertidos</th></tr>';
 foreach($voteLogs as $key=>$thisVote) {
   $converted=$database->query_fetch_single('SELECT ISNULL(SUM(credits_granted),0) AS total FROM NEXORA_VOTE_CONVERSIONS WHERE realm=? AND user_id=? AND created_at>=DATEFROMPARTS(?,?,1) AND created_at<DATEADD(MONTH,1,DATEFROMPARTS(?,?,1))',array($realm,(int)$thisVote['user_id'],$currentYear,$currentMonth,$currentYear,$currentMonth));
   $name=check_value($thisVote['account_username'])?$thisVote['account_username']:'#'.(int)$thisVote['user_id'];
   echo '<tr><td>'.($key+1).'</td><td>'.htmlspecialchars($name,ENT_QUOTES,'UTF-8').'</td><td>'.(int)$thisVote['totalvotes'].'</td><td>'.(int)$thisVote['points'].'</td><td>'.(is_array($converted)?(int)$converted['total']:0).'</td></tr>';
 }
 echo '</table>';
} else message('error','No hay votos registrados en este reino durante el mes actual.');
?>
