1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-27 05:33:03 +00:00
plateforme-tfjm2/server_files/controllers/solutions_orga.php
2019-09-06 13:48:50 +02:00

73 lines
3.0 KiB
PHP

<?php
require_once "../config.php";
if (!isset($_SESSION["role"]) || $_SESSION["role"] != "ADMIN" && $_SESSION["role"] != "ORGANIZER")
require_once "../403.php";
$req = $DB->query("SELECT `tournaments`.`id`, `name` FROM `tournaments` JOIN `organizers` ON `tournament` = `tournaments`.`id` WHERE "
. ($_SESSION["role"] == "ADMIN" ? "" : "`organizer` = '" . $_SESSION["user_id"] . "' AND ")
. "`year` = $YEAR GROUP BY `tournament` ORDER BY `name`;");
if (isset($_POST["download_zip"])) {
$id = $_POST["tournament"];
$tournament_name = $_POST["tournament_name"];
$files_req = $DB->query("SELECT *, COUNT(`problem`) AS `version` FROM `solutions` WHERE `tournament` = '$id' GROUP BY `team`, `problem` ORDER BY `team`, `problem`, `uploaded_at` DESC;");
$zip = new ZipArchive();
$temp = tempnam("tmp", "tfjm-");
if ($zip->open($temp, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
while (($data_file = $files_req->fetch()) !== false) {
$file_id = $data_file["file_id"];
$problem = $data_file["problem"];
$version = $data_file["version"];
$team_id = $data_file["team"];
$team_data = $DB->query("SELECT `name`, `trigram` FROM `teams` WHERE `id` = '$team_id' AND `year` = $YEAR;")->fetch();
$team_name = $team_data["name"];
$team_trigram = $team_data["trigram"];
$zip->addFile("$LOCAL_PATH/files/$file_id", "Problème $problem $team_trigram.pdf");
}
$zip->close();
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"Solutions du tournoi de $tournament_name.zip\"");
header("Content-Length: " . strval(filesize($temp)));
readfile($temp);
exit();
}
require_once "../views/header.php";
while (($data_tournament = $req->fetch()) !== false) {
echo "<h1>Tournoi de " . $data_tournament["name"] . "</h1>\n";
$id = $data_tournament["id"];
$files_req = $DB->query("SELECT *, COUNT(`problem`) AS `version` FROM `solutions` WHERE `tournament` = '$id' GROUP BY `team` ORDER BY `team`, `problem`, `uploaded_at` DESC;");
while (($data_file = $files_req->fetch()) !== false) {
$file_id = $data_file["file_id"];
$problem = $data_file["problem"];
$version = $data_file["version"];
$team_id = $data_file["team"];
$team_data = $DB->query("SELECT `name`, `trigram` FROM `teams` WHERE `id` = '$team_id' AND `year` = $YEAR;")->fetch();
$team_name = $team_data["name"];
$team_trigram = $team_data["trigram"];
echo "Problème n°$problem de l'équipe $team_name ($team_trigram), version $version : <a href=\"$URL_BASE/file/$file_id\">Télécharger</a><br />";
}
echo "<form method=\"POST\">\n";
echo "<input type=\"hidden\" name=\"tournament\" value=\"$id\" />\n";
echo "<input type=\"hidden\" name=\"tournament_name\" value=\"" . $data_tournament["name"] . "\" />\n";
echo "<input style=\"width: 100%\" type=\"submit\" name=\"download_zip\" value=\"Télécharger l'archive\" />\n";
echo "</form><hr />\n";
}
require_once "../views/footer.php";