Passed
Push — master ( d27573...c495f0 )
by Yannick
08:35
created

SkillRelUser::setAssignedBy()   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\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
10
use ApiPlatform\Metadata\ApiFilter;
11
use ApiPlatform\Metadata\ApiResource;
12
use ApiPlatform\Metadata\Delete;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\Post;
15
use ApiPlatform\Metadata\Put;
16
use Chamilo\CoreBundle\Entity\Listener\SkillRelUserListener;
17
use Chamilo\CoreBundle\Traits\UserTrait;
18
use DateTime;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use Doctrine\Common\Collections\Criteria;
22
use Doctrine\ORM\Mapping as ORM;
23
use Symfony\Component\Serializer\Annotation\Groups;
24
use Symfony\Component\Validator\Constraints as Assert;
25
26
#[ApiResource(
27
    operations: [
28
        new Get(security: "is_granted('VIEW', object)"),
29
        new Put(security: "is_granted('EDIT', object)"),
30
        new Delete(security: "is_granted('DELETE', object)"),
31
        new Post(securityPostDenormalize: "is_granted('CREATE', object)"),
32
    ],
33
    normalizationContext: ['groups' => ['skill_rel_user:read']],
34
    denormalizationContext: ['groups' => ['skill_rel_user:write']],
35
    security: "is_granted('ROLE_USER')"
36
)]
37
#[ApiFilter(SearchFilter::class, properties: ['user' => 'exact'])]
38
#[ORM\Table(name: 'skill_rel_user')]
39
#[ORM\Index(columns: ['course_id', 'session_id'], name: 'idx_select_cs')]
40
#[ORM\Index(columns: ['session_id', 'course_id', 'user_id'], name: 'idx_select_s_c_u')]
41
#[ORM\Index(columns: ['skill_id', 'user_id'], name: 'idx_select_sk_u')]
42
#[ORM\Entity]
43
#[ORM\EntityListeners([SkillRelUserListener::class])]
44
class SkillRelUser
45
{
46
    use UserTrait;
47
48
    #[ORM\Column(name: 'id', type: 'integer')]
49
    #[ORM\Id]
50
    #[ORM\GeneratedValue]
51
    protected ?int $id = null;
52
53
    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'achievedSkills')]
54
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
55
    protected User $user;
56
57
    #[Groups(['skill_rel_user:read'])]
58
    #[ORM\ManyToOne(targetEntity: Skill::class, inversedBy: 'issuedSkills')]
59
    #[ORM\JoinColumn(name: 'skill_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
60
    protected ?Skill $skill = null;
61
62
    #[ORM\ManyToOne(targetEntity: Course::class, inversedBy: 'issuedSkills')]
63
    #[ORM\JoinColumn(name: 'course_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
64
    protected ?Course $course = null;
65
66
    #[ORM\ManyToOne(targetEntity: Session::class, inversedBy: 'issuedSkills')]
67
    #[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
68
    protected ?Session $session = null;
69
70
    /**
71
     * @var Collection<int, SkillRelUserComment>
72
     */
73
    #[ORM\OneToMany(
74
        mappedBy: 'skillRelUser',
75
        targetEntity: SkillRelUserComment::class,
76
        cascade: ['persist', 'remove'],
77
        orphanRemoval: true,
78
    )]
79
    protected Collection $comments;
80
81
    #[ORM\ManyToOne(targetEntity: Level::class)]
82
    #[ORM\JoinColumn(name: 'acquired_level', referencedColumnName: 'id')]
83
    protected ?Level $acquiredLevel = null;
84
85
    #[ORM\Column(name: 'acquired_skill_at', type: 'datetime', nullable: false)]
86
    protected DateTime $acquiredSkillAt;
87
88
    /**
89
     * Whether this has been confirmed by a teacher or not
90
     * Only set to 0 when the skill_rel_item says requires_validation = 1.
91
     */
92
    #[ORM\Column(name: 'validation_status', type: 'integer')]
93
    protected int $validationStatus;
94
95
    #[Assert\NotBlank]
96
    #[ORM\Column(name: 'argumentation', type: 'text')]
97
    protected string $argumentation;
98
99
    #[ORM\Column(name: 'argumentation_author_id', type: 'integer')]
100
    protected int $argumentationAuthorId;
101
102
    public function __construct()
103
    {
104
        $this->validationStatus = 0;
105
        $this->comments = new ArrayCollection();
106
        $this->acquiredLevel = null;
107
        $this->acquiredSkillAt = new DateTime();
108
    }
109
110
    public function setSkill(?Skill $skill): self
111
    {
112
        $this->skill = $skill;
113
114
        return $this;
115
    }
116
117
    public function getSkill(): ?Skill
118
    {
119
        return $this->skill;
120
    }
121
122
    public function setCourse(Course $course): self
123
    {
124
        $this->course = $course;
125
126
        return $this;
127
    }
128
129
    public function getCourse(): ?Course
130
    {
131
        return $this->course;
132
    }
133
134
    public function setSession(Session $session): self
135
    {
136
        $this->session = $session;
137
138
        return $this;
139
    }
140
141
    public function getSession(): ?Session
142
    {
143
        return $this->session;
144
    }
145
146
    public function setAcquiredSkillAt(DateTime $acquiredSkillAt): self
147
    {
148
        $this->acquiredSkillAt = $acquiredSkillAt;
149
150
        return $this;
151
    }
152
153
    public function getAcquiredSkillAt(): DateTime
154
    {
155
        return $this->acquiredSkillAt;
156
    }
157
158
    public function getId(): ?int
159
    {
160
        return $this->id;
161
    }
162
163
    public function setAcquiredLevel(Level $acquiredLevel): self
164
    {
165
        $this->acquiredLevel = $acquiredLevel;
166
167
        return $this;
168
    }
169
170
    public function getAcquiredLevel(): ?Level
171
    {
172
        return $this->acquiredLevel;
173
    }
174
175
    public function setArgumentationAuthorId(int $argumentationAuthorId): self
176
    {
177
        $this->argumentationAuthorId = $argumentationAuthorId;
178
179
        return $this;
180
    }
181
182
    public function getArgumentationAuthorId(): int
183
    {
184
        return $this->argumentationAuthorId;
185
    }
186
187
    public function setArgumentation(string $argumentation): self
188
    {
189
        $this->argumentation = $argumentation;
190
191
        return $this;
192
    }
193
194
    public function getArgumentation(): string
195
    {
196
        return $this->argumentation;
197
    }
198
199
    /**
200
     * Get the source which the skill was obtained.
201
     */
202
    public function getSourceName(): string
203
    {
204
        $source = '';
205
        if (null !== $this->session) {
206
            $source .= \sprintf('[%s] ', $this->session->getTitle());
207
        }
208
209
        if (null !== $this->course) {
210
            $source .= $this->course->getTitle();
211
        }
212
213
        return $source;
214
    }
215
216
    /**
217
     * Get the URL for the issue.
218
     */
219
    public function getIssueUrl(): string
220
    {
221
        return api_get_path(WEB_PATH).\sprintf('badge/%s', $this->id);
222
    }
223
224
    /**
225
     * Get the URL for the issues page.
226
     */
227
    public function getIssueUrlAll(): string
228
    {
229
        return api_get_path(WEB_PATH).\sprintf('skill/%s/user/%s', $this->skill->getId(), $this->user->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

229
        return api_get_path(WEB_PATH).\sprintf('skill/%s/user/%s', $this->skill->/** @scrutinizer ignore-call */ getId(), $this->user->getId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
    }
231
232
    /**
233
     * Get the URL for the assertion.
234
     */
235
    public function getAssertionUrl(): string
236
    {
237
        $url = api_get_path(WEB_CODE_PATH).'skills/assertion.php?';
238
239
        return $url.http_build_query([
240
            'user' => $this->user->getId(),
241
            'skill' => $this->skill->getId(),
242
            'course' => null !== $this->course ? $this->course->getId() : 0,
243
            'session' => null !== $this->session ? $this->session->getId() : 0,
244
        ]);
245
    }
246
247
    public function getComments(bool $sortDescByDateTime = false): Collection
248
    {
249
        if ($sortDescByDateTime) {
250
            $criteria = Criteria::create();
251
            $criteria->orderBy([
252
                'feedbackDateTime' => Criteria::DESC,
253
            ]);
254
255
            return $this->comments->matching($criteria);
256
        }
257
258
        return $this->comments;
259
    }
260
261
    /**
262
     * Calculate the average value from the feedback comments.
263
     */
264
    public function getAverage(): string
265
    {
266
        $sum = 0;
267
        $countValues = 0;
268
        foreach ($this->comments as $comment) {
269
            if (0 === $comment->getFeedbackValue()) {
270
                continue;
271
            }
272
273
            $sum += $comment->getFeedbackValue();
274
            $countValues++;
275
        }
276
277
        $average = $countValues > 0 ? $sum / $countValues : 0;
278
279
        return number_format($average, 2);
280
    }
281
282
    public function getValidationStatus(): int
283
    {
284
        return $this->validationStatus;
285
    }
286
287
    public function setValidationStatus(int $validationStatus): static
288
    {
289
        $this->validationStatus = $validationStatus;
290
291
        return $this;
292
    }
293
}
294