Completed
Push — master ( b75c6e...ecc25c )
by Julito
07:54
created

CAnnouncementRepository   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 89
dl 0
loc 178
rs 10
c 0
b 0
f 0
wmc 23

1 Method

Rating   Name   Duplication   Size   Complexity  
F getAnnouncements() 0 161 23
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\CoreBundle\Repository\ResourceRepository;
9
use Chamilo\CourseBundle\Entity\CGroupInfo;
10
use Chamilo\UserBundle\Entity\User;
11
12
/**
13
 * Class CAnnouncementRepository.
14
 */
15
final class CAnnouncementRepository extends ResourceRepository
16
{
17
    /**
18
     * @param User            $user
19
     * @param Course          $course
20
     * @param CGroupInfo|null $group
21
     * @param Session|null    $session
22
     * @param bool            $allowUserEditSetting
23
     * @param bool            $allowOnlyGroup
24
     * @param bool            $getCount
25
     * @param int|null        $start
26
     * @param int|null        $limit
27
     * @param string          $titleToSearch
28
     * @param User|null       $userToSearch
29
     *
30
     * @return mixed
31
     */
32
    public function getAnnouncements(
33
        User $user,
34
        Course $course,
35
        CGroupInfo $group = null,
36
        Session $session = null,
37
        $allowUserEditSetting,
38
        $allowOnlyGroup,
39
        $getCount = false,
40
        $start = null,
41
        $limit = null,
42
        $titleToSearch = '',
43
        User $userToSearch = null
44
    ) {
45
        $sessionId = $session ? $session->getId() : 0;
46
47
        $conditionSession = api_get_session_condition(
48
            $sessionId,
49
            true,
50
            true,
51
            'announcement.sessionId'
52
        );
53
54
        $select = 'DISTINCT announcement, ip';
55
        $groupBy = 'GROUP BY announcement.iid';
56
57
        if ($getCount) {
58
            $groupBy = '';
59
            $select = 'COUNT(DISTINCT announcement) AS count';
60
        }
61
62
        $parameters = [];
63
        $searchCondition = '';
64
65
        if (!empty($titleToSearch)) {
66
            $parameters['search_title'] = "%$titleToSearch%";
67
            $searchCondition .= " AND (title LIKE :search_title) ";
68
        }
69
70
        if (!empty($userToSearch)) {
71
            $searchCondition .= " AND (ip.insertUser = ".$userToSearch->getId().") ";
72
        }
73
74
        $extraGroupCondition = '';
75
76
        if ($allowOnlyGroup) {
77
            $extraGroupCondition = ' AND ip.group = '.$group->getId().' ';
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $extraGroupCondition = ' AND ip.group = '.$group->/** @scrutinizer ignore-call */ getId().' ';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        }
79
80
        if (api_is_allowed_to_edit(false, true)
81
            || ($allowUserEditSetting && !api_is_anonymous())
82
        ) {
83
            $dqlCondition = "AND (ip.visibility = 0 OR ip.visibility = 1)";
84
85
            if (!empty($group)) {
86
                $dqlCondition = "AND ip.visibility != 2 AND
87
                    (ip.group = ".$group->getId()." OR ip.group IS NULL )
88
                    $extraGroupCondition";
89
            }
90
        } else {
91
            $groupMemberships = $user->getCourseGroupsAsMemberFromCourse($course);
92
            $tutoredGroups = $user->getCourseGroupsAsTutorFromCourse($course);
93
            $memberships = array_merge($groupMemberships->toArray(), $tutoredGroups->toArray());
94
95
            if (!empty($memberships)) {
96
                if ($allowUserEditSetting && !api_is_anonymous()) {
97
                    $parameters['memberships'] = $memberships;
98
                    $condUserId = " AND (
99
                            ip.lasteditUserId = ".$user->getId()." OR(
100
                                (ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) OR
101
                                (ip.group IS NULL OR ip.group = 0 OR ip.group IN :memberships)
102
                            )
103
                        ) ";
104
105
                    if (!empty($group)) {
106
                        unset($parameters['memberships']);
107
                        $condUserId = " AND (
108
                                ip.lasteditUserId = ".$user->getId()." OR ip.group IS NULL OR ip.group IN (0, ".$group->getId()
109
                            .")
110
                            ) ".$extraGroupCondition;
111
                    }
112
                } else {
113
                    $parameters['memberships'] = $memberships;
114
                    $condUserId = " AND (
115
                            (ip.toUser = ".$user->getId().") OR ip.toUser IS NULL) AND
116
                            (ip.group IS NULL OR ip.group = 0 OR ip.group IN :memberships)
117
                        ) ";
118
119
                    if (!empty($group)) {
120
                        unset($parameters['memberships']);
121
                        $condUserId = " AND (
122
                            (ip.toUser = ".$user->getId().") OR ip.toUser IS NULL) AND
123
                            (ip.group IS NULL OR ip.group IN (0, ".$group->getId()."))
124
                        ) ".$extraGroupCondition;
125
                    }
126
                }
127
128
                $dqlCondition = "$condUserId AND ip.visibility = 1";
129
            } else {
130
                if (!empty($user->getId())) {
131
                    $condUserId = " AND (
132
                            (ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) AND
133
                            (ip.group = 0 OR ip.group IS NULL)
134
                        ) ";
135
136
                    if ($allowUserEditSetting && !api_is_anonymous()) {
137
                        $condUserId = " AND (
138
                                ip.lasteditUserId = ".$user->getId()." OR
139
                                (
140
                                    (ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) AND
141
                                    (ip.group = 0 OR ip.group IS NULL)
142
                                )
143
                            ) ";
144
                    }
145
146
                    $dqlCondition = "$condUserId
147
                        AND ip.visibility = 1
148
                        AND announcement.sessionId IN (0, $sessionId)";
149
                } else {
150
                    $condUserId = " AND ip.group = 0 OR ip.group IS NULL ";
151
152
                    if ($allowUserEditSetting && !api_is_anonymous()) {
153
                        $condUserId = " AND (
154
                                ip.lastEditUserId = ".$user->getId()." OR ip.group = 0 OR ip.group IS NULL
155
                            ) ";
156
                    }
157
158
                    $dqlCondition = "$condUserId
159
                        AND ip.visibility = 1
160
                        AND announcement.sessionId IN (0, $sessionId)";
161
                }
162
            }
163
        }
164
165
        $dql = "SELECT $select
166
            FROM ChamiloCourseBundle:CAnnouncement announcement
167
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
168
                WITH (announcement.id = ip.ref AND announcement.cId = ip.course)
169
            WHERE
170
                ip.tool = 'announcement' AND
171
                announcement.cId = ".$course->getId()."
172
                $dqlCondition
173
                $conditionSession
174
                $searchCondition
175
            $groupBy
176
            ORDER BY announcement.displayOrder DESC";
177
178
        $query = $this->getEntityManager()
179
            ->createQuery($dql)
180
            ->setParameters($parameters);
181
182
        if (!is_null($start) && !is_null($limit)) {
183
            $query
184
                ->setFirstResult($start)
185
                ->setMaxResults($limit);
186
        }
187
188
        if ($getCount) {
189
            return $query->getResult();
190
        }
191
192
        return $query->getResult();
193
    }
194
}
195