Completed
Push — master ( ba250d...4fc9f8 )
by Julito
11:07
created

AbstractResource::setParentResourceNode()   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
namespace Chamilo\CoreBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiProperty;
8
use ApiPlatform\Core\Annotation\ApiSubresource;
9
use Chamilo\CourseBundle\Entity\CGroup;
10
use Doctrine\Common\Collections\Criteria;
11
use Doctrine\ORM\Mapping as ORM;
12
use Symfony\Component\Serializer\Annotation\Groups;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * @ORM\MappedSuperclass
17
 * @ORM\HasLifecycleCallbacks
18
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener"})
19
 */
20
abstract class AbstractResource
21
{
22
    /**
23
     * @var string|null
24
     *
25
     * @ApiProperty(iri="http://schema.org/contentUrl")
26
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
27
     */
28
    public $contentUrl;
29
30
    /**
31
     * @var string|null
32
     *
33
     * @Groups({"resource_file:read", "resource_node:read", "document:read", "document:write", "media_object_read"})
34
     */
35
    public $contentFile;
36
37
    /**
38
     * @Assert\Valid()
39
     * @ApiSubresource()
40
     * @Groups({"resource_node:read", "resource_node:write", "document:write" })
41
     * @ORM\OneToOne(
42
     *     targetEntity="Chamilo\CoreBundle\Entity\ResourceNode",
43
     *     cascade={"persist", "remove"},
44
     *     orphanRemoval=true
45
     * )
46
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
47
     */
48
    public $resourceNode;
49
50
    /**
51
     * @Groups({"resource_node:read", "resource_node:write", "document:read", "document:write"})
52
     */
53
    public $parentResourceNode;
54
55
    /**
56
     * @ApiProperty(iri="http://schema.org/image")
57
     */
58
    public $uploadFile;
59
60
    /**
61
     * @Groups({"resource_node:read", "document:read"})
62
     */
63
    public $resourceLinkList;
64
65
    /** @var ResourceLink[] */
66
    public $linkEntityList;
67
68
    /** @var AbstractResource */
69
    public $parentResource;
70
71
    abstract public function getResourceName(): string;
72
73
    public function addLink(ResourceLink $link)
74
    {
75
        $this->linkEntityList[] = $link;
76
77
        return $this;
78
    }
79
80
    public function addCourseLink(Course $course, Session $session = null, CGroup $group = null)
81
    {
82
        $resourceLink = new ResourceLink();
83
        $resourceLink
84
            ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
85
            ->setCourse($course)
86
            ->setSession($session)
87
            ->setGroup($group)
88
        ;
89
        $this->addLink($resourceLink);
90
91
        return $this;
92
    }
93
94
    public function addGroupLink(Course $course, Session $session = null, CGroup $group = null)
95
    {
96
        $resourceNode = $this->getResourceNode();
97
        $exists = $resourceNode->getResourceLinks()->exists(
98
            function ($key, $element) use ($group) {
99
                if ($element->getGroup()) {
100
                    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

100
                    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...
101
                }
102
            }
103
        );
104
105
        if (false === $exists) {
106
            $resourceLink = new ResourceLink();
107
            $resourceLink
108
                ->setResourceNode($resourceNode)
109
                ->setCourse($course)
110
                ->setSession($session)
111
                ->setGroup($group)
112
                ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
113
            ;
114
            $this->addLink($resourceLink);
115
        }
116
117
        return $this;
118
    }
119
120
    public function addUserLink(User $user, Course $course = null, Session $session = null, CGroup $group = null)
121
    {
122
        $resourceLink = new ResourceLink();
123
        $resourceLink
124
            ->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
125
            ->setUser($user)
126
            ->setCourse($course)
127
            ->setSession($session)
128
            ->setGroup($group)
129
        ;
130
131
        $this->addLink($resourceLink);
132
133
        return $this;
134
    }
135
136
    public function setParent(AbstractResource $parent)
137
    {
138
        $this->parentResource = $parent;
139
140
        return $this;
141
    }
142
143
    public function getParent()
144
    {
145
        return $this->parentResource;
146
    }
147
148
    /**
149
     * @param array $userList User id list
150
     */
151
    public function addResourceToUserList(
152
        array $userList,
153
        Course $course = null,
154
        Session $session = null,
155
        CGroup $group = null
156
    ) {
157
        if (!empty($userList)) {
158
            foreach ($userList as $user) {
159
                $this->addUserLink($user, $course, $session, $group);
160
            }
161
        }
162
163
        return $this;
164
    }
165
166
    public function getLinks()
167
    {
168
        return $this->linkEntityList;
169
    }
170
171
    public function addResourceLink($link)
172
    {
173
        $this->resourceLinkList[] = $link;
174
175
        return $this;
176
    }
177
178
    public function setResourceLinkList($links)
179
    {
180
        $this->resourceLinkList = $links;
181
182
        return $this;
183
    }
184
185
    public function getResourceLinkListFromEntity()
186
    {
187
        return $this->resourceLinkList;
188
    }
189
190
    /*public function getResourceLinkList(): array
191
    {
192
        $resourceNode = $this->getResourceNode();
193
        $links = $resourceNode->getResourceLinks();
194
        $resourceLinkList = [];
195
196
        foreach ($links as $link) {
197
            $resourceLinkList[] = [
198
                'id' => $link->getId(),
199
                'session' => $link->getSession(),
200
                'course' => $link->getCourse(),
201
                'visibility' => $link->getVisibility(),
202
                'visibilityName' => $link->getVisibilityName(),
203
                'group' => $link->getGroup(),
204
                'userGroup' => $link->getUserGroup(),
205
            ];
206
        }
207
208
        return $resourceLinkList;
209
    }*/
210
211
    public function hasParentResourceNode(): bool
212
    {
213
        return null !== $this->parentResourceNode;
214
    }
215
216
    public function setParentResourceNode($resourceNode): self
217
    {
218
        $this->parentResourceNode = $resourceNode;
219
220
        return $this;
221
    }
222
223
    public function getParentResourceNode()
224
    {
225
        return $this->parentResourceNode;
226
    }
227
228
    public function hasUploadFile(): bool
229
    {
230
        return null !== $this->uploadFile;
231
    }
232
233
    public function getUploadFile()
234
    {
235
        return $this->uploadFile;
236
    }
237
238
    public function setUploadFile($file): self
239
    {
240
        $this->uploadFile = $file;
241
242
        return $this;
243
    }
244
245
    public function setResourceNode(ResourceNode $resourceNode): self
246
    {
247
        $this->resourceNode = $resourceNode;
248
249
        return $this;
250
    }
251
252
    public function hasResourceNode(): bool
253
    {
254
        return $this->resourceNode instanceof ResourceNode;
255
    }
256
257
    public function getResourceNode(): ResourceNode
258
    {
259
        return $this->resourceNode;
260
    }
261
262
    public function getCourseSessionResourceLink(Course $course, Session $session = null): ?ResourceLink
263
    {
264
        return $this->getFirstResourceLinkFromCourseSession($course, $session);
265
    }
266
267
    public function getFirstResourceLink(): ?ResourceLink
268
    {
269
        $resourceNode = $this->getResourceNode();
270
271
        if ($resourceNode && $resourceNode->getResourceLinks()->count()) {
272
            $result = $resourceNode->getResourceLinks()->first();
273
            if ($result) {
274
                return $result;
275
            }
276
        }
277
278
        return null;
279
    }
280
281
    /**
282
     * See ResourceLink to see the visibility constants. Example: ResourceLink::VISIBILITY_DELETED.
283
     */
284
    public function getLinkVisibility(Course $course, Session $session = null): ?ResourceLink
285
    {
286
        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...
287
    }
288
289
    public function isVisible(Course $course, Session $session = null): bool
290
    {
291
        $link = $this->getCourseSessionResourceLink($course, $session);
292
        if (null === $link) {
293
            return false;
294
        }
295
296
        return ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility();
297
    }
298
299
    public function getFirstResourceLinkFromCourseSession(Course $course, Session $session = null): ?ResourceLink
300
    {
301
        $criteria = Criteria::create();
302
        $criteria
303
            ->where(Criteria::expr()->eq('course', $course))
304
            ->andWhere(
305
                Criteria::expr()->eq('session', $session)
306
            );
307
        $resourceNode = $this->getResourceNode();
308
309
        $result = null;
310
        if ($resourceNode && $resourceNode->getResourceLinks()->count() > 0) {
311
            //var_dump($resourceNode->getResourceLinks()->count());
312
            foreach ($resourceNode->getResourceLinks() as $link) {
313
                //var_dump(get_class($link));
314
            }
315
            $result = $resourceNode->getResourceLinks()->matching($criteria)->first();
316
            //var_dump($result);
317
            if ($result) {
318
                return $result;
319
            }
320
        }
321
322
        return null;
323
    }
324
}
325