1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Repository; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\TrackEOnline; |
10
|
|
|
use Chamilo\CoreBundle\Settings\SettingsManager; |
11
|
|
|
use DateTime; |
12
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
13
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
14
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
15
|
|
|
|
16
|
|
|
class TrackEOnlineRepository extends ServiceEntityRepository |
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
ManagerRegistry $registry, |
20
|
|
|
private readonly SettingsManager $settingsManager |
21
|
|
|
) { |
22
|
|
|
parent::__construct($registry, TrackEOnline::class); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function isUserOnline(int $userId): bool |
26
|
|
|
{ |
27
|
|
|
$accessUrlId = 1; |
28
|
|
|
$timeLimit = $this->settingsManager->getSetting('display.time_limit_whosonline'); |
29
|
|
|
|
30
|
|
|
$onlineTime = new DateTime(); |
31
|
|
|
$onlineTime->modify("-{$timeLimit} minutes"); |
32
|
|
|
|
33
|
|
|
$qb = $this->createQueryBuilder('t') |
34
|
|
|
->select('COUNT(t.loginUserId)') |
35
|
|
|
->where('t.loginUserId = :userId') |
36
|
|
|
->andWhere('t.accessUrlId = :accessUrlId') |
37
|
|
|
->andWhere('t.loginDate >= :limitDate') |
38
|
|
|
->setParameter('userId', $userId) |
39
|
|
|
->setParameter('accessUrlId', $accessUrlId) |
40
|
|
|
->setParameter('limitDate', $onlineTime) |
41
|
|
|
->setMaxResults(1) |
42
|
|
|
; |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$count = $qb->getQuery()->getSingleScalarResult(); |
46
|
|
|
|
47
|
|
|
return $count > 0; |
48
|
|
|
} catch (NonUniqueResultException $e) { |
49
|
|
|
// Handle exception |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|