Passed
Pull Request — master (#5897)
by
unknown
09:27
created

CStudentPublication::getGroupCategoryWorkId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface;
10
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Post;
17
use ApiPlatform\Metadata\Put;
18
use Chamilo\CoreBundle\Entity\AbstractResource;
19
use Chamilo\CoreBundle\Entity\ResourceInterface;
20
use Chamilo\CoreBundle\Entity\ResourceNode;
21
use Chamilo\CoreBundle\Entity\Session;
22
use Chamilo\CoreBundle\Entity\User;
23
use Chamilo\CoreBundle\Filter\CidFilter;
24
use Chamilo\CoreBundle\Filter\SidFilter;
25
use Chamilo\CoreBundle\State\CStudentPublicationPostStateProcessor;
26
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
27
use DateTime;
28
use Doctrine\Common\Collections\ArrayCollection;
29
use Doctrine\Common\Collections\Collection;
30
use Doctrine\ORM\Mapping as ORM;
31
use Stringable;
32
use Symfony\Component\Serializer\Annotation\Groups;
33
use Symfony\Component\Validator\Constraints as Assert;
34
35
#[ORM\Table(name: 'c_student_publication')]
36
#[ORM\Entity(repositoryClass: CStudentPublicationRepository::class)]
37
#[ApiResource(
38
    operations: [
39
        new Put(security: "is_granted('EDIT', object.resourceNode)"),
40
        new Get(
41
            normalizationContext: [
42
                'groups' => ['student_publication:read', 'student_publication:item:get'],
43
            ],
44
            security: "is_granted('VIEW', object.resourceNode)",
45
        ),
46
        new GetCollection(),
47
        new Delete(security: "is_granted('DELETE', object.resourceNode)"),
48
        new Post(
49
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
50
            processor: CStudentPublicationPostStateProcessor::class
51
        ),
52
    ],
53
    normalizationContext: [
54
        'groups' => ['student_publication:read'],
55
    ],
56
    denormalizationContext: [
57
        'groups' => ['c_student_publication:write'],
58
    ],
59
    order: ['sentDate' => 'DESC'],
60
)]
61
#[ApiFilter(
62
    OrderFilter::class,
63
    properties: [
64
        'title',
65
        'sentDate' => ['nulls_comparison' => OrderFilterInterface::NULLS_SMALLEST],
66
        'assignment.expiresOn' => ['nulls_comparison' => OrderFilterInterface::NULLS_SMALLEST],
67
        'assingment.endsOn' => ['nulls_comparison' => OrderFilterInterface::NULLS_SMALLEST],
68
    ]
69
)]
70
#[ApiFilter(filterClass: CidFilter::class)]
71
#[ApiFilter(filterClass: SidFilter::class)]
72
class CStudentPublication extends AbstractResource implements ResourceInterface, Stringable
73
{
74
    #[Groups(['c_student_publication:write'])]
75
    public bool $addToGradebook = false;
76
77
    #[Groups(['c_student_publication:write'])]
78
    public int $gradebookCategoryId = 0;
79
80
    #[Groups(['c_student_publication:write'])]
81
    public bool $addToCalendar = false;
82
    #[ORM\Column(name: 'iid', type: 'integer')]
83
    #[ORM\Id]
84
    #[ORM\GeneratedValue]
85
    protected ?int $iid = null;
86
87
    #[Assert\NotBlank]
88
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
89
    #[Groups(['c_student_publication:write', 'student_publication:read'])]
90
    protected string $title;
91
92
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
93
    #[Groups(['c_student_publication:write', 'student_publication:item:get'])]
94
    protected ?string $description;
95
96
    #[ORM\Column(name: 'author', type: 'string', length: 255, nullable: true)]
97
    protected ?string $author = null;
98
99
    #[ORM\Column(name: 'active', type: 'integer', nullable: true)]
100
    protected ?int $active = null;
101
102
    #[ORM\Column(name: 'accepted', type: 'boolean', nullable: true)]
103
    protected ?bool $accepted = null;
104
105
    #[ORM\Column(name: 'post_group_id', type: 'integer', nullable: false)]
106
    protected int $postGroupId;
107
108
    #[ORM\Column(name: 'sent_date', type: 'datetime', nullable: true)]
109
    #[Groups(['student_publication:read'])]
110
    protected ?DateTime $sentDate;
111
112
    #[Assert\NotBlank]
113
    #[Assert\Choice(callback: 'getFileTypes')]
114
    #[ORM\Column(name: 'filetype', type: 'string', length: 10, nullable: false)]
115
    protected string $filetype;
116
117
    #[ORM\Column(name: 'has_properties', type: 'integer', nullable: false)]
118
    protected int $hasProperties;
119
120
    #[ORM\Column(name: 'view_properties', type: 'boolean', nullable: true)]
121
    protected ?bool $viewProperties = null;
122
123
    #[ORM\Column(name: 'qualification', type: 'float', precision: 6, scale: 2, nullable: false)]
124
    #[Groups(['c_student_publication:write', 'student_publication:read'])]
125
    protected float $qualification;
126
127
    #[ORM\Column(name: 'date_of_qualification', type: 'datetime', nullable: true)]
128
    protected ?DateTime $dateOfQualification = null;
129
130
    /**
131
     * @var Collection<int, CStudentPublication>
132
     */
133
    #[ORM\OneToMany(mappedBy: 'publicationParent', targetEntity: self::class)]
134
    protected Collection $children;
135
136
    /**
137
     * @var Collection<int, CStudentPublicationComment>
138
     */
139
    #[ORM\OneToMany(mappedBy: 'publication', targetEntity: CStudentPublicationComment::class)]
140
    protected Collection $comments;
141
142
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
143
    #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'iid')]
144
    protected ?CStudentPublication $publicationParent;
145
146
    #[ORM\ManyToOne(targetEntity: User::class)]
147
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
148
    protected User $user;
149
150
    #[Groups(['c_student_publication:write', 'student_publication:read'])]
151
    #[ORM\OneToOne(mappedBy: 'publication', targetEntity: CStudentPublicationAssignment::class, cascade: ['persist'])]
152
    #[Assert\Valid]
153
    protected ?CStudentPublicationAssignment $assignment = null;
154
155
    #[ORM\Column(name: 'qualificator_id', type: 'integer', nullable: false)]
156
    protected int $qualificatorId;
157
158
    #[Assert\NotBlank]
159
    #[ORM\Column(name: 'weight', type: 'float', precision: 6, scale: 2, nullable: false)]
160
    #[Groups(['c_student_publication:write', 'student_publication:read'])]
161
    protected float $weight = 0;
162
163
    #[ORM\Column(name: 'allow_text_assignment', type: 'integer', nullable: false)]
164
    #[Groups(['c_student_publication:write', 'student_publication:item:get'])]
165
    protected int $allowTextAssignment;
166
167
    #[ORM\Column(name: 'contains_file', type: 'integer', nullable: false)]
168
    protected int $containsFile;
169
170
    #[ORM\Column(name: 'document_id', type: 'integer', nullable: false)]
171
    protected int $documentId;
172
173
    #[ORM\Column(name: 'filesize', type: 'integer', nullable: true)]
174
    protected ?int $fileSize = null;
175
176
    #[ORM\Column(name: 'duration', type: 'integer', nullable: true)]
177
    protected ?int $duration = null;
178
179
    #[ORM\ManyToOne(targetEntity: CGroupCategory::class)]
180
    #[ORM\JoinColumn(name: 'group_category_id', referencedColumnName: 'iid', nullable: true)]
181
    protected ?CGroupCategory $groupCategory = null;
182
183
    #[ORM\Column(name: 'student_delete_own_publication', type: 'boolean', nullable: true, options: ['default' => 0])]
184
    protected ?bool $studentDeleteOwnPublication = null;
185
186
    #[ORM\Column(name: 'default_visibility', type: 'boolean', nullable: true, options: ['default' => 0])]
187
    protected ?bool $defaultVisibility = null;
188
189
    #[ORM\Column(name: 'extensions', type: 'text', nullable: true)]
190
    protected ?string $extensions = null;
191
192
    #[ORM\Column(name: 'group_category_work_id', type: 'integer', nullable: false, options: ['default' => 0])]
193
    #[Groups(['c_student_publication:write', 'student_publication:read'])]
194
    protected int $groupCategoryWorkId = 0;
195
196
    public function __construct()
197
    {
198
        $this->description = '';
199
        $this->documentId = 0;
200
        $this->active = 1;
201
        $this->hasProperties = 0;
202
        $this->containsFile = 0;
203
        $this->publicationParent = null;
204
        $this->qualificatorId = 0;
205
        $this->qualification = 0;
206
        $this->assignment = null;
207
        $this->postGroupId = 0;
208
        $this->allowTextAssignment = 0;
209
        $this->filetype = 'folder';
210
        $this->sentDate = new DateTime();
211
        $this->children = new ArrayCollection();
212
        $this->comments = new ArrayCollection();
213
    }
214
215
    public function __toString(): string
216
    {
217
        return $this->getTitle();
218
    }
219
220
    public function getTitle(): string
221
    {
222
        return $this->title;
223
    }
224
225
    public function setTitle(string $title): self
226
    {
227
        $this->title = $title;
228
229
        return $this;
230
    }
231
232
    public function getFileTypes(): array
233
    {
234
        return ['file', 'folder'];
235
    }
236
237
    public function getDescription(): ?string
238
    {
239
        return $this->description;
240
    }
241
242
    public function setDescription(string $description): self
243
    {
244
        $this->description = $description;
245
246
        return $this;
247
    }
248
249
    public function getAuthor(): ?string
250
    {
251
        return $this->author;
252
    }
253
254
    public function setAuthor(string $author): self
255
    {
256
        $this->author = $author;
257
258
        return $this;
259
    }
260
261
    public function getActive(): ?int
262
    {
263
        return $this->active;
264
    }
265
266
    public function setActive(int $active): self
267
    {
268
        $this->active = $active;
269
270
        return $this;
271
    }
272
273
    public function getAccepted(): ?bool
274
    {
275
        return $this->accepted;
276
    }
277
278
    public function setAccepted(bool $accepted): self
279
    {
280
        $this->accepted = $accepted;
281
282
        return $this;
283
    }
284
285
    public function getPostGroupId(): int
286
    {
287
        return $this->postGroupId;
288
    }
289
290
    public function setPostGroupId(int $postGroupId): static
291
    {
292
        $this->postGroupId = $postGroupId;
293
294
        return $this;
295
    }
296
297
    public function getSentDate(): ?DateTime
298
    {
299
        return $this->sentDate;
300
    }
301
302
    public function setSentDate(DateTime $sentDate): self
303
    {
304
        $this->sentDate = $sentDate;
305
306
        return $this;
307
    }
308
309
    public function getFiletype(): string
310
    {
311
        return $this->filetype;
312
    }
313
314
    public function setFiletype(string $filetype): self
315
    {
316
        $this->filetype = $filetype;
317
318
        return $this;
319
    }
320
321
    public function getHasProperties(): int
322
    {
323
        return $this->hasProperties;
324
    }
325
326
    public function setHasProperties(int $hasProperties): self
327
    {
328
        $this->hasProperties = $hasProperties;
329
330
        return $this;
331
    }
332
333
    public function getViewProperties(): ?bool
334
    {
335
        return $this->viewProperties;
336
    }
337
338
    public function setViewProperties(bool $viewProperties): self
339
    {
340
        $this->viewProperties = $viewProperties;
341
342
        return $this;
343
    }
344
345
    public function getQualification(): float
346
    {
347
        return $this->qualification;
348
    }
349
350
    public function setQualification(float $qualification): self
351
    {
352
        $this->qualification = $qualification;
353
354
        return $this;
355
    }
356
357
    public function getDateOfQualification(): ?DateTime
358
    {
359
        return $this->dateOfQualification;
360
    }
361
362
    public function setDateOfQualification(DateTime $dateOfQualification): self
363
    {
364
        $this->dateOfQualification = $dateOfQualification;
365
366
        return $this;
367
    }
368
369
    public function getQualificatorId(): int
370
    {
371
        return $this->qualificatorId;
372
    }
373
374
    public function setQualificatorId(int $qualificatorId): static
375
    {
376
        $this->qualificatorId = $qualificatorId;
377
378
        return $this;
379
    }
380
381
    public function getWeight(): float
382
    {
383
        return $this->weight;
384
    }
385
386
    public function setWeight(float $weight): self
387
    {
388
        $this->weight = $weight;
389
390
        return $this;
391
    }
392
393
    public function getAllowTextAssignment(): int
394
    {
395
        return $this->allowTextAssignment;
396
    }
397
398
    public function setAllowTextAssignment(int $allowTextAssignment): self
399
    {
400
        $this->allowTextAssignment = $allowTextAssignment;
401
402
        return $this;
403
    }
404
405
    public function getContainsFile(): int
406
    {
407
        return $this->containsFile;
408
    }
409
410
    public function setContainsFile(int $containsFile): self
411
    {
412
        $this->containsFile = $containsFile;
413
414
        return $this;
415
    }
416
417
    public function getDocumentId(): int
418
    {
419
        return $this->documentId;
420
    }
421
422
    public function setDocumentId(int $documentId): self
423
    {
424
        $this->documentId = $documentId;
425
426
        return $this;
427
    }
428
429
    public function getFileSize(): int
430
    {
431
        return $this->fileSize;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fileSize could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
432
    }
433
434
    public function setFileSize(int $fileSize): self
435
    {
436
        $this->fileSize = $fileSize;
437
438
        return $this;
439
    }
440
441
    public function getCorrection(): ?ResourceNode
442
    {
443
        if ($this->hasResourceNode()) {
444
            $children = $this->getResourceNode()->getChildren();
445
            foreach ($children as $child) {
446
                $name = $child->getResourceType()->getTitle();
447
                if ('student_publications_corrections' === $name) {
448
                    return $child;
449
                }
450
            }
451
        }
452
453
        return null;
454
    }
455
456
    /**
457
     * @return Collection<int, CStudentPublication>
458
     */
459
    public function getChildren(): Collection
460
    {
461
        return $this->children;
462
    }
463
464
    public function setChildren(Collection $children): self
465
    {
466
        $this->children = $children;
467
468
        return $this;
469
    }
470
471
    public function getAssignment(): ?CStudentPublicationAssignment
472
    {
473
        return $this->assignment;
474
    }
475
476
    public function setAssignment(?CStudentPublicationAssignment $assignment): self
477
    {
478
        $this->assignment = $assignment;
479
480
        if ($assignment) {
481
            $assignment->setPublication($this);
482
        }
483
484
        return $this;
485
    }
486
487
    public function getPublicationParent(): ?self
488
    {
489
        return $this->publicationParent;
490
    }
491
492
    public function setPublicationParent(?self $publicationParent): self
493
    {
494
        $this->publicationParent = $publicationParent;
495
496
        return $this;
497
    }
498
499
    public function getUser(): User
500
    {
501
        return $this->user;
502
    }
503
504
    public function setUser(User $user): self
505
    {
506
        $this->user = $user;
507
508
        return $this;
509
    }
510
511
    /**
512
     * @return Collection<int, CStudentPublicationComment>
513
     */
514
    public function getComments(): Collection
515
    {
516
        return $this->comments;
517
    }
518
519
    public function setComments(Collection $comments): self
520
    {
521
        $this->comments = $comments;
522
523
        return $this;
524
    }
525
526
    public function getDuration(): ?int
527
    {
528
        return $this->duration;
529
    }
530
531
    public function setDuration(?int $duration): self
532
    {
533
        $this->duration = $duration;
534
535
        return $this;
536
    }
537
538
    public function getGroupCategory(): ?CGroupCategory
539
    {
540
        return $this->groupCategory;
541
    }
542
543
    public function setGroupCategory(?CGroupCategory $groupCategory): self
544
    {
545
        $this->groupCategory = $groupCategory;
546
547
        return $this;
548
    }
549
550
    public function getStudentDeleteOwnPublication(): ?bool
551
    {
552
        return $this->studentDeleteOwnPublication;
553
    }
554
555
    public function setStudentDeleteOwnPublication(?bool $studentDeleteOwnPublication): self
556
    {
557
        $this->studentDeleteOwnPublication = $studentDeleteOwnPublication;
558
559
        return $this;
560
    }
561
562
    public function getDefaultVisibility(): ?bool
563
    {
564
        return $this->defaultVisibility;
565
    }
566
567
    public function setDefaultVisibility(?bool $defaultVisibility): self
568
    {
569
        $this->defaultVisibility = $defaultVisibility;
570
571
        return $this;
572
    }
573
574
    public function getExtensions(): ?string
575
    {
576
        return $this->extensions;
577
    }
578
579
    public function setExtensions(?string $extensions): self
580
    {
581
        $this->extensions = $extensions;
582
583
        return $this;
584
    }
585
586
    public function getResourceIdentifier(): int
587
    {
588
        return $this->getIid();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIid() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
589
    }
590
591
    public function getIid(): ?int
592
    {
593
        return $this->iid;
594
    }
595
596
    public function getResourceName(): string
597
    {
598
        return $this->getTitle();
599
    }
600
601
    public function setResourceName(string $name): self
602
    {
603
        return $this->setTitle($name);
604
    }
605
606
    #[Groups(['student_publication:read'])]
607
    public function getUniqueStudentAttemptsTotal(): int
608
    {
609
        $userIdList = [];
610
611
        $reduce = $this->children
612
            ->filter(function (self $child) {
613
                return $child->postGroupId === $this->postGroupId;
614
            })
615
            ->reduce(function (int $accumulator, self $child) use (&$userIdList): int {
616
                $user = $child->getUser();
617
618
                if (!\in_array($user->getId(), $userIdList, true)) {
619
                    $userIdList[] = $user->getId();
620
621
                    return $accumulator + 1;
622
                }
623
624
                return $accumulator;
625
            })
626
        ;
627
628
        return $reduce ?: 0;
629
    }
630
631
    #[Groups(['student_publication:read'])]
632
    public function getStudentSubscribedToWork(): int
633
    {
634
        $firstLink = $this->getFirstResourceLink();
635
636
        $course = $firstLink->getCourse();
637
        $session = $firstLink->getSession();
638
        $group = $firstLink->getGroup();
639
640
        if ($group) {
641
            return $group->getMembers()->count();
642
        }
643
644
        if ($session) {
645
            return $session->getSessionRelCourseRelUsersByStatus($course, Session::STUDENT)->count();
646
        }
647
648
        if ($course) {
649
            return $course->getStudentSubscriptions()->count();
650
        }
651
652
        return 0;
653
    }
654
655
    public function getGroupCategoryWorkId(): int
656
    {
657
        return $this->groupCategoryWorkId;
658
    }
659
660
    public function setGroupCategoryWorkId(int $groupCategoryWorkId): self
661
    {
662
        $this->groupCategoryWorkId = $groupCategoryWorkId;
663
664
        return $this;
665
    }
666
}
667