2019-09-05 17:07:41 +00:00
< ? php
require_once 'config.php' ;
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 ;
}
?>
< ? php require_once " header.php " ?>
< ? php
if ( ! isset ( $_SESSION [ " role " ]) or ( $_SESSION [ " role " ] != " PARTICIPANT " && $_SESSION [ " role " ] != " ENCADRANT " )) {
?>
< h2 > Vous devez être participant ou encadrant pour pouvoir rejoindre une équipe .</ h2 >
< ? php } else if ( isset ( $access_code )) { ?>
Vous avez bien rejoint l ' équipe < ? php echo $data [ " name " ] ?> !
< ? php } else if ( $_SESSION [ " team_id " ] != NULL ) { ?>
< h2 > Vous êtes déjà dans une équipe .</ h2 >
< ? php } else { ?>
< ? php if ( isset ( $error_message ) && $error_message ) echo " <h2>Erreur : " . $error_message . " </h2> " ; ?>
< form method = " POST " >
< input type = " hidden " name = " submitted " value = " true " />
< table style = " width: 100%; " >
< tbody >
< tr >
< td style = " width: 30%; " >
< label for = " access_code " > Code d ' accès :</ label >
</ td >
< td style = " width: 70%; " >
< input style = " width: 100%; " type = " text " id = " access_code " name = " access_code " />
</ td >
</ tr >
< tr >
< td colspan = " 2 " >
< input style = " width: 100%; " type = " submit " value = " Rejoindre l'équipe " />
</ td >
</ tr >
</ tbody >
</ table >
</ form >
< ? php require_once " footer.php " ?>
< ? php } ?>