plateforme-corres2math/server_files/controllers/commenter_echange.php

77 lines
2.5 KiB
PHP

<?php
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] != Role::PARTICIPANT && $_SESSION["role"] != Role::ENCADRANT || Phase::getCurrentPhase() != Phase::PHASE4)
require_once "server_files/403.php";
/**
* @var User $user
* @var Team $team
*/
$user = $_SESSION["user"];
$team = $user->getRole() == Role::PARTICIPANT ? $_SESSION["team"] : Team::fromTrigram($_GET["trigram"]);
if ($team == null)
require_once "server_files/404.php";
if ($user->getRole() == Role::ENCADRANT && $team->getEncadrantId() != $user->getId() || $team->getValidationStatus() != ValidationStatus::VALIDATED)
require_once "server_files/403.php";
if (isset($_POST["upload_answer"])) {
$new_answer = new NewAnswer($_POST);
try {
$new_answer->makeVerifications();
$new_answer->uploadVideo();
} catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
class NewAnswer
{
public $link;
public $team;
private $valid_link;
private $no_change;
public function __construct($data)
{
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(preg_match("#[1|2]#", $this->team), "L'équipe n'a pas été trouvée.");
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()
{
global $DB, $YEAR, $team;
$req = $DB->prepare("INSERT INTO `videos`(`team`, `problem`, `link`, `reason`, `year`) VALUES (?, ?, ?, ?, ?)");
$req->execute([$team->getId(), $team->getProblem(), $this->link, "ANSWER" . $this->team, $YEAR]);
Mailer::sendNewAnswer($this, $team);
}
}
$team1 = Team::fromId($team->getVideoTeamIds()[0]);
$sol1 = Video::getVideo(Reason::SOLUTION, $team1);
$answer1 = Video::getVideo(Reason::ANSWER1, $team);
$answer1_validated = Video::getVideo(Reason::ANSWER1, $team, Video::ACCEPTED);
$team2 = Team::fromId($team->getVideoTeamIds()[1]);
$sol2 = Video::getVideo(Reason::SOLUTION, $team2);
$answer2 = Video::getVideo(Reason::ANSWER2, $team);
$answer2_validated = Video::getVideo(Reason::ANSWER2, $team, Video::ACCEPTED);
$teams = [$team1, $team2];
$sols = [$sol1, $sol2];
$answers = [$answer1, $answer2];
$answers_validated = [$answer1_validated, $answer2_validated];
require_once "server_files/views/commenter_echange.php";