<?php


include_once './includes/database/MySQLDatabase.inc.php';
include_once './includes/tournaments/Tournament.inc.php';

include_once './includes/utils/MapperUtils.inc.php';

/**
 * This class allows for easy listings of tournaments.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class TournamentLister {


    public static function getAllTournaments() {
        return TournamentLister::getTournaments("");
    }

    public static function getTournamentsByDate($from, $to) {
        $from = DateFormatter::formatSQL($from);
        $to = DateFormatter::formatSQL($to);
        return TournamentLister::getTournaments("WHERE date BETWEEN '$from' AND '$to'");
    }

    public static function getTournament($name, $location, $date, $type, $comments) {
        $restriction = "WHERE date = '" . DateFormatter::formatSQL($date) . "'";
        $restriction .=" AND location = '" . $location . "' AND type = '" . $type . "'";
        return TournamentLister::getTournaments($restriction);
    }

    private static function getTournaments($restriction) {
        $sql = "SELECT * FROM tournaments $restriction ORDER BY date DESC;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        if ($result) {
            $result1 = array_map("mapIdentifierToTournament", $result);
            return $result1;
        } else {
            return array();
        }

    }
}
?>
