Passed
Push — 1.11.x ( b90206...a9dc20 )
by Angel Fernando Quiroz
11:15
created

findByUserSuscribedToItem()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 1
nop 5
dl 0
loc 18
rs 9.9666
c 2
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity\Repository;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\CourseBundle\Entity\CItemProperty;
9
use Chamilo\UserBundle\Entity\Group;
10
use Chamilo\UserBundle\Entity\User;
11
use Doctrine\ORM\EntityRepository;
12
13
/**
14
 * Class ItemPropertyRepository.
15
 */
16
class ItemPropertyRepository extends EntityRepository
17
{
18
    /**
19
     * Get users subscribed to a item LP, Document, etc (item_property).
20
     *
21
     * @param string  $tool    learnpath | document | etc
22
     * @param int     $itemId
23
     * @param Session $session
24
     * @param Group   $group
25
     *
26
     * @return array
27
     */
28
    public function getUsersSubscribedToItem(
29
        $tool,
30
        $itemId,
31
        Course $course,
32
        Session $session = null,
33
        Group $group = null
34
    ) {
35
        $criteria = [
36
            'tool' => $tool,
37
            'lasteditType' => 'LearnpathSubscription',
38
            'ref' => $itemId,
39
            'course' => $course,
40
            'session' => $session,
41
            'group' => $group,
42
        ];
43
44
        return $this->findBy($criteria);
45
    }
46
47
    public function findByUserSuscribedToItem(
48
        $tool,
49
        $itemId,
50
        int $userId,
51
        int $courseId,
52
        int $sessionId = null
53
    ): ?CItemProperty {
54
        $criteria = [
55
            'tool' => $tool,
56
            'lasteditType' => 'LearnpathSubscription',
57
            'ref' => $itemId,
58
            'toUser' => $userId,
59
            'course' => $courseId,
60
            'session' => $sessionId ?: null,
61
            'group' => null,
62
        ];
63
64
        return $this->findOneBy($criteria);
65
    }
66
67
    public function findByGroupSuscribedToLp(
68
        $tool,
69
        $lpId,
70
        int $groupId,
71
        int $courseId,
72
        int $sessionId = 0
73
    ): ?CItemProperty {
74
        $criteria = [
75
            'tool' => $tool,
76
            'lasteditType' => 'LearnpathSubscription',
77
            'ref' => $lpId,
78
            'toUser' => null,
79
            'course' => $courseId,
80
            'session' => $sessionId ?: null,
81
            'group' => $groupId,
82
        ];
83
84
        return $this->findOneBy($criteria);
85
    }
86
87
    /**
88
     * Get Groups subscribed to a item: LP, Doc, etc.
89
     *
90
     * @param string  $tool    learnpath | document | etc
91
     * @param int     $itemId
92
     * @param Session $session
93
     *
94
     * @return array
95
     */
96
    public function getGroupsSubscribedToItem(
97
        $tool,
98
        $itemId,
99
        Course $course,
100
        Session $session = null
101
    ) {
102
        $criteria = [
103
            'tool' => $tool,
104
            'lasteditType' => 'LearnpathSubscription',
105
            'ref' => $itemId,
106
            'course' => $course,
107
            'session' => $session,
108
            'toUser' => null,
109
        ];
110
111
        return $this->findBy($criteria);
112
    }
113
114
    /**
115
     * Subscribe groups to a LP, doc (itemproperty).
116
     *
117
     * @param User    $currentUser
118
     * @param string  $tool        learnpath | document | etc
119
     * @param Session $session
120
     * @param int     $itemId
121
     * @param array   $newList
122
     */
123
    public function subscribeGroupsToItem(
124
        $currentUser,
125
        $tool,
126
        Course $course,
127
        Session $session = null,
128
        $itemId,
129
        $newList = []
130
    ) {
131
        $em = $this->getEntityManager();
132
        $groupsSubscribedToItem = $this->getGroupsSubscribedToItem(
133
            $tool,
134
            $itemId,
135
            $course,
136
            $session
137
        );
138
139
        $alreadyAdded = [];
140
        if ($groupsSubscribedToItem) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupsSubscribedToItem 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...
141
            /** @var CItemProperty $itemProperty */
142
            foreach ($groupsSubscribedToItem as $itemProperty) {
143
                $getGroup = $itemProperty->getGroup();
144
                if (!empty($getGroup)) {
145
                    $alreadyAdded[] = $getGroup->getId();
146
                }
147
            }
148
        }
149
150
        $toDelete = $alreadyAdded;
151
152
        if (!empty($newList)) {
153
            $toDelete = array_diff($alreadyAdded, $newList);
154
        }
155
156
        if ($toDelete) {
157
            $this->unsubscribeGroupsToItem(
158
                $tool,
159
                $course,
160
                $session,
161
                $itemId,
162
                $toDelete,
163
                true
164
            );
165
        }
166
167
        foreach ($newList as $groupId) {
168
            if (!in_array($groupId, $alreadyAdded)) {
169
                $item = new CItemProperty($course);
170
                $groupObj = $em->find('ChamiloCourseBundle:CGroupInfo', $groupId);
171
                $item->setGroup($groupObj);
172
                $item->setTool($tool);
173
                $item->setRef($itemId);
174
                $item->setInsertUser($currentUser);
175
176
                if (!empty($session)) {
177
                    $item->setSession($session);
178
                }
179
                $item->setLasteditType('LearnpathSubscription');
180
                $item->setVisibility('1');
181
                $em->persist($item); //$em is an instance of EntityManager
182
            }
183
        }
184
185
        $em->flush();
186
    }
187
188
    /**
189
     * Unsubscribe groups to item.
190
     *
191
     * @param string  $tool
192
     * @param Session $session
193
     * @param int     $itemId
194
     * @param array   $groups
195
     * @param bool    $unsubscribeUserToo
196
     */
197
    public function unsubscribeGroupsToItem(
198
        $tool,
199
        Course $course,
200
        Session $session = null,
201
        $itemId,
202
        $groups,
203
        $unsubscribeUserToo = false
204
    ) {
205
        if (!empty($groups)) {
206
            $em = $this->getEntityManager();
207
208
            foreach ($groups as $groupId) {
209
                $item = $this->findOneBy([
210
                    'tool' => $tool,
211
                    'session' => $session,
212
                    'ref' => $itemId,
213
                    'group' => $groupId,
214
                ]);
215
                if ($item) {
216
                    $em->remove($item);
217
                }
218
219
                if ($unsubscribeUserToo) {
220
                    //Adding users from this group to the item
221
                    $users = \GroupManager::getStudentsAndTutors($groupId);
222
                    $newUserList = [];
223
                    if (!empty($users)) {
224
                        foreach ($users as $user) {
225
                            $newUserList[] = $user['user_id'];
226
                        }
227
                        $this->unsubcribeUsersToItem(
228
                            'learnpath',
229
                            $course,
230
                            $session,
231
                            $itemId,
232
                            $newUserList
233
                        );
234
                    }
235
                }
236
            }
237
            $em->flush();
238
        }
239
    }
240
241
    /**
242
     * Subscribe users to a LP, doc (itemproperty).
243
     *
244
     * @param User    $currentUser
245
     * @param string  $tool
246
     * @param Session $session
247
     * @param int     $itemId
248
     * @param array   $newUserList
249
     * @param bool    $deleteUsers
250
     */
251
    public function subscribeUsersToItem(
252
        $currentUser,
253
        $tool,
254
        Course $course,
255
        Session $session = null,
256
        $itemId,
257
        $newUserList = [],
258
        $deleteUsers = true
259
    ) {
260
        $em = $this->getEntityManager();
261
        $user = $em->getRepository('ChamiloUserBundle:User');
262
263
        $usersSubscribedToItem = $this->getUsersSubscribedToItem(
264
            $tool,
265
            $itemId,
266
            $course,
267
            $session
268
        );
269
270
        $alreadyAddedUsers = [];
271
        if ($usersSubscribedToItem) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $usersSubscribedToItem 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...
272
            /** @var CItemProperty $itemProperty */
273
            foreach ($usersSubscribedToItem as $itemProperty) {
274
                $getToUser = $itemProperty->getToUser();
275
                if (!empty($getToUser)) {
276
                    $alreadyAddedUsers[] = $itemProperty->getToUser()->getId();
277
                }
278
            }
279
        }
280
281
        if ($deleteUsers) {
282
            $usersToDelete = $alreadyAddedUsers;
283
            if (!empty($newUserList)) {
284
                $usersToDelete = array_diff($alreadyAddedUsers, $newUserList);
285
            }
286
287
            if ($usersToDelete) {
288
                $this->unsubcribeUsersToItem(
289
                    $tool,
290
                    $course,
291
                    $session,
292
                    $itemId,
293
                    $usersToDelete
294
                );
295
            }
296
        }
297
298
        foreach ($newUserList as $userId) {
299
            if (!in_array($userId, $alreadyAddedUsers)) {
300
                $userObj = $user->find($userId);
301
302
                $item = new CItemProperty($course);
303
                $item
304
                    ->setToUser($userObj)
305
                    ->setTool($tool)
306
                    ->setInsertUser($currentUser)
307
                    ->setRef($itemId);
308
309
                if (!empty($session)) {
310
                    $item->setSession($session);
311
                }
312
                $item->setLasteditType('LearnpathSubscription');
313
                $item->setVisibility('1');
314
                $em->persist($item); //$em is an instance of EntityManager
315
            }
316
        }
317
318
        $em->flush();
319
    }
320
321
    /**
322
     * Unsubscribe users to item.
323
     *
324
     * @param string  $tool
325
     * @param Session $session
326
     * @param int     $itemId
327
     * @param array   $usersToDelete
328
     */
329
    public function unsubcribeUsersToItem(
330
        $tool,
331
        Course $course,
332
        Session $session = null,
333
        $itemId,
334
        $usersToDelete
335
    ) {
336
        $em = $this->getEntityManager();
337
338
        if (!empty($usersToDelete)) {
339
            foreach ($usersToDelete as $userId) {
340
                $item = $this->findOneBy(
341
                    [
342
                        'tool' => $tool,
343
                        'session' => $session,
344
                        'ref' => $itemId,
345
                        'toUser' => $userId,
346
                    ]
347
                );
348
                if ($item) {
349
                    $em->remove($item);
350
                }
351
            }
352
            $em->flush();
353
        }
354
    }
355
}
356