<?php
namespace UserBundle\Entity;
use CoreBundle\Entity\Institution;
use CoreBundle\Entity\SelfStudy;
use DateTime;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;
use Scheb\TwoFactorBundle\Model\Totp\TotpConfiguration;
use Scheb\TwoFactorBundle\Model\Totp\TotpConfigurationInterface;
use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use JsonSerializable;
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="UserBundle\Entity\UserRepository")
* @AttributeOverrides({
* @AttributeOverride(name="password",
* column=@ORM\Column(
* name="password",
* type="string",
* length=255,
* nullable=true
* )
* ),
* @AttributeOverride(name="email",
* column=@ORM\Column(
* name="email",
* type="string",
* length=255,
* unique=false,
* nullable=true
* )
* ),
* @AttributeOverride(name="emailCanonical",
* column=@ORM\Column(
* name="email_canonical",
* type="string",
* length=255,
* unique=false,
* nullable=true
* )
* )
* })
* @UniqueEntity(
* fields={"username"},
* message="user.username.taken"
* )
*/
class User extends BaseUser implements JsonSerializable, TwoFactorInterface
{
const role_admin = 'ROLE_ADMIN';
const role_student = 'ROLE_STUDENT';
const role_teacher = 'ROLE_TEACHER';
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255)
*/
protected $firstName;
/**
* @var string
* @ORM\Column(name="surname", type="string", length=255)
*/
protected $surname;
/**
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Institution", cascade={"persist"})
* @ORM\JoinColumn(name="institution", referencedColumnName="id", onDelete="CASCADE", nullable = true)
*/
protected ?Institution $institution = null;
/**
* @var string
*
* @ORM\Column(name="nickname", type="string", length=255, nullable = true)
*/
protected $nickname;
/**
* @var string
*
* @ORM\Column(name="first_login", type="boolean", options={"default" = true})
*/
protected $firstLogin = true;
/**
* @var bool
*
* @ORM\Column(name="is_subscribed", type="boolean", options={"default" = false})
*/
protected $isSubscribed = false;
/**
* @var bool
*
* @ORM\Column(name="is_demo", type="boolean", options={"default" = false})
*/
protected $isDemo = false;
/**
* @var int
* @ORM\Column(name="desired_grade", type="smallint", nullable=true)
*/
private $desiredGrade;
/**
* @var int
* @ORM\Column(name="class_level", type="smallint", nullable=true)
*/
private $classLevel;
/**
* @var int
* @ORM\Column(name="self_study_level_adjustment", type="smallint", options={"default" = 0})
*/
private $selfStudyLevelAdjustment = 0;
/**
* @var string
* @ORM\Column(type="string", nullable=true)
*/
private $userAgent;
/**
* @var bool
* @ORM\Column(name="show_audio", type="boolean", options={"default" = false}, nullable=true)
*/
private $showAudio;
/**
* @var bool
* @ORM\Column(name="classroom_overview", type="boolean", options={"default" = true}, nullable=true)
*/
private $classroomOverview;
/**
* @var \DateTime
*
* @ORM\Column(name="expiration_basis", type="datetime", nullable=true)
*/
private $expirationBasis;
/**
* @var \DateTime
*
* @ORM\Column(name="expiration_self_study", type="datetime", nullable=true)
*/
private $expirationSelfStudy;
/**
* @var \DateTime
*
* @ORM\Column(name="expiration_textbook", type="datetime", nullable=true)
*/
private $expirationTextbook;
/**
* @var \DateTime
*
* @ORM\Column(name="last_login_with_unilogin", type="datetime", nullable=true)
*/
private $lastLoginWithUnilogin; // note that the last_login in the database is handled by FOSUserBundle and will be removed
/**
* @var SelfStudy
*
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\SelfStudy")
* @ORM\JoinColumn(name="selected_self_study", referencedColumnName="id", nullable=true)
*/
private $selectedSelfStudy;
/**
* @var string
*
* @ORM\Column(name="ekeys_user_id", type="string", nullable=true)
*/
private ?string $ekeysUserId;
/**
* @ORM\Column(name="totp_secret", type="string", nullable=true)
*/
private ?string $totpSecret;
/**
* @ORM\Column(name="totp_validated", type="boolean", options={"default" = false})
*/
private bool $totpValidated = false;
/**
* @ORM\Column(name="class_name", type="string", nullable=true)
*/
private ?string $className;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getClassName(): ?string
{
return $this->className;
}
/**
* @param string|null $className
* @return void
*/
public function setClassName(?string $className): void
{
$this->className = $className;
}
/**
* @param ?Institution $institution
*
* @return User
*/
public function setInstitution(?Institution $institution)
{
$this->institution = $institution;
return $this;
}
public function getInstitution(): ?Institution
{
return $this->institution;
}
/**
* @return string|null
*/
public function getEkeysUserId(): ?string
{
return $this->ekeysUserId;
}
/**
* @param string|null $ekeysUserId
* @return void
*/
public function setEkeysUserId(?string $ekeysUserId): void
{
$this->ekeysUserId = $ekeysUserId;
}
/**
* @return string
*/
public function getClassLevelName(?bool $isSelfStudy = false) {
$levelName = '';
$classLevel = $isSelfStudy ? $this->getSelfStudyLevel() : $this->getClassLevel();
if ($classLevel <= 3) {
$levelName = 'indskoling';
}
elseif ($classLevel >= 4 && $classLevel <= 6) {
$levelName = 'mellemtrin';
}
elseif ($classLevel >= 7) {
$levelName = 'udskoling';
}
return $levelName;
}
/**
* @return string
*/
public function getNickname()
{
if (!empty($this->nickname)) {
return $this->nickname;
}
if (!empty($this->getFirstName()) && !empty($this->getSurname())) {
return $this->getFirstName() . ' ' . $this->getSurname();
}
$nickname = $this->getFirstName();
if ($surname = $this->getSurname()) {
preg_match_all('/(?<=\s|^)[a-z]/i', $surname, $matches);
$nickname .= implode('', $matches[0]);
}
return $nickname;
}
/**
* @param string | null $nickname
*
* @return $this
*/
public function setNickname($nickname = null)
{
if (!empty($nickname)) {
$this->nickname = $nickname;
return $this;
}
if (!empty($this->getFirstName()) && !empty($this->getSurname())) {
$this->nickname = $this->getFirstName() . ' ' . $this->getSurname();
return $this;
}
$nickname = $this->getFirstName();
if ($surname = $this->getSurname()) {
preg_match_all('/(?<=\s|^)[a-z]/i', $surname, $matches);
$nickname .= implode('', $matches[0]);
}
$this->nickname = $nickname;
return $this;
}
/**
* @return string
*/
public function getFirstLogin()
{
return $this->firstLogin;
}
/**
* @param string $firstLogin
*
* @return $this
*/
public function setFirstLogin($firstLogin)
{
$this->firstLogin = $firstLogin;
return $this;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* @param string $surname
*
* @return $this
*/
public function setSurname(string $surname)
{
$this->surname = $surname;
return $this;
}
/**
* @return mixed
*/
public function getIsSubscribed()
{
return $this->isSubscribed;
}
/**
* @param mixed $isSubscribed
*/
public function setIsSubscribed($isSubscribed)
{
$this->isSubscribed = $isSubscribed;
}
/**
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @param string $firstName
*
* @return $this
*/
public function setFirstName(string $firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* @return bool
*/
public function isDemo()
{
return $this->isDemo;
}
/**
* @param bool $isDemo
*/
public function setIsDemo(bool $isDemo)
{
$this->isDemo = $isDemo;
}
public function isDebugUser()
{
return $this->getUsername() === "teacher1_demo";
}
/**
* @return int | null
*/
public function getDesiredGrade()
{
return $this->desiredGrade;
}
/**
* @param int | null $desiredGrade
*/
public function setDesiredGrade($desiredGrade)
{
$this->desiredGrade = $desiredGrade;
}
/**
* @return int
*/
public function getSelfStudyLevelAdjustment(): int
{
return $this->selfStudyLevelAdjustment;
}
/**
* @param int $levelAdjustment
*/
public function setSelfStudyLevelAdjustment(int $levelAdjustment)
{
$this->selfStudyLevelAdjustment = $levelAdjustment;
}
/**
* @return int|null
*/
public function getSelfStudyLevel(): ?int
{
return $this->getClassLevel() === null ? null : $this->getClassLevel() + $this->getSelfStudyLevelAdjustment();
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(string $userAgent)
{
$this->userAgent = $userAgent;
}
/**
* @return string
*/
public function getFullName()
{
return ucwords($this->firstName) . ' ' . ucwords($this->surname);
}
/**
* @return string
*/
public function getCompactName()
{
return ucwords($this->firstName) . ' ' . strtoupper(substr($this->surname, 0, 2));
}
public function getClassLevel(): ?int
{
return $this->classLevel;
}
/**
* @param $classLevel
*/
public function setClassLevel($classLevel): void
{
$this->classLevel = (int) $classLevel;
}
/**
* @return bool
*/
public function isShowAudio(): ?bool
{
return $this->showAudio;
}
/**
* @param bool $showAudio
*/
public function setShowAudio(bool $showAudio): void
{
$this->showAudio = $showAudio;
}
/**
* @return bool
*/
public function getClassroomOverview(): ?bool
{
return $this->classroomOverview;
}
/**
* @param bool $classroomOverview
*/
public function setClassroomOverview(bool $classroomOverview): void
{
$this->classroomOverview = $classroomOverview;
}
/**
* @return \DateTime|null
*/
public function getExpirationBasis(): ?\DateTime
{
return $this->expirationBasis;
}
/**
* @param \DateTime|null $expirationBasis
*/
public function setExpirationBasis(?\DateTime $expirationBasis): void
{
$this->expirationBasis = $expirationBasis;
}
/**
* @return \DateTime|null
*/
public function getExpirationSelfStudy(): ?\DateTime
{
return $this->expirationSelfStudy;
}
/**
* @param \DateTime|null $expirationSelfStudy
*/
public function setExpirationSelfStudy(?\DateTime $expirationSelfStudy): void
{
$this->expirationSelfStudy = $expirationSelfStudy;
}
/**
* @return \DateTime|null
*/
public function getExpirationTextbook(): ?\DateTime
{
return $this->expirationTextbook;
}
/**
* @param \DateTime|null $expirationTextbook
*/
public function setExpirationTextbook(?\DateTime $expirationTextbook): void
{
$this->expirationTextbook = $expirationTextbook;
}
public function jsonSerialize(): mixed
{
return [
"id" => $this->getId(),
"name" => $this->getFullName(),
"classLevel" => $this->getClassLevel()
];
}
public function getLastLoginWithUnilogin(): ?DateTime
{
return $this->lastLoginWithUnilogin;
}
public function setLastLoginWithUnilogin(DateTime $lastLoginWithUnilogin): void
{
$this->lastLoginWithUnilogin = $lastLoginWithUnilogin;
}
public function getSelectedSelfStudy(): ?SelfStudy
{
return $this->selectedSelfStudy;
}
public function setSelectedSelfStudy(SelfStudy $selectedSelfStudy): void
{
$this->selectedSelfStudy = $selectedSelfStudy;
}
public function setTotpSecret(string $secret): void
{
$this->totpSecret = $secret;
}
public function isTotpAuthenticationEnabled(): bool
{
return $this->totpValidated;
}
public function getTotpAuthenticationUsername(): string
{
return $this->username;
}
public function getTotpAuthenticationConfiguration(): ?TotpConfigurationInterface
{
// SHA1, 30 seconds and 6 digits is will make it work with Google authenticator and equivalent.
return new TotpConfiguration($this->totpSecret, TotpConfiguration::ALGORITHM_SHA1, 30, 6);
}
public function isTotpValidated(): bool
{
return $this->totpValidated;
}
public function setTotpValidated(bool $totpValidated): void
{
$this->totpValidated = $totpValidated;
}
}