Completed
Push — master ( a88956...84abaf )
by Julito
13:00
created

AbstractResource::addResourceLink()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiProperty;
8
use ApiPlatform\Core\Annotation\ApiSubresource;
9
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
10
use Chamilo\CourseBundle\Entity\CGroup;
11
use Doctrine\Common\Collections\Criteria;
12
use Doctrine\ORM\Mapping as ORM;
13
use Symfony\Component\Serializer\Annotation\Groups;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * @ORM\MappedSuperclass
18
 * @ORM\HasLifecycleCallbacks
19
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener"})
20
 */
21
abstract class AbstractResource
22
{
23
    /**
24
     * @var string|null
25
     *
26
     * @ApiProperty(iri="http://schema.org/contentUrl")
27
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
28
     */
29
    public $contentUrl;
30
31
    /**
32
     * @var string|null
33
     *
34
     * @ApiProperty(iri="http://schema.org/contentUrl")
35
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
36
     */
37
    public $downloadUrl;
38
39
    /**
40
     * @var string|null
41
     *
42
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "document:write", "media_object_read"})
43
     */
44
    public $contentFile;
45
46
    /**
47
     * @Assert\Valid()
48
     * @ApiSubresource()
49
     * @Groups({"resource_node:read", "resource_node:write", "document:write" })
50
     * @ORM\OneToOne(
51
     *     targetEntity="Chamilo\CoreBundle\Entity\ResourceNode",
52
     *     cascade={"persist", "remove"},
53
     *     orphanRemoval=true
54
     * )
55
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
56
     */
57
    public $resourceNode;
58
59
    /**
60
     * @Groups({"resource_node:read", "resource_node:write", "document:read", "document:write"})
61
     */
62
    public $parentResourceNode;
63
64
    /**
65
     * @ApiProperty(iri="http://schema.org/image")
66
     */
67
    public $uploadFile;
68
69
    /** @var AbstractResource */
70
    public $parentResource;
71
72
    /**
73
     * @Groups({"resource_node:read", "document:read"})
74
     */
75
    public $resourceLinkListFromEntity;
76
77
    /**
78
     * Use when sending a request to Api platform.
79
     * Temporal array that saves the resource link list that will be filled by CreateResourceNodeFileAction.php.
80
     *
81
     * @var array
82
     */
83
    public $resourceLinkList;
84
85
    /**
86
     * Use when sending request to Chamilo.
87
     * Temporal array of objects locates the resource link list that will be filled by CreateResourceNodeFileAction.php.
88
     *
89
     * @var ResourceLink[]
90
     */
91
    public $resourceLinkEntityList;
92
93
    abstract public function getResourceName(): string;
94
95
    abstract public function setResourceName(string $name);
96
97
    public function getResourceLinkEntityList()
98
    {
99
        return $this->resourceLinkEntityList;
100
    }
101
102
    public function addLink(ResourceLink $link)
103
    {
104
        $this->resourceLinkEntityList[] = $link;
105
106
        return $this;
107
    }
108
109
    public function addCourseLink(Course $course, Session $session = null, CGroup $group = null, int $visibility = ResourceLink::VISIBILITY_PUBLISHED)
110
    {
111
        if (null === $this->getParent()) {
112
            throw new \Exception('addCourseLink requires to set the parent.');
113
        }
114
115
        $resourceLink = new ResourceLink();
116
        $resourceLink
117
            ->setVisibility($visibility)
118
            ->setCourse($course)
119
            ->setSession($session)
120
            ->setGroup($group)
121
        ;
122
        $this->addLink($resourceLink);
123
124
        $rights = [];
125
        switch ($visibility) {
126
            case ResourceLink::VISIBILITY_PENDING:
127
            case ResourceLink::VISIBILITY_DRAFT:
128
                $editorMask = ResourceNodeVoter::getEditorMask();
129
                $resourceRight = new ResourceRight();
130
                $resourceRight
131
                    ->setMask($editorMask)
132
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
133
                ;
134
                $rights[] = $resourceRight;
135
136
                break;
137
        }
138
139
        if (!empty($rights)) {
140
            foreach ($rights as $right) {
141
                $resourceLink->addResourceRight($right);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    public function addGroupLink(Course $course, Session $session = null, CGroup $group = null)
149
    {
150
        $resourceNode = $this->getResourceNode();
151
        $exists = $resourceNode->getResourceLinks()->exists(
152
            function ($key, $element) use ($group) {
153
                if ($element->getGroup()) {
154
                    return $group->getIid() == $element->getGroup()->getIid();
0 ignored issues
show
Bug introduced by
The method getIid() 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

154
                    return $group->/** @scrutinizer ignore-call */ getIid() == $element->getGroup()->getIid();

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...
155
                }
156
            }
157
        );
158
159
        if (false === $exists) {
160
            $resourceLink = new ResourceLink();
161
            $resourceLink
162
                ->setResourceNode($resourceNode)
163
                ->setCourse($course)
164
                ->setSession($session)
165
                ->setGroup($group)
166
                ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
167
            ;
168
            $this->addLink($resourceLink);
169
        }
170
171
        return $this;
172
    }
173
174
    public function addUserLink(User $user, Course $course = null, Session $session = null, CGroup $group = null)
175
    {
176
        $resourceLink = new ResourceLink();
177
        $resourceLink
178
            ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
179
            ->setUser($user)
180
            ->setCourse($course)
181
            ->setSession($session)
182
            ->setGroup($group)
183
        ;
184
185
        $this->addLink($resourceLink);
186
187
        return $this;
188
    }
189
190
    public function setParent(AbstractResource $parent)
191
    {
192
        $this->parentResource = $parent;
193
194
        return $this;
195
    }
196
197
    public function getParent()
198
    {
199
        return $this->parentResource;
200
    }
201
202
    /**
203
     * @param array $userList User id list
204
     */
205
    public function addResourceToUserList(
206
        array $userList,
207
        Course $course = null,
208
        Session $session = null,
209
        CGroup $group = null
210
    ) {
211
        if (!empty($userList)) {
212
            foreach ($userList as $user) {
213
                $this->addUserLink($user, $course, $session, $group);
214
            }
215
        }
216
217
        return $this;
218
    }
219
220
    public function setResourceLinkArray(array $links)
221
    {
222
        $this->resourceLinkList = $links;
223
224
        return $this;
225
    }
226
227
    public function getResourceLinkArray()
228
    {
229
        return $this->resourceLinkList;
230
    }
231
232
    public function getResourceLinkListFromEntity()
233
    {
234
        return $this->resourceLinkListFromEntity;
235
    }
236
237
    public function setResourceLinkListFromEntity()
238
    {
239
        $resourceNode = $this->getResourceNode();
240
        $links = $resourceNode->getResourceLinks();
241
        $resourceLinkList = [];
242
243
        foreach ($links as $link) {
244
            $resourceLinkList[] = [
245
                'id' => $link->getId(),
246
                'session' => $link->getSession(),
247
                'course' => $link->getCourse(),
248
                'visibility' => $link->getVisibility(),
249
                'visibilityName' => $link->getVisibilityName(),
250
                'group' => $link->getGroup(),
251
                'userGroup' => $link->getUserGroup(),
252
            ];
253
        }
254
        $this->resourceLinkListFromEntity = $resourceLinkList;
255
    }
256
257
258
    public function hasParentResourceNode(): bool
259
    {
260
        return null !== $this->parentResourceNode;
261
    }
262
263
    public function setParentResourceNode($resourceNode): self
264
    {
265
        $this->parentResourceNode = $resourceNode;
266
267
        return $this;
268
    }
269
270
    public function getParentResourceNode()
271
    {
272
        return $this->parentResourceNode;
273
    }
274
275
    public function hasUploadFile(): bool
276
    {
277
        return null !== $this->uploadFile;
278
    }
279
280
    public function getUploadFile()
281
    {
282
        return $this->uploadFile;
283
    }
284
285
    public function setUploadFile($file): self
286
    {
287
        $this->uploadFile = $file;
288
289
        return $this;
290
    }
291
292
    public function setResourceNode(ResourceNode $resourceNode): self
293
    {
294
        $this->resourceNode = $resourceNode;
295
296
        return $this;
297
    }
298
299
    public function hasResourceNode(): bool
300
    {
301
        return $this->resourceNode instanceof ResourceNode;
302
    }
303
304
    public function getResourceNode(): ResourceNode
305
    {
306
        return $this->resourceNode;
307
    }
308
309
    public function getCourseSessionResourceLink(Course $course, Session $session = null): ?ResourceLink
310
    {
311
        return $this->getFirstResourceLinkFromCourseSession($course, $session);
312
    }
313
314
    public function getFirstResourceLink(): ?ResourceLink
315
    {
316
        $resourceNode = $this->getResourceNode();
317
318
        if ($resourceNode && $resourceNode->getResourceLinks()->count()) {
319
            $result = $resourceNode->getResourceLinks()->first();
320
            if ($result) {
321
                return $result;
322
            }
323
        }
324
325
        return null;
326
    }
327
328
    /**
329
     * See ResourceLink to see the visibility constants. Example: ResourceLink::VISIBILITY_DELETED.
330
     */
331
    public function getLinkVisibility(Course $course, Session $session = null): ?ResourceLink
332
    {
333
        return $this->getCourseSessionResourceLink($course, $session)->getVisibility();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getCourseS...ssion)->getVisibility() returns the type integer which is incompatible with the type-hinted return Chamilo\CoreBundle\Entity\ResourceLink|null.
Loading history...
334
    }
335
336
    public function isVisible(Course $course, Session $session = null): bool
337
    {
338
        $link = $this->getCourseSessionResourceLink($course, $session);
339
        if (null === $link) {
340
            return false;
341
        }
342
343
        return ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility();
344
    }
345
346
    public function getFirstResourceLinkFromCourseSession(Course $course, Session $session = null): ?ResourceLink
347
    {
348
        $criteria = Criteria::create();
349
        $criteria
350
            ->where(Criteria::expr()->eq('course', $course))
351
            ->andWhere(
352
                Criteria::expr()->eq('session', $session)
353
            );
354
        $resourceNode = $this->getResourceNode();
355
356
        $result = null;
357
        if ($resourceNode && $resourceNode->getResourceLinks()->count() > 0) {
358
            //var_dump($resourceNode->getResourceLinks()->count());
359
            foreach ($resourceNode->getResourceLinks() as $link) {
360
                //var_dump(get_class($link));
361
            }
362
            $result = $resourceNode->getResourceLinks()->matching($criteria)->first();
363
            //var_dump($result);
364
            if ($result) {
365
                return $result;
366
            }
367
        }
368
369
        return null;
370
    }
371
}
372