Passed
Push — dependabot/composer/symfony/se... ( 6a3ee0...ae0984 )
by
unknown
28:58 queued 18:58
created

Skill::setSkills()   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
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
10
use ApiPlatform\Metadata\ApiFilter;
11
use ApiPlatform\Metadata\ApiResource;
12
use ApiPlatform\Metadata\Delete;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\GetCollection;
15
use ApiPlatform\Metadata\Patch;
16
use ApiPlatform\Metadata\Post;
17
use ApiPlatform\Metadata\Put;
18
use Chamilo\CoreBundle\ApiResource\SkillTreeNode;
19
use Chamilo\CoreBundle\Repository\SkillRepository;
20
use Chamilo\CoreBundle\State\SkillTreeStateProvider;
21
use DateTime;
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\Common\Collections\Collection;
24
use Doctrine\ORM\Mapping as ORM;
25
use Gedmo\Mapping\Annotation as Gedmo;
26
use Gedmo\Translatable\Translatable;
27
use Stringable;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
use Symfony\Component\Validator\Constraints as Assert;
30
31
#[ApiResource(
32
    operations: [
33
        new Post(),
34
        new Patch(),
35
        new Put(),
36
        new Delete(),
37
        new GetCollection(
38
            uriTemplate: '/skills/tree.{_format}',
39
            paginationEnabled: false,
40
            normalizationContext: [
41
                'groups' => ['skill:tree:read']
42
            ],
43
            output: SkillTreeNode::class,
44
            provider: SkillTreeStateProvider::class
45
        ),
46
        new GetCollection(),
47
        new Get(),
48
    ],
49
    normalizationContext: [
50
        'groups' => ['skill:read'],
51
    ],
52
    security: "is_granted('ROLE_ADMIN')"
53
)]
54
#[ApiFilter(SearchFilter::class, properties: ['issuedSkills.user' => 'exact'])]
55
#[ORM\Table(name: 'skill')]
56
#[ORM\Entity(repositoryClass: SkillRepository::class)]
57
class Skill implements Stringable, Translatable
58
{
59
    public const STATUS_DISABLED = 0;
60
    public const STATUS_ENABLED = 1;
61
62
    #[Groups(['skill:read'])]
63
    #[ORM\Column(name: 'id', type: 'integer')]
64
    #[ORM\Id]
65
    #[ORM\GeneratedValue]
66
    protected ?int $id = null;
67
68
    #[ORM\ManyToOne(targetEntity: SkillLevelProfile::class, inversedBy: 'skills')]
69
    #[ORM\JoinColumn(name: 'profile_id', referencedColumnName: 'id')]
70
    protected ?SkillLevelProfile $levelProfile = null;
71
72
    /**
73
     * @var Collection<int, SkillRelUser>
74
     */
75
    #[Groups(['skill:read'])]
76
    #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelUser::class, cascade: ['persist'])]
77
    protected Collection $issuedSkills;
78
79
    /**
80
     * @var Collection<int, SkillRelItem>
81
     */
82
    #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelItem::class, cascade: ['persist'])]
83
    protected Collection $items;
84
85
    /**
86
     * @var Collection<int, SkillRelSkill>
87
     */
88
    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: SkillRelSkill::class, cascade: ['persist'])]
89
    protected Collection $skills;
90
91
    /**
92
     * @var Collection<int, SkillRelCourse>
93
     */
94
    #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelCourse::class, cascade: ['persist'])]
95
    protected Collection $courses;
96
97
    /**
98
     * @var Collection<int, SkillRelGradebook>
99
     */
100
    #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelGradebook::class, cascade: ['persist'])]
101
    protected Collection $gradeBookCategories;
102
103
    #[Gedmo\Translatable]
104
    #[Assert\NotBlank]
105
    #[Groups(['skill:read', 'skill:write', 'skill_rel_user:read'])]
106
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
107
    protected string $title;
108
109
    #[Gedmo\Translatable]
110
    #[Assert\NotBlank]
111
    #[Groups(['skill:read', 'skill:write'])]
112
    #[ORM\Column(name: 'short_code', type: 'string', length: 100, nullable: false)]
113
    protected string $shortCode;
114
115
    #[Groups(['skill:read', 'skill:write'])]
116
    #[ORM\Column(name: 'description', type: 'text', nullable: false)]
117
    protected string $description;
118
119
    #[Assert\NotNull]
120
    #[ORM\Column(name: 'access_url_id', type: 'integer', nullable: false)]
121
    protected int $accessUrlId;
122
123
    #[Groups(['skill:read', 'skill_rel_user:read'])]
124
    #[ORM\Column(name: 'icon', type: 'string', length: 255, nullable: false)]
125
    protected string $icon;
126
127
    #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['persist', 'remove'])]
128
    #[ORM\JoinColumn(name: 'asset_id', referencedColumnName: 'id')]
129
    protected ?Asset $asset = null;
130
131
    #[ORM\Column(name: 'criteria', type: 'text', nullable: true)]
132
    protected ?string $criteria = null;
133
134
    #[ORM\Column(name: 'status', type: 'integer', nullable: false, options: ['default' => 1])]
135
    protected int $status;
136
137
    #[Gedmo\Timestampable(on: 'update')]
138
    #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: false)]
139
    protected DateTime $updatedAt;
140
141
    #[Gedmo\Locale]
142
    private ?string $locale = null;
143
144
    public function __construct()
145
    {
146
        $this->issuedSkills = new ArrayCollection();
147
        $this->items = new ArrayCollection();
148
        $this->courses = new ArrayCollection();
149
        $this->gradeBookCategories = new ArrayCollection();
150
        $this->skills = new ArrayCollection();
151
        $this->icon = '';
152
        $this->description = '';
153
        $this->status = self::STATUS_ENABLED;
154
    }
155
156
    public function __toString(): string
157
    {
158
        return $this->getTitle();
159
    }
160
161
    public function setTitle(string $title): self
162
    {
163
        $this->title = $title;
164
165
        return $this;
166
    }
167
168
    public function getTitle(): string
169
    {
170
        return $this->title;
171
    }
172
173
    public function getShortCode(): string
174
    {
175
        return $this->shortCode;
176
    }
177
178
    public function setShortCode(string $shortCode): self
179
    {
180
        $this->shortCode = $shortCode;
181
182
        return $this;
183
    }
184
185
    public function setDescription(string $description): self
186
    {
187
        $this->description = $description;
188
189
        return $this;
190
    }
191
192
    public function getDescription(): string
193
    {
194
        return $this->description;
195
    }
196
197
    public function setAccessUrlId(int $accessUrlId): static
198
    {
199
        $this->accessUrlId = $accessUrlId;
200
201
        return $this;
202
    }
203
204
    public function getAccessUrlId(): int
205
    {
206
        return $this->accessUrlId;
207
    }
208
209
    public function setIcon(string $icon): self
210
    {
211
        $this->icon = $icon;
212
213
        return $this;
214
    }
215
216
    public function getIcon(): string
217
    {
218
        return $this->icon;
219
    }
220
221
    public function setCriteria(string $criteria): self
222
    {
223
        $this->criteria = $criteria;
224
225
        return $this;
226
    }
227
228
    public function getCriteria(): ?string
229
    {
230
        return $this->criteria;
231
    }
232
233
    public function setStatus(int $status): self
234
    {
235
        $this->status = $status;
236
237
        return $this;
238
    }
239
240
    public function getStatus(): int
241
    {
242
        return $this->status;
243
    }
244
245
    public function setUpdatedAt(DateTime $updatedAt): static
246
    {
247
        $this->updatedAt = $updatedAt;
248
249
        return $this;
250
    }
251
252
    public function getUpdatedAt(): DateTime
253
    {
254
        return $this->updatedAt;
255
    }
256
257
    public function getId(): ?int
258
    {
259
        return $this->id;
260
    }
261
262
    public function getLevelProfile(): ?SkillLevelProfile
263
    {
264
        return $this->levelProfile;
265
    }
266
267
    public function setLevelProfile(SkillLevelProfile $levelProfile): self
268
    {
269
        $this->levelProfile = $levelProfile;
270
271
        return $this;
272
    }
273
274
    /**
275
     * @return Collection<int, SkillRelUser>
276
     */
277
    public function getIssuedSkills(): Collection
278
    {
279
        return $this->issuedSkills;
280
    }
281
282
    /**
283
     * @return Collection<int, SkillRelItem>
284
     */
285
    public function getItems(): Collection
286
    {
287
        return $this->items;
288
    }
289
290
    public function setItems(ArrayCollection $items): self
291
    {
292
        $this->items = $items;
293
294
        return $this;
295
    }
296
297
    public function hasItem(int $typeId, int $itemId): bool
298
    {
299
        if (0 !== $this->getItems()->count()) {
300
            $found = false;
301
302
            /** @var SkillRelItem $item */
303
            foreach ($this->getItems() as $item) {
304
                if ($item->getItemId() === $itemId && $item->getItemType() === $typeId) {
305
                    $found = true;
306
307
                    break;
308
                }
309
            }
310
311
            return $found;
312
        }
313
314
        return false;
315
    }
316
317
    public function addItem(SkillRelItem $skillRelItem): void
318
    {
319
        $skillRelItem->setSkill($this);
320
        $this->items[] = $skillRelItem;
321
    }
322
323
    public function getCourses(): Collection
324
    {
325
        return $this->courses;
326
    }
327
328
    public function setCourses(ArrayCollection $courses): self
329
    {
330
        $this->courses = $courses;
331
332
        return $this;
333
    }
334
335
    /**
336
     * @return Collection<int, SkillRelSkill>
337
     */
338
    public function getSkills(): Collection
339
    {
340
        return $this->skills;
341
    }
342
343
    /**
344
     * @param Collection<int, SkillRelSkill> $skills
345
     */
346
    public function setSkills(Collection $skills): self
347
    {
348
        $this->skills = $skills;
349
350
        return $this;
351
    }
352
353
    /**
354
     * @return Collection<int, SkillRelGradebook>
355
     */
356
    public function getGradeBookCategories(): Collection
357
    {
358
        return $this->gradeBookCategories;
359
    }
360
361
    /**
362
     * @param Collection<int, SkillRelGradebook> $gradeBookCategories
363
     */
364
    public function setGradeBookCategories(Collection $gradeBookCategories): self
365
    {
366
        $this->gradeBookCategories = $gradeBookCategories;
367
368
        return $this;
369
    }
370
371
    public function hasAsset(): bool
372
    {
373
        return null !== $this->asset;
374
    }
375
376
    public function getAsset(): ?Asset
377
    {
378
        return $this->asset;
379
    }
380
381
    public function setAsset(?Asset $asset): self
382
    {
383
        $this->asset = $asset;
384
385
        return $this;
386
    }
387
388
    public function hasCourseAndSession(SkillRelCourse $searchItem): bool
389
    {
390
        if (0 !== $this->getCourses()->count()) {
391
            $found = false;
392
393
            /** @var SkillRelCourse $item */
394
            foreach ($this->getCourses() as $item) {
395
                $sessionPassFilter = false;
396
                $session = $item->getSession();
397
                $sessionId = empty($session) ? 0 : $session->getId();
398
                $searchSessionId = empty($searchItem->getSession()) ? 0 : $searchItem->getSession()->getId();
399
                if ($sessionId === $searchSessionId) {
400
                    $sessionPassFilter = true;
401
                }
402
                if ($item->getCourse()->getId() === $searchItem->getCourse()->getId() && $sessionPassFilter) {
403
                    $found = true;
404
405
                    break;
406
                }
407
            }
408
409
            return $found;
410
        }
411
412
        return false;
413
    }
414
415
    public function addToCourse(SkillRelCourse $item): void
416
    {
417
        $item->setSkill($this);
418
        $this->courses[] = $item;
419
    }
420
421
    public function getLocale(): string
422
    {
423
        return $this->locale;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->locale could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
424
    }
425
426
    public function setLocale(string $locale): self
427
    {
428
        $this->locale = $locale;
429
430
        return $this;
431
    }
432
433
    /**
434
     * @return Collection<int, Skill>
435
     */
436
    public function getChildSkills(): Collection
437
    {
438
        return $this
439
            ->getSkills()
440
            ->map(fn(SkillRelSkill $skillRelSkill): Skill => $skillRelSkill->getSkill())
441
        ;
442
    }
443
}
444