Amélioration du code de la page "Notes de synthèse" (vue équipe)

This commit is contained in:
galaxyoyo 2019-09-10 01:02:13 +02:00
parent 44e3e3f639
commit cca62a99d0
3 changed files with 48 additions and 33 deletions

View File

@ -231,34 +231,34 @@ class DestType
public static function getTranslatedName($status) { public static function getTranslatedName($status) {
switch ($status) { switch ($status) {
case self::DEFENSEUR:
return "Défenseur";
case self::OPPOSANT: case self::OPPOSANT:
return "Opposant"; return "Opposant";
default: case self::RAPPORTEUR:
return "Rapporteur"; return "Rapporteur";
default:
return "Défenseur";
} }
} }
public static function getName($status) { public static function getName($status) {
switch ($status) { switch ($status) {
case self::DEFENSEUR:
return "DEFENSEUR";
case self::OPPOSANT: case self::OPPOSANT:
return "OPPOSANT"; return "OPPOSANT";
default: case self::RAPPORTEUR:
return "RAPPORTEUR"; return "RAPPORTEUR";
default:
return "DEFENSEUR";
} }
} }
public static function fromName($name) { public static function fromName($name) {
switch ($name) { switch ($name) {
case "DEFENSEUR":
return self::DEFENSEUR;
case "OPPOSANT": case "OPPOSANT":
return self::OPPOSANT; return self::OPPOSANT;
default: case "RAPPORTEUR":
return self::RAPPORTEUR; return self::RAPPORTEUR;
default:
return self::DEFENSEUR;
} }
} }
} }

View File

@ -54,6 +54,7 @@ class SaveSolution
public function saveSolution() public function saveSolution()
{ {
global $LOCAL_PATH, $DB, $team, $tournament, $FINAL; global $LOCAL_PATH, $DB, $team, $tournament, $FINAL;
do do
$id = genRandomPhrase(64); $id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id")); while (file_exists("$LOCAL_PATH/files/$id"));

View File

@ -11,7 +11,14 @@ $team = $_SESSION["team"];
$tournament = Tournament::fromId($team->getTournamentId()); $tournament = Tournament::fromId($team->getTournamentId());
if (isset($_POST["send_synthesis"])) { if (isset($_POST["send_synthesis"])) {
$error_message = saveSynthesis(); $save_synthesis = new SaveSynthesis();
try {
$save_synthesis->makeVerifications();
$save_synthesis->saveSynthesis();
} catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
} }
$syntheses = $tournament->getAllSyntheses($team->getId()); $syntheses = $tournament->getAllSyntheses($team->getId());
@ -19,36 +26,43 @@ $syntheses_final = null;
if ($team->isSelectedForFinal()) if ($team->isSelectedForFinal())
$syntheses_final = $FINAL->getAllSyntheses($team->getId()); $syntheses_final = $FINAL->getAllSyntheses($team->getId());
function saveSynthesis() { class SaveSynthesis
{
private $dest;
private $file;
public function __construct()
{
$this->file = $_FILES["document"];
$this->dest = DestType::fromName(strtoupper(htmlspecialchars($_POST["problem"])));
}
public function makeVerifications()
{
global $LOCAL_PATH;
ensure($this->dest != DestType::DEFENSEUR, "Le destinataire est invalide.");
ensure($this->file["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo.");
ensure(!$this->file["error"], "Une erreur est survenue.");
ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->file["tmp_name"]) == "application/pdf", "Le fichier doit être au format PDF.");
ensure(is_dir("$LOCAL_PATH/files") || mkdir("$LOCAL_PATH/files"), "Un problème est survenue dans l'envoi du fichier. Veuillez contacter l'administrateur du serveur.");
}
public function saveSynthesis()
{
global $LOCAL_PATH, $DB, $team, $tournament, $FINAL; global $LOCAL_PATH, $DB, $team, $tournament, $FINAL;
$dest = strtoupper(htmlspecialchars($_POST["dest"]));
if (!isset($dest) || ($dest != "OPPOSANT" && $dest != "RAPPORTEUR"))
return "Le destinataire est invalide.";
$file = $_FILES["synthese"];
if ($file["size"] > 5000000 || $file["error"])
return "Une erreur est survenue. Merci de vérifier que le fichier pèse moins que 5 Mo.";
if (finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file["tmp_name"]) != 'application/pdf')
return "Le fichier doit être au destmat PDF.";
if (!is_dir("$LOCAL_PATH/files") && !mkdir("$LOCAL_PATH/files"))
return "Les droits sont insuffisants. Veuillez contacter l'administrateur du serveur.";
do do
$id = genRandomPhrase(64); $id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id")); while (file_exists("$LOCAL_PATH/files/$id"));
if (!rename($file["tmp_name"], "$LOCAL_PATH/files/$id")) if (!rename($this->file["tmp_name"], "$LOCAL_PATH/files/$id"))
return "Une erreur est survenue lors de l'envoi du fichier."; throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
$req = $DB->prepare("INSERT INTO `syntheses`(`file_id`, `team`, `tournament`, `dest`) VALUES (?, ?, ?, ?);"); $req = $DB->prepare("INSERT INTO `syntheses`(`file_id`, `team`, `tournament`, `dest`) VALUES (?, ?, ?, ?);");
$req->execute([$id, $team->getId(), $team->isSelectedForFinal() ? $FINAL->getId() : $tournament->getId(), $dest]); $req->execute([$id, $team->getId(), $team->isSelectedForFinal() ? $FINAL->getId() : $tournament->getId(), $this->dest]);
return false; return false;
}
} }
require_once "server_files/views/syntheses.php"; require_once "server_files/views/syntheses.php";