Passed
Pull Request — master (#6257)
by
unknown
08:47
created

getAssignmentSubmissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
nc 1
nop 5
dl 0
loc 32
rs 9.6333
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 = $row['commentsCount'] ?? 0;
41
42
            $item = json_decode($serializer->serialize($publication, 'json', [
43
                'groups' => ['student_publication:read']
44
            ]), true);
45
46
            $item['commentsCount'] = (int) $commentsCount;
47
48
            return $item;
49
        }, $assignments);
50
51
        return new JsonResponse([
52
            'hydra:member' => $data,
53
            'hydra:totalItems' => count($data),
54
        ]);
55
    }
56
57
    #[Route('/progress', name: 'chamilo_core_student_progress', methods: ['GET'])]
58
    public function getStudentProgress(
59
        SerializerInterface $serializer
60
    ): JsonResponse {
61
        $course = $this->cidReqHelper->getCourseEntity();
62
        $session = $this->cidReqHelper->getSessionEntity();
63
64
        $progressList = $this->studentPublicationRepo->findStudentProgressByCourse($course, $session);
65
66
        return new JsonResponse([
67
            'hydra:member' => $progressList,
68
            'hydra:totalItems' => count($progressList),
69
        ]);
70
    }
71
72
    #[Route('/{assignmentId}/submissions', name: 'chamilo_core_student_submission_list', methods: ['GET'])]
73
    public function getAssignmentSubmissions(
74
        int $assignmentId,
75
        Request $request,
76
        SerializerInterface $serializer,
77
        CStudentPublicationRepository $repo,
78
        Security $security
79
    ): JsonResponse {
80
        /* @var User $user */
81
        $user = $security->getUser();
82
83
        $page = (int) $request->query->get('page', 1);
84
        $itemsPerPage = (int) $request->query->get('itemsPerPage', 10);
85
        $order = $request->query->all('order');
86
87
        [$submissions, $total] = $repo->findAssignmentSubmissionsPaginated(
88
            $assignmentId,
89
            $user,
90
            $page,
91
            $itemsPerPage,
92
            $order
93
        );
94
95
        $data = json_decode($serializer->serialize(
96
            $submissions,
97
            'json',
98
            ['groups' => ['student_publication:read']]
99
        ), true);
100
101
        return new JsonResponse([
102
            'hydra:member' => $data,
103
            'hydra:totalItems' => $total,
104
        ]);
105
    }
106
107
    #[Route('/{assignmentId}/submissions/teacher', name: 'chamilo_core_teacher_submission_list', methods: ['GET'])]
108
    public function getAssignmentSubmissionsForTeacher(
109
        int $assignmentId,
110
        Request $request,
111
        SerializerInterface $serializer,
112
        CStudentPublicationRepository $repo
113
    ): JsonResponse {
114
        $page = (int) $request->query->get('page', 1);
115
        $itemsPerPage = (int) $request->query->get('itemsPerPage', 10);
116
        $order = $request->query->all('order');
117
118
        [$submissions, $total] = $repo->findAllSubmissionsByAssignment(
119
            $assignmentId,
120
            $page,
121
            $itemsPerPage,
122
            $order
123
        );
124
125
        $data = json_decode($serializer->serialize(
126
            $submissions,
127
            'json',
128
            ['groups' => ['student_publication:read']]
129
        ), true);
130
131
        return new JsonResponse([
132
            'hydra:member' => $data,
133
            'hydra:totalItems' => $total,
134
        ]);
135
    }
136
137
    #[Route('/submissions/{id}', name: 'chamilo_core_student_submission_delete', methods: ['DELETE'])]
138
    public function deleteSubmission(
139
        int $id,
140
        EntityManagerInterface $em,
141
        CStudentPublicationRepository $repo
142
    ): JsonResponse {
143
        $submission = $repo->find($id);
144
145
        if (!$submission) {
146
            return new JsonResponse(['error' => 'Submission not found.'], 404);
147
        }
148
149
        $this->denyAccessUnlessGranted('DELETE', $submission->getResourceNode());
150
151
        $em->remove($submission->getResourceNode());
152
        $em->flush();
153
154
        return new JsonResponse(null, 204);
155
    }
156
157
    #[Route('/submissions/{id}/edit', name: 'chamilo_core_student_submission_edit', methods: ['PATCH'])]
158
    public function editSubmission(
159
        int $id,
160
        Request $request,
161
        EntityManagerInterface $em,
162
        CStudentPublicationRepository $repo,
163
        MessageHelper $messageHelper
164
    ): JsonResponse {
165
        $submission = $repo->find($id);
166
167
        if (!$submission) {
168
            return new JsonResponse(['error' => 'Submission not found.'], 404);
169
        }
170
171
        $this->denyAccessUnlessGranted('EDIT', $submission->getResourceNode());
172
173
        $data = json_decode($request->getContent(), true);
174
        $title = $data['title'] ?? null;
175
        $description = $data['description'] ?? null;
176
        $sendMail = $data['sendMail'] ?? false;
177
178
        if ($title !== null) {
179
            $submission->setTitle($title);
180
        }
181
        if ($description !== null) {
182
            $submission->setDescription($description);
183
        }
184
185
        $em->flush();
186
187
        if ($sendMail) {
188
            $user = $submission->getUser();
189
            if ($user) {
190
                $messageSubject = sprintf('Feedback updated for "%s"', $submission->getTitle());
191
                $messageContent = sprintf(
192
                    'There is a new feedback update for your submission "%s". Please check it on the platform.',
193
                    $submission->getTitle()
194
                );
195
196
                $messageHelper->sendMessageSimple(
197
                    $user->getId(),
198
                    $messageSubject,
199
                    $messageContent,
200
                    $this->getUser()->getId()
201
                );
202
            }
203
        }
204
205
        return new JsonResponse(['success' => true]);
206
    }
207
}
208