2019-09-06 11:48:50 +00:00
< ? php
2019-09-07 12:31:28 +00:00
$FINAL = Tournament :: getFinalTournament ();
2019-09-06 23:33:05 +00:00
2019-09-07 12:31:28 +00:00
function loadUserValues () {
2019-09-06 23:33:05 +00:00
$_SESSION [ " user " ] = $_SESSION [ " team " ] = $_SESSION [ " tournament " ] = null ;
unset ( $_SESSION [ " user " ]);
unset ( $_SESSION [ " role " ]);
unset ( $_SESSION [ " team " ]);
unset ( $_SESSION [ " tournament " ]);
2019-09-06 11:48:50 +00:00
if ( isset ( $_SESSION [ " user_id " ])) {
2019-09-06 23:33:05 +00:00
$user = $_SESSION [ " user " ] = User :: fromId ( $_SESSION [ " user_id " ]);
$_SESSION [ " role " ] = $user -> getRole ();
2019-09-06 11:48:50 +00:00
2019-09-06 23:33:05 +00:00
if ( $user -> getTeamId () !== null ) {
$team = $_SESSION [ " team " ] = Team :: fromId ( $user -> getTeamId ());
$_SESSION [ " tournament " ] = Tournament :: fromId ( $team -> getTournamentId ());
2019-09-06 11:48:50 +00:00
}
2019-09-06 23:33:05 +00:00
if ( isset ( $_GET [ " be-admin " ])) {
quitTeam ();
$user -> setRole ( Role :: ADMIN );
exit ();
2019-09-06 11:48:50 +00:00
}
2019-09-06 23:33:05 +00:00
if ( isset ( $_GET [ " be-organizer " ])) {
quitTeam ();
$user -> setRole ( Role :: ORGANIZER );
exit ();
}
2019-09-06 11:48:50 +00:00
2019-09-06 23:33:05 +00:00
if ( isset ( $_GET [ " be-participant " ])) {
quitTeam ();
$user -> setRole ( Role :: PARTICIPANT );
exit ();
}
2019-09-06 11:48:50 +00:00
2019-09-06 23:33:05 +00:00
if ( isset ( $_GET [ " be-encadrant " ])) {
quitTeam ();
$user -> setRole ( Role :: ENCADRANT );
exit ();
}
2019-09-06 11:48:50 +00:00
}
}
function quitTeam () {
global $DB , $URL_BASE ;
2019-09-06 23:33:05 +00:00
header ( " Location: $URL_BASE " );
/** @var User $user */
$user = $_SESSION [ " user " ];
$user_id = $user -> getId ();
$role = $user -> getRole ();
if ( $role == Role :: ADMIN || $role == Role :: ORGANIZER )
2019-09-06 11:48:50 +00:00
return ;
2019-09-06 23:33:05 +00:00
for ( $i = 1 ; $i <= ( $role == Role :: ENCADRANT ? 6 : 2 ); ++ $i )
2019-09-06 11:48:50 +00:00
/** @noinspection SqlResolve */
2019-09-06 23:33:05 +00:00
$DB -> exec ( " UPDATE `teams` SET ` " . strtolower ( Role :: getName ( $role )) . " _ $i ` = NULL WHERE ` " . strtolower ( Role :: getName ( $role )) . " _ $i ` = $user_id ; " );
$user -> setTeamId ( null );
2019-09-06 11:48:50 +00:00
$DB -> exec ( " UPDATE `teams` SET `encadrant_1` = `encadrant_2`, `encadrant_2` = NULL WHERE `encadrant_1` IS NULL; " );
for ( $i = 1 ; $i <= 5 ; ++ $i ) {
/** @noinspection SqlResolve */
$DB -> exec ( " UPDATE `teams` SET `participant_ $i ` = `participant_ " . strval ( $i + 1 ) . " `, `participant_ " . strval ( $i + 1 ) . " ` = NULL WHERE `participant_ $i ` IS NULL; " );
}
2019-09-06 23:33:05 +00:00
$req = $DB -> query ( " SELECT `file_id` FROM `documents` WHERE `user` = $user_id ; " );
2019-09-06 11:48:50 +00:00
while (( $data = $req -> fetch ()) !== false )
unlink ( " $URL_BASE /files/ " . $data [ " file_id " ]);
2019-09-06 23:33:05 +00:00
$DB -> exec ( " DELETE FROM `documents` WHERE `user` = $user_id ; " );
2019-09-06 11:48:50 +00:00
if ( $DB -> exec ( " DELETE FROM `teams` WHERE `encadrant_1` IS NULL AND `participant_1` IS NULL; " ) > 0 ) {
2019-09-06 23:33:05 +00:00
$team_id = $user -> getTeamId ();
$req = $DB -> query ( " SELECT `file_id` FROM `solutions` WHERE `team` = $team_id ; " );
2019-09-06 11:48:50 +00:00
while (( $data = $req -> fetch ()) !== false )
unlink ( " $URL_BASE /files/ " . $data [ " file_id " ]);
2019-09-06 23:33:05 +00:00
$DB -> exec ( " DELETE FROM `solutions` WHERE `team` = $team_id ; " );
2019-09-06 11:48:50 +00:00
2019-09-06 23:33:05 +00:00
$req = $DB -> query ( " SELECT `file_id` FROM `syntheses` WHERE `team` = $team_id ; " );
2019-09-06 11:48:50 +00:00
while (( $data = $req -> fetch ()) !== false )
unlink ( " $URL_BASE /files/ " . $data [ " file_id " ]);
2019-09-06 23:33:05 +00:00
$DB -> exec ( " DELETE FROM `syntheses` WHERE `team` = $team_id ; " );
2019-09-06 11:48:50 +00:00
}
2019-09-06 23:33:05 +00:00
$_SESSION [ " team " ] = null ;
unset ( $_SESSION [ " team " ]);
2019-09-07 13:51:16 +00:00
}
2019-09-07 14:37:00 +00:00
function userExists ( $email ) {
global $DB , $YEAR ;
2019-09-07 14:58:23 +00:00
$req = $DB -> prepare ( " SELECT `id` FROM `users` WHERE `email` = ? AND `year` = ' $YEAR '; " );
2019-09-07 14:37:00 +00:00
$req -> execute ([ $email ]);
2019-09-07 14:58:23 +00:00
return $req -> fetch ();
}
function teamExists ( $name ) {
global $DB , $YEAR ;
$req = $DB -> prepare ( " SELECT `id` FROM `teams` WHERE `name` = ? AND `year` = ' $YEAR '; " );
$req -> execute ([ $name ]);
return $req -> fetch ();
}
function trigramExists ( $trigram ) {
global $DB , $YEAR ;
$req = $DB -> prepare ( " SELECT `id` FROM `teams` WHERE `trigram` = ? AND `year` = ' $YEAR '; " );
$req -> execute ([ $trigram ]);
return $req -> fetch ();
2019-09-07 16:08:40 +00:00
}
function tournamentExists ( $name ) {
global $DB , $YEAR ;
$req = $DB -> prepare ( " SELECT `id` FROM `tournaments` WHERE `name` = ? AND `year` = ' $YEAR '; " );
$req -> execute ([ $name ]);
return $req -> fetch ();
2019-09-07 23:35:05 +00:00
}
function printDocuments ( $documents ) {
global $URL_BASE ;
foreach ( $documents as $document ) {
$file_id = $document -> getFileId ();
$user = User :: fromId ( $document -> getUserId ());
$surname = $user -> getSurname ();
$first_name = $user -> getFirstName ();
$name = DocumentType :: getTranslatedName ( $document -> getType ());
$version = $document -> getVersion ();
echo " $name de $first_name $surname (version $version ) : <a href= \" $URL_BASE /file/ $file_id\ " > Télécharger </ a >< br /> " ;
}
2019-09-06 11:48:50 +00:00
}