Passed
Push — master ( 445983...d786ab )
by Yannick
39:28
created

CStudentPublication::getDefaultVisibility()   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
    public function __construct()
193
    {
194
        $this->description = '';
195
        $this->documentId = 0;
196
        $this->active = 1;
197
        $this->hasProperties = 0;
198
        $this->containsFile = 0;
199
        $this->publicationParent = null;
200
        $this->qualificatorId = 0;
201
        $this->qualification = 0;
202
        $this->assignment = null;
203
        $this->postGroupId = 0;
204
        $this->allowTextAssignment = 0;
205
        $this->filetype = 'folder';
206
        $this->sentDate = new DateTime();
207
        $this->children = new ArrayCollection();
208
        $this->comments = new ArrayCollection();
209
    }
210
211
    public function __toString(): string
212
    {
213
        return $this->getTitle();
214
    }
215
216
    public function getTitle(): string
217
    {
218
        return $this->title;
219
    }
220
221
    public function setTitle(string $title): self
222
    {
223
        $this->title = $title;
224
225
        return $this;
226
    }
227
228
    public function getFileTypes(): array
229
    {
230
        return ['file', 'folder'];
231
    }
232
233
    public function getDescription(): ?string
234
    {
235
        return $this->description;
236
    }
237
238
    public function setDescription(string $description): self
239
    {
240
        $this->description = $description;
241
242
        return $this;
243
    }
244
245
    public function getAuthor(): ?string
246
    {
247
        return $this->author;
248
    }
249
250
    public function setAuthor(string $author): self
251
    {
252
        $this->author = $author;
253
254
        return $this;
255
    }
256
257
    public function getActive(): ?int
258
    {
259
        return $this->active;
260
    }
261
262
    public function setActive(int $active): self
263
    {
264
        $this->active = $active;
265
266
        return $this;
267
    }
268
269
    public function getAccepted(): ?bool
270
    {
271
        return $this->accepted;
272
    }
273
274
    public function setAccepted(bool $accepted): self
275
    {
276
        $this->accepted = $accepted;
277
278
        return $this;
279
    }
280
281
    public function getPostGroupId(): int
282
    {
283
        return $this->postGroupId;
284
    }
285
286
    public function setPostGroupId(int $postGroupId): static
287
    {
288
        $this->postGroupId = $postGroupId;
289
290
        return $this;
291
    }
292
293
    public function getSentDate(): ?DateTime
294
    {
295
        return $this->sentDate;
296
    }
297
298
    public function setSentDate(DateTime $sentDate): self
299
    {
300
        $this->sentDate = $sentDate;
301
302
        return $this;
303
    }
304
305
    public function getFiletype(): string
306
    {
307
        return $this->filetype;
308
    }
309
310
    public function setFiletype(string $filetype): self
311
    {
312
        $this->filetype = $filetype;
313
314
        return $this;
315
    }
316
317
    public function getHasProperties(): int
318
    {
319
        return $this->hasProperties;
320
    }
321
322
    public function setHasProperties(int $hasProperties): self
323
    {
324
        $this->hasProperties = $hasProperties;
325
326
        return $this;
327
    }
328
329
    public function getViewProperties(): ?bool
330
    {
331
        return $this->viewProperties;
332
    }
333
334
    public function setViewProperties(bool $viewProperties): self
335
    {
336
        $this->viewProperties = $viewProperties;
337
338
        return $this;
339
    }
340
341
    public function getQualification(): float
342
    {
343
        return $this->qualification;
344
    }
345
346
    public function setQualification(float $qualification): self
347
    {
348
        $this->qualification = $qualification;
349
350
        return $this;
351
    }
352
353
    public function getDateOfQualification(): ?DateTime
354
    {
355
        return $this->dateOfQualification;
356
    }
357
358
    public function setDateOfQualification(DateTime $dateOfQualification): self
359
    {
360
        $this->dateOfQualification = $dateOfQualification;
361
362
        return $this;
363
    }
364
365
    public function getQualificatorId(): int
366
    {
367
        return $this->qualificatorId;
368
    }
369
370
    public function setQualificatorId(int $qualificatorId): static
371
    {
372
        $this->qualificatorId = $qualificatorId;
373
374
        return $this;
375
    }
376
377
    public function getWeight(): float
378
    {
379
        return $this->weight;
380
    }
381
382
    public function setWeight(float $weight): self
383
    {
384
        $this->weight = $weight;
385
386
        return $this;
387
    }
388
389
    public function getAllowTextAssignment(): int
390
    {
391
        return $this->allowTextAssignment;
392
    }
393
394
    public function setAllowTextAssignment(int $allowTextAssignment): self
395
    {
396
        $this->allowTextAssignment = $allowTextAssignment;
397
398
        return $this;
399
    }
400
401
    public function getContainsFile(): int
402
    {
403
        return $this->containsFile;
404
    }
405
406
    public function setContainsFile(int $containsFile): self
407
    {
408
        $this->containsFile = $containsFile;
409
410
        return $this;
411
    }
412
413
    public function getDocumentId(): int
414
    {
415
        return $this->documentId;
416
    }
417
418
    public function setDocumentId(int $documentId): self
419
    {
420
        $this->documentId = $documentId;
421
422
        return $this;
423
    }
424
425
    public function getFileSize(): int
426
    {
427
        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...
428
    }
429
430
    public function setFileSize(int $fileSize): self
431
    {
432
        $this->fileSize = $fileSize;
433
434
        return $this;
435
    }
436
437
    public function getCorrection(): ?ResourceNode
438
    {
439
        if ($this->hasResourceNode()) {
440
            $children = $this->getResourceNode()->getChildren();
441
            foreach ($children as $child) {
442
                $name = $child->getResourceType()->getTitle();
443
                if ('student_publications_corrections' === $name) {
444
                    return $child;
445
                }
446
            }
447
        }
448
449
        return null;
450
    }
451
452
    /**
453
     * @return Collection<int, CStudentPublication>
454
     */
455
    public function getChildren(): Collection
456
    {
457
        return $this->children;
458
    }
459
460
    public function setChildren(Collection $children): self
461
    {
462
        $this->children = $children;
463
464
        return $this;
465
    }
466
467
    public function getAssignment(): ?CStudentPublicationAssignment
468
    {
469
        return $this->assignment;
470
    }
471
472
    public function setAssignment(?CStudentPublicationAssignment $assignment): self
473
    {
474
        $this->assignment = $assignment;
475
476
        if ($assignment) {
477
            $assignment->setPublication($this);
478
        }
479
480
        return $this;
481
    }
482
483
    public function getPublicationParent(): ?self
484
    {
485
        return $this->publicationParent;
486
    }
487
488
    public function setPublicationParent(?self $publicationParent): self
489
    {
490
        $this->publicationParent = $publicationParent;
491
492
        return $this;
493
    }
494
495
    public function getUser(): User
496
    {
497
        return $this->user;
498
    }
499
500
    public function setUser(User $user): self
501
    {
502
        $this->user = $user;
503
504
        return $this;
505
    }
506
507
    /**
508
     * @return Collection<int, CStudentPublicationComment>
509
     */
510
    public function getComments(): Collection
511
    {
512
        return $this->comments;
513
    }
514
515
    public function setComments(Collection $comments): self
516
    {
517
        $this->comments = $comments;
518
519
        return $this;
520
    }
521
522
    public function getDuration(): ?int
523
    {
524
        return $this->duration;
525
    }
526
527
    public function setDuration(?int $duration): self
528
    {
529
        $this->duration = $duration;
530
531
        return $this;
532
    }
533
534
    public function getGroupCategory(): ?CGroupCategory
535
    {
536
        return $this->groupCategory;
537
    }
538
539
    public function setGroupCategory(?CGroupCategory $groupCategory): self
540
    {
541
        $this->groupCategory = $groupCategory;
542
543
        return $this;
544
    }
545
546
    public function getStudentDeleteOwnPublication(): ?bool
547
    {
548
        return $this->studentDeleteOwnPublication;
549
    }
550
551
    public function setStudentDeleteOwnPublication(?bool $studentDeleteOwnPublication): self
552
    {
553
        $this->studentDeleteOwnPublication = $studentDeleteOwnPublication;
554
555
        return $this;
556
    }
557
558
    public function getDefaultVisibility(): ?bool
559
    {
560
        return $this->defaultVisibility;
561
    }
562
563
    public function setDefaultVisibility(?bool $defaultVisibility): self
564
    {
565
        $this->defaultVisibility = $defaultVisibility;
566
567
        return $this;
568
    }
569
570
    public function getExtensions(): ?string
571
    {
572
        return $this->extensions;
573
    }
574
575
    public function setExtensions(?string $extensions): self
576
    {
577
        $this->extensions = $extensions;
578
579
        return $this;
580
    }
581
582
    public function getResourceIdentifier(): int
583
    {
584
        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...
585
    }
586
587
    public function getIid(): ?int
588
    {
589
        return $this->iid;
590
    }
591
592
    public function getResourceName(): string
593
    {
594
        return $this->getTitle();
595
    }
596
597
    public function setResourceName(string $name): self
598
    {
599
        return $this->setTitle($name);
600
    }
601
602
    #[Groups(['student_publication:read'])]
603
    public function getUniqueStudentAttemptsTotal(): int
604
    {
605
        $userIdList = [];
606
607
        $reduce = $this->children
608
            ->filter(function (self $child) {
609
                return $child->postGroupId === $this->postGroupId;
610
            })
611
            ->reduce(function (int $accumulator, self $child) use (&$userIdList): int {
612
                $user = $child->getUser();
613
614
                if (!\in_array($user->getId(), $userIdList, true)) {
615
                    $userIdList[] = $user->getId();
616
617
                    return $accumulator + 1;
618
                }
619
620
                return $accumulator;
621
            })
622
        ;
623
624
        return $reduce ?: 0;
625
    }
626
627
    #[Groups(['student_publication:read'])]
628
    public function getStudentSubscribedToWork(): int
629
    {
630
        $firstLink = $this->getFirstResourceLink();
631
632
        $course = $firstLink->getCourse();
633
        $session = $firstLink->getSession();
634
        $group = $firstLink->getGroup();
635
636
        if ($group) {
637
            return $group->getMembers()->count();
638
        }
639
640
        if ($session) {
641
            return $session->getSessionRelCourseRelUsersByStatus($course, Session::STUDENT)->count();
642
        }
643
644
        if ($course) {
645
            return $course->getStudentSubscriptions()->count();
646
        }
647
648
        return 0;
649
    }
650
}
651