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

CNotebookRepository::__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\CoreBundle\Entity\Session;
8
use Chamilo\CourseBundle\Entity\CNotebook;
9
use Chamilo\UserBundle\Entity\User;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Common\Persistence\ManagerRegistry;
12
13
/**
14
 * Class CNotebookRepository.
15
 *
16
 * @package Chamilo\CourseBundle\Repository
17
 */
18
class CNotebookRepository extends ServiceEntityRepository
19
{
20
    /**
21
     * CNotebookRepository constructor.
22
     *
23
     * @param ManagerRegistry $registry
24
     */
25
    public function __construct(ManagerRegistry $registry)
26
    {
27
        parent::__construct($registry, CNotebook::class);
28
    }
29
30
    /**
31
     * Get the user notebooks in a course.
32
     *
33
     * @param User         $user
34
     * @param Course       $course
35
     * @param Session|null $session
36
     * @param string       $orderField
37
     * @param string       $orderDirection
38
     *
39
     * @return array
40
     */
41
    public function findByUser(
42
        User $user,
43
        Course $course,
44
        Session $session = null,
45
        $orderField = 'creation_date',
46
        $orderDirection = 'DESC'
47
    ) {
48
        switch ($orderField) {
49
            case 'creation_date':
50
                $orderField = 'N.creationDate';
51
                break;
52
            case 'update_date':
53
                $orderField = 'N.updateDate';
54
                break;
55
            case 'title':
56
                $orderField = 'N.title';
57
                break;
58
        }
59
60
        $qb = $this->createQueryBuilder('N');
61
        $qb
62
            ->where(
63
                $qb->expr()->andX(
64
                    $qb->expr()->eq('N.userId', $user->getId()),
65
                    $qb->expr()->eq('N.cId', $course->getId())
66
                )
67
            );
68
69
        if ($session) {
70
            $qb->andWhere(
71
                $qb->expr()->eq('N.sessionId', $session->getId())
72
            );
73
        } else {
74
            $qb->andWhere(
75
                $qb->expr()->orX(
76
                    $qb->expr()->eq('N.sessionId', 0),
77
                    $qb->expr()->isNull('N.sessionId')
78
                )
79
            );
80
        }
81
82
        if ($orderField === 'N.updateDate') {
83
            $qb->andWhere(
84
                $qb->expr()->orX(
85
                    $qb->expr()->neq('N.updateDate', ''),
86
                    $qb->expr()->isNotNull('N.updateDate')
87
                )
88
            );
89
        }
90
91
        $qb->orderBy($orderField, $orderDirection);
92
93
        return $qb->getQuery()->getResult();
94
    }
95
}
96