Passed
Push — master ( 2d734c...ff47af )
by Julito
10:08
created

CForumCategoryRepository::findAllInCourse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 3
dl 0
loc 41
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Repository;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Repository\ResourceRepository;
11
use Chamilo\CourseBundle\Entity\CForumCategory;
12
use Chamilo\CourseBundle\Entity\CGroupInfo;
13
use Chamilo\CourseBundle\Entity\CItemProperty;
14
use Chamilo\UserBundle\Entity\User;
15
16
class CForumCategoryRepository extends ResourceRepository
17
{
18
    public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroupInfo $group = null)
19
    {
20
        return $this->getResourcesByCourse($course, $session, $group, $parentNode);
0 ignored issues
show
Bug introduced by
It seems like $course can also be of type null; however, parameter $course of Chamilo\CoreBundle\Repos...:getResourcesByCourse() does only seem to accept Chamilo\CoreBundle\Entity\Course, maybe add an additional type check? ( Ignorable by Annotation )

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

20
        return $this->getResourcesByCourse(/** @scrutinizer ignore-type */ $course, $session, $group, $parentNode);
Loading history...
21
    }
22
23
    /**
24
     * @param bool $isAllowedToEdit
25
     *
26
     * @todo Remove api_get_session_condition
27
     */
28
    public function findAllInCourse($isAllowedToEdit, Course $course, Session $session = null): array
29
    {
30
        $conditionSession = api_get_session_condition(
31
            $session ? $session->getId() : 0,
32
            true,
33
            true,
34
            'fcat.sessionId'
35
        );
36
        $conditionVisibility = $isAllowedToEdit ? 'ip.visibility != 2' : 'ip.visibility = 1';
37
38
        $dql = "SELECT ip, fcat
39
            FROM ChamiloCourseBundle:CItemProperty AS ip
40
            INNER JOIN ChamiloCourseBundle:CForumCategory fcat
41
                WITH (fcat.catId = ip.ref AND ip.course = fcat.cId)
42
            WHERE
43
                ip.tool = :tool AND
44
                ip.course = :course
45
                $conditionSession AND
46
                $conditionVisibility
47
                ORDER BY fcat.catOrder ASC";
48
49
        $result = $this
50
            ->_em
51
            ->createQuery($dql)
52
            ->setParameters(['course' => $course, 'tool' => TOOL_FORUM_CATEGORY])
53
            ->getResult();
54
55
        $categories = [];
56
57
        for ($i = 0; $i < count($result); $i += 2) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
58
            /** @var CItemProperty $ip */
59
            $ip = $result[$i];
60
            /** @var CForumCategory $fc */
61
            $fc = $result[$i + 1];
62
63
            $fc->setItemProperty($ip);
64
65
            $categories[] = $fc;
66
        }
67
68
        return $categories;
69
    }
70
71
    /**
72
     * @param int $id
73
     *
74
     * @return CForumCategory|null
75
     *
76
     * @todo Remove api_get_session_condition
77
     */
78
    public function findOneInCourse($id, Course $course, Session $session)
79
    {
80
        $conditionSession = api_get_session_condition(
81
            $session ? $session->getId() : 0,
0 ignored issues
show
introduced by
$session is of type Chamilo\CoreBundle\Entity\Session, thus it always evaluated to true.
Loading history...
82
            true,
83
            true,
84
            'fcat.sessionId'
85
        );
86
87
        $dql = "SELECT ip, fcat
88
            FROM ChamiloCourseBundle:CItemProperty AS ip
89
            INNER JOIN ChamiloCourseBundle:CForumCategory fcat
90
                WITH (fcat.catId = ip.ref AND ip.course = fcat.cId)
91
            WHERE
92
                ip.tool = :tool AND
93
                ip.course = :course
94
                fcat.iid = :id
95
                $conditionSession AND
96
                ORDER BY fcat.catOrder ASC";
97
98
        $result = $this
99
            ->_em
100
            ->createQuery($dql)
101
            ->setParameters(['tool' => TOOL_FORUM_CATEGORY, 'course' => $course, 'id' => (int) $id])
102
            ->getResult();
103
104
        if (empty($result)) {
105
            return null;
106
        }
107
108
        /** @var CItemProperty $ip */
109
        $ip = $result[0];
110
        /** @var CForumCategory $fc */
111
        $fc = $result[1];
112
113
        $fc->setItemProperty($ip);
114
115
        return $fc;
116
    }
117
}
118