Passed
Pull Request — master (#5636)
by
unknown
07:08
created

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