Completed
Push — master ( 38e61c...cd9a17 )
by Julito
14:03
created

ResourceRepository::addResourceNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
c 0
b 0
f 0
nop 3
dl 0
loc 28
rs 9.7666
nc 2
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
20
/**
21
 * Class ResourceRepository.
22
 *
23
 * @package Chamilo\CoreBundle\Entity
24
 */
25
class ResourceRepository
26
{
27
    /**
28
     * @var EntityManager
29
     */
30
    protected $entityManager;
31
32
    /**
33
     * @return EntityManager
34
     */
35
    public function getEntityManager()
36
    {
37
        return $this->entityManager;
38
    }
39
40
    /**
41
     * Creates a ResourceNode.
42
     *
43
     * @param AbstractResource $resource
44
     * @param User             $creator
45
     * @param AbstractResource $parent
46
     *
47
     * @return ResourceNode
48
     */
49
    public function addResourceNode(
50
        AbstractResource $resource,
51
        User $creator,
52
        AbstractResource $parent = null
53
    ): ResourceNode {
54
        $em = $this->getEntityManager();
55
        $resourceNode = new ResourceNode();
56
57
        //$tool = $this->getTool($resource->getToolName());
58
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
59
            ['name' => $resource->getToolName()]
60
        );
61
62
        $resourceNode
63
            ->setName($resource->getResourceName())
64
            ->setCreator($creator)
65
            ->setResourceType($resourceType);
66
67
        if ($parent !== null) {
68
            $resourceNode->setParent($parent->getResourceNode());
69
        }
70
71
        $resource->setResourceNode($resourceNode);
72
        $em->persist($resourceNode);
73
        $em->persist($resource);
74
        $em->flush();
75
76
        return $resourceNode;
77
    }
78
79
    /**
80
     * @param ResourceNode $resourceNode
81
     *
82
     * @return ResourceLink
83
     */
84
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
85
    {
86
        $resourceLink = new ResourceLink();
87
        $resourceLink
88
            ->setResourceNode($resourceNode)
89
            ->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

89
            ->/** @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...
90
91
        $this->getEntityManager()->persist($resourceLink);
92
        $this->getEntityManager()->flush();
93
94
        return $resourceLink;
95
    }
96
97
    /**
98
     * @param ResourceNode  $resourceNode
99
     * @param ResourceRight $right
100
     *
101
     * @return ResourceLink
102
     */
103
    public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
104
    {
105
        $resourceLink = new ResourceLink();
106
        $resourceLink
107
            ->setResourceNode($resourceNode)
108
            ->addResourceRight($right)
109
        ;
110
111
        $this->getEntityManager()->persist($resourceLink);
112
        $this->getEntityManager()->flush();
113
114
        return $resourceLink;
115
    }
116
117
    /**
118
     * @param ResourceNode $resourceNode
119
     * @param int          $visibility
120
     * @param Course       $course
121
     * @param Session      $session
122
     * @param CGroupInfo   $group
123
     *
124
     */
125
    public function addResourceToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
126
    {
127
        $visibility = (int) $visibility;
128
        if (empty($visibility)) {
129
            $visibility = ResourceLink::VISIBILITY_PUBLISHED;
130
        }
131
132
        $link = new ResourceLink();
133
        $link
134
            ->setCourse($course)
135
            ->setSession($session)
136
            ->setGroup($group)
137
            //->setUser($toUser)
138
            ->setResourceNode($resourceNode)
139
            ->setVisibility($visibility)
140
        ;
141
142
        $rights = [];
143
        switch ($visibility) {
144
            case ResourceLink::VISIBILITY_PENDING:
145
            case ResourceLink::VISIBILITY_DRAFT:
146
                $editorMask = ResourceNodeVoter::getEditorMask();
147
                $resourceRight = new ResourceRight();
148
                $resourceRight
149
                    ->setMask($editorMask)
150
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
151
                ;
152
                $rights[] = $resourceRight;
153
                break;
154
        }
155
156
        if (!empty($rights)) {
157
            foreach ($rights as $right) {
158
                $link->addResourceRight($right);
159
            }
160
        }
161
162
        $em = $this->getEntityManager();
163
        $em->persist($link);
164
        //$em->persist($document);
165
        $em->flush();
166
    }
167
168
    /**
169
     * @param ResourceNode  $resourceNode
170
     * @param Course        $course
171
     * @param ResourceRight $right
172
     *
173
     * @return ResourceLink
174
     */
175
    private function addResourceToCourse2(ResourceNode $resourceNode, Course $course, ResourceRight $right): ResourceLink
0 ignored issues
show
Unused Code introduced by
The method addResourceToCourse2() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
176
    {
177
        $resourceLink = new ResourceLink();
178
        $resourceLink
179
            ->setResourceNode($resourceNode)
180
            ->setCourse($course)
181
            ->addResourceRight($right);
182
        $this->getEntityManager()->persist($resourceLink);
183
        $this->getEntityManager()->flush();
184
185
        return $resourceLink;
186
    }
187
188
    /**
189
     * @param ResourceNode $resourceNode
190
     * @param User         $toUser
191
     *
192
     * @return ResourceLink
193
     */
194
    private function addResourceToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
0 ignored issues
show
Unused Code introduced by
The method addResourceToUser() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
195
    {
196
        $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
197
        $this->getEntityManager()->persist($resourceLink);
198
199
        return $resourceLink;
200
    }
201
    /**
202
     * @param ResourceNode $resourceNode
203
     * @param User         $toUser
204
     *
205
     * @return ResourceLink
206
     */
207
    private function addResourceNodeToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
208
    {
209
        $resourceLink = new ResourceLink();
210
        $resourceLink
211
            ->setResourceNode($resourceNode)
212
            ->setUser($toUser);
213
214
        return $resourceLink;
215
    }
216
217
    /**
218
     * @param ResourceNode  $resourceNode
219
     * @param Course        $course
220
     * @param Session       $session
221
     * @param ResourceRight $right
222
     *
223
     * @return ResourceLink
224
     */
225
    private function addResourceToSession(
0 ignored issues
show
Unused Code introduced by
The method addResourceToSession() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
226
        ResourceNode $resourceNode,
227
        Course $course,
228
        Session $session,
229
        ResourceRight $right
230
    ) {
231
        $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

231
        /** @scrutinizer ignore-call */ 
232
        $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...
232
            $resourceNode,
233
            $course,
234
            $right
235
        );
236
        $resourceLink->setSession($session);
237
        $this->getEntityManager()->persist($resourceLink);
238
239
        return $resourceLink;
240
    }
241
242
    /**
243
     * @param ResourceNode  $resourceNode
244
     * @param Course        $course
245
     * @param CGroupInfo    $group
246
     * @param ResourceRight $right
247
     *
248
     * @return ResourceLink
249
     */
250
    private function addResourceToCourseGroup(
0 ignored issues
show
Unused Code introduced by
The method addResourceToCourseGroup() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
251
        ResourceNode $resourceNode,
252
        Course $course,
253
        CGroupInfo $group,
254
        ResourceRight $right
255
    ) {
256
        $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

256
        /** @scrutinizer ignore-call */ 
257
        $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...
257
            $resourceNode,
258
            $course,
259
            $right
260
        );
261
        $resourceLink->setGroup($group);
262
        $this->getEntityManager()->persist($resourceLink);
263
264
        return $resourceLink;
265
    }
266
267
    /**
268
     * @param ResourceNode  $resourceNode
269
     * @param Usergroup     $group
270
     * @param ResourceRight $right
271
     *
272
     * @return ResourceLink
273
     */
274
    private function addResourceToGroup(
0 ignored issues
show
Unused Code introduced by
The method addResourceToGroup() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
275
        ResourceNode $resourceNode,
276
        Usergroup $group,
277
        ResourceRight $right
278
    ) {
279
        $resourceLink = new ResourceLink();
280
        $resourceLink
281
            ->setResourceNode($resourceNode)
282
            ->setUserGroup($group)
283
            ->addResourceRight($right);
284
285
        return $resourceLink;
286
    }
287
288
    /**
289
     * @param ResourceNode $resourceNode
290
     * @param array        $userList     User id list
291
     */
292
    private function addResourceToUserList(ResourceNode $resourceNode, array $userList)
0 ignored issues
show
Unused Code introduced by
The method addResourceToUserList() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
293
    {
294
        $em = $this->getEntityManager();
295
296
        if (!empty($userList)) {
297
            foreach ($userList as $userId) {
298
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
299
300
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
301
                $em->persist($resourceLink);
302
            }
303
        }
304
    }
305
306
    /**
307
     * @param Course           $course
308
     * @param Tool             $tool
309
     * @param AbstractResource $parent
310
     *
311
     * @return ResourceLink
312
     */
313
    public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
314
    {
315
        $query = $this->getEntityManager()->createQueryBuilder()
316
            ->select('resource')
317
            ->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
318
            ->innerJoin('node.links', 'links')
319
            ->innerJoin(
320
                $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

320
                $this->/** @scrutinizer ignore-call */ 
321
                       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...
321
                'resource',
322
                Join::WITH,
323
                'resource.course = links.course AND resource.resourceNode = node.id'
324
            )
325
            ->where('node.tool = :tool')
326
            ->andWhere('links.course = :course')
327
            //->where('link.cId = ?', $course->getId())
328
            //->where('node.cId = 0')
329
            //->orderBy('node');
330
            ->setParameters(
331
                [
332
                    'tool' => $tool,
333
                    'course' => $course,
334
                ]
335
            );
336
337
        if ($parent !== null) {
338
            $query->andWhere('node.parent = :parentId');
339
            $query->setParameter('parentId', $parent->getResourceNode()->getId());
340
        } else {
341
            $query->andWhere('node.parent IS NULL');
342
        }
343
344
        $query = $query->getQuery();
345
346
        /*$query = $this->getEntityManager()->createQueryBuilder()
347
            ->select('notebook')
348
            ->from('ChamiloNotebookBundle:CNotebook', 'notebook')
349
            ->innerJoin('notebook.resourceNodes', 'node')
350
            //->innerJoin('node.links', 'link')
351
            ->where('node.tool = :tool')
352
            //->where('link.cId = ?', $course->getId())
353
            //->where('node.cId = 0')
354
            //->orderBy('node');
355
            ->setParameters(array(
356
                    'tool'=> 'notebook'
357
                )
358
            )
359
            ->getQuery()
360
        ;*/
361
362
        return $query->getResult();
363
    }
364
365
    /**
366
     * @param string $tool
367
     *
368
     * @return Tool|null
369
     */
370
    public function getTool($tool)
371
    {
372
        return $this
373
            ->getEntityManager()
374
            ->getRepository('ChamiloCoreBundle:Tool')
375
            ->findOneBy(['name' => $tool]);
376
    }
377
}
378