2019-09-05 17:07:41 +00:00
< ? php
2019-09-06 11:48:50 +00:00
require_once " ../config.php " ;
2019-09-05 17:07:41 +00:00
if ( isset ( $_POST [ " submitted " ])) {
$error_message = joinTeam ();
}
function joinTeam () {
global $DB , $YEAR , $MAIL_ADDRESS , $access_code , $data ;
if ( $_SESSION [ " team_id " ] != NULL )
return " Vous êtes déjà dans une équipe. " ;
$access_code = htmlspecialchars ( $_POST [ " access_code " ]);
if ( ! isset ( $access_code ) || strlen ( $access_code ) != 6 )
return " Le code d'accès doit comporter 6 caractères. " ;
$result = $DB -> query ( " SELECT * FROM `teams` WHERE `access_code` = ' " . $access_code . " ' AND `year` = ' $YEAR '; " );
if (( $data = $result -> fetch ()) === FALSE )
return " Ce code d'accès est invalide. " ;
if ( $_SESSION [ " role " ] != " PARTICIPANT " && $_SESSION [ " role " ] != " ENCADRANT " )
return " Seuls les participants et les encadrants peuvent rejoindre une équipe. " ;
if ( $data [ " validation_status " ] != " NOT_READY " )
return " Cette équipe est déjà en cours de validation ou validée, vous ne pouvez pas la rejoindre. " ;
for ( $i = 1 ; $i <= $_SESSION [ " role " ] == " PARTICIPANT " ? 6 : 2 ; ++ $i ) {
if ( $data [ strtolower ( $_SESSION [ " role " ]) . " _ " . strval ( $i )] == NULL )
break ;
}
if ( $_SESSION [ " role " ] == " PARTICIPANT " && $i == 7 || $_SESSION [ " role " ] == " ENCADRANT " && $i == 3 )
return " Il n'y a plus de place pour vous dans l'équipe. " ;
$DB -> prepare ( " UPDATE `users` SET `team_id` = ? WHERE `id` = " . $_SESSION [ " user_id " ] . " ; " ) -> execute ([ $data [ " id " ]]);
/** @noinspection SqlResolve */
$DB -> prepare ( " UPDATE `teams` SET ` " . strtolower ( $_SESSION [ " role " ]) . " _ " . strval ( $i ) . " ` = ? WHERE `id` = " . $data [ " id " ] . " ; " ) -> execute ([ $_SESSION [ " user_id " ]]);
$_SESSION [ " team_id " ] = $data [ " id " ];
$_SESSION [ " team_validation_status " ] = $data [ " validation_status " ];
$msg = " Bonjour " . $_SESSION [ " first_name " ] . " " . $_SESSION [ " surname " ] . " , \r \n \r \n " ;
$msg .= " Vous venez de rejoindre l'équipe « " . $data [ " name " ] . " » ( " . $data [ " trigram " ] . " ) pour le TFJM² de " . $data [ " name " ] . " et nous vous en remercions. \r \n \r \n " ;
$msg .= " Cordialement, \r \n \r \n L'organisation du TFJM² $YEAR " ;
mail ( $_SESSION [ " email " ], " Équipe rejointe TFJM² $YEAR " , $msg , " From: $MAIL_ADDRESS\r\n " );
return false ;
}
2019-09-06 11:48:50 +00:00
require_once " ../views/header.php " ;
require_once " ../views/rejoindre_equipe.php " ;
require_once " ../views/footer.php " ;