|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Controller\Api; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Helpers\CidReqHelper; |
|
8
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
|
9
|
|
|
use Chamilo\CourseBundle\Repository\CDocumentRepository; |
|
10
|
|
|
use Chamilo\CourseBundle\Repository\CGroupRepository; |
|
11
|
|
|
use DocumentManager; |
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
#[AsController] |
|
18
|
|
|
class DocumentUsageAction extends AbstractController |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
private readonly CourseRepository $courseRepository, |
|
23
|
|
|
private readonly CDocumentRepository $documentRepository, |
|
24
|
|
|
private readonly CGroupRepository $groupRepository, |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function __invoke($cid): JsonResponse |
|
29
|
|
|
{ |
|
30
|
|
|
$courseId = (int) $cid; |
|
31
|
|
|
|
|
32
|
|
|
$courseEntity = $this->courseRepository->find($courseId); |
|
33
|
|
|
if (!$courseEntity) { |
|
34
|
|
|
return new JsonResponse(['error' => 'Course not found'], 404); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$totalQuotaBytes = ($courseEntity->getDiskQuota() * 1024 * 1024) ?? DEFAULT_DOCUMENT_QUOTA; |
|
38
|
|
|
$usedQuotaBytes = $this->documentRepository->getTotalSpaceByCourse($courseEntity); |
|
39
|
|
|
|
|
40
|
|
|
$chartData = []; |
|
41
|
|
|
|
|
42
|
|
|
// Process sessions |
|
43
|
|
|
$this->processCourseSessions($courseEntity, $totalQuotaBytes, $usedQuotaBytes, $chartData); |
|
44
|
|
|
|
|
45
|
|
|
// Process groups |
|
46
|
|
|
$this->processCourseGroups($courseEntity, $totalQuotaBytes, $usedQuotaBytes, $chartData); |
|
47
|
|
|
|
|
48
|
|
|
// Process user documents |
|
49
|
|
|
$users = $this->courseRepository->getUsersByCourse($courseEntity); |
|
50
|
|
|
foreach ($users as $user) { |
|
51
|
|
|
$this->processUserDocuments($courseEntity, $user, $totalQuotaBytes, $chartData); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Add available space |
|
55
|
|
|
$availableBytes = $totalQuotaBytes - $usedQuotaBytes; |
|
56
|
|
|
$availablePercentage = $this->calculatePercentage($availableBytes, $totalQuotaBytes); |
|
57
|
|
|
|
|
58
|
|
|
$chartData[] = [ |
|
59
|
|
|
'label' => addslashes(get_lang('Available space')) . ' (' . format_file_size($availableBytes) . ')', |
|
60
|
|
|
'percentage' => $availablePercentage, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
return new JsonResponse([ |
|
64
|
|
|
'datasets' => [ |
|
65
|
|
|
['data' => array_column($chartData, 'percentage')], |
|
66
|
|
|
], |
|
67
|
|
|
'labels' => array_column($chartData, 'label'), |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function processCourseSessions($courseEntity, int $totalQuotaBytes, int &$usedQuotaBytes, array &$chartData): void |
|
72
|
|
|
{ |
|
73
|
|
|
foreach ($courseEntity->getSessions() as $sessionRel) { |
|
74
|
|
|
$session = $sessionRel->getSession(); |
|
75
|
|
|
$quotaBytes = $this->documentRepository->getTotalSpaceByCourse($courseEntity, null, $session); |
|
76
|
|
|
|
|
77
|
|
|
if ($quotaBytes > 0) { |
|
78
|
|
|
$usedQuotaBytes += $quotaBytes; |
|
79
|
|
|
$chartData[] = [ |
|
80
|
|
|
'label' => addslashes(get_lang('Session') . ': ' . $session->getTitle()) . ' (' . format_file_size($quotaBytes) . ')', |
|
81
|
|
|
'percentage' => $this->calculatePercentage($quotaBytes, $totalQuotaBytes), |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function processCourseGroups($courseEntity, int $totalQuotaBytes, int &$usedQuotaBytes, array &$chartData): void |
|
88
|
|
|
{ |
|
89
|
|
|
$groupsList = $this->groupRepository->findAllByCourse($courseEntity)->getQuery()->getResult(); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($groupsList as $groupEntity) { |
|
92
|
|
|
$quotaBytes = $this->documentRepository->getTotalSpaceByCourse($courseEntity, $groupEntity->getIid()); |
|
93
|
|
|
|
|
94
|
|
|
if ($quotaBytes > 0) { |
|
95
|
|
|
$usedQuotaBytes += $quotaBytes; |
|
96
|
|
|
$chartData[] = [ |
|
97
|
|
|
'label' => addslashes(get_lang('Group') . ': ' . $groupEntity->getTitle() . ' (' . format_file_size($quotaBytes) . ')'), |
|
98
|
|
|
'percentage' => $this->calculatePercentage($quotaBytes, $totalQuotaBytes), |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function processUserDocuments($courseEntity, $user, int $totalQuotaBytes, array &$chartData): void |
|
105
|
|
|
{ |
|
106
|
|
|
$documentsList = $this->documentRepository->getAllDocumentDataByUserAndGroup($courseEntity); |
|
107
|
|
|
$userQuotaBytes = 0; |
|
108
|
|
|
|
|
109
|
|
|
foreach ($documentsList as $documentEntity) { |
|
110
|
|
|
if ($documentEntity->getResourceNode()->getCreator()?->getId() === $user->getId() |
|
111
|
|
|
&& $documentEntity->getFiletype() === 'file') { |
|
112
|
|
|
$resourceFiles = $documentEntity->getResourceNode()->getResourceFiles(); |
|
113
|
|
|
if (!$resourceFiles->isEmpty()) { |
|
114
|
|
|
$userQuotaBytes += $resourceFiles->first()->getSize(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($userQuotaBytes > 0) { |
|
120
|
|
|
$chartData[] = [ |
|
121
|
|
|
'label' => addslashes(get_lang('Teacher') . ': ' . $user->getFullName()) . ' (' . format_file_size($userQuotaBytes) . ')', |
|
122
|
|
|
'percentage' => $this->calculatePercentage($userQuotaBytes, $totalQuotaBytes), |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function calculatePercentage(int $bytes, int $totalBytes): float |
|
128
|
|
|
{ |
|
129
|
|
|
if ($totalBytes === 0) { |
|
130
|
|
|
return 0.0; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return round(($bytes / $totalBytes) * 100, 2); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|