diff --git a/server_files/classes/User.php b/server_files/classes/User.php index 0fbb77b..4a4845f 100644 --- a/server_files/classes/User.php +++ b/server_files/classes/User.php @@ -16,6 +16,7 @@ class User private $confirm_email; private $forgotten_password; private $inscription_date; + private $receive_animath_mails; private function __construct() {} @@ -48,6 +49,21 @@ class User $user->fill($data); return $user; } + + public static function getAdmins() + { + global $DB, $YEAR; + $admins = []; + $req = $DB->query("SELECT * FROM `users` WHERE `year` = $YEAR;"); + + while (($data = $req->fetch()) !== false) { + $admin = new User(); + $admin->fill($data); + $admins[] = $admin; + } + + return $admins; + } private function fill($data) { @@ -65,6 +81,7 @@ class User $this->confirm_email = $data["confirm_email"]; $this->forgotten_password = $data["forgotten_password"]; $this->inscription_date = $data["inscription_date"]; + $this->receive_animath_mails = $data["receive_animath_mails"]; } public function getEmail() @@ -170,7 +187,6 @@ class User { global $DB; $this->role = $role; - /** @noinspection PhpUndefinedMethodInspection */ $DB->prepare("UPDATE `users` SET `role` = ? WHERE `id` = ?;")->execute([Role::getName($role), $this->getId()]); } @@ -215,6 +231,18 @@ class User return $this->inscription_date; } + public function doReceiveAnimathMails() + { + return $this->receive_animath_mails; + } + + public function setReceiveAnimathMails($receive_animath_mails) + { + global $DB; + $this->receive_animath_mails = $receive_animath_mails; + $DB->prepare("UPDATE `users` SET `receive_animath_mails` = ? WHERE `id` = ?;")->execute([$receive_animath_mails, $this->getId()]); + } + public function getAllDocuments($problem) { global $DB; diff --git a/server_files/classes/Video.php b/server_files/classes/Video.php index 25f0c52..7d46df2 100644 --- a/server_files/classes/Video.php +++ b/server_files/classes/Video.php @@ -3,11 +3,16 @@ class Video { + const NOT_CONTROLLED = 0; + const REJECTED = -1; + const ACCEPTED = 1; + private $id; private $team; private $problem; private $link; private $reason; + private $validation; private $uploaded_at; private $year; private $version; @@ -29,14 +34,16 @@ class Video return $video; } - public static function getVideos($reason, $problem, $team_id = -1) + public static function getVideos($reason, $problem, $validation_min = -1, $team_id = -1) { - global $DB; + global $DB, $YEAR; $req = $DB->query("SELECT * FROM `videos` AS `t1` " - . "INNER JOIN (SELECT `team`, `problem`, `reason`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `videos` GROUP BY `problem`, `reason`, `team`) `t2` " + . "INNER JOIN (SELECT `team`, `problem`, `reason`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `videos` " + . "WHERE `validation` >= $validation_min AND `year` = $YEAR GROUP BY `problem`, `reason`, `team`) `t2` " . "ON `t1`.`team` = `t2`.`team` AND `t1`.`reason` = `t2`.`reason` AND `t1`.`problem` = `t2`.`problem` " . "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`problem` = $problem AND `t1`.`reason` = '" . Reason::getName($reason) . "'" - . ($team_id >= 0 ? " AND `t1`.`team` = $team_id" : "") . " ORDER BY `t1`.`problem`, `t1`.`reason`;"); + . ($team_id >= 0 ? " AND `t1`.`team` = $team_id" : "") + . " AND `validation` >= $validation_min AND `year` = $YEAR ORDER BY `t1`.`problem`, `t1`.`reason`;"); $videos = []; @@ -52,10 +59,11 @@ class Video /** * @param int $reason * @param Team $team + * @param int $validation_min * @return Video|null */ - public static function getVideo($reason, Team $team) { - $videos = self::getVideos($reason, $team->getProblem(), $team->getId()); + public static function getVideo($reason, Team $team, $validation_min = -1) { + $videos = self::getVideos($reason, $team->getProblem(), $validation_min, $team->getId()); if (sizeof($videos) == 0) return null; else @@ -95,6 +103,18 @@ class Video return $this->reason; } + public function getValidation() + { + return $this->validation; + } + + public function setValidation($validation) + { + global $DB; + $this->validation = $validation; + $DB->exec("UPDATE `videos` SET `validation` = $validation WHERE `id` = $this->id;"); + } + public function getUploadedAt() { return $this->uploaded_at; diff --git a/server_files/controllers/envoyer_video.php b/server_files/controllers/envoyer_video.php index 7817ec1..4025ae1 100644 --- a/server_files/controllers/envoyer_video.php +++ b/server_files/controllers/envoyer_video.php @@ -22,17 +22,22 @@ if (isset($_POST["upload"])) { class NewVideo { - private $link; + public $link; + private $valid_link; + private $no_change; public function __construct($data) { - $this->link = $data["link"]; + foreach ($data as $key => $value) + $this->$key = $value; } public function makeVerifications() { ensure(preg_match("#(https?\:\/\/|)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?#", $this->link), "Ce n'est pas une URL valide."); $this->link = preg_replace('/^(?!https?:\/\/)/', 'https://', $this->link); + ensure($this->valid_link != null, "Vous devez confirmer que le lien est valide."); + ensure($this->no_change != null, "Vous devez vous engager à ne pas changer le contenu du lien et de la vidéo."); } public function uploadVideo() @@ -41,9 +46,12 @@ class NewVideo $req = $DB->prepare("INSERT INTO `videos`(`team`, `problem`, `link`, `reason`, `year`) VALUES (?, ?, ?, ?, ?)"); $req->execute([$team->getId(), $team->getProblem(), $this->link, "SOLUTION", $YEAR]); + + Mailer::sendNewVideo($this, $team); } } $video = Video::getVideo(Reason::SOLUTION, $team); +$video_validated = Video::getVideo(Reason::SOLUTION, $team, Video::ACCEPTED); require_once "server_files/views/envoyer_video.php"; \ No newline at end of file diff --git a/server_files/controllers/mon_compte.php b/server_files/controllers/mon_compte.php index f6740dc..a539efc 100644 --- a/server_files/controllers/mon_compte.php +++ b/server_files/controllers/mon_compte.php @@ -41,6 +41,7 @@ class MyAccount public $school; public $class; public $description; + public $receive_animath_mails; /** @var User */ private $user; @@ -65,6 +66,7 @@ class MyAccount ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide."); $this->email = strtolower($this->email); ensure($this->email == $this->user->getEmail() || !userExists($this->email), "Un compte existe déjà avec cette adresse e-mail."); + $this->receive_animath_mails = $this->receive_animath_mails != false; } public function updateAccount() @@ -74,6 +76,7 @@ class MyAccount $this->user->setSchool($this->school); $this->user->setClass($this->class); $this->user->setDescription($this->description); + $this->user->setReceiveAnimathMails($this->receive_animath_mails); if ($this->email != $this->user->getEmail()) { $this->user->setEmail($this->email); diff --git a/server_files/controllers/videos_solutions.php b/server_files/controllers/videos_solutions.php index 242b203..66a479e 100644 --- a/server_files/controllers/videos_solutions.php +++ b/server_files/controllers/videos_solutions.php @@ -3,9 +3,54 @@ if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::ADMIN) require_once "server_files/403.php"; +$has_error = false; +$error_message = null; + +if (isset($_POST["validate_video"])) { + $validate_video = new ValidateVideo($_POST); + try { + $validate_video->makeVerifications(); + $validate_video->validate(); + } + catch (AssertionError $e) { + $has_error = true; + $error_message = $e->getMessage(); + } +} + +class ValidateVideo +{ + private $video_id; + private $accept; + private $reject; + /** @var Video */ + private $video; + + public function __construct($data) + { + foreach ($data as $key => $value) + $this->$key = $value; + } + + public function makeVerifications() + { + $this->video = Video::fromId($this->video_id); + ensure($this->video != null, "La vidéo n'existe pas."); + ensure($this->video->getValidation() == 0, "La vidéo est déjà validée / rejetée."); + ensure(($this->accept == null || $this->reject == null) && $this->accept != $this->reject, "Impossible de déterminer s'il faut accepter ou non la vidéo."); + } + + public function validate() + { + $this->video->setValidation($this->accept ? 1 : -1); + Mailer::validateVideo($this->video); + } +} + $videos = []; for ($problem = 1; $problem <= 4; ++$problem) $videos[] = Video::getVideos(Reason::SOLUTION, $problem); + require_once "server_files/views/videos_solutions.php"; \ No newline at end of file diff --git a/server_files/model.php b/server_files/model.php index 4988ff1..d0e2207 100644 --- a/server_files/model.php +++ b/server_files/model.php @@ -53,7 +53,7 @@ function quitTeam() /** @noinspection SqlResolve */ $DB->exec("UPDATE `teams` SET `participant_$i` = NULL WHERE `participant_$i` = $user_id;"); else - $DB->exec("UPDATE `teams` SET `encadrant` = NULL WHERE `encadrant` = $user_id;"); + $DB->exec("UPDATE `teams` SET `encadrant` = NULL WHERE `encadrant` = $user_id;"); $user->setTeamId(null); for ($i = 1; $i <= 4; ++$i) { /** @noinspection SqlResolve */ @@ -167,4 +167,12 @@ function getZipFile($problem, $team_id = -1) $zip->close(); return $file_name; +} + +function displayVideo($link) +{ + if (preg_match("#(https?\://|)(www\.|)youtube\.com\/watch\?v=(.*)#", $link, $matches)) { + $code = $matches[3]; + echo "