2019-09-05 17:07:59 +00:00
< ? php
class User
{
2019-09-09 21:28:03 +00:00
private $id ;
2019-09-09 20:42:38 +00:00
public $email ;
2019-09-09 21:28:03 +00:00
private $pwd_hash ;
2019-09-09 20:42:38 +00:00
public $surname ;
public $first_name ;
public $birth_date ;
public $gender ;
public $address ;
public $postal_code ;
public $city ;
public $country ;
public $phone_number ;
public $school ;
public $class ;
public $responsible_name ;
public $responsible_phone ;
public $responsible_email ;
public $description ;
2019-09-09 21:28:03 +00:00
private $role ;
private $team_id ;
private $year ;
private $confirm_email ;
private $forgotten_password ;
private $inscription_date ;
2019-09-06 12:02:32 +00:00
private function __construct () {}
2019-09-05 17:07:59 +00:00
2019-09-06 12:02:32 +00:00
public static function fromId ( $id )
2019-09-05 17:07:59 +00:00
{
global $DB ;
$req = $DB -> prepare ( " SELECT * FROM `users` WHERE `id` = ?; " );
$req -> execute ([ htmlspecialchars ( $id )]);
$data = $req -> fetch ();
if ( $data === false )
2019-09-06 23:33:05 +00:00
return null ;
2019-09-05 17:07:59 +00:00
2019-09-06 12:02:32 +00:00
$user = new User ();
$user -> fill ( $data );
return $user ;
}
public static function fromEmail ( $email )
{
global $DB , $YEAR ;
$req = $DB -> prepare ( " SELECT * FROM `users` WHERE `email` = ? AND `year` = $YEAR ; " );
$req -> execute ([ htmlspecialchars ( $email )]);
$data = $req -> fetch ();
if ( $data === false )
2019-09-06 23:33:05 +00:00
return null ;
2019-09-06 12:02:32 +00:00
$user = new User ();
$user -> fill ( $data );
return $user ;
}
private function fill ( $data )
{
$this -> id = $data [ " id " ];
2019-09-05 17:07:59 +00:00
$this -> email = $data [ " email " ];
$this -> pwd_hash = $data [ " pwd_hash " ];
$this -> surname = $data [ " surname " ];
$this -> first_name = $data [ " first_name " ];
$this -> birth_date = $data [ " birth_date " ];
$this -> gender = $data [ " gender " ];
$this -> address = $data [ " address " ];
$this -> postal_code = $data [ " postal_code " ];
$this -> city = $data [ " city " ];
$this -> country = $data [ " country " ];
$this -> phone_number = $data [ " phone_number " ];
$this -> school = $data [ " school " ];
2019-09-07 13:51:16 +00:00
$this -> class = SchoolClass :: fromName ( $data [ " class " ]);
2019-09-05 17:07:59 +00:00
$this -> responsible_name = $data [ " responsible_name " ];
$this -> responsible_phone = $data [ " responsible_phone " ];
$this -> responsible_email = $data [ " responsible_email " ];
$this -> description = $data [ " description " ];
$this -> role = Role :: fromName ( $data [ " role " ]);
$this -> team_id = $data [ " team_id " ];
$this -> year = $data [ " year " ];
$this -> confirm_email = $data [ " confirm_email " ];
$this -> forgotten_password = $data [ " forgotten_password " ];
2019-09-06 23:33:05 +00:00
$this -> inscription_date = $data [ " inscription_date " ];
2019-09-05 17:07:59 +00:00
}
2019-12-26 21:30:42 +00:00
public static function getOrganizers ()
{
global $DB , $YEAR ;
$admins = [];
2020-01-21 11:43:13 +00:00
$req = $DB -> query ( " SELECT * FROM `users` WHERE `role` = 'ORGANIZER' OR `role` = 'ADMIN' AND `year` = $YEAR ORDER BY `role`, `surname`, `first_name`; " );
2019-12-26 21:30:42 +00:00
while (( $data = $req -> fetch ()) !== false ) {
$admin = new User ();
$admin -> fill ( $data );
$admins [] = $admin ;
}
return $admins ;
}
2020-01-14 16:16:27 +00:00
public static function getAdmins ()
{
global $DB , $YEAR ;
$users = [];
$req = $DB -> query ( " SELECT * FROM `users` WHERE (`role` = 'ADMIN') "
. " AND `year` = $YEAR ORDER BY `role`, `inscription_date`; " );
while (( $data = $req -> fetch ()) !== false ) {
$orphan = new User ();
$orphan -> fill ( $data );
$users [] = $orphan ;
}
return $users ;
}
2019-12-26 21:30:42 +00:00
public static function getAllUsers ()
{
global $DB , $YEAR ;
$users = [];
$req = $DB -> query ( " SELECT * FROM `users` WHERE (`role` = 'PARTICIPANT' OR `role` = 'ENCADRANT') "
. " AND `year` = $YEAR ORDER BY `role`, `inscription_date`; " );
while (( $data = $req -> fetch ()) !== false ) {
$orphan = new User ();
$orphan -> fill ( $data );
$users [] = $orphan ;
}
return $users ;
}
public static function getOrphanUsers ()
{
global $DB , $YEAR ;
$orphans = [];
$req = $DB -> query ( " SELECT * FROM `users` WHERE `role` != 'ADMIN' AND `team_id` IS NULL "
. " AND `year` = $YEAR ORDER BY `role`, `inscription_date`; " );
while (( $data = $req -> fetch ()) !== false ) {
$orphan = new User ();
$orphan -> fill ( $data );
$orphans [] = $orphan ;
}
return $orphans ;
}
2019-09-05 17:07:59 +00:00
public function getEmail ()
{
return $this -> email ;
}
public function setEmail ( $email )
{
global $DB ;
$this -> email = $email ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `email` = ? WHERE `id` = ?; " ) -> execute ([ $email , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getId ()
{
return $this -> id ;
}
public function checkPassword ( $password )
{
return password_verify ( $password , $this -> pwd_hash );
}
public function setPassword ( $password )
{
$this -> setPasswordHash ( password_hash ( $password , PASSWORD_BCRYPT ));
}
private function setPasswordHash ( $password_hash )
{
global $DB ;
$this -> pwd_hash = $password_hash ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `pwd_hash` = ? WHERE `id` = ?; " ) -> execute ([ $password_hash , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getSurname ()
{
return $this -> surname ;
}
public function setSurname ( $surname )
{
global $DB ;
$this -> surname = $surname ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `surname` = ? WHERE `id` = ?; " ) -> execute ([ $surname , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getFirstName ()
{
return $this -> first_name ;
}
public function setFirstName ( $first_name )
{
global $DB ;
$this -> first_name = $first_name ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `first_name` = ? WHERE `id` = ?; " ) -> execute ([ $first_name , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getBirthDate ()
{
return $this -> birth_date ;
}
public function setBirthDate ( $birth_date )
{
global $DB ;
$this -> birth_date = $birth_date ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `birth_date` = ? WHERE `id` = ?; " ) -> execute ([ $birth_date , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getGender ()
{
return $this -> gender ;
}
public function setGender ( $gender )
{
global $DB ;
$this -> gender = $gender ;
2019-09-06 23:33:05 +00:00
$DB -> prepare ( " UPDATE `users` SET `gender` = ? WHERE `id` = ?; " ) -> execute ([ $gender , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getAddress ()
{
return $this -> address ;
}
public function setAddress ( $address )
{
global $DB ;
$this -> address = $address ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `address` = ? WHERE `id` = ?; " ) -> execute ([ $address , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getPostalCode ()
{
return $this -> postal_code ;
}
public function setPostalCode ( $postal_code )
{
global $DB ;
$this -> postal_code = $postal_code ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `postal_code` = ? WHERE `id` = ?; " ) -> execute ([ $postal_code , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getCity ()
{
return $this -> city ;
}
public function setCity ( $city )
{
global $DB ;
$this -> city = $city ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `city` = ? WHERE `id` = ?; " ) -> execute ([ $city , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getCountry ()
{
return $this -> country ;
}
public function setCountry ( $country )
{
global $DB ;
$this -> country = $country ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `country` = ? WHERE `id` = ?; " ) -> execute ([ $country , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getPhoneNumber ()
{
return $this -> phone_number ;
}
public function setPhoneNumber ( $phone_number )
{
global $DB ;
$this -> phone_number = $phone_number ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `phone_number` = ? WHERE `id` = ?; " ) -> execute ([ $phone_number , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getSchool ()
{
return $this -> school ;
}
public function setSchool ( $school )
{
global $DB ;
$this -> school = $school ;
2019-09-09 20:42:38 +00:00
$DB -> prepare ( " UPDATE `users` SET `school` = ? WHERE `id` = ?; " ) -> execute ([ SchoolClass :: getName ( $school ), $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getClass ()
{
return $this -> class ;
}
public function setClass ( $class )
{
global $DB ;
$this -> class = $class ;
2019-09-07 13:51:16 +00:00
$DB -> prepare ( " UPDATE `users` SET `class` = ? WHERE `id` = ?; " ) -> execute ([ SchoolClass :: getName ( $class ), $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getResponsibleName ()
{
return $this -> responsible_name ;
}
public function setResponsibleName ( $responsible_name )
{
global $DB ;
$this -> responsible_name = $responsible_name ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `responsible_name` = ? WHERE `id` = ?; " ) -> execute ([ $responsible_name , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getResponsiblePhone ()
{
return $this -> responsible_phone ;
}
public function setResponsiblePhone ( $responsible_phone )
{
global $DB ;
$this -> responsible_phone = $responsible_phone ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `responsible_phone` = ? WHERE `id` = ?; " ) -> execute ([ $responsible_phone , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getResponsibleEmail ()
{
return $this -> responsible_email ;
}
public function setResponsibleEmail ( $responsible_email )
{
global $DB ;
$this -> responsible_email = $responsible_email ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `responsible_email` = ? WHERE `id` = ?; " ) -> execute ([ $responsible_email , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getDescription ()
{
return $this -> description ;
}
public function setDescription ( $desc )
{
global $DB ;
$this -> description = $desc ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `description` = ? WHERE `id` = ?; " ) -> execute ([ $desc , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getRole ()
{
return $this -> role ;
}
public function setRole ( $role )
{
global $DB ;
$this -> role = $role ;
/** @noinspection PhpUndefinedMethodInspection */
2019-09-06 23:33:05 +00:00
$DB -> prepare ( " UPDATE `users` SET `role` = ? WHERE `id` = ?; " ) -> execute ([ Role :: getName ( $role ), $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getTeamId ()
{
return $this -> team_id ;
}
public function setTeamId ( $team_id )
{
global $DB ;
$this -> team_id = $team_id ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `team_id` = ? WHERE `id` = ?; " ) -> execute ([ $team_id , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getYear ()
{
return $this -> year ;
}
public function getConfirmEmailToken ()
{
return $this -> confirm_email ;
}
public function setConfirmEmailToken ( $token )
{
global $DB ;
$this -> confirm_email = $token ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `confirm_email` = ? WHERE `id` = ?; " ) -> execute ([ $token , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
public function getForgottenPasswordToken ()
{
return $this -> forgotten_password ;
}
public function setForgottenPasswordToken ( $token )
{
global $DB ;
$this -> forgotten_password = $token ;
2019-09-06 12:02:32 +00:00
$DB -> prepare ( " UPDATE `users` SET `forgotten_password` = ? WHERE `id` = ?; " ) -> execute ([ $token , $this -> getId ()]);
2019-09-05 17:07:59 +00:00
}
2019-09-06 23:33:05 +00:00
public function getInscriptionDate ()
{
return $this -> inscription_date ;
}
2019-09-07 23:35:05 +00:00
public function getAllDocuments ( $tournament_id )
{
global $DB ;
$req = $DB -> query ( " SELECT * FROM `documents` AS `t1` "
2019-09-08 20:54:57 +00:00
. " INNER JOIN (SELECT `user`, `type`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `tournament`, `type`, `user`) `t2` "
2019-09-07 23:35:05 +00:00
. " ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`tournament` = `t2`.`tournament` "
. " WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $tournament_id AND `t1`.`user` = $this->id ORDER BY `t1`.`type`; " );
$docs = [];
while (( $data = $req -> fetch ()) !== false )
$docs [] = Document :: fromData ( $data );
2020-01-18 13:43:42 +00:00
if ( $this -> team_id > 0 ) {
$req = $DB -> query ( " SELECT * FROM `documents` AS `t1` "
. " INNER JOIN (SELECT `user`, `type`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `tournament`, `type`, `user`) `t2` "
. " ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`tournament` = `t2`.`tournament` "
. " WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $tournament_id AND `t1`.`team` = $this->team_id "
. " AND `t1`.`type` = 'MOTIVATION_LETTER'; " );
while (( $data = $req -> fetch ()) !== false )
$docs [] = Document :: fromData ( $data );
}
2019-09-07 23:35:05 +00:00
return $docs ;
}
2020-01-01 20:53:46 +00:00
public function getPayment () {
global $DB ;
$team = Team :: fromId ( $this -> team_id );
$tournament = $team -> getEffectiveTournament ();
$req = $DB -> prepare ( " SELECT `id` FROM `payments` WHERE `user` = ? AND `tournament` = ?; " );
$req -> execute ([ $this -> id , $tournament -> getId ()]);
if (( $data = $req -> fetch ()) !== false )
return Payment :: fromId ( $data [ " id " ]);
$req = $DB -> prepare ( " INSERT INTO `payments`(`user`, `tournament`, `amount`, `method`, `transaction_infos`, `validation_status`) VALUES (?, ?, ?, ?, ?, ?); " );
$req -> execute ([ $this -> id , $tournament -> getId (), 0 , PaymentMethod :: getName ( PaymentMethod :: NOT_PAID ), " L'inscription n'est pas encore payée. " , ValidationStatus :: getName ( ValidationStatus :: NOT_READY )]);
2020-01-01 23:09:02 +00:00
return $this -> getPayment ();
}
2020-01-01 20:53:46 +00:00
2019-09-07 23:35:05 +00:00
public function getOrganizedTournaments ()
{
global $DB ;
$req = $DB -> query ( " SELECT `tournament` FROM `organizers` JOIN `tournaments` ON `tournaments`.`id` = `tournament` WHERE `organizer` = $this->id ORDER BY `date_start`, `name`; " );
$tournaments = [];
while (( $data = $req -> fetch ()) !== false )
$tournaments [] = Tournament :: fromId ( $data [ " tournament " ]);
return $tournaments ;
}
2019-09-05 17:07:59 +00:00
}