Passed
Pull Request — master (#6037)
by Yannick
19:16 queued 11:05
created

removeByResourceInContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Entity\Course;
11
use Chamilo\CoreBundle\Entity\ResourceLink;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\User;
14
use Chamilo\CoreBundle\Entity\Usergroup;
15
use Chamilo\CourseBundle\Entity\CGroup;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Gedmo\Sortable\Entity\Repository\SortableRepository;
18
19
/**
20
 * @template-extends SortableRepository<ResourceLink>
21
 */
22
class ResourceLinkRepository extends SortableRepository
23
{
24
    private array $toolList = [
25
        'course_description' => '/main/course_description/index.php',
26
        'document' => '/resources/document/%resource_node_id%/',
27
        'learnpath' => '/main/lp/lp_controller.php',
28
        'link' => '/resources/links/%resource_node_id%/',
29
        'quiz' => '/main/exercise/exercise.php',
30
        'announcement' => '/main/announcements/announcements.php',
31
        'glossary' => '/resources/glossary/%resource_node_id%/',
32
        'attendance' => '/main/attendance/index.php',
33
        'course_progress' => '/main/course_progress/index.php',
34
        'agenda' => '/resources/ccalendarevent',
35
        'forum' => '/main/forum/index.php',
36
        'student_publication' => '/resources/assignment/%resource_node_id%',
37
        'survey' => '/main/survey/survey_list.php',
38
        'notebook' => '/main/notebook/index.php',
39
    ];
40
41
    public function __construct(EntityManagerInterface $em)
42
    {
43
        parent::__construct($em, $em->getClassMetadata(ResourceLink::class));
44
    }
45
46
    public function remove(ResourceLink $resourceLink): void
47
    {
48
        $em = $this->getEntityManager();
49
50
        // To move the resource link at the end to reorder the list
51
        $resourceLink->setDisplayOrder(-1);
52
53
        $em->flush();
54
        // soft delete handled by Gedmo\SoftDeleteable
55
        $em->remove($resourceLink);
56
        $em->flush();
57
    }
58
59
    public function removeByResourceInContext(
60
        AbstractResource $resource,
61
        Course $course,
62
        ?Session $session = null,
63
        ?CGroup $group = null,
64
        ?Usergroup $usergroup = null,
65
        ?User $user = null,
66
    ): void {
67
        $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group, $usergroup, $user);
68
69
        if ($link) {
70
            $this->remove($link);
71
        }
72
    }
73
74
    /**
75
     * Retrieves the list of available tools filtered by a predefined tool list.
76
     *
77
     * @return array The list of tools with their IDs and titles.
78
     */
79
    public function getAvailableTools(): array
80
    {
81
        $queryBuilder = $this->_em->createQueryBuilder();
82
        $queryBuilder
83
            ->select('DISTINCT t.id, t.title')
84
            ->from('ChamiloCoreBundle:ResourceLink', 'rl')
85
            ->innerJoin('ChamiloCoreBundle:ResourceType', 'rt', 'WITH', 'rt.id = rl.resourceTypeGroup')
86
            ->innerJoin('ChamiloCoreBundle:Tool', 't', 'WITH', 't.id = rt.tool')
87
            ->where('rl.course IS NOT NULL')
88
            ->andWhere('t.title IN (:toolList)')
89
            ->setParameter('toolList', array_keys($this->toolList));
90
91
        $result = $queryBuilder->getQuery()->getArrayResult();
92
93
        $tools = [];
94
        foreach ($result as $row) {
95
            $tools[$row['id']] = ucfirst(str_replace('_', ' ', $row['title']));
96
        }
97
98
        return $tools;
99
    }
100
101
    /**
102
     * Retrieves a usage report of tools with dynamic links.
103
     *
104
     * @return array The tool usage data including counts, last update timestamps, and dynamic links.
105
     */
106
    public function getToolUsageReportByTools(array $toolIds): array
107
    {
108
        $queryBuilder = $this->_em->createQueryBuilder();
109
110
        $queryBuilder
111
            ->select(
112
                'COUNT(rl.id) AS resource_count',
113
                'IDENTITY(rl.course) AS course_id',
114
                'IDENTITY(rl.session) AS session_id',
115
                'IDENTITY(c.resourceNode) AS course_resource_node_id',
116
                't.title AS tool_name',
117
                'c.title AS course_name',
118
                's.title AS session_name',
119
                'MAX(rl.updatedAt) AS last_updated'
120
            )
121
            ->from('ChamiloCoreBundle:ResourceLink', 'rl')
122
            ->innerJoin('ChamiloCoreBundle:ResourceType', 'rt', 'WITH', 'rt.id = rl.resourceTypeGroup')
123
            ->innerJoin('ChamiloCoreBundle:Tool', 't', 'WITH', 't.id = rt.tool')
124
            ->innerJoin('ChamiloCoreBundle:Course', 'c', 'WITH', 'c.id = rl.course')
125
            ->leftJoin('ChamiloCoreBundle:Session', 's', 'WITH', 's.id = rl.session')
126
            ->where($queryBuilder->expr()->in('t.id', ':toolIds'))
127
            ->groupBy('rl.course, rl.session, t.title')
128
            ->orderBy('t.title', 'ASC')
129
            ->addOrderBy('c.title', 'ASC')
130
            ->addOrderBy('s.title', 'ASC')
131
            ->setParameter('toolIds', $toolIds);
132
133
        $result = $queryBuilder->getQuery()->getArrayResult();
134
135
        return array_map(function ($row) {
136
            $toolName = $row['tool_name'];
137
            $baseLink = $this->toolList[$toolName] ?? null;
138
            $link = '-';
139
            if ($baseLink) {
140
                $link = str_replace(
141
                    ['%resource_node_id%'],
142
                    [$row['course_resource_node_id']],
143
                    $baseLink
144
                );
145
146
                $queryParams = [
147
                    'cid' => $row['course_id'],
148
                ];
149
150
                if (!empty($row['session_id'])) {
151
                    $queryParams['sid'] = $row['session_id'];
152
                }
153
154
                $link .= '?' . http_build_query($queryParams);
155
            }
156
157
            return [
158
                'tool_name' => $toolName,
159
                'session_id' => $row['session_id'],
160
                'session_name' => $row['session_name'] ?: '-',
161
                'course_id' => $row['course_id'],
162
                'course_name' => $row['course_name'],
163
                'resource_count' => (int) $row['resource_count'],
164
                'last_updated' => $row['last_updated'] ?: '-',
165
                'link' => $link,
166
            ];
167
        }, $result);
168
    }
169
}
170