Passed
Push — master ( 58448b...8ed61e )
by Julito
09:32
created

CLpCategory::setUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Entity;
6
7
use Chamilo\CoreBundle\Entity\AbstractResource;
8
use Chamilo\CoreBundle\Entity\ResourceInterface;
9
use Chamilo\CoreBundle\Entity\User;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Criteria;
12
use Doctrine\ORM\Mapping as ORM;
13
use Gedmo\Mapping\Annotation as Gedmo;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * CLpCategory.
18
 *
19
 * @ORM\Table(
20
 *  name="c_lp_category",
21
 * )
22
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
23
 */
24
class CLpCategory extends AbstractResource implements ResourceInterface
25
{
26
    /**
27
     * @var int
28
     *
29
     * @ORM\Column(name="iid", type="integer")
30
     * @ORM\Id
31
     * @ORM\GeneratedValue
32
     */
33
    protected $iid;
34
35
    /**
36
     * @Gedmo\SortableGroup
37
     * @ORM\Column(name="c_id", type="integer")
38
     */
39
    protected $cId;
40
41
    /**
42
     * @var string
43
     * @Assert\NotBlank()
44
     * @ORM\Column(name="name", type="text", nullable=false)
45
     */
46
    protected $name;
47
48
    /**
49
     * @Gedmo\SortablePosition
50
     * @ORM\Column(name="position", type="integer")
51
     */
52
    protected $position;
53
54
    /**
55
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CLpCategoryUser", mappedBy="category", cascade={"persist", "remove"}, orphanRemoval=true)
56
     */
57
    protected $users;
58
59
    /**
60
     * @var ArrayCollection|CLp[]
61
     *
62
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CLp", mappedBy="category", cascade={"detach"})
63
     */
64
    protected $lps;
65
66
    /**
67
     * CLpCategory constructor.
68
     */
69
    public function __construct()
70
    {
71
        $this->users = new ArrayCollection();
72
        $this->lps = new ArrayCollection();
73
    }
74
75
    public function __toString(): string
76
    {
77
        return $this->getName();
78
    }
79
80
    public function getIid()
81
    {
82
        return $this->iid;
83
    }
84
85
    /**
86
     * Set cId.
87
     *
88
     * @param int $cId
89
     *
90
     * @return CLpCategory
91
     */
92
    public function setCId($cId)
93
    {
94
        $this->cId = $cId;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Get cId.
101
     *
102
     * @return int
103
     */
104
    public function getCId()
105
    {
106
        return $this->cId;
107
    }
108
109
    /**
110
     * @param $name
111
     *
112
     * @return $this
113
     */
114
    public function setName($name)
115
    {
116
        $this->name = $name;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Get category name.
123
     *
124
     * @return string
125
     */
126
    public function getName()
127
    {
128
        return (string) $this->name;
129
    }
130
131
    /**
132
     * @param $position
133
     *
134
     * @return $this
135
     */
136
    public function setPosition($position)
137
    {
138
        $this->position = $position;
139
140
        return $this;
141
    }
142
143
    /**
144
     * @return int
145
     */
146
    public function getPosition()
147
    {
148
        return $this->position;
149
    }
150
151
    /**
152
     * @return CLp[]|ArrayCollection
153
     */
154
    public function getLps()
155
    {
156
        return $this->lps;
157
    }
158
159
    /**
160
     * @return ArrayCollection
161
     */
162
    public function getUsers()
163
    {
164
        return $this->users;
165
    }
166
167
    /**
168
     * @param $users
169
     */
170
    public function setUsers($users)
171
    {
172
        $this->users = new ArrayCollection();
173
174
        foreach ($users as $user) {
175
            $this->addUser($user);
176
        }
177
    }
178
179
    public function addUser(CLpCategoryUser $categoryUser)
180
    {
181
        $categoryUser->setCategory($this);
182
183
        if (!$this->hasUser($categoryUser)) {
184
            $this->users->add($categoryUser);
185
        }
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function hasUser(CLpCategoryUser $categoryUser)
192
    {
193
        if ($this->getUsers()->count()) {
194
            $criteria = Criteria::create()->where(
195
                Criteria::expr()->eq('user', $categoryUser->getUser())
196
            )->andWhere(
197
                Criteria::expr()->eq('category', $categoryUser->getCategory())
198
            );
199
200
            $relation = $this->getUsers()->matching($criteria);
201
202
            return $relation->count() > 0;
203
        }
204
205
        return false;
206
    }
207
208
    /**
209
     * @param User $user
210
     *
211
     * @return bool
212
     */
213
    public function hasUserAdded($user)
214
    {
215
        if ($this->getUsers()->count()) {
216
            $categoryUser = new CLpCategoryUser();
217
            $categoryUser->setCategory($this);
218
            $categoryUser->setUser($user);
219
220
            return $this->hasUser($categoryUser);
221
        }
222
223
        return false;
224
    }
225
226
    /**
227
     * @return $this
228
     */
229
    public function removeUsers(CLpCategoryUser $user)
230
    {
231
        $this->users->removeElement($user);
232
233
        return $this;
234
    }
235
236
    /**
237
     * Resource identifier.
238
     */
239
    public function getResourceIdentifier(): int
240
    {
241
        return $this->getIid();
242
    }
243
244
    public function getResourceName(): string
245
    {
246
        return $this->getName();
247
    }
248
249
    public function setResourceName(string $name): self
250
    {
251
        return $this->setName($name);
252
    }
253
}
254