Passed
Push — master ( c87549...c35fdc )
by Julito
09:10
created

CNotebookRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 66
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B findByUser() 0 56 6
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Repository;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Repository\ResourceRepository;
11
12
/**
13
 * Class CNotebookRepository.
14
 */
15
class CNotebookRepository extends ResourceRepository
16
{
17
    /**
18
     * Get the user notebooks in a course.
19
     *
20
     * @param string $orderField
21
     * @param string $orderDirection
22
     *
23
     * @return array
24
     */
25
    public function findByUser(
26
        User $user,
27
        Course $course,
28
        Session $session = null,
29
        $orderField = 'creation_date',
30
        $orderDirection = 'DESC'
31
    ) {
32
        switch ($orderField) {
33
            case 'creation_date':
34
                $orderField = 'N.creationDate';
35
36
                break;
37
            case 'update_date':
38
                $orderField = 'N.updateDate';
39
40
                break;
41
            case 'title':
42
                $orderField = 'N.title';
43
44
                break;
45
        }
46
47
        $qb = $this->createQueryBuilder('N');
48
        $qb
49
            ->where(
50
                $qb->expr()->andX(
51
                    $qb->expr()->eq('N.userId', $user->getId()),
52
                    $qb->expr()->eq('N.cId', $course->getId())
53
                )
54
            );
55
56
        if ($session) {
57
            $qb->andWhere(
58
                $qb->expr()->eq('N.sessionId', $session->getId())
59
            );
60
        } else {
61
            $qb->andWhere(
62
                $qb->expr()->orX(
63
                    $qb->expr()->eq('N.sessionId', 0),
64
                    $qb->expr()->isNull('N.sessionId')
65
                )
66
            );
67
        }
68
69
        if ('N.updateDate' === $orderField) {
70
            $qb->andWhere(
71
                $qb->expr()->orX(
72
                    $qb->expr()->neq('N.updateDate', ''),
73
                    $qb->expr()->isNotNull('N.updateDate')
74
                )
75
            );
76
        }
77
78
        $qb->orderBy($orderField, $orderDirection);
79
80
        return $qb->getQuery()->getResult();
81
    }
82
}
83