Completed
Push — master ( db9c88...be5baf )
by Julito
14:58
created

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