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

countUserPublications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 7
rs 10
c 1
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\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 getStudentAssignments(
29
        CStudentPublication $publication,
30
        Course $course,
31
        Session $session = null,
32
        CGroup $group = null
33
    ): QueryBuilder {
34
        $qb = $this->getResourcesByCourse($course, $session, $group);
35
36
        $qb->andWhere($qb->expr()->in('resource.active', [1, 0]));
37
        $qb
38
            ->andWhere('resource.publicationParent =:publicationParent')
39
            ->setParameter('publicationParent', $publication)
40
        ;
41
42
        return $qb;
43
    }
44
45
    public function countUserPublications(User $user, Course $course, Session $session = null, CGroup $group = null)
46
    {
47
        $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session);
48
49
        $qb->select('count(resource)');
50
51
        return $qb->getQuery()->getSingleScalarResult();
52
    }
53
54
    public function countCoursePublications(Course $course, Session $session = null, CGroup $group = null)
55
    {
56
        $qb = $this->getResourcesByCourse($course, $session, $group);
57
58
        $qb->select('count(resource)');
59
        $this->addNotDeletedPublicationQueryBuilder($qb);
60
61
        return $qb->getQuery()->getSingleScalarResult();
62
    }
63
64
    /**
65
     * Find all the works registered by a teacher.
66
     */
67
    public function findWorksByTeacher(User $user, Course $course, Session $session = null, int $groupId = 0): array
68
    {
69
        $qb = $this->createQueryBuilder('w');
70
71
        return $qb
72
            ->leftJoin(
73
                CStudentPublicationAssignment::class,
74
                'a',
75
                Join::WITH,
76
                'a.publicationId = w.iid AND a.cId = w.cId'
77
            )
78
            ->where(
79
                $qb->expr()->andX(
80
                    $qb->expr()->eq('w.cId', ':course'),
81
                    $qb->expr()->eq('w.session', ':session'),
82
                    $qb->expr()->in('w.active', [0, 1]),
83
                    $qb->expr()->eq('w.parentId', 0),
84
                    $qb->expr()->eq('w.postGroupId', ':group'),
85
                    $qb->expr()->eq('w.userId', ':user')
86
                )
87
            )
88
89
            ->orderBy('w.sentDate', Criteria::ASC)
90
            ->setParameters([
91
                'course' => $course->getId(),
92
                'session' => $session,
93
                'group' => $groupId,
94
                'user' => $user->getId(),
95
            ])
96
            ->getQuery()
97
            ->getResult()
98
        ;
99
    }
100
101
    protected function addNotDeletedPublicationQueryBuilder(QueryBuilder $qb = null): QueryBuilder
102
    {
103
        $qb = $this->getOrCreateQueryBuilder($qb);
104
        $qb
105
            ->andWhere('resource.active <> 2')
106
        ;
107
108
        return $qb;
109
    }
110
}
111