Passed
Pull Request — 1.11.x (#3859)
by Angel Fernando Quiroz
09:26
created

findByUserSuscribedToItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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