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

CForumPostRepository::__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\CourseBundle\Entity\CForumPost;
8
use Chamilo\CourseBundle\Entity\CForumThread;
9
use Chamilo\CourseBundle\Entity\CGroupInfo;
10
use Chamilo\UserBundle\Entity\User;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Common\Persistence\ManagerRegistry;
13
14
/**
15
 * Class CForumPostRepository.
16
 *
17
 * @package Chamilo\CourseBundle\Repository
18
 */
19
class CForumPostRepository extends ServiceEntityRepository
20
{
21
    /**
22
     * CForumPostRepository constructor.
23
     *
24
     * @param ManagerRegistry $registry
25
     */
26
    public function __construct(ManagerRegistry $registry)
27
    {
28
        parent::__construct($registry, CForumPost::class);
29
    }
30
31
    /**
32
     * @param bool            $onlyVisibles
33
     * @param bool            $isAllowedToEdit
34
     * @param CForumThread    $thread
35
     * @param Course          $course
36
     * @param User|null       $currentUser
37
     * @param CGroupInfo|null $group
38
     * @param string          $orderDirection
39
     *
40
     * @return array
41
     */
42
    public function findAllInCourseByThread(
43
        $onlyVisibles,
44
        $isAllowedToEdit,
45
        CForumThread $thread,
46
        Course $course,
47
        User $currentUser = null,
48
        CGroupInfo $group = null,
49
        $orderDirection = 'ASC'
50
    ): array {
51
        $conditionVisibility = $onlyVisibles ? 'p.visible = 1' : 'p.visible != 2';
52
        $conditionModetared = '';
53
        $filterModerated = true;
54
55
        if (
56
            (empty($group) && $isAllowedToEdit) ||
57
            (
58
                ($group ? $group->userIsTutor($currentUser) : false) ||
59
                !$onlyVisibles
60
            )
61
        ) {
62
            $filterModerated = false;
63
        }
64
65
        if ($filterModerated && $thread->getForum()->isModerated() && $onlyVisibles) {
66
            $userId = $currentUser ? $currentUser->getId() : 0;
67
68
            $conditionModetared = "AND p.status = 1 OR
69
                (p.status = ".CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
70
                (p.status = ".CForumPost::STATUS_REJECTED." AND p.poster = $userId) OR
71
                (p.status IS NULL AND p.posterId = $userId)";
72
        }
73
74
        $dql = "SELECT p
75
            FROM ChamiloCourseBundle:CForumPost p
76
            WHERE
77
                p.thread = :thread AND
78
                p.cId = :course AND
79
                $conditionVisibility
80
                $conditionModetared
81
            ORDER BY p.iid $orderDirection";
82
83
        $result = $this
84
            ->_em
85
            ->createQuery($dql)
86
            ->setParameters(['thread' => $thread, 'course' => $course])
87
            ->getResult();
88
89
        return $result;
90
    }
91
}
92