Passed
Push — master ( fc8817...f91339 )
by Angel Fernando Quiroz
10:10 queued 10s
created

TopLinkRelToolRepository::updateTools()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 24
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\TopLinks\Entity\Repository;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CourseBundle\Entity\CTool;
9
use Chamilo\PluginBundle\TopLinks\Entity\TopLink;
10
use Chamilo\PluginBundle\TopLinks\Entity\TopLinkRelTool;
11
use Doctrine\ORM\EntityRepository;
12
use Doctrine\ORM\Query\Expr\Join;
13
14
/**
15
 * Class TopLinkRelToolRepository.
16
 */
17
class TopLinkRelToolRepository extends EntityRepository
18
{
19
    public function findInCourse(Course $course)
20
    {
21
        $qb = $this->createQueryBuilder('tlrt');
22
23
        return $qb
24
            ->innerJoin('tlrt.tool', 'tool', Join::WITH)
25
            ->where($qb->expr()->eq('tool.cId', ':course'))
26
            ->setParameter('course', $course)
27
            ->getQuery()
28
            ->getResult();
29
    }
30
31
    public function updateTools(TopLink $link)
32
    {
33
        $subQb = $this->createQueryBuilder('tlrt');
34
        $subQb
35
            ->select('tool.iid')
36
            ->innerJoin('tlrt.tool', 'tool', Join::WITH)
37
            ->where($subQb->expr()->eq('tlrt.link', ':link'))
38
            ->setParameter('link', $link);
39
40
        $linkTools = $subQb->getQuery()->getArrayResult();
41
42
        $qb = $this->_em->createQueryBuilder();
43
        $qb
44
            ->update(CTool::class, 'tool')
45
            ->set('tool.name', ':link_name')
46
            ->set('tool.target', ':link_target')
47
            ->where(
48
                $qb->expr()->in('tool.iid', ':tools')
49
            )
50
            ->setParameter('link_name', $link->getTitle())
51
            ->setParameter('link_target', $link->getTarget())
52
            ->setParameter('tools', array_column($linkTools, 'iid'))
53
            ->getQuery()
54
            ->execute();
55
    }
56
57
    public function getMissingCoursesForTool(int $linkId)
58
    {
59
        $qb = $this->_em->createQueryBuilder();
60
61
        $subQb = $this->_em->createQueryBuilder();
62
        $subQb
63
            ->select('t.cId')
64
            ->from(CTool::class, 't')
65
            ->innerJoin(TopLinkRelTool::class, 'tlrt', Join::WITH, $subQb->expr()->eq('t.iid', 'tlrt.tool'))
66
            ->where($subQb->expr()->eq('tlrt.link', ':link_id'));
67
68
        return $qb
69
            ->select('c')
70
            ->from(Course::class, 'c')
71
            ->where($qb->expr()->notIn('c.id', $subQb->getDQL()))
72
            ->setParameter('link_id', $linkId)
73
            ->getQuery()
74
            ->getResult();
75
    }
76
}
77