diff --git a/server_files/classes/Document.php b/server_files/classes/Document.php index 76622e9..47897be 100644 --- a/server_files/classes/Document.php +++ b/server_files/classes/Document.php @@ -271,6 +271,7 @@ class DocumentType const SOLUTION = 3; const SYNTHESIS = 4; const SCHOLARSHIP = 5; + const MOTIVATION_LETTER = 6; public static function getTranslatedName($type) { switch ($type) { @@ -282,6 +283,8 @@ class DocumentType return "Fiche sanitaire"; case self::SCHOLARSHIP: return "Notification de bourse"; + case self::MOTIVATION_LETTER: + return "Lettre de motivation"; case self::SOLUTION: return "Solution"; default: @@ -299,6 +302,8 @@ class DocumentType return "SANITARY_PLUG"; case self::SCHOLARSHIP: return "SCHOLARSHIP"; + case self::MOTIVATION_LETTER: + return "MOTIVATION_LETTER"; case self::SOLUTION: return "SOLUTION"; default: @@ -316,6 +321,8 @@ class DocumentType return self::SANITARY_PLUG; case "SCHOLARSHIP": return self::SCHOLARSHIP; + case "MOTIVATION_LETTER": + return self::MOTIVATION_LETTER; case "SOLUTION": return self::SOLUTION; default: diff --git a/server_files/classes/User.php b/server_files/classes/User.php index 96b310f..161195b 100644 --- a/server_files/classes/User.php +++ b/server_files/classes/User.php @@ -435,6 +435,17 @@ class User while (($data = $req->fetch()) !== false) $docs[] = Document::fromData($data); + if ($this->team_id > 0) { + $req = $DB->query("SELECT * FROM `documents` AS `t1` " + . "INNER JOIN (SELECT `user`, `type`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `tournament`, `type`, `user`) `t2` " + . "ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`tournament` = `t2`.`tournament` " + . "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $tournament_id AND `t1`.`team` = $this->team_id " + . "AND `t1`.`type` = 'MOTIVATION_LETTER';"); + + while (($data = $req->fetch()) !== false) + $docs[] = Document::fromData($data); + } + return $docs; } diff --git a/server_files/controllers/mon_compte.php b/server_files/controllers/mon_compte.php index 4f709ca..219ded5 100644 --- a/server_files/controllers/mon_compte.php +++ b/server_files/controllers/mon_compte.php @@ -205,7 +205,8 @@ class SendDocument $req = $DB->prepare("INSERT INTO `documents`(`file_id`, `user`, `team`, `tournament`, `type`) VALUES (?, ?, ?, ?, ?);"); - $req->execute([$id, $_SESSION["user_id"], $_SESSION["team"]->getId(), $_SESSION["team"]->isSelectedForFinal() ? $FINAL->getId() : $_SESSION["team"]->getTournamentId(), $this->type]); + $req->execute([$id, $this->type == DocumentType::getName(DocumentType::MOTIVATION_LETTER) ? -1 : $_SESSION["user_id"], $_SESSION["team"]->getId(), + $_SESSION["team"]->isSelectedForFinal() ? $FINAL->getId() : $_SESSION["team"]->getTournamentId(), $this->type]); } } diff --git a/server_files/controllers/view_file.php b/server_files/controllers/view_file.php index 10d4fe4..f576f9e 100644 --- a/server_files/controllers/view_file.php +++ b/server_files/controllers/view_file.php @@ -48,11 +48,11 @@ if ($file !== null) { $user = User::fromId($file->getUserId()); $type = $file->getType(); - if (($_SESSION["role"] == Role::PARTICIPANT || $_SESSION["role"] == Role::ENCADRANT) && $user->getId() != $_SESSION["user_id"]) - require_once "server_files/403.php"; + if (($_SESSION["role"] == Role::PARTICIPANT || $_SESSION["role"] == Role::ENCADRANT)) { + if ($type != DocumentType::MOTIVATION_LETTER && $user->getId() != $_SESSION["user_id"] || $file->getTeamId() != $team->getId()) + require_once "server_files/403.php"; + } - $surname = $user->getSurname(); - $first_name = $user->getFirstName(); switch ($type) { case DocumentType::PARENTAL_CONSENT: $name = "Autorisation parentale"; @@ -67,7 +67,13 @@ if ($file !== null) { $name = "Notification de bourse"; break; } - $name .= " de $first_name $surname.pdf"; + if ($type == DocumentType::MOTIVATION_LETTER) + $name = "Lettre de motivation de l'équipe $trigram.pdf"; + else { + $surname = $user->getSurname(); + $first_name = $user->getFirstName(); + $name .= " de $first_name $surname.pdf"; + } } } else diff --git a/server_files/model.php b/server_files/model.php index d4122d1..8e94f4f 100644 --- a/server_files/model.php +++ b/server_files/model.php @@ -136,6 +136,7 @@ function canValidate(Team $team, Tournament $tournament) $d = $req->fetch(); $can_validate &= $d["version"] > 0; } + for ($i = 1; $i <= 6; ++$i) { if ($team->getParticipants()[$i] === NULL) continue; @@ -159,6 +160,11 @@ function canValidate(Team $team, Tournament $tournament) } } + $req = $DB->prepare("SELECT COUNT(*) AS `version` FROM `documents` WHERE `team` = ? AND `tournament` = ? AND `type` = ?;"); + $req->execute([$team->getId(), $tournament->getId(), "MOTIVATION_LETTER"]); + $d = $req->fetch(); + $can_validate &= $d["version"] > 0; + return $can_validate; } @@ -172,12 +178,20 @@ function printDocuments($documents) echo "