Completed
Push — master ( cbfb7e...6f5084 )
by Julito
20:33
created

CLpCategory::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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