Completed
Push — master ( a3ace3...0ae69c )
by Julito
11:10
created

ResourceRepository::getResourceByCourse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 3
dl 0
loc 50
rs 9.584
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\ResourceLink;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\Tool;
13
use Chamilo\CoreBundle\Entity\Usergroup;
14
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
15
use Chamilo\CourseBundle\Entity\CGroupInfo;
16
use Chamilo\UserBundle\Entity\User;
17
use Doctrine\ORM\EntityManager;
18
use Doctrine\ORM\EntityRepository;
19
use Doctrine\ORM\Query\Expr\Join;
20
use League\Flysystem\FilesystemInterface;
21
use League\Flysystem\MountManager;
22
23
/**
24
 * Class ResourceRepository.
25
 */
26
class ResourceRepository
27
{
28
    /**
29
     * @var EntityRepository
30
     */
31
    protected $repository;
32
33
    /**
34
     * @var FilesystemInterface
35
     */
36
    protected $fs;
37
38
    /**
39
     * @var EntityManager
40
     */
41
    protected $entityManager;
42
43
    /**
44
     * The entity class FQN
45
     *
46
     * @var string
47
     */
48
    protected $className;
49
50
    /**
51
     * ResourceRepository constructor.
52
     *
53
     * @param EntityManager $entityManager
54
     * @param MountManager  $mountManager
55
     * @param string        $className
56
     */
57
    public function __construct(EntityManager $entityManager, MountManager $mountManager, string $className)
58
    {
59
        $this->repository = $entityManager->getRepository($className);
60
        $this->fs = $mountManager->getFilesystem('resources_fs');
61
        $this->entityManager = $entityManager;
62
    }
63
64
    /**
65
     * @param int $id
66
     *
67
     * @return AbstractResource
68
     */
69
    public function find(int $id)
70
    {
71
        return $this->repository->find($id);
72
    }
73
74
    /**
75
     * @param array      $criteria
76
     * @param array|null $orderBy
77
     *
78
     * @return AbstractResource
79
     */
80
    public function findOneBy(array $criteria, array $orderBy = null)
81
    {
82
        return $this->repository->findOneBy($criteria, $orderBy);
83
    }
84
85
    /**
86
     * @return EntityManager
87
     */
88
    public function getEntityManager(): EntityManager
89
    {
90
        return $this->entityManager;
91
    }
92
93
    /**
94
     * Creates a ResourceNode.
95
     *
96
     * @param AbstractResource $resource
97
     * @param User             $creator
98
     * @param AbstractResource $parent
99
     *
100
     * @return ResourceNode
101
     */
102
    public function addResourceNode(
103
        AbstractResource $resource,
104
        User $creator,
105
        AbstractResource $parent = null
106
    ): ResourceNode {
107
        $em = $this->getEntityManager();
108
        $resourceNode = new ResourceNode();
109
110
        //$tool = $this->getTool($resource->getToolName());
111
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
112
            ['name' => $resource->getToolName()]
113
        );
114
115
        $resourceNode
116
            ->setName($resource->getResourceName())
117
            ->setCreator($creator)
118
            ->setResourceType($resourceType);
119
120
        if ($parent !== null) {
121
            $resourceNode->setParent($parent->getResourceNode());
122
        }
123
124
        $resource->setResourceNode($resourceNode);
125
        $em->persist($resourceNode);
126
        $em->persist($resource);
127
        $em->flush();
128
129
        return $resourceNode;
130
    }
131
132
    /**
133
     * @param ResourceNode $resourceNode
134
     *
135
     * @return ResourceLink
136
     */
137
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
138
    {
139
        $resourceLink = new ResourceLink();
140
        $resourceLink
141
            ->setResourceNode($resourceNode)
142
            ->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

142
            ->/** @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...
143
144
        $this->getEntityManager()->persist($resourceLink);
145
        $this->getEntityManager()->flush();
146
147
        return $resourceLink;
148
    }
149
150
    /**
151
     * @param ResourceNode  $resourceNode
152
     * @param ResourceRight $right
153
     *
154
     * @return ResourceLink
155
     */
156
    public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
157
    {
158
        $resourceLink = new ResourceLink();
159
        $resourceLink
160
            ->setResourceNode($resourceNode)
161
            ->addResourceRight($right)
162
        ;
163
164
        $this->getEntityManager()->persist($resourceLink);
165
        $this->getEntityManager()->flush();
166
167
        return $resourceLink;
168
    }
169
170
    /**
171
     * @param ResourceNode $resourceNode
172
     * @param int          $visibility
173
     * @param Course       $course
174
     * @param Session      $session
175
     * @param CGroupInfo   $group
176
     */
177
    public function addResourceToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
178
    {
179
        $visibility = (int) $visibility;
180
        if (empty($visibility)) {
181
            $visibility = ResourceLink::VISIBILITY_PUBLISHED;
182
        }
183
184
        $link = new ResourceLink();
185
        $link
186
            ->setCourse($course)
187
            ->setSession($session)
188
            ->setGroup($group)
189
            //->setUser($toUser)
190
            ->setResourceNode($resourceNode)
191
            ->setVisibility($visibility)
192
        ;
193
194
        $rights = [];
195
        switch ($visibility) {
196
            case ResourceLink::VISIBILITY_PENDING:
197
            case ResourceLink::VISIBILITY_DRAFT:
198
                $editorMask = ResourceNodeVoter::getEditorMask();
199
                $resourceRight = new ResourceRight();
200
                $resourceRight
201
                    ->setMask($editorMask)
202
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
203
                ;
204
                $rights[] = $resourceRight;
205
                break;
206
        }
207
208
        if (!empty($rights)) {
209
            foreach ($rights as $right) {
210
                $link->addResourceRight($right);
211
            }
212
        }
213
214
        $em = $this->getEntityManager();
215
        $em->persist($link);
216
        $em->flush();
217
    }
218
219
    /**
220
     * @param ResourceNode  $resourceNode
221
     * @param Course        $course
222
     * @param ResourceRight $right
223
     *
224
     * @return ResourceLink
225
     */
226
    public function addResourceToCourse2(ResourceNode $resourceNode, Course $course, ResourceRight $right): ResourceLink
227
    {
228
        $resourceLink = new ResourceLink();
229
        $resourceLink
230
            ->setResourceNode($resourceNode)
231
            ->setCourse($course)
232
            ->addResourceRight($right);
233
        $this->getEntityManager()->persist($resourceLink);
234
        $this->getEntityManager()->flush();
235
236
        return $resourceLink;
237
    }
238
239
    /**
240
     * @param ResourceNode $resourceNode
241
     * @param User         $toUser
242
     *
243
     * @return ResourceLink
244
     */
245
    public function addResourceToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
246
    {
247
        $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
248
        $this->getEntityManager()->persist($resourceLink);
249
250
        return $resourceLink;
251
    }
252
253
    /**
254
     * @param ResourceNode $resourceNode
255
     * @param User         $toUser
256
     *
257
     * @return ResourceLink
258
     */
259
    public function addResourceNodeToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
260
    {
261
        $resourceLink = new ResourceLink();
262
        $resourceLink
263
            ->setResourceNode($resourceNode)
264
            ->setUser($toUser);
265
266
        return $resourceLink;
267
    }
268
269
    /**
270
     * @param ResourceNode  $resourceNode
271
     * @param Course        $course
272
     * @param Session       $session
273
     * @param ResourceRight $right
274
     *
275
     * @return ResourceLink
276
     */
277
    public function addResourceToSession(
278
        ResourceNode $resourceNode,
279
        Course $course,
280
        Session $session,
281
        ResourceRight $right
282
    ) {
283
        $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

283
        /** @scrutinizer ignore-call */ 
284
        $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...
284
            $resourceNode,
285
            $course,
286
            $right
287
        );
288
        $resourceLink->setSession($session);
289
        $this->getEntityManager()->persist($resourceLink);
290
291
        return $resourceLink;
292
    }
293
294
    /**
295
     * @param ResourceNode  $resourceNode
296
     * @param Course        $course
297
     * @param CGroupInfo    $group
298
     * @param ResourceRight $right
299
     *
300
     * @return ResourceLink
301
     */
302
    public function addResourceToCourseGroup(
303
        ResourceNode $resourceNode,
304
        Course $course,
305
        CGroupInfo $group,
306
        ResourceRight $right
307
    ) {
308
        $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

308
        /** @scrutinizer ignore-call */ 
309
        $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...
309
            $resourceNode,
310
            $course,
311
            $right
312
        );
313
        $resourceLink->setGroup($group);
314
        $this->getEntityManager()->persist($resourceLink);
315
316
        return $resourceLink;
317
    }
318
319
    /**
320
     * @param ResourceNode  $resourceNode
321
     * @param Usergroup     $group
322
     * @param ResourceRight $right
323
     *
324
     * @return ResourceLink
325
     */
326
    public function addResourceToGroup(
327
        ResourceNode $resourceNode,
328
        Usergroup $group,
329
        ResourceRight $right
330
    ) {
331
        $resourceLink = new ResourceLink();
332
        $resourceLink
333
            ->setResourceNode($resourceNode)
334
            ->setUserGroup($group)
335
            ->addResourceRight($right);
336
337
        return $resourceLink;
338
    }
339
340
    /**
341
     * @param ResourceNode $resourceNode
342
     * @param array        $userList     User id list
343
     */
344
    public function addResourceToUserList(ResourceNode $resourceNode, array $userList)
345
    {
346
        $em = $this->getEntityManager();
347
348
        if (!empty($userList)) {
349
            foreach ($userList as $userId) {
350
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
351
352
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
353
                $em->persist($resourceLink);
354
            }
355
        }
356
    }
357
358
    /**
359
     * @param Course           $course
360
     * @param Tool             $tool
361
     * @param AbstractResource $parent
362
     *
363
     * @return ResourceLink
364
     */
365
    public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
366
    {
367
        $query = $this->getEntityManager()->createQueryBuilder()
368
            ->select('resource')
369
            ->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
370
            ->innerJoin('node.links', 'links')
371
            ->innerJoin(
372
                $this->getClassName(),
0 ignored issues
show
Bug introduced by
The method getClassName() does not exist on Chamilo\CoreBundle\Repository\ResourceRepository. ( Ignorable by Annotation )

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

372
                $this->/** @scrutinizer ignore-call */ 
373
                       getClassName(),

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...
373
                'resource',
374
                Join::WITH,
375
                'resource.course = links.course AND resource.resourceNode = node.id'
376
            )
377
            ->where('node.tool = :tool')
378
            ->andWhere('links.course = :course')
379
            //->where('link.cId = ?', $course->getId())
380
            //->where('node.cId = 0')
381
            //->orderBy('node');
382
            ->setParameters(
383
                [
384
                    'tool' => $tool,
385
                    'course' => $course,
386
                ]
387
            );
388
389
        if ($parent !== null) {
390
            $query->andWhere('node.parent = :parentId');
391
            $query->setParameter('parentId', $parent->getResourceNode()->getId());
392
        } else {
393
            $query->andWhere('node.parent IS NULL');
394
        }
395
396
        $query = $query->getQuery();
397
398
        /*$query = $this->getEntityManager()->createQueryBuilder()
399
            ->select('notebook')
400
            ->from('ChamiloNotebookBundle:CNotebook', 'notebook')
401
            ->innerJoin('notebook.resourceNodes', 'node')
402
            //->innerJoin('node.links', 'link')
403
            ->where('node.tool = :tool')
404
            //->where('link.cId = ?', $course->getId())
405
            //->where('node.cId = 0')
406
            //->orderBy('node');
407
            ->setParameters(array(
408
                    'tool'=> 'notebook'
409
                )
410
            )
411
            ->getQuery()
412
        ;*/
413
414
        return $query->getResult();
415
    }
416
417
    /**
418
     * @param string $tool
419
     *
420
     * @return Tool|null
421
     */
422
    public function getTool($tool)
423
    {
424
        return $this
425
            ->getEntityManager()
426
            ->getRepository('ChamiloCoreBundle:Tool')
427
            ->findOneBy(['name' => $tool]);
428
    }
429
}
430