Passed
Push — master ( b34284...653853 )
by Angel Fernando Quiroz
12:36
created

PortfolioRepository::findTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Repository\Node;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ExtraField;
11
use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
12
use Chamilo\CoreBundle\Entity\Portfolio;
13
use Chamilo\CoreBundle\Entity\Session;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CoreBundle\Repository\ResourceRepository;
16
use DateTime;
17
use Doctrine\ORM\Query\Expr\Join;
18
use Doctrine\Persistence\ManagerRegistry;
19
20
class PortfolioRepository extends ResourceRepository
21
{
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, Portfolio::class);
25
    }
26
27
    public function findItemsByUser(
28
        User $user,
29
        ?Course $course,
30
        ?Session $session,
31
        ?array $orderBy = null,
32
        array $visibility = []
33
    ): array {
34
        $criteria = [];
35
        $criteria['user'] = $user;
36
37
        if ($course) {
38
            $criteria['course'] = $course;
39
            $criteria['session'] = $session;
40
        }
41
42
        if ($visibility) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $visibility of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $criteria['visibility'] = $visibility;
44
        }
45
46
        return $this->findBy($criteria, $orderBy);
47
    }
48
49
    public function findTemplates(User $creator, ?Course $course, ?Session $session)
50
    {
51
        $qb = $this->getResourcesByCourse($course, $session);
52
53
        $this->addCreatorQueryBuilder($creator, $qb);
54
55
        return $qb
56
            ->andWhere($qb->expr()->eq('resource.isTemplate', true))
57
            ->getQuery()
58
            ->getResult()
59
        ;
60
    }
61
62
    public function getIndexCourseItems(
63
        User $currentUser,
64
        User $owner,
65
        Course $course,
66
        ?Session $session = null,
67
        bool $showBaseContentInSession = false,
68
        bool $listByUser = false,
69
        ?DateTime $date = null,
70
        array $tags = [],
71
        ?string $searchText = null,
72
        array $searchCategories = [],
73
        array $searchNoInCategories = [],
74
        bool $advancedSharingEnabled = false
75
    ): array {
76
        $queryBuilder = $this->getResources();
77
        $this->addCourseQueryBuilder($course, $queryBuilder);
78
79
        if ($session) {
80
            if ($showBaseContentInSession) {
81
                $this->addSessionAndBaseContentQueryBuilder($session, $queryBuilder);
82
            } else {
83
                $this->addSessionOnlyQueryBuilder($session, $queryBuilder);
84
            }
85
        } else {
86
            $this->addSessionNullQueryBuilder($queryBuilder);
87
        }
88
89
        if ($date) {
90
            $queryBuilder
91
                ->andWhere('resource.creationDate >= :date')
92
                ->setParameter(':date', $date)
93
            ;
94
        }
95
96
        if ($tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            $queryBuilder
98
                ->innerJoin(ExtraFieldRelTag::class, 'efrt', Join::WITH, 'efrt.itemId = resource.id')
99
                ->innerJoin(ExtraField::class, 'ef', Join::WITH, 'ef.id = efrt.fieldId')
100
                ->andWhere('ef.extraFieldType = :efType')
101
                ->andWhere('ef.variable = :variable')
102
                ->andWhere('efrt.tagId IN (:tags)')
103
            ;
104
105
            $queryBuilder->setParameter('efType', ExtraField::PORTFOLIO_TYPE);
106
            $queryBuilder->setParameter('variable', 'tags');
107
            $queryBuilder->setParameter('tags', $tags);
108
        }
109
110
        if (!empty($searchText)) {
111
            $queryBuilder->andWhere(
112
                $queryBuilder->expr()->orX(
113
                    $queryBuilder->expr()->like('resource.title', ':text'),
114
                    $queryBuilder->expr()->like('resource.content', ':text')
115
                )
116
            );
117
118
            $queryBuilder->setParameter('text', '%'.$searchText.'%');
119
        }
120
121
        if ($searchCategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchCategories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
            $queryBuilder->andWhere(
123
                $queryBuilder->expr()->in('resource.category', $searchCategories)
124
            );
125
        }
126
127
        if ($searchNoInCategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchNoInCategories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
128
            $queryBuilder->andWhere('resource.category NOT IN('.implode(',', $searchNoInCategories).')');
129
        }
130
131
        if ($listByUser) {
132
            $queryBuilder
133
                ->andWhere('resource.user = :user')
134
                ->setParameter('user', $owner)
135
            ;
136
        }
137
138
        if ($advancedSharingEnabled) {
139
            $queryBuilder->andWhere(
140
                $queryBuilder->expr()->orX(
141
                    $queryBuilder->expr()->eq('resource.visibility', Portfolio::VISIBILITY_VISIBLE),
142
                    $queryBuilder->expr()->eq('links.user', ':current_user')
143
                )
144
            );
145
        } else {
146
            $visibilityCriteria = [Portfolio::VISIBILITY_VISIBLE];
147
148
            if (api_is_allowed_to_edit()) {
149
                $visibilityCriteria[] = Portfolio::VISIBILITY_HIDDEN_EXCEPT_TEACHER;
150
            }
151
152
            $queryBuilder->andWhere(
153
                $queryBuilder->expr()->orX(
154
                    'node.creator = :current_user',
155
                    $queryBuilder->expr()->andX(
156
                        'node.creator != :current_user',
157
                        $queryBuilder->expr()->in('resource.visibility', $visibilityCriteria)
158
                    )
159
                )
160
            );
161
        }
162
163
        $queryBuilder
164
            ->setParameter('current_user', $currentUser->getId())
165
            ->orderBy('node.createdAt', 'DESC')
166
        ;
167
168
        return $queryBuilder->getQuery()->getResult();
169
    }
170
}
171