2019-09-20 12:57:06 +00:00
< ? php
2019-09-21 18:39:40 +00:00
if ( ! isset ( $_SESSION [ " user_id " ]) || $_SESSION [ " role " ] != Role :: PARTICIPANT && $_SESSION [ " role " ] != Role :: ENCADRANT || Phase :: getCurrentPhase () != Phase :: PHASE2 )
2019-09-20 12:57:06 +00:00
require_once " server_files/403.php " ;
/** @var Team $team */
$team = $_SESSION [ " team " ];
if ( $team == null )
require_once " server_files/403.php " ;
$has_error = false ;
$error_message = null ;
if ( isset ( $_POST [ " give_questions " ])) {
2019-09-21 11:37:48 +00:00
$give_questions = new GiveQuestions ( $_POST , $_FILES );
2019-09-20 12:57:06 +00:00
try {
$give_questions -> makeVerifications ();
$give_questions -> giveQuestions ();
} catch ( AssertionError $e ) {
$has_error = true ;
$error_message = $e -> getMessage ();
}
}
class GiveQuestions
{
private $to ;
2019-09-21 10:21:42 +00:00
/**
* @ var Team $to_team
*/
2019-09-20 12:57:06 +00:00
private $to_team ;
private $question_1 ;
private $question_2 ;
private $question_3 ;
private $question_4 ;
private $question_5 ;
2019-09-21 10:21:42 +00:00
private $question_6 ;
2019-09-21 12:31:31 +00:00
private $questions ;
2019-09-21 11:37:48 +00:00
private $no_drawing ;
2019-09-21 12:31:31 +00:00
private $has_files ;
2019-09-21 11:37:48 +00:00
private $files ;
2019-09-20 12:57:06 +00:00
2019-09-21 11:37:48 +00:00
public function __construct ( $data , $files )
2019-09-20 12:57:06 +00:00
{
foreach ( $data as $key => $value ) {
$this -> $key = $value ;
}
2019-09-21 11:37:48 +00:00
$this -> files = [];
2019-09-21 12:31:31 +00:00
$this -> has_files = false ;
2019-09-21 11:37:48 +00:00
2019-09-21 12:31:31 +00:00
for ( $i = 1 ; $i <= 6 ; ++ $i ) {
if ( strlen ( $files [ " file_ $i " ][ " name " ]) > 0 ) {
$this -> files [] = $files [ " file_ $i " ];
$this -> has_files = true ;
}
else
$this -> files [] = null ;
}
$this -> questions = [ $this -> question_1 , $this -> question_2 , $this -> question_3 , $this -> question_4 , $this -> question_5 , $this -> question_6 ];
2019-09-20 12:57:06 +00:00
}
public function makeVerifications ()
{
2019-09-21 11:37:48 +00:00
global $LOCAL_PATH , $team ;
2019-09-21 10:21:42 +00:00
$this -> to_team = Team :: fromTrigram ( $this -> to );
2019-09-20 12:57:06 +00:00
ensure ( $this -> to_team , " L'équipe indiquée n'existe pas. " );
2019-09-21 10:21:42 +00:00
ensure ( $team -> getProblem () == $this -> to_team -> getProblem (), " Les équipes ne travaillent pas sur le même problème. " );
2019-09-21 11:37:48 +00:00
ensure ( $this -> question_1 != null && $this -> question_1 != " " && $this -> question_2 != null && $this -> question_2 != " " && $this -> question_3 != null && $this -> question_3 != " " ,
2019-09-21 10:21:42 +00:00
" Vous devez poser au moins 3 questions. " );
2019-09-21 12:31:31 +00:00
ensure ( ! $this -> has_files || $this -> no_drawing , " Vous devez confirmer ne pas avoir inclus de texte dans vos pièces jointes. " );
for ( $i = 3 ; $i < 6 ; ++ $i ) {
if ( $this -> questions [ $i ] == " " )
$this -> questions [ $i ] = null ;
}
2019-09-21 11:37:48 +00:00
for ( $i = 0 ; $i < 6 ; ++ $i ) {
ensure ( $this -> files [ $i ][ " size " ] <= 2e6 , " Le fichier doit peser moins que 2 Mo. " );
2019-09-21 12:31:31 +00:00
ensure ( ! $this -> files [ $i ][ " error " ], " Une erreur est survenue. Veuillez vérifier vos pièces jointes. Elles ne doivent pas peser plus de 2 Mo chacune. " );
2019-09-21 11:37:48 +00:00
}
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. " );
2019-09-20 12:57:06 +00:00
}
public function giveQuestions ()
{
2019-09-21 12:31:31 +00:00
global $LOCAL_PATH , $team ;
/** @var Question[] $questions */
$questions = Question :: getQuestions ( $team , $this -> to_team );
for ( $i = 0 ; $i < 6 ; ++ $i ) {
$question = $questions [ $i ];
if ( $question -> getQuestion () != $this -> questions [ $i ] && $question -> getAttachedFile () != null ) {
unlink ( " $LOCAL_PATH /files/ " . $question -> getAttachedFile ());
$question -> setAttachedFile ( null );
}
$question -> setQuestion ( $this -> questions [ $i ]);
if ( $this -> files [ $i ] != null ) {
2019-09-21 11:37:48 +00:00
do
2019-09-21 12:31:31 +00:00
$file_id = genRandomPhrase ( 64 );
while ( file_exists ( " $LOCAL_PATH /files/ $file_id " ));
2019-09-21 11:37:48 +00:00
2019-09-21 12:31:31 +00:00
if ( ! rename ( $this -> files [ $i ][ " tmp_name " ], " $LOCAL_PATH /files/ $file_id " ))
2019-09-21 11:37:48 +00:00
throw new AssertionError ( " Une erreur est survenue lors de l'envoi du fichier. " );
2019-09-21 12:31:31 +00:00
$question -> setAttachedFile ( $file_id );
2019-09-21 11:37:48 +00:00
}
}
2019-09-20 12:57:06 +00:00
}
}
/**
* @ var Team [] $receivers
* @ var Video [] $videos
* @ var Question [][] $questions
*/
$receivers = [ Team :: fromId ( $team -> getVideoTeamIds ()[ 0 ]), Team :: fromId ( $team -> getVideoTeamIds ()[ 1 ])];
$videos = [ Video :: getVideo ( Reason :: SOLUTION , $receivers [ 0 ], Video :: ACCEPTED ),
Video :: getVideo ( Reason :: SOLUTION , $receivers [ 1 ], Video :: ACCEPTED )];
$questions = [ Question :: getQuestions ( $team , $receivers [ 0 ]),
Question :: getQuestions ( $team , $receivers [ 1 ])];
require_once " server_files/views/poser_questions.php " ;