2019-09-05 17:07:41 +00:00
< ? php
2019-09-06 23:33:05 +00:00
if ( isset ( $_SESSION [ " team " ]) || ! isset ( $_SESSION [ " user " ]) || ( $_SESSION [ " role " ] != Role :: PARTICIPANT && $_SESSION [ " role " ] != Role :: ENCADRANT ))
2019-09-07 11:42:36 +00:00
require_once " server_files/403.php " ;
2019-09-06 23:33:05 +00:00
2019-09-09 22:49:55 +00:00
$has_error = false ;
$error_message = null ;
2019-12-19 12:02:01 +00:00
if ( isset ( $_POST [ " join_team " ])) {
2019-09-09 22:49:55 +00:00
$join_team = new JoinTeam ( $_POST );
try {
$join_team -> makeVerifications ();
$join_team -> joinTeam ();
} catch ( AssertionError $e ) {
$has_error = true ;
$error_message = $e -> getMessage ();
}
2019-09-05 17:07:41 +00:00
}
2019-09-09 22:49:55 +00:00
class JoinTeam
{
private $access_code ;
private $team ;
private $min_null_index ;
public function __construct ( $data )
{
$this -> access_code = strtolower ( htmlspecialchars ( $data [ " access_code " ]));
$this -> team = Team :: fromAccessCode ( $this -> access_code );
}
public function makeVerifications ()
{
ensure ( preg_match ( " #[a-z0-9] { 6}# " , $this -> access_code ), " Le code d'accès doit comporter 6 caractères alphanumériques. " );
ensure ( $this -> team != null , " Ce code d'accès est invalide. " );
ensure ( $this -> team -> getValidationStatus () == ValidationStatus :: NOT_READY , " Cette équipe est déjà validée ou en cours de validation, vous ne pouvez pas la rejoindre. " );
2020-02-18 14:33:55 +00:00
for ( $i = 1 ; $i <= $_SESSION [ " role " ] == Role :: PARTICIPANT ? 6 : 3 ; ++ $i ) {
2019-09-09 22:49:55 +00:00
if (( $_SESSION [ " role " ] == Role :: PARTICIPANT ? $this -> team -> getParticipants ()[ $i - 1 ] : $this -> team -> getEncadrants ()[ $i - 1 ]) == NULL )
break ;
}
$this -> min_null_index = $i ;
2020-02-18 14:33:55 +00:00
ensure ( $_SESSION [ " role " ] == Role :: PARTICIPANT && $this -> min_null_index <= 6 || $_SESSION [ " role " ] == Role :: ENCADRANT && $this -> min_null_index <= 3 , " Il n'y a plus de place pour vous dans l'équipe. " );
2019-09-05 17:07:41 +00:00
}
2019-09-09 22:49:55 +00:00
public function joinTeam ()
{
2019-12-19 12:02:01 +00:00
global $team ;
2019-09-09 22:49:55 +00:00
$user = $_SESSION [ " user " ];
2019-09-06 23:33:05 +00:00
2019-09-09 22:49:55 +00:00
$user -> setTeamId ( $this -> team -> getId ());
2019-09-06 23:33:05 +00:00
2019-09-09 22:49:55 +00:00
if ( $_SESSION [ " role " ] == Role :: ENCADRANT )
$this -> team -> setEncadrant ( $this -> min_null_index , $user -> getId ());
else
$this -> team -> setParticipant ( $this -> min_null_index , $user -> getId ());
2019-09-05 17:07:41 +00:00
2019-12-19 12:02:01 +00:00
$team = $_SESSION [ " team " ] = $this -> team ;
2019-09-09 22:49:55 +00:00
$tournament = $_SESSION [ " tournament " ] = Tournament :: fromId ( $this -> team -> getTournamentId ());
Mailer :: sendJoinTeamMail ( $user , $this -> team , $tournament );
}
2019-09-05 17:07:41 +00:00
}
2019-09-07 11:42:36 +00:00
require_once " server_files/views/rejoindre_equipe.php " ;