Passed
Push — master ( ff47af...4fe9ef )
by Julito
08:21
created

CForumThreadRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneInCourse() 0 36 3
A findAllInCourseByForum() 0 49 5
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\CForumForum;
11
use Chamilo\CourseBundle\Entity\CForumThread;
12
use Chamilo\CourseBundle\Entity\CGroupInfo;
13
use Chamilo\CourseBundle\Entity\CItemProperty;
14
15
/**
16
 * Class CForumThreadRepository.
17
 */
18
class CForumThreadRepository extends ResourceRepository
19
{
20
    /**
21
     * @param bool $isAllowedToEdit
22
     *
23
     * @todo Remove api_get_session_condition
24
     */
25
    public function findAllInCourseByForum(
26
        $isAllowedToEdit,
27
        CForumForum $forum,
28
        Course $course,
29
        CGroupInfo $group = null,
30
        Session $session = null
31
    ): array {
32
        $conditionSession = api_get_session_condition(
33
            $session ? $session->getId() : 0,
34
            true,
35
            false,
36
            't.sessionId'
37
        );
38
        $conditionVisibility = $isAllowedToEdit ? 'ip.visibility != 2' : 'ip.visibility = 1';
39
        $conditionGroup = $group
40
            ? 'AND ip.group = '.$group->getIid()
41
            : '';
42
43
        $dql = "SELECT DISTINCT t, ip
44
            FROM ChamiloCourseBundle:CForumThread t
45
            INNER JOIN ChamiloCourseBundle:CItemProperty ip
46
                WITH (t.iid = ip.ref AND t.cId = ip.course AND ip.tool = :tool)
47
            WHERE
48
                ip.course = :course AND
49
                t.forum = :forum AND
50
                $conditionVisibility
51
                $conditionGroup
52
                $conditionSession
53
            ORDER BY t.threadSticky DESC, t.threadDate DESC";
54
55
        $result = $this
56
            ->_em
57
            ->createQuery($dql)
58
            ->setParameters(['forum' => $forum, 'course' => $course, 'tool' => TOOL_FORUM_THREAD])
59
            ->getResult();
60
61
        $forums = [];
62
63
        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...
64
            /** @var CForumThread $t */
65
            $t = $result[$i];
66
            /** @var CItemProperty $ip */
67
            $ip = $result[$i + 1];
68
            $t->setItemProperty($ip);
69
70
            $forums[] = $t;
71
        }
72
73
        return $forums;
74
    }
75
76
    /**
77
     * @param int $id
78
     *
79
     * @return CForumThread|null
80
     *
81
     * @todo Remove api_get_session_condition
82
     */
83
    public function findOneInCourse($id, Course $course, Session $session = null)
84
    {
85
        $conditionSession = api_get_session_condition(
86
            $session ? $session->getId() : 0,
87
            true,
88
            false,
89
            't.sessionId'
90
        );
91
92
        $dql = "SELECT t, ip
93
            FROM ChamiloCourseBundle:CForumThread AS t
94
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
95
                WITH (t.iid = ip.ref AND t.cId = ip.course)
96
            WHERE
97
                t.iid = :id AND
98
                ip.tool = :tool AND
99
                t.cId = :course
100
                $conditionSession";
101
102
        $result = $this
103
            ->_em
104
            ->createQuery($dql)
105
            ->setParameters(['id' => (int) $id, 'course' => $course, 'tool' => TOOL_FORUM_THREAD])
106
            ->getResult();
107
108
        if (empty($result)) {
109
            return null;
110
        }
111
112
        /** @var CForumThread $t */
113
        $t = $result[0];
114
        /** @var CItemProperty $ip */
115
        $ip = $result[1];
116
        $t->setItemProperty($ip);
117
118
        return $t;
119
    }
120
}
121