Passed
Push — master ( 6d3a7a...482b77 )
by Julito
12:08 queued 02:23
created

SequenceRepository::getRequirements()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 6
nop 2
dl 0
loc 48
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Repository;
6
7
use Chamilo\CoreBundle\Entity\Sequence;
8
use Chamilo\CoreBundle\Entity\SequenceResource;
9
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10
use Doctrine\Common\Persistence\ManagerRegistry;
11
use Doctrine\ORM\Query\Expr\Join;
12
13
/**
14
 * Class SequenceRepository
15
 * The functions inside this class should return an instance of QueryBuilder.
16
 */
17
class SequenceRepository extends ServiceEntityRepository
18
{
19
    /**
20
     * CourseCategoryRepository constructor.
21
     */
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, Sequence::class);
25
    }
26
27
    /**
28
     * @param string $type
29
     *
30
     * @return array
31
     */
32
    public static function getItems($type)
33
    {
34
        $list = [];
35
36
        switch ($type) {
37
            case SequenceResource::COURSE_TYPE:
38
                $courseListFromDatabase = \CourseManager::get_course_list();
39
40
                if (!empty($courseListFromDatabase)) {
41
                    foreach ($courseListFromDatabase as $item) {
42
                        $list[$item['id']] = $item['title'].' ('.$item['id'].')';
43
                    }
44
                }
45
46
                break;
47
            case SequenceResource::SESSION_TYPE:
48
                $sessionList = \SessionManager::get_sessions_list();
49
                if (!empty($sessionList)) {
50
                    foreach ($sessionList as $sessionItem) {
51
                        $list[$sessionItem['id']] = $sessionItem['name'].' ('.$sessionItem['id'].')';
52
                    }
53
                }
54
55
                break;
56
        }
57
58
        return $list;
59
    }
60
61
    public function getItem($itemId, $type)
62
    {
63
        $resource = null;
64
        switch ($type) {
65
            case SequenceResource::COURSE_TYPE:
66
                $repo = $this->getEntityManager()->getRepository('ChamiloCoreBundle:Course');
67
68
                break;
69
            case SequenceResource::SESSION_TYPE:
70
                $repo = $this->getEntityManager()->getRepository('ChamiloCoreBundle:Session');
71
72
                break;
73
        }
74
75
        if ($repo) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $repo does not seem to be defined for all execution paths leading up to this point.
Loading history...
76
            $resource = $repo->find($itemId);
77
        }
78
79
        return $resource;
80
    }
81
82
    /**
83
     * @param int $id
84
     */
85
    public function removeSequence($id)
86
    {
87
        $sequence = $this->find($id);
88
        $em = $this->getEntityManager();
89
        $em
90
            ->createQuery('DELETE FROM ChamiloCoreBundle:SequenceResource sr WHERE sr.sequence = :seq')
91
            ->execute(['seq' => $sequence]);
92
93
        $em->remove($sequence);
94
        $em->flush();
95
    }
96
97
    /**
98
     * @param string $type
99
     *
100
     * @return array
101
     */
102
    public function findAllToSelect($type)
103
    {
104
        $qb = $this->createQueryBuilder('r');
105
        $qb
106
            ->leftJoin('ChamiloCoreBundle:SequenceResource', 'sr', Join::WITH, 'sr.sequence = r');
107
108
        $qb
109
            ->andWhere(
110
                $qb->expr()->orX(
111
                    $qb->expr()->isNull('r.graph'),
112
                    $qb->expr()->eq('sr.type', $type)
113
                )
114
            )
115
            ->orderBy('r.name');
116
117
        return $qb->getQuery()->getResult();
118
    }
119
}
120