<?php
/**
 * Mola más - vota1r.php
 * 
 * @author    Bartolomé Sintes Marco <bartolome.sintes+mclibre@gmail.com>
 * @copyright 2010 Bartolomé Sintes Marco
 * @license   http://www.gnu.org/licenses/agpl.txt AGPL 3 or later
 * @version   2010-06-18
 * @link      http://www.mclibre.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

include('funciones.php');
$db = conectaDb();
cabecera('Votar términos', CABECERA_SIN_CURSOR);

$consulta = "SELECT COUNT(*) FROM $dbTerminos";
$result = $db->query($consulta);
$registros = $result->fetchColumn();
if (!$result) {
    print "<p>Error en la consulta.</p>\n";
} elseif ($registros==0) {
    print "<p>No se ha creado todavía ningún registro.</p>\n";
} else {
    $consulta = "SELECT sum(votos) as total FROM $dbTerminos";
    $result = $db->query($consulta);
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";
    } else {
        $totalVotos = $result->fetchColumn();
        $mediaVotos = $totalVotos/$registros;
    }
    $consulta = "SELECT * FROM $dbTerminos
        ORDER BY termino";
    $result = $db->query($consulta);
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";
    } else {
        print "<p>Términos (con un tamaño proporcional a sus votos). "
            ."Haga clic en un término para votar por él:</p>\n"
            ."<p class=\"listado\">\n";
        foreach ($result as $valor) {
            $tamanyo = TAM_MIN_LISTADO+round($valor['votos']/$mediaVotos*TAM_MED_LISTADO, 1);
            print "  <a href=\"votar2.php?id=$valor[id]\"><span style=\"font-size:{$tamanyo}em\">$valor[termino]</span></a>&nbsp;&nbsp;\n";
        }
        print "</p>\n";
    }
}

$db = NULL;
pie();
?>
