Completed
Push — master ( 079342...737906 )
by Julito
09:35
created

CForumForumRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\Session;
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\CourseBundle\Entity\CForumCategory;
11
use Chamilo\CourseBundle\Entity\CForumForum;
12
use Chamilo\CourseBundle\Entity\CItemProperty;
13
14
final class CForumForumRepository extends ResourceRepository
15
{
16
    /**
17
     * @param bool $isAllowedToEdit
18
     * @param bool $includeGroupsForums
19
     *
20
     * @todo Remove api_get_session_condition
21
     */
22
    public function findAllInCourseByCategory(
23
        $isAllowedToEdit,
24
        CForumCategory $category,
25
        Course $course,
26
        Session $session = null,
27
        $includeGroupsForums = true
28
    ): array {
29
        $conditionSession = api_get_session_condition(
30
            $session ? $session->getId() : 0,
31
            true,
32
            true,
33
            'f.sessionId'
34
        );
35
        $conditionVisibility = $isAllowedToEdit ? 'ip.visibility != 2' : 'ip.visibility = 1';
36
        $conditionGroups = $includeGroupsForums
37
            ? 'AND (f.forumOfGroup = 0 OR f.forumOfGroup IS NULL)'
38
            : '';
39
40
        $dql = "SELECT f, ip
41
            FROM ChamiloCourseBundle:CForumForum AS f
42
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
43
                WITH (f.iid = ip.ref AND f.cId = ip.course)
44
            WHERE
45
                f.forumCategory = :category AND
46
                ip.tool = :tool AND
47
                f.cId = :course
48
                $conditionSession AND
49
                $conditionVisibility
50
                $conditionGroups
51
            ORDER BY f.forumOrder ASC";
52
53
        $result = $this
54
            ->_em
55
            ->createQuery($dql)
56
            ->setParameters(['category' => $category, 'course' => $course, 'tool' => TOOL_FORUM])
57
            ->getResult();
58
59
        $forums = [];
60
61
        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...
62
            /** @var CForumForum $f */
63
            $f = $result[$i];
64
            /** @var CItemProperty $ip */
65
            $ip = $result[$i + 1];
66
            $f->setItemProperty($ip);
67
68
            $forums[] = $f;
69
        }
70
71
        return $forums;
72
    }
73
74
    /**
75
     * @param int $id
76
     *
77
     * @return CForumForum
78
     */
79
    public function findOneInCourse($id, Course $course)
80
    {
81
        $dql = 'SELECT f, ip
82
            FROM ChamiloCourseBundle:CForumForum AS f
83
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
84
                WITH (f.iid = ip.ref AND f.cId = ip.course)
85
            WHERE
86
                f.iid = :id AND
87
                ip.tool = :tool AND
88
                f.cId = :course AND
89
                ip.visibility != 2';
90
91
        $result = $this
92
            ->_em
93
            ->createQuery($dql)
94
            ->setParameters(['id' => (int) $id, 'course' => $course, 'tool' => TOOL_FORUM])
95
            ->getResult();
96
97
        if (empty($result)) {
98
            return null;
99
        }
100
101
        /** @var CForumForum $f */
102
        $f = $result[0];
103
        /** @var CItemProperty $ip */
104
        $ip = $result[1];
105
        $f->setItemProperty($ip);
106
107
        return $f;
108
    }
109
}
110