Passed
Push — master ( 8f07b0...3fcb32 )
by Julito
15:06
created

CForumPostRepository::countUserForumPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\User;
13
use Chamilo\CoreBundle\Repository\ResourceRepository;
14
use Chamilo\CourseBundle\Entity\CForumPost;
15
use Chamilo\CourseBundle\Entity\CForumThread;
16
use Chamilo\CourseBundle\Entity\CGroup;
17
use Doctrine\Persistence\ManagerRegistry;
18
19
class CForumPostRepository extends ResourceRepository
20
{
21
    public function __construct(ManagerRegistry $registry)
22
    {
23
        parent::__construct($registry, CForumPost::class);
24
    }
25
26
    public function countUserForumPosts(User $user, Course $course, Session $session = null)
27
    {
28
        $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session);
29
30
        $qb->select('count(resource)');
31
32
        return $qb->getQuery()->getSingleScalarResult();
33
    }
34
35
    public function countCourseForumPosts(Course $course, Session $session = null)
36
    {
37
        $qb = $this->getResourcesByCourse($course, $session);
38
39
        $qb->select('count(resource)');
40
41
        return $qb->getQuery()->getSingleScalarResult();
42
    }
43
44
    /*public function findAllInCourseByThread(
45
        bool $onlyVisible,
46
        bool $isAllowedToEdit,
47
        CForumThread $thread,
48
        Course $course,
49
        User $currentUser = null,
50
        CGroup $group = null,
51
        string $orderDirection = 'ASC'
52
    ): array {
53
        $conditionVisibility = $onlyVisible ? 'p.visible = 1' : 'p.visible != 2';
54
        $conditionModerated = '';
55
        $filterModerated = true;
56
57
        if (
58
            (empty($group) && $isAllowedToEdit) ||
59
            (
60
                (null !== $group ? $group->userIsTutor($currentUser) : false) ||
61
                !$onlyVisible
62
            )
63
        ) {
64
            $filterModerated = false;
65
        }
66
67
        if ($filterModerated && $onlyVisible && $thread->getForum()->isModerated()) {
68
            $userId = null !== $currentUser ? $currentUser->getId() : 0;
69
70
            $conditionModerated = 'AND p.status = 1 OR
71
                (p.status = '.CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = {$userId}) OR
72
                (p.status = ".CForumPost::STATUS_REJECTED." AND p.poster = {$userId}) OR
73
                (p.status IS NULL AND p.posterId = {$userId})";
74
        }
75
76
        $dql = "SELECT p
77
            FROM ChamiloCourseBundle:CForumPost p
78
            WHERE
79
                p.thread = :thread AND
80
                p.cId = :course AND
81
                {$conditionVisibility}
82
                {$conditionModerated}
83
            ORDER BY p.iid {$orderDirection}";
84
85
        return $this
86
            ->getEntityManager()
87
            ->createQuery($dql)
88
            ->setParameters([
89
                'thread' => $thread,
90
                'course' => $course,
91
            ])
92
            ->getResult()
93
        ;
94
    }*/
95
96
    public function delete(ResourceInterface $resource): void
97
    {
98
        /** @var CForumPost $resource */
99
        $attachments = $resource->getAttachments();
100
        if (!empty($attachments)) {
101
            foreach ($attachments as $attachment) {
102
                $this->getEntityManager()->remove($attachment);
103
            }
104
        }
105
        parent::delete($resource);
106
    }
107
}
108