Passed
Pull Request — master (#6229)
by
unknown
09:20
created

CSurveyInvitationRepository::hasUserAnswered()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 17
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 27
rs 9.7
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\CSurvey;
14
use Chamilo\CourseBundle\Entity\CSurveyInvitation;
15
use Datetime;
16
use Doctrine\Common\Collections\Criteria;
17
use Doctrine\Persistence\ManagerRegistry;
18
19
final class CSurveyInvitationRepository extends ResourceRepository
20
{
21
    public function __construct(ManagerRegistry $registry)
22
    {
23
        parent::__construct($registry, CSurveyInvitation::class);
24
    }
25
26
    /**
27
     * @return CSurveyInvitation[]
28
     */
29
    public function getUserPendingInvitations(User $user)
30
    {
31
        $qb = $this->createQueryBuilder('i');
32
        $qb
33
            ->select('i')
34
            ->innerJoin('i.user', 'u')
35
            ->innerJoin('i.survey', 's')
36
            ->andWhere('i.user = :u')
37
            ->andWhere('s.availFrom <= :now AND s.availTill >= :now')
38
            ->andWhere('s.answered = 0')
39
            ->setParameters([
40
                'now' => new Datetime(),
41
                'u' => $user,
42
            ])
43
            ->orderBy('s.availTill', Criteria::ASC)
44
        ;
45
46
        return $qb->getQuery()->getResult();
47
    }
48
49
    public function getAnsweredInvitations(CSurvey $survey, Course $course, ?Session $session = null): array
50
    {
51
        $qb = $this->createQueryBuilder('i')
52
            ->select('i')
53
            ->innerJoin('i.user', 'u')
54
            ->innerJoin('i.survey', 's')
55
            ->where('s = :survey')
56
            ->andWhere('i.course = :course')
57
            ->andWhere('i.answered = 1')
58
            ->setParameter('survey', $survey)
59
            ->setParameter('course', $course);
60
61
        if ($session) {
62
            $qb->andWhere('i.session = :session')
63
                ->setParameter('session', $session);
64
        } else {
65
            $qb->andWhere('i.session IS NULL');
66
        }
67
68
        return $qb->getQuery()->getResult();
69
    }
70
71
    public function hasUserAnswered(
72
        CSurvey $survey,
73
        Course $course,
74
        User $user,
75
        ?Session $session = null
76
    ): bool {
77
        $qb = $this->createQueryBuilder('i')
78
            ->select('i')
79
            ->innerJoin('i.survey', 's')
80
            ->where('s = :survey')
81
            ->andWhere('i.user = :user')
82
            ->andWhere('i.course = :course')
83
            ->andWhere('i.answered = 1')
84
            ->setParameters([
85
                'survey' => $survey,
86
                'user' => $user,
87
                'course' => $course,
88
            ]);
89
90
        if ($session) {
91
            $qb->andWhere('i.session = :session')
92
                ->setParameter('session', $session);
93
        } else {
94
            $qb->andWhere('i.session IS NULL');
95
        }
96
97
        return (bool) $qb->getQuery()->getOneOrNullResult();
98
    }
99
}
100