Passed
Push — master ( 76abd2...ab5d59 )
by Julito
10:01
created

ItemPropertyRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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