Passed
Pull Request — master (#6037)
by
unknown
07:33
created

getToolUsageReportByTools()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 64
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 48
nc 1
nop 1
dl 0
loc 64
rs 8.8234
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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