1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-26 03:42:20 +00:00
plateforme-corres2math/server_files/classes/User.php

261 lines
6.1 KiB
PHP

<?php
class User
{
private $id;
public $email;
private $pwd_hash;
public $surname;
public $first_name;
public $school;
public $class;
public $description;
private $role;
private $team_id;
private $year;
private $confirm_email;
private $forgotten_password;
private $inscription_date;
private $receive_animath_mails;
private function __construct() {}
public static function fromId($id)
{
global $DB;
$req = $DB->prepare("SELECT * FROM `users` WHERE `id` = ?;");
$req->execute([htmlspecialchars($id)]);
$data = $req->fetch();
if ($data === false)
return null;
$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)
return null;
$user = new User();
$user->fill($data);
return $user;
}
public static function getAdmins()
{
global $DB, $YEAR;
$admins = [];
$req = $DB->query("SELECT * FROM `users` WHERE `year` = $YEAR;");
while (($data = $req->fetch()) !== false) {
$admin = new User();
$admin->fill($data);
$admins[] = $admin;
}
return $admins;
}
private function fill($data)
{
$this->id = $data["id"];
$this->email = $data["email"];
$this->pwd_hash = $data["pwd_hash"];
$this->surname = $data["surname"];
$this->first_name = $data["first_name"];
$this->school = $data["school"];
$this->class = SchoolClass::fromName($data["class"]);
$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"];
$this->inscription_date = $data["inscription_date"];
$this->receive_animath_mails = $data["receive_animath_mails"];
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
global $DB;
$this->email = $email;
$DB->prepare("UPDATE `users` SET `email` = ? WHERE `id` = ?;")->execute([$email, $this->getId()]);
}
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;
$DB->prepare("UPDATE `users` SET `pwd_hash` = ? WHERE `id` = ?;")->execute([$password_hash, $this->getId()]);
}
public function getSurname()
{
return $this->surname;
}
public function setSurname($surname)
{
global $DB;
$this->surname = $surname;
$DB->prepare("UPDATE `users` SET `surname` = ? WHERE `id` = ?;")->execute([$surname, $this->getId()]);
}
public function getFirstName()
{
return $this->first_name;
}
public function setFirstName($first_name)
{
global $DB;
$this->first_name = $first_name;
$DB->prepare("UPDATE `users` SET `first_name` = ? WHERE `id` = ?;")->execute([$first_name, $this->getId()]);
}
public function getSchool()
{
return $this->school;
}
public function setSchool($school)
{
global $DB;
$this->school = $school;
$DB->prepare("UPDATE `users` SET `school` = ? WHERE `id` = ?;")->execute([$school, $this->getId()]);
}
public function getClass()
{
return $this->class;
}
public function setClass($class)
{
global $DB;
$this->class = $class;
$DB->prepare("UPDATE `users` SET `class` = ? WHERE `id` = ?;")->execute([SchoolClass::getName($class), $this->getId()]);
}
public function getDescription()
{
return $this->description;
}
public function setDescription($desc)
{
global $DB;
$this->description = $desc;
$DB->prepare("UPDATE `users` SET `description` = ? WHERE `id` = ?;")->execute([$desc, $this->getId()]);
}
public function getRole()
{
return $this->role;
}
public function setRole($role)
{
global $DB;
$this->role = $role;
$DB->prepare("UPDATE `users` SET `role` = ? WHERE `id` = ?;")->execute([Role::getName($role), $this->getId()]);
}
public function getTeamId()
{
return $this->team_id;
}
public function setTeamId($team_id)
{
global $DB;
$this->team_id = $team_id;
$DB->prepare("UPDATE `users` SET `team_id` = ? WHERE `id` = ?;")->execute([$team_id, $this->getId()]);
}
public function getConfirmEmailToken()
{
return $this->confirm_email;
}
public function setConfirmEmailToken($token)
{
global $DB;
$this->confirm_email = $token;
$DB->prepare("UPDATE `users` SET `confirm_email` = ? WHERE `id` = ?;")->execute([$token, $this->getId()]);
}
public function getForgottenPasswordToken()
{
return $this->forgotten_password;
}
public function setForgottenPasswordToken($token)
{
global $DB;
$this->forgotten_password = $token;
$DB->prepare("UPDATE `users` SET `forgotten_password` = ? WHERE `id` = ?;")->execute([$token, $this->getId()]);
}
public function getInscriptionDate()
{
return $this->inscription_date;
}
public function doReceiveAnimathMails()
{
return $this->receive_animath_mails;
}
public function setReceiveAnimathMails($receive_animath_mails)
{
global $DB;
$this->receive_animath_mails = $receive_animath_mails;
$DB->prepare("UPDATE `users` SET `receive_animath_mails` = ? WHERE `id` = ?;")->execute([$receive_animath_mails, $this->getId()]);
}
public function getAllDocuments($problem)
{
global $DB;
$req = $DB->query("SELECT * FROM `documents` AS `t1` "
. "INNER JOIN (SELECT `user`, `type`, `problem`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `problem`, `type`, `user`) `t2` "
. "ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`problem` = `t2`.`problem` "
. "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`problem` = $problem AND `t1`.`user` = $this->id ORDER BY `t1`.`type`;");
$docs = [];
while (($data = $req->fetch()) !== false)
$docs[] = Document::fromData($data);
return $docs;
}
}