Passed
Push — master ( a86a3f...6d8511 )
by Julito
09:12
created

CGroup::hasTutors()   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
namespace Chamilo\CourseBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiResource;
8
use Chamilo\CoreBundle\Entity\AbstractResource;
9
use Chamilo\CoreBundle\Entity\ResourceInterface;
10
use Chamilo\CoreBundle\Entity\User;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Criteria;
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\Serializer\Annotation\Groups;
15
use Symfony\Component\Validator\Constraints as Assert;
16
17
/**
18
 * @ApiResource(
19
 *     attributes={"security"="is_granted('ROLE_ADMIN')"},
20
 *     normalizationContext={"groups"={"group:read"}},
21
 * )
22
 *
23
 * @ORM\Table(
24
 *  name="c_group_info",
25
 *  indexes={
26
 *  }
27
 * )
28
 *
29
 * @ORM\Entity
30
 */
31
class CGroup extends AbstractResource implements ResourceInterface
32
{
33
    /**
34
     * @var int
35
     *
36
     * @ORM\Column(name="iid", type="integer")
37
     * @ORM\Id
38
     * @ORM\GeneratedValue
39
     * @Groups({"group:read", "group:write"})
40
     */
41
    protected $iid;
42
43
    /**
44
     * @Assert\NotBlank()
45
     * @ORM\Column(name="name", type="string", length=100, nullable=true)
46
     * @Groups({"group:read", "group:write"})
47
     */
48
    protected string $name;
49
50
    /**
51
     * @var bool
52
     * @Assert\NotBlank()
53
     * @ORM\Column(name="status", type="boolean", nullable=true)
54
     */
55
    protected $status;
56
57
    /**
58
     * @var CGroupCategory
59
     *
60
     * @ORM\ManyToOne(targetEntity="CGroupCategory", cascade={"persist"})
61
     * @ORM\JoinColumn(name="category_id", referencedColumnName="iid", onDelete="CASCADE")
62
     */
63
    protected $category;
64
65
    /**
66
     * @ORM\Column(name="description", type="text", nullable=true)
67
     */
68
    protected ?string $description;
69
70
    /**
71
     * @var int
72
     *
73
     * @ORM\Column(name="max_student", type="integer", nullable=false)
74
     */
75
    protected $maxStudent;
76
77
    /**
78
     * @var int
79
     *
80
     * @ORM\Column(name="doc_state", type="integer", nullable=false)
81
     */
82
    protected $docState;
83
84
    /**
85
     * @var int
86
     *
87
     * @ORM\Column(name="calendar_state", type="integer", nullable=false)
88
     */
89
    protected $calendarState;
90
91
    /**
92
     * @var int
93
     *
94
     * @ORM\Column(name="work_state", type="integer", nullable=false)
95
     */
96
    protected $workState;
97
98
    /**
99
     * @var int
100
     *
101
     * @ORM\Column(name="announcements_state", type="integer", nullable=false)
102
     */
103
    protected $announcementsState;
104
105
    /**
106
     * @var int
107
     *
108
     * @ORM\Column(name="forum_state", type="integer", nullable=false)
109
     */
110
    protected $forumState;
111
112
    /**
113
     * @var int
114
     *
115
     * @ORM\Column(name="wiki_state", type="integer", nullable=false)
116
     */
117
    protected $wikiState;
118
119
    /**
120
     * @var int
121
     *
122
     * @ORM\Column(name="chat_state", type="integer", nullable=false)
123
     */
124
    protected $chatState;
125
126
    /**
127
     * @var string
128
     *
129
     * @ORM\Column(name="secret_directory", type="string", length=255, nullable=true)
130
     */
131
    protected $secretDirectory;
132
133
    /**
134
     * @var bool
135
     *
136
     * @ORM\Column(name="self_registration_allowed", type="boolean", nullable=false)
137
     */
138
    protected $selfRegistrationAllowed;
139
140
    /**
141
     * @var bool
142
     *
143
     * @ORM\Column(name="self_unregistration_allowed", type="boolean", nullable=false)
144
     */
145
    protected $selfUnregistrationAllowed;
146
147
    /**
148
     * @var int
149
     *
150
     * @ORM\Column(name="document_access", type="integer", nullable=false, options={"default":0})
151
     */
152
    protected $documentAccess;
153
154
    /**
155
     * @var ArrayCollection|CGroupRelUser[]
156
     *
157
     * @ORM\OneToMany(targetEntity="CGroupRelUser", mappedBy="group")
158
     */
159
    protected $members;
160
161
    /**
162
     * @var ArrayCollection|CGroupRelTutor[]
163
     *
164
     * @ORM\OneToMany(targetEntity="CGroupRelTutor", mappedBy="group")
165
     */
166
    protected $tutors;
167
168
    public function __construct()
169
    {
170
        $this->status = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
171
        $this->members = new ArrayCollection();
172
        $this->tutors = new ArrayCollection();
173
    }
174
175
    public function __toString(): string
176
    {
177
        return $this->getName();
178
    }
179
180
    /**
181
     * Get iid.
182
     *
183
     * @return int
184
     */
185
    public function getIid()
186
    {
187
        return $this->iid;
188
    }
189
190
    /**
191
     * Set name.
192
     *
193
     * @param string $name
194
     */
195
    public function setName($name): self
196
    {
197
        $this->name = $name;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Get name.
204
     */
205
    public function getName(): string
206
    {
207
        return $this->name;
208
    }
209
210
    /**
211
     * Set status.
212
     *
213
     * @param bool $status
214
     */
215
    public function setStatus($status): self
216
    {
217
        $this->status = $status;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Get status.
224
     *
225
     * @return bool
226
     */
227
    public function getStatus()
228
    {
229
        return $this->status;
230
    }
231
232
    /**
233
     * Set description.
234
     */
235
    public function setDescription(string $description): self
236
    {
237
        $this->description = $description;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Get description.
244
     */
245
    public function getDescription(): ?string
246
    {
247
        return $this->description;
248
    }
249
250
    public function setMaxStudent(int $maxStudent): self
251
    {
252
        $this->maxStudent = $maxStudent;
253
254
        return $this;
255
    }
256
257
    public function getMaxStudent(): int
258
    {
259
        return (int) $this->maxStudent;
260
    }
261
262
    /**
263
     * Set docState.
264
     */
265
    public function setDocState($docState): self
266
    {
267
        $this->docState = $docState;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Get docState.
274
     *
275
     * @return int
276
     */
277
    public function getDocState()
278
    {
279
        return $this->docState;
280
    }
281
282
    /**
283
     * Set calendarState.
284
     *
285
     * @param int $calendarState
286
     */
287
    public function setCalendarState($calendarState): self
288
    {
289
        $this->calendarState = $calendarState;
290
291
        return $this;
292
    }
293
294
    /**
295
     * Get calendarState.
296
     *
297
     * @return int
298
     */
299
    public function getCalendarState()
300
    {
301
        return $this->calendarState;
302
    }
303
304
    /**
305
     * Set workState.
306
     *
307
     * @param int $workState
308
     */
309
    public function setWorkState($workState): self
310
    {
311
        $this->workState = $workState;
312
313
        return $this;
314
    }
315
316
    /**
317
     * Get workState.
318
     *
319
     * @return int
320
     */
321
    public function getWorkState()
322
    {
323
        return $this->workState;
324
    }
325
326
    /**
327
     * Set announcementsState.
328
     *
329
     * @param int $announcementsState
330
     */
331
    public function setAnnouncementsState($announcementsState): self
332
    {
333
        $this->announcementsState = $announcementsState;
334
335
        return $this;
336
    }
337
338
    /**
339
     * Get announcementsState.
340
     *
341
     * @return int
342
     */
343
    public function getAnnouncementsState()
344
    {
345
        return $this->announcementsState;
346
    }
347
348
    /**
349
     * Set forumState.
350
     *
351
     * @param int $forumState
352
     */
353
    public function setForumState($forumState): self
354
    {
355
        $this->forumState = $forumState;
356
357
        return $this;
358
    }
359
360
    /**
361
     * Get forumState.
362
     */
363
    public function getForumState(): int
364
    {
365
        return (int) $this->forumState;
366
    }
367
368
    /**
369
     * Set wikiState.
370
     *
371
     * @param int $wikiState
372
     */
373
    public function setWikiState($wikiState): self
374
    {
375
        $this->wikiState = $wikiState;
376
377
        return $this;
378
    }
379
380
    /**
381
     * Get wikiState.
382
     *
383
     * @return int
384
     */
385
    public function getWikiState()
386
    {
387
        return $this->wikiState;
388
    }
389
390
    /**
391
     * Set chatState.
392
     *
393
     * @param int $chatState
394
     */
395
    public function setChatState($chatState): self
396
    {
397
        $this->chatState = $chatState;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Get chatState.
404
     *
405
     * @return int
406
     */
407
    public function getChatState()
408
    {
409
        return $this->chatState;
410
    }
411
412
    /**
413
     * Set secretDirectory.
414
     *
415
     * @param string $secretDirectory
416
     *
417
     * @return CGroup
418
     */
419
    public function setSecretDirectory($secretDirectory)
420
    {
421
        $this->secretDirectory = $secretDirectory;
422
423
        return $this;
424
    }
425
426
    public function getSecretDirectory(): string
427
    {
428
        return $this->secretDirectory;
429
    }
430
431
    /**
432
     * Set selfRegistrationAllowed.
433
     *
434
     * @param bool $selfRegistrationAllowed
435
     */
436
    public function setSelfRegistrationAllowed($selfRegistrationAllowed): self
437
    {
438
        $this->selfRegistrationAllowed = $selfRegistrationAllowed;
439
440
        return $this;
441
    }
442
443
    /**
444
     * Get selfRegistrationAllowed.
445
     *
446
     * @return bool
447
     */
448
    public function getSelfRegistrationAllowed()
449
    {
450
        return $this->selfRegistrationAllowed;
451
    }
452
453
    /**
454
     * Set selfUnregistrationAllowed.
455
     *
456
     * @param bool $selfUnregistrationAllowed
457
     */
458
    public function setSelfUnregistrationAllowed($selfUnregistrationAllowed): self
459
    {
460
        $this->selfUnregistrationAllowed = $selfUnregistrationAllowed;
461
462
        return $this;
463
    }
464
465
    /**
466
     * Get selfUnregistrationAllowed.
467
     *
468
     * @return bool
469
     */
470
    public function getSelfUnregistrationAllowed()
471
    {
472
        return $this->selfUnregistrationAllowed;
473
    }
474
475
    public function getDocumentAccess(): int
476
    {
477
        return $this->documentAccess;
478
    }
479
480
    public function setDocumentAccess(int $documentAccess): self
481
    {
482
        $this->documentAccess = $documentAccess;
483
484
        return $this;
485
    }
486
487
    public function getMembers()
488
    {
489
        return $this->members;
490
    }
491
492
    public function setMembers(ArrayCollection $members): self
493
    {
494
        $this->members = $members;
495
496
        return $this;
497
    }
498
499
    public function hasMembers(): bool
500
    {
501
        return $this->members->count() > 0;
502
    }
503
504
    public function hasMember(User $user): bool
505
    {
506
        $criteria = Criteria::create()->where(
507
            Criteria::expr()->eq('user', $user)
508
        );
509
510
        return $this->members->matching($criteria)->count() > 0;
511
    }
512
513
    public function hasTutor(User $user): bool
514
    {
515
        $criteria = Criteria::create()->where(
516
            Criteria::expr()->eq('user', $user)
517
        );
518
519
        return $this->tutors->matching($criteria)->count() > 0;
520
    }
521
522
    public function getTutors()
523
    {
524
        return $this->tutors;
525
    }
526
527
    public function setTutors(ArrayCollection $tutors): self
528
    {
529
        $this->tutors = $tutors;
530
531
        return $this;
532
    }
533
534
    public function hasTutors(): bool
535
    {
536
        return $this->tutors->count() > 0;
537
    }
538
539
    public function userIsTutor(User $user = null): bool
540
    {
541
        if (empty($user)) {
542
            return false;
543
        }
544
545
        if (0 === $this->tutors->count()) {
546
            return false;
547
        }
548
549
        $criteria = Criteria::create()
550
            ->where(
551
                Criteria::expr()->eq('cId', $this->course)
0 ignored issues
show
Bug Best Practice introduced by
The property course does not exist on Chamilo\CourseBundle\Entity\CGroup. Did you maybe forget to declare it?
Loading history...
552
            )
553
            ->andWhere(
554
                Criteria::expr()->eq('user', $user)
555
            );
556
557
        $relation = $this->tutors->matching($criteria);
558
559
        return $relation->count() > 0;
560
    }
561
562
    public function getCategory(): CGroupCategory
563
    {
564
        return $this->category;
565
    }
566
567
    public function setCategory(CGroupCategory $category = null): CGroup
568
    {
569
        $this->category = $category;
570
571
        return $this;
572
    }
573
574
    public function getResourceIdentifier(): int
575
    {
576
        return $this->iid;
577
    }
578
579
    public function getResourceName(): string
580
    {
581
        return $this->getName();
582
    }
583
584
    public function setResourceName(string $name): self
585
    {
586
        return $this->setName($name);
587
    }
588
}
589