Passed
Push — master ( cf75f9...66ca98 )
by Julito
09:35
created

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