Passed
Pull Request — master (#6257)
by
unknown
08:47
created

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