2019-09-09 23:48:52 +00:00
< ? php
function loadUserValues ()
{
2019-09-11 16:41:45 +00:00
$_SESSION [ " user " ] = $_SESSION [ " team " ] = null ;
2019-09-09 23:48:52 +00:00
unset ( $_SESSION [ " user " ]);
unset ( $_SESSION [ " role " ]);
unset ( $_SESSION [ " team " ]);
if ( isset ( $_SESSION [ " user_id " ])) {
$user = $_SESSION [ " user " ] = User :: fromId ( $_SESSION [ " user_id " ]);
$_SESSION [ " role " ] = $user -> getRole ();
2019-09-10 23:17:05 +00:00
if ( $user -> getTeamId () !== null )
$_SESSION [ " team " ] = Team :: fromId ( $user -> getTeamId ());
2019-09-09 23:48:52 +00:00
if ( isset ( $_GET [ " be-admin " ])) {
quitTeam ();
$user -> setRole ( Role :: ADMIN );
exit ();
}
if ( isset ( $_GET [ " be-participant " ])) {
quitTeam ();
$user -> setRole ( Role :: PARTICIPANT );
exit ();
}
if ( isset ( $_GET [ " be-encadrant " ])) {
quitTeam ();
$user -> setRole ( Role :: ENCADRANT );
exit ();
}
}
}
function quitTeam ()
{
global $DB , $URL_BASE ;
header ( " Location: $URL_BASE " );
/** @var User $user */
$user = $_SESSION [ " user " ];
$user_id = $user -> getId ();
$role = $user -> getRole ();
2019-09-11 16:41:45 +00:00
if ( $role == Role :: ADMIN )
2019-09-09 23:48:52 +00:00
return ;
2019-09-11 16:41:45 +00:00
if ( $role == Role :: PARTICIPANT )
2019-09-12 12:11:59 +00:00
for ( $i = 1 ; $i <= 5 ; ++ $i )
2019-09-11 16:41:45 +00:00
/** @noinspection SqlResolve */
$DB -> exec ( " UPDATE `teams` SET `participant_ $i ` = NULL WHERE `participant_ $i ` = $user_id ; " );
else
$DB -> exec ( " UPDATE `teams` SET `encadrant` = NULL WHERE `encadrant` = $user_id ; " );
2019-09-09 23:48:52 +00:00
$user -> setTeamId ( null );
2019-09-12 13:35:16 +00:00
for ( $i = 1 ; $i <= 4 ; ++ $i ) {
2019-09-09 23:48:52 +00:00
/** @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-12 14:01:40 +00:00
$DB -> exec ( " DELETE FROM `teams` WHERE `encadrant` IS NULL OR `participant_1` IS NULL; " );
2019-09-09 23:48:52 +00:00
$req = $DB -> query ( " SELECT `file_id` FROM `documents` WHERE `user` = $user_id ; " );
while (( $data = $req -> fetch ()) !== false )
unlink ( " $URL_BASE /files/ " . $data [ " file_id " ]);
$DB -> exec ( " DELETE FROM `documents` WHERE `user` = $user_id ; " );
$_SESSION [ " team " ] = null ;
unset ( $_SESSION [ " team " ]);
}
function userExists ( $email )
{
global $DB , $YEAR ;
$req = $DB -> prepare ( " SELECT `id` FROM `users` WHERE `email` = ? AND `year` = ' $YEAR '; " );
$req -> execute ([ $email ]);
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-11 16:41:45 +00:00
function canValidate ( Team $team )
2019-09-09 23:48:52 +00:00
{
2019-09-11 16:41:45 +00:00
global $DB ;
2019-09-09 23:48:52 +00:00
$can_validate = $team -> getValidationStatus () == ValidationStatus :: NOT_READY ;
2019-09-11 16:41:45 +00:00
$can_validate &= $team -> getEncadrantId () != null ;
2019-09-12 12:11:59 +00:00
$can_validate &= $team -> getParticipants ()[ 2 ] != null ;
2019-09-09 23:48:52 +00:00
2019-09-11 16:41:45 +00:00
if ( $team -> getEncadrantId () != null ) {
$req = $DB -> prepare ( " SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `problem` = ? AND `type` = ?; " );
$req -> execute ([ $team -> getEncadrantId (), $team -> getProblem (), " PHOTO_CONSENT " ]);
2019-09-09 23:48:52 +00:00
$d = $req -> fetch ();
$can_validate &= $d [ " version " ] > 0 ;
}
2019-09-11 16:41:45 +00:00
2019-09-12 12:11:59 +00:00
for ( $i = 1 ; $i <= 5 ; ++ $i ) {
2019-09-09 23:48:52 +00:00
if ( $team -> getParticipants ()[ $i ] === NULL )
continue ;
2019-09-11 16:41:45 +00:00
$req = $DB -> prepare ( " SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `problem` = ? AND `type` = ?; " );
$req -> execute ([ $team -> getParticipants ()[ $i ], $team -> getProblem (), " PHOTO_CONSENT " ]);
2019-09-09 23:48:52 +00:00
$d = $req -> fetch ();
$can_validate &= $d [ " version " ] > 0 ;
}
return $can_validate ;
}
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-11 16:41:45 +00:00
function getZipFile ( $problem , $team_id = - 1 )
2019-09-09 23:48:52 +00:00
{
2019-09-11 16:41:45 +00:00
global $LOCAL_PATH , $DB ;
2019-09-09 23:48:52 +00:00
$zip = new ZipArchive ();
2019-09-10 18:25:26 +00:00
$file_name = tempnam ( " tmp " , " corres2math- " );
2019-09-09 23:48:52 +00:00
if ( $zip -> open ( $file_name , ZipArchive :: CREATE ) !== true ) {
die ( " Impossible de créer le fichier zip. " );
}
2019-09-12 14:01:40 +00:00
$data = Document :: getAllDocuments ( $problem , $team_id );
2019-09-09 23:48:52 +00:00
2019-09-11 16:41:45 +00:00
/** @var Document $file */
2019-09-12 14:01:40 +00:00
foreach ( $data as $file ) {
2019-09-09 23:48:52 +00:00
$file_id = $file -> getFileId ();
2019-09-11 16:41:45 +00:00
$user = User :: fromId ( $file -> getUserId ());
$name = " Autorisation de droit à l'image de " . $user -> getFirstName () . " " . $user -> getSurname () . " .pdf " ;
2019-09-09 23:48:52 +00:00
$zip -> addFile ( " $LOCAL_PATH /files/ $file_id " , $name );
}
$zip -> close ();
return $file_name ;
}