Passed
Pull Request — master (#4020)
by Yannick
09:55
created

LegalRepository::countTerms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\Language;
10
use Chamilo\CoreBundle\Entity\Legal;
11
use Chamilo\CoreBundle\Entity\User;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\Common\Collections\Criteria;
14
use Doctrine\Persistence\ManagerRegistry;
15
use Exception;
16
17
class LegalRepository extends ServiceEntityRepository
18
{
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, Legal::class);
22
    }
23
24
    /**
25
     * Count the legal terms by language (only count one set of terms for each
26
     * language).
27
     *
28
     * @throws Exception
29
     *
30
     * @return int
31
     */
32
    public function countAllActiveLegalTerms()
33
    {
34
        $qb = $this->createQueryBuilder('l');
35
        $qb->select('l.languageId, COUNT(l.id)')
36
            ->groupBy('l.languageId')
37
        ;
38
39
        return \count($qb->getQuery()->getResult());
40
    }
41
42
    /**
43
     * Get the latest version of terms of the given type and language.
44
     *
45
     * @param int $typeId     The type of terms:
46
     *                        0 for general text,
47
     *                        1 for general HTML link,
48
     *                        101 for private data collection,
49
     *                        etc - see personal_data.php
50
     * @param int $languageId The Id of the language
51
     *
52
     * @return array The terms for those type and language
53
     */
54
    public function findOneByTypeAndLanguage(int $typeId, int $languageId)
55
    {
56
        $qb = $this->createQueryBuilder('l');
57
        $qb->select('l.content')
58
            ->where($qb->expr()->eq('l.type', $typeId))
59
            ->andWhere($qb->expr()->eq('l.languageId', $languageId))
60
        ;
61
62
        return $qb->getQuery()->getResult();
63
    }
64
65
    /**
66
     * Get type of terms and conditions.
67
     * Type 0 is HTML Text
68
     * Type 1 is a link to a different terms and conditions page.
69
     *
70
     * @return mixed The current type of terms and conditions (int) or false on error
71
     */
72
    public function getTypeOfTermsAndConditions(Legal $legal, Language $language)
73
    {
74
        $qb = $this->createQueryBuilder('l');
75
        $qb->select('l.type')
76
            ->where($qb->expr()->eq('l.id', $legal->getId()))
77
            ->andWhere($qb->expr()->eq('l.languageId', $language->getId()))
78
        ;
79
80
        return $qb->getQuery()->getResult();
81
    }
82
83
    /**
84
     * Checks whether we already approved the last version term and condition.
85
     *
86
     * @return bool true if we pass false otherwise
87
     */
88
    public function checkTermCondition(User $user)
89
    {
90
        if ('true' === api_get_setting('allow_terms_conditions')) {
91
            // Check if exists terms and conditions
92
            if (0 == $this->countTerms()) {
93
                return true;
94
            }
95
96
            $extraFieldValue = new \ExtraFieldValue('user');
97
            $data = $extraFieldValue->get_values_by_handler_and_field_variable(
98
                $user->getId(),
99
                'legal_accept'
100
            );
101
102
            if (!empty($data) && isset($data['value']) && !empty($data['value'])) {
103
                $result = $data['value'];
104
                $userConditions = explode(':', $result);
105
                $version = $userConditions[0];
106
                $langId = (int) $userConditions[1];
107
                $realVersion = $this->getLastVersion($langId);
108
109
                return $version >= $realVersion;
110
            }
111
112
            return false;
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * Gets the number of terms and conditions available.
120
     *
121
     * @return int
122
     */
123
    public function countTerms()
124
    {
125
        $qb = $this->createQueryBuilder('l');
126
        $qb->select('COUNT(l.id)');
127
128
        return \count($qb->getQuery()->getResult());
129
    }
130
131
    /**
132
     * Gets the last version of a Term and condition by language.
133
     *
134
     * @return bool | int the version or false if does not exist
135
     */
136
    public function getLastVersion(int $languageId)
137
    {
138
        $qb = $this->createQueryBuilder('l');
139
140
        $result = $qb
141
            ->select('l.version')
142
            ->where(
143
                $qb->expr()->eq('l.language_id', $languageId)
144
            )
145
            ->setMaxResults(1)
146
            ->orderBy('l.version', Criteria::DESC)
147
            ->getQuery()
148
            ->getOneOrNullResult()
149
            ;
150
        if (!empty($result)) {
151
            $version = explode(':', $result);
152
            return (int) $version[0];
153
        }
154
155
        return false;
156
    }
157
}
158