Passed
Pull Request — master (#5831)
by
unknown
07:39
created

CLpRepository::addNotDeletedQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
14
use Chamilo\CourseBundle\Entity\CForum;
15
use Chamilo\CourseBundle\Entity\CLp;
16
use Chamilo\CourseBundle\Entity\CLpItem;
17
use Doctrine\ORM\QueryBuilder;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Exception;
20
use Symfony\Component\Routing\RouterInterface;
21
22
final class CLpRepository extends ResourceRepository implements ResourceWithLinkInterface
23
{
24
    public function __construct(ManagerRegistry $registry)
25
    {
26
        parent::__construct($registry, CLp::class);
27
    }
28
29
    public function createLp(CLp $lp): void
30
    {
31
        if (null !== $lp->getResourceNode()) {
32
            throw new Exception('Lp should not have a resource node during creation');
33
        }
34
35
        $lpItem = (new CLpItem())
36
            ->setTitle('root')
37
            ->setPath('root')
38
            ->setLp($lp)
39
            ->setItemType('root')
40
        ;
41
        $lp->getItems()->add($lpItem);
42
        $this->create($lp);
43
    }
44
45
    public function findForumByCourse(CLp $lp, Course $course, ?Session $session = null): ?CForum
46
    {
47
        $forums = $lp->getForums();
48
        $result = null;
49
        foreach ($forums as $forum) {
50
            $links = $forum->getResourceNode()->getResourceLinks();
51
            foreach ($links as $link) {
52
                if ($link->getCourse() === $course && $link->getSession() === $session) {
53
                    $result = $forum;
54
55
                    break 2;
56
                }
57
            }
58
        }
59
60
        return $result;
61
    }
62
63
    public function findAllByCourse(
64
        Course $course,
65
        ?Session $session = null,
66
        ?string $title = null,
67
        ?int $active = null,
68
        bool $onlyPublished = true,
69
        ?int $categoryId = null
70
    ): QueryBuilder {
71
        $qb = $this->getResourcesByCourse($course, $session);
72
73
        /*if ($onlyPublished) {
74
            $this->addDateFilterQueryBuilder(new DateTime(), $qb);
75
        }*/
76
        // $this->addCategoryQueryBuilder($categoryId, $qb);
77
        // $this->addActiveQueryBuilder($active, $qb);
78
        // $this->addNotDeletedQueryBuilder($qb);
79
        $this->addTitleQueryBuilder($title, $qb);
80
81
        return $qb;
82
    }
83
84
    public function getLink(ResourceInterface $resource, RouterInterface $router, array $extraParams = []): string
85
    {
86
        $params = [
87
            'lp_id' => $resource->getResourceIdentifier(),
88
            'name' => 'lp/lp_controller.php',
89
            'action' => 'view',
90
        ];
91
        if (!empty($extraParams)) {
92
            $params = array_merge($params, $extraParams);
93
        }
94
95
        return $router->generate('legacy_main', $params);
96
    }
97
98
    public function findAutoLaunchableLPByCourseAndSession(Course $course, ?Session $session = null): ?int
99
    {
100
        $qb = $this->getResourcesByCourse($course, $session)
101
            ->select('resource.iid')
102
            ->andWhere('resource.autolaunch = 1')
103
        ;
104
105
        $qb->setMaxResults(1);
106
107
        $result = $qb->getQuery()->getOneOrNullResult();
108
109
        return $result ? $result['iid'] : null;
110
    }
111
112
    protected function addNotDeletedQueryBuilder(?QueryBuilder $qb = null): QueryBuilder
113
    {
114
        $qb = $this->getOrCreateQueryBuilder($qb);
115
116
        $qb->andWhere('resource.active <> -1');
117
118
        return $qb;
119
    }
120
121
    public function getLpSessionId(int $lpId): ?int
122
    {
123
        $lp = $this->find($lpId);
124
125
        if (!$lp) {
126
            return null;
127
        }
128
129
        $resourceNode = $lp->getResourceNode();
130
        if ($resourceNode) {
131
            $link = $resourceNode->getResourceLinks()->first();
132
133
            if ($link && $link->getSession()) {
134
135
                return (int) $link->getSession()->getId();
136
            }
137
        }
138
139
        return null;
140
    }
141
}
142