Passed
Push — 1.11.x ( c309d7...7d3fca )
by Julito
11:19
created

SequenceRepository::removeSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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