Passed
Push — master ( 323135...9f183a )
by Julito
13:17
created

ResourceRepository::getIllustration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nop 1
dl 0
loc 15
rs 10
nc 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceFile;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
11
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\Tool;
14
use Chamilo\CoreBundle\Entity\Usergroup;
15
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
16
use Chamilo\CourseBundle\Entity\CGroupInfo;
17
use Chamilo\UserBundle\Entity\User;
18
use Doctrine\Common\Collections\Criteria;
19
use Doctrine\ORM\EntityManager;
20
use Doctrine\ORM\Query\Expr\Join;
21
use League\Flysystem\FilesystemInterface;
22
use League\Flysystem\MountManager;
23
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
24
use Symfony\Component\HttpFoundation\File\UploadedFile;
25
26
/**
27
 * Class ResourceRepository.
28
 */
29
class ResourceRepository extends EntityRepository
30
{
31
    /**
32
     * @var EntityRepository
33
     */
34
    protected $repository;
35
36
    /**
37
     * @var FilesystemInterface
38
     */
39
    protected $fs;
40
41
    /**
42
     * @var EntityManager
43
     */
44
    protected $entityManager;
45
46
    /**
47
     * The entity class FQN.
48
     *
49
     * @var string
50
     */
51
    protected $className;
52
53
    /**
54
     * ResourceRepository constructor.
55
     *
56
     * @param EntityManager $entityManager
57
     * @param MountManager  $mountManager
58
     * @param string        $className
59
     */
60
    public function __construct(EntityManager $entityManager, MountManager $mountManager, string $className)
61
    {
62
        $this->repository = $entityManager->getRepository($className);
63
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
64
        $this->fs = $mountManager->getFilesystem('resources_fs');
65
    }
66
67
    /**
68
     * @return FilesystemInterface
69
     */
70
    public function getFileSystem()
71
    {
72
        return $this->fs;
73
    }
74
75
    /**
76
     * @return EntityManager
77
     */
78
    public function getEntityManager(): EntityManager
79
    {
80
        return $this->entityManager;
81
    }
82
83
    /**
84
     * @return EntityRepository
85
     */
86
    public function getRepository()
87
    {
88
        return $this->repository;
89
    }
90
91
    /**
92
     * @param mixed $id
93
     * @param null  $lockMode
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lockMode is correct as it would always require null to be passed?
Loading history...
94
     * @param null  $lockVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lockVersion is correct as it would always require null to be passed?
Loading history...
95
     *
96
     * @return AbstractResource|null
97
     */
98
    public function find($id, $lockMode = null, $lockVersion = null)
99
    {
100
        return $this->getRepository()->find($id);
101
    }
102
103
    /**
104
     * @param array      $criteria
105
     * @param array|null $orderBy
106
     *
107
     * @return AbstractResource
108
     */
109
    public function findOneBy(array $criteria, array $orderBy = null)
110
    {
111
        return $this->getRepository()->findOneBy($criteria, $orderBy);
112
    }
113
114
    /**
115
     * @param AbstractResource $resource
116
     *
117
     * @return ResourceNode|mixed
118
     */
119
    public function getIllustration(AbstractResource $resource)
120
    {
121
        $node = $resource->getResourceNode();
122
        // @todo also filter by the resource type = Illustration
123
        $criteria = Criteria::create()->where(
124
            Criteria::expr()->eq('name', 'course_picture')
125
        );
126
127
        $illustration = $node->getChildren()->matching($criteria)->first();
128
129
        return $illustration;
130
131
        /** @var ResourceNode $illustration */
132
        //$illustration = $this->getRepository()->findOneBy(['parent' => $node, 'name' => 'course_picture']);
133
        var_dump($illustration);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($illustration) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Unused Code introduced by
var_dump($illustration) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
134
    }
135
136
    /**
137
     * @param AbstractResource $resource
138
     * @param UploadedFile     $file
139
     *
140
     * @return ResourceFile
141
     */
142
    public function addFileToResource(AbstractResource $resource, UploadedFile $file)
143
    {
144
        $resourceNode = $resource->getResourceNode();
145
146
        if (!$resourceNode) {
0 ignored issues
show
introduced by
$resourceNode is of type Chamilo\CoreBundle\Entity\Resource\ResourceNode, thus it always evaluated to true.
Loading history...
147
            return false;
148
        }
149
150
        $resourceFile = $resourceNode->getResourceFile();
151
        if ($resourceFile === null) {
152
            $resourceFile = new ResourceFile();
153
        }
154
155
        $em = $this->getEntityManager();
156
157
        $resourceFile->setFile($file);
158
        $resourceFile->setName($resource->getResourceName());
159
        $em->persist($resourceFile);
160
        $resourceNode->setResourceFile($resourceFile);
161
        $em->persist($resourceNode);
162
        $em->flush();
163
164
        return $resourceFile;
165
    }
166
167
168
    /**
169
     * Creates a ResourceNode.
170
     *
171
     * @param AbstractResource $resource
172
     * @param User             $creator
173
     * @param AbstractResource $parent
174
     *
175
     * @return ResourceNode
176
     */
177
    public function addResourceNode(
178
        AbstractResource $resource,
179
        User $creator,
180
        AbstractResource $parent = null
181
    ): ResourceNode {
182
        $em = $this->getEntityManager();
183
184
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
185
            ['name' => $resource->getToolName()]
186
        );
187
188
        $resourceNode = new ResourceNode();
189
        $resourceNode
190
            ->setName($resource->getResourceName())
191
            ->setCreator($creator)
192
            ->setResourceType($resourceType)
193
        ;
194
195
        if ($parent !== null) {
196
            $resourceNode->setParent($parent->getResourceNode());
197
        }
198
199
        $resource->setResourceNode($resourceNode);
200
201
        $em->persist($resourceNode);
202
        $em->persist($resource);
203
204
        return $resourceNode;
205
    }
206
207
    /**
208
     * @param ResourceNode $resourceNode
209
     *
210
     * @return ResourceLink
211
     */
212
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
213
    {
214
        $resourceLink = new ResourceLink();
215
        $resourceLink
216
            ->setResourceNode($resourceNode)
217
            ->setPrivate(true);
0 ignored issues
show
Bug introduced by
The method setPrivate() does not exist on Chamilo\CoreBundle\Entity\Resource\ResourceLink. ( Ignorable by Annotation )

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

217
            ->/** @scrutinizer ignore-call */ setPrivate(true);

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...
218
219
        $this->getEntityManager()->persist($resourceLink);
220
        $this->getEntityManager()->flush();
221
222
        return $resourceLink;
223
    }
224
225
    /**
226
     * @param ResourceNode  $resourceNode
227
     * @param ResourceRight $right
228
     *
229
     * @return ResourceLink
230
     */
231
    public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
232
    {
233
        $resourceLink = new ResourceLink();
234
        $resourceLink
235
            ->setResourceNode($resourceNode)
236
            ->addResourceRight($right)
237
        ;
238
239
        $this->getEntityManager()->persist($resourceLink);
240
        $this->getEntityManager()->flush();
241
242
        return $resourceLink;
243
    }
244
245
    /**
246
     * @param ResourceNode $resourceNode
247
     * @param int          $visibility
248
     * @param Course       $course
249
     * @param Session      $session
250
     * @param CGroupInfo   $group
251
     */
252
    public function addResourceToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
253
    {
254
        $visibility = (int) $visibility;
255
        if (empty($visibility)) {
256
            $visibility = ResourceLink::VISIBILITY_PUBLISHED;
257
        }
258
259
        $link = new ResourceLink();
260
        $link
261
            ->setCourse($course)
262
            ->setSession($session)
263
            ->setGroup($group)
264
            //->setUser($toUser)
265
            ->setResourceNode($resourceNode)
266
            ->setVisibility($visibility)
267
        ;
268
269
        $rights = [];
270
        switch ($visibility) {
271
            case ResourceLink::VISIBILITY_PENDING:
272
            case ResourceLink::VISIBILITY_DRAFT:
273
                $editorMask = ResourceNodeVoter::getEditorMask();
274
                $resourceRight = new ResourceRight();
275
                $resourceRight
276
                    ->setMask($editorMask)
277
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
278
                ;
279
                $rights[] = $resourceRight;
280
                break;
281
        }
282
283
        if (!empty($rights)) {
284
            foreach ($rights as $right) {
285
                $link->addResourceRight($right);
286
            }
287
        }
288
289
        $em = $this->getEntityManager();
290
        $em->persist($link);
291
    }
292
293
    /**
294
     * @param ResourceNode  $resourceNode
295
     * @param Course        $course
296
     * @param ResourceRight $right
297
     *
298
     * @return ResourceLink
299
     */
300
    public function addResourceToCourse2(ResourceNode $resourceNode, Course $course, ResourceRight $right): ResourceLink
301
    {
302
        $resourceLink = new ResourceLink();
303
        $resourceLink
304
            ->setResourceNode($resourceNode)
305
            ->setCourse($course)
306
            ->addResourceRight($right);
307
        $this->getEntityManager()->persist($resourceLink);
308
        $this->getEntityManager()->flush();
309
310
        return $resourceLink;
311
    }
312
313
    /**
314
     * @param ResourceNode $resourceNode
315
     * @param User         $toUser
316
     *
317
     * @return ResourceLink
318
     */
319
    public function addResourceToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
320
    {
321
        $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
322
        $this->getEntityManager()->persist($resourceLink);
323
324
        return $resourceLink;
325
    }
326
327
    /**
328
     * @param ResourceNode $resourceNode
329
     * @param User         $toUser
330
     *
331
     * @return ResourceLink
332
     */
333
    public function addResourceNodeToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
334
    {
335
        $resourceLink = new ResourceLink();
336
        $resourceLink
337
            ->setResourceNode($resourceNode)
338
            ->setUser($toUser);
339
340
        return $resourceLink;
341
    }
342
343
    /**
344
     * @param ResourceNode  $resourceNode
345
     * @param Course        $course
346
     * @param Session       $session
347
     * @param ResourceRight $right
348
     *
349
     * @return ResourceLink
350
     */
351
    public function addResourceToSession(
352
        ResourceNode $resourceNode,
353
        Course $course,
354
        Session $session,
355
        ResourceRight $right
356
    ) {
357
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resourceLink is correct as $this->addResourceToCour...eNode, $course, $right) targeting Chamilo\CoreBundle\Repos...::addResourceToCourse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with session. ( Ignorable by Annotation )

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

357
        /** @scrutinizer ignore-call */ 
358
        $resourceLink = $this->addResourceToCourse(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
358
            $resourceNode,
359
            $course,
360
            $right
361
        );
362
        $resourceLink->setSession($session);
363
        $this->getEntityManager()->persist($resourceLink);
364
365
        return $resourceLink;
366
    }
367
368
    /**
369
     * @param ResourceNode  $resourceNode
370
     * @param Course        $course
371
     * @param CGroupInfo    $group
372
     * @param ResourceRight $right
373
     *
374
     * @return ResourceLink
375
     */
376
    public function addResourceToCourseGroup(
377
        ResourceNode $resourceNode,
378
        Course $course,
379
        CGroupInfo $group,
380
        ResourceRight $right
381
    ) {
382
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with session. ( Ignorable by Annotation )

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

382
        /** @scrutinizer ignore-call */ 
383
        $resourceLink = $this->addResourceToCourse(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the assignment to $resourceLink is correct as $this->addResourceToCour...eNode, $course, $right) targeting Chamilo\CoreBundle\Repos...::addResourceToCourse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
383
            $resourceNode,
384
            $course,
385
            $right
386
        );
387
        $resourceLink->setGroup($group);
388
        $this->getEntityManager()->persist($resourceLink);
389
390
        return $resourceLink;
391
    }
392
393
    /**
394
     * @param ResourceNode  $resourceNode
395
     * @param Usergroup     $group
396
     * @param ResourceRight $right
397
     *
398
     * @return ResourceLink
399
     */
400
    public function addResourceToGroup(
401
        ResourceNode $resourceNode,
402
        Usergroup $group,
403
        ResourceRight $right
404
    ) {
405
        $resourceLink = new ResourceLink();
406
        $resourceLink
407
            ->setResourceNode($resourceNode)
408
            ->setUserGroup($group)
409
            ->addResourceRight($right);
410
411
        return $resourceLink;
412
    }
413
414
    /**
415
     * @param ResourceNode $resourceNode
416
     * @param array        $userList     User id list
417
     */
418
    public function addResourceToUserList(ResourceNode $resourceNode, array $userList)
419
    {
420
        $em = $this->getEntityManager();
421
422
        if (!empty($userList)) {
423
            foreach ($userList as $userId) {
424
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
425
426
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
427
                $em->persist($resourceLink);
428
            }
429
        }
430
    }
431
432
    /**
433
     * @param Course           $course
434
     * @param Tool             $tool
435
     * @param AbstractResource $parent
436
     *
437
     * @return ResourceLink
438
     */
439
    public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
440
    {
441
        $query = $this->getEntityManager()->createQueryBuilder()
442
            ->select('resource')
443
            ->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
444
            ->innerJoin('node.links', 'links')
445
            ->innerJoin(
446
                $this->getClassName(),
447
                'resource',
448
                Join::WITH,
449
                'resource.course = links.course AND resource.resourceNode = node.id'
450
            )
451
            ->where('node.tool = :tool')
452
            ->andWhere('links.course = :course')
453
            //->where('link.cId = ?', $course->getId())
454
            //->where('node.cId = 0')
455
            //->orderBy('node');
456
            ->setParameters(
457
                [
458
                    'tool' => $tool,
459
                    'course' => $course,
460
                ]
461
            );
462
463
        if ($parent !== null) {
464
            $query->andWhere('node.parent = :parentId');
465
            $query->setParameter('parentId', $parent->getResourceNode()->getId());
466
        } else {
467
            $query->andWhere('node.parent IS NULL');
468
        }
469
470
        $query = $query->getQuery();
471
472
        /*$query = $this->getEntityManager()->createQueryBuilder()
473
            ->select('notebook')
474
            ->from('ChamiloNotebookBundle:CNotebook', 'notebook')
475
            ->innerJoin('notebook.resourceNodes', 'node')
476
            //->innerJoin('node.links', 'link')
477
            ->where('node.tool = :tool')
478
            //->where('link.cId = ?', $course->getId())
479
            //->where('node.cId = 0')
480
            //->orderBy('node');
481
            ->setParameters(array(
482
                    'tool'=> 'notebook'
483
                )
484
            )
485
            ->getQuery()
486
        ;*/
487
488
        return $query->getResult();
489
    }
490
491
    /**
492
     * @param string $tool
493
     *
494
     * @return Tool|null
495
     */
496
    public function getTool($tool)
497
    {
498
        return $this
499
            ->getEntityManager()
500
            ->getRepository('ChamiloCoreBundle:Tool')
501
            ->findOneBy(['name' => $tool]);
502
    }
503
504
    /**
505
     * @return mixed
506
     */
507
    public function create()
508
    {
509
        return new $this->className();
510
    }
511
}
512