Passed
Push — master ( 7b6b24...78c3b4 )
by Julito
24:27 queued 15:59
created

addNotDeletedPublicationQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 2
b 1
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\Session;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\CourseBundle\Entity\CGroup;
14
use Chamilo\CourseBundle\Entity\CStudentPublication;
15
use Chamilo\CourseBundle\Entity\CStudentPublicationAssignment;
16
use Doctrine\Common\Collections\Criteria;
17
use Doctrine\ORM\Query\Expr\Join;
18
use Doctrine\ORM\QueryBuilder;
19
use Doctrine\Persistence\ManagerRegistry;
20
21
final class CStudentPublicationRepository extends ResourceRepository
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, CStudentPublication::class);
26
    }
27
28
    public function findAllByCourse(
29
        Course $course,
30
        Session $session = null,
31
        ?string $title = null,
32
        ?int $active = null,
33
        ?string $fileType = null
34
    ): QueryBuilder {
35
        $qb = $this->getResourcesByCourse($course, $session);
36
37
        $this->addTitleQueryBuilder($title, $qb);
38
        $this->addActiveQueryBuilder($active, $qb);
39
        $this->addFileTypeQueryBuilder($fileType, $qb);
40
41
        return $qb;
42
    }
43
44
    public function getStudentAssignments(
45
        CStudentPublication $publication,
46
        Course $course,
47
        ?Session $session = null,
48
        ?CGroup $group = null,
49
        ?User $user = null
50
    ): QueryBuilder {
51
        $qb = $this->getResourcesByCourse($course, $session, $group);
52
53
        $this->addNotDeletedPublicationQueryBuilder($qb);
54
        $qb
55
            ->andWhere('resource.publicationParent =:publicationParent')
56
            ->setParameter('publicationParent', $publication)
57
        ;
58
59
        return $qb;
60
    }
61
62
    public function getStudentPublicationByUser(User $user, Course $course, Session $session = null)
63
    {
64
        $qb = $this->findAllByCourse($course, $session);
65
        /** @var CStudentPublication[] $works */
66
        $works = $qb->getQuery()->getResult();
67
        $list = [];
68
        foreach ($works as $work) {
69
            $qb = $this->getStudentAssignments($work, $course, $session, null, $user);
70
            $results = $qb->getQuery()->getResult();
71
            $list[$work->getIid()]['work'] = $work;
72
            $list[$work->getIid()]['results'] = $results;
73
        }
74
75
        return $list;
76
    }
77
78
    public function countUserPublications(User $user, Course $course, Session $session = null, CGroup $group = null): int
79
    {
80
        $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session);
81
82
        return $this->getCount($qb);
83
    }
84
85
    public function countCoursePublications(Course $course, Session $session = null, CGroup $group = null): int
86
    {
87
        $qb = $this->getResourcesByCourse($course, $session, $group);
88
89
        $this->addNotDeletedPublicationQueryBuilder($qb);
90
91
        return $this->getCount($qb);
92
    }
93
94
    /**
95
     * Find all the works registered by a teacher.
96
     */
97
    public function findWorksByTeacher(User $user, Course $course, Session $session = null, int $groupId = 0): array
98
    {
99
        $qb = $this->createQueryBuilder('w');
100
101
        return $qb
102
            ->leftJoin(
103
                CStudentPublicationAssignment::class,
104
                'a',
105
                Join::WITH,
106
                'a.publicationId = w.iid AND a.cId = w.cId'
107
            )
108
            ->where(
109
                $qb->expr()->andX(
110
                    $qb->expr()->eq('w.cId', ':course'),
111
                    $qb->expr()->eq('w.session', ':session'),
112
                    $qb->expr()->in('w.active', [0, 1]),
113
                    $qb->expr()->eq('w.parentId', 0),
114
                    $qb->expr()->eq('w.postGroupId', ':group'),
115
                    $qb->expr()->eq('w.userId', ':user')
116
                )
117
            )
118
119
            ->orderBy('w.sentDate', Criteria::ASC)
120
            ->setParameters([
121
                'course' => $course->getId(),
122
                'session' => $session,
123
                'group' => $groupId,
124
                'user' => $user->getId(),
125
            ])
126
            ->getQuery()
127
            ->getResult()
128
        ;
129
    }
130
131
    private function addActiveQueryBuilder(?int $active = null, QueryBuilder $qb = null): QueryBuilder
132
    {
133
        $qb = $this->getOrCreateQueryBuilder($qb);
134
135
        if (null !== $active) {
136
            $qb
137
                ->andWhere('resource.active = :active')
138
                ->setParameter('active', $active)
139
            ;
140
        }
141
142
        return $qb;
143
    }
144
145
    private function addNotDeletedPublicationQueryBuilder(QueryBuilder $qb = null): QueryBuilder
146
    {
147
        $qb = $this->getOrCreateQueryBuilder($qb);
148
        $qb
149
            ->andWhere('resource.active <> 2')
150
        ;
151
152
        return $qb;
153
    }
154
155
    private function addFileTypeQueryBuilder(?string $fileType, QueryBuilder $qb = null): QueryBuilder
156
    {
157
        $qb = $this->getOrCreateQueryBuilder($qb);
158
        if (null === $fileType) {
159
            return $qb;
160
        }
161
162
        $qb
163
            ->andWhere('resource.filetype = :filetype')
164
            ->setParameter('filetype', $fileType)
165
        ;
166
167
        return $qb;
168
    }
169
}
170