Passed
Push — master ( 8cd85a...5627cd )
by Julito
09:53 queued 10s
created

ResourceRepository::getTool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\ORM\EntityManager;
19
use Doctrine\ORM\Query\Expr\Join;
20
use League\Flysystem\FilesystemInterface;
21
use League\Flysystem\MountManager;
22
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
use Symfony\Component\Routing\RouterInterface;
25
26
/**
27
 * Class ResourceRepository.
28
 */
29
class ResourceRepository extends EntityRepository
30
{
31
    /** @var string */
32
    public $shortClassName;
33
    /**
34
     * @var EntityRepository
35
     */
36
    protected $repository;
37
38
    /**
39
     * @var FilesystemInterface
40
     */
41
    protected $fs;
42
43
    /**
44
     * @var EntityManager
45
     */
46
    protected $entityManager;
47
48
    /**
49
     * The entity class FQN.
50
     *
51
     * @var string
52
     */
53
    protected $className;
54
55
    /** @var RouterInterface */
56
    protected $router;
57
58
    protected $resourceNodeRepository;
59
60
    /**
61
     * ResourceRepository constructor.
62
     *
63
     * @param EntityManager   $entityManager
64
     * @param MountManager    $mountManager
65
     * @param RouterInterface $router
66
     * @param string          $className
67
     */
68
    public function __construct(EntityManager $entityManager, MountManager $mountManager, RouterInterface $router, string $className)
69
    {
70
        $this->repository = $entityManager->getRepository($className);
71
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
72
        $this->fs = $mountManager->getFilesystem('resources_fs');
73
        $this->router = $router;
74
        $this->resourceNodeRepository = $entityManager->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
75
        $this->shortClassName = (new \ReflectionClass($className))->getShortName();
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getShortClassName(): string
82
    {
83
        return $this->shortClassName;
84
    }
85
86
    /**
87
     * @return EntityRepository
88
     */
89
    public function getResourceNodeRepository()
90
    {
91
        return $this->resourceNodeRepository;
92
    }
93
94
    /**
95
     * @return FilesystemInterface
96
     */
97
    public function getFileSystem()
98
    {
99
        return $this->fs;
100
    }
101
102
    /**
103
     * @return EntityManager
104
     */
105
    public function getEntityManager(): EntityManager
106
    {
107
        return $this->getRepository()->getEntityManager();
108
    }
109
110
    /**
111
     * @return EntityRepository
112
     */
113
    public function getRepository()
114
    {
115
        return $this->repository;
116
    }
117
118
    /**
119
     * @param mixed $id
120
     * @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...
121
     * @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...
122
     *
123
     * @return AbstractResource|null
124
     */
125
    public function find($id, $lockMode = null, $lockVersion = null)
126
    {
127
        return $this->getRepository()->find($id);
128
    }
129
130
    /**
131
     * @param array      $criteria
132
     * @param array|null $orderBy
133
     *
134
     * @return AbstractResource
135
     */
136
    public function findOneBy(array $criteria, array $orderBy = null)
137
    {
138
        return $this->getRepository()->findOneBy($criteria, $orderBy);
139
    }
140
141
    /**
142
     * @param ResourceNode $resourceNode
143
     * @param UploadedFile $file
144
     *
145
     * @return ResourceFile
146
     */
147
    public function addFile(ResourceNode $resourceNode, UploadedFile $file)
148
    {
149
        $resourceFile = $resourceNode->getResourceFile();
150
        if ($resourceFile === null) {
151
            $resourceFile = new ResourceFile();
152
        }
153
154
        $em = $this->getEntityManager();
155
156
        $resourceFile->setFile($file);
157
        $resourceFile->setName($resourceNode->getName());
158
        $em->persist($resourceFile);
159
        $resourceNode->setResourceFile($resourceFile);
160
        $em->persist($resourceNode);
161
        $em->flush();
162
163
        return $resourceFile;
164
    }
165
166
    /**
167
     * Creates a ResourceNode.
168
     *
169
     * @param AbstractResource $resource
170
     * @param User             $creator
171
     * @param AbstractResource $parent
172
     *
173
     * @return ResourceNode
174
     */
175
    public function addResourceNode(
176
        AbstractResource $resource,
177
        User $creator,
178
        AbstractResource $parent = null
179
    ): ResourceNode {
180
        $em = $this->getEntityManager();
181
182
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
183
            ['name' => $this->getShortClassName()]
184
        );
185
186
        $resourceNode = new ResourceNode();
187
        $resourceNode
188
            ->setName($resource->getResourceName())
189
            ->setCreator($creator)
190
            ->setResourceType($resourceType)
191
        ;
192
193
        if ($parent !== null) {
194
            $resourceNode->setParent($parent->getResourceNode());
195
        }
196
197
        $resource->setResourceNode($resourceNode);
198
199
        $em->persist($resourceNode);
200
        $em->persist($resource);
201
202
        return $resourceNode;
203
    }
204
205
    /**
206
     * @param ResourceNode $resourceNode
207
     *
208
     * @return ResourceLink
209
     */
210
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
211
    {
212
        $resourceLink = new ResourceLink();
213
        $resourceLink
214
            ->setResourceNode($resourceNode)
215
            ->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

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

355
        /** @scrutinizer ignore-call */ 
356
        $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...
356
            $resourceNode,
357
            $course,
358
            $right
359
        );
360
        $resourceLink->setSession($session);
361
        $this->getEntityManager()->persist($resourceLink);
362
363
        return $resourceLink;
364
    }
365
366
    /**
367
     * @param ResourceNode  $resourceNode
368
     * @param Course        $course
369
     * @param CGroupInfo    $group
370
     * @param ResourceRight $right
371
     *
372
     * @return ResourceLink
373
     */
374
    public function addResourceToCourseGroup(
375
        ResourceNode $resourceNode,
376
        Course $course,
377
        CGroupInfo $group,
378
        ResourceRight $right
379
    ) {
380
        $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

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