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 " ]);
2019-12-04 10:45:14 +00:00
unset ( $_SESSION [ " teams " ]);
2019-09-09 23:48:52 +00:00
if ( isset ( $_SESSION [ " user_id " ])) {
$user = $_SESSION [ " user " ] = User :: fromId ( $_SESSION [ " user_id " ]);
2019-09-24 09:00:44 +00:00
if ( $user == null ) {
unset ( $_SESSION [ " user_id " ]);
return ;
}
2019-09-09 23:48:52 +00:00
$_SESSION [ " role " ] = $user -> getRole ();
2019-12-04 10:45:14 +00:00
$_SESSION [ " teams " ] = $user -> getTeams ();
if ( $user -> getRole () == Role :: PARTICIPANT )
$_SESSION [ " team " ] = sizeof ( $_SESSION [ " teams " ]) > 0 ? $_SESSION [ " teams " ][ 0 ] : null ;
2019-09-09 23:48:52 +00:00
}
2019-10-21 22:10:46 +00:00
if ( isset ( $_GET [ " view-as-admin " ])) {
if ( isset ( $_SESSION [ " admin " ])) {
$_SESSION [ " user_id " ] = $_SESSION [ " admin " ];
unset ( $_SESSION [ " admin " ]);
}
header ( " Location: / " );
exit ();
}
2019-09-09 23:48:52 +00:00
}
2019-09-27 22:48:30 +00:00
function quitTeam ( $user_id = - 1 )
2019-09-09 23:48:52 +00:00
{
global $DB , $URL_BASE ;
2019-09-27 22:48:30 +00:00
if ( $user_id == - 1 )
header ( " Location: $URL_BASE " );
2019-09-09 23:48:52 +00:00
2019-09-27 22:48:30 +00:00
$user_id = $user_id >= 0 ? $user_id : $_SESSION [ " user_id " ];
2019-09-09 23:48:52 +00:00
/** @var User $user */
2019-09-27 22:48:30 +00:00
$user = User :: fromId ( $user_id );
2019-09-09 23:48:52 +00:00
$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
2019-09-18 22:31:53 +00:00
$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-27 22:48:30 +00:00
$DB -> exec ( " DELETE FROM `teams` WHERE `encadrant` IS NULL AND `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 " ]);
2019-12-04 10:45:14 +00:00
$_SESSION [ " teams " ] = $user -> getTeams ();
2019-09-09 23:48:52 +00:00
}
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-12 16:47:57 +00:00
global $DB , $CONFIG ;
2019-09-09 23:48:52 +00:00
2019-09-12 16:47:57 +00:00
$can_validate = date ( " Y-m-d H:i:s " ) < $CONFIG -> getInscriptionDate ();
$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-10-04 19:41:12 +00:00
$can_validate &= preg_match ( " #[1-4]# " , $team -> getProblem ());
2019-09-09 23:48:52 +00:00
2019-12-09 14:20:46 +00:00
for ( $i = 0 ; $i < 5 ; ++ $i ) {
2019-09-09 23:48:52 +00:00
if ( $team -> getParticipants ()[ $i ] === NULL )
continue ;
2019-09-24 23:08:38 +00:00
$req = $DB -> prepare ( " SELECT COUNT(*) AS `version` FROM `documents` WHERE `user` = ? AND `problem` = ?; " );
$req -> execute ([ $team -> getParticipants ()[ $i ], $team -> getProblem ()]);
2019-09-09 23:48:52 +00:00
$d = $req -> fetch ();
$can_validate &= $d [ " version " ] > 0 ;
}
return $can_validate ;
}
function printDocuments ( $documents )
{
2019-10-22 12:55:33 +00:00
if ( sizeof ( $documents ) == 0 ) {
echo " <div class= \" alert alert-warning \" > \n Pas de document envoyé pour le moment. \n </div> \n " ;
return ;
}
2019-09-24 22:57:41 +00:00
echo " <div class= \" alert alert-info \" > \n " ;
2019-09-09 23:48:52 +00:00
foreach ( $documents as $document ) {
$file_id = $document -> getFileId ();
$user = User :: fromId ( $document -> getUserId ());
$surname = $user -> getSurname ();
$first_name = $user -> getFirstName ();
2019-09-24 23:15:03 +00:00
$name = " Autorisation de droit à l'image " ;
2019-09-09 23:48:52 +00:00
$version = $document -> getVersion ();
2019-09-24 22:57:41 +00:00
echo " $name de $first_name $surname (version $version ) : <a href= \" /file/ $file_id\ " >< strong > Télécharger </ strong ></ a >< br /> \n " ;
2019-09-09 23:48:52 +00:00
}
2019-09-24 22:57:41 +00:00
echo " </div> \n " ;
2019-09-09 23:48:52 +00:00
}
2019-09-11 16:41:45 +00:00
function getZipFile ( $problem , $team_id = - 1 )
2019-09-09 23:48:52 +00:00
{
2019-09-12 16:47:57 +00:00
global $LOCAL_PATH ;
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 ());
2019-11-17 11:24:51 +00:00
$mime_type = finfo_file ( finfo_open ( FILEINFO_MIME_TYPE ), " $LOCAL_PATH /files/ $file_id " );
$ext = $mime_type == " application/pdf " ? " pdf " : ( $mime_type == " image/png " ? " png " : " jpg " );
$name = " Autorisation de droit à l'image de " . $user -> getFirstName () . " " . $user -> getSurname () . " . " . $ext ;
2019-09-09 23:48:52 +00:00
$zip -> addFile ( " $LOCAL_PATH /files/ $file_id " , $name );
}
$zip -> close ();
return $file_name ;
2019-10-18 20:23:00 +00:00
}
function exportUserData () {
global $DB , $YEAR ;
$file_name = tempnam ( " tmp " , " corres2math- " );
$csv = " Identifiant,Nom,Prénom,E-mail,École,Ville,Pays,Classe,Description,Rôle,Équipe,Autorise l'envoi de mails,Date d'inscription \n " ;
$req = $DB -> query ( " SELECT `id` FROM `users` WHERE `year` = $YEAR ; " );
while (( $data = $req -> fetch ()) !== false ) {
$user = User :: fromId ( $data [ " id " ]);
$csv .= $user -> getId () . " , " ;
$csv .= $user -> getSurname () . " , " ;
$csv .= $user -> getFirstName () . " , " ;
$csv .= $user -> getEmail () . " , " ;
$csv .= $user -> getSchool () . " , " ;
$csv .= $user -> getCity () . " , " ;
$csv .= $user -> getCountry () . " , " ;
$csv .= SchoolClass :: getTranslatedName ( $user -> getClass ()) . " , " ;
$csv .= $user -> getDescription () . " , " ;
$csv .= Role :: getTranslatedName ( $user -> getRole ()) . " , " ;
$csv .= ( $user -> getTeamId () == null ? " " : Team :: fromId ( $user -> getTeamId ()) -> getTrigram ()) . " , " ;
$csv .= ( $user -> doReceiveAnimathMails () ? " oui " : " non " ) . " , " ;
$csv .= $user -> getInscriptionDate () . " \n " ;
}
file_put_contents ( $file_name , $csv , FILE_APPEND );
return $file_name ;
}
function exportTeamData () {
global $DB , $YEAR ;
$file_name = tempnam ( " tmp " , " corres2math- " );
$csv = " Identifiant,Nom,Trigramme,Problème,Encadrant,Participant 1,Participant 2,Participant 3,Participant 4,Participant 5, "
. " Date d'inscription,Autorise la publication,État de validation,Code d'accès \n " ;
$req = $DB -> query ( " SELECT `id` FROM `teams` WHERE `year` = $YEAR ORDER BY `problem`; " );
while (( $data = $req -> fetch ()) !== false ) {
$team = Team :: fromId ( $data [ " id " ]);
$csv .= $team -> getId () . " , " ;
$csv .= $team -> getName () . " , " ;
$csv .= $team -> getTrigram () . " , " ;
$csv .= $team -> getProblem () . " , " ;
$csv .= $team -> getEncadrantId () . " , " ;
$csv .= $team -> getParticipants ()[ 0 ] . " , " ;
$csv .= $team -> getParticipants ()[ 1 ] . " , " ;
$csv .= $team -> getParticipants ()[ 2 ] . " , " ;
$csv .= $team -> getParticipants ()[ 3 ] . " , " ;
$csv .= $team -> getParticipants ()[ 4 ] . " , " ;
$csv .= $team -> getInscriptionDate () . " , " ;
$csv .= $team -> allowPublish () . " , " ;
$csv .= ValidationStatus :: getTranslatedName ( $team -> getValidationStatus ()) . " , " ;
$csv .= $team -> getAccessCode () . " \n " ;
}
file_put_contents ( $file_name , $csv , FILE_APPEND );
return $file_name ;
}
function exportProblemsData () {
global $DB , $URL_BASE , $YEAR ;
$file_name = tempnam ( " tmp " , " corres2math- " );
$csv = " Équipe,Problème,Lien,Équipe 1 ayant reçu la vidéo, "
. " Question 1,Pièce jointe,Réponse,Pièce jointe, "
. " Question 2,Pièce jointe,Réponse,Pièce jointe, "
. " Question 3,Pièce jointe,Réponse,Pièce jointe, "
. " Question 4,Pièce jointe,Réponse,Pièce jointe, "
. " Question 5,Pièce jointe,Réponse,Pièce jointe, "
. " Question 6,Pièce jointe,Réponse,Pièce jointe, "
. " Vidéo de réponse, "
. " Équipe 2 ayant reçu la vidéo, "
. " Question 1,Pièce jointe,Réponse,Pièce jointe, "
. " Question 2,Pièce jointe,Réponse,Pièce jointe, "
. " Question 3,Pièce jointe,Réponse,Pièce jointe, "
. " Question 4,Pièce jointe,Réponse,Pièce jointe, "
. " Question 5,Pièce jointe,Réponse,Pièce jointe, "
. " Question 6,Pièce jointe,Réponse,Pièce jointe, "
. " Vidéo de réponse \n " ;
$req = $DB -> query ( " SELECT `id` FROM `teams` WHERE `validation_status` = 'VALIDATED' AND `year` = $YEAR ORDER BY `problem`; " );
while (( $data = $req -> fetch ()) !== false ) {
$team = Team :: fromId ( $data [ " id " ]);
$video = Video :: getVideo ( Reason :: SOLUTION , $team );
$questions_received = Question :: getQuestionsTo ( $team );
$csv .= $team -> getTrigram () . " , " ;
$csv .= $team -> getProblem () . " , " ;
$csv .= ( $video == null ? " " : $video -> getLink ()) . " , " ;
for ( $i = 0 ; $i < 2 ; ++ $i ) {
/** @var Question[] $questions */
$questions = $questions_received [ $i ];
$asker = Team :: fromId ( $questions [ 0 ] -> getFrom ());
$csv .= $asker -> getTrigram () . " , " ;
for ( $j = 0 ; $j < 6 ; ++ $j ) {
$question = $questions [ $j ];
if ( $question == null ) {
$csv .= " ,,,, " ;
continue ;
}
$csv .= $question -> getQuestion () . " , " ;
$csv .= ( $question -> getAttachedFile () == null ? " " : $URL_BASE . " /file/ " . $question -> getAttachedFile ()) . ',' ;
$csv .= $question -> getAnswer () . " , " ;
$csv .= ( $question -> getAttachedFileAnswer () == null ? " " : $URL_BASE . " /file/ " . $question -> getAttachedFileAnswer ()) . ',' ;
}
$answer = Video :: getVideo ( $asker -> getVideoTeamIds ()[ 0 ] == $team -> getId () ? Reason :: ANSWER1 : Reason :: ANSWER2 ,
$asker , Video :: ACCEPTED );
$csv .= $answer == null ? " " : $answer -> getLink ();
$csv .= ( $i == 0 ? " , " : " \n " );
}
}
file_put_contents ( $file_name , $csv , FILE_APPEND );
return $file_name ;
2019-09-18 22:31:53 +00:00
}
function displayVideo ( $link )
{
2019-12-22 18:19:02 +00:00
if ( preg_match ( " #(https?://|)(www \ .|)(youtube \ .com/watch \ ?v=|youtu.be/)([a-zA-Z0-9, \ -,_]*)?.*?# " , $link , $matches )) {
2019-12-20 11:53:28 +00:00
$code = $matches [ 4 ];
2019-12-22 16:24:14 +00:00
/** @noinspection HtmlDeprecatedAttribute */
/** @noinspection HtmlDeprecatedTag */
echo " <center><iframe width= \" 854px \" height= \" 480px \" src= \" https://www.youtube.com/embed/ $code\ " frameborder = \ " 0 \" allow= \" accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture \" allowfullscreen></iframe></center><br /> \n " ;
}
2019-12-31 15:52:10 +00:00
elseif ( preg_match ( " #(https?://|)drive \ .google \ .com/(file/d/|open \ ?id=)([a-zA-Z0-9 \ -_]*)/?.*?# " , $link , $matches )) {
$code = $matches [ 3 ];
2019-12-22 16:24:14 +00:00
/** @noinspection HtmlDeprecatedAttribute */
/** @noinspection HtmlDeprecatedTag */
2020-01-01 23:51:23 +00:00
echo " <center><iframe width= \" 854px \" height= \" 480px \" src= \" https://drive.google.com/file/d/ $code /preview \" allowfullscreen></iframe></center><br /> \n " ;
2019-09-18 22:31:53 +00:00
}
2019-10-21 22:10:46 +00:00
elseif ( preg_match ( " #(https?://|)(www \ .|)vimeo \ .com/([0-9]*)# " , $link , $matches )) {
2019-09-21 15:36:07 +00:00
$code = $matches [ 3 ];
2019-10-21 22:10:46 +00:00
/** @noinspection HtmlDeprecatedAttribute */
/** @noinspection HtmlDeprecatedTag */
echo " <center><iframe src= \" https://player.vimeo.com/video/ $code ?color=3be6c3 \" width= \" 853 \" height= \" 480 \" frameborder= \" 0 \" allow= \" autoplay; fullscreen \" allowfullscreen></iframe></center> " ;
2019-09-21 15:36:07 +00:00
}
2019-10-21 22:10:46 +00:00
elseif ( preg_match ( " #(https?://|)(www \ .|)dailymotion \ .com/video/(.*)# " , $link , $matches )) {
2019-09-21 15:48:49 +00:00
$code = $matches [ 3 ];
2019-10-21 22:10:46 +00:00
/** @noinspection HtmlDeprecatedAttribute */
/** @noinspection HtmlDeprecatedTag */
2019-09-21 15:48:49 +00:00
echo " <center><iframe frameborder= \" 0 \" width= \" 853 \" height= \" 480 \" src= \" https://www.dailymotion.com/embed/video/ $code\ " allowfullscreen allow = \ " autoplay \" ></iframe></center> " ;
}
2019-09-09 23:48:52 +00:00
}