Completed
Push — master ( b05893...eedd1b )
by Julito
09:17
created

ResourceRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
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\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\Query\Expr\Join;
19
use League\Flysystem\FilesystemInterface;
20
use League\Flysystem\MountManager;
21
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
22
23
/**
24
 * Class ResourceRepository.
25
 */
26
class ResourceRepository extends EntityRepository
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
     * @return EntityRepository
66
     */
67
    public function getRepository()
68
    {
69
        return $this->repository;
70
    }
71
72
    /**
73
     * @param mixed $id
74
     * @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...
75
     * @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...
76
     *
77
     * @return object|null
78
     */
79
    public function find($id, $lockMode = null, $lockVersion = null)
80
    {
81
        return $this->getRepository()->find($id);
82
    }
83
84
    /**
85
     * @param array      $criteria
86
     * @param array|null $orderBy
87
     *
88
     * @return AbstractResource
89
     */
90
    public function findOneBy(array $criteria, array $orderBy = null)
91
    {
92
        return $this->getRepository()->findOneBy($criteria, $orderBy);
93
    }
94
95
    /**
96
     * @return EntityManager
97
     */
98
    public function getEntityManager(): EntityManager
99
    {
100
        return $this->entityManager;
101
    }
102
103
    /**
104
     * Creates a ResourceNode.
105
     *
106
     * @param AbstractResource $resource
107
     * @param User             $creator
108
     * @param AbstractResource $parent
109
     *
110
     * @return ResourceNode
111
     */
112
    public function addResourceNode(
113
        AbstractResource $resource,
114
        User $creator,
115
        AbstractResource $parent = null
116
    ): ResourceNode {
117
        $em = $this->getEntityManager();
118
        $resourceNode = new ResourceNode();
119
120
        //$tool = $this->getTool($resource->getToolName());
121
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
122
            ['name' => $resource->getToolName()]
123
        );
124
125
        $resourceNode
126
            ->setName($resource->getResourceName())
127
            ->setCreator($creator)
128
            ->setResourceType($resourceType);
129
130
        if ($parent !== null) {
131
            $resourceNode->setParent($parent->getResourceNode());
132
        }
133
134
        $resource->setResourceNode($resourceNode);
135
        $em->persist($resourceNode);
136
        $em->persist($resource);
137
        $em->flush();
138
139
        return $resourceNode;
140
    }
141
142
    /**
143
     * @param ResourceNode $resourceNode
144
     *
145
     * @return ResourceLink
146
     */
147
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
148
    {
149
        $resourceLink = new ResourceLink();
150
        $resourceLink
151
            ->setResourceNode($resourceNode)
152
            ->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

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

293
        /** @scrutinizer ignore-call */ 
294
        $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...
294
            $resourceNode,
295
            $course,
296
            $right
297
        );
298
        $resourceLink->setSession($session);
299
        $this->getEntityManager()->persist($resourceLink);
300
301
        return $resourceLink;
302
    }
303
304
    /**
305
     * @param ResourceNode  $resourceNode
306
     * @param Course        $course
307
     * @param CGroupInfo    $group
308
     * @param ResourceRight $right
309
     *
310
     * @return ResourceLink
311
     */
312
    public function addResourceToCourseGroup(
313
        ResourceNode $resourceNode,
314
        Course $course,
315
        CGroupInfo $group,
316
        ResourceRight $right
317
    ) {
318
        $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

318
        /** @scrutinizer ignore-call */ 
319
        $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...
319
            $resourceNode,
320
            $course,
321
            $right
322
        );
323
        $resourceLink->setGroup($group);
324
        $this->getEntityManager()->persist($resourceLink);
325
326
        return $resourceLink;
327
    }
328
329
    /**
330
     * @param ResourceNode  $resourceNode
331
     * @param Usergroup     $group
332
     * @param ResourceRight $right
333
     *
334
     * @return ResourceLink
335
     */
336
    public function addResourceToGroup(
337
        ResourceNode $resourceNode,
338
        Usergroup $group,
339
        ResourceRight $right
340
    ) {
341
        $resourceLink = new ResourceLink();
342
        $resourceLink
343
            ->setResourceNode($resourceNode)
344
            ->setUserGroup($group)
345
            ->addResourceRight($right);
346
347
        return $resourceLink;
348
    }
349
350
    /**
351
     * @param ResourceNode $resourceNode
352
     * @param array        $userList     User id list
353
     */
354
    public function addResourceToUserList(ResourceNode $resourceNode, array $userList)
355
    {
356
        $em = $this->getEntityManager();
357
358
        if (!empty($userList)) {
359
            foreach ($userList as $userId) {
360
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
361
362
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
363
                $em->persist($resourceLink);
364
            }
365
        }
366
    }
367
368
    /**
369
     * @param Course           $course
370
     * @param Tool             $tool
371
     * @param AbstractResource $parent
372
     *
373
     * @return ResourceLink
374
     */
375
    public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
376
    {
377
        $query = $this->getEntityManager()->createQueryBuilder()
378
            ->select('resource')
379
            ->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
380
            ->innerJoin('node.links', 'links')
381
            ->innerJoin(
382
                $this->getClassName(),
383
                'resource',
384
                Join::WITH,
385
                'resource.course = links.course AND resource.resourceNode = node.id'
386
            )
387
            ->where('node.tool = :tool')
388
            ->andWhere('links.course = :course')
389
            //->where('link.cId = ?', $course->getId())
390
            //->where('node.cId = 0')
391
            //->orderBy('node');
392
            ->setParameters(
393
                [
394
                    'tool' => $tool,
395
                    'course' => $course,
396
                ]
397
            );
398
399
        if ($parent !== null) {
400
            $query->andWhere('node.parent = :parentId');
401
            $query->setParameter('parentId', $parent->getResourceNode()->getId());
402
        } else {
403
            $query->andWhere('node.parent IS NULL');
404
        }
405
406
        $query = $query->getQuery();
407
408
        /*$query = $this->getEntityManager()->createQueryBuilder()
409
            ->select('notebook')
410
            ->from('ChamiloNotebookBundle:CNotebook', 'notebook')
411
            ->innerJoin('notebook.resourceNodes', 'node')
412
            //->innerJoin('node.links', 'link')
413
            ->where('node.tool = :tool')
414
            //->where('link.cId = ?', $course->getId())
415
            //->where('node.cId = 0')
416
            //->orderBy('node');
417
            ->setParameters(array(
418
                    'tool'=> 'notebook'
419
                )
420
            )
421
            ->getQuery()
422
        ;*/
423
424
        return $query->getResult();
425
    }
426
427
    /**
428
     * @param string $tool
429
     *
430
     * @return Tool|null
431
     */
432
    public function getTool($tool)
433
    {
434
        return $this
435
            ->getEntityManager()
436
            ->getRepository('ChamiloCoreBundle:Tool')
437
            ->findOneBy(['name' => $tool]);
438
    }
439
}
440