Passed
Pull Request — master (#6257)
by
unknown
09:03
created

StudentPublicationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\ServiceHelper\CidReqHelper;
11
use Chamilo\CoreBundle\ServiceHelper\MessageHelper;
12
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15
use Symfony\Bundle\SecurityBundle\Security;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\Annotation\Route;
19
use Symfony\Component\Serializer\SerializerInterface;
20
21
#[Route('/assignments')]
22
class StudentPublicationController extends AbstractController
23
{
24
    public function __construct(
25
        private readonly CStudentPublicationRepository $studentPublicationRepo,
26
        private readonly CidReqHelper $cidReqHelper
27
    ) {}
28
29
30
    #[Route('/student', name: 'chamilo_core_assignment_student_list', methods: ['GET'])]
31
    public function getStudentAssignments(SerializerInterface $serializer): JsonResponse
32
    {
33
        $course = $this->cidReqHelper->getCourseEntity();
34
        $session = $this->cidReqHelper->getSessionEntity();
35
36
        $assignments = $this->studentPublicationRepo->findVisibleAssignmentsForStudent($course, $session);
37
38
        $data = array_map(function ($row) use ($serializer) {
39
            $publication = $row[0] ?? null;
40
            $commentsCount = (int) ($row['commentsCount'] ?? 0);
41
            $correctionsCount = (int) ($row['correctionsCount'] ?? 0);
42
            $lastUpload = $row['lastUpload'] ?? null;
43
44
            $item = json_decode($serializer->serialize($publication, 'json', [
45
                'groups' => ['student_publication:read'],
46
            ]), true);
47
48
            $item['commentsCount'] = $commentsCount;
49
            $item['feedbackCount'] = $correctionsCount;
50
            $item['lastUpload'] = $lastUpload;
51
52
            return $item;
53
        }, $assignments);
54
55
        return new JsonResponse([
56
            'hydra:member' => $data,
57
            'hydra:totalItems' => count($data),
58
        ]);
59
    }
60
61
    #[Route('/progress', name: 'chamilo_core_student_progress', methods: ['GET'])]
62
    public function getStudentProgress(
63
        SerializerInterface $serializer
64
    ): JsonResponse {
65
        $course = $this->cidReqHelper->getCourseEntity();
66
        $session = $this->cidReqHelper->getSessionEntity();
67
68
        $progressList = $this->studentPublicationRepo->findStudentProgressByCourse($course, $session);
69
70
        return new JsonResponse([
71
            'hydra:member' => $progressList,
72
            'hydra:totalItems' => count($progressList),
73
        ]);
74
    }
75
76
    #[Route('/{assignmentId}/submissions', name: 'chamilo_core_student_submission_list', methods: ['GET'])]
77
    public function getAssignmentSubmissions(
78
        int $assignmentId,
79
        Request $request,
80
        SerializerInterface $serializer,
81
        CStudentPublicationRepository $repo,
82
        Security $security
83
    ): JsonResponse {
84
        /* @var User $user */
85
        $user = $security->getUser();
86
87
        $page = (int) $request->query->get('page', 1);
88
        $itemsPerPage = (int) $request->query->get('itemsPerPage', 10);
89
        $order = $request->query->all('order');
90
91
        [$submissions, $total] = $repo->findAssignmentSubmissionsPaginated(
92
            $assignmentId,
93
            $user,
94
            $page,
95
            $itemsPerPage,
96
            $order
97
        );
98
99
        $data = json_decode($serializer->serialize(
100
            $submissions,
101
            'json',
102
            ['groups' => ['student_publication:read']]
103
        ), true);
104
105
        return new JsonResponse([
106
            'hydra:member' => $data,
107
            'hydra:totalItems' => $total,
108
        ]);
109
    }
110
111
    #[Route('/{assignmentId}/submissions/teacher', name: 'chamilo_core_teacher_submission_list', methods: ['GET'])]
112
    public function getAssignmentSubmissionsForTeacher(
113
        int $assignmentId,
114
        Request $request,
115
        SerializerInterface $serializer,
116
        CStudentPublicationRepository $repo
117
    ): JsonResponse {
118
        $page = (int) $request->query->get('page', 1);
119
        $itemsPerPage = (int) $request->query->get('itemsPerPage', 10);
120
        $order = $request->query->all('order');
121
122
        [$submissions, $total] = $repo->findAllSubmissionsByAssignment(
123
            $assignmentId,
124
            $page,
125
            $itemsPerPage,
126
            $order
127
        );
128
129
        $data = json_decode($serializer->serialize(
130
            $submissions,
131
            'json',
132
            ['groups' => ['student_publication:read']]
133
        ), true);
134
135
        return new JsonResponse([
136
            'hydra:member' => $data,
137
            'hydra:totalItems' => $total,
138
        ]);
139
    }
140
141
    #[Route('/submissions/{id}', name: 'chamilo_core_student_submission_delete', methods: ['DELETE'])]
142
    public function deleteSubmission(
143
        int $id,
144
        EntityManagerInterface $em,
145
        CStudentPublicationRepository $repo
146
    ): JsonResponse {
147
        $submission = $repo->find($id);
148
149
        if (!$submission) {
150
            return new JsonResponse(['error' => 'Submission not found.'], 404);
151
        }
152
153
        $this->denyAccessUnlessGranted('DELETE', $submission->getResourceNode());
154
155
        $em->remove($submission->getResourceNode());
156
        $em->flush();
157
158
        return new JsonResponse(null, 204);
159
    }
160
161
    #[Route('/submissions/{id}/edit', name: 'chamilo_core_student_submission_edit', methods: ['PATCH'])]
162
    public function editSubmission(
163
        int $id,
164
        Request $request,
165
        EntityManagerInterface $em,
166
        CStudentPublicationRepository $repo,
167
        MessageHelper $messageHelper
168
    ): JsonResponse {
169
        $submission = $repo->find($id);
170
171
        if (!$submission) {
172
            return new JsonResponse(['error' => 'Submission not found.'], 404);
173
        }
174
175
        $this->denyAccessUnlessGranted('EDIT', $submission->getResourceNode());
176
177
        $data = json_decode($request->getContent(), true);
178
        $title = $data['title'] ?? null;
179
        $description = $data['description'] ?? null;
180
        $sendMail = $data['sendMail'] ?? false;
181
182
        if ($title !== null) {
183
            $submission->setTitle($title);
184
        }
185
        if ($description !== null) {
186
            $submission->setDescription($description);
187
        }
188
189
        $em->flush();
190
191
        if ($sendMail) {
192
            $user = $submission->getUser();
193
            if ($user) {
194
                $messageSubject = sprintf('Feedback updated for "%s"', $submission->getTitle());
195
                $messageContent = sprintf(
196
                    'There is a new feedback update for your submission "%s". Please check it on the platform.',
197
                    $submission->getTitle()
198
                );
199
200
                $messageHelper->sendMessageSimple(
201
                    $user->getId(),
202
                    $messageSubject,
203
                    $messageContent,
204
                    $this->getUser()->getId()
205
                );
206
            }
207
        }
208
209
        return new JsonResponse(['success' => true]);
210
    }
211
212
    #[Route('/submissions/{id}/move', name: 'chamilo_core_student_submission_move', methods: ['PATCH'])]
213
    public function moveSubmission(
214
        int $id,
215
        Request $request,
216
        EntityManagerInterface $em,
217
        CStudentPublicationRepository $repo
218
    ): JsonResponse {
219
        $submission = $repo->find($id);
220
221
        if (!$submission) {
222
            return new JsonResponse(['error' => 'Submission not found.'], 404);
223
        }
224
225
        $this->denyAccessUnlessGranted('EDIT', $submission->getResourceNode());
226
227
        $data = json_decode($request->getContent(), true);
228
        $newAssignmentId = $data['newAssignmentId'] ?? null;
229
230
        if (!$newAssignmentId) {
231
            return new JsonResponse(['error' => 'New assignment ID is required.'], 400);
232
        }
233
234
        $newParent = $repo->find($newAssignmentId);
235
236
        if (!$newParent) {
237
            return new JsonResponse(['error' => 'Target assignment not found.'], 404);
238
        }
239
240
        $submission->setPublicationParent($newParent);
241
242
        $em->flush();
243
244
        return new JsonResponse(['success' => true]);
245
    }
246
}
247